[llvm] [VectorCombine] Fix crash when folding select of bitcast (PR #177183)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 25 05:00:43 PST 2026


================
@@ -1651,7 +1651,38 @@ bool VectorCombine::foldSelectsFromBitcast(Instruction &I) {
     }
 
     // Create the vector select and bitcast once for this condition.
-    Builder.SetInsertPoint(BC->getNextNode());
+    // Insert in a block that dominates all selects, and where Cond and SrcVec
+    // are defined.
+    BasicBlock *InsertBB = Selects.front()->getParent();
+    for (SelectInst *Sel : drop_begin(Selects))
+      InsertBB = DT.findNearestCommonDominator(InsertBB, Sel->getParent());
+    if (!InsertBB)
+      continue;
+
+    BasicBlock::iterator InsertPt = InsertBB->getFirstInsertionPt();
+    if (InsertPt == InsertBB->end())
+      continue;
+
+    auto ProcessDef = [&](Value *V) -> bool {
+      auto *DefI = dyn_cast<Instruction>(V);
+      if (!DefI || DefI->getParent() != InsertBB)
+        return true;
+
+      auto AfterDefOpt = DefI->getInsertionPointAfterDef();
+      if (!AfterDefOpt)
+        return false;
+
+      BasicBlock::iterator AfterDefIt = *AfterDefOpt;
+      if (InsertPt->comesBefore(&*AfterDefIt))
----------------
arsenm wrote:

Replace with a function argument 

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


More information about the llvm-commits mailing list