aboutsummaryrefslogtreecommitdiff
path: root/gcc/fold-const.c
diff options
context:
space:
mode:
authorRoger Sayle <roger@eyesopen.com>2006-12-31 16:17:49 +0000
committerRoger Sayle <sayle@gcc.gnu.org>2006-12-31 16:17:49 +0000
commit870aa1ebe0d3e05912f763ece1ed4491282d34e2 (patch)
treeb1cf895f653525f285670f935e58e7beff2a7eef /gcc/fold-const.c
parent7c2da05e604c27549afadf4df26c679aead5f414 (diff)
downloadgcc-870aa1ebe0d3e05912f763ece1ed4491282d34e2.zip
gcc-870aa1ebe0d3e05912f763ece1ed4491282d34e2.tar.gz
gcc-870aa1ebe0d3e05912f763ece1ed4491282d34e2.tar.bz2
re PR middle-end/30322 (((-i-1) + i) +1) is turned into ~i + (i+1) and never into 0 on the tree level)
PR middle-end/30322 * fold-const.c (fold_binary): Fold X + ~X and ~X + X into -1 when we don't care about trapping overflow. Only convert -A - 1 into ~A when we don't care about trapping overflow. * gcc.dg/fold-plusnot-1.c: New test case. From-SVN: r120300
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r--gcc/fold-const.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index dbdc9c2..da9579e 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -8662,6 +8662,26 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
if (integer_zerop (arg1))
return non_lvalue (fold_convert (type, arg0));
+ /* ~X + X is -1. */
+ if (TREE_CODE (arg0) == BIT_NOT_EXPR
+ && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
+ && !TYPE_TRAP_SIGNED (type))
+ {
+ t1 = build_int_cst (type, -1);
+ t1 = force_fit_type (t1, 0, false, false);
+ return omit_one_operand (type, t1, arg1);
+ }
+
+ /* X + ~X is -1. */
+ if (TREE_CODE (arg1) == BIT_NOT_EXPR
+ && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
+ && !TYPE_TRAP_SIGNED (type))
+ {
+ t1 = build_int_cst (type, -1);
+ t1 = force_fit_type (t1, 0, false, false);
+ return omit_one_operand (type, t1, arg0);
+ }
+
/* If we are adding two BIT_AND_EXPR's, both of which are and'ing
with a constant, and the two constants have no bits in common,
we should treat this as a BIT_IOR_EXPR since this may produce more
@@ -9002,7 +9022,8 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
/* Convert -A - 1 to ~A. */
if (INTEGRAL_TYPE_P (type)
&& TREE_CODE (arg0) == NEGATE_EXPR
- && integer_onep (arg1))
+ && integer_onep (arg1)
+ && !TYPE_TRAP_SIGNED (type))
return fold_build1 (BIT_NOT_EXPR, type,
fold_convert (type, TREE_OPERAND (arg0, 0)));