Index: llvm/lib/Transforms/Utils/Local.cpp =================================================================== --- llvm/lib/Transforms/Utils/Local.cpp +++ llvm/lib/Transforms/Utils/Local.cpp @@ -2228,7 +2228,8 @@ SmallSetVector DeadBlockSet; for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ++I) { auto *BB = &*I; - if (Reachable.count(BB)) + //skip reachable basic blocks + if (Reachable.find(BB)!=Reachable.end()) continue; DeadBlockSet.insert(BB); } @@ -2250,12 +2251,8 @@ LVI->eraseBlock(BB); BB->dropAllReferences(); } - for (Function::iterator I = ++F.begin(); I != F.end();) { - auto *BB = &*I; - if (Reachable.count(BB)) { - ++I; - continue; - } + + for (auto *BB : DeadBlockSet) { if (DTU) { // Remove the terminator of BB to clear the successor list of BB. if (BB->getTerminator()) @@ -2263,9 +2260,8 @@ new UnreachableInst(BB->getContext(), BB); assert(succ_empty(BB) && "The successor list of BB isn't empty before " "applying corresponding DTU updates."); - ++I; } else { - I = F.getBasicBlockList().erase(I); + BB->eraseFromParent(); } }