aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/BasicBlock.cpp
diff options
context:
space:
mode:
authorMax Kazantsev <mkazantsev@azul.com>2022-10-27 17:54:15 +0700
committerMax Kazantsev <mkazantsev@azul.com>2022-10-27 17:54:15 +0700
commitec2e5b8c92e6fc98fdb33ff1bd4fba5f92dc7194 (patch)
tree59bc01d0a16e19e9f335ba91a1ed016b6f6ae1f2 /llvm/lib/IR/BasicBlock.cpp
parent7154f89c1ab9dcf63a0cc47c413b600a793ffa7a (diff)
downloadllvm-ec2e5b8c92e6fc98fdb33ff1bd4fba5f92dc7194.zip
llvm-ec2e5b8c92e6fc98fdb33ff1bd4fba5f92dc7194.tar.gz
llvm-ec2e5b8c92e6fc98fdb33ff1bd4fba5f92dc7194.tar.bz2
Fix iterator corruption in splitBasicBlockBefore
We should not delete block predecessors (via replacing successors of terminators) while iterating them, otherwise we may skip some of them. Instead, save predecessors to a separate vector and iterate over it.
Diffstat (limited to 'llvm/lib/IR/BasicBlock.cpp')
-rw-r--r--llvm/lib/IR/BasicBlock.cpp6
1 files changed, 5 insertions, 1 deletions
diff --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp
index 687865d..20101a9 100644
--- a/llvm/lib/IR/BasicBlock.cpp
+++ b/llvm/lib/IR/BasicBlock.cpp
@@ -452,7 +452,11 @@ BasicBlock *BasicBlock::splitBasicBlockBefore(iterator I, const Twine &BBName) {
// If there were PHI nodes in 'this' block, the PHI nodes are updated
// to reflect that the incoming branches will be from the New block and not
// from predecessors of the 'this' block.
- for (BasicBlock *Pred : predecessors(this)) {
+ // Save predecessors to separate vector before modifying them.
+ SmallVector<BasicBlock *, 4> Predecessors;
+ for (BasicBlock *Pred : predecessors(this))
+ Predecessors.push_back(Pred);
+ for (BasicBlock *Pred : Predecessors) {
Instruction *TI = Pred->getTerminator();
TI->replaceSuccessorWith(this, New);
this->replacePhiUsesWith(Pred, New);