[llvm] [InstCombine] Fold ZExt(i1) Pred lshr(A, BW - 1) => i1 Pred A s< 0 (PR #68244)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 13 01:07:20 PDT 2023


================
@@ -7186,6 +7186,23 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
       if (Instruction *R = processUMulZExtIdiom(I, Op1, Op0, *this))
         return R;
     }
+
+    // Signbit test folds
+    // Fold (X u>> BitWidth - 1 Pred Zext(i1))  -->  X s< 0 Pred i1
+    Value *X, *Y;
+    if ((I.isUnsigned() || I.isEquality()) &&
+        (Op0->hasOneUse() || Op1->hasOneUse()) &&
+        match(Op1, m_ZExt(m_Value(Y))) &&
+        (match(Op0, m_LShr(m_Value(X),
+                           m_SpecificIntAllowUndef(
+                               Op0->getType()->getScalarSizeInBits() - 1))) &&
+         Y->getType()->getScalarSizeInBits() == 1)) {
+
+      Value *SLTZero =
+          Builder.CreateICmpSLT(X, Constant::getNullValue(X->getType()));
+      Value *Cmp = Builder.CreateICmp(Pred, SLTZero, Y, I.getName());
+      return replaceInstUsesWith(I, Cmp);
+    }
----------------
XChy wrote:

That's correct. [Proof](https://alive2.llvm.org/ce/z/_VBqK7). And this transform just handles special cases which foldXorICmp also handles.

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


More information about the llvm-commits mailing list