1 //===- LoopInterchange.cpp - Loop interchange pass------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This Pass handles loop interchange transform.
11 // This pass interchanges loops to provide a more cache-friendly memory access
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Analysis/AssumptionCache.h"
19 #include "llvm/Analysis/BlockFrequencyInfo.h"
20 #include "llvm/Analysis/CodeMetrics.h"
21 #include "llvm/Analysis/DependenceAnalysis.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/Analysis/LoopIterator.h"
24 #include "llvm/Analysis/LoopPass.h"
25 #include "llvm/Analysis/ScalarEvolution.h"
26 #include "llvm/Analysis/ScalarEvolutionExpander.h"
27 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
28 #include "llvm/Analysis/TargetTransformInfo.h"
29 #include "llvm/Analysis/ValueTracking.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/IRBuilder.h"
33 #include "llvm/IR/InstIterator.h"
34 #include "llvm/IR/IntrinsicInst.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Transforms/Scalar.h"
40 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
41 #include "llvm/Transforms/Utils/LoopUtils.h"
42 #include "llvm/Transforms/Utils/SSAUpdater.h"
45 #define DEBUG_TYPE "loop-interchange"
49 typedef SmallVector<Loop *, 8> LoopVector;
51 // TODO: Check if we can use a sparse matrix here.
52 typedef std::vector<std::vector<char>> CharMatrix;
54 // Maximum number of dependencies that can be handled in the dependency matrix.
55 static const unsigned MaxMemInstrCount = 100;
57 // Maximum loop depth supported.
58 static const unsigned MaxLoopNestDepth = 10;
60 struct LoopInterchange;
62 #ifdef DUMP_DEP_MATRICIES
63 void printDepMatrix(CharMatrix &DepMatrix) {
64 for (auto I = DepMatrix.begin(), E = DepMatrix.end(); I != E; ++I) {
65 std::vector<char> Vec = *I;
66 for (auto II = Vec.begin(), EE = Vec.end(); II != EE; ++II)
67 DEBUG(dbgs() << *II << " ");
68 DEBUG(dbgs() << "\n");
73 static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
74 Loop *L, DependenceInfo *DI) {
75 typedef SmallVector<Value *, 16> ValueVector;
78 if (Level > MaxLoopNestDepth) {
79 DEBUG(dbgs() << "Cannot handle loops of depth greater than "
80 << MaxLoopNestDepth << "\n");
85 for (Loop::block_iterator BB = L->block_begin(), BE = L->block_end();
87 // Scan the BB and collect legal loads and stores.
88 for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E;
90 Instruction *Ins = dyn_cast<Instruction>(I);
93 LoadInst *Ld = dyn_cast<LoadInst>(I);
94 StoreInst *St = dyn_cast<StoreInst>(I);
97 if (Ld && !Ld->isSimple())
99 if (St && !St->isSimple())
101 MemInstr.push_back(&*I);
105 DEBUG(dbgs() << "Found " << MemInstr.size()
106 << " Loads and Stores to analyze\n");
108 ValueVector::iterator I, IE, J, JE;
110 for (I = MemInstr.begin(), IE = MemInstr.end(); I != IE; ++I) {
111 for (J = I, JE = MemInstr.end(); J != JE; ++J) {
112 std::vector<char> Dep;
113 Instruction *Src = dyn_cast<Instruction>(*I);
114 Instruction *Des = dyn_cast<Instruction>(*J);
117 if (isa<LoadInst>(Src) && isa<LoadInst>(Des))
119 if (auto D = DI->depends(Src, Des, true)) {
120 DEBUG(dbgs() << "Found Dependency between Src=" << Src << " Des=" << Des
123 // TODO: Handle Flow dependence.Check if it is sufficient to populate
124 // the Dependence Matrix with the direction reversed.
125 DEBUG(dbgs() << "Flow dependence not handled");
129 DEBUG(dbgs() << "Found Anti dependence \n");
130 unsigned Levels = D->getLevels();
132 for (unsigned II = 1; II <= Levels; ++II) {
133 const SCEV *Distance = D->getDistance(II);
134 const SCEVConstant *SCEVConst =
135 dyn_cast_or_null<SCEVConstant>(Distance);
137 const ConstantInt *CI = SCEVConst->getValue();
138 if (CI->isNegative())
140 else if (CI->isZero())
144 Dep.push_back(Direction);
145 } else if (D->isScalar(II)) {
147 Dep.push_back(Direction);
149 unsigned Dir = D->getDirection(II);
150 if (Dir == Dependence::DVEntry::LT ||
151 Dir == Dependence::DVEntry::LE)
153 else if (Dir == Dependence::DVEntry::GT ||
154 Dir == Dependence::DVEntry::GE)
156 else if (Dir == Dependence::DVEntry::EQ)
160 Dep.push_back(Direction);
163 while (Dep.size() != Level) {
167 DepMatrix.push_back(Dep);
168 if (DepMatrix.size() > MaxMemInstrCount) {
169 DEBUG(dbgs() << "Cannot handle more than " << MaxMemInstrCount
170 << " dependencies inside loop\n");
178 // We don't have a DepMatrix to check legality return false.
179 if (DepMatrix.size() == 0)
184 // A loop is moved from index 'from' to an index 'to'. Update the Dependence
185 // matrix by exchanging the two columns.
186 static void interChangeDepedencies(CharMatrix &DepMatrix, unsigned FromIndx,
188 unsigned numRows = DepMatrix.size();
189 for (unsigned i = 0; i < numRows; ++i) {
190 char TmpVal = DepMatrix[i][ToIndx];
191 DepMatrix[i][ToIndx] = DepMatrix[i][FromIndx];
192 DepMatrix[i][FromIndx] = TmpVal;
196 // Checks if outermost non '=','S'or'I' dependence in the dependence matrix is
198 static bool isOuterMostDepPositive(CharMatrix &DepMatrix, unsigned Row,
200 for (unsigned i = 0; i <= Column; ++i) {
201 if (DepMatrix[Row][i] == '<')
203 if (DepMatrix[Row][i] == '>')
206 // All dependencies were '=','S' or 'I'
210 // Checks if no dependence exist in the dependency matrix in Row before Column.
211 static bool containsNoDependence(CharMatrix &DepMatrix, unsigned Row,
213 for (unsigned i = 0; i < Column; ++i) {
214 if (DepMatrix[Row][i] != '=' || DepMatrix[Row][i] != 'S' ||
215 DepMatrix[Row][i] != 'I')
221 static bool validDepInterchange(CharMatrix &DepMatrix, unsigned Row,
222 unsigned OuterLoopId, char InnerDep,
225 if (isOuterMostDepPositive(DepMatrix, Row, OuterLoopId))
228 if (InnerDep == OuterDep)
231 // It is legal to interchange if and only if after interchange no row has a
232 // '>' direction as the leftmost non-'='.
234 if (InnerDep == '=' || InnerDep == 'S' || InnerDep == 'I')
240 if (InnerDep == '>') {
241 // If OuterLoopId represents outermost loop then interchanging will make the
242 // 1st dependency as '>'
243 if (OuterLoopId == 0)
246 // If all dependencies before OuterloopId are '=','S'or 'I'. Then
247 // interchanging will result in this row having an outermost non '='
249 if (!containsNoDependence(DepMatrix, Row, OuterLoopId))
256 // Checks if it is legal to interchange 2 loops.
257 // [Theorem] A permutation of the loops in a perfect nest is legal if and only
259 // the direction matrix, after the same permutation is applied to its columns,
260 // has no ">" direction as the leftmost non-"=" direction in any row.
261 static bool isLegalToInterChangeLoops(CharMatrix &DepMatrix,
262 unsigned InnerLoopId,
263 unsigned OuterLoopId) {
265 unsigned NumRows = DepMatrix.size();
266 // For each row check if it is valid to interchange.
267 for (unsigned Row = 0; Row < NumRows; ++Row) {
268 char InnerDep = DepMatrix[Row][InnerLoopId];
269 char OuterDep = DepMatrix[Row][OuterLoopId];
270 if (InnerDep == '*' || OuterDep == '*')
272 else if (!validDepInterchange(DepMatrix, Row, OuterLoopId, InnerDep,
279 static void populateWorklist(Loop &L, SmallVector<LoopVector, 8> &V) {
281 DEBUG(dbgs() << "Calling populateWorklist called\n");
283 Loop *CurrentLoop = &L;
284 const std::vector<Loop *> *Vec = &CurrentLoop->getSubLoops();
285 while (!Vec->empty()) {
286 // The current loop has multiple subloops in it hence it is not tightly
288 // Discard all loops above it added into Worklist.
289 if (Vec->size() != 1) {
293 LoopList.push_back(CurrentLoop);
294 CurrentLoop = Vec->front();
295 Vec = &CurrentLoop->getSubLoops();
297 LoopList.push_back(CurrentLoop);
298 V.push_back(std::move(LoopList));
301 static PHINode *getInductionVariable(Loop *L, ScalarEvolution *SE) {
302 PHINode *InnerIndexVar = L->getCanonicalInductionVariable();
304 return InnerIndexVar;
305 if (L->getLoopLatch() == nullptr || L->getLoopPredecessor() == nullptr)
307 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
308 PHINode *PhiVar = cast<PHINode>(I);
309 Type *PhiTy = PhiVar->getType();
310 if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() &&
311 !PhiTy->isPointerTy())
313 const SCEVAddRecExpr *AddRec =
314 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(PhiVar));
315 if (!AddRec || !AddRec->isAffine())
317 const SCEV *Step = AddRec->getStepRecurrence(*SE);
318 const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
321 // Found the induction variable.
322 // FIXME: Handle loops with more than one induction variable. Note that,
323 // currently, legality makes sure we have only one induction variable.
329 /// LoopInterchangeLegality checks if it is legal to interchange the loop.
330 class LoopInterchangeLegality {
332 LoopInterchangeLegality(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
333 LoopInfo *LI, DominatorTree *DT, bool PreserveLCSSA)
334 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT),
335 PreserveLCSSA(PreserveLCSSA), InnerLoopHasReduction(false) {}
337 /// Check if the loops can be interchanged.
338 bool canInterchangeLoops(unsigned InnerLoopId, unsigned OuterLoopId,
339 CharMatrix &DepMatrix);
340 /// Check if the loop structure is understood. We do not handle triangular
342 bool isLoopStructureUnderstood(PHINode *InnerInductionVar);
344 bool currentLimitations();
346 bool hasInnerLoopReduction() { return InnerLoopHasReduction; }
349 bool tightlyNested(Loop *Outer, Loop *Inner);
350 bool containsUnsafeInstructionsInHeader(BasicBlock *BB);
351 bool areAllUsesReductions(Instruction *Ins, Loop *L);
352 bool containsUnsafeInstructionsInLatch(BasicBlock *BB);
353 bool findInductionAndReductions(Loop *L,
354 SmallVector<PHINode *, 8> &Inductions,
355 SmallVector<PHINode *, 8> &Reductions);
364 bool InnerLoopHasReduction;
367 /// LoopInterchangeProfitability checks if it is profitable to interchange the
369 class LoopInterchangeProfitability {
371 LoopInterchangeProfitability(Loop *Outer, Loop *Inner, ScalarEvolution *SE)
372 : OuterLoop(Outer), InnerLoop(Inner), SE(SE) {}
374 /// Check if the loop interchange is profitable.
375 bool isProfitable(unsigned InnerLoopId, unsigned OuterLoopId,
376 CharMatrix &DepMatrix);
379 int getInstrOrderCost();
388 /// LoopInterchangeTransform interchanges the loop.
389 class LoopInterchangeTransform {
391 LoopInterchangeTransform(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
392 LoopInfo *LI, DominatorTree *DT,
393 BasicBlock *LoopNestExit,
394 bool InnerLoopContainsReductions)
395 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT),
396 LoopExit(LoopNestExit),
397 InnerLoopHasReduction(InnerLoopContainsReductions) {}
399 /// Interchange OuterLoop and InnerLoop.
401 void restructureLoops(Loop *InnerLoop, Loop *OuterLoop);
402 void removeChildLoop(Loop *OuterLoop, Loop *InnerLoop);
405 void splitInnerLoopLatch(Instruction *);
406 void splitInnerLoopHeader();
407 bool adjustLoopLinks();
408 void adjustLoopPreheaders();
409 bool adjustLoopBranches();
410 void updateIncomingBlock(BasicBlock *CurrBlock, BasicBlock *OldPred,
411 BasicBlock *NewPred);
420 BasicBlock *LoopExit;
421 bool InnerLoopHasReduction;
424 // Main LoopInterchange Pass.
425 struct LoopInterchange : public FunctionPass {
433 : FunctionPass(ID), SE(nullptr), LI(nullptr), DI(nullptr), DT(nullptr) {
434 initializeLoopInterchangePass(*PassRegistry::getPassRegistry());
437 void getAnalysisUsage(AnalysisUsage &AU) const override {
438 AU.addRequired<ScalarEvolutionWrapperPass>();
439 AU.addRequired<AAResultsWrapperPass>();
440 AU.addRequired<DominatorTreeWrapperPass>();
441 AU.addRequired<LoopInfoWrapperPass>();
442 AU.addRequired<DependenceAnalysisWrapperPass>();
443 AU.addRequiredID(LoopSimplifyID);
444 AU.addRequiredID(LCSSAID);
447 bool runOnFunction(Function &F) override {
451 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
452 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
453 DI = &getAnalysis<DependenceAnalysisWrapperPass>().getDI();
454 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
455 DT = DTWP ? &DTWP->getDomTree() : nullptr;
456 PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
458 // Build up a worklist of loop pairs to analyze.
459 SmallVector<LoopVector, 8> Worklist;
462 populateWorklist(*L, Worklist);
464 DEBUG(dbgs() << "Worklist size = " << Worklist.size() << "\n");
466 while (!Worklist.empty()) {
467 LoopVector LoopList = Worklist.pop_back_val();
468 Changed = processLoopList(LoopList, F);
473 bool isComputableLoopNest(LoopVector LoopList) {
474 for (Loop *L : LoopList) {
475 const SCEV *ExitCountOuter = SE->getBackedgeTakenCount(L);
476 if (ExitCountOuter == SE->getCouldNotCompute()) {
477 DEBUG(dbgs() << "Couldn't compute Backedge count\n");
480 if (L->getNumBackEdges() != 1) {
481 DEBUG(dbgs() << "NumBackEdges is not equal to 1\n");
484 if (!L->getExitingBlock()) {
485 DEBUG(dbgs() << "Loop Doesn't have unique exit block\n");
492 unsigned selectLoopForInterchange(const LoopVector &LoopList) {
493 // TODO: Add a better heuristic to select the loop to be interchanged based
494 // on the dependence matrix. Currently we select the innermost loop.
495 return LoopList.size() - 1;
498 bool processLoopList(LoopVector LoopList, Function &F) {
500 bool Changed = false;
501 CharMatrix DependencyMatrix;
502 if (LoopList.size() < 2) {
503 DEBUG(dbgs() << "Loop doesn't contain minimum nesting level.\n");
506 if (!isComputableLoopNest(LoopList)) {
507 DEBUG(dbgs() << "Not vaild loop candidate for interchange\n");
510 Loop *OuterMostLoop = *(LoopList.begin());
512 DEBUG(dbgs() << "Processing LoopList of size = " << LoopList.size()
515 if (!populateDependencyMatrix(DependencyMatrix, LoopList.size(),
516 OuterMostLoop, DI)) {
517 DEBUG(dbgs() << "Populating Dependency matrix failed\n");
520 #ifdef DUMP_DEP_MATRICIES
521 DEBUG(dbgs() << "Dependence before inter change \n");
522 printDepMatrix(DependencyMatrix);
525 BasicBlock *OuterMostLoopLatch = OuterMostLoop->getLoopLatch();
526 BranchInst *OuterMostLoopLatchBI =
527 dyn_cast<BranchInst>(OuterMostLoopLatch->getTerminator());
528 if (!OuterMostLoopLatchBI)
531 // Since we currently do not handle LCSSA PHI's any failure in loop
532 // condition will now branch to LoopNestExit.
533 // TODO: This should be removed once we handle LCSSA PHI nodes.
535 // Get the Outermost loop exit.
536 BasicBlock *LoopNestExit;
537 if (OuterMostLoopLatchBI->getSuccessor(0) == OuterMostLoop->getHeader())
538 LoopNestExit = OuterMostLoopLatchBI->getSuccessor(1);
540 LoopNestExit = OuterMostLoopLatchBI->getSuccessor(0);
542 if (isa<PHINode>(LoopNestExit->begin())) {
543 DEBUG(dbgs() << "PHI Nodes in loop nest exit is not handled for now "
544 "since on failure all loops branch to loop nest exit.\n");
548 unsigned SelecLoopId = selectLoopForInterchange(LoopList);
549 // Move the selected loop outwards to the best possible position.
550 for (unsigned i = SelecLoopId; i > 0; i--) {
552 processLoop(LoopList, i, i - 1, LoopNestExit, DependencyMatrix);
555 // Loops interchanged reflect the same in LoopList
556 std::swap(LoopList[i - 1], LoopList[i]);
558 // Update the DependencyMatrix
559 interChangeDepedencies(DependencyMatrix, i, i - 1);
561 #ifdef DUMP_DEP_MATRICIES
562 DEBUG(dbgs() << "Dependence after inter change \n");
563 printDepMatrix(DependencyMatrix);
565 Changed |= Interchanged;
570 bool processLoop(LoopVector LoopList, unsigned InnerLoopId,
571 unsigned OuterLoopId, BasicBlock *LoopNestExit,
572 std::vector<std::vector<char>> &DependencyMatrix) {
574 DEBUG(dbgs() << "Processing Innder Loop Id = " << InnerLoopId
575 << " and OuterLoopId = " << OuterLoopId << "\n");
576 Loop *InnerLoop = LoopList[InnerLoopId];
577 Loop *OuterLoop = LoopList[OuterLoopId];
579 LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, LI, DT,
581 if (!LIL.canInterchangeLoops(InnerLoopId, OuterLoopId, DependencyMatrix)) {
582 DEBUG(dbgs() << "Not interchanging Loops. Cannot prove legality\n");
585 DEBUG(dbgs() << "Loops are legal to interchange\n");
586 LoopInterchangeProfitability LIP(OuterLoop, InnerLoop, SE);
587 if (!LIP.isProfitable(InnerLoopId, OuterLoopId, DependencyMatrix)) {
588 DEBUG(dbgs() << "Interchanging Loops not profitable\n");
592 LoopInterchangeTransform LIT(OuterLoop, InnerLoop, SE, LI, DT,
593 LoopNestExit, LIL.hasInnerLoopReduction());
595 DEBUG(dbgs() << "Loops interchanged\n");
600 } // end of namespace
601 bool LoopInterchangeLegality::areAllUsesReductions(Instruction *Ins, Loop *L) {
602 return !std::any_of(Ins->user_begin(), Ins->user_end(), [=](User *U) -> bool {
603 PHINode *UserIns = dyn_cast<PHINode>(U);
604 RecurrenceDescriptor RD;
605 return !UserIns || !RecurrenceDescriptor::isReductionPHI(UserIns, L, RD);
609 bool LoopInterchangeLegality::containsUnsafeInstructionsInHeader(
611 for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
612 // Load corresponding to reduction PHI's are safe while concluding if
614 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
615 if (!areAllUsesReductions(L, InnerLoop))
617 } else if (I->mayHaveSideEffects() || I->mayReadFromMemory())
623 bool LoopInterchangeLegality::containsUnsafeInstructionsInLatch(
625 for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
626 // Stores corresponding to reductions are safe while concluding if tightly
628 if (StoreInst *L = dyn_cast<StoreInst>(I)) {
629 PHINode *PHI = dyn_cast<PHINode>(L->getOperand(0));
632 } else if (I->mayHaveSideEffects() || I->mayReadFromMemory())
638 bool LoopInterchangeLegality::tightlyNested(Loop *OuterLoop, Loop *InnerLoop) {
639 BasicBlock *OuterLoopHeader = OuterLoop->getHeader();
640 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
641 BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
643 DEBUG(dbgs() << "Checking if Loops are Tightly Nested\n");
645 // A perfectly nested loop will not have any branch in between the outer and
646 // inner block i.e. outer header will branch to either inner preheader and
648 BranchInst *outerLoopHeaderBI =
649 dyn_cast<BranchInst>(OuterLoopHeader->getTerminator());
650 if (!outerLoopHeaderBI)
652 unsigned num = outerLoopHeaderBI->getNumSuccessors();
653 for (unsigned i = 0; i < num; i++) {
654 if (outerLoopHeaderBI->getSuccessor(i) != InnerLoopPreHeader &&
655 outerLoopHeaderBI->getSuccessor(i) != OuterLoopLatch)
659 DEBUG(dbgs() << "Checking instructions in Loop header and Loop latch \n");
660 // We do not have any basic block in between now make sure the outer header
661 // and outer loop latch doesn't contain any unsafe instructions.
662 if (containsUnsafeInstructionsInHeader(OuterLoopHeader) ||
663 containsUnsafeInstructionsInLatch(OuterLoopLatch))
666 DEBUG(dbgs() << "Loops are perfectly nested \n");
667 // We have a perfect loop nest.
672 bool LoopInterchangeLegality::isLoopStructureUnderstood(
673 PHINode *InnerInduction) {
675 unsigned Num = InnerInduction->getNumOperands();
676 BasicBlock *InnerLoopPreheader = InnerLoop->getLoopPreheader();
677 for (unsigned i = 0; i < Num; ++i) {
678 Value *Val = InnerInduction->getOperand(i);
679 if (isa<Constant>(Val))
681 Instruction *I = dyn_cast<Instruction>(Val);
684 // TODO: Handle triangular loops.
685 // e.g. for(int i=0;i<N;i++)
686 // for(int j=i;j<N;j++)
687 unsigned IncomBlockIndx = PHINode::getIncomingValueNumForOperand(i);
688 if (InnerInduction->getIncomingBlock(IncomBlockIndx) ==
689 InnerLoopPreheader &&
690 !OuterLoop->isLoopInvariant(I)) {
697 bool LoopInterchangeLegality::findInductionAndReductions(
698 Loop *L, SmallVector<PHINode *, 8> &Inductions,
699 SmallVector<PHINode *, 8> &Reductions) {
700 if (!L->getLoopLatch() || !L->getLoopPredecessor())
702 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
703 RecurrenceDescriptor RD;
704 InductionDescriptor ID;
705 PHINode *PHI = cast<PHINode>(I);
706 if (InductionDescriptor::isInductionPHI(PHI, SE, ID))
707 Inductions.push_back(PHI);
708 else if (RecurrenceDescriptor::isReductionPHI(PHI, L, RD))
709 Reductions.push_back(PHI);
712 dbgs() << "Failed to recognize PHI as an induction or reduction.\n");
719 static bool containsSafePHI(BasicBlock *Block, bool isOuterLoopExitBlock) {
720 for (auto I = Block->begin(); isa<PHINode>(I); ++I) {
721 PHINode *PHI = cast<PHINode>(I);
722 // Reduction lcssa phi will have only 1 incoming block that from loop latch.
723 if (PHI->getNumIncomingValues() > 1)
725 Instruction *Ins = dyn_cast<Instruction>(PHI->getIncomingValue(0));
728 // Incoming value for lcssa phi's in outer loop exit can only be inner loop
729 // exits lcssa phi else it would not be tightly nested.
730 if (!isa<PHINode>(Ins) && isOuterLoopExitBlock)
736 static BasicBlock *getLoopLatchExitBlock(BasicBlock *LatchBlock,
737 BasicBlock *LoopHeader) {
738 if (BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator())) {
739 unsigned Num = BI->getNumSuccessors();
741 for (unsigned i = 0; i < Num; ++i) {
742 if (BI->getSuccessor(i) == LoopHeader)
744 return BI->getSuccessor(i);
750 // This function indicates the current limitations in the transform as a result
751 // of which we do not proceed.
752 bool LoopInterchangeLegality::currentLimitations() {
754 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
755 BasicBlock *InnerLoopHeader = InnerLoop->getHeader();
756 BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
757 BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
758 BasicBlock *OuterLoopHeader = OuterLoop->getHeader();
760 PHINode *InnerInductionVar;
761 SmallVector<PHINode *, 8> Inductions;
762 SmallVector<PHINode *, 8> Reductions;
763 if (!findInductionAndReductions(InnerLoop, Inductions, Reductions))
766 // TODO: Currently we handle only loops with 1 induction variable.
767 if (Inductions.size() != 1) {
768 DEBUG(dbgs() << "We currently only support loops with 1 induction variable."
769 << "Failed to interchange due to current limitation\n");
772 if (Reductions.size() > 0)
773 InnerLoopHasReduction = true;
775 InnerInductionVar = Inductions.pop_back_val();
777 if (!findInductionAndReductions(OuterLoop, Inductions, Reductions))
780 // Outer loop cannot have reduction because then loops will not be tightly
782 if (!Reductions.empty())
784 // TODO: Currently we handle only loops with 1 induction variable.
785 if (Inductions.size() != 1)
788 // TODO: Triangular loops are not handled for now.
789 if (!isLoopStructureUnderstood(InnerInductionVar)) {
790 DEBUG(dbgs() << "Loop structure not understood by pass\n");
794 // TODO: We only handle LCSSA PHI's corresponding to reduction for now.
795 BasicBlock *LoopExitBlock =
796 getLoopLatchExitBlock(OuterLoopLatch, OuterLoopHeader);
797 if (!LoopExitBlock || !containsSafePHI(LoopExitBlock, true))
800 LoopExitBlock = getLoopLatchExitBlock(InnerLoopLatch, InnerLoopHeader);
801 if (!LoopExitBlock || !containsSafePHI(LoopExitBlock, false))
804 // TODO: Current limitation: Since we split the inner loop latch at the point
805 // were induction variable is incremented (induction.next); We cannot have
806 // more than 1 user of induction.next since it would result in broken code
809 // for(i=0;i<N;i++) {
810 // for(j = 0;j<M;j++) {
811 // A[j+1][i+2] = A[j][i]+k;
814 Instruction *InnerIndexVarInc = nullptr;
815 if (InnerInductionVar->getIncomingBlock(0) == InnerLoopPreHeader)
817 dyn_cast<Instruction>(InnerInductionVar->getIncomingValue(1));
820 dyn_cast<Instruction>(InnerInductionVar->getIncomingValue(0));
822 if (!InnerIndexVarInc)
825 // Since we split the inner loop latch on this induction variable. Make sure
826 // we do not have any instruction between the induction variable and branch
829 bool FoundInduction = false;
830 for (const Instruction &I : reverse(*InnerLoopLatch)) {
831 if (isa<BranchInst>(I) || isa<CmpInst>(I) || isa<TruncInst>(I))
833 // We found an instruction. If this is not induction variable then it is not
834 // safe to split this loop latch.
835 if (!I.isIdenticalTo(InnerIndexVarInc))
838 FoundInduction = true;
841 // The loop latch ended and we didn't find the induction variable return as
842 // current limitation.
849 bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
850 unsigned OuterLoopId,
851 CharMatrix &DepMatrix) {
853 if (!isLegalToInterChangeLoops(DepMatrix, InnerLoopId, OuterLoopId)) {
854 DEBUG(dbgs() << "Failed interchange InnerLoopId = " << InnerLoopId
855 << "and OuterLoopId = " << OuterLoopId
856 << "due to dependence\n");
860 // Create unique Preheaders if we already do not have one.
861 BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader();
862 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
864 // Create a unique outer preheader -
865 // 1) If OuterLoop preheader is not present.
866 // 2) If OuterLoop Preheader is same as OuterLoop Header
867 // 3) If OuterLoop Preheader is same as Header of the previous loop.
868 // 4) If OuterLoop Preheader is Entry node.
869 if (!OuterLoopPreHeader || OuterLoopPreHeader == OuterLoop->getHeader() ||
870 isa<PHINode>(OuterLoopPreHeader->begin()) ||
871 !OuterLoopPreHeader->getUniquePredecessor()) {
873 InsertPreheaderForLoop(OuterLoop, DT, LI, PreserveLCSSA);
876 if (!InnerLoopPreHeader || InnerLoopPreHeader == InnerLoop->getHeader() ||
877 InnerLoopPreHeader == OuterLoop->getHeader()) {
879 InsertPreheaderForLoop(InnerLoop, DT, LI, PreserveLCSSA);
882 // TODO: The loops could not be interchanged due to current limitations in the
884 if (currentLimitations()) {
885 DEBUG(dbgs() << "Not legal because of current transform limitation\n");
889 // Check if the loops are tightly nested.
890 if (!tightlyNested(OuterLoop, InnerLoop)) {
891 DEBUG(dbgs() << "Loops not tightly nested\n");
898 int LoopInterchangeProfitability::getInstrOrderCost() {
899 unsigned GoodOrder, BadOrder;
900 BadOrder = GoodOrder = 0;
901 for (auto BI = InnerLoop->block_begin(), BE = InnerLoop->block_end();
903 for (Instruction &Ins : **BI) {
904 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Ins)) {
905 unsigned NumOp = GEP->getNumOperands();
906 bool FoundInnerInduction = false;
907 bool FoundOuterInduction = false;
908 for (unsigned i = 0; i < NumOp; ++i) {
909 const SCEV *OperandVal = SE->getSCEV(GEP->getOperand(i));
910 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(OperandVal);
914 // If we find the inner induction after an outer induction e.g.
915 // for(int i=0;i<N;i++)
916 // for(int j=0;j<N;j++)
917 // A[i][j] = A[i-1][j-1]+k;
918 // then it is a good order.
919 if (AR->getLoop() == InnerLoop) {
920 // We found an InnerLoop induction after OuterLoop induction. It is
922 FoundInnerInduction = true;
923 if (FoundOuterInduction) {
928 // If we find the outer induction after an inner induction e.g.
929 // for(int i=0;i<N;i++)
930 // for(int j=0;j<N;j++)
931 // A[j][i] = A[j-1][i-1]+k;
932 // then it is a bad order.
933 if (AR->getLoop() == OuterLoop) {
934 // We found an OuterLoop induction after InnerLoop induction. It is
936 FoundOuterInduction = true;
937 if (FoundInnerInduction) {
946 return GoodOrder - BadOrder;
949 static bool isProfitabileForVectorization(unsigned InnerLoopId,
950 unsigned OuterLoopId,
951 CharMatrix &DepMatrix) {
952 // TODO: Improve this heuristic to catch more cases.
953 // If the inner loop is loop independent or doesn't carry any dependency it is
954 // profitable to move this to outer position.
955 unsigned Row = DepMatrix.size();
956 for (unsigned i = 0; i < Row; ++i) {
957 if (DepMatrix[i][InnerLoopId] != 'S' && DepMatrix[i][InnerLoopId] != 'I')
959 // TODO: We need to improve this heuristic.
960 if (DepMatrix[i][OuterLoopId] != '=')
963 // If outer loop has dependence and inner loop is loop independent then it is
964 // profitable to interchange to enable parallelism.
968 bool LoopInterchangeProfitability::isProfitable(unsigned InnerLoopId,
969 unsigned OuterLoopId,
970 CharMatrix &DepMatrix) {
972 // TODO: Add better profitability checks.
974 // 1) Construct dependency matrix and move the one with no loop carried dep
975 // inside to enable vectorization.
977 // This is rough cost estimation algorithm. It counts the good and bad order
978 // of induction variables in the instruction and allows reordering if number
979 // of bad orders is more than good.
981 Cost += getInstrOrderCost();
982 DEBUG(dbgs() << "Cost = " << Cost << "\n");
986 // It is not profitable as per current cache profitability model. But check if
987 // we can move this loop outside to improve parallelism.
989 isProfitabileForVectorization(InnerLoopId, OuterLoopId, DepMatrix);
993 void LoopInterchangeTransform::removeChildLoop(Loop *OuterLoop,
995 for (Loop::iterator I = OuterLoop->begin(), E = OuterLoop->end(); I != E;
997 if (*I == InnerLoop) {
998 OuterLoop->removeChildLoop(I);
1002 llvm_unreachable("Couldn't find loop");
1005 void LoopInterchangeTransform::restructureLoops(Loop *InnerLoop,
1007 Loop *OuterLoopParent = OuterLoop->getParentLoop();
1008 if (OuterLoopParent) {
1009 // Remove the loop from its parent loop.
1010 removeChildLoop(OuterLoopParent, OuterLoop);
1011 removeChildLoop(OuterLoop, InnerLoop);
1012 OuterLoopParent->addChildLoop(InnerLoop);
1014 removeChildLoop(OuterLoop, InnerLoop);
1015 LI->changeTopLevelLoop(OuterLoop, InnerLoop);
1018 while (!InnerLoop->empty())
1019 OuterLoop->addChildLoop(InnerLoop->removeChildLoop(InnerLoop->begin()));
1021 InnerLoop->addChildLoop(OuterLoop);
1024 bool LoopInterchangeTransform::transform() {
1026 DEBUG(dbgs() << "transform\n");
1027 bool Transformed = false;
1028 Instruction *InnerIndexVar;
1030 if (InnerLoop->getSubLoops().size() == 0) {
1031 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
1032 DEBUG(dbgs() << "Calling Split Inner Loop\n");
1033 PHINode *InductionPHI = getInductionVariable(InnerLoop, SE);
1034 if (!InductionPHI) {
1035 DEBUG(dbgs() << "Failed to find the point to split loop latch \n");
1039 if (InductionPHI->getIncomingBlock(0) == InnerLoopPreHeader)
1040 InnerIndexVar = dyn_cast<Instruction>(InductionPHI->getIncomingValue(1));
1042 InnerIndexVar = dyn_cast<Instruction>(InductionPHI->getIncomingValue(0));
1045 // Split at the place were the induction variable is
1046 // incremented/decremented.
1047 // TODO: This splitting logic may not work always. Fix this.
1048 splitInnerLoopLatch(InnerIndexVar);
1049 DEBUG(dbgs() << "splitInnerLoopLatch Done\n");
1051 // Splits the inner loops phi nodes out into a separate basic block.
1052 splitInnerLoopHeader();
1053 DEBUG(dbgs() << "splitInnerLoopHeader Done\n");
1056 Transformed |= adjustLoopLinks();
1058 DEBUG(dbgs() << "adjustLoopLinks Failed\n");
1062 restructureLoops(InnerLoop, OuterLoop);
1066 void LoopInterchangeTransform::splitInnerLoopLatch(Instruction *Inc) {
1067 BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
1068 BasicBlock *InnerLoopLatchPred = InnerLoopLatch;
1069 InnerLoopLatch = SplitBlock(InnerLoopLatchPred, Inc, DT, LI);
1072 void LoopInterchangeTransform::splitInnerLoopHeader() {
1074 // Split the inner loop header out. Here make sure that the reduction PHI's
1075 // stay in the innerloop body.
1076 BasicBlock *InnerLoopHeader = InnerLoop->getHeader();
1077 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
1078 if (InnerLoopHasReduction) {
1079 // FIXME: Check if the induction PHI will always be the first PHI.
1080 BasicBlock *New = InnerLoopHeader->splitBasicBlock(
1081 ++(InnerLoopHeader->begin()), InnerLoopHeader->getName() + ".split");
1083 if (Loop *L = LI->getLoopFor(InnerLoopHeader))
1084 L->addBasicBlockToLoop(New, *LI);
1086 // Adjust Reduction PHI's in the block.
1087 SmallVector<PHINode *, 8> PHIVec;
1088 for (auto I = New->begin(); isa<PHINode>(I); ++I) {
1089 PHINode *PHI = dyn_cast<PHINode>(I);
1090 Value *V = PHI->getIncomingValueForBlock(InnerLoopPreHeader);
1091 PHI->replaceAllUsesWith(V);
1092 PHIVec.push_back((PHI));
1094 for (PHINode *P : PHIVec) {
1095 P->eraseFromParent();
1098 SplitBlock(InnerLoopHeader, InnerLoopHeader->getFirstNonPHI(), DT, LI);
1101 DEBUG(dbgs() << "Output of splitInnerLoopHeader InnerLoopHeaderSucc & "
1102 "InnerLoopHeader \n");
1105 /// \brief Move all instructions except the terminator from FromBB right before
1107 static void moveBBContents(BasicBlock *FromBB, Instruction *InsertBefore) {
1108 auto &ToList = InsertBefore->getParent()->getInstList();
1109 auto &FromList = FromBB->getInstList();
1111 ToList.splice(InsertBefore->getIterator(), FromList, FromList.begin(),
1112 FromBB->getTerminator()->getIterator());
1115 void LoopInterchangeTransform::updateIncomingBlock(BasicBlock *CurrBlock,
1116 BasicBlock *OldPred,
1117 BasicBlock *NewPred) {
1118 for (auto I = CurrBlock->begin(); isa<PHINode>(I); ++I) {
1119 PHINode *PHI = cast<PHINode>(I);
1120 unsigned Num = PHI->getNumIncomingValues();
1121 for (unsigned i = 0; i < Num; ++i) {
1122 if (PHI->getIncomingBlock(i) == OldPred)
1123 PHI->setIncomingBlock(i, NewPred);
1128 bool LoopInterchangeTransform::adjustLoopBranches() {
1130 DEBUG(dbgs() << "adjustLoopBranches called\n");
1131 // Adjust the loop preheader
1132 BasicBlock *InnerLoopHeader = InnerLoop->getHeader();
1133 BasicBlock *OuterLoopHeader = OuterLoop->getHeader();
1134 BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
1135 BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
1136 BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader();
1137 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
1138 BasicBlock *OuterLoopPredecessor = OuterLoopPreHeader->getUniquePredecessor();
1139 BasicBlock *InnerLoopLatchPredecessor =
1140 InnerLoopLatch->getUniquePredecessor();
1141 BasicBlock *InnerLoopLatchSuccessor;
1142 BasicBlock *OuterLoopLatchSuccessor;
1144 BranchInst *OuterLoopLatchBI =
1145 dyn_cast<BranchInst>(OuterLoopLatch->getTerminator());
1146 BranchInst *InnerLoopLatchBI =
1147 dyn_cast<BranchInst>(InnerLoopLatch->getTerminator());
1148 BranchInst *OuterLoopHeaderBI =
1149 dyn_cast<BranchInst>(OuterLoopHeader->getTerminator());
1150 BranchInst *InnerLoopHeaderBI =
1151 dyn_cast<BranchInst>(InnerLoopHeader->getTerminator());
1153 if (!OuterLoopPredecessor || !InnerLoopLatchPredecessor ||
1154 !OuterLoopLatchBI || !InnerLoopLatchBI || !OuterLoopHeaderBI ||
1158 BranchInst *InnerLoopLatchPredecessorBI =
1159 dyn_cast<BranchInst>(InnerLoopLatchPredecessor->getTerminator());
1160 BranchInst *OuterLoopPredecessorBI =
1161 dyn_cast<BranchInst>(OuterLoopPredecessor->getTerminator());
1163 if (!OuterLoopPredecessorBI || !InnerLoopLatchPredecessorBI)
1165 BasicBlock *InnerLoopHeaderSuccessor = InnerLoopHeader->getUniqueSuccessor();
1166 if (!InnerLoopHeaderSuccessor)
1169 // Adjust Loop Preheader and headers
1171 unsigned NumSucc = OuterLoopPredecessorBI->getNumSuccessors();
1172 for (unsigned i = 0; i < NumSucc; ++i) {
1173 if (OuterLoopPredecessorBI->getSuccessor(i) == OuterLoopPreHeader)
1174 OuterLoopPredecessorBI->setSuccessor(i, InnerLoopPreHeader);
1177 NumSucc = OuterLoopHeaderBI->getNumSuccessors();
1178 for (unsigned i = 0; i < NumSucc; ++i) {
1179 if (OuterLoopHeaderBI->getSuccessor(i) == OuterLoopLatch)
1180 OuterLoopHeaderBI->setSuccessor(i, LoopExit);
1181 else if (OuterLoopHeaderBI->getSuccessor(i) == InnerLoopPreHeader)
1182 OuterLoopHeaderBI->setSuccessor(i, InnerLoopHeaderSuccessor);
1185 // Adjust reduction PHI's now that the incoming block has changed.
1186 updateIncomingBlock(InnerLoopHeaderSuccessor, InnerLoopHeader,
1189 BranchInst::Create(OuterLoopPreHeader, InnerLoopHeaderBI);
1190 InnerLoopHeaderBI->eraseFromParent();
1192 // -------------Adjust loop latches-----------
1193 if (InnerLoopLatchBI->getSuccessor(0) == InnerLoopHeader)
1194 InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(1);
1196 InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(0);
1198 NumSucc = InnerLoopLatchPredecessorBI->getNumSuccessors();
1199 for (unsigned i = 0; i < NumSucc; ++i) {
1200 if (InnerLoopLatchPredecessorBI->getSuccessor(i) == InnerLoopLatch)
1201 InnerLoopLatchPredecessorBI->setSuccessor(i, InnerLoopLatchSuccessor);
1204 // Adjust PHI nodes in InnerLoopLatchSuccessor. Update all uses of PHI with
1205 // the value and remove this PHI node from inner loop.
1206 SmallVector<PHINode *, 8> LcssaVec;
1207 for (auto I = InnerLoopLatchSuccessor->begin(); isa<PHINode>(I); ++I) {
1208 PHINode *LcssaPhi = cast<PHINode>(I);
1209 LcssaVec.push_back(LcssaPhi);
1211 for (PHINode *P : LcssaVec) {
1212 Value *Incoming = P->getIncomingValueForBlock(InnerLoopLatch);
1213 P->replaceAllUsesWith(Incoming);
1214 P->eraseFromParent();
1217 if (OuterLoopLatchBI->getSuccessor(0) == OuterLoopHeader)
1218 OuterLoopLatchSuccessor = OuterLoopLatchBI->getSuccessor(1);
1220 OuterLoopLatchSuccessor = OuterLoopLatchBI->getSuccessor(0);
1222 if (InnerLoopLatchBI->getSuccessor(1) == InnerLoopLatchSuccessor)
1223 InnerLoopLatchBI->setSuccessor(1, OuterLoopLatchSuccessor);
1225 InnerLoopLatchBI->setSuccessor(0, OuterLoopLatchSuccessor);
1227 updateIncomingBlock(OuterLoopLatchSuccessor, OuterLoopLatch, InnerLoopLatch);
1229 if (OuterLoopLatchBI->getSuccessor(0) == OuterLoopLatchSuccessor) {
1230 OuterLoopLatchBI->setSuccessor(0, InnerLoopLatch);
1232 OuterLoopLatchBI->setSuccessor(1, InnerLoopLatch);
1237 void LoopInterchangeTransform::adjustLoopPreheaders() {
1239 // We have interchanged the preheaders so we need to interchange the data in
1240 // the preheader as well.
1241 // This is because the content of inner preheader was previously executed
1242 // inside the outer loop.
1243 BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader();
1244 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
1245 BasicBlock *OuterLoopHeader = OuterLoop->getHeader();
1246 BranchInst *InnerTermBI =
1247 cast<BranchInst>(InnerLoopPreHeader->getTerminator());
1249 // These instructions should now be executed inside the loop.
1250 // Move instruction into a new block after outer header.
1251 moveBBContents(InnerLoopPreHeader, OuterLoopHeader->getTerminator());
1252 // These instructions were not executed previously in the loop so move them to
1253 // the older inner loop preheader.
1254 moveBBContents(OuterLoopPreHeader, InnerTermBI);
1257 bool LoopInterchangeTransform::adjustLoopLinks() {
1259 // Adjust all branches in the inner and outer loop.
1260 bool Changed = adjustLoopBranches();
1262 adjustLoopPreheaders();
1266 char LoopInterchange::ID = 0;
1267 INITIALIZE_PASS_BEGIN(LoopInterchange, "loop-interchange",
1268 "Interchanges loops for cache reuse", false, false)
1269 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
1270 INITIALIZE_PASS_DEPENDENCY(DependenceAnalysisWrapperPass)
1271 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1272 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
1273 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
1274 INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
1275 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
1277 INITIALIZE_PASS_END(LoopInterchange, "loop-interchange",
1278 "Interchanges loops for cache reuse", false, false)
1280 Pass *llvm::createLoopInterchangePass() { return new LoopInterchange(); }