@@ -508,21 +508,11 @@ struct LoopInterchange {
508508 // and do interchange based on a bubble-sort fasion. We start from
509509 // the innermost loop, move it outwards to the best possible position
510510 // and repeat this process.
511- for (unsigned j = SelecLoopId; j > 0 ; j --) {
511+ for (unsigned J = SelecLoopId; J > 0 ; J --) {
512512 bool ChangedPerIter = false ;
513- for (unsigned i = SelecLoopId; i > SelecLoopId - j; i--) {
514- bool Interchanged = processLoop (LoopList[i], LoopList[i - 1 ], i, i - 1 ,
515- DependencyMatrix, CostMap);
516- if (!Interchanged)
517- continue ;
518- // Loops interchanged, update LoopList accordingly.
519- std::swap (LoopList[i - 1 ], LoopList[i]);
520- // Update the DependencyMatrix
521- interChangeDependencies (DependencyMatrix, i, i - 1 );
522-
523- LLVM_DEBUG (dbgs () << " Dependency matrix after interchange:\n " ;
524- printDepMatrix (DependencyMatrix));
525-
513+ for (unsigned I = SelecLoopId; I > SelecLoopId - J; I--) {
514+ bool Interchanged =
515+ processLoop (LoopList, I, I - 1 , DependencyMatrix, CostMap);
526516 ChangedPerIter |= Interchanged;
527517 Changed |= Interchanged;
528518 }
@@ -534,10 +524,12 @@ struct LoopInterchange {
534524 return Changed;
535525 }
536526
537- bool processLoop (Loop *InnerLoop, Loop *OuterLoop , unsigned InnerLoopId,
527+ bool processLoop (SmallVectorImpl< Loop *> &LoopList , unsigned InnerLoopId,
538528 unsigned OuterLoopId,
539529 std::vector<std::vector<char >> &DependencyMatrix,
540530 const DenseMap<const Loop *, unsigned > &CostMap) {
531+ Loop *OuterLoop = LoopList[OuterLoopId];
532+ Loop *InnerLoop = LoopList[InnerLoopId];
541533 LLVM_DEBUG (dbgs () << " Processing InnerLoopId = " << InnerLoopId
542534 << " and OuterLoopId = " << OuterLoopId << " \n " );
543535 LoopInterchangeLegality LIL (OuterLoop, InnerLoop, SE, ORE);
@@ -566,6 +558,15 @@ struct LoopInterchange {
566558 LoopsInterchanged++;
567559
568560 llvm::formLCSSARecursively (*OuterLoop, *DT, LI, SE);
561+
562+ // Loops interchanged, update LoopList accordingly.
563+ std::swap (LoopList[OuterLoopId], LoopList[InnerLoopId]);
564+ // Update the DependencyMatrix
565+ interChangeDependencies (DependencyMatrix, InnerLoopId, OuterLoopId);
566+
567+ LLVM_DEBUG (dbgs () << " Dependency matrix after interchange:\n " ;
568+ printDepMatrix (DependencyMatrix));
569+
569570 return true ;
570571 }
571572};
0 commit comments