+2015-06-26  Marek Polacek  <polacek@redhat.com>
+
+       * match.pd ((x | y) & ~(x & y) -> x ^ y,
+       (x | y) & (~x ^ y) -> x & y): New patterns.
+
 2015-06-26  Richard Sandiford  <richard.sandiford@arm.com>
 
        * rtl.h (emit): Add an optional boolean parameter to control
 
  (minus (bit_ior @0 @1) (bit_and @0 @1))
  (bit_xor @0 @1))
 
+/* (x | y) & ~(x & y) -> x ^ y */
+(simplify
+ (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
+ (bit_xor @0 @1))
+
+/* (x | y) & (~x ^ y) -> x & y */
+(simplify
+ (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
+ (bit_and @0 @1))
+
 (simplify
  (abs (negate @0))
  (abs @0))
 
+2015-06-26  Marek Polacek  <polacek@redhat.com>
+
+       * gcc.dg/fold-and-1.c: New test.
+       * gcc.dg/fold-and-2.c: New test.
+
 2015-06-26  Eric Botcazou  <ebotcazou@adacore.com>
 
        * gnat.dg/warn11.adb: Add missing dg directive.
 
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+int
+fn1 (int x, int y)
+{
+  int tem1 = x | y;
+  int tem2 = ~(x & y);
+  return tem1 & tem2;
+}
+
+int
+fn2 (int x, int y)
+{
+  int tem1 = y | x;
+  int tem2 = ~(x & y);
+  return tem1 & tem2;
+}
+
+int
+fn3 (int x, int y)
+{
+  int tem1 = x | y;
+  int tem2 = ~(y & x);
+  return tem1 & tem2;
+}
+
+int
+fn4 (int x, int y)
+{
+  int tem1 = y | x;
+  int tem2 = ~(y & x);
+  return tem1 & tem2;
+}
+
+int
+fn5 (int x, int y)
+{
+  int tem1 = ~(x & y);
+  int tem2 = x | y;
+  return tem1 & tem2;
+}
+
+int
+fn6 (int x, int y)
+{
+  int tem1 = ~(x & y);
+  int tem2 = y | x;
+  return tem1 & tem2;
+}
+
+int
+fn7 (int x, int y)
+{
+  int tem1 = ~(y & x);
+  int tem2 = x | y;
+  return tem1 & tem2;
+}
+
+int
+fn8 (int x, int y)
+{
+  int tem1 = ~(y & x);
+  int tem2 = y | x;
+  return tem1 & tem2;
+}
+
+/* { dg-final { scan-tree-dump-not " \\| " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not " \\& " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not "~" "cddce1" } } */
 
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+int
+fn1 (int x, int y)
+{
+  int tem1 = x | y;
+  int tem2 = ~x ^ y;
+  return tem1 & tem2;
+}
+
+int
+fn2 (int x, int y)
+{
+  int tem1 = y | x;
+  int tem2 = ~x ^ y;
+  return tem1 & tem2;
+}
+
+int
+fn3 (int x, int y)
+{
+  int tem1 = x | y;
+  int tem2 = y ^ ~x;
+  return tem1 & tem2;
+}
+
+int
+fn4 (int x, int y)
+{
+  int tem1 = y | x;
+  int tem2 = y ^ ~x;
+  return tem1 & tem2;
+}
+
+int
+fn5 (int x, int y)
+{
+  int tem1 = ~x ^ y;
+  int tem2 = x | y;
+  return tem1 & tem2;
+}
+
+int
+fn6 (int x, int y)
+{
+  int tem1 = ~x ^ y;
+  int tem2 = y | x;
+  return tem1 & tem2;
+}
+
+int
+fn7 (int x, int y)
+{
+  int tem1 = y ^ ~x;
+  int tem2 = x | y;
+  return tem1 & tem2;
+}
+
+int
+fn8 (int x, int y)
+{
+  int tem1 = y ^ ~x;
+  int tem2 = y | x;
+  return tem1 & tem2;
+}
+
+/* { dg-final { scan-tree-dump-not " \\| " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not " \\^ " "cddce1" } } */
+/* { dg-final { scan-tree-dump-not "~" "cddce1" } } */