[PATCH] Teach Instcombine to use the nsw attribute for signed comparisons to 0 of (shl %v, Cst) or (mul %v, Cst)

Nick Lewycky nicholas at mxc.ca
Sun Mar 17 23:07:17 PDT 2013


Arnaud de Grandmaison wrote:
> The attached patch teaches InstCombine/visitICmpInstWithInstAndIntCst to
> optimize 'icmp pred (mul %v, Cst), 0' when the mul has the nsw attribute set.
>   The comparison can the be performed on the mul RHS.
>
> The patch also handles the case of the mul in strength reduced form : shl

There's two optimizations here: remove operations that don't change sign 
test and only feed into a sign test, and remove operations that don't 
change "zeroness" and only feed into an is-zero test.

We already have the latter, so please extend it. You can find it 
starting at InstCombineCompares.cpp:1419, and it supports all sorts of 
great cases like "popcount(A) == 0  ->  A == 0" (line 1555).

Then append the sign testing bits to that function 
(visitICmpInstWithInstAndIntCst). Your "isCompareToZero" becomes 
"isSignTest".

The tests only cover multiply, but the code checks for 'nsw' on any 
BinaryOperator. Are we already doing this correctly for 'and' and 'sub', 
and if so, through what code path (and why not extend that)?

What if the instruction is an 'or' whose RHS includes the sign bit? Or 
an 'xor' that doesn't have the sign bit in the RHS? Were we already 
getting these? Through SimplifyDemandedBits?

If not, please extend the tests. If so, you may want to remove any 
optimizations that are now redundant?

Nick



More information about the llvm-commits mailing list