[PATCH] D39112: [CodeExtractor] Fix iterator invalidation in findOrCreateBlockForHoisting.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 19 23:05:47 PDT 2017


fhahn created this revision.

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.


https://reviews.llvm.org/D39112

Files:
  lib/Transforms/Utils/CodeExtractor.cpp
  test/Transforms/CodeExtractor/live_shrink_hoist.ll


Index: test/Transforms/CodeExtractor/live_shrink_hoist.ll
===================================================================
--- test/Transforms/CodeExtractor/live_shrink_hoist.ll
+++ test/Transforms/CodeExtractor/live_shrink_hoist.ll
@@ -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 }
@@ -16,6 +16,10 @@
   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
Index: lib/Transforms/Utils/CodeExtractor.cpp
===================================================================
--- lib/Transforms/Utils/CodeExtractor.cpp
+++ lib/Transforms/Utils/CodeExtractor.cpp
@@ -307,7 +307,9 @@
   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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39112.119631.patch
Type: text/x-patch
Size: 1730 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171020/0fb67b70/attachment.bin>


More information about the llvm-commits mailing list