From 6bfa5aac87329dfa40b16181408237e9d0df764e Mon Sep 17 00:00:00 2001 From: Roger Sayle Date: Mon, 1 Jul 2002 20:59:00 +0000 Subject: [PATCH] re PR rtl-optimization/4046 (redundant conditional branch) 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 | 7 +++++++ gcc/fold-const.c | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 42f58cc14fb..013ac5c422c 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2002-07-01 Roger Sayle + + 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 * config/mmix/t-mmix: Eliminate last reference to LIBGCC1_TEST. diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 72266c5ed77..3dafe0afb31 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: