This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch] More TRUNC_MOD_EXPR folding
- From: ja2morri at csclub dot uwaterloo dot ca (James A. Morrison)
- To: gcc-patches at gcc dot gnu dot org
- Date: 17 Jun 2005 17:07:56 -0400
- Subject: [patch] More TRUNC_MOD_EXPR folding
Hi,
This patch allows signed typed TRUNC_MOD_EXPRs to be folded to AND if the
value is known to be positive. E.g. abs(x) % 8 -> abs(x) & 7;
Bootstrapped and regtested on ia64-linux with no new regressions, ok for
mainline?
--
Thanks,
Jim
http://www.csclub.uwaterloo.ca/~ja2morri/
http://phython.blogspot.com
http://open.nit.ca/wiki/?page=jim
2005-06-14 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.
Index: fold-const.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.596
diff -u -p -r1.596 fold-const.c
--- fold-const.c 16 Jun 2005 18:09:31 -0000 1.596
+++ fold-const.c 16 Jun 2005 23:25:19 -0000
@@ -8337,11 +8360,11 @@ fold_binary (enum tree_code code, tree t
&& 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;
/* { 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" } } */