This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] PR/34522, fold (T)(a * b) for operands bigger than T
- From: Paolo Bonzini <bonzini at gnu dot org>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Cc: Richard Guenther <rguenther at suse dot de>
- Date: Wed, 12 Mar 2008 15:11:32 +0100
- Subject: [PATCH] PR/34522, fold (T)(a * b) for operands bigger than T
This is done in fold-const.c, even though it probably could be done in
expand too. Some extra care is needed to avoid introducing overflows as
in PR6677.
Bootstrapped/regtested i686-pc-linux-gnu, ok for mainline?
Paolo
/* { dg-options "-O2" } */
/* { dg-do compile } */
/* { dg-require-effective-target ilp32 } */
int test(long long a, long long b)
{
return a * b;
}
/* Check that we did not spill anything. This is all that is needed
to qualify the generated code as "decent"... */
/* { dg-final { scan-assembler-not "%e[sd]i" } } */
2008-03-12 Paolo Bonzini <bonzini@gnu.org>
* fold-const.c (fold_unary) <case NOP_EXPR>: Distribute
narrowing of the result of a multiplication to the operands.
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c (revision 133114)
+++ gcc/fold-const.c (working copy)
@@ -7926,6 +7926,26 @@ fold_unary (enum tree_code code, tree ty
return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
}
+ /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
+ type of X and Y (integer types only). */
+ if (INTEGRAL_TYPE_P (type)
+ && TREE_CODE (op0) == MULT_EXPR
+ && INTEGRAL_TYPE_P (TREE_TYPE (op0))
+ && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
+ {
+ /* Be careful not to introduce new overflows. */
+ tree mult_type;
+ if (TYPE_OVERFLOW_WRAPS (type))
+ mult_type = type;
+ else
+ mult_type = unsigned_type_for (type);
+
+ tem = fold_build2 (MULT_EXPR, mult_type,
+ fold_convert (mult_type, TREE_OPERAND (op0, 0)),
+ fold_convert (mult_type, TREE_OPERAND (op0, 1)));
+ return fold_convert (type, tem);
+ }
+
tem = fold_convert_const (code, type, op0);
return tem ? tem : NULL_TREE;