PR target/107172: Avoid "unusual" MODE_CC comparisons in simplify-rtx.cc

Message ID 024501d9900a$682ef1c0$388cd540$@nextmovesoftware.com
State Accepted
Headers
Series PR target/107172: Avoid "unusual" MODE_CC comparisons in simplify-rtx.cc |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Roger Sayle May 26, 2023, 7:43 p.m. UTC
  I believe that a better (or supplementary) fix to PR target/107172 is to
avoid
producing incorrect (but valid) RTL in simplify_const_relational_operation
when
presented with questionable (obviously invalid) expressions, such as those
produced during combine.  Just as with the "first do no harm" clause with
the
Hippocratic Oath, simplify-rtx (probably) shouldn't unintentionally
transform
invalid RTL expressions, into incorrect (non-equivalent) but valid RTL that
may be inappropriately recognized by recog.

In this specific case, many GCC backends represent their flags register via
MODE_CC, whose representation is intentionally "opaque" to the middle-end.
The only use of MODE_CC comprehensible to the middle-end's RTL optimizers
is relational comparisons between the result of a COMPARE rtx (op0) and zero
(op1).  Any other uses of MODE_CC should be left alone, and some might argue
indicate representational issues in the backend.

In practice, CPUs occasionally have numerous instructions that affect the
flags register(s) other than comparisons [AVR's setc, powerpc's mtcrf,
x86's clc, stc and cmc and x86_64's ptest that sets C and Z flags in
non-obvious ways, c.f. PR target/109973].  Currently care has to be taken,
wrapping these in UNSPEC, to avoid combine inappropriately merging flags
setters with flags consumers (such as conditional jumps).  It's safer to
teach simplify_const_relational_operation not to modify expressions that
it doesn't understand/recognize.

This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check, both with and without --target_board=unix{-m32}
with no new failures.  Ok for mainline?


2023-05-26  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	* simplify-rtx.cc (simplify_const_relational_operation): Return
early
	if we have a MODE_CC comparison that isn't a COMPARE against
const0_rtx.


Thanks in advance,
Roger
--
  

Comments

Jeff Law May 29, 2023, 11:25 p.m. UTC | #1
On 5/26/23 13:43, Roger Sayle wrote:
> 
> I believe that a better (or supplementary) fix to PR target/107172 is to
> avoid
> producing incorrect (but valid) RTL in simplify_const_relational_operation
> when
> presented with questionable (obviously invalid) expressions, such as those
> produced during combine.  Just as with the "first do no harm" clause with
> the
> Hippocratic Oath, simplify-rtx (probably) shouldn't unintentionally
> transform
> invalid RTL expressions, into incorrect (non-equivalent) but valid RTL that
> may be inappropriately recognized by recog.
> 
> In this specific case, many GCC backends represent their flags register via
> MODE_CC, whose representation is intentionally "opaque" to the middle-end.
> The only use of MODE_CC comprehensible to the middle-end's RTL optimizers
> is relational comparisons between the result of a COMPARE rtx (op0) and zero
> (op1).  Any other uses of MODE_CC should be left alone, and some might argue
> indicate representational issues in the backend.
> 
> In practice, CPUs occasionally have numerous instructions that affect the
> flags register(s) other than comparisons [AVR's setc, powerpc's mtcrf,
> x86's clc, stc and cmc and x86_64's ptest that sets C and Z flags in
> non-obvious ways, c.f. PR target/109973].  Currently care has to be taken,
> wrapping these in UNSPEC, to avoid combine inappropriately merging flags
> setters with flags consumers (such as conditional jumps).  It's safer to
> teach simplify_const_relational_operation not to modify expressions that
> it doesn't understand/recognize.
> 
> This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
> and make -k check, both with and without --target_board=unix{-m32}
> with no new failures.  Ok for mainline?
> 
> 
> 2023-05-26  Roger Sayle  <roger@nextmovesoftware.com>
> 
> gcc/ChangeLog
> 	* simplify-rtx.cc (simplify_const_relational_operation): Return
> early
> 	if we have a MODE_CC comparison that isn't a COMPARE against
> const0_rtx.
OK
jeff
  

Patch

diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
index d4aeebc..d6444b4 100644
--- a/gcc/simplify-rtx.cc
+++ b/gcc/simplify-rtx.cc
@@ -6120,6 +6120,12 @@  simplify_const_relational_operation (enum rtx_code code,
 	      || (GET_MODE (op0) == VOIDmode
 		  && GET_MODE (op1) == VOIDmode));
 
+  /* We only handle MODE_CC comparisons that are COMPARE against zero.  */
+  if (GET_MODE_CLASS (mode) == MODE_CC
+      && (op1 != const0_rtx
+	  || GET_CODE (op0) != COMPARE))
+    return NULL_RTX;
+
   /* If op0 is a compare, extract the comparison arguments from it.  */
   if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
     {