diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp index f45d90ff13e14..967be109a7ba6 100644 --- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp +++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp @@ -511,18 +511,8 @@ struct LoopInterchange { for (unsigned j = SelecLoopId; j > 0; j--) { bool ChangedPerIter = false; for (unsigned i = SelecLoopId; i > SelecLoopId - j; i--) { - bool Interchanged = processLoop(LoopList[i], LoopList[i - 1], i, i - 1, - DependencyMatrix, CostMap); - if (!Interchanged) - continue; - // Loops interchanged, update LoopList accordingly. - std::swap(LoopList[i - 1], LoopList[i]); - // Update the DependencyMatrix - interChangeDependencies(DependencyMatrix, i, i - 1); - - LLVM_DEBUG(dbgs() << "Dependency matrix after interchange:\n"; - printDepMatrix(DependencyMatrix)); - + bool Interchanged = + processLoop(LoopList, i, i - 1, DependencyMatrix, CostMap); ChangedPerIter |= Interchanged; Changed |= Interchanged; } @@ -534,10 +524,12 @@ struct LoopInterchange { return Changed; } - bool processLoop(Loop *InnerLoop, Loop *OuterLoop, unsigned InnerLoopId, + bool processLoop(SmallVectorImpl &LoopList, unsigned InnerLoopId, unsigned OuterLoopId, std::vector> &DependencyMatrix, const DenseMap &CostMap) { + Loop *OuterLoop = LoopList[OuterLoopId]; + Loop *InnerLoop = LoopList[InnerLoopId]; LLVM_DEBUG(dbgs() << "Processing InnerLoopId = " << InnerLoopId << " and OuterLoopId = " << OuterLoopId << "\n"); LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, ORE); @@ -566,6 +558,15 @@ struct LoopInterchange { LoopsInterchanged++; llvm::formLCSSARecursively(*OuterLoop, *DT, LI, SE); + + // Loops interchanged, update LoopList accordingly. + std::swap(LoopList[OuterLoopId], LoopList[InnerLoopId]); + // Update the DependencyMatrix + interChangeDependencies(DependencyMatrix, InnerLoopId, OuterLoopId); + + LLVM_DEBUG(dbgs() << "Dependency matrix after interchange:\n"; + printDepMatrix(DependencyMatrix)); + return true; } };