diff mbox

[2/4] match.pd: Add x & ~(x & y) -> x & ~y pattern

Message ID 5543C5E7.8020209@redhat.com
State New
Headers show

Commit Message

Jeff Law May 1, 2015, 6:28 p.m. UTC
On 01/21/2015 03:49 AM, Rasmus Villemoes wrote:
> gcc.dg/20150120-2.c: New test
>
> Clearing a certain subset of bits, for example to round down x to a
> multiple of a power of 2, is sometimes written x & ~(x & y), where y
> may or may not be a constant. It is shorter to use x & ~y,
> particularly when y is a constant. gcc already does this when it is
> spelled x - (x & y) or x ^ (x & y).
>
> Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
> ---
>   gcc/match.pd                      |  6 ++++++
>   gcc/testsuite/gcc.dg/20150120-2.c | 32 ++++++++++++++++++++++++++++++++
>   2 files changed, 38 insertions(+)
>   create mode 100644 gcc/testsuite/gcc.dg/20150120-2.c
I combined this pattern with its bit-ior 'dual' into a single pattern. 
Then bootstrapped, regression tested, wrote the ChangeLog and installed 
the result onto the trunk.

I'm attaching the final patch which includes the changes in #1, #2 and 
#3 of this series for archival purposes.

jeff
commit 0a66cfe379a8217c69705535303626d12aca0c5f
Author: law <law@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Fri May 1 18:25:12 2015 +0000

    	* match.pd: New simplification patterns.
    	(x + (x & 1))  -> ((x + 1) & ~1)
    	(x & ~(x & y)) -> ((x & ~y))
    	(x | ~(x | y)) -> ((x | ~y))
    
    	* gcc.dg/20150120-1.c: New test.
    	* gcc.dg/20150120-2.c: New test.
    	* gcc.dg/20150120-3.c: New test.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@222697 138bc75d-0d04-0410-961f-82ee72b054a4
diff mbox

Patch

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 65816c7..e006b26 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,10 @@ 
+2015-05-01  Rasmus Villemoes  <rv@rasmusvillemoes.dk>
+
+	* match.pd: New simplification patterns.
+	(x + (x & 1))  -> ((x + 1) & ~1)
+	(x & ~(x & y)) -> ((x & ~y))
+	(x | ~(x | y)) -> ((x | ~y))
+
 2015-05-01  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>
 
 	* target.def (attribute_table): Mention that struct attribute_spec
diff --git a/gcc/match.pd b/gcc/match.pd
index fc374de..87ecaf1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -255,6 +255,20 @@  along with GCC; see the file COPYING3.  If not see
   (bitop @0 @0)
   (non_lvalue @0)))
 
+/* x + (x & 1) -> (x + 1) & ~1 */
+(simplify
+ (plus:c @0 (bit_and@2 @0 integer_onep@1))
+ (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2))
+  (bit_and (plus @0 @1) (bit_not @1))))
+
+/* x & ~(x & y) -> x & ~y */
+/* x | ~(x | y) -> x | ~y  */
+(for bitop (bit_and bit_ior)
+  (simplify
+    (bitop:c @0 (bit_not (bitop:c@2 @0 @1)))
+      (if (TREE_CODE (@2) != SSA_NAME || has_single_use (@2))
+	(bitop @0 (bit_not @1)))))
+
 (simplify
  (abs (negate @0))
  (abs @0))
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index bfdde3b..2aedc46 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,9 @@ 
+2015-05-01  Rasmus Villemoes  <rv@rasmusvillemoes.dk>
+
+	* gcc.dg/20150120-1.c: New test.
+	* gcc.dg/20150120-2.c: New test.
+	* gcc.dg/20150120-3.c: New test.
+
 2015-05-01  David Edelsohn  <dje.gcc@gmail.com>
 
 	* gcc.dg/debug/pr65771.c: Add "dg-add-options tls".
diff --git a/gcc/testsuite/gcc.dg/20150120-1.c b/gcc/testsuite/gcc.dg/20150120-1.c
new file mode 100644
index 0000000..18906c4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/20150120-1.c
@@ -0,0 +1,51 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+
+/* x + (x & 1) -> (x + 1) & ~1 */
+int
+fn1 (int x)
+{
+	return x + (x & 1);
+}
+int
+fn2 (int x)
+{
+	return (x & 1) + x;
+}
+int
+fn3 (int x)
+{
+	return x + (1 & x);
+}
+int
+fn4 (int x)
+{
+	return (1 & x) + x;
+}
+unsigned int
+fn5 (unsigned int x)
+{
+	return x + (x & 1);
+}
+unsigned int
+fn6 (unsigned int x)
+{
+	return (x & 1) + x;
+}
+unsigned int
+fn7 (unsigned int x)
+{
+	return x + (x % 2);
+}
+unsigned int
+fn8 (unsigned int x)
+{
+	return (x % 2) + x;
+}
+unsigned int
+fn9 (unsigned int x)
+{
+	return (1LL & x) + x;
+}
+
+/* { dg-final { scan-tree-dump-times "x \\+ 1" 9 "original" } } */
diff --git a/gcc/testsuite/gcc.dg/20150120-2.c b/gcc/testsuite/gcc.dg/20150120-2.c
new file mode 100644
index 0000000..976b654
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/20150120-2.c
@@ -0,0 +1,32 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+
+/* x & ~(x & y) -> x & ~y */
+int fn1 (int x, int y)
+{
+	return x & ~(x & y);
+}
+int fn2 (int x, int y)
+{
+	return ~(x & y) & x;
+}
+int fn3 (int x, int y)
+{
+	return x & ~(y & x);
+}
+int fn4 (int x, int y)
+{
+	return ~(y & x) & x;
+}
+int fn5 (int z)
+{
+	return z & ~(z & 3);
+}
+int fn6 (int z)
+{
+	return ~(z & 3) & z;
+}
+
+
+/* { dg-final { scan-tree-dump-times "~y & x" 4 "original" } } */
+/* { dg-final { scan-tree-dump-times "z & -4" 2 "original" } } */
diff --git a/gcc/testsuite/gcc.dg/20150120-3.c b/gcc/testsuite/gcc.dg/20150120-3.c
new file mode 100644
index 0000000..322556f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/20150120-3.c
@@ -0,0 +1,32 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+
+/* x | ~(x | y) -> x | ~y */
+int fn1 (int x, int y)
+{
+	return x | ~(x | y);
+}
+int fn2 (int x, int y)
+{
+	return ~(x | y) | x;
+}
+int fn3 (int x, int y)
+{
+	return x | ~(y | x);
+}
+int fn4 (int x, int y)
+{
+	return ~(y | x) | x;
+}
+int fn5 (int z)
+{
+	return z | ~(z | 3);
+}
+int fn6 (int z)
+{
+	return ~(z | 3) | z;
+}
+
+
+/* { dg-final { scan-tree-dump-times "~y \\| x" 4 "original" } } */
+/* { dg-final { scan-tree-dump-times "z \\| -4" 2 "original" } } */