https://gcc.gnu.org/g:05e4bdd9390ce456301f007dd51baac29b07359f

commit r16-8944-g05e4bdd9390ce456301f007dd51baac29b07359f
Author: Jakub Jelinek <[email protected]>
Date:   Wed May 20 09:18:15 2026 +0200

    i386: Fix up peephole2s with const359_operand [PR125373]
    
    The following testcase ICEs, because the const359_operand peephole2s
    optimize multiplication of %rsp by 5 (or could by 3 or 9) into a lea.
    Which is not valid, because sp is not a valid index register.
    
    The following patch makes sure to use index_reg_operand predicate instead
    so that it won't match for the stack pointer.
    
    2026-05-20  Jakub Jelinek  <[email protected]>
    
            PR target/125373
            * config/i386/i386.md
            (Convert imul by three, five and nine into lea define_peephole2s): 
Use
            index_reg_operand instead of register_operand.
    
            * gcc.c-torture/compile/pr125373.c: New test.
    
    Reviewed-by: Uros Bizjak <[email protected]>
    (cherry picked from commit 568d439f1d1f65eabed55a54da812a865dc5c946)

Diff:
---
 gcc/config/i386/i386.md                        |  4 ++--
 gcc/testsuite/gcc.c-torture/compile/pr125373.c | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index 179c30494e82..cea79cd24465 100644
--- a/gcc/config/i386/i386.md
+++ b/gcc/config/i386/i386.md
@@ -29449,7 +29449,7 @@
 (define_peephole2
   [(parallel
     [(set (match_operand:SWI48 0 "register_operand")
-         (mult:SWI48 (match_operand:SWI48 1 "register_operand")
+         (mult:SWI48 (match_operand:SWI48 1 "index_reg_operand")
                      (match_operand:SWI48 2 "const359_operand")))
      (clobber (reg:CC FLAGS_REG))])]
   "!TARGET_PARTIAL_REG_STALL
@@ -29462,7 +29462,7 @@
 
 (define_peephole2
   [(parallel
-    [(set (match_operand:SWI48 0 "register_operand")
+    [(set (match_operand:SWI48 0 "index_reg_operand")
          (mult:SWI48 (match_operand:SWI48 1 "nonimmediate_operand")
                      (match_operand:SWI48 2 "const359_operand")))
      (clobber (reg:CC FLAGS_REG))])]
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr125373.c 
b/gcc/testsuite/gcc.c-torture/compile/pr125373.c
new file mode 100644
index 000000000000..203e848836c3
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr125373.c
@@ -0,0 +1,20 @@
+/* PR target/125373 */
+
+int foo (int);
+
+void
+bar (int *v, int w)
+{
+  for (int i = 0; i < (__INTPTR_TYPE__) (v - 1); i++)
+    if (v)
+      v[i] = v[w];
+}
+
+int
+main ()
+{
+  int a[] = { 0, 0, 0, 0 };
+  bar (a, 4);
+  for (int i = 0; i < 4; i++)
+    foo (a[i]);
+}

Reply via email to