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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 13 00:58:04 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);
+    }
----------------
nikic wrote:

This code was previously located next to https://github.com/llvm/llvm-project/blob/24950fd2f4138caed78fd5fb28decd00474cb6cb/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp#L5385, which seems like a special case of this transform. Would it be possible to extend this one to handle the `SExt + AShr` variant as well, and then drop the other code completely? Or does what we generate here not fold down to the xor pattern that one produces?

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


More information about the llvm-commits mailing list