This commit tries hoist VXRM writes to preheaders on microarchs that
have a pipeline flush on such CSR write.  The main motivation of this
patch is x264, as after the LTO patches, some get_ref related functions
get inlined into callers and VXRM can no longer be hoisted to the
function epilogue.  This version currently only handles those in
innermost loops.  The tuning parameters are added as per the LLVM
handling of the similar optimization.

Related testcases were run manually.  Regression tests on rv64gcv and
rv32gcv in flight.

gcc/ChangeLog:

        * config/riscv/riscv-protos.h (riscv_optimize_mode_switching): Declare.
        * config/riscv/riscv.cc (struct machine_function): Add loop
        requirement cache.
        (struct riscv_tune_param): Add vxrm_pipeline_flush.
        (generic_tune_info): Initialize vxrm_pipeline_flush.
        (rocket_tune_info): Ditto.
        (sifive_7_tune_info): Ditto.
        (sifive_p400_tune_info): Ditto.
        (sifive_p600_tune_info): Ditto.
        (sifive_p550_tune_info): Ditto.
        (sifive_p870_tune_info): Ditto.
        (thead_c906_tune_info): Ditto.
        (xiangshan_nanhu_tune_info): Ditto.
        (generic_ooo_tune_info): Ditto.
        (xt_c9501_tune_info): Ditto.
        (tt_ascalon_d8_tune_info): Ditto.
        (optimize_size_tune_info): Ditto.
        (mips_p8700_tune_info): Ditto.
        (mips_i8500_tune_info): Ditto.
        (andes_25_tune_info): Ditto.
        (xt_c908_tune_info): Ditto.
        (spacemit_x60_tune_info): Ditto.
        (spacemit_x100_tune_info): Ditto.
        (andes_23_tune_info): Ditto.
        (andes_45_tune_info): Ditto.
        (arcv_rhx100_tune_info): Ditto.
        (arcv_rmx100_tune_info): Ditto.
        (asm_insn_p): Move before riscv_vxrm_loop_safe_p.
        (vxrm_unknown_p): Ditto.
        (riscv_vxrm_loop_safe_p): New function.
        (riscv_vxrm_loop_requirements): Ditto.
        (riscv_optimize_mode_switching): Ditto.
        (riscv_mode_needed): Look up prepared loop requirements.
        * config/riscv/riscv.h (OPTIMIZE_MODE_SWITCHING): Call
        riscv_optimize_mode_switching.

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/rvv/base/vxrm-loop-1.c: New test.
        * gcc.target/riscv/rvv/base/vxrm-loop-2.c: New test.
---
 gcc/config/riscv/riscv-protos.h               |   2 +
 gcc/config/riscv/riscv.cc                     | 220 +++++++++++++++---
 gcc/config/riscv/riscv.h                      |   2 +-
 .../gcc.target/riscv/rvv/base/vxrm-loop-1.c   |  67 ++++++
 .../gcc.target/riscv/rvv/base/vxrm-loop-2.c   |  49 ++++
 5 files changed, 312 insertions(+), 28 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/vxrm-loop-1.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/vxrm-loop-2.c

diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h
index 07a1da94605..e799b9b552e 100644
--- a/gcc/config/riscv/riscv-protos.h
+++ b/gcc/config/riscv/riscv-protos.h
@@ -959,4 +959,6 @@ enum
   RISCV_REVISION_VERSION_BASE = 1,
 };
 
+extern bool riscv_optimize_mode_switching (int);
+
 #endif /* ! GCC_RISCV_PROTOS_H */
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index d83e26351c9..4141e0faaf9 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -221,6 +221,10 @@ struct GTY(())  machine_function {
 
   /* The mode switching information for the FRM rounding modes.  */
   struct mode_switching_info mode_sw_info;
+
+  /* Additional VXRM requirements indexed by insn UID.  Zero means no
+     requirement; other values encode the required mode plus one.  */
+  vec<int, va_gc_atomic> *vxrm_loop_requirements;
 };
 
 /* Information about a single argument.  */
@@ -294,6 +298,7 @@ struct riscv_tune_param
   bool overlap_op_by_pieces;
   bool use_zero_stride_load;
   bool speculative_sched_vsetvl;
+  bool vxrm_pipeline_flush;
   unsigned HOST_WIDE_INT fusible_ops;
   const struct cpu_vector_cost *vec_costs;
   const char *function_align;
@@ -496,6 +501,7 @@ static const struct riscv_tune_param generic_tune_info = {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                           /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -521,6 +527,7 @@ static const struct riscv_tune_param rocket_tune_info = {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                           /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -546,6 +553,7 @@ static const struct riscv_tune_param sifive_7_tune_info = {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                           /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -571,6 +579,7 @@ static const struct riscv_tune_param sifive_p400_tune_info 
= {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_LUI_ADDI | RISCV_FUSE_AUIPC_ADDI,  /* fusible_ops */
   &generic_vector_cost,                                /* vector cost */
   NULL,                                                /* function_align */
@@ -596,6 +605,7 @@ static const struct riscv_tune_param sifive_p600_tune_info 
= {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  true,                                                /* vxrm_pipeline_flush 
*/
   RISCV_FUSE_LUI_ADDI | RISCV_FUSE_AUIPC_ADDI,  /* fusible_ops */
   &generic_vector_cost,                                /* vector cost */
   NULL,                                                /* function_align */
@@ -621,6 +631,7 @@ static const struct riscv_tune_param sifive_p550_tune_info 
= {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_LUI_ADDI | RISCV_FUSE_AUIPC_ADDI, /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -646,6 +657,7 @@ static const struct riscv_tune_param sifive_p870_tune_info 
= {
   true,                                                /* overlap_op_by_pieces 
*/
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  true,                                                /* vxrm_pipeline_flush 
*/
   RISCV_FUSE_LUI_ADDI | RISCV_FUSE_AUIPC_ADDI, /* fusible_ops */
   &generic_vector_cost,                                /* vector cost */
   NULL,                                                /* function_align */
@@ -671,6 +683,7 @@ static const struct riscv_tune_param thead_c906_tune_info = 
{
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                           /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -696,6 +709,7 @@ static const struct riscv_tune_param 
xiangshan_nanhu_tune_info = {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_ZEXTW | RISCV_FUSE_ZEXTH,          /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -721,6 +735,7 @@ static const struct riscv_tune_param generic_ooo_tune_info 
= {
   true,                                                /* overlap_op_by_pieces 
*/
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                           /* fusible_ops */
   &generic_vector_cost,                                /* vector cost */
   NULL,                                                /* function_align */
@@ -800,6 +815,7 @@ static const struct riscv_tune_param xt_c9501_tune_info = {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  true,                                                /* vxrm_pipeline_flush 
*/
   RISCV_FUSE_NOTHING,                          /* fusible_ops */
   &xt_c9501_vector_cost,                       /* vector cost */
   "32",                                                /* function_align */
@@ -831,6 +847,7 @@ static const struct riscv_tune_param 
tt_ascalon_d8_tune_info = {
   true,                                                /* overlap_op_by_pieces 
*/
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                           /* fusible_ops */
   &generic_vector_cost,                                /* vector cost */
   NULL,                                                /* function_align */
@@ -856,6 +873,7 @@ static const struct riscv_tune_param 
optimize_size_tune_info = {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                           /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -881,6 +899,7 @@ static const struct riscv_tune_param mips_p8700_tune_info = 
{
   false,        /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                          /* fusible_ops */
   NULL,         /* vector cost */
   NULL,         /* function_align */
@@ -906,6 +925,7 @@ static const struct riscv_tune_param mips_i8500_tune_info = 
{
   false,    /* overlap_op_by_pieces.  */
   true,     /* use_zero_stride_load.  */
   false,    /* speculative_sched_vsetvl.  */
+  false,    /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,   /* fusible_ops.  */
   NULL,     /* vector cost.  */
   NULL,     /* function_align.  */
@@ -931,6 +951,7 @@ static const struct riscv_tune_param andes_25_tune_info = {
   false,                                       /* overlap_op_by_pieces */
   false,                                       /* use_zero_stride_load */
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                          /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -955,6 +976,7 @@ static const struct riscv_tune_param xt_c908_tune_info= {
   true,                                        /* overlap_op_by_pieces */
   false,                                       /* use_zero_stride_load */
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                          /* fusible_ops */
   &generic_vector_cost,                                /* vector cost */
   NULL,                                                /* function_align */
@@ -979,6 +1001,7 @@ static const struct riscv_tune_param 
spacemit_x60_tune_info= {
   false,                                       /* overlap_op_by_pieces */
   false,                                       /* use_zero_stride_load */
   true,                                                /* 
speculative_sched_vsetvl */
+  true,                                                /* vxrm_pipeline_flush 
*/
   RISCV_FUSE_NOTHING,                          /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -1004,6 +1027,7 @@ static const struct riscv_tune_param 
spacemit_x100_tune_info = {
   true,                                                /* overlap_op_by_pieces 
*/
   false,                                       /* use_zero_stride_load */
   false,                                       /* speculative_sched_vsetvl */
+  true,                                                /* vxrm_pipeline_flush 
*/
   RISCV_FUSE_NOTHING,                          /* fusible_ops */
   &generic_vector_cost,                                /* vector cost */
   NULL,                                                /* function_align */
@@ -1029,6 +1053,7 @@ static const struct riscv_tune_param andes_23_tune_info = 
{
   false,                                       /* overlap_op_by_pieces */
   false,                                       /* use_zero_stride_load */
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                          /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -1054,6 +1079,7 @@ static const struct riscv_tune_param andes_45_tune_info = 
{
   false,                                       /* overlap_op_by_pieces */
   false,                                       /* use_zero_stride_load */
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                          /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -1079,6 +1105,7 @@ static const struct riscv_tune_param 
arcv_rhx100_tune_info = {
   false,                                      /* overlap_op_by_pieces */
   true,                                               /* use_zero_stride_load 
*/
   false,                                      /* speculative_sched_vsetvl */
+  false,                                      /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                         /* fusible_ops */
   NULL,                                               /* vector cost */
   NULL,                                               /* function_align */
@@ -1105,6 +1132,7 @@ static const struct riscv_tune_param 
arcv_rmx100_tune_info = {
   false,                                       /* overlap_op_by_pieces */
   true,                                                /* use_zero_stride_load 
*/
   false,                                       /* speculative_sched_vsetvl */
+  false,                                       /* vxrm_pipeline_flush */
   RISCV_FUSE_NOTHING,                          /* fusible_ops */
   NULL,                                                /* vector cost */
   NULL,                                                /* function_align */
@@ -14288,6 +14316,163 @@ singleton_vxrm_need (void)
   return found ? saved_vxrm_mode : VXRM_MODE_NONE;
 }
 
+/* Return TRUE that an insn is asm.  */
+
+static bool
+asm_insn_p (rtx_insn *insn)
+{
+  extract_insn (insn);
+
+  return recog_data.is_asm;
+}
+
+/* Return TRUE that an insn is unknown for VXRM.  */
+
+static bool
+vxrm_unknown_p (rtx_insn *insn)
+{
+  /* Return true if VXRM is set or clobbered.  */
+  if (reg_set_p (gen_rtx_REG (SImode, VXRM_REGNUM), insn))
+    return true;
+
+  /* Return true for all assembly since users may hardcode an assembly
+     like this: asm volatile ("csrwi vxrm, 0").  */
+  if (asm_insn_p (insn))
+    return true;
+
+  return false;
+}
+
+/* Return true if VXRM uses in this function can be moved across insns
+   that have no rounding-mode requirement.  Inline assembly and explicit
+   accesses to VXRM are not described by the mode-switching attributes.  */
+static bool
+riscv_vxrm_loop_safe_p ()
+{
+  rtx vxrm = gen_rtx_REG (SImode, VXRM_REGNUM);
+  for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
+    if (NONDEBUG_INSN_P (insn) && !CALL_P (insn))
+      {
+       if (vxrm_unknown_p (insn))
+         return false;
+       if (reg_mentioned_p (vxrm, PATTERN (insn))
+           && (recog_memoized (insn) < 0
+               || get_attr_vxrm_mode (insn) == VXRM_MODE_NONE))
+         return false;
+      }
+  return true;
+}
+
+/* Record a common VXRM requirement at an existing dedicated preheader.
+   Restrict this to innermost loops so that one preheader cannot acquire
+   conflicting requirements from nested loops.  Do not change the CFG.  */
+static void
+riscv_vxrm_loop_requirements ()
+{
+  if (current_loops && loops_state_satisfies_p (LOOPS_NEED_FIXUP))
+    return;
+
+  bool own_loops = !current_loops;
+  bool had_dom = dom_info_available_p (CDI_DOMINATORS);
+  if (own_loops)
+    loop_optimizer_init (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
+  calculate_dominance_info (CDI_DOMINATORS);
+
+  for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
+    {
+      edge entry = nullptr;
+      edge e;
+      edge_iterator ei;
+      bool safe = true;
+      FOR_EACH_EDGE (e, ei, loop->header->preds)
+       if (!flow_bb_inside_loop_p (loop, e->src))
+         {
+           if (entry || (e->flags & (EDGE_ABNORMAL | EDGE_EH)))
+             {
+               safe = false;
+               break;
+             }
+           entry = e;
+         }
+      if (!safe || !entry || !single_succ_p (entry->src)
+         || entry->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
+       continue;
+
+      rtx_insn *last = BB_END (entry->src);
+      while (last != BB_HEAD (entry->src) && !NONDEBUG_INSN_P (last))
+       last = PREV_INSN (last);
+      if (!NONDEBUG_INSN_P (last) || CALL_P (last))
+       continue;
+
+      int mode = VXRM_MODE_NONE;
+      basic_block *body = get_loop_body (loop);
+      for (unsigned b = 0; b < loop->num_nodes && safe; ++b)
+       {
+         FOR_EACH_EDGE (e, ei, body[b]->preds)
+           if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
+             safe = false;
+         rtx_insn *insn;
+         FOR_BB_INSNS (body[b], insn)
+           {
+             if (!NONDEBUG_INSN_P (insn))
+               continue;
+             int need = recog_memoized (insn) >= 0
+                        ? get_attr_vxrm_mode (insn) : VXRM_MODE_NONE;
+             if (CALL_P (insn) || need > VXRM_MODE_NONE
+                 || (need != VXRM_MODE_NONE
+                     && (need > VXRM_MODE_ROD
+                         || (mode != VXRM_MODE_NONE && mode != need))))
+               {
+                 safe = false;
+                 break;
+               }
+             if (need != VXRM_MODE_NONE)
+               mode = need;
+           }
+       }
+      free (body);
+      if (!safe || mode == VXRM_MODE_NONE)
+       continue;
+
+      int need = recog_memoized (last) >= 0
+                ? get_attr_vxrm_mode (last) : VXRM_MODE_NONE;
+      if (need != VXRM_MODE_NONE && need != mode)
+       continue;
+
+      if (!cfun->machine->vxrm_loop_requirements)
+       vec_safe_grow_cleared (cfun->machine->vxrm_loop_requirements,
+                             get_max_uid () + 1);
+      (*cfun->machine->vxrm_loop_requirements)[INSN_UID (last)] = mode + 1;
+      if (dump_file)
+       fprintf (dump_file, "VXRM mode %d required in preheader bb %d "
+                "of loop header bb %d\n", mode, entry->src->index,
+                loop->header->index);
+    }
+
+  if (own_loops)
+    loop_optimizer_finalize ();
+  if (!had_dom)
+    free_dominance_info (CDI_DOMINATORS);
+}
+
+/* Prepare VXRM requirements before mode switching collects per-insn modes.
+   Calls to MODE_NEEDED then only look up the prepared requirements.  */
+bool
+riscv_optimize_mode_switching (int entity)
+{
+  if (!TARGET_VECTOR)
+    return false;
+  if (entity == RISCV_VXRM)
+    {
+      cfun->machine->vxrm_loop_requirements = nullptr;
+      if (optimize >= 2 && tune_param->vxrm_pipeline_flush
+         && singleton_vxrm_need () == VXRM_MODE_NONE
+         && riscv_vxrm_loop_safe_p ())
+       riscv_vxrm_loop_requirements ();
+    }
+  return true;
+}
+
 /* Return mode that entity must be switched into
    prior to the execution of insn.  */
 
@@ -14304,11 +14489,19 @@ riscv_mode_needed (int entity, rtx_insn *insn, 
HARD_REG_SET)
         elimination of subsequent sets of VXRM.  */
       if (insn == get_first_nonnote_nondebug_insn ())
        {
+
          int need = singleton_vxrm_need ();
          if (need != VXRM_MODE_NONE)
            return need;
        }
 
+      if ((unsigned) INSN_UID (insn)
+         < vec_safe_length (cfun->machine->vxrm_loop_requirements))
+       {
+         int mode = (*cfun->machine->vxrm_loop_requirements)[INSN_UID (insn)];
+         if (mode)
+           return mode - 1;
+       }
       return code >= 0 ? get_attr_vxrm_mode (insn) : VXRM_MODE_NONE;
     case RISCV_FRM:
       return riscv_frm_mode_needed (insn, code);
@@ -14317,33 +14510,6 @@ riscv_mode_needed (int entity, rtx_insn *insn, 
HARD_REG_SET)
     }
 }
 
-/* Return TRUE that an insn is asm.  */
-
-static bool
-asm_insn_p (rtx_insn *insn)
-{
-  extract_insn (insn);
-
-  return recog_data.is_asm;
-}
-
-/* Return TRUE that an insn is unknown for VXRM.  */
-
-static bool
-vxrm_unknown_p (rtx_insn *insn)
-{
-  /* Return true if VXRM is set or clobbered.  */
-  if (reg_set_p (gen_rtx_REG (SImode, VXRM_REGNUM), insn))
-    return true;
-
-  /* Return true for all assembly since users may hardcode an assembly
-     like this: asm volatile ("csrwi vxrm, 0").  */
-  if (asm_insn_p (insn))
-    return true;
-
-  return false;
-}
-
 /* Return TRUE that an insn is unknown dynamic for FRM.  */
 
 static bool
diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
index 7ba2a726ac4..fea4283282f 100644
--- a/gcc/config/riscv/riscv.h
+++ b/gcc/config/riscv/riscv.h
@@ -1326,7 +1326,7 @@ extern void riscv_remove_unneeded_save_restore_calls 
(void);
 #define VECTOR_STORE_FLAG_VALUE(MODE) CONSTM1_RTX (GET_MODE_INNER (MODE))
 
 /* Mode switching (Lazy code motion) for RVV rounding mode instructions.  */
-#define OPTIMIZE_MODE_SWITCHING(ENTITY) (TARGET_VECTOR)
+#define OPTIMIZE_MODE_SWITCHING(ENTITY) riscv_optimize_mode_switching (ENTITY)
 #define NUM_MODES_FOR_MODE_SWITCHING {VXRM_MODE_NONE, riscv_vector::FRM_NONE}
 
 /* The size difference between different RVV modes can be up to 64 times.
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/vxrm-loop-1.c 
b/gcc/testsuite/gcc.target/riscv/rvv/base/vxrm-loop-1.c
new file mode 100644
index 00000000000..bf884a8ef70
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/base/vxrm-loop-1.c
@@ -0,0 +1,67 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gcv -mabi=lp64d -mtune=xt-c9501fdvt 
-fdump-rtl-mode_sw" } */
+
+#include <riscv_vector.h>
+
+extern void clobber_vxrm (void);
+
+/* The call prevents the whole-function singleton optimization.  The
+   conditional vector operation is not anticipated on every loop path.  */
+__attribute__ ((noipa))
+void
+average (unsigned char *dst, const unsigned char *a, const unsigned char *b,
+        const int *take, int n)
+{
+  clobber_vxrm ();
+  for (int i = 0; i < n; ++i)
+    {
+      if (take[i])
+       {
+         vuint8m1_t x = __riscv_vle8_v_u8m1 (a, 16);
+         vuint8m1_t y = __riscv_vle8_v_u8m1 (b, 16);
+         vuint8m1_t z
+           = __riscv_vaaddu_vv_u8m1 (x, y, __RISCV_VXRM_RNU, 16);
+         __riscv_vse8_v_u8m1 (dst, z, 16);
+       }
+      else
+       dst[0] = a[0];
+      dst += 16;
+      a += 16;
+      b += 16;
+    }
+}
+
+/* The two innermost loops have different modes.  Their requirements must
+   remain on their respective dedicated preheaders, not on the shared
+   conditional predecessor.  */
+void
+separate_modes (unsigned char *dst, const unsigned char *a,
+               const int *take, int n, int which)
+{
+#define AVERAGE_LOOP(MODE) \
+  for (int i = 0; i < n; ++i) \
+    { \
+      if (take[i]) \
+       { \
+         vuint8m1_t x = __riscv_vle8_v_u8m1 (a + i * 16, 16); \
+         x = __riscv_vaaddu_vx_u8m1 (x, 1, MODE, 16); \
+         __riscv_vse8_v_u8m1 (dst + i * 16, x, 16); \
+       } \
+      else \
+       dst[i * 16] = a[i * 16]; \
+    }
+  if (which)
+    {
+      AVERAGE_LOOP (__RISCV_VXRM_RNU)
+    }
+  else
+    {
+      AVERAGE_LOOP (__RISCV_VXRM_RDN)
+    }
+#undef AVERAGE_LOOP
+}
+
+/* { dg-final { scan-rtl-dump-times "VXRM mode 0 required in preheader" 2 
"mode_sw" } } */
+/* { dg-final { scan-rtl-dump-times "VXRM mode 2 required in preheader" 1 
"mode_sw" } } */
+/* { dg-final { scan-assembler-times {csrwi\s+vxrm,\s*0} 2 } } */
+/* { dg-final { scan-assembler-times {csrwi\s+vxrm,\s*2} 1 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/vxrm-loop-2.c 
b/gcc/testsuite/gcc.target/riscv/rvv/base/vxrm-loop-2.c
new file mode 100644
index 00000000000..4548653a240
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rvv/base/vxrm-loop-2.c
@@ -0,0 +1,49 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gcv -mabi=lp64d -mtune=xt-c9501fdvt 
-fdump-rtl-mode_sw" } */
+
+#include <riscv_vector.h>
+
+extern void clobber_vxrm (void);
+
+/* A call inside the loop invalidates the rounding state.  */
+void
+with_call (unsigned char *dst, const unsigned char *a, int n)
+{
+  for (int i = 0; i < n; ++i)
+    {
+      clobber_vxrm ();
+      vuint8m1_t x = __riscv_vle8_v_u8m1 (a + i * 16, 16);
+      x = __riscv_vaaddu_vv_u8m1 (x, x, __RISCV_VXRM_RNU, 16);
+      __riscv_vse8_v_u8m1 (dst + i * 16, x, 16);
+    }
+}
+
+/* A loop with two modes must retain its mode changes.  */
+void
+mixed (unsigned char *dst, const unsigned char *a, int n)
+{
+  clobber_vxrm ();
+  for (int i = 0; i < n; ++i)
+    {
+      vuint8m1_t x = __riscv_vle8_v_u8m1 (a + i * 16, 16);
+      x = __riscv_vaaddu_vx_u8m1 (x, 1, __RISCV_VXRM_RNU, 16);
+      x = __riscv_vaaddu_vx_u8m1 (x, 2, __RISCV_VXRM_RDN, 16);
+      __riscv_vse8_v_u8m1 (dst + i * 16, x, 16);
+    }
+}
+
+/* An explicit CSR access is not described by the VXRM attributes.  */
+void
+explicit_access (unsigned char *dst, const unsigned char *a, int n)
+{
+  clobber_vxrm ();
+  for (int i = 0; i < n; ++i)
+    {
+      asm volatile ("csrwi vxrm,2" ::: "vxrm", "memory");
+      vuint8m1_t x = __riscv_vle8_v_u8m1 (a + i * 16, 16);
+      x = __riscv_vaaddu_vx_u8m1 (x, 1, __RISCV_VXRM_RNU, 16);
+      __riscv_vse8_v_u8m1 (dst + i * 16, x, 16);
+    }
+}
+
+/* { dg-final { scan-rtl-dump-not "required in preheader" "mode_sw" } } */
-- 
2.47.1

Reply via email to