OSDN Git Service

2005-06-18 James A. Morrison <phython@gcc.gnu.org>
authorphython <phython@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 18 Jun 2005 19:57:12 +0000 (19:57 +0000)
committerphython <phython@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 18 Jun 2005 19:57:12 +0000 (19:57 +0000)
        * fold_const (fold_binary): Fold X % (2**N) to X & (2**N - 1) for
        nonnegative values of X.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@101163 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/fold-mod-1.c [new file with mode: 0644]

index 2fefbb6..52c3a96 100644 (file)
@@ -1,3 +1,8 @@
+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
index 8433d1d..335e556 100644 (file)
@@ -8337,11 +8337,11 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
          && 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;
index 2b7719c..31c4c10 100644 (file)
@@ -1,3 +1,7 @@
+2005-06-18  James A Morrison  <phython@gcc.gnu.org>
+
+       * gcc.dg/fold-mod-1.c: New test.
+
 2005-06-18  Steven G. Kargl  <kargls@comcast.net>
 
        PR fortran/19926
diff --git a/gcc/testsuite/gcc.dg/fold-mod-1.c b/gcc/testsuite/gcc.dg/fold-mod-1.c
new file mode 100644 (file)
index 0000000..1b312ea
--- /dev/null
@@ -0,0 +1,25 @@
+/* { 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" } } */