Index: lib/Transforms/Scalar/LoopInterchange.cpp =================================================================== --- lib/Transforms/Scalar/LoopInterchange.cpp +++ lib/Transforms/Scalar/LoopInterchange.cpp @@ -663,7 +663,8 @@ return false; for (BasicBlock *Succ : successors(OuterLoopHeaderBI)) - if (Succ != InnerLoopPreHeader && Succ != OuterLoopLatch) + if (Succ != InnerLoopPreHeader && Succ != InnerLoop->getHeader() && + Succ != OuterLoopLatch) return false; LLVM_DEBUG(dbgs() << "Checking instructions in Loop header and Loop latch\n"); @@ -1016,28 +1017,6 @@ return false; } - // Create unique Preheaders if we already do not have one. - BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader(); - BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); - - // Create a unique outer preheader - - // 1) If OuterLoop preheader is not present. - // 2) If OuterLoop Preheader is same as OuterLoop Header - // 3) If OuterLoop Preheader is same as Header of the previous loop. - // 4) If OuterLoop Preheader is Entry node. - if (!OuterLoopPreHeader || OuterLoopPreHeader == OuterLoop->getHeader() || - isa(OuterLoopPreHeader->begin()) || - !OuterLoopPreHeader->getUniquePredecessor()) { - OuterLoopPreHeader = - InsertPreheaderForLoop(OuterLoop, DT, LI, PreserveLCSSA); - } - - if (!InnerLoopPreHeader || InnerLoopPreHeader == InnerLoop->getHeader() || - InnerLoopPreHeader == OuterLoop->getHeader()) { - InnerLoopPreHeader = - InsertPreheaderForLoop(InnerLoop, DT, LI, PreserveLCSSA); - } - // TODO: The loops could not be interchanged due to current limitations in the // transform module. if (currentLimitations()) { @@ -1356,16 +1335,29 @@ LLVM_DEBUG(dbgs() << "adjustLoopBranches called\n"); std::vector DTUpdates; + BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader(); + BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); + + assert(OuterLoopPreHeader != OuterLoop->getHeader() && + InnerLoopPreHeader != InnerLoop->getHeader() && OuterLoopPreHeader && + InnerLoopPreHeader && "Guaranteed by loop-simplify form"); + // Ensure that both preheaders do not contain PHI nodes and have single + // predecessors. This allows us to move them easily. + if (isa(OuterLoopPreHeader->begin()) || + !OuterLoopPreHeader->getUniquePredecessor()) + OuterLoopPreHeader = InsertPreheaderForLoop(OuterLoop, DT, LI, true); + if (InnerLoopPreHeader == OuterLoop->getHeader()) + InnerLoopPreHeader = InsertPreheaderForLoop(InnerLoop, DT, LI, true); + // Adjust the loop preheader BasicBlock *InnerLoopHeader = InnerLoop->getHeader(); BasicBlock *OuterLoopHeader = OuterLoop->getHeader(); BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch(); BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch(); - BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader(); - BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader(); BasicBlock *OuterLoopPredecessor = OuterLoopPreHeader->getUniquePredecessor(); BasicBlock *InnerLoopLatchPredecessor = InnerLoopLatch->getUniquePredecessor(); + BasicBlock *InnerLoopLatchSuccessor; BasicBlock *OuterLoopLatchSuccessor;