[RFC] mips: Add Allegrex VFPU vector arithmetic and auto-vectorization

Message ID 20261104211941.605562-1-dublikunt@gmail.com
State New
Headers
Series [RFC] mips: Add Allegrex VFPU vector arithmetic and auto-vectorization |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm warning Skipped because it is an RFC
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 warning Skipped because it is an RFC
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap warning Skipped because it is an RFC
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap warning Skipped because it is an RFC

Commit Message

Yaroslav Solodovnyk Nov. 4, 2026, 9:19 p.m. UTC
  Add opt-in -mvfpu support for little-endian Allegrex with the o32 or
32-bit EABI, hard float and single precision. Model the 32 nonoverlapping
quad columns using COP2 registers, preserving the existing argument and
return conventions. All VFPU columns are caller-clobbered.

Under fast-math, expose V4SF addition, subtraction and multiplication to
the vectorizer. Reset prefixes before arithmetic and register moves;
keep each sequence out of branch delay slots. Use quad memory operations
for 16-byte-aligned data, scalar lane operations for word-aligned data,
and aligned temporaries for packed accesses. Preserve all four words in
GPR moves and support transfers between GPR groups and VFPU columns.

Share V4SF expanders with MSA while retaining the existing MSA operation
semantics. Add compile coverage for arithmetic, strict/disabled modes,
unaligned memory, packed vectors, ABI transfers, calls and diagnostics.

Tested with an x86_64-pc-linux-gnu host and mipsel-elf target. Cross-built
C, C++ and libgcc. The 280 VFPU checks pass; all 7,374 MSA passes and
13 unsupported cases match unmodified trunk. MSA tests use freestanding
mode because this cross build has no libc headers. Info and DVI builds
also pass.

The EABI32 kernels pass all nine run-time cases in each of PPSSPP's JIT,
interpreter and IR interpreter, and on a physical PSP with firmware 6.61
through PSPLINK 3.2.1. The generated kernels use the compiler under test;
SDK startup and reference code use the installed PSP GCC 15.2.0 toolchain.

An all-default-language bootstrap and complete testsuite run have not
been performed. This is an RFC for back-end design and implementation
review while that validation remains outstanding.

gcc/ChangeLog:

	* config/mips/mips-msa.md (MSA_MOVE): New mode iterator.
	(mov<mode>, movmisalign<mode>): Use MSA_MOVE.
	(add<mode>3, sub<mode>3, mul<mode>3): Share V4SF patterns with VFPU.
	(addv2df3, subv2df3, mulv2df3): Define the V2DF patterns separately.
	* config/mips/mips-protos.h (mips_output_vfpu_move,
	mips_vfpu_math_p): Declare.
	* config/mips/mips-vfpu.md: New file.
	* config/mips/mips.cc (mips_insert_attributes): Reject compressed
	functions with VFPU.
	(mips_symbol_insns, mips_valid_offset_p,
	mips_classify_address, mips_const_insns): Handle VFPU addressing and
	constants.
	(mips_legitimize_move, mips_split_move_p,
	mips_split_move): Handle VFPU, packed and four-word GPR moves.
	(mips_output_vfpu_move, mips_vfpu_math_p): New functions.
	(mips_print_operand): Add VFPU quad register formatting.
	(mips_hard_regno_mode_ok_uncached,
	mips_hard_regno_nregs, mips_class_max_nregs,
	mips_can_change_mode_class, mips_modes_tieable_p): Handle VFPU column
	registers.
	(mips_vector_mode_supported_p, mips_preferred_simd_mode): Support
	VFPU vectors.
	(mips_preferred_vector_alignment,
	mips_builtin_vectorization_cost): New functions. Install target
	hooks.
	(mips_option_override): Validate VFPU options.
	(mips_conditional_register_usage): Enable caller-clobbered VFPU
	columns.
	(TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT): Define.
	(TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST): Likewise.
	* config/mips/mips.md: Include mips-vfpu.md.
	* config/mips/mips.opt (mvfpu): New option.
	* doc/invoke.texi (MIPS Options): Document -mvfpu.

gcc/testsuite/ChangeLog:

	* gcc.target/mips/allegrex-vfpu-abi-eabi.c: New test.
	* gcc.target/mips/allegrex-vfpu-abi.c: New test.
	* gcc.target/mips/allegrex-vfpu-add.c: New test.
	* gcc.target/mips/allegrex-vfpu-call-eabi.c: New test.
	* gcc.target/mips/allegrex-vfpu-call.c: New test.
	* gcc.target/mips/allegrex-vfpu-delay.c: New test.
	* gcc.target/mips/allegrex-vfpu-disabled.c: New test.
	* gcc.target/mips/allegrex-vfpu-error-arch.c: New test.
	* gcc.target/mips/allegrex-vfpu-error-compressed.c: New test.
	* gcc.target/mips/allegrex-vfpu-error-endian.c: New test.
	* gcc.target/mips/allegrex-vfpu-error-float.c: New test.
	* gcc.target/mips/allegrex-vfpu-move.c: New test.
	* gcc.target/mips/allegrex-vfpu-mul.c: New test.
	* gcc.target/mips/allegrex-vfpu-packed.c: New test.
	* gcc.target/mips/allegrex-vfpu-return.c: New test.
	* gcc.target/mips/allegrex-vfpu-strict.c: New test.
	* gcc.target/mips/allegrex-vfpu-sub.c: New test.
	* gcc.target/mips/allegrex-vfpu-unaligned.c: New test.
	* gcc.target/mips/mips.exp (mips_option_groups): Recognize -mvfpu.

Signed-off-by: Yaroslav Solodovnyk <dublikunt@gmail.com>
---
 gcc/config/mips/mips-msa.md                   |  48 ++--
 gcc/config/mips/mips-protos.h                 |   2 +
 gcc/config/mips/mips-vfpu.md                  | 102 +++++++
 gcc/config/mips/mips.cc                       | 248 +++++++++++++++++-
 gcc/config/mips/mips.md                       |   2 +
 gcc/config/mips/mips.opt                      |   4 +
 gcc/doc/invoke.texi                           |  20 +-
 .../gcc.target/mips/allegrex-vfpu-abi-eabi.c  |  21 ++
 .../gcc.target/mips/allegrex-vfpu-abi.c       |  21 ++
 .../gcc.target/mips/allegrex-vfpu-add.c       |  18 ++
 .../gcc.target/mips/allegrex-vfpu-call-eabi.c |  19 ++
 .../gcc.target/mips/allegrex-vfpu-call.c      |  19 ++
 .../gcc.target/mips/allegrex-vfpu-delay.c     |  18 ++
 .../gcc.target/mips/allegrex-vfpu-disabled.c  |  30 +++
 .../mips/allegrex-vfpu-error-arch.c           |   5 +
 .../mips/allegrex-vfpu-error-compressed.c     |   5 +
 .../mips/allegrex-vfpu-error-endian.c         |   5 +
 .../mips/allegrex-vfpu-error-float.c          |   5 +
 .../gcc.target/mips/allegrex-vfpu-move.c      |  24 ++
 .../gcc.target/mips/allegrex-vfpu-mul.c       |  18 ++
 .../gcc.target/mips/allegrex-vfpu-packed.c    |  18 ++
 .../gcc.target/mips/allegrex-vfpu-return.c    |  24 ++
 .../gcc.target/mips/allegrex-vfpu-strict.c    |  30 +++
 .../gcc.target/mips/allegrex-vfpu-sub.c       |  18 ++
 .../gcc.target/mips/allegrex-vfpu-unaligned.c |  15 ++
 gcc/testsuite/gcc.target/mips/mips.exp        |   1 +
 26 files changed, 710 insertions(+), 30 deletions(-)
 create mode 100644 gcc/config/mips/mips-vfpu.md
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-abi-eabi.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-abi.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-add.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-call-eabi.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-call.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-delay.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-disabled.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-arch.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-compressed.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-endian.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-float.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-move.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-mul.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-packed.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-return.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-strict.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-sub.c
 create mode 100644 gcc/testsuite/gcc.target/mips/allegrex-vfpu-unaligned.c


base-commit: 52f07783f7c238e39a83fed66e9a9749b816146c
  

Patch

diff --git a/gcc/config/mips/mips-msa.md b/gcc/config/mips/mips-msa.md
index decf98d4688..3a479b2a0a8 100644
--- a/gcc/config/mips/mips-msa.md
+++ b/gcc/config/mips/mips-msa.md
@@ -95,6 +95,9 @@ 
 ;; All vector modes with 128 bits.
 (define_mode_iterator MSA      [V2DF V4SF V2DI V4SI V8HI V16QI])
 
+;; The V4SF move expander is shared by MSA and Allegrex VFPU.
+(define_mode_iterator MSA_MOVE [V2DF V2DI V4SI V8HI V16QI])
+
 ;; Same as MSA.  Used by vcond to iterate two modes.
 (define_mode_iterator MSA_2    [V2DF V4SF V2DI V4SI V8HI V16QI])
 
@@ -657,8 +660,8 @@ 
    (set_attr "mode" "<MODE>")])
 
 (define_expand "mov<mode>"
-  [(set (match_operand:MSA 0)
-	(match_operand:MSA 1))]
+  [(set (match_operand:MSA_MOVE 0)
+	(match_operand:MSA_MOVE 1))]
   "ISA_HAS_MSA"
 {
   if (mips_legitimize_move (<MODE>mode, operands[0], operands[1]))
@@ -666,8 +669,8 @@ 
 })
 
 (define_expand "movmisalign<mode>"
-  [(set (match_operand:MSA 0)
-	(match_operand:MSA 1))]
+  [(set (match_operand:MSA_MOVE 0)
+	(match_operand:MSA_MOVE 1))]
   "ISA_HAS_MSA"
 {
   if (mips_legitimize_move (<MODE>mode, operands[0], operands[1]))
@@ -935,32 +938,33 @@ 
    (set_attr "mode" "<MODE>")])
 
 ;; Floating-point operations
-(define_insn "add<mode>3"
-  [(set (match_operand:FMSA 0 "register_operand" "=f")
-	(plus:FMSA (match_operand:FMSA 1 "register_operand" "f")
-		   (match_operand:FMSA 2 "register_operand" "f")))]
+;; V4SF addition, subtraction and multiplication are shared with Allegrex.
+(define_insn "addv2df3"
+  [(set (match_operand:V2DF 0 "register_operand" "=f")
+	(plus:V2DF (match_operand:V2DF 1 "register_operand" "f")
+		   (match_operand:V2DF 2 "register_operand" "f")))]
   "ISA_HAS_MSA"
-  "fadd.<msafmt>\t%w0,%w1,%w2"
+  "fadd.d\t%w0,%w1,%w2"
   [(set_attr "type" "simd_fadd")
-   (set_attr "mode" "<MODE>")])
+   (set_attr "mode" "V2DF")])
 
-(define_insn "sub<mode>3"
-  [(set (match_operand:FMSA 0 "register_operand" "=f")
-	(minus:FMSA (match_operand:FMSA 1 "register_operand" "f")
-		    (match_operand:FMSA 2 "register_operand" "f")))]
+(define_insn "subv2df3"
+  [(set (match_operand:V2DF 0 "register_operand" "=f")
+	(minus:V2DF (match_operand:V2DF 1 "register_operand" "f")
+		    (match_operand:V2DF 2 "register_operand" "f")))]
   "ISA_HAS_MSA"
-  "fsub.<msafmt>\t%w0,%w1,%w2"
+  "fsub.d\t%w0,%w1,%w2"
   [(set_attr "type" "simd_fadd")
-   (set_attr "mode" "<MODE>")])
+   (set_attr "mode" "V2DF")])
 
-(define_insn "mul<mode>3"
-  [(set (match_operand:FMSA 0 "register_operand" "=f")
-	(mult:FMSA (match_operand:FMSA 1 "register_operand" "f")
-		   (match_operand:FMSA 2 "register_operand" "f")))]
+(define_insn "mulv2df3"
+  [(set (match_operand:V2DF 0 "register_operand" "=f")
+	(mult:V2DF (match_operand:V2DF 1 "register_operand" "f")
+		   (match_operand:V2DF 2 "register_operand" "f")))]
   "ISA_HAS_MSA"
-  "fmul.<msafmt>\t%w0,%w1,%w2"
+  "fmul.d\t%w0,%w1,%w2"
   [(set_attr "type" "simd_fmul")
-   (set_attr "mode" "<MODE>")])
+   (set_attr "mode" "V2DF")])
 
 (define_insn "div<mode>3"
   [(set (match_operand:FMSA 0 "register_operand" "=f")
diff --git a/gcc/config/mips/mips-protos.h b/gcc/config/mips/mips-protos.h
index d02d60c38e0..862524c31b2 100644
--- a/gcc/config/mips/mips-protos.h
+++ b/gcc/config/mips/mips-protos.h
@@ -223,6 +223,8 @@  extern void mips_split_msa_copy_d (rtx, rtx, rtx, rtx (*)(rtx, rtx, rtx));
 extern void mips_split_msa_insert_d (rtx, rtx, rtx, rtx);
 extern void mips_split_msa_fill_d (rtx, rtx);
 extern const char *mips_output_move (rtx, rtx);
+extern const char *mips_output_vfpu_move (rtx, rtx);
+extern bool mips_vfpu_math_p (void);
 extern bool mips_cfun_has_cprestore_slot_p (void);
 extern bool mips_cprestore_address_p (rtx, bool);
 extern void mips_save_gp_to_cprestore_slot (rtx, rtx, rtx, rtx);
diff --git a/gcc/config/mips/mips-vfpu.md b/gcc/config/mips/mips-vfpu.md
new file mode 100644
index 00000000000..fba9ee5eb78
--- /dev/null
+++ b/gcc/config/mips/mips-vfpu.md
@@ -0,0 +1,102 @@ 
+;; Allegrex VFPU quad-vector arithmetic and moves.
+;; Copyright (C) 2026 Free Software Foundation, Inc.
+;;
+;; This file is part of GCC.
+;;
+;; GCC is free software; you can redistribute it and/or modify it under
+;; the terms of the GNU General Public License as published by the Free
+;; Software Foundation; either version 3, or (at your option) any later
+;; version.
+;;
+;; GCC is distributed in the hope that it will be useful, but WITHOUT
+;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
+;; License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with GCC; see the file COPYING3.  If not see
+;; <https://www.gnu.org/licenses/>.
+
+(define_code_iterator VFPU_BINARY [plus minus mult])
+(define_code_attr vfpu_op [(plus "add") (minus "sub") (mult "mul")])
+(define_code_attr vfpu_type
+  [(plus "simd_fadd") (minus "simd_fadd") (mult "simd_fmul")])
+
+;; Keep the canonical V4SF optabs shared with the existing MSA backend.
+(define_expand "<vfpu_op>v4sf3"
+  [(set (match_operand:V4SF 0 "register_operand")
+	(VFPU_BINARY:V4SF (match_operand:V4SF 1 "register_operand")
+		   (match_operand:V4SF 2 "register_operand")))]
+  "ISA_HAS_MSA || mips_vfpu_math_p ()"
+  "")
+
+(define_insn "*msa_<vfpu_op>v4sf3"
+  [(set (match_operand:V4SF 0 "register_operand" "=f")
+	(VFPU_BINARY:V4SF (match_operand:V4SF 1 "register_operand" "f")
+		   (match_operand:V4SF 2 "register_operand" "f")))]
+  "ISA_HAS_MSA"
+  "f<vfpu_op>.w\t%w0,%w1,%w2"
+  [(set_attr "type" "<vfpu_type>")
+   (set_attr "mode" "V4SF")])
+
+;; 'C' is the existing MIPS COP2 register constraint; %P prints Cxyz.
+;; The identity prefixes and arithmetic must stay out of delay slots.
+(define_insn "*vfpu_<vfpu_op>v4sf3"
+  [(set (match_operand:V4SF 0 "register_operand" "=C")
+	(VFPU_BINARY:V4SF (match_operand:V4SF 1 "register_operand" "C")
+		   (match_operand:V4SF 2 "register_operand" "C")))]
+  "mips_vfpu_math_p ()"
+  "vpfxs\tx,y,z,w\n\tvpfxt\tx,y,z,w\n\tvpfxd\t,,,\n\tv<vfpu_op>.q\t%P0,%P1,%P2"
+  [(set_attr "type" "<vfpu_type>")
+   (set_attr "mode" "V4SF")
+   (set_attr "insn_count" "4")])
+
+;; The standard move expander already exists for MSA; its V4SF instance
+;; is excluded by MSA_MOVE, so this can serve both target implementations.
+(define_expand "movv4sf"
+  [(set (match_operand:V4SF 0)
+	(match_operand:V4SF 1))]
+  "ISA_HAS_MSA || TARGET_ALLEGREX_VFPU"
+{
+  if (mips_legitimize_move (V4SFmode, operands[0], operands[1]))
+    DONE;
+})
+
+(define_expand "movmisalignv4sf"
+  [(set (match_operand:V4SF 0)
+	(match_operand:V4SF 1))]
+  "ISA_HAS_MSA || TARGET_ALLEGREX_VFPU"
+{
+  if (mips_legitimize_move (V4SFmode, operands[0], operands[1]))
+    DONE;
+})
+
+;; The existing MSA V4SF move recognizer is still in mips-msa.md.
+;; COP2 is selected only when Allegrex VFPU is enabled.
+;; A quad move or transfer to o32 GPRs can require four instructions.
+(define_insn "*vfpu_movv4sf"
+  [(set (match_operand:V4SF 0 "nonimmediate_operand" "=C,C,m,C,d,d,d,m")
+	(match_operand:V4SF 1 "nonimmediate_operand" "C,m,C,d,C,d,m,d"))]
+  "TARGET_ALLEGREX_VFPU"
+  { return mips_output_vfpu_move (operands[0], operands[1]); }
+  [(set (attr "type")
+	(cond [(eq_attr "alternative" "1") (const_string "simd_load")
+	       (eq_attr "alternative" "2") (const_string "simd_store")
+	       (eq_attr "alternative" "5") (const_string "move")
+	       (eq_attr "alternative" "6") (const_string "load")
+	       (eq_attr "alternative" "7") (const_string "store")]
+	      (const_string "simd_move")))
+   (set_attr "mode" "V4SF")
+   (set_attr "insn_count" "4")])
+
+;; Ordinary o32 arguments and returns still use groups of GPRs.
+(define_split
+  [(set (match_operand:V4SF 0 "nonimmediate_operand")
+	(match_operand:V4SF 1 "nonimmediate_operand"))]
+  "TARGET_ALLEGREX_VFPU && reload_completed
+   && mips_split_move_insn_p (operands[0], operands[1], insn)"
+  [(const_int 0)]
+{
+  mips_split_move_insn (operands[0], operands[1], curr_insn);
+  DONE;
+})
diff --git a/gcc/config/mips/mips.cc b/gcc/config/mips/mips.cc
index 1cf3a1ab9b9..9ef6d6d3622 100644
--- a/gcc/config/mips/mips.cc
+++ b/gcc/config/mips/mips.cc
@@ -1531,6 +1531,10 @@  mips_insert_attributes (tree decl, tree *attributes)
       nocompression_flags |=
 	mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
 
+      if (TARGET_ALLEGREX_VFPU && compression_flags)
+	error ("%qs attribute is incompatible with %<-mvfpu%>",
+	       mips_get_compress_on_name (compression_flags));
+
       if (compression_flags && nocompression_flags)
 	error ("%qE cannot have both %qs and %qs attributes",
 	       DECL_NAME (decl), mips_get_compress_on_name (compression_flags),
@@ -2544,9 +2548,10 @@  mips_symbol_insns_1 (enum mips_symbol_type type, machine_mode mode)
 static int
 mips_symbol_insns (enum mips_symbol_type type, machine_mode mode)
 {
-  /* MSA LD.* and ST.* cannot support loading symbols via an immediate
-     operand.  */
-  if (mode != MAX_MACHINE_MODE && MSA_SUPPORTED_MODE_P (mode))
+  /* MSA and VFPU vector loads and stores require a base register.  */
+  if (mode != MAX_MACHINE_MODE
+      && (MSA_SUPPORTED_MODE_P (mode)
+	  || (TARGET_ALLEGREX_VFPU && mode == V4SFmode)))
     return 0;
 
   return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
@@ -2672,6 +2677,12 @@  mips_valid_offset_p (rtx x, machine_mode mode)
       && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
     return false;
 
+  /* The VFPU memory formats encode offsets in multiples of four.
+     Leave room for the last scalar element of an unaligned quad move.  */
+  if (TARGET_ALLEGREX_VFPU && mode == V4SFmode
+      && (INTVAL (x) & 3) != 0)
+    return false;
+
   /* MSA LD.* and ST.* supports 10-bit signed offsets.  */
   if (MSA_SUPPORTED_MODE_P (mode)
       && !mips_signed_immediate_p (INTVAL (x), 10,
@@ -2757,7 +2768,8 @@  mips_classify_address (struct mips_address_info *info, rtx x,
       /* Small-integer addresses don't occur very often, but they
 	 are legitimate if $0 is a valid base register.  */
       info->type = ADDRESS_CONST_INT;
-      return !TARGET_MIPS16 && SMALL_INT (x);
+      return (!TARGET_MIPS16 && SMALL_INT (x)
+	      && !(TARGET_ALLEGREX_VFPU && mode == V4SFmode));
 
     case CONST:
     case LABEL_REF:
@@ -3054,6 +3066,8 @@  mips_const_insns (rtx x)
       return mips_build_integer (codes, INTVAL (x));
 
     case CONST_VECTOR:
+      if (TARGET_ALLEGREX_VFPU && GET_MODE (x) == V4SFmode)
+	return 0;
       if (MSA_SUPPORTED_MODE_P (GET_MODE (x))
 	  && mips_const_vector_same_int_p (x, GET_MODE (x), -512, 511))
 	return 1;
@@ -3905,14 +3919,35 @@  mips_legitimize_const_move (machine_mode mode, rtx dest, rtx src)
 bool
 mips_legitimize_move (machine_mode mode, rtx dest, rtx src)
 {
+  /* Packed vectors must not reach the VFPU memory instructions.  Copy
+     their representation through an aligned temporary instead.  */
+  if (TARGET_ALLEGREX_VFPU && mode == V4SFmode
+      && ((MEM_P (src) && MEM_ALIGN (src) < 32)
+	  || (MEM_P (dest) && MEM_ALIGN (dest) < 32)))
+    {
+      rtx temp = assign_stack_temp (mode, GET_MODE_SIZE (mode));
+      if (MEM_P (src) && MEM_ALIGN (src) < 32)
+	{
+	  emit_block_move (temp, src, GEN_INT (16), BLOCK_OP_NORMAL);
+	  emit_move_insn (dest, temp);
+	}
+      else
+	{
+	  emit_move_insn (temp, src);
+	  emit_block_move (dest, temp, GEN_INT (16), BLOCK_OP_NORMAL);
+	}
+      return true;
+    }
+
   /* Both src and dest are non-registers;  one special case is supported where
      the source is (const_int 0) and the store can source the zero register.
-     MIPS16 and MSA are never able to source the zero register directly in
+     MIPS16, MSA and VFPU cannot source the zero register directly in these
      memory operations.  */
   if (!register_operand (dest, mode)
       && !register_operand (src, mode)
       && (TARGET_MIPS16 || !const_0_operand (src, mode)
-	  || MSA_SUPPORTED_MODE_P (mode)))
+	  || MSA_SUPPORTED_MODE_P (mode)
+	  || (TARGET_ALLEGREX_VFPU && mode == V4SFmode)))
     {
       mips_emit_move (dest, force_reg (mode, src));
       return true;
@@ -4995,6 +5030,11 @@  mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
 bool
 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
 {
+  if (TARGET_ALLEGREX_VFPU && GET_MODE (dest) == V4SFmode
+      && ((REG_P (dest) && COP2_REG_P (REGNO (dest)))
+	  || (REG_P (src) && COP2_REG_P (REGNO (src)))))
+    return false;
+
   /* Check whether the move can be done using some variant of MULT $0,$0.  */
   if (mips_mult_move_p (dest, src, split_type))
     return false;
@@ -5033,7 +5073,27 @@  mips_split_move (rtx dest, rtx src, enum mips_split_type split_type,
   rtx low_dest;
 
   gcc_checking_assert (mips_split_move_p (dest, src, split_type));
-  if (MSA_SUPPORTED_MODE_P (GET_MODE (dest)))
+  if (TARGET_ALLEGREX_VFPU && GET_MODE (dest) == V4SFmode)
+    {
+      /* Preserve all four words, including when a load overwrites its
+	 own base register or the GPR groups overlap.  */
+      int deferred = -1;
+      bool reverse = (REG_P (dest) && REG_P (src)
+		      && REGNO (dest) > REGNO (src));
+      for (int i = 0; i < 4; ++i)
+	{
+	  int byte = (reverse ? 3 - i : i) * 4;
+	  rtx d = mips_subword_at_byte (dest, byte);
+	  if (MEM_P (src) && reg_overlap_mentioned_p (d, src))
+	    deferred = byte;
+	  else
+	    mips_emit_move (d, mips_subword_at_byte (src, byte));
+	}
+      if (deferred >= 0)
+	mips_emit_move (mips_subword_at_byte (dest, deferred),
+			mips_subword_at_byte (src, deferred));
+    }
+  else if (MSA_SUPPORTED_MODE_P (GET_MODE (dest)))
     mips_split_128bit_move (dest, src);
   else if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
     {
@@ -5344,6 +5404,82 @@  mips_split_move_insn (rtx dest, rtx src, rtx_insn *insn)
   mips_split_move (dest, src, mips_insn_split_type (insn), insn);
 }
 
+/* Move four consecutive floats using a quad operation if the memory is
+   16-byte aligned, and four scalar operations otherwise.  The o32 stack
+   boundary is unchanged, so ordinary spill slots use scalar accesses.  */
+
+const char *
+mips_output_vfpu_move (rtx dest, rtx src)
+{
+  gcc_assert (TARGET_ALLEGREX_VFPU);
+
+  bool dest_vfpu = REG_P (dest) && COP2_REG_P (REGNO (dest));
+  bool src_vfpu = REG_P (src) && COP2_REG_P (REGNO (src));
+  if (!dest_vfpu && !src_vfpu)
+    return mips_output_move (dest, src);
+
+  if (REG_P (dest) && REG_P (src) && dest_vfpu != src_vfpu)
+    {
+      rtx vec = dest_vfpu ? dest : src;
+      rtx gp = dest_vfpu ? src : dest;
+      unsigned int n = REGNO (vec) - COP2_REG_FIRST;
+      for (unsigned int lane = 0; lane < 4; ++lane)
+	{
+	  rtx ops[1] = { gen_rtx_REG (SImode, REGNO (gp) + lane) };
+	  char pattern[64];
+	  snprintf (pattern, sizeof (pattern),
+		    dest_vfpu ? "mtv\t%%0,S%u%u%u" : "mfv\t%%0,S%u%u%u",
+		    n / 4, n % 4, lane);
+	  output_asm_insn (pattern, ops);
+	}
+      return "";
+    }
+
+  if (REG_P (dest) && REG_P (src))
+    return ("vpfxs\tx,y,z,w\n\tvpfxt\tx,y,z,w\n\tvpfxd\t,,,\n\t"
+	    "vmov.q\t%P0,%P1");
+
+  bool load_p = REG_P (dest) && MEM_P (src);
+  gcc_assert (load_p || (MEM_P (dest) && REG_P (src)));
+
+  rtx mem = load_p ? src : dest;
+  rtx vec = load_p ? dest : src;
+  gcc_assert (COP2_REG_P (REGNO (vec)));
+
+  gcc_assert (MEM_ALIGN (mem) >= 32);
+
+  /* The o32 and EABI stack boundaries are only 64 bits.  In particular,
+     invisible reference arguments and hidden return buffers can refer to
+     stack temporaries even when their vector type has 128-bit alignment.
+     Do not use that type alignment as a guarantee for quad accesses.  */
+  tree expr = MEM_EXPR (mem);
+  tree base = expr ? get_base_address (expr) : NULL_TREE;
+  bool abi_mem = (!base || TREE_CODE (base) == RESULT_DECL
+		  || TREE_CODE (base) == PARM_DECL);
+  rtx addr = XEXP (mem, 0);
+  bool stack_mem = (reg_overlap_mentioned_p (stack_pointer_rtx, addr)
+		    || reg_overlap_mentioned_p (hard_frame_pointer_rtx, addr));
+  if (MEM_ALIGN (mem) >= 128 && !abi_mem && !stack_mem)
+    return load_p ? "lv.q\t%P0,%1" : "sv.q\t%P1,%0";
+
+  unsigned int n = REGNO (vec) - COP2_REG_FIRST;
+  unsigned int matrix = n / 4;
+  unsigned int column = n % 4;
+
+  for (unsigned int lane = 0; lane < 4; ++lane)
+    {
+      rtx scalar_mem = adjust_address (mem, SFmode, lane * 4);
+      rtx ops[2] = { load_p ? vec : scalar_mem,
+		     load_p ? scalar_mem : vec };
+      char pattern[64];
+      snprintf (pattern, sizeof (pattern),
+		load_p ? "lv.s\tS%u%u%u,%%1" : "sv.s\tS%u%u%u,%%0",
+		matrix, column, lane);
+      output_asm_insn (pattern, ops);
+    }
+  return "";
+}
+
 /* Return the appropriate instructions to move SRC into DEST.  Assume
    that SRC is operand 1 and DEST is operand 0.  */
 
@@ -9642,6 +9778,18 @@  mips_print_operand (FILE *file, rtx op, int letter)
 	output_operand_lossage ("invalid use of '%%%c'", letter);
       break;
 
+    case 'P':
+      /* GCC COP2 r112..r143 -> nonoverlapping Allegrex quad columns.  */
+      if (TARGET_ALLEGREX_VFPU && code == REG
+	  && COP2_REG_P (REGNO (op)) && GET_MODE (op) == V4SFmode)
+	{
+	  unsigned int n = REGNO (op) - COP2_REG_FIRST;
+	  fprintf (file, "C%u%u0", n / 4, n % 4);
+	}
+      else
+	output_operand_lossage ("invalid use of '%%%c'", letter);
+      break;
+
     case 'v':
       switch (GET_MODE (op))
 	{
@@ -13306,6 +13454,12 @@  mips_hard_regno_mode_ok_uncached (unsigned int regno, machine_mode mode)
   if (mode == CCmode || mode == CCEmode)
     return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
 
+  /* Each VFPU column has one hard register and no subregister modes.  */
+  if (TARGET_ALLEGREX_VFPU && COP2_REG_P (regno))
+    return mode == V4SFmode;
+  if (TARGET_ALLEGREX_VFPU && mode == V4SFmode && !GP_REG_P (regno))
+    return false;
+
   size = GET_MODE_SIZE (mode);
   mclass = GET_MODE_CLASS (mode);
 
@@ -13455,6 +13609,9 @@  mips_hard_regno_call_part_clobbered (unsigned int, unsigned int regno,
 static unsigned int
 mips_hard_regno_nregs (unsigned int regno, machine_mode mode)
 {
+  if (TARGET_ALLEGREX_VFPU && COP2_REG_P (regno) && mode == V4SFmode)
+    return 1;
+
   if (ST_REG_P (regno))
     /* The size of FP status registers is always 4, because they only hold
        CCmode values, and CCmode is always considered to be 4 bytes wide.  */
@@ -13481,6 +13638,9 @@  mips_class_max_nregs (enum reg_class rclass, machine_mode mode)
   int size;
   HARD_REG_SET left;
 
+  if (TARGET_ALLEGREX_VFPU && rclass == COP2_REGS && mode == V4SFmode)
+    return 1;
+
   size = 0x8000;
   left = reg_class_contents[rclass];
   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
@@ -13546,6 +13706,11 @@  mips_can_change_mode_class (machine_mode from,
 
      We therefore disallow all mode changes involving FPRs.  */
 
+  if (TARGET_ALLEGREX_VFPU
+      && (from == V4SFmode || to == V4SFmode)
+      && reg_classes_intersect_p (COP2_REGS, rclass))
+    return from == to;
+
   return !reg_classes_intersect_p (FP_REGS, rclass);
 }
 
@@ -13586,6 +13751,10 @@  mips_mode_ok_for_mov_fmt_p (machine_mode mode)
 static bool
 mips_modes_tieable_p (machine_mode mode1, machine_mode mode2)
 {
+  if (TARGET_ALLEGREX_VFPU
+      && (mode1 == V4SFmode || mode2 == V4SFmode))
+    return mode1 == mode2;
+
   /* FPRs allow no mode punning, so it's not worth tying modes if we'd
      prefer to put one of them in FPRs.  */
   return (mode1 == mode2
@@ -13889,6 +14058,9 @@  mips_vector_mode_supported_p (machine_mode mode)
 {
   switch (mode)
     {
+    case E_V4SFmode:
+      return TARGET_ALLEGREX_VFPU || MSA_SUPPORTED_MODE_P (mode);
+
     case E_V2SFmode:
       return TARGET_PAIRED_SINGLE_FLOAT;
 
@@ -13924,11 +14096,24 @@  mips_scalar_mode_supported_p (scalar_mode mode)
   return default_scalar_mode_supported_p (mode);
 }
 
+/* VFPU arithmetic does not implement the full scalar IEEE behavior.  */
+
+bool
+mips_vfpu_math_p (void)
+{
+  return (TARGET_ALLEGREX_VFPU
+	  && flag_unsafe_math_optimizations && flag_finite_math_only
+	  && !flag_rounding_math && !flag_trapping_math && !flag_signed_zeros);
+}
+
 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE.  */
 
 static machine_mode
 mips_preferred_simd_mode (scalar_mode mode)
 {
+  if (mode == SFmode && mips_vfpu_math_p ())
+    return V4SFmode;
+
   if (TARGET_PAIRED_SINGLE_FLOAT
       && mode == SFmode)
     return V2SFmode;
@@ -13959,6 +14144,32 @@  mips_preferred_simd_mode (scalar_mode mode)
   return word_mode;
 }
 
+/* Quad memory accesses require 128-bit alignment, even though the o32
+   vector type uses its usual ABI alignment.  */
+
+static poly_uint64
+mips_preferred_vector_alignment (const_tree type)
+{
+  if (TARGET_ALLEGREX_VFPU && TYPE_MODE (type) == V4SFmode)
+    return 128;
+
+  return default_preferred_vector_alignment (type);
+}
+
+/* Account for the identity prefixes and scalar lane transfers.  */
+
+static int
+mips_builtin_vectorization_cost (enum vect_cost_for_stmt kind, tree type,
+				 int misalign)
+{
+  if (TARGET_ALLEGREX_VFPU && type && TYPE_MODE (type) == V4SFmode
+      && (kind == vector_stmt || kind == unaligned_load
+	  || kind == unaligned_store))
+    return 4;
+
+  return default_builtin_vectorization_cost (kind, type, misalign);
+}
+
 /* Implement TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES.  */
 
 static unsigned int
@@ -20502,6 +20713,16 @@  mips_option_override (void)
   if (mips_arch_info == 0)
     mips_set_architecture (mips_default_arch ());
 
+  if (TARGET_ALLEGREX_VFPU
+      && (!TARGET_ALLEGREX || TARGET_BIG_ENDIAN
+	  || (mips_abi != ABI_32 && mips_abi != ABI_EABI)
+	  || TARGET_64BIT
+	  || mips_base_compression_flags || TARGET_FLIP_MIPS16))
+    error ("%<-mvfpu%> requires little-endian uncompressed 32-bit "
+	   "Allegrex o32 or EABI");
+  if (TARGET_ALLEGREX_VFPU && (!TARGET_HARD_FLOAT || !TARGET_SINGLE_FLOAT))
+    error ("%<-mvfpu%> requires %<-mhard-float%> and %<-msingle-float%>");
+
   if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
     error ("%<-march=%s%> is not compatible with the selected ABI",
 	   mips_arch_info->name);
@@ -21051,6 +21272,13 @@  mips_swap_registers (unsigned int i)
 static void
 mips_conditional_register_usage (void)
 {
+  /* Each COP2 register represents one caller-clobbered VFPU column.  */
+  if (TARGET_ALLEGREX_VFPU)
+    for (unsigned int r = COP2_REG_FIRST; r <= COP2_REG_LAST; ++r)
+      {
+	fixed_regs[r] = 0;
+	call_used_regs[r] = 1;
+      }
 
   if (ISA_HAS_DSP)
     {
@@ -23556,6 +23784,12 @@  mips_print_patchable_function_entry (FILE *file ATTRIBUTE_UNUSED,
 #undef TARGET_SCALAR_MODE_SUPPORTED_P
 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
 
+#undef TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT
+#define TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT \
+  mips_preferred_vector_alignment
+#undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
+#define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
+  mips_builtin_vectorization_cost
 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES
diff --git a/gcc/config/mips/mips.md b/gcc/config/mips/mips.md
index 18244e4abcc..2339601ac66 100644
--- a/gcc/config/mips/mips.md
+++ b/gcc/config/mips/mips.md
@@ -8169,6 +8169,8 @@ 
 ; The MIPS MSA Instructions.
 (include "mips-msa.md")
 
+(include "mips-vfpu.md")
+
 (define_c_enum "unspec" [
   UNSPEC_ADDRESS_FIRST
 ])
diff --git a/gcc/config/mips/mips.opt b/gcc/config/mips/mips.opt
index eb14761ea27..634942921d1 100644
--- a/gcc/config/mips/mips.opt
+++ b/gcc/config/mips/mips.opt
@@ -328,6 +328,10 @@  mmsa
 Target Mask(MSA)
 Use MIPS MSA Extension instructions.
 
+mvfpu
+Target Var(TARGET_ALLEGREX_VFPU) Init(0)
+Use the Allegrex Vector Floating Point Unit.
+
 mmt
 Target Var(TARGET_MT)
 Allow the use of MT instructions.
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 8a2c65a861e..4d54bd0d429 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -1231,7 +1231,7 @@  Objective-C and Objective-C++ Dialects}.
 -mno-float  -msingle-float  -mdouble-float  -modd-spreg
 -mabs=@var{mode}  -mnan=@var{encoding}
 -mdsp  -mdspr2  -mmcu  -meva  -mvirt  -mxpa  -mcrc  -mginv
--mmicromips  -mmsa
+-mmicromips  -mmsa  -mvfpu
 -mloongson-mmi  -mloongson-ext  -mloongson-ext2
 -mfpu=@var{fpu-type}
 -msmartmips  -mpaired-single  -mdmx  -mips3d  -mmt  -mllsc
@@ -29635,6 +29635,24 @@  Use (do not use) the MIPS Global INValidate (GINV) instructions.
 @itemx -mno-msa
 Use (do not use) the MIPS MSA extension instructions.
 
+@opindex mvfpu
+@opindex mno-vfpu
+@item -mvfpu
+@itemx -mno-vfpu
+Use (do not use) the Allegrex Vector Floating Point Unit.  The default is
+@option{-mno-vfpu}.  This requires little-endian uncompressed Allegrex,
+the o32 or 32-bit EABI, @option{-mhard-float}, @option{-msingle-float}, and an
+assembler that supports the Allegrex VFPU.
+
+With @option{-ffast-math}, the vectorizer can use four-lane
+single-precision VFPU addition, subtraction and multiplication.  Quad
+memory accesses require 16-byte alignment.  Stack boundaries and vector
+type alignment are unchanged.
+
+The VFPU must be available to the executing thread.  Generated code
+treats all VFPU registers as caller-clobbered and resets VFPU prefix
+state before arithmetic and register moves.
+
 @opindex mloongson-mmi
 @opindex mno-loongson-mmi
 @item -mloongson-mmi
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-abi-eabi.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-abi-eabi.c
new file mode 100644
index 00000000000..1a10624add6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-abi-eabi.c
@@ -0,0 +1,21 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=eabi -mgp32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math" } */
+/* Exercise transfers between VFPU registers and the o32 argument and
+   return registers, including arguments split between registers and stack.  */
+/* { dg-final { scan-assembler {vadd[.]q} } } */
+
+typedef float v4sf __attribute__ ((vector_size (16)));
+
+extern v4sf callee (int, v4sf, v4sf);
+
+v4sf
+add (int ignored, v4sf a, v4sf b)
+{
+  return a + b;
+}
+
+v4sf
+call (v4sf a, v4sf b)
+{
+  return callee (1, a, b);
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-abi.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-abi.c
new file mode 100644
index 00000000000..12e3f190ec2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-abi.c
@@ -0,0 +1,21 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math" } */
+/* Exercise transfers between VFPU registers and the o32 argument and
+   return registers, including arguments split between registers and stack.  */
+/* { dg-final { scan-assembler {vadd[.]q} } } */
+
+typedef float v4sf __attribute__ ((vector_size (16)));
+
+extern v4sf callee (int, v4sf, v4sf);
+
+v4sf
+add (int ignored, v4sf a, v4sf b)
+{
+  return a + b;
+}
+
+v4sf
+call (v4sf a, v4sf b)
+{
+  return callee (1, a, b);
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-add.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-add.c
new file mode 100644
index 00000000000..20b9f01c477
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-add.c
@@ -0,0 +1,18 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math -fdump-tree-vect-details" } */
+/* { dg-skip-if "requires -O3" { *-*-* } { "*" } { "-O3" } } */
+/* { dg-final { scan-tree-dump {loop vectorized using 16 byte vectors} "vect" } } */
+/* { dg-final { scan-assembler {lv[.]q} } } */
+/* { dg-final { scan-assembler {vadd[.]q} } } */
+/* { dg-final { scan-assembler {sv[.]q} } } */
+
+void
+add (float *__restrict d, const float *__restrict a,
+     const float *__restrict b, unsigned int n)
+{
+  d = __builtin_assume_aligned (d, 16);
+  a = __builtin_assume_aligned (a, 16);
+  b = __builtin_assume_aligned (b, 16);
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] + b[i];
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-call-eabi.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-call-eabi.c
new file mode 100644
index 00000000000..b58a5774aae
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-call-eabi.c
@@ -0,0 +1,19 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=eabi -mgp32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math" } */
+/* A value live across an external call must be spilled and reloaded;
+   o32 does not guarantee 16-byte stack alignment.  */
+/* { dg-final { scan-assembler {vadd[.]q} } } */
+/* { dg-final { scan-assembler {l(v[.]s|w)} } } */
+/* { dg-final { scan-assembler {s(v[.]s|w)} } } */
+
+typedef float v4sf __attribute__ ((vector_size (16), aligned (16)));
+
+extern void clobber (void);
+
+void
+add_across_call (v4sf *d, const v4sf *a, const v4sf *b)
+{
+  v4sf sum = *a + *b;
+  clobber ();
+  *d = sum + *b;
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-call.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-call.c
new file mode 100644
index 00000000000..9f2b15aef84
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-call.c
@@ -0,0 +1,19 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math" } */
+/* A value live across an external call must be spilled and reloaded;
+   o32 does not guarantee 16-byte stack alignment.  */
+/* { dg-final { scan-assembler {vadd[.]q} } } */
+/* { dg-final { scan-assembler {l(v[.]s|w)} } } */
+/* { dg-final { scan-assembler {s(v[.]s|w)} } } */
+
+typedef float v4sf __attribute__ ((vector_size (16), aligned (16)));
+
+extern void clobber (void);
+
+void
+add_across_call (v4sf *d, const v4sf *a, const v4sf *b)
+{
+  v4sf sum = *a + *b;
+  clobber ();
+  *d = sum + *b;
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-delay.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-delay.c
new file mode 100644
index 00000000000..69228fba8b6
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-delay.c
@@ -0,0 +1,18 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math -fdelayed-branch" } */
+/* { dg-skip-if "requires optimization" { *-*-* } { "-O0" } { "" } } */
+/* { dg-final { scan-assembler {vadd[.]q} } } */
+/* { dg-final { scan-assembler-not {\t(b[a-z0-9]*|j[a-z]*)\t[^\n]*\n\tvpfx[sdt]\t} } } */
+
+typedef float v4sf __attribute__ ((vector_size (16), aligned (16)));
+
+void
+accumulate (v4sf *d, const v4sf *a, const v4sf *b, unsigned int n)
+{
+  v4sf sum = *a;
+  const v4sf addend = *b;
+  do
+    sum += addend;
+  while (--n);
+  *d = sum;
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-disabled.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-disabled.c
new file mode 100644
index 00000000000..c96ebef3dbf
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-disabled.c
@@ -0,0 +1,30 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mno-vfpu -ffast-math" } */
+/* { dg-skip-if "requires -O3" { *-*-* } { "*" } { "-O3" } } */
+/* { dg-final { scan-assembler {add[.]s} } } */
+/* { dg-final { scan-assembler-not {vadd[.]q} } } */
+
+void
+add (float *__restrict d, const float *__restrict a,
+     const float *__restrict b, unsigned int n)
+{
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] + b[i];
+}
+
+void
+subtract (float *__restrict d, const float *__restrict a,
+          const float *__restrict b, unsigned int n)
+{
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] - b[i];
+}
+
+void
+multiply (float *__restrict d, const float *__restrict a,
+          const float *__restrict b, unsigned int n)
+{
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] * b[i];
+}
+/* { dg-final { scan-assembler-not {v(sub|mul)[.]q} } } */
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-arch.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-arch.c
new file mode 100644
index 00000000000..3ed9a55845b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-arch.c
@@ -0,0 +1,5 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=mips2 -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu" } */
+/* { dg-error "requires little-endian uncompressed .*Allegrex o32" "" { target *-*-* } 0 } */
+
+int x;
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-compressed.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-compressed.c
new file mode 100644
index 00000000000..afdb3d9163f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-compressed.c
@@ -0,0 +1,5 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mips16 -mno-micromips -mhard-float -msingle-float -mvfpu" } */
+/* { dg-error "requires little-endian uncompressed .*Allegrex o32" "" { target *-*-* } 0 } */
+
+int x;
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-endian.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-endian.c
new file mode 100644
index 00000000000..5e612e9ec84
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-endian.c
@@ -0,0 +1,5 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EB -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu" } */
+/* { dg-error "requires little-endian uncompressed .*Allegrex o32" "" { target *-*-* } 0 } */
+
+int x;
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-float.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-float.c
new file mode 100644
index 00000000000..a0b5e5448a5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-error-float.c
@@ -0,0 +1,5 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -msoft-float -msingle-float -mvfpu" } */
+/* { dg-error "requires .*mhard-float.* and .*msingle-float" "" { target *-*-* } 0 } */
+
+int x;
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-move.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-move.c
new file mode 100644
index 00000000000..557013ea518
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-move.c
@@ -0,0 +1,24 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math" } */
+/* { dg-final { scan-assembler {lv[.]s} } } */
+/* { dg-final { scan-assembler {sv[.]s} } } */
+
+typedef float v4sf __attribute__ ((vector_size (16), aligned (4)));
+
+void
+add (v4sf *d, const v4sf *a, const v4sf *b)
+{
+  *d = *a + *b;
+}
+
+void
+clear (v4sf *d)
+{
+  *d = (v4sf) { 0, 0, 0, 0 };
+}
+
+void
+construct (v4sf *d, float a, float b, float c, float e)
+{
+  *d = (v4sf) { a, b, c, e };
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-mul.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-mul.c
new file mode 100644
index 00000000000..8a12ce7a081
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-mul.c
@@ -0,0 +1,18 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math -fdump-tree-vect-details" } */
+/* { dg-skip-if "requires -O3" { *-*-* } { "*" } { "-O3" } } */
+/* { dg-final { scan-tree-dump {loop vectorized using 16 byte vectors} "vect" } } */
+/* { dg-final { scan-assembler {lv[.]q} } } */
+/* { dg-final { scan-assembler {vmul[.]q} } } */
+/* { dg-final { scan-assembler {sv[.]q} } } */
+
+void
+mul (float *__restrict d, const float *__restrict a,
+     const float *__restrict b, unsigned int n)
+{
+  d = __builtin_assume_aligned (d, 16);
+  a = __builtin_assume_aligned (a, 16);
+  b = __builtin_assume_aligned (b, 16);
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] * b[i];
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-packed.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-packed.c
new file mode 100644
index 00000000000..13a6a11a89f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-packed.c
@@ -0,0 +1,18 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math" } */
+/* Byte-aligned memory must not be accessed with aligned VFPU loads
+   and stores.  */
+
+typedef float v4sf __attribute__ ((vector_size (16)));
+struct __attribute__ ((packed)) packed_vector
+{
+  char pad;
+  v4sf value;
+};
+
+void
+add (struct packed_vector *d, const struct packed_vector *a,
+     const struct packed_vector *b)
+{
+  d->value = a->value + b->value;
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-return.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-return.c
new file mode 100644
index 00000000000..9ea9e4a1065
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-return.c
@@ -0,0 +1,24 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=eabi -mgp32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math" } */
+/* EABI hidden return buffers and invisible reference arguments can have
+   only stack alignment, even though the vector type is 16-byte aligned.  */
+/* { dg-final { scan-assembler {vadd[.]q} } } */
+/* { dg-final { scan-assembler {sv[.]s} } } */
+/* { dg-final { scan-assembler-not {sv[.]q} } } */
+
+/* { dg-final { scan-assembler-not {[ls]v[.]q[^\n]*\(\$(sp|fp)\)} } } */
+
+typedef float v4sf __attribute__ ((vector_size (16)));
+
+__attribute__ ((noipa)) v4sf
+sum (v4sf a, v4sf b)
+{
+  return a + b;
+}
+
+v4sf
+relay (v4sf a, v4sf b)
+{
+  v4sf value = sum (a, b);
+  return value + a;
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-strict.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-strict.c
new file mode 100644
index 00000000000..3c35e87ddcd
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-strict.c
@@ -0,0 +1,30 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -fno-fast-math" } */
+/* { dg-skip-if "requires -O3" { *-*-* } { "*" } { "-O3" } } */
+/* { dg-final { scan-assembler {add[.]s} } } */
+/* { dg-final { scan-assembler-not {vadd[.]q} } } */
+
+void
+add (float *__restrict d, const float *__restrict a,
+     const float *__restrict b, unsigned int n)
+{
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] + b[i];
+}
+
+void
+subtract (float *__restrict d, const float *__restrict a,
+          const float *__restrict b, unsigned int n)
+{
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] - b[i];
+}
+
+void
+multiply (float *__restrict d, const float *__restrict a,
+          const float *__restrict b, unsigned int n)
+{
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] * b[i];
+}
+/* { dg-final { scan-assembler-not {v(sub|mul)[.]q} } } */
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-sub.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-sub.c
new file mode 100644
index 00000000000..d7d15984a14
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-sub.c
@@ -0,0 +1,18 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math -fdump-tree-vect-details" } */
+/* { dg-skip-if "requires -O3" { *-*-* } { "*" } { "-O3" } } */
+/* { dg-final { scan-tree-dump {loop vectorized using 16 byte vectors} "vect" } } */
+/* { dg-final { scan-assembler {lv[.]q} } } */
+/* { dg-final { scan-assembler {vsub[.]q} } } */
+/* { dg-final { scan-assembler {sv[.]q} } } */
+
+void
+sub (float *__restrict d, const float *__restrict a,
+     const float *__restrict b, unsigned int n)
+{
+  d = __builtin_assume_aligned (d, 16);
+  a = __builtin_assume_aligned (a, 16);
+  b = __builtin_assume_aligned (b, 16);
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] - b[i];
+}
diff --git a/gcc/testsuite/gcc.target/mips/allegrex-vfpu-unaligned.c b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-unaligned.c
new file mode 100644
index 00000000000..d356e8a141f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/mips/allegrex-vfpu-unaligned.c
@@ -0,0 +1,15 @@ 
+/* { dg-do compile } */
+/* { dg-options "-march=allegrex -mabi=32 -EL -mno-mips16 -mno-micromips -mhard-float -msingle-float -mvfpu -ffast-math -fdump-tree-vect-details" } */
+/* { dg-skip-if "requires -O3" { *-*-* } { "*" } { "-O3" } } */
+/* { dg-final { scan-tree-dump {loop vectorized using 16 byte vectors} "vect" } } */
+/* { dg-final { scan-assembler {lv[.]s} } } */
+/* { dg-final { scan-assembler {vadd[.]q} } } */
+/* { dg-final { scan-assembler {sv[.]s} } } */
+
+void
+add (float *__restrict d, const float *__restrict a,
+     const float *__restrict b, unsigned int n)
+{
+  for (unsigned int i = 0; i < n; ++i)
+    d[i] = a[i] + b[i];
+}
diff --git a/gcc/testsuite/gcc.target/mips/mips.exp b/gcc/testsuite/gcc.target/mips/mips.exp
index 9492b1dcaf8..c4b665733f7 100644
--- a/gcc/testsuite/gcc.target/mips/mips.exp
+++ b/gcc/testsuite/gcc.target/mips/mips.exp
@@ -299,6 +299,7 @@  foreach option {
     mcount-ra-address
     odd-spreg
     msa
+    vfpu
     loongson-mmi
     loongson-ext
     loongson-ext2