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

via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 26 11:44:29 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());
+    }
----------------
goldsteinn wrote:

What is the motivation for `LookThruTrunc`? Doesn't `(trunc (trunc X))` always get folded to `(trunc X)` in InstCombine?

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


More information about the llvm-commits mailing list