Index: include/llvm/Transforms/Utils/Cloning.h =================================================================== --- include/llvm/Transforms/Utils/Cloning.h +++ include/llvm/Transforms/Utils/Cloning.h @@ -221,7 +221,8 @@ /// /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block /// \p LoopDomBB. Insert the new blocks before block specified in \p Before. -Loop *cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, +/// \p OrigLoop should not have sub-loops. +Loop *cloneInnermostLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, Loop *OrigLoop, ValueToValueMapTy &VMap, const Twine &NameSuffix, LoopInfo *LI, DominatorTree *DT, Index: lib/Transforms/Scalar/LoopDistribute.cpp =================================================================== --- lib/Transforms/Scalar/LoopDistribute.cpp +++ lib/Transforms/Scalar/LoopDistribute.cpp @@ -122,10 +122,12 @@ /// /// Updates LoopInfo and DominatorTree using the information that block \p /// LoopDomBB dominates the loop. - Loop *cloneLoopWithPreheader(BasicBlock *InsertBefore, BasicBlock *LoopDomBB, + Loop *cloneInnermostLoopWithPreheader(BasicBlock *InsertBefore, + BasicBlock *LoopDomBB, unsigned Index, LoopInfo *LI, DominatorTree *DT) { - ClonedLoop = ::cloneLoopWithPreheader(InsertBefore, LoopDomBB, OrigLoop, + ClonedLoop = ::cloneInnermostLoopWithPreheader(InsertBefore, LoopDomBB, + OrigLoop, VMap, Twine(".ldist") + Twine(Index), LI, DT, ClonedLoopBlocks); return ClonedLoop; @@ -403,7 +405,8 @@ I != E; ++I, --Index, TopPH = NewLoop->getLoopPreheader()) { auto *Part = &*I; - NewLoop = Part->cloneLoopWithPreheader(TopPH, Pred, Index, LI, DT); + NewLoop = Part->cloneInnermostLoopWithPreheader + (TopPH, Pred, Index, LI, DT); Part->getVMap()[ExitBlock] = TopPH; Part->remapInstructions(); @@ -412,7 +415,7 @@ // Now go in forward order and update the immediate dominator for the // preheaders with the exiting block of the previous loop. Dominance - // within the loop is updated in cloneLoopWithPreheader. + // within the loop is updated in cloneInnermostLoopWithPreheader. for (auto Curr = PartitionContainer.cbegin(), Next = std::next(PartitionContainer.cbegin()), E = PartitionContainer.cend(); Index: lib/Transforms/Utils/CloneFunction.cpp =================================================================== --- lib/Transforms/Utils/CloneFunction.cpp +++ lib/Transforms/Utils/CloneFunction.cpp @@ -702,11 +702,15 @@ /// /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block /// \p LoopDomBB. Insert the new blocks before block specified in \p Before. -Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, +/// \p OrigLoop should not have sub-loops. +Loop *llvm::cloneInnermostLoopWithPreheader(BasicBlock *Before, + BasicBlock *LoopDomBB, Loop *OrigLoop, ValueToValueMapTy &VMap, const Twine &NameSuffix, LoopInfo *LI, DominatorTree *DT, SmallVectorImpl &Blocks) { + assert(OrigLoop->getSubLoops().empty() && + "Loop to be cloned cannot have inner loop"); Function *F = OrigLoop->getHeader()->getParent(); Loop *ParentLoop = OrigLoop->getParentLoop(); Index: lib/Transforms/Utils/LoopVersioning.cpp =================================================================== --- lib/Transforms/Utils/LoopVersioning.cpp +++ lib/Transforms/Utils/LoopVersioning.cpp @@ -102,7 +102,7 @@ // block is a join between the two loops. SmallVector NonVersionedLoopBlocks; NonVersionedLoop = - cloneLoopWithPreheader(PH, RuntimeCheckBB, VersionedLoop, VMap, + cloneInnermostLoopWithPreheader(PH, RuntimeCheckBB, VersionedLoop, VMap, ".lver.orig", LI, DT, NonVersionedLoopBlocks); remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap);