summaryrefslogtreecommitdiff
path: root/gcc/fold-const.c
diff options
context:
space:
mode:
authorRichard Guenther <rguenther@suse.de>2012-05-08 12:50:18 +0000
committerRichard Biener <rguenth@gcc.gnu.org>2012-05-08 12:50:18 +0000
commitad9fc55abd20a1501ab649693818a93b1538f375 (patch)
tree1fdd185372150a013ed29ccd9e618fe1a9495380 /gcc/fold-const.c
parent04d170d214ae0a801ccfc4f66bf11531d19e75a8 (diff)
downloadgcc-ad9fc55abd20a1501ab649693818a93b1538f375.tar.gz
fold-const.c (fold_binary_loc): Fold (X * CST1) & CST2 to zero or to (X * CST1) & CST2' when...
2012-05-08 Richard Guenther <rguenther@suse.de> * fold-const.c (fold_binary_loc): Fold (X * CST1) & CST2 to zero or to (X * CST1) & CST2' when CST1 has trailing zeros. * gcc.dg/fold-bitand-4.c: New testcase. From-SVN: r187280
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r--gcc/fold-const.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index f8b31b7d113..5bb75d490d9 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -11449,6 +11449,30 @@ fold_binary_loc (location_t loc,
return fold_convert_loc (loc, type, arg0);
}
+ /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
+ bits from CST2. */
+ if (TREE_CODE (arg1) == INTEGER_CST
+ && TREE_CODE (arg0) == MULT_EXPR
+ && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
+ {
+ int arg1tz
+ = double_int_ctz (tree_to_double_int (TREE_OPERAND (arg0, 1)));
+ if (arg1tz > 0)
+ {
+ double_int arg1mask, masked;
+ arg1mask = double_int_not (double_int_mask (arg1tz));
+ arg1mask = double_int_ext (arg1mask, TYPE_PRECISION (type),
+ TYPE_UNSIGNED (type));
+ masked = double_int_and (arg1mask, tree_to_double_int (arg1));
+ if (double_int_zero_p (masked))
+ return omit_two_operands_loc (loc, type, build_zero_cst (type),
+ arg0, arg1);
+ else if (!double_int_equal_p (masked, tree_to_double_int (arg1)))
+ return fold_build2_loc (loc, code, type, op0,
+ double_int_to_tree (type, masked));
+ }
+ }
+
/* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
((A & N) + B) & M -> (A + B) & M
Similarly if (N & M) == 0,