[llvm] [InstCombine] Canonicalize Bit Testing by Shifting to Sign Bit (PR #101822)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 10 07:57:32 PDT 2024


================
@@ -2304,19 +2304,33 @@ Instruction *InstCombinerImpl::foldICmpShlConstant(ICmpInst &Cmp,
     if (C.isZero() || (Pred == ICmpInst::ICMP_SGT ? C.isAllOnes() : C.isOne()))
       return new ICmpInst(Pred, Shl->getOperand(0), Cmp.getOperand(1));
 
+  unsigned TypeBits = C.getBitWidth();
+  Value *X = Shl->getOperand(0);
+  Type *ShType = Shl->getType();
+
+  // (icmp slt (shl X, (sub bw-1, Y)), 0)  --> (icmp ne (and X, (shl 1, Y)), 0)
+  // (icmp sgt (shl X, (sub bw-1, Y)), -1) --> (icmp eq (and X, (shl 1, Y)), 0)
+  Value *Y;
+  if (Shl->hasOneUse() &&
+      (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGT) &&
+      (Pred == ICmpInst::ICMP_SLT ? C.isZero() : C.isAllOnes()) &&
----------------
goldsteinn wrote:

nit: Slightly simpler `(Pred == ICmpInst::ICMP_SLT && C.isZero()) || (Pred == ICmpInst::ICMP_SGT && C.isAllOnes())


https://github.com/llvm/llvm-project/pull/101822


More information about the llvm-commits mailing list