diff options
author | Roger Sayle <roger@eyesopen.com> | 2002-07-01 20:59:00 +0000 |
---|---|---|
committer | Roger Sayle <sayle@gcc.gnu.org> | 2002-07-01 20:59:00 +0000 |
commit | 6bfa5aac87329dfa40b16181408237e9d0df764e (patch) | |
tree | ede6758e3c42f784cd014a563a92268147d976cf /gcc/fold-const.c | |
parent | 9a5c1b9db14b269edfea010febc1ae3066190900 (diff) | |
download | gcc-6bfa5aac87329dfa40b16181408237e9d0df764e.tar.gz |
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
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r-- | gcc/fold-const.c | 27 |
1 files changed, 27 insertions, 0 deletions
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: |