gimple-fold.c (gimple_fold_stmt_to_constant_1): Canonicalize bool compares on RHS.
authorRichard Biener <rguenther@suse.de>
Tue, 4 Aug 2015 13:51:50 +0000 (13:51 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Tue, 4 Aug 2015 13:51:50 +0000 (13:51 +0000)
2015-08-04  Richard Biener  <rguenther@suse.de>

* gimple-fold.c (gimple_fold_stmt_to_constant_1): Canonicalize
bool compares on RHS.
* match.pd: Add X ==/!= !X is false/true pattern.

* gcc.dg/tree-ssa/ssa-ccp-38.c: New testcase.

From-SVN: r226576

gcc/ChangeLog
gcc/gimple-fold.c
gcc/match.pd
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-38.c [new file with mode: 0644]

index cd017f47eb6a4eaa813525d9642b8f9d6d461ca1..3706b6729a9d118cd923ca4c91260bf8e5c22eba 100644 (file)
@@ -1,3 +1,9 @@
+2015-08-04  Richard Biener  <rguenther@suse.de>
+
+       * gimple-fold.c (gimple_fold_stmt_to_constant_1): Canonicalize
+       bool compares on RHS.
+       * match.pd: Add X ==/!= !X is false/true pattern.
+
 2015-08-04  Pawel Kupidura  <pawel.kupidura@arm.com>
 
        * config/aarch64/aarch64.c: Change inner loop statement cost
index 6c53bac027b82fb7752f1d747fab46b45faceab6..1a70d8ff7225cd21038fe2eab05024fa1268a433 100644 (file)
@@ -5012,10 +5012,8 @@ gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree),
               further propagation.  */
            if (subcode == POINTER_PLUS_EXPR)
              {
-               /* Handle binary operators that can appear in GIMPLE form.  */
                tree op0 = (*valueize) (gimple_assign_rhs1 (stmt));
                tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
-
                if (TREE_CODE (op0) == ADDR_EXPR
                    && TREE_CODE (op1) == INTEGER_CST)
                  {
@@ -5027,6 +5025,38 @@ gimple_fold_stmt_to_constant_1 (gimple stmt, tree (*valueize) (tree),
                                      unshare_expr (op0), off));
                  }
              }
+           /* Canonicalize bool != 0 and bool == 0 appearing after
+              valueization.  While gimple_simplify handles this
+              it can get confused by the ~X == 1 -> X == 0 transform
+              which we cant reduce to a SSA name or a constant
+              (and we have no way to tell gimple_simplify to not
+              consider those transforms in the first place).  */
+           else if (subcode == EQ_EXPR
+                    || subcode == NE_EXPR)
+             {
+               tree lhs = gimple_assign_lhs (stmt);
+               tree op0 = gimple_assign_rhs1 (stmt);
+               if (useless_type_conversion_p (TREE_TYPE (lhs),
+                                              TREE_TYPE (op0)))
+                 {
+                   tree op1 = (*valueize) (gimple_assign_rhs2 (stmt));
+                   op0 = (*valueize) (op0);
+                   if (subcode == NE_EXPR)
+                     {
+                       if (integer_zerop (op1))
+                         return op0;
+                       else if (integer_zerop (op0))
+                         return op1;
+                     }
+                   else
+                     {
+                       if (integer_onep (op1))
+                         return op0;
+                       else if (integer_onep (op0))
+                         return op1;
+                     }
+                 }
+             }
            return NULL_TREE;
 
           case GIMPLE_TERNARY_RHS:
index 913a1493b5db33cbaa105a2a8a24620ad772e7d1..3e9100e81f0e1589a2f6cd984b5b2bafaf279d14 100644 (file)
@@ -618,6 +618,11 @@ along with GCC; see the file COPYING3.  If not see
  (simplify
   (op:c truth_valued_p@0 (logical_inverted_value @0))
   { constant_boolean_node (true, type); }))
+/* X ==/!= !X is false/true.  */
+(for op (eq ne)
+ (simplify
+  (op:c truth_valued_p@0 (logical_inverted_value @0))
+  { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
 
 /* If arg1 and arg2 are booleans (or any single bit type)
    then try to simplify:
index 69b511a849f0e64a848def3cfbc301e029c8f037..dc1262fdfbef75be94f192f8151d66b98e407f9b 100644 (file)
@@ -1,3 +1,7 @@
+2015-08-04  Richard Biener  <rguenther@suse.de>
+
+       * gcc.dg/tree-ssa/ssa-ccp-38.c: New testcase.
+
 2015-08-04  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR target/67110
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-38.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-38.c
new file mode 100644 (file)
index 0000000..e5fb813
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-ccp1" } */
+
+int foo (_Bool x)
+{
+  _Bool t = 1;
+  _Bool xx = !x;
+  _Bool y = xx == t;
+  _Bool z = y == x;
+  return z ? 1 : 0;
+}
+
+/* { dg-final { scan-tree-dump "return 0;" "ccp1" } } */