public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Fold some equal to and not equal to patterns in match.pd
@ 2015-07-21  9:16 Hurugalawadi, Naveen
  2015-07-21  9:38 ` pinskia
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Hurugalawadi, Naveen @ 2015-07-21  9:16 UTC (permalink / raw)
  To: gcc-patches; +Cc: Pinski, Andrew

[-- Attachment #1: Type: text/plain, Size: 994 bytes --]

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  <apinski@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  <apinski@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.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: gcc_match.patch --]
[-- Type: text/x-patch; name="gcc_match.patch", Size: 2739 bytes --]

--- 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" } } */

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2015-07-24  9:26 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-21  9:16 Fold some equal to and not equal to patterns in match.pd Hurugalawadi, Naveen
2015-07-21  9:38 ` pinskia
2015-07-21 10:39   ` Kyrill Tkachov
2015-07-21 10:56     ` pinskia
2015-07-21 11:09       ` Kyrill Tkachov
2015-07-21 15:21       ` Kyrill Tkachov
2015-07-21  9:51 ` Jakub Jelinek
2015-07-21 19:55   ` Richard Biener
2015-07-22  7:09     ` Andrew Pinski
2015-07-22 11:22       ` Richard Biener
2015-07-22 13:50       ` Segher Boessenkool
2015-07-23 17:06       ` Jeff Law
2015-07-23 17:12         ` Segher Boessenkool
2015-07-24  7:48           ` Jeff Law
2015-07-24  8:01             ` Kai Tietz
2015-07-24  9:31               ` Richard Biener
2015-07-22 11:37 ` Richard Biener

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).