[llvm] [InstCombine] Fold select chain to frozen bool reduction (PR #217482)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 00:05:06 PDT 2026


================
@@ -4582,6 +4582,65 @@ static bool isSelectZeroSignInsignificant(SelectInst &SI) {
   return true;
 }
 
+static Instruction *foldSelectExtractEl(SelectInst &SI,
+                                        InstCombiner::BuilderTy &Builder) {
+  //       select (extractelement (icmp Pred X, Y), C1)
+  //              (extractelement (icmp Pred X, Y) C2), 0
+  //              ->
+  //              icmp eq (bitcast (freeze (icmp Pred X, Y)) to iN), C3
+  Value *X;
+  Value *Y;
+  CmpPredicate Pred1, Pred2;
+  const APInt *C1, *C2;
+  if (match(&SI,
+            m_Select(m_ExtractElt(m_ICmp(Pred1, m_Value(X), m_Value(Y)),
+                                  m_APInt(C1)),
+                     m_ExtractElt(m_ICmp(Pred2, m_Deferred(X), m_Deferred(Y)),
+                                  m_APInt(C2)),
+                     m_Zero()))
+
+  ) {
+    // Make sure we're operating on the same comparison
+    auto MatchedPred = CmpPredicate::getMatching(Pred1, Pred2);
+    if (!MatchedPred)
+      return nullptr;
+
+    // Extract values and check if we're operating on the same predicate
+    auto *Ext0 = cast<ExtractElementInst>(SI.getCondition());
+    auto *Ext1 = cast<ExtractElementInst>(SI.getTrueValue());
+    Value *Cmp = Ext0->getVectorOperand();
+    if (Ext1->getVectorOperand() != Cmp)
+      return nullptr;
+
+    auto *VecTy = dyn_cast_or_null<FixedVectorType>(Cmp->getType());
+    if (!VecTy)
+      return nullptr;
+    unsigned NumElts = VecTy->getNumElements();
+    if (NumElts > IntegerType::MAX_INT_BITS)
----------------
RKSimon wrote:

necessary?

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


More information about the llvm-commits mailing list