This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PR25530] Convert (unsigned t / 2) * 2 into (unsigned t & ~1)
- From: Richard Biener <richard dot guenther at gmail dot com>
- To: "Hurugalawadi, Naveen" <Naveen dot Hurugalawadi at caviumnetworks dot com>
- Cc: "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 7 Jul 2015 11:08:24 +0200
- Subject: Re: [PR25530] Convert (unsigned t / 2) * 2 into (unsigned t & ~1)
- Authentication-results: sourceware.org; auth=none
- References: <SN2PR0701MB1024719A5C46B8E7276E718C8E920 at SN2PR0701MB1024 dot namprd07 dot prod dot outlook dot com>
On Tue, Jul 7, 2015 at 6:52 AM, Hurugalawadi, Naveen
<Naveen.Hurugalawadi@caviumnetworks.com> wrote:
> Hi,
>
> Please find attached the patch PR25530.patch that converts the pattern:-
> (unsigned / 2) * 2 is into (unsigned & ~1).
>
> Please review and let me know if its okay.
For EXACT_DIV fold-const.c has
/* ((T) (X /[ex] C)) * C cancels out if the conversion is
sign-changing only. */
if (TREE_CODE (arg1) == INTEGER_CST
&& TREE_CODE (arg0) == EXACT_DIV_EXPR
&& operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
we know the remainder is zero for EXACT_DIV. It also gives hints that
a sign-changing conversion is ok.
+/* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
+/* PR25530. */
+(for div (trunc_div ceil_div floor_div round_div exact_div)
+ (simplify
+ (mult (div @0 INTEGER_CST@1) INTEGER_CST@1)
+ (with { tree n2 = build_int_cst (TREE_TYPE (@0),
+ wi::exact_log2 (@1)); }
+ (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (bit_and @0 (lshift (rshift { build_minus_one_cst (TREE_TYPE (@0)); }
+ { n2; }) { n2; }))))))
you should move the (with inside the (if to save work if the type is not
unsigned. Also you are using wi::exact_log2 without checking whether
@1 was a power of two (I think exact_log2 returns -1 in this case).
Then expressing ~1 with the result expression is really excessive - you
should simply build this with @1 - 1 if @1 is a power of two.
So please handle exact_div differently, like fold-const.c does.
Also I am not sure ceil_div and floor_div can be handled this way.
(5 /[ceil] 2) * 2 == 6 but you compute it as 4. So I am only convinced
trunc_div works this way.
Thanks,
Richard.
> Regression tested on AARH64 and x86_64.
>
> Thanks,
> Naveen
>
> gcc/testsuite/ChangeLog:
>
> 2015-07-07 Naveen H.S <Naveen.Hurugalawadi@caviumnetworks.com>
>
> PR middle-end/25530
> * gcc.dg/pr25530.c: New test.
>
> gcc/ChangeLog:
>
> 2015-07-07 Naveen H.S <Naveen.Hurugalawadi@caviumnetworks.com>
>
> PR middle-end/25530
> * match.pd (mult (div @0 INTEGER_CST@1) INTEGER_CST@1) :
> New simplifier.