Index: include/polly/Support/ScopHelper.h =================================================================== --- include/polly/Support/ScopHelper.h +++ include/polly/Support/ScopHelper.h @@ -75,8 +75,8 @@ /// @brief Split the block into two successive blocks. /// -/// Like llvm::SplitBlock but also preserves RegionInfo and a suffix other than -/// ".split" +/// Like llvm::SplitBlock, but also preserves RegionInfo and allows a suffix +/// other than ".split" /// /// @param Old Block to split; will be predecessor of the new BasicBlock. /// @param SplitPt All instructions after and including this instruction are @@ -91,27 +91,5 @@ llvm::BasicBlock *splitBlock(llvm::BasicBlock *Old, llvm::Instruction *SplitPt, llvm::StringRef Suffix, llvm::DominatorTree *DT, llvm::LoopInfo *LI, llvm::RegionInfo *RI); - -/// @brief Move a set of incoming edges to a new successor. -/// -/// Similar to llvm::SplitPredecessors, but instead of creating a new -/// predecessor, this creates a new successor. -/// We generally cannot update RegionInfo since it could make them invalid; do -/// this on a higher level where consistency is ensured. -/// Preserves LCSSA since it all PHIs will exist in both BasicBlocks -/// -/// @param BB Block to split. -/// @param NonPreds Set of incoming edges to move to the new successor. -/// @param Suffix Suffic to append to the name of the new block. -/// @param DT DominatorTree to update. -/// @param LI LoopIndo to update. -/// @param SE ScalarEvolution to update. -/// -/// @return The newly created successor of BB. -llvm::BasicBlock * -splitBlockNonPredecessors(llvm::BasicBlock *BB, - llvm::ArrayRef<llvm::BasicBlock *> NonPreds, - llvm::StringRef Suffix, llvm::DominatorTree *DT, - llvm::LoopInfo *LI, llvm::ScalarEvolution *SE); } #endif Index: lib/CodeGen/Utils.cpp =================================================================== --- lib/CodeGen/Utils.cpp +++ lib/CodeGen/Utils.cpp @@ -22,20 +22,17 @@ using namespace llvm; -// splitEdge - alternative to llvm::SplitCriticalEdge -// llvm::SplitCriticalEdge does nothing if the edge is not critical -// llvm::SplitEdge is unpredictable in which block it creates +// Alternative to llvm::SplitCriticalEdge. +// llvm::SplitCriticalEdge does nothing if the edge is not critical. +// llvm::SplitEdge is unpredictable in which block it creates. +// +// Creates a new block which branches to Succ. The edge to split is redirected +// to the new block. static BasicBlock *splitEdge(BasicBlock *Prev, BasicBlock *Succ, const char *Suffix, DominatorTree *DT, LoopInfo *LI, RegionInfo *RI) { assert(Prev && Succ); -#ifndef NDEBUG - DT->verifyDomTree(); - LI->verify(); - RI->verifyAnalysis(); -#endif - // Before: /* \ / / */ /* Prev / */ @@ -59,17 +56,6 @@ } else { RI->setRegionFor(MiddleBlock, SuccRegion); } -#if 0 - if (PrevRegion->getExit() == Succ) { - // MiddleBlock got "sucked" into the predecessor region - RI->setRegionFor(MiddleBlock, PrevRegion); - } else if (SuccRegion->getEntry() != Succ || SuccRegion->contains(Prev)) { - // This case also applies to the TopLevelRegion - RI->setRegionFor(MiddleBlock, PrevRegion); - } else { - RI->setRegionFor(MiddleBlock, SuccRegion->getParent()); - } -#endif } // After: @@ -83,12 +69,6 @@ /* Succ \ */ /* / \ \ */ -#ifndef NDEBUG - DT->verifyDomTree(); - LI->verify(); - RI->verifyAnalysis(); -#endif - return MiddleBlock; } @@ -136,11 +116,5 @@ Builder.CreateBr(MergeBlock); DT.changeImmediateDominator(MergeBlock, JunctionBlock); -#ifndef NDEBUG - DT.verifyDomTree(); - LI.verify(); - RI.verifyAnalysis(); -#endif - return StartBlock; } Index: lib/Support/ScopHelper.cpp =================================================================== --- lib/Support/ScopHelper.cpp +++ lib/Support/ScopHelper.cpp @@ -201,12 +201,12 @@ } assert(ExitingBB == R->getExitingBlock()); -// After: -/* \ / */ -/* ExitingBB _____/ */ -/* \ / */ -/* ExitBB */ -/* / \ */ + // After: + /* \ / */ + /* ExitingBB _____/ */ + /* \ / */ + /* ExitBB */ + /* / \ */ } void polly::simplifyRegion(Region *R, Pass *P) { @@ -242,18 +242,18 @@ assert(Old && SplitPt); // Before: - /* \ / */ - /* Old */ - /* / \ */ + /* \ / */ + /* Old */ + /* / \ */ auto OldName = Old->getName(); BasicBlock *NewBlock = llvm::SplitBlock(Old, SplitPt, DT, LI); NewBlock->setName(OldName + Suffix); // After: - /* \ / */ - /* Old */ - /* | */ - /* NewBlock */ - /* / \ */ + /* \ / */ + /* Old */ + /* | */ + /* NewBlock */ + /* / \ */ if (RI) { Region *R = RI->getRegionFor(Old); @@ -262,132 +262,3 @@ return NewBlock; } - -BasicBlock *polly::splitBlockNonPredecessors( - BasicBlock *BB, ArrayRef<BasicBlock *> NonPreds, llvm::StringRef Suffix, - DominatorTree *DT, LoopInfo *LI, /*RegionInfo *RI,*/ ScalarEvolution *SE) { - assert(BB); - - // Do not attempt to split that which cannot be split. - if (!BB->canSplitPredecessors()) - return nullptr; - - // Currently cannot handle landing pads - if (BB->isLandingPad()) - return nullptr; - - // Before: - /* NonPreds[0] */ - /* \ / */ - /* BB */ - /* / \ */ - - // Create new basic block, insert right after the original block. - BasicBlock *NewBB = - splitBlock(BB, BB->getFirstInsertionPt(), Suffix, DT, LI, nullptr); - - // Create new PHIs into the new block - for (auto I = BB->begin(); isa<PHINode>(I); ++I) { - PHINode *OldPHI = cast<PHINode>(&*I); - PHINode *NewPHI = PHINode::Create(OldPHI->getType(), NonPreds.size() + 1, - OldPHI->getName() + ".np", - NewBB->getFirstInsertionPt()); - - // Replaces uses of the original PHI by the new ones - OldPHI->replaceAllUsesWith(NewPHI); - - // Add an edge from the original block (this adds a use) - NewPHI->addIncoming(OldPHI, BB); - - // Move the incoming block's values - for (auto Edge : NonPreds) { - BasicBlock *RedirectedEdge = Edge == BB ? NewBB : Edge; - auto i = OldPHI->getBasicBlockIndex(RedirectedEdge); - assert(i >= 0); - Value *Val = OldPHI->getIncomingValue(i); - NewPHI->addIncoming(Val, RedirectedEdge); - OldPHI->removeIncomingValue(i, false); - - if (SE) - SE->forgetValue(OldPHI); - } - } - - // If there were no edges to move, there is nothing more to do - if (NonPreds.empty()) - return NewBB; - - // Move all non-pred edges to the new BB - for (auto Edge : NonPreds) { - BasicBlock *RedirectedEdge = Edge == BB ? NewBB : Edge; - assert(!isa<IndirectBrInst>(RedirectedEdge->getTerminator()) && - "Cannot split an edge from an IndirectBrInst"); - RedirectedEdge->getTerminator()->replaceUsesOfWith(BB, NewBB); - } - - // After: - /* NonPreds[0] */ - /* \ / */ - /* BB / */ - /* \ / */ - /* NewBB */ - /* / \ */ - - if (DT) { - // DominatorTree::splitBlock assumes that it is the predecessor which is - // new. - // In order to reuse that function, we recreated the situtation as if it - // was. - auto BBNode = DT->getNode(BB); - auto NewBBNode = DT->getNode(NewBB); - DT->changeImmediateDominator(NewBBNode, BBNode->getIDom()); - DT->eraseNode(BB); - - DT->splitBlock(BB); - } - - if (LI) { - BasicBlock *OldBB = BB; - Loop *L = LI->getLoopFor(OldBB); - while (L) { - assert(L->contains(OldBB)); - - bool OldBBReachableFromInside = false; - for (auto Pred : make_range(pred_begin(OldBB), pred_end(OldBB))) { - if (Pred == NewBB || L->contains(Pred)) { - OldBBReachableFromInside = true; - break; - } - } - - bool NewBBReachableFromOutside = false; - for (auto Pred : make_range(pred_begin(NewBB), pred_end(NewBB))) { - if (Pred == OldBB) - continue; - - if (Pred != NewBB && !L->contains(Pred)) { - NewBBReachableFromOutside = true; - break; - } - } - - if (NewBBReachableFromOutside || !OldBBReachableFromInside) { - // Remove BB from the loop and make the NewBB the next loop header - // Since NewBB dominates OldBB, it can be the new header - if (L->getHeader() == OldBB) - L->moveToHeader(NewBB); - } - - if (!OldBBReachableFromInside) { - // OldBB cannot be reached from the loop header anymore, i.e. it has - // been excluded from the loop - L->removeBlockFromLoop(BB); - LI->changeLoopFor(BB, L->getParentLoop()); - } - - L = L->getParentLoop(); - } - } - - return NewBB; -}