[PATCH] D28406: [InstCombine] icmp sgt (shl nsw X, C1), C0 --> icmp sgt X, C0 >> C1

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 19 06:57:54 PST 2017

spatel added inline comments.

Comment at: lib/Transforms/InstCombine/InstCombineCompares.cpp:1915
-  // If this is a signed comparison to 0 and the shift is sign preserving,
-  // use the shift LHS operand instead; isSignTest may change 'Pred', so only
-  // do that if we're sure to not continue on in this function.
-  if (Shl->hasNoSignedWrap() && isSignTest(Pred, *C))
-    return new ICmpInst(Pred, X, Constant::getNullValue(ShType));
+  // A 'shl nsw' is just shifting out sign bits, so adjust the compare constant
+  // and eliminate the shift.
bryant wrote:
> bryant wrote:
> > `shl nsw` is also shifting out zeroes, no? It's `ashr` that shifts in sign bits.
> What I mean is (also for the ugt summary): It's because **shl shifts in zeroes** that warrants this transform. In other words, in the un-transformed icmp, we're comparing against known zero bits introduced by the shl.
It's the nuw or nsw property of the shift that allows these transforms. Ie, any shift-left *shifts in* zeroes to the low bits. The fact that we've guaranteed *shifting out* sign-bits or zeroes from the high bits is what allows us to safely lshr/ashr the constant.

I'll try to make that clearer in the comment.


More information about the llvm-commits mailing list