When ext_dce_try_optimize_extension turns an extension such as
(set (reg:DI 145) (sign_extend:DI (subreg:SI (reg:DI 100) 0)))
into a plain move
(set (reg:DI 145) (reg:DI 100))
the upper bits of the destination are no longer guaranteed to be a sign
extension of the lower 32 bits. The existing code only wipes the notes
on the converted insn itself, but insns that use the destination register
may still carry REG_EQUAL/REG_EQUIV notes containing the original extension
expression, e.g.
(set (reg:DI 96) (reg:DI 145))
(expr_list:REG_EQUAL (sign_extend:DI (subreg:SI (reg:DI 100) 0)))
These notes are now incorrect. Later passes such as combine may take
them to mean that the upper 33 bits of the register are all copies of
the sign bit, and make wrong transformations based on that.
This patch walks the use chain of the destination register and removes
any REG_EQUAL or REG_EQUIV note that contains the original extension
expression.
PR rtl-optimization/127558
gcc/ChangeLog:
* ext-dce.cc (ext_dce_try_optimize_extension): Remove
REG_EQUAL/REG_EQUIV notes on users of the destination
register that reference the eliminated extension.
gcc/testsuite/ChangeLog:
* gcc.dg/torture/pr127558.c: New test.
---
gcc/ext-dce.cc | 37 ++++++++++++++++++++++++-
gcc/testsuite/gcc.dg/torture/pr127558.c | 35 +++++++++++++++++++++++
2 files changed, 71 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.dg/torture/pr127558.c
diff --git a/gcc/ext-dce.cc b/gcc/ext-dce.cc
index e431bb0e404..fc8253b299e 100644
--- a/gcc/ext-dce.cc
+++ b/gcc/ext-dce.cc
@@ -637,7 +637,42 @@ ext_dce_try_optimize_extension (rtx_insn *insn, rtx set)
sign or zero extended. That note is no longer valid since we've
just removed the extension. Just wipe the notes. */
if (ok)
- remove_reg_equal_equiv_notes (insn, false);
+ {
+ remove_reg_equal_equiv_notes (insn, false);
+
+ auto_vec<rtx_insn *> to_invalidate;
+ for (df_ref use = DF_REG_USE_CHAIN (REGNO (x));
+ use;
+ use = DF_REF_NEXT_REG (use))
+ {
+ rtx_insn *use_insn = DF_REF_INSN (use);
+ if (!use_insn || !INSN_P (use_insn))
+ continue;
+
+ rtx reg_equal_note = find_reg_equal_equiv_note (use_insn);
+ if (!reg_equal_note)
+ continue;
+
+ rtx reg_equal = XEXP (reg_equal_note, 0);
+ if (!reg_equal)
+ continue;
+
+ subrtx_iterator::array_type array;
+ FOR_EACH_SUBRTX (iter, array, reg_equal, NONCONST)
+ {
+ if (rtx_equal_p (*iter, src))
+ {
+ to_invalidate.safe_push (use_insn);
+ break;
+ }
+ }
+ }
+
+ rtx_insn *use_insn;
+ unsigned int i;
+ FOR_EACH_VEC_ELT (to_invalidate, i, use_insn)
+ remove_reg_equal_equiv_notes (use_insn, false);
+ }
}
else
{
diff --git a/gcc/testsuite/gcc.dg/torture/pr127558.c
b/gcc/testsuite/gcc.dg/torture/pr127558.c
new file mode 100644
index 00000000000..463228e8e53
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr127558.c
@@ -0,0 +1,35 @@
+/* PR rtl-optimization/127558 */
+/* { dg-do run { target bitint } } */
+
+#if __BITINT_MAXWIDTH__ >= 17
+volatile long A = 0;
+long B = 0x80000001;
+long D = 1;
+long m[2];
+long e = 1;
+
+__attribute__((noipa)) void
+foo (void)
+{
+ int z = 0;
+ _BitInt(17) g = A;
+ for (; A; z = (int)B)
+ ;
+ unsigned long long t = (int)(D ? B : -g) ^ (_BitInt(7))e;
+ m[0] = m[(unsigned)B & 1] / B;
+ int r = 0 == z ? 2 * B : 0;
+ unsigned long long res = (unsigned long long)r * t;
+
+ if (res != 18446744069414584320ULL)
+ __builtin_abort ();
+}
+#endif
+
+int
+main (void)
+{
+#if __BITINT_MAXWIDTH__ >= 17
+ foo ();
+#endif
+ return 0;
+}
--
2.20.1