Introduce splitBlock and use it in splitEntryBlockForAlloca
RegionInfo::splitBlock did not update RegionInfo correctly. Specifically, it tried to make the new block the entry block if possible. This breaks for nested regions that have edges to the old block.
We simply do not change the entry block. Updating RegionInfo becomes trivial as both block will always be in the same region.
splitEntryBlockForAlloca makes use of the new splitBlock.
Reviewers: grosser
Part of Differential Revision: http://reviews.llvm.org/D11867
llvm-svn: 244600
diff --git a/polly/lib/Support/ScopHelper.cpp b/polly/lib/Support/ScopHelper.cpp
index 176b30d..633f74b 100644
--- a/polly/lib/Support/ScopHelper.cpp
+++ b/polly/lib/Support/ScopHelper.cpp
@@ -169,6 +169,38 @@
return EnteringBB;
}
+// Split the block into two successive blocks.
+//
+// Like llvm::SplitBlock, but also preserves RegionInfo
+static BasicBlock *splitBlock(BasicBlock *Old, Instruction *SplitPt,
+ DominatorTree *DT, llvm::LoopInfo *LI,
+ RegionInfo *RI) {
+ assert(Old && SplitPt);
+
+ // Before:
+ //
+ // \ / //
+ // Old //
+ // / \ //
+
+ BasicBlock *NewBlock = llvm::SplitBlock(Old, SplitPt, DT, LI);
+
+ if (RI) {
+ Region *R = RI->getRegionFor(Old);
+ RI->setRegionFor(NewBlock, R);
+ }
+
+ // After:
+ //
+ // \ / //
+ // Old //
+ // | //
+ // NewBlock //
+ // / \ //
+
+ return NewBlock;
+}
+
void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) {
// Find first non-alloca instruction. Every basic block has a non-alloc
// instruction, as every well formed basic block has a terminator.
@@ -180,9 +212,9 @@
auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
+ RegionInfoPass *RIP = P->getAnalysisIfAvailable<RegionInfoPass>();
+ RegionInfo *RI = RIP ? &RIP->getRegionInfo() : nullptr;
- // SplitBlock updates DT, DF and LI.
- BasicBlock *NewEntry = SplitBlock(EntryBlock, I, DT, LI);
- if (RegionInfoPass *RIP = P->getAnalysisIfAvailable<RegionInfoPass>())
- RIP->getRegionInfo().splitBlock(NewEntry, EntryBlock);
+ // splitBlock updates DT, LI and RI.
+ splitBlock(EntryBlock, I, DT, LI, RI);
}