[llvm] [VectorCombine] Avoid crash when the next node is deleted. (PR #155115)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 25 07:50:25 PDT 2025


================
@@ -4254,13 +4260,22 @@ bool VectorCombine::run() {
     if (!DT.isReachableFromEntry(&BB))
       continue;
     // Use early increment range so that we can erase instructions in loop.
-    for (Instruction &I : make_early_inc_range(BB)) {
-      if (I.isDebugOrPseudoInst())
-        continue;
-      MadeChange |= FoldInst(I);
+    // make_early_inc_range is not applicable here, as the next iterator may
+    // be invalidated by RecursivelyDeleteTriviallyDeadInstructions.
+    // We manually maintain the next instruction and update it when it is about
+    // to be deleted.
+    Instruction *I = &BB.front();
+    while (I) {
+      NextInst = I->getNextNode();
+      if (!I->isDebugOrPseudoInst()) {
+        MadeChange |= FoldInst(*I);
+      }
----------------
nikic wrote:

Omit braces

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


More information about the llvm-commits mailing list