]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
rtlanal: Fix set_noop_p for volatile loads or stores [PR114768]
authorJakub Jelinek <jakub@redhat.com>
Fri, 19 Apr 2024 06:47:53 +0000 (08:47 +0200)
committerJakub Jelinek <jakub@redhat.com>
Sun, 21 Apr 2024 04:08:08 +0000 (06:08 +0200)
On the following testcase, combine propagates the mem/v load into mem store
with the same address and then removes it, because noop_move_p says it is a
no-op move.  If it was the other way around, i.e. mem/v store and mem load,
or both would be mem/v, it would be kept.
The problem is that rtx_equal_p never checks any kind of flags on the rtxes
(and I think it would be quite dangerous to change it at this point), and
set_noop_p checks side_effects_p on just one of the operands, not both.
In the MEM <- MEM set, it only checks it on the destination, in
store to ZERO_EXTRACT only checks it on the source.

The following patch adds the missing side_effects_p checks.

2024-04-19  Jakub Jelinek  <jakub@redhat.com>

PR rtl-optimization/114768
* rtlanal.cc (set_noop_p): Don't return true for MEM <- MEM
sets if src has side-effects or for stores into ZERO_EXTRACT
if ZERO_EXTRACT operand has side-effects.

* gcc.dg/pr114768.c: New test.

(cherry picked from commit 9f295847a9c32081bdd0fe908ffba58e830a24fb)

gcc/rtlanal.cc
gcc/testsuite/gcc.dg/pr114768.c [new file with mode: 0644]

index 6b02428b735030ec9d51a4cc3d85fcd5f33748b1..7edb07a9b2c96cb6a7c3cc3e5a4f7afef21c837a 100644 (file)
@@ -1639,12 +1639,15 @@ set_noop_p (const_rtx set)
     return 1;
 
   if (MEM_P (dst) && MEM_P (src))
-    return rtx_equal_p (dst, src) && !side_effects_p (dst);
+    return (rtx_equal_p (dst, src)
+           && !side_effects_p (dst)
+           && !side_effects_p (src));
 
   if (GET_CODE (dst) == ZERO_EXTRACT)
-    return rtx_equal_p (XEXP (dst, 0), src)
-          && !BITS_BIG_ENDIAN && XEXP (dst, 2) == const0_rtx
-          && !side_effects_p (src);
+    return (rtx_equal_p (XEXP (dst, 0), src)
+           && !BITS_BIG_ENDIAN && XEXP (dst, 2) == const0_rtx
+           && !side_effects_p (src)
+           && !side_effects_p (XEXP (dst, 0)));
 
   if (GET_CODE (dst) == STRICT_LOW_PART)
     dst = XEXP (dst, 0);
diff --git a/gcc/testsuite/gcc.dg/pr114768.c b/gcc/testsuite/gcc.dg/pr114768.c
new file mode 100644 (file)
index 0000000..ffe3b36
--- /dev/null
@@ -0,0 +1,10 @@
+/* PR rtl-optimization/114768 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-rtl-final" } */
+/* { dg-final { scan-rtl-dump "\\\(mem/v:" "final" } } */
+
+void
+foo (int *p)
+{
+  *p = *(volatile int *) p;
+}