+2005-06-18 James A. Morrison <phython@gcc.gnu.org>
+
+ * fold_const (fold_binary): Fold X % (2**N) to X & (2**N - 1) for
+ nonnegative values of X.
+
2005-06-18 Uros Bizjak <uros@kss-loka.si>
* doc/md.texi (Standard Names): Change insn pattern name
&& TREE_INT_CST_HIGH (arg1) == -1)
return omit_one_operand (type, integer_zero_node, arg0);
- /* Optimize unsigned TRUNC_MOD_EXPR by a power of two into a
- BIT_AND_EXPR, i.e. "X % C" into "X & C2". */
- if (code == TRUNC_MOD_EXPR
- && TYPE_UNSIGNED (type)
- && integer_pow2p (arg1))
+ /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
+ i.e. "X % C" into "X & C2", if X and C are positive. */
+ if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
+ && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (arg0))
+ && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) >= 0)
{
unsigned HOST_WIDE_INT high, low;
tree mask;
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-gimple" } */
+
+#define ABS(x) (x > 0 ? x : -x)
+
+unsigned int f (unsigned int a) {
+ /* (unsigned)-8 is not a power of 2. */
+ return a % -8;
+}
+
+int g (int b) {
+ return ABS (b) % -8;
+}
+
+int h (int c) {
+ return ABS (c) % 8;
+}
+
+unsigned int k (unsigned int d) {
+ return d % 8;
+}
+
+/* { dg-final { scan-tree-dump "a % 4294967288" "gimple" } } */
+/* { dg-final { scan-tree-dump-times " & 7" 3 "gimple" } } */
+/* { dg-final { cleanup-tree-dump "gimple" } } */