Skip to content

Commit b93c063

Browse files
committed
[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
1 parent 948c0bc commit b93c063

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ CodeExtractor::findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock) {
307307
BasicBlock *NewExitBlock = CommonExitBlock->splitBasicBlock(
308308
CommonExitBlock->getFirstNonPHI()->getIterator());
309309

310-
for (auto *Pred : predecessors(CommonExitBlock)) {
310+
for (auto PI = pred_begin(CommonExitBlock), PE = pred_end(CommonExitBlock);
311+
PI != PE;) {
312+
BasicBlock *Pred = *PI++;
311313
if (Blocks.count(Pred))
312314
continue;
313315
Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock);

llvm/test/Transforms/CodeExtractor/live_shrink_hoist.ll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: opt -S -partial-inliner -max-num-inline-blocks=2 -skip-partial-inlining-cost-analysis < %s | FileCheck %s
1+
; RUN: opt -S -partial-inliner -max-num-inline-blocks=3 -skip-partial-inlining-cost-analysis < %s | FileCheck %s
22
; RUN: opt -S -passes=partial-inliner -max-num-inline-blocks=2 -skip-partial-inlining-cost-analysis < %s | FileCheck %s
33

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

1818
bb4: ; preds = %bb
19+
%foo = icmp eq i32 %tmp2, 0
20+
br i1 %foo, label %bb5, label %bb9
21+
22+
bb5: ; preds = %bb4
1923
call void @_ZN1A7memfuncEv(%class.A* nonnull %tmp)
2024
%tmp5 = getelementptr inbounds %class.A, %class.A* %tmp, i64 0, i32 0
2125
%tmp6 = load i32, i32* %tmp5, align 4, !tbaa !6

0 commit comments

Comments
 (0)