]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/91838 - fix FAIL of g++.dg/opt/pr91838.C
authorRichard Biener <rguenther@suse.de>
Thu, 27 Jul 2023 11:08:32 +0000 (13:08 +0200)
committerAndre Vieira <andre.simoesdiasvieira@arm.com>
Tue, 26 Mar 2024 14:57:40 +0000 (14:57 +0000)
The following fixes the lack of simplification of a vector shift
by an out-of-bounds shift value.  For scalars this is done both
by CCP and VRP but vectors are not handled there.  This results
in PR91838 differences in outcome dependent on whether a vector
shift ISA is available and thus vector lowering does or does not
expose scalar shifts here.

The following adds a match.pd pattern to catch uniform out-of-bound
shifts, simplifying them to zero when not sanitizing shift amounts.

PR tree-optimization/91838
* gimple-match-head.cc: Include attribs.h and asan.h.
* generic-match-head.cc: Likewise.
* match.pd (([rl]shift @0 out-of-bounds) -> zero): New pattern.

(cherry picked from commit d1c072a1c3411a6fe29900750b38210af8451eeb)

gcc/generic-match-head.cc
gcc/gimple-match-head.cc
gcc/match.pd

index f011204c5be450663231bdece0596317b37f9f9b..36f1832a05d0c722bf87cfade98e546e0a06a9b8 100644 (file)
@@ -41,6 +41,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-eh.h"
 #include "langhooks.h"
 #include "tree-pass.h"
+#include "attribs.h"
+#include "asan.h"
 
 /* Routine to determine if the types T1 and T2 are effectively
    the same for GENERIC.  If T1 or T2 is not a type, the test
index ec603f9d043c3924ea442bb49b5300a3573503cf..21843d7870b30fc1c0222b049a2d9f00d080188f 100644 (file)
@@ -47,6 +47,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "tm.h"
 #include "gimple-range.h"
 #include "langhooks.h"
+#include "attribs.h"
+#include "asan.h"
 
 /* Forward declarations of the private auto-generated matchers.
    They expect valueized operands in canonical order and do not
index 908959de3e7f4ece01827a3676d85e5c89a53a83..6812853df6754d93d854008685b989c0a633d0c2 100644 (file)
@@ -997,6 +997,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
    (lshift @0 @2)))
 
+/* Shifts by precision or greater result in zero.  */
+(for shift (lshift rshift)
+ (simplify
+  (shift @0 uniform_integer_cst_p@1)
+  (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
+       /* Use a signed compare to leave negative shift counts alone.  */
+       && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
+                    element_precision (type)))
+   { build_zero_cst (type); })))
+
 /* Shifts by constants distribute over several binary operations,
    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
 (for op (plus minus)