[llvm] [InstSimplify] Handle trunc to i1 in Select with bit test folds. (PR #122944)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 18 10:46:08 PST 2025


================
@@ -176,6 +177,24 @@ llvm::decomposeBitTest(Value *Cond, bool LookThruTrunc, bool AllowNonZeroC) {
                                 ICmp->getPredicate(), LookThruTrunc,
                                 AllowNonZeroC);
   }
+  Value *X;
+  if (Cond->getType()->isIntOrIntVectorTy(1) &&
+      (match(Cond, m_Trunc(m_Value(X))) ||
+       match(Cond, m_Not(m_Trunc(m_Value(X)))))) {
+    DecomposedBitTest Result;
+    Result.X = X;
+    unsigned BitWidth = X->getType()->getScalarSizeInBits();
+    Result.Mask = APInt(BitWidth, 1);
+    Result.C = APInt::getZero(BitWidth);
+    Result.Pred = isa<TruncInst>(Cond) ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
+
+    if (LookThruTrunc && match(Result.X, m_Trunc(m_Value(X)))) {
+      Result.X = X;
+      Result.Mask = Result.Mask.zext(X->getType()->getScalarSizeInBits());
+      Result.C = Result.C.zext(X->getType()->getScalarSizeInBits());
+    }
----------------
andjo403 wrote:

good catch test added.

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


More information about the llvm-commits mailing list