Hi,
Please find attached the patch which performs following patterns folding
in match.pd:-
a ==/!= a p+ b to b ==/!= 0.
a << N ==/!= 0 to a&(-1>>N) ==/!= 0.
a * N ==/!= 0 where N is a power of 2 to a & (-1<<N2) ==/!= 0 where N2 is log2
of N.
Please review the same and let us know if its okay?
Regression Tested on X86_64.
On Behalf of Andrew Pinski.
Thanks,
gcc/testsuite/ChangeLog:
2015-01-21 Andrew Pinski <apin...@cavium.com>
* testsuite/gcc.dg/tree-ssa/compare-shiftmult-1.c: New testcase.
* testsuite/gcc.dg/tree-ssa/compare_pointers-1.c: New testcase.
gcc/ChangeLog:
2015-01-21 Andrew Pinski <apin...@cavium.com>
* match.pd (define_predicates): Add integer_pow2p.
Add pattern for folding of a ==/!= a p+ b to b ==/!= 0.
(unsigned_integral_valued_p): New match.
Add pattern for folding of a<<N ==/!= 0 to a&(-1>>N) ==/!= 0.
Add pattern for folding of a*N ==/!= 0 where N is a power of 2
to a&(-1<<N2) ==/!= 0 where N2 is log2 of N.
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -29,7 +29,8 @@ along with GCC; see the file COPYING3. If not see
integer_each_onep integer_truep
real_zerop real_onep real_minus_onep
CONSTANT_CLASS_P
- tree_expr_nonnegative_p)
+ tree_expr_nonnegative_p
+ integer_pow2p)
/* Operator lists. */
(define_operator_list tcc_comparison
@@ -104,6 +105,35 @@ along with GCC; see the file COPYING3. If not see
(if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
@1))
+/* Fold a ==/!= a p + b to b ==/!= 0. */
+(for op (ne eq)
+ (simplify
+ (op:c @0 (pointer_plus @0 @1))
+ (op @1 { build_zero_cst (TREE_TYPE (@1)); })))
+
+(match unsigned_integral_valued_p
+ @0
+ (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))))
+
+/* a << N ==/!= 0 to a & (-1>>N) ==/!= 0. */
+(for op (ne eq)
+ (simplify
+ (op (lshift unsigned_integral_valued_p@0 INTEGER_CST@1) integer_zerop@2)
+ (op (bit_and @0 (rshift { build_minus_one_cst (TREE_TYPE (@0)); }
+ @1))
+ @2)))
+
+/* a * N ==/!= 0 where N is a power of 2 to a & (-1<<N2) ==/!= 0
+ where N2 is log2 of N. */
+(for op (ne eq)
+ (simplify
+ (op (mult:c unsigned_integral_valued_p@0 integer_pow2p@1) integer_zerop@2)
+ (with { tree n2 = build_int_cst (TREE_TYPE (@0),
+ wi::exact_log2 (@1)); }
+ (op (bit_and @0 (rshift { build_minus_one_cst (TREE_TYPE (@0)); }
+ { n2 ; }))
+ @2))))
+
/* In IEEE floating point, x*1 is not equivalent to x for snans.
Likewise for complex arithmetic with signed zeros. */
(simplify
new file mode 100644
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/compare-shiftmult-1.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-forwprop1-details" } */
+
+int f(unsigned c)
+{
+ unsigned a = c*4;
+ return (a == 0);
+}
+
+/* { dg-final { scan-tree-dump-times "gimple_simplified to _\[0-9\] = c_\[0-9\].D. & 1073741823" 2 "forwprop1" } } */
+int f1(unsigned c)
+{
+ unsigned a = c<<2;
+ return (a != 0);
+}
+
+/* { dg-final { cleanup-tree-dump "forwprop1" } } */
new file mode 100644
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/compare_pointers-1.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-forwprop1-details" } */
+
+int f(char *b, __SIZE_TYPE__ c)
+{
+ char *a = b + c;
+ return (a == b);
+}
+
+/* { dg-final { scan-tree-dump-times "gimple_simplified to _\[0-9\] = c_\[0-9\].D. == 0" 1 "forwprop1" } } */
+int f1(char *b, __SIZE_TYPE__ c)
+{
+ char *a = b + c;
+ return (a != b);
+}
+
+/* { dg-final { scan-tree-dump-times "gimple_simplified to _\[0-9\] = c_\[0-9\].D. != 0" 1 "forwprop1" } } */
+/* { dg-final { scan-tree-dump-times "Applying pattern" 2 "forwprop1" } } */
+/* { dg-final { cleanup-tree-dump "forwprop1" } } */