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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 22 01:01:10 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:

This feels out of place for the pass. It's unusual to need to do this much insertion point management for an instruction-at-a-time transformation, and this is pretty brute force. Usually there's an implied insert point from the combine that you know must dominate all the uses, and you can just use that. 


Alternatively there's a missing parent block check somewhere? If I take your multi-block testcase and replace the degenerate bitcast of constant, I don't see the error. I suspect you're seeing an artifact of mishandling the constant?

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


More information about the llvm-commits mailing list