diff options
author | Marc Glisse <marc.glisse@inria.fr> | 2020-08-04 17:30:16 +0200 |
---|---|---|
committer | Marc Glisse <marc.glisse@inria.fr> | 2020-08-04 17:30:16 +0200 |
commit | ca2b8c082c4f16919071c9f8de8db0b33b54c405 (patch) | |
tree | 457a68e6317228a5710215d659e7234c9da209ad /gcc | |
parent | c79d8335fd5905324b32d65cc0115986301f07eb (diff) | |
download | gcc-ca2b8c082c4f16919071c9f8de8db0b33b54c405.zip gcc-ca2b8c082c4f16919071c9f8de8db0b33b54c405.tar.gz gcc-ca2b8c082c4f16919071c9f8de8db0b33b54c405.tar.bz2 |
Simplify X * C1 == C2 with undefined overflow
this transformation is quite straightforward, without overflow, 3*X==15 is
the same as X==5 and 3*X==5 cannot happen. Adding a single_use restriction
for the first case didn't seem necessary, although of course it can
slightly increase register pressure in some cases.
2020-08-04 Marc Glisse <marc.glisse@inria.fr>
PR tree-optimization/95433
* match.pd (X * C1 == C2): New transformation.
* gcc.c-torture/execute/pr23135.c: Add -fwrapv to avoid
undefined behavior.
* gcc.dg/tree-ssa/pr95433.c: New file.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/match.pd | 14 | ||||
-rw-r--r-- | gcc/testsuite/gcc.c-torture/execute/pr23135.c | 2 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/pr95433.c | 8 |
3 files changed, 23 insertions, 1 deletions
diff --git a/gcc/match.pd b/gcc/match.pd index a052c9e..1372f89 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3779,6 +3779,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (scmp @0 @2) (cmp @0 @2)))))) +/* For integral types with undefined overflow fold + x * C1 == C2 into x == C2 / C1 or false. */ +(for cmp (eq ne) + (simplify + (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) + && wi::to_wide (@1) != 0) + (with { widest_int quot; } + (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1), + TYPE_SIGN (TREE_TYPE (@0)), ")) + (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); }) + { constant_boolean_node (cmp == NE_EXPR, type); }))))) + /* Simplify comparison of something with itself. For IEEE floating-point, we can only do some of these simplifications. */ (for cmp (eq ge le) diff --git a/gcc/testsuite/gcc.c-torture/execute/pr23135.c b/gcc/testsuite/gcc.c-torture/execute/pr23135.c index e740ff5..ef9b7ef 100644 --- a/gcc/testsuite/gcc.c-torture/execute/pr23135.c +++ b/gcc/testsuite/gcc.c-torture/execute/pr23135.c @@ -1,7 +1,7 @@ /* Based on execute/simd-1.c, modified by joern.rennecke@st.com to trigger a reload bug. Verified for gcc mainline from 20050722 13:00 UTC for sh-elf -m4 -O2. */ -/* { dg-options "-Wno-psabi" } */ +/* { dg-options "-Wno-psabi -fwrapv" } */ /* { dg-add-options stack_size } */ #ifndef STACK_SIZE diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr95433.c b/gcc/testsuite/gcc.dg/tree-ssa/pr95433.c new file mode 100644 index 0000000..4e161ee --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr95433.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized" } */ + +int f(int x){return x*7==17;} +int g(int x){return x*3==15;} + +/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */ +/* { dg-final { scan-tree-dump "== 5;" "optimized" } } */ |