https://gcc.gnu.org/g:9952f5931f2f887588a8b428125adf962ccf8e1e

commit r17-6250-g9952f5931f2f887588a8b428125adf962ccf8e1e
Author: Jiawei <[email protected]>
Date:   Sat Nov 14 13:53:34 2026 +0800

    AArch64: Reject masked left rotates in right-rotate patterns
    
    Exclude left rotations from the masked register-count patterns that
    emit ROR without negating the count. Also exclude them from the masked
    negative-count splitter, whose left-rotate expansion can allocate new
    pseudos after reload.
    
    Add a target selftest covering both masked and negated masked counts.
    
    Tested with an AArch64 checking-enabled C compiler build and selftests.
    Focused rotation tests passed on gem5 at -O2, -O3 and -Os.
    
    gcc/ChangeLog:
    
            * config/aarch64/aarch64.md
            (*aarch64_<optab>_reg_<mode>3_mask1): Exclude left rotations.
            (*aarch64_<optab>_reg_<mode>3_neg_mask2): Ditto.
            (*aarch64_<optab>_reg_di3_mask2): Ditto.
            * config/aarch64/aarch64.cc
            (selftest::aarch64_test_masked_rotate): New function.
            (selftest::aarch64_run_selftests): Call it.

Diff:
---
 gcc/config/aarch64/aarch64.cc | 36 ++++++++++++++++++++++++++++++++++++
 gcc/config/aarch64/aarch64.md |  9 ++++++---
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index 397ffd5089d6..5a0bc214f13a 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -34650,6 +34650,41 @@ aarch64_test_sve_folding ()
 
 extern void aarch64_json_tunings_tests ();
 
+/* Masked register counts must not turn a left rotate into a right rotate.  */
+
+static void
+aarch64_test_masked_rotate ()
+{
+  for (auto mode : { SImode, DImode })
+    for (auto count_mode : { SImode, DImode })
+      {
+       if (mode == SImode && count_mode == DImode)
+         continue;
+       rtx dest = gen_rtx_REG (mode, LAST_VIRTUAL_REGISTER + 1);
+       rtx value = gen_rtx_REG (mode, LAST_VIRTUAL_REGISTER + 2);
+       rtx count = gen_rtx_REG (count_mode, LAST_VIRTUAL_REGISTER + 3);
+       count = gen_rtx_AND (count_mode, count,
+                            GEN_INT (GET_MODE_BITSIZE (mode) - 1));
+       for (bool negate : { false, true })
+         {
+           if (negate && count_mode != SImode)
+             continue;
+           rtx masked_count = negate ? gen_rtx_NEG (count_mode, count) : count;
+           masked_count = gen_rtx_SUBREG
+             (QImode, masked_count, subreg_lowpart_offset (QImode, 
count_mode));
+           for (auto code : { ASHIFT, ASHIFTRT, LSHIFTRT, ROTATERT, ROTATE })
+             {
+               rtx op = gen_rtx_fmt_ee (code, mode, value, masked_count);
+               int icode = recog (gen_rtx_SET (dest, op), nullptr, nullptr);
+               if (code == ROTATE)
+                 ASSERT_EQ (icode, -1);
+               else
+                 ASSERT_TRUE (icode >= 0);
+             }
+         }
+      }
+}
+
 /* Run all target-specific selftests.  */
 
 static void
@@ -34660,6 +34695,7 @@ aarch64_run_selftests (void)
   aarch64_test_sysreg_encoding_clashes ();
   aarch64_test_sve_folding ();
   aarch64_json_tunings_tests ();
+  aarch64_test_masked_rotate ();
 }
 
 } // namespace selftest
diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md
index 6916f6ec0aa9..4c6a0f492cff 100644
--- a/gcc/config/aarch64/aarch64.md
+++ b/gcc/config/aarch64/aarch64.md
@@ -6178,7 +6178,8 @@
          (match_operator 4 "subreg_lowpart_operator"
           [(and:GPI (match_operand:GPI 2 "register_operand" "r")
                     (match_operand 3 "const_int_operand" "n"))])))]
-  "(~INTVAL (operands[3]) & (GET_MODE_BITSIZE (<MODE>mode) - 1)) == 0"
+  "!<is_rotl>
+   && (~INTVAL (operands[3]) & (GET_MODE_BITSIZE (<MODE>mode) - 1)) == 0"
   "<shift>\t%<w>0, %<w>1, %<w>2"
   [(set_attr "type" "shift_reg")]
 )
@@ -6190,7 +6191,8 @@
          (match_operator 4 "subreg_lowpart_operator"
          [(neg:SI (and:SI (match_operand:SI 2 "register_operand" "r")
                           (match_operand 3 "const_int_operand" "n")))])))]
-  "((~INTVAL (operands[3]) & (GET_MODE_BITSIZE (<MODE>mode) - 1)) == 0)"
+  "!<is_rotl>
+   && ((~INTVAL (operands[3]) & (GET_MODE_BITSIZE (<MODE>mode) - 1)) == 0)"
   "#"
   "&& true"
   [(const_int 0)]
@@ -6242,7 +6244,8 @@
          (match_operator 4 "subreg_lowpart_operator"
           [(and:SI (match_operand:SI 2 "register_operand" "r")
                    (match_operand 3 "const_int_operand" "n"))])))]
-  "((~INTVAL (operands[3]) & (GET_MODE_BITSIZE (DImode) - 1)) == 0)"
+  "!<is_rotl>
+   && ((~INTVAL (operands[3]) & (GET_MODE_BITSIZE (DImode) - 1)) == 0)"
 {
   rtx xop[3];
   xop[0] = operands[0];

Reply via email to