The following patch fixes

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115568

The patch was successfully bootstrapped and tested on x86-64.

commit 98545441308c2ae4d535f14b108ad6551fd927d5
Author: Vladimir N. Makarov <vmaka...@redhat.com>
Date:   Wed Feb 5 14:23:23 2025 -0500

    [PR115568][LRA]: Use more strict output reload check in rematerialization
    
      In this PR case LRA rematerialized a value from inheritance insn
    instead of output reload one.  This resulted in considering a
    rematerilization candidate value available when it was actually
    not.  As a consequence an insn after rematerliazation used the
    unexpected value and this use resulted in fp exception.  The patch
    fixes this bug.
    
    gcc/ChangeLog:
    
            PR rtl-optimization/115568
            * lra-remat.cc (create_cands): Check that output reload insn is
            adjacent to given insn.  Update a comment.
    
    gcc/testsuite/ChangeLog:
    
            PR rtl-optimization/115568
            * gcc.target/i386/pr115568.c: New.

diff --git a/gcc/lra-remat.cc b/gcc/lra-remat.cc
index bb13c616a74..2f3afffcf5b 100644
--- a/gcc/lra-remat.cc
+++ b/gcc/lra-remat.cc
@@ -459,7 +459,8 @@ create_cands (void)
 	    if (insn2 != NULL
 		&& dst_regno >= FIRST_PSEUDO_REGISTER
 		&& reg_renumber[dst_regno] < 0
-		&& BLOCK_FOR_INSN (insn2) == BLOCK_FOR_INSN (insn))
+		&& BLOCK_FOR_INSN (insn2) == BLOCK_FOR_INSN (insn)
+		&& insn2 == prev_nonnote_insn (insn))
 	      {
 		create_cand (insn2, regno_potential_cand[src_regno].nop,
 			     dst_regno, insn);
@@ -473,9 +474,10 @@ create_cands (void)
 	    gcc_assert (REG_P (*id->operand_loc[nop]));
 	    int regno = REGNO (*id->operand_loc[nop]);
 	    gcc_assert (regno >= FIRST_PSEUDO_REGISTER);
-	    /* If we're setting an unrenumbered pseudo, make a candidate immediately.
-	       If it's an output reload register, save it for later; the code above
-	       looks for output reload insns later on.  */
+	    /* If we're setting an unrenumbered pseudo, make a candidate
+	       immediately.  If it's a potential output reload register, save
+	       it for later; the code above looks for output reload insns later
+	       on.  */
 	    if (reg_renumber[regno] < 0)
 	      create_cand (insn, nop, regno);
 	    else if (regno >= lra_constraint_new_regno_start)
diff --git a/gcc/testsuite/gcc.target/i386/pr115568.c b/gcc/testsuite/gcc.target/i386/pr115568.c
new file mode 100644
index 00000000000..cedc7ac3843
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr115568.c
@@ -0,0 +1,38 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -fno-tree-sink -fno-tree-ter -fschedule-insns" } */
+
+int a, c, d = 1, e, f = 1, h, i, j;
+unsigned b = 1, g;
+int main() {
+  for (; h < 2; h++) {
+    int k = ~(b || 0), l = ((~e - j) ^ a % b) % k, m = (b ^ -1) + e;
+    unsigned o = ~a % ~1;
+    if (f) {
+      l = d;
+      m = 10;
+      i = e;
+      d = -(~e + b);
+      g = o % m;
+      e = -1;
+    n:
+      a = a % ~i;
+      b = ~k;
+      if (!g) {
+        b = e + o % -1;
+        continue;
+      }
+      if (!l)
+        break;
+    }
+    int q = (~d + g) << ~e, p = (~d - q) & a >> b;
+    unsigned s = ~((g & e) + (p | (b ^ (d + k))));
+    int r = (e & s) + p, u = d | ~a,
+        t = ((~(q + (~a + (s + e)))) & u) | (-g & (c << d ^ p));
+    if (t)
+      if (!r)
+        goto n;
+    g = m;
+    e = i;
+  }
+  return 0;
+}

Reply via email to