diff mbox

[PR23664] Fold (a & C1) + (b & C2) to (a & C1) | (b & C2) iff (C1 & C2) == 0

Message ID SN2PR0701MB102400298F04C7A61E748F618E920@SN2PR0701MB1024.namprd07.prod.outlook.com
State New
Headers show

Commit Message

Hurugalawadi, Naveen July 7, 2015, 4:51 a.m. UTC
Hi,

Please find attached the patch "PR23664.patch" that converts the pattern:-
(a & C1) + (b & C2) into (a & C1) | (b & C2) iff (C1 & C2) == 0.

Please review and let me know if its okay.

Regression tested on AARH64 and x86_64.

Thanks,
Naveen

gcc/testsuite/ChangeLog:

2015-07-07  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>

	PR middle-end/23664
	* gcc.dg/pr23664.c: New test.

gcc/ChangeLog:

2015-07-07  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>

	PR middle-end/23664
	* match.pd ((plus (bit_and @0 INTEGER_CST@1)
	(bit_and @2 INTEGER_CST@3)) : New simplifier.

Comments

Marc Glisse July 7, 2015, 5:56 a.m. UTC | #1
On Tue, 7 Jul 2015, Hurugalawadi, Naveen wrote:

> Please find attached the patch "PR23664.patch" that converts the pattern:-
> (a & C1) + (b & C2) into (a & C1) | (b & C2) iff (C1 & C2) == 0.

We already have the following. I believe it would be better to merge them 
(use a 'for').

(simplify
  (bit_xor (convert1? (bit_and@4 @0 INTEGER_CST@1))
           (convert2? (bit_and@5 @2 INTEGER_CST@3)))
  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
       && tree_nop_conversion_p (type, TREE_TYPE (@2))
       && wi::bit_and (@1, @3) == 0)
   (bit_ior (convert @4) (convert @5))))
diff mbox

Patch

--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -529,6 +529,12 @@  along with GCC; see the file COPYING3.  If not see
   (bitop (bit_and:c @0 @1) (bit_and @2 @1))
   (bit_and (bitop @0 @2) @1)))
 
+/* Simplify (a & C1) + (b & C2) to (a & C1) | (b & C2) iff (C1 & C2) == 0.  */
+(simplify
+  (plus (bit_and @0 INTEGER_CST@1) (bit_and @2 INTEGER_CST@3))
+  (if (wi::bit_and (@1, @3) == 0)
+   (bit_ior (bit_and @0 @1) (bit_and @2 @3))))
+
 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
 (simplify
   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)

--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr23664.c
@@ -0,0 +1,12 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+/* Testcase for PR23664.  */
+
+int
+f (int a, int b)
+{
+  return (a & 0xF) + (b & 0xF0);
+}
+
+/* { dg-final { scan-tree-dump "\|" "optimized" } } */