* [PATCH] tree-optimization/91838 - fix FAIL of g++.dg/opt/pr91838.C
@ 2023-07-27 12:00 Richard Biener
0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2023-07-27 12:00 UTC (permalink / raw)
To: gcc-patches; +Cc: Jakub Jelinek
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.
Bootstrapped and tested on x86_64-unknown-linux-gnu.
OK?
Thanks,
Richard.
PR tree-optimization/91838
* match.pd (([rl]shift @0 out-of-bounds) -> zero): New pattern.
---
gcc/match.pd | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/gcc/match.pd b/gcc/match.pd
index a443dc48634..eace7d635e7 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1059,6 +1059,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 (!(flag_sanitize & 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)
--
2.35.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tree-optimization/91838 - fix FAIL of g++.dg/opt/pr91838.C
2023-07-27 13:07 ` Richard Biener
@ 2023-07-27 13:09 ` Jakub Jelinek
0 siblings, 0 replies; 4+ messages in thread
From: Jakub Jelinek @ 2023-07-27 13:09 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc-patches
On Thu, Jul 27, 2023 at 01:07:58PM +0000, Richard Biener wrote:
> On Thu, 27 Jul 2023, Jakub Jelinek wrote:
>
> > On Thu, Jul 27, 2023 at 12:00:56PM +0000, Richard Biener wrote:
> > > 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.
> > >
> > > Bootstrapped and tested on x86_64-unknown-linux-gnu.
> > >
> > > OK?
> > >
> > > Thanks,
> > > Richard.
> > >
> > > PR tree-optimization/91838
> > > * match.pd (([rl]shift @0 out-of-bounds) -> zero): New pattern.
> >
> > The !(flag_sanitize & SANITIZE_SHIFT_EXPONENT)
> > should be !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT)
> > or maybe even
> > GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT)
> > because the shift ubsan instrumentation is done on GENERIC, so it can be
> > optimized on GIMPLE even with ubsan.
> >
> > Otherwise LGTM.
>
> So like the following, will push after re-testing succeeded.
Yes, thanks.
Jakub
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tree-optimization/91838 - fix FAIL of g++.dg/opt/pr91838.C
2023-07-27 12:30 ` Jakub Jelinek
@ 2023-07-27 13:07 ` Richard Biener
2023-07-27 13:09 ` Jakub Jelinek
0 siblings, 1 reply; 4+ messages in thread
From: Richard Biener @ 2023-07-27 13:07 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: gcc-patches
On Thu, 27 Jul 2023, Jakub Jelinek wrote:
> On Thu, Jul 27, 2023 at 12:00:56PM +0000, Richard Biener wrote:
> > 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.
> >
> > Bootstrapped and tested on x86_64-unknown-linux-gnu.
> >
> > OK?
> >
> > Thanks,
> > Richard.
> >
> > PR tree-optimization/91838
> > * match.pd (([rl]shift @0 out-of-bounds) -> zero): New pattern.
>
> The !(flag_sanitize & SANITIZE_SHIFT_EXPONENT)
> should be !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT)
> or maybe even
> GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT)
> because the shift ubsan instrumentation is done on GENERIC, so it can be
> optimized on GIMPLE even with ubsan.
>
> Otherwise LGTM.
So like the following, will push after re-testing succeeded.
Thanks,
Richard.
From c6d348acdc2143fc4c2849e33075a3975fe29b26 Mon Sep 17 00:00:00 2001
From: Richard Biener <rguenther@suse.de>
Date: Thu, 27 Jul 2023 13:08:32 +0200
Subject: [PATCH] tree-optimization/91838 - fix FAIL of g++.dg/opt/pr91838.C
To: gcc-patches@gcc.gnu.org
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.
---
gcc/generic-match-head.cc | 2 ++
gcc/gimple-match-head.cc | 2 ++
gcc/match.pd | 10 ++++++++++
3 files changed, 14 insertions(+)
diff --git a/gcc/generic-match-head.cc b/gcc/generic-match-head.cc
index b4b5bc88f4b..a71c0727b0b 100644
--- a/gcc/generic-match-head.cc
+++ b/gcc/generic-match-head.cc
@@ -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
diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc
index d795066e53e..5d6d26d009b 100644
--- a/gcc/gimple-match-head.cc
+++ b/gcc/gimple-match-head.cc
@@ -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"
tree do_valueize (tree, tree (*)(tree), bool &);
tree do_valueize (tree (*)(tree), tree);
diff --git a/gcc/match.pd b/gcc/match.pd
index a443dc48634..fcb1a735507 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1059,6 +1059,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)
--
2.35.3
k
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] tree-optimization/91838 - fix FAIL of g++.dg/opt/pr91838.C
[not found] <81140.123072708005901359@us-mta-160.us.mimecast.lan>
@ 2023-07-27 12:30 ` Jakub Jelinek
2023-07-27 13:07 ` Richard Biener
0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2023-07-27 12:30 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc-patches
On Thu, Jul 27, 2023 at 12:00:56PM +0000, Richard Biener wrote:
> 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.
>
> Bootstrapped and tested on x86_64-unknown-linux-gnu.
>
> OK?
>
> Thanks,
> Richard.
>
> PR tree-optimization/91838
> * match.pd (([rl]shift @0 out-of-bounds) -> zero): New pattern.
The !(flag_sanitize & SANITIZE_SHIFT_EXPONENT)
should be !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT)
or maybe even
GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT)
because the shift ubsan instrumentation is done on GENERIC, so it can be
optimized on GIMPLE even with ubsan.
Otherwise LGTM.
Jakub
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-07-27 13:09 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-27 12:00 [PATCH] tree-optimization/91838 - fix FAIL of g++.dg/opt/pr91838.C Richard Biener
[not found] <81140.123072708005901359@us-mta-160.us.mimecast.lan>
2023-07-27 12:30 ` Jakub Jelinek
2023-07-27 13:07 ` Richard Biener
2023-07-27 13:09 ` Jakub Jelinek
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).