public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 2/2] rtl-optimization/113255 - avoid re-associating REG_POINTER MINUS
@ 2024-02-01 14:20 Richard Biener
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Biener @ 2024-02-01 14:20 UTC (permalink / raw)
  To: gcc-patches

The following avoids re-associating

 (minus:DI (reg/f:DI 119)
    (minus:DI (reg/f:DI 120)
        (reg/f:DI 114)))

into

 (minus:DI (plus:DI (reg/f:DI 114)
        (reg/f:DI 119))
    (reg/f:DI 120))

as that possibly confuses the REG_POINTER heuristics of RTL
alias analysis.  This happens to miscompile the PRs testcase
during DSE which expands addresses via CSELIB which eventually
simplifies what it substituted to.  The original code does
the innocent ptr - (ptr2 - ptr2'), bias a pointer by the
difference of two other pointers.

--

This is what I propose for the PR for branches, I have not made much
progress with fixing the fallout on the RTL alias analysis change
on trunk, so this is the alternative if we decide to revert that.

Bootstrapped and tested on x86_64-unknown-linux-gnu on the gcc-13
branch, bootstrapped after reverting of the previous fix on
x86_64-unknown-linux-gnu on trunk, testing is still ongoing there.

OK?  Any preference for trunk?

Thanks,
Richard.

	PR rtl-optimization/113255
	* simplify-rtx.cc (simplify_context::simplify_binary_operation_1):
	Do not re-associate a MINUS with a REG_POINTER op0.
---
 gcc/simplify-rtx.cc | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
index ee75079917f..0108d0aa3bd 100644
--- a/gcc/simplify-rtx.cc
+++ b/gcc/simplify-rtx.cc
@@ -3195,11 +3195,15 @@ simplify_context::simplify_binary_operation_1 (rtx_code code,
          canonicalize (minus A (plus B C)) to (minus (minus A B) C).
 	 Don't use the associative law for floating point.
 	 The inaccuracy makes it nonassociative,
-	 and subtle programs can break if operations are associated.  */
+	 and subtle programs can break if operations are associated.
+	 Don't use the associative law when subtracting a MINUS from
+	 a REG_POINTER as that can trick find_base_term into discovering
+	 the wrong base.  */
 
       if (INTEGRAL_MODE_P (mode)
 	  && (plus_minus_operand_p (op0)
-	      || plus_minus_operand_p (op1))
+	      || ((!REG_P (op0) || !REG_POINTER (op0))
+		  && plus_minus_operand_p (op1)))
 	  && (tem = simplify_plus_minus (code, mode, op0, op1)) != 0)
 	return tem;
 
-- 
2.35.3

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-02-06  9:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20240201142121.B149B38582BB@sourceware.org>
2024-02-02 14:40 ` [PATCH 2/2] rtl-optimization/113255 - avoid re-associating REG_POINTER MINUS Jeff Law
2024-02-05  8:15   ` Richard Biener
2024-02-05 15:43     ` Jeff Law
2024-02-06  8:59       ` Richard Biener
2024-02-01 14:20 Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).