Skip to content

Commit

Permalink
[CodeExtractor] Fix iterator invalidation in findOrCreateBlockForHois…
Browse files Browse the repository at this point in the history
…ting.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317084 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
fhahn committed Nov 1, 2017
1 parent bfaa9ed commit 81b03a3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/Transforms/Utils/CodeExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion test/Transforms/CodeExtractor/live_shrink_hoist.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: opt -S -partial-inliner -max-num-inline-blocks=2 -skip-partial-inlining-cost-analysis < %s | FileCheck %s
; RUN: opt -S -partial-inliner -max-num-inline-blocks=3 -skip-partial-inlining-cost-analysis < %s | FileCheck %s
; RUN: opt -S -passes=partial-inliner -max-num-inline-blocks=2 -skip-partial-inlining-cost-analysis < %s | FileCheck %s

%class.A = type { i32 }
Expand All @@ -16,6 +16,10 @@ bb:
br i1 %tmp3, label %bb4, label %bb9

bb4: ; preds = %bb
%foo = icmp eq i32 %tmp2, 0
br i1 %foo, label %bb5, label %bb9

bb5: ; preds = %bb4
call void @_ZN1A7memfuncEv(%class.A* nonnull %tmp)
%tmp5 = getelementptr inbounds %class.A, %class.A* %tmp, i64 0, i32 0
%tmp6 = load i32, i32* %tmp5, align 4, !tbaa !6
Expand Down

0 comments on commit 81b03a3

Please sign in to comment.