diff options
author | Jakub Jelinek <jakub@redhat.com> | 2021-01-15 21:12:14 +0100 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2021-01-15 21:12:14 +0100 |
commit | 0425f4c1b63107bf3bc4778d1fe53c91ace7838d (patch) | |
tree | 48f0ca415fb19a52b755cefdd92d0434efee9831 /gcc/match.pd | |
parent | 5c046034e3ea61dd68965154a398f8f813daf8f2 (diff) | |
download | gcc-0425f4c1b63107bf3bc4778d1fe53c91ace7838d.zip gcc-0425f4c1b63107bf3bc4778d1fe53c91ace7838d.tar.gz gcc-0425f4c1b63107bf3bc4778d1fe53c91ace7838d.tar.bz2 |
match.pd: Generalize the PR64309 simplifications [PR96669]
The following patch generalizes the PR64309 simplifications, so that instead
of working only with constants 1 and 1 it works with any two power of two
constants, and works also for right shift (in that case it rules out the
first one being negative, as it is arithmetic shift then).
2021-01-15 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/96669
* match.pd (((1 << A) & 1) != 0 -> A == 0,
((1 << A) & 1) == 0 -> A != 0): Generalize for 1s replaced by
possibly different power of two constants and to right shift too.
* gcc.dg/tree-ssa/pr96669-1.c: New test.
Diffstat (limited to 'gcc/match.pd')
-rw-r--r-- | gcc/match.pd | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/gcc/match.pd b/gcc/match.pd index e731bdb..84c4ee6 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3117,13 +3117,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (op @0 { build_int_cst (TREE_TYPE (@1), low); }))))))) -/* ((1 << A) & 1) != 0 -> A == 0 - ((1 << A) & 1) == 0 -> A != 0 */ +/* Simplify ((C << x) & D) != 0 where C and D are power of two constants, + either to false if D is smaller (unsigned comparison) than C, or to + x == log2 (D) - log2 (C). Similarly for right shifts. */ (for cmp (ne eq) icmp (eq ne) (simplify - (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop) - (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))) + (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop) + (with { int c1 = wi::clz (wi::to_wide (@1)); + int c2 = wi::clz (wi::to_wide (@2)); } + (if (c1 < c2) + { constant_boolean_node (cmp == NE_EXPR ? false : true, type); } + (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); })))) + (simplify + (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop) + (if (tree_int_cst_sgn (@1) > 0) + (with { int c1 = wi::clz (wi::to_wide (@1)); + int c2 = wi::clz (wi::to_wide (@2)); } + (if (c1 > c2) + { constant_boolean_node (cmp == NE_EXPR ? false : true, type); } + (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))) /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1) (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1) |