aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog7
-rw-r--r--gcc/fold-const.c27
2 files changed, 34 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 42f58cc..013ac5c 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -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.
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 72266c5..3dafe0a 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -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: