public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-3623] PR c/102245: Disable sign-changing optimization for shifts by zero.
@ 2021-09-17 14:58 Roger Sayle
  0 siblings, 0 replies; only message in thread
From: Roger Sayle @ 2021-09-17 14:58 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:2578a3870ef849dc77e98796600181b64ae4fd61

commit r12-3623-g2578a3870ef849dc77e98796600181b64ae4fd61
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Fri Sep 17 15:57:34 2021 +0100

    PR c/102245: Disable sign-changing optimization for shifts by zero.
    
    Respecting Jakub's suggestion that it may be better to warn-on-valid for
    "if (x << 0)" as the author might have intended "if (x < 0)" [which will
    also warn when x is _Bool], the simplest way to resolve this regression
    is to disable the recently added fold transformation for shifts by zero;
    these will be optimized later (elsewhere).  Guarding against integer_zerop
    is the simplest of three alternatives; the second being to only apply
    this transformation to GIMPLE and not GENERIC, and the third (potentially)
    being to explicitly handle shifts by zero here, with an (if cond then else),
    optimizing the expression to a convert, but awkwardly duplicating a
    more general transformation earlier in match.pd's shift simplifications.
    
    2021-09-17  Roger Sayle  <roger@nextmovesoftware.com>
    
    gcc/ChangeLog
            PR c/102245
            * match.pd (shift optimizations): Disable recent sign-changing
            optimization for shifts by zero, these will be folded later.
    
    gcc/testsuite/ChangeLog
            PR c/102245
            * gcc.dg/Wint-in-bool-context-4.c: New test case.

Diff:
---
 gcc/match.pd                                  |  6 +++--
 gcc/testsuite/gcc.dg/Wint-in-bool-context-4.c | 35 +++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 41f9e6d97f0..097ed2e5dff 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3401,13 +3401,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
      (cmp @0 @2)))))
 
 /* Both signed and unsigned lshift produce the same result, so use
-   the form that minimizes the number of conversions.  */
+   the form that minimizes the number of conversions.  Postpone this
+   transformation until after shifts by zero have been folded.  */
 (simplify
  (convert (lshift:s@0 (convert:s@1 @2) INTEGER_CST@3))
  (if (INTEGRAL_TYPE_P (type)
       && tree_nop_conversion_p (type, TREE_TYPE (@0))
       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
-      && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type))
+      && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type)
+      && !integer_zerop (@3))
   (lshift (convert @2) @3)))
 
 /* Simplifications of conversions.  */
diff --git a/gcc/testsuite/gcc.dg/Wint-in-bool-context-4.c b/gcc/testsuite/gcc.dg/Wint-in-bool-context-4.c
new file mode 100644
index 00000000000..0e96dd7bd7c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wint-in-bool-context-4.c
@@ -0,0 +1,35 @@
+/* PR c/102245 */
+/* { dg-options "-Wint-in-bool-context" } */
+/* { dg-do compile } */
+
+_Bool test1(_Bool x)
+{
+  return !(x << 0);  /* { dg-warning "boolean context" } */
+}
+
+_Bool test2(_Bool x)
+{
+  return !(x << 1);  /* { dg-warning "boolean context" } */
+}
+
+_Bool test3(_Bool x, int y)
+{
+  return !(x << y);  /* { dg-warning "boolean context" } */
+}
+
+_Bool test4(int x, int y)
+{
+  return !(x << y);  /* { dg-warning "boolean context" } */
+}
+
+_Bool test5(int x, int y)
+{
+  return !((x << y) << 0);  /* { dg-warning "boolean context" } */
+}
+
+int test6(_Bool x)
+{
+  int v = 0;
+  return (v & ~1L) | (1L & (x << 0));  /* { dg-bogus "boolean context" } */
+}
+


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-09-17 14:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-17 14:58 [gcc r12-3623] PR c/102245: Disable sign-changing optimization for shifts by zero Roger Sayle

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).