]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR rtl-optimization/4046 (redundant conditional branch)
authorRoger Sayle <roger@eyesopen.com>
Mon, 1 Jul 2002 20:59:00 +0000 (20:59 +0000)
committerRoger Sayle <sayle@gcc.gnu.org>
Mon, 1 Jul 2002 20:59:00 +0000 (20:59 +0000)
PR opt/4046
* fold-const.c (fold) [COND_EXPR]: Simplify A ? 0 : 1 to !A,
A ? B : 0 to A && B and A ? B : 1 into !A || B if both A and
B are truth values.

From-SVN: r55153

gcc/ChangeLog
gcc/fold-const.c

index 42f58cc14fb8726c2a08d163bc2945bdd82293ad..013ac5c422ce44d1581b70310b7e02ae7af3b32d 100644 (file)
@@ -1,3 +1,10 @@
+2002-07-01  Roger Sayle  <roger@eyesopen.com>
+
+       PR opt/4046
+       * fold-const.c (fold) [COND_EXPR]: Simplify A ? 0 : 1 to !A,
+       A ? B : 0 to A && B and A ? B : 1 into !A || B if both A and
+       B are truth values.
+
 2002-07-01  Nathanael Nerode  <neroden@gcc.gnu.org>
 
        * config/mmix/t-mmix: Eliminate last reference to LIBGCC1_TEST.
index 72266c5ed773e69405909840740cca7271f7078e..3dafe0afb31115f095a91e97096d33585630f4ec 100644 (file)
@@ -7036,6 +7036,14 @@ fold (expr)
          && type == TREE_TYPE (arg0))
        return pedantic_non_lvalue (arg0);
 
+      /* Convert A ? 0 : 1 to !A.  This prefers the use of NOT_EXPR
+        over COND_EXPR in cases such as floating point comparisons.  */
+      if (integer_zerop (TREE_OPERAND (t, 1))
+         && integer_onep (TREE_OPERAND (t, 2))
+         && truth_value_p (TREE_CODE (arg0)))
+       return pedantic_non_lvalue (convert (type,
+                                            invert_truthvalue (arg0)));
+
       /* Look for expressions of the form A & 2 ? 2 : 0.  The result of this
         operation is simply A & 2.  */
 
@@ -7048,6 +7056,25 @@ fold (expr)
                              arg1, 1))
        return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
 
+      /* Convert A ? B : 0 into A && B if A and B are truth values.  */
+      if (integer_zerop (TREE_OPERAND (t, 2))
+         && truth_value_p (TREE_CODE (arg0))
+         && truth_value_p (TREE_CODE (arg1)))
+       return pedantic_non_lvalue (fold (build (TRUTH_ANDIF_EXPR, type,
+                                                arg0, arg1)));
+
+      /* Convert A ? B : 1 into !A || B if A and B are truth values.  */
+      if (integer_onep (TREE_OPERAND (t, 2))
+         && truth_value_p (TREE_CODE (arg0))
+         && truth_value_p (TREE_CODE (arg1)))
+       {
+         /* Only perform transformation if ARG0 is easily inverted.  */
+         tem = invert_truthvalue (arg0);
+         if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
+           return pedantic_non_lvalue (fold (build (TRUTH_ORIF_EXPR, type,
+                                                    tem, arg1)));
+       }
+
       return t;
 
     case COMPOUND_EXPR: