[CodeExtractor] Fix iterator invalidation in findOrCreateBlockForHoisting.

Summary:
By replacing branches to CommonExitBlock, we remove the node from
CommonExitBlock's predecessors, invalidating the iterator. The problem
is exposed when the common exit block has multiple predecessors and
needs to sink lifetime info. The modification in the test case trigger
the issue.

Reviewers: davidxl, davide, wmi

Reviewed By: davidxl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D39112

llvm-svn: 317084
This commit is contained in:
Florian Hahn
2017-11-01 09:48:12 +00:00
parent 948c0bcbd6
commit b93c06331e
2 changed files with 8 additions and 2 deletions

View File

@@ -307,7 +307,9 @@ CodeExtractor::findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock) {
BasicBlock *NewExitBlock = CommonExitBlock->splitBasicBlock(
CommonExitBlock->getFirstNonPHI()->getIterator());
for (auto *Pred : predecessors(CommonExitBlock)) {
for (auto PI = pred_begin(CommonExitBlock), PE = pred_end(CommonExitBlock);
PI != PE;) {
BasicBlock *Pred = *PI++;
if (Blocks.count(Pred))
continue;
Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock);