This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Division Optimization in match and simplify
- From: Marc Glisse <marc dot glisse at inria dot fr>
- To: "Hurugalawadi, Naveen" <Naveen dot Hurugalawadi at caviumnetworks dot com>
- Cc: Richard Biener <richard dot guenther at gmail dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 5 Nov 2015 07:20:26 +0100 (CET)
- Subject: Re: Division Optimization in match and simplify
- Authentication-results: sourceware.org; auth=none
- References: <BLUPR0701MB10112FDAF6046909BE252FFB8E2A0 at BLUPR0701MB1011 dot namprd07 dot prod dot outlook dot com> <alpine dot DEB dot 2 dot 20 dot 1511040920400 dot 2317 at laptop-mg dot saclay dot inria dot fr> <BLUPR0701MB10110152D9B362818EB337668E2A0 at BLUPR0701MB1011 dot namprd07 dot prod dot outlook dot com> <alpine dot DEB dot 2 dot 20 dot 1511041155570 dot 2317 at laptop-mg dot saclay dot inria dot fr>,<CAFiYyc172oRzH6H8dNP0E5mbEcN5GVGF1ujoWtUvDUV0TBQkcw at mail dot gmail dot com> <SN2PR0701MB102426084E1A305896CB1D6A8E290 at SN2PR0701MB1024 dot namprd07 dot prod dot outlook dot com>
- Reply-to: gcc-patches at gcc dot gnu dot org
+/* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
+(for div (exact_div trunc_div)
Actually, it probably works for all integer divisions (floor_div, etc)
since it is exact and thus does not depend on the rounding.
(sorry for giving the comments small piece by small piece, I write them as
I think of them...)
+ (simplify
+ (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
+ (if (integer_pow2p (@2)
+ && tree_int_cst_sign_bit (@2) == 0
I think the previous test tree_int_cst_sgn > 0 was better: for unsigned,
it is ok if that bit is set, it is only for signed that we want to avoid
-1/-1 which gives 1 (while a shift by 31 would give -1, except that
wi::exact_log2 might return -1 so we would end up with a negative shift).
(actually, we could generate (unsigned)x>>31 in that case, but let's
forget it for now)
+ && wi::add (@2, @1) == 0
+ && tree_nop_conversion_p (type, TREE_TYPE (@0)))
+ (rshift (convert @0) { build_int_cst (integer_type_node, wi::exact_log2 (@2)); }))))
(does this fit in 80 cols?)
Note that if we port the following to match.pd from wherever it currently
is, the 'convert?' becomes much less useful on this transformation
(simplify
(convert (bit_and:s @0 INTEGER_CST@1))
(if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
(bit_and (convert @0) (convert @1))))
--
Marc Glisse