blob: 4d3e4563c1ec0ee88973466dadf050d8c962012c [file] [log] [blame]
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +00001//===- LoopInterchange.cpp - Loop interchange pass-------------------------===//
Karthik Bhat88db86d2015-03-06 10:11:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This Pass handles loop interchange transform.
11// This pass interchanges loops to provide a more cache-friendly memory access
12// patterns.
13//
14//===----------------------------------------------------------------------===//
15
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000016#include "llvm/ADT/STLExtras.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000017#include "llvm/ADT/SmallVector.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000018#include "llvm/ADT/StringRef.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000019#include "llvm/Analysis/AliasAnalysis.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000020#include "llvm/Analysis/DependenceAnalysis.h"
21#include "llvm/Analysis/LoopInfo.h"
Adam Nemet0965da22017-10-09 23:19:02 +000022#include "llvm/Analysis/OptimizationRemarkEmitter.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000023#include "llvm/Analysis/ScalarEvolution.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000024#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000025#include "llvm/IR/BasicBlock.h"
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000028#include "llvm/IR/Dominators.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000029#include "llvm/IR/Function.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000030#include "llvm/IR/InstrTypes.h"
31#include "llvm/IR/Instruction.h"
32#include "llvm/IR/Instructions.h"
33#include "llvm/IR/Type.h"
34#include "llvm/IR/User.h"
35#include "llvm/IR/Value.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000036#include "llvm/Pass.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000037#include "llvm/Support/Casting.h"
38#include "llvm/Support/CommandLine.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000039#include "llvm/Support/Debug.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000040#include "llvm/Support/ErrorHandling.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000041#include "llvm/Support/raw_ostream.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000042#include "llvm/Transforms/Scalar.h"
David Blaikiea373d182018-03-28 17:44:36 +000043#include "llvm/Transforms/Utils.h"
Karthik Bhat88db86d2015-03-06 10:11:25 +000044#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000045#include "llvm/Transforms/Utils/LoopUtils.h"
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000046#include <cassert>
47#include <utility>
48#include <vector>
Davide Italiano9d8f6f82017-01-29 01:55:24 +000049
Karthik Bhat88db86d2015-03-06 10:11:25 +000050using namespace llvm;
51
52#define DEBUG_TYPE "loop-interchange"
53
Chad Rosier72431892016-09-14 17:07:13 +000054static cl::opt<int> LoopInterchangeCostThreshold(
55 "loop-interchange-threshold", cl::init(0), cl::Hidden,
56 cl::desc("Interchange if you gain more than this number"));
57
Karthik Bhat88db86d2015-03-06 10:11:25 +000058namespace {
59
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000060using LoopVector = SmallVector<Loop *, 8>;
Karthik Bhat88db86d2015-03-06 10:11:25 +000061
62// TODO: Check if we can use a sparse matrix here.
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000063using CharMatrix = std::vector<std::vector<char>>;
64
65} // end anonymous namespace
Karthik Bhat88db86d2015-03-06 10:11:25 +000066
67// Maximum number of dependencies that can be handled in the dependency matrix.
68static const unsigned MaxMemInstrCount = 100;
69
70// Maximum loop depth supported.
71static const unsigned MaxLoopNestDepth = 10;
72
Karthik Bhat88db86d2015-03-06 10:11:25 +000073#ifdef DUMP_DEP_MATRICIES
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000074static void printDepMatrix(CharMatrix &DepMatrix) {
Florian Hahnf66efd62017-07-24 11:41:30 +000075 for (auto &Row : DepMatrix) {
76 for (auto D : Row)
77 DEBUG(dbgs() << D << " ");
Karthik Bhat88db86d2015-03-06 10:11:25 +000078 DEBUG(dbgs() << "\n");
79 }
80}
81#endif
82
Karthik Bhat8210fdf2015-04-23 04:51:44 +000083static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
Chandler Carruth49c22192016-05-12 22:19:39 +000084 Loop *L, DependenceInfo *DI) {
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +000085 using ValueVector = SmallVector<Value *, 16>;
86
Karthik Bhat88db86d2015-03-06 10:11:25 +000087 ValueVector MemInstr;
88
Karthik Bhat88db86d2015-03-06 10:11:25 +000089 // For each block.
Florian Hahnf66efd62017-07-24 11:41:30 +000090 for (BasicBlock *BB : L->blocks()) {
Karthik Bhat88db86d2015-03-06 10:11:25 +000091 // Scan the BB and collect legal loads and stores.
Florian Hahnf66efd62017-07-24 11:41:30 +000092 for (Instruction &I : *BB) {
Chad Rosier09c11092016-09-13 12:56:04 +000093 if (!isa<Instruction>(I))
Karthik Bhat88db86d2015-03-06 10:11:25 +000094 return false;
Florian Hahnf66efd62017-07-24 11:41:30 +000095 if (auto *Ld = dyn_cast<LoadInst>(&I)) {
Chad Rosier09c11092016-09-13 12:56:04 +000096 if (!Ld->isSimple())
97 return false;
Florian Hahnf66efd62017-07-24 11:41:30 +000098 MemInstr.push_back(&I);
99 } else if (auto *St = dyn_cast<StoreInst>(&I)) {
Chad Rosier09c11092016-09-13 12:56:04 +0000100 if (!St->isSimple())
101 return false;
Florian Hahnf66efd62017-07-24 11:41:30 +0000102 MemInstr.push_back(&I);
Chad Rosier09c11092016-09-13 12:56:04 +0000103 }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000104 }
105 }
106
107 DEBUG(dbgs() << "Found " << MemInstr.size()
108 << " Loads and Stores to analyze\n");
109
110 ValueVector::iterator I, IE, J, JE;
111
112 for (I = MemInstr.begin(), IE = MemInstr.end(); I != IE; ++I) {
113 for (J = I, JE = MemInstr.end(); J != JE; ++J) {
114 std::vector<char> Dep;
Chad Rosier09c11092016-09-13 12:56:04 +0000115 Instruction *Src = cast<Instruction>(*I);
116 Instruction *Dst = cast<Instruction>(*J);
Chad Rosier90bcb912016-09-07 16:07:17 +0000117 if (Src == Dst)
Karthik Bhat88db86d2015-03-06 10:11:25 +0000118 continue;
Chad Rosier00eb8db2016-09-21 19:16:47 +0000119 // Ignore Input dependencies.
Chad Rosier90bcb912016-09-07 16:07:17 +0000120 if (isa<LoadInst>(Src) && isa<LoadInst>(Dst))
Karthik Bhat88db86d2015-03-06 10:11:25 +0000121 continue;
Chad Rosier00eb8db2016-09-21 19:16:47 +0000122 // Track Output, Flow, and Anti dependencies.
Chad Rosier90bcb912016-09-07 16:07:17 +0000123 if (auto D = DI->depends(Src, Dst, true)) {
Chad Rosier00eb8db2016-09-21 19:16:47 +0000124 assert(D->isOrdered() && "Expected an output, flow or anti dep.");
125 DEBUG(StringRef DepType =
126 D->isFlow() ? "flow" : D->isAnti() ? "anti" : "output";
127 dbgs() << "Found " << DepType
128 << " dependency between Src and Dst\n"
Chad Rosier90bcb912016-09-07 16:07:17 +0000129 << " Src:" << *Src << "\n Dst:" << *Dst << '\n');
Chad Rosier00eb8db2016-09-21 19:16:47 +0000130 unsigned Levels = D->getLevels();
131 char Direction;
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);
136 if (SCEVConst) {
137 const ConstantInt *CI = SCEVConst->getValue();
138 if (CI->isNegative())
139 Direction = '<';
140 else if (CI->isZero())
141 Direction = '=';
142 else
143 Direction = '>';
144 Dep.push_back(Direction);
145 } else if (D->isScalar(II)) {
146 Direction = 'S';
147 Dep.push_back(Direction);
148 } else {
149 unsigned Dir = D->getDirection(II);
150 if (Dir == Dependence::DVEntry::LT ||
151 Dir == Dependence::DVEntry::LE)
152 Direction = '<';
153 else if (Dir == Dependence::DVEntry::GT ||
154 Dir == Dependence::DVEntry::GE)
155 Direction = '>';
156 else if (Dir == Dependence::DVEntry::EQ)
157 Direction = '=';
158 else
159 Direction = '*';
160 Dep.push_back(Direction);
161 }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000162 }
Chad Rosier00eb8db2016-09-21 19:16:47 +0000163 while (Dep.size() != Level) {
164 Dep.push_back('I');
165 }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000166
Chad Rosier00eb8db2016-09-21 19:16:47 +0000167 DepMatrix.push_back(Dep);
168 if (DepMatrix.size() > MaxMemInstrCount) {
169 DEBUG(dbgs() << "Cannot handle more than " << MaxMemInstrCount
170 << " dependencies inside loop\n");
171 return false;
Karthik Bhat88db86d2015-03-06 10:11:25 +0000172 }
173 }
174 }
175 }
176
Karthik Bhat88db86d2015-03-06 10:11:25 +0000177 return true;
178}
179
180// A loop is moved from index 'from' to an index 'to'. Update the Dependence
181// matrix by exchanging the two columns.
Chad Rosierd18ea062016-09-13 13:00:29 +0000182static void interChangeDependencies(CharMatrix &DepMatrix, unsigned FromIndx,
183 unsigned ToIndx) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000184 unsigned numRows = DepMatrix.size();
185 for (unsigned i = 0; i < numRows; ++i) {
186 char TmpVal = DepMatrix[i][ToIndx];
187 DepMatrix[i][ToIndx] = DepMatrix[i][FromIndx];
188 DepMatrix[i][FromIndx] = TmpVal;
189 }
190}
191
192// Checks if outermost non '=','S'or'I' dependence in the dependence matrix is
193// '>'
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000194static bool isOuterMostDepPositive(CharMatrix &DepMatrix, unsigned Row,
195 unsigned Column) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000196 for (unsigned i = 0; i <= Column; ++i) {
197 if (DepMatrix[Row][i] == '<')
198 return false;
199 if (DepMatrix[Row][i] == '>')
200 return true;
201 }
202 // All dependencies were '=','S' or 'I'
203 return false;
204}
205
206// Checks if no dependence exist in the dependency matrix in Row before Column.
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000207static bool containsNoDependence(CharMatrix &DepMatrix, unsigned Row,
208 unsigned Column) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000209 for (unsigned i = 0; i < Column; ++i) {
Chandler Carruthfca1ff02016-11-03 16:39:25 +0000210 if (DepMatrix[Row][i] != '=' && DepMatrix[Row][i] != 'S' &&
Karthik Bhat88db86d2015-03-06 10:11:25 +0000211 DepMatrix[Row][i] != 'I')
212 return false;
213 }
214 return true;
215}
216
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000217static bool validDepInterchange(CharMatrix &DepMatrix, unsigned Row,
218 unsigned OuterLoopId, char InnerDep,
219 char OuterDep) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000220 if (isOuterMostDepPositive(DepMatrix, Row, OuterLoopId))
221 return false;
222
223 if (InnerDep == OuterDep)
224 return true;
225
226 // It is legal to interchange if and only if after interchange no row has a
227 // '>' direction as the leftmost non-'='.
228
229 if (InnerDep == '=' || InnerDep == 'S' || InnerDep == 'I')
230 return true;
231
232 if (InnerDep == '<')
233 return true;
234
235 if (InnerDep == '>') {
236 // If OuterLoopId represents outermost loop then interchanging will make the
237 // 1st dependency as '>'
238 if (OuterLoopId == 0)
239 return false;
240
241 // If all dependencies before OuterloopId are '=','S'or 'I'. Then
242 // interchanging will result in this row having an outermost non '='
243 // dependency of '>'
244 if (!containsNoDependence(DepMatrix, Row, OuterLoopId))
245 return true;
246 }
247
248 return false;
249}
250
251// Checks if it is legal to interchange 2 loops.
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000252// [Theorem] A permutation of the loops in a perfect nest is legal if and only
Chad Rosier61683a22016-09-13 13:08:53 +0000253// if the direction matrix, after the same permutation is applied to its
254// columns, has no ">" direction as the leftmost non-"=" direction in any row.
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000255static bool isLegalToInterChangeLoops(CharMatrix &DepMatrix,
256 unsigned InnerLoopId,
257 unsigned OuterLoopId) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000258 unsigned NumRows = DepMatrix.size();
259 // For each row check if it is valid to interchange.
260 for (unsigned Row = 0; Row < NumRows; ++Row) {
261 char InnerDep = DepMatrix[Row][InnerLoopId];
262 char OuterDep = DepMatrix[Row][OuterLoopId];
263 if (InnerDep == '*' || OuterDep == '*')
264 return false;
Chad Rosier61683a22016-09-13 13:08:53 +0000265 if (!validDepInterchange(DepMatrix, Row, OuterLoopId, InnerDep, OuterDep))
Karthik Bhat88db86d2015-03-06 10:11:25 +0000266 return false;
267 }
268 return true;
269}
270
271static void populateWorklist(Loop &L, SmallVector<LoopVector, 8> &V) {
Chad Rosierf5814f52016-09-07 15:56:59 +0000272 DEBUG(dbgs() << "Calling populateWorklist on Func: "
273 << L.getHeader()->getParent()->getName() << " Loop: %"
274 << L.getHeader()->getName() << '\n');
Karthik Bhat88db86d2015-03-06 10:11:25 +0000275 LoopVector LoopList;
276 Loop *CurrentLoop = &L;
Benjamin Kramere448b5b2015-07-13 17:21:14 +0000277 const std::vector<Loop *> *Vec = &CurrentLoop->getSubLoops();
278 while (!Vec->empty()) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000279 // The current loop has multiple subloops in it hence it is not tightly
280 // nested.
281 // Discard all loops above it added into Worklist.
Benjamin Kramere448b5b2015-07-13 17:21:14 +0000282 if (Vec->size() != 1) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000283 LoopList.clear();
284 return;
285 }
286 LoopList.push_back(CurrentLoop);
Benjamin Kramere448b5b2015-07-13 17:21:14 +0000287 CurrentLoop = Vec->front();
288 Vec = &CurrentLoop->getSubLoops();
Karthik Bhat88db86d2015-03-06 10:11:25 +0000289 }
290 LoopList.push_back(CurrentLoop);
Benjamin Kramere448b5b2015-07-13 17:21:14 +0000291 V.push_back(std::move(LoopList));
Karthik Bhat88db86d2015-03-06 10:11:25 +0000292}
293
294static PHINode *getInductionVariable(Loop *L, ScalarEvolution *SE) {
295 PHINode *InnerIndexVar = L->getCanonicalInductionVariable();
296 if (InnerIndexVar)
297 return InnerIndexVar;
298 if (L->getLoopLatch() == nullptr || L->getLoopPredecessor() == nullptr)
299 return nullptr;
300 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
301 PHINode *PhiVar = cast<PHINode>(I);
302 Type *PhiTy = PhiVar->getType();
303 if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() &&
304 !PhiTy->isPointerTy())
305 return nullptr;
306 const SCEVAddRecExpr *AddRec =
307 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(PhiVar));
308 if (!AddRec || !AddRec->isAffine())
309 continue;
310 const SCEV *Step = AddRec->getStepRecurrence(*SE);
Chad Rosierf7c76f92016-09-21 13:28:41 +0000311 if (!isa<SCEVConstant>(Step))
Karthik Bhat88db86d2015-03-06 10:11:25 +0000312 continue;
313 // Found the induction variable.
314 // FIXME: Handle loops with more than one induction variable. Note that,
315 // currently, legality makes sure we have only one induction variable.
316 return PhiVar;
317 }
318 return nullptr;
319}
320
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000321namespace {
322
Karthik Bhat88db86d2015-03-06 10:11:25 +0000323/// LoopInterchangeLegality checks if it is legal to interchange the loop.
324class LoopInterchangeLegality {
325public:
326 LoopInterchangeLegality(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
Florian Hahnad993522017-07-15 13:13:19 +0000327 LoopInfo *LI, DominatorTree *DT, bool PreserveLCSSA,
328 OptimizationRemarkEmitter *ORE)
Justin Bogner843fb202015-12-15 19:40:57 +0000329 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT),
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000330 PreserveLCSSA(PreserveLCSSA), ORE(ORE) {}
Karthik Bhat88db86d2015-03-06 10:11:25 +0000331
332 /// Check if the loops can be interchanged.
333 bool canInterchangeLoops(unsigned InnerLoopId, unsigned OuterLoopId,
334 CharMatrix &DepMatrix);
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000335
Karthik Bhat88db86d2015-03-06 10:11:25 +0000336 /// Check if the loop structure is understood. We do not handle triangular
337 /// loops for now.
338 bool isLoopStructureUnderstood(PHINode *InnerInductionVar);
339
340 bool currentLimitations();
341
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000342 bool hasInnerLoopReduction() { return InnerLoopHasReduction; }
343
Karthik Bhat88db86d2015-03-06 10:11:25 +0000344private:
345 bool tightlyNested(Loop *Outer, Loop *Inner);
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000346 bool containsUnsafeInstructionsInHeader(BasicBlock *BB);
347 bool areAllUsesReductions(Instruction *Ins, Loop *L);
348 bool containsUnsafeInstructionsInLatch(BasicBlock *BB);
349 bool findInductionAndReductions(Loop *L,
350 SmallVector<PHINode *, 8> &Inductions,
351 SmallVector<PHINode *, 8> &Reductions);
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000352
Karthik Bhat88db86d2015-03-06 10:11:25 +0000353 Loop *OuterLoop;
354 Loop *InnerLoop;
355
Karthik Bhat88db86d2015-03-06 10:11:25 +0000356 ScalarEvolution *SE;
Justin Bogner843fb202015-12-15 19:40:57 +0000357 LoopInfo *LI;
358 DominatorTree *DT;
359 bool PreserveLCSSA;
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000360
Florian Hahnad993522017-07-15 13:13:19 +0000361 /// Interface to emit optimization remarks.
362 OptimizationRemarkEmitter *ORE;
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000363
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000364 bool InnerLoopHasReduction = false;
Karthik Bhat88db86d2015-03-06 10:11:25 +0000365};
366
367/// LoopInterchangeProfitability checks if it is profitable to interchange the
368/// loop.
369class LoopInterchangeProfitability {
370public:
Florian Hahnad993522017-07-15 13:13:19 +0000371 LoopInterchangeProfitability(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
372 OptimizationRemarkEmitter *ORE)
373 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), ORE(ORE) {}
Karthik Bhat88db86d2015-03-06 10:11:25 +0000374
Vikram TV74b41112015-12-09 05:16:24 +0000375 /// Check if the loop interchange is profitable.
Karthik Bhat88db86d2015-03-06 10:11:25 +0000376 bool isProfitable(unsigned InnerLoopId, unsigned OuterLoopId,
377 CharMatrix &DepMatrix);
378
379private:
380 int getInstrOrderCost();
381
382 Loop *OuterLoop;
383 Loop *InnerLoop;
384
385 /// Scev analysis.
386 ScalarEvolution *SE;
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000387
Florian Hahnad993522017-07-15 13:13:19 +0000388 /// Interface to emit optimization remarks.
389 OptimizationRemarkEmitter *ORE;
Karthik Bhat88db86d2015-03-06 10:11:25 +0000390};
391
Vikram TV74b41112015-12-09 05:16:24 +0000392/// LoopInterchangeTransform interchanges the loop.
Karthik Bhat88db86d2015-03-06 10:11:25 +0000393class LoopInterchangeTransform {
394public:
395 LoopInterchangeTransform(Loop *Outer, Loop *Inner, ScalarEvolution *SE,
396 LoopInfo *LI, DominatorTree *DT,
Justin Bogner843fb202015-12-15 19:40:57 +0000397 BasicBlock *LoopNestExit,
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000398 bool InnerLoopContainsReductions)
Karthik Bhat88db86d2015-03-06 10:11:25 +0000399 : OuterLoop(Outer), InnerLoop(Inner), SE(SE), LI(LI), DT(DT),
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000400 LoopExit(LoopNestExit),
401 InnerLoopHasReduction(InnerLoopContainsReductions) {}
Karthik Bhat88db86d2015-03-06 10:11:25 +0000402
403 /// Interchange OuterLoop and InnerLoop.
404 bool transform();
405 void restructureLoops(Loop *InnerLoop, Loop *OuterLoop);
406 void removeChildLoop(Loop *OuterLoop, Loop *InnerLoop);
Karthik Bhat88db86d2015-03-06 10:11:25 +0000407
408private:
409 void splitInnerLoopLatch(Instruction *);
Karthik Bhat88db86d2015-03-06 10:11:25 +0000410 void splitInnerLoopHeader();
411 bool adjustLoopLinks();
412 void adjustLoopPreheaders();
Karthik Bhat88db86d2015-03-06 10:11:25 +0000413 bool adjustLoopBranches();
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000414 void updateIncomingBlock(BasicBlock *CurrBlock, BasicBlock *OldPred,
415 BasicBlock *NewPred);
Karthik Bhat88db86d2015-03-06 10:11:25 +0000416
417 Loop *OuterLoop;
418 Loop *InnerLoop;
419
420 /// Scev analysis.
421 ScalarEvolution *SE;
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000422
Karthik Bhat88db86d2015-03-06 10:11:25 +0000423 LoopInfo *LI;
424 DominatorTree *DT;
425 BasicBlock *LoopExit;
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000426 bool InnerLoopHasReduction;
Karthik Bhat88db86d2015-03-06 10:11:25 +0000427};
428
Vikram TV74b41112015-12-09 05:16:24 +0000429// Main LoopInterchange Pass.
Karthik Bhat88db86d2015-03-06 10:11:25 +0000430struct LoopInterchange : public FunctionPass {
431 static char ID;
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000432 ScalarEvolution *SE = nullptr;
433 LoopInfo *LI = nullptr;
434 DependenceInfo *DI = nullptr;
435 DominatorTree *DT = nullptr;
Justin Bogner843fb202015-12-15 19:40:57 +0000436 bool PreserveLCSSA;
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000437
Florian Hahnad993522017-07-15 13:13:19 +0000438 /// Interface to emit optimization remarks.
439 OptimizationRemarkEmitter *ORE;
440
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000441 LoopInterchange() : FunctionPass(ID) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000442 initializeLoopInterchangePass(*PassRegistry::getPassRegistry());
443 }
444
445 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000446 AU.addRequired<ScalarEvolutionWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000447 AU.addRequired<AAResultsWrapperPass>();
Karthik Bhat88db86d2015-03-06 10:11:25 +0000448 AU.addRequired<DominatorTreeWrapperPass>();
449 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth49c22192016-05-12 22:19:39 +0000450 AU.addRequired<DependenceAnalysisWrapperPass>();
Karthik Bhat88db86d2015-03-06 10:11:25 +0000451 AU.addRequiredID(LoopSimplifyID);
452 AU.addRequiredID(LCSSAID);
Florian Hahnad993522017-07-15 13:13:19 +0000453 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
Florian Hahnc6296fe2018-02-14 13:13:15 +0000454
455 AU.addPreserved<DominatorTreeWrapperPass>();
Karthik Bhat88db86d2015-03-06 10:11:25 +0000456 }
457
458 bool runOnFunction(Function &F) override {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000459 if (skipFunction(F))
460 return false;
461
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000462 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Karthik Bhat88db86d2015-03-06 10:11:25 +0000463 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruth49c22192016-05-12 22:19:39 +0000464 DI = &getAnalysis<DependenceAnalysisWrapperPass>().getDI();
Florian Hahnc6296fe2018-02-14 13:13:15 +0000465 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Florian Hahnad993522017-07-15 13:13:19 +0000466 ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
Justin Bogner843fb202015-12-15 19:40:57 +0000467 PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
468
Karthik Bhat88db86d2015-03-06 10:11:25 +0000469 // Build up a worklist of loop pairs to analyze.
470 SmallVector<LoopVector, 8> Worklist;
471
472 for (Loop *L : *LI)
473 populateWorklist(*L, Worklist);
474
Chad Rosiera4c42462016-09-12 13:24:47 +0000475 DEBUG(dbgs() << "Worklist size = " << Worklist.size() << "\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000476 bool Changed = true;
477 while (!Worklist.empty()) {
478 LoopVector LoopList = Worklist.pop_back_val();
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000479 Changed = processLoopList(LoopList, F);
Karthik Bhat88db86d2015-03-06 10:11:25 +0000480 }
481 return Changed;
482 }
483
484 bool isComputableLoopNest(LoopVector LoopList) {
Benjamin Kramer135f7352016-06-26 12:28:59 +0000485 for (Loop *L : LoopList) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000486 const SCEV *ExitCountOuter = SE->getBackedgeTakenCount(L);
487 if (ExitCountOuter == SE->getCouldNotCompute()) {
Chad Rosierf7c76f92016-09-21 13:28:41 +0000488 DEBUG(dbgs() << "Couldn't compute backedge count\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000489 return false;
490 }
491 if (L->getNumBackEdges() != 1) {
492 DEBUG(dbgs() << "NumBackEdges is not equal to 1\n");
493 return false;
494 }
495 if (!L->getExitingBlock()) {
Chad Rosierf7c76f92016-09-21 13:28:41 +0000496 DEBUG(dbgs() << "Loop doesn't have unique exit block\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000497 return false;
498 }
499 }
500 return true;
501 }
502
Benjamin Kramerc321e532016-06-08 19:09:22 +0000503 unsigned selectLoopForInterchange(const LoopVector &LoopList) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000504 // TODO: Add a better heuristic to select the loop to be interchanged based
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000505 // on the dependence matrix. Currently we select the innermost loop.
Karthik Bhat88db86d2015-03-06 10:11:25 +0000506 return LoopList.size() - 1;
507 }
508
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000509 bool processLoopList(LoopVector LoopList, Function &F) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000510 bool Changed = false;
Chad Rosier7ea0d392016-09-13 13:30:30 +0000511 unsigned LoopNestDepth = LoopList.size();
512 if (LoopNestDepth < 2) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000513 DEBUG(dbgs() << "Loop doesn't contain minimum nesting level.\n");
514 return false;
515 }
Chad Rosier7ea0d392016-09-13 13:30:30 +0000516 if (LoopNestDepth > MaxLoopNestDepth) {
517 DEBUG(dbgs() << "Cannot handle loops of depth greater than "
518 << MaxLoopNestDepth << "\n");
519 return false;
520 }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000521 if (!isComputableLoopNest(LoopList)) {
Chad Rosiera4c42462016-09-12 13:24:47 +0000522 DEBUG(dbgs() << "Not valid loop candidate for interchange\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000523 return false;
524 }
Chad Rosier7ea0d392016-09-13 13:30:30 +0000525
526 DEBUG(dbgs() << "Processing LoopList of size = " << LoopNestDepth << "\n");
527
528 CharMatrix DependencyMatrix;
Karthik Bhat88db86d2015-03-06 10:11:25 +0000529 Loop *OuterMostLoop = *(LoopList.begin());
Chad Rosier7ea0d392016-09-13 13:30:30 +0000530 if (!populateDependencyMatrix(DependencyMatrix, LoopNestDepth,
Chandler Carruth49c22192016-05-12 22:19:39 +0000531 OuterMostLoop, DI)) {
Chad Rosierf7c76f92016-09-21 13:28:41 +0000532 DEBUG(dbgs() << "Populating dependency matrix failed\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000533 return false;
534 }
535#ifdef DUMP_DEP_MATRICIES
Chad Rosier58ede272016-09-14 16:43:19 +0000536 DEBUG(dbgs() << "Dependence before interchange\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000537 printDepMatrix(DependencyMatrix);
538#endif
539
540 BasicBlock *OuterMostLoopLatch = OuterMostLoop->getLoopLatch();
541 BranchInst *OuterMostLoopLatchBI =
542 dyn_cast<BranchInst>(OuterMostLoopLatch->getTerminator());
Florian Hahn1f95ef12018-02-13 10:02:52 +0000543 if (!OuterMostLoopLatchBI || OuterMostLoopLatchBI->getNumSuccessors() != 2)
Karthik Bhat88db86d2015-03-06 10:11:25 +0000544 return false;
545
546 // Since we currently do not handle LCSSA PHI's any failure in loop
547 // condition will now branch to LoopNestExit.
548 // TODO: This should be removed once we handle LCSSA PHI nodes.
549
550 // Get the Outermost loop exit.
551 BasicBlock *LoopNestExit;
552 if (OuterMostLoopLatchBI->getSuccessor(0) == OuterMostLoop->getHeader())
553 LoopNestExit = OuterMostLoopLatchBI->getSuccessor(1);
554 else
555 LoopNestExit = OuterMostLoopLatchBI->getSuccessor(0);
556
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000557 if (isa<PHINode>(LoopNestExit->begin())) {
558 DEBUG(dbgs() << "PHI Nodes in loop nest exit is not handled for now "
559 "since on failure all loops branch to loop nest exit.\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000560 return false;
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000561 }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000562
563 unsigned SelecLoopId = selectLoopForInterchange(LoopList);
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000564 // Move the selected loop outwards to the best possible position.
Karthik Bhat88db86d2015-03-06 10:11:25 +0000565 for (unsigned i = SelecLoopId; i > 0; i--) {
566 bool Interchanged =
567 processLoop(LoopList, i, i - 1, LoopNestExit, DependencyMatrix);
568 if (!Interchanged)
569 return Changed;
570 // Loops interchanged reflect the same in LoopList
Benjamin Kramer79442922015-03-06 18:59:14 +0000571 std::swap(LoopList[i - 1], LoopList[i]);
Karthik Bhat88db86d2015-03-06 10:11:25 +0000572
573 // Update the DependencyMatrix
Chad Rosierd18ea062016-09-13 13:00:29 +0000574 interChangeDependencies(DependencyMatrix, i, i - 1);
Karthik Bhat88db86d2015-03-06 10:11:25 +0000575#ifdef DUMP_DEP_MATRICIES
Chad Rosier58ede272016-09-14 16:43:19 +0000576 DEBUG(dbgs() << "Dependence after interchange\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000577 printDepMatrix(DependencyMatrix);
578#endif
579 Changed |= Interchanged;
580 }
581 return Changed;
582 }
583
584 bool processLoop(LoopVector LoopList, unsigned InnerLoopId,
585 unsigned OuterLoopId, BasicBlock *LoopNestExit,
586 std::vector<std::vector<char>> &DependencyMatrix) {
Chad Rosier13bc0d192016-09-07 18:15:12 +0000587 DEBUG(dbgs() << "Processing Inner Loop Id = " << InnerLoopId
Karthik Bhat88db86d2015-03-06 10:11:25 +0000588 << " and OuterLoopId = " << OuterLoopId << "\n");
589 Loop *InnerLoop = LoopList[InnerLoopId];
590 Loop *OuterLoop = LoopList[OuterLoopId];
591
Justin Bogner843fb202015-12-15 19:40:57 +0000592 LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, LI, DT,
Florian Hahnad993522017-07-15 13:13:19 +0000593 PreserveLCSSA, ORE);
Karthik Bhat88db86d2015-03-06 10:11:25 +0000594 if (!LIL.canInterchangeLoops(InnerLoopId, OuterLoopId, DependencyMatrix)) {
Sebastian Popbf6e1c22018-03-06 21:55:59 +0000595 DEBUG(dbgs() << "Not interchanging loops. Cannot prove legality.\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000596 return false;
597 }
598 DEBUG(dbgs() << "Loops are legal to interchange\n");
Florian Hahnad993522017-07-15 13:13:19 +0000599 LoopInterchangeProfitability LIP(OuterLoop, InnerLoop, SE, ORE);
Karthik Bhat88db86d2015-03-06 10:11:25 +0000600 if (!LIP.isProfitable(InnerLoopId, OuterLoopId, DependencyMatrix)) {
Sebastian Popbf6e1c22018-03-06 21:55:59 +0000601 DEBUG(dbgs() << "Interchanging loops not profitable.\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000602 return false;
603 }
604
Vivek Pandya95906582017-10-11 17:12:59 +0000605 ORE->emit([&]() {
606 return OptimizationRemark(DEBUG_TYPE, "Interchanged",
607 InnerLoop->getStartLoc(),
608 InnerLoop->getHeader())
609 << "Loop interchanged with enclosing loop.";
610 });
Florian Hahnad993522017-07-15 13:13:19 +0000611
Justin Bogner843fb202015-12-15 19:40:57 +0000612 LoopInterchangeTransform LIT(OuterLoop, InnerLoop, SE, LI, DT,
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000613 LoopNestExit, LIL.hasInnerLoopReduction());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000614 LIT.transform();
Sebastian Popbf6e1c22018-03-06 21:55:59 +0000615 DEBUG(dbgs() << "Loops interchanged.\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000616 return true;
617 }
618};
619
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000620} // end anonymous namespace
621
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000622bool LoopInterchangeLegality::areAllUsesReductions(Instruction *Ins, Loop *L) {
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000623 return llvm::none_of(Ins->users(), [=](User *U) -> bool {
David Majnemer0a16c222016-08-11 21:15:00 +0000624 auto *UserIns = dyn_cast<PHINode>(U);
Tyler Nowicki0a913102015-06-16 18:07:34 +0000625 RecurrenceDescriptor RD;
626 return !UserIns || !RecurrenceDescriptor::isReductionPHI(UserIns, L, RD);
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000627 });
628}
Karthik Bhat88db86d2015-03-06 10:11:25 +0000629
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000630bool LoopInterchangeLegality::containsUnsafeInstructionsInHeader(
631 BasicBlock *BB) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000632 for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000633 // Load corresponding to reduction PHI's are safe while concluding if
634 // tightly nested.
635 if (LoadInst *L = dyn_cast<LoadInst>(I)) {
636 if (!areAllUsesReductions(L, InnerLoop))
637 return true;
638 } else if (I->mayHaveSideEffects() || I->mayReadFromMemory())
639 return true;
640 }
641 return false;
642}
643
644bool LoopInterchangeLegality::containsUnsafeInstructionsInLatch(
645 BasicBlock *BB) {
646 for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
647 // Stores corresponding to reductions are safe while concluding if tightly
648 // nested.
649 if (StoreInst *L = dyn_cast<StoreInst>(I)) {
Chad Rosierf7c76f92016-09-21 13:28:41 +0000650 if (!isa<PHINode>(L->getOperand(0)))
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000651 return true;
652 } else if (I->mayHaveSideEffects() || I->mayReadFromMemory())
Karthik Bhat88db86d2015-03-06 10:11:25 +0000653 return true;
654 }
655 return false;
656}
657
658bool LoopInterchangeLegality::tightlyNested(Loop *OuterLoop, Loop *InnerLoop) {
659 BasicBlock *OuterLoopHeader = OuterLoop->getHeader();
660 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
661 BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
662
Chad Rosierf7c76f92016-09-21 13:28:41 +0000663 DEBUG(dbgs() << "Checking if loops are tightly nested\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000664
665 // A perfectly nested loop will not have any branch in between the outer and
666 // inner block i.e. outer header will branch to either inner preheader and
667 // outerloop latch.
Chad Rosierf7c76f92016-09-21 13:28:41 +0000668 BranchInst *OuterLoopHeaderBI =
Karthik Bhat88db86d2015-03-06 10:11:25 +0000669 dyn_cast<BranchInst>(OuterLoopHeader->getTerminator());
Chad Rosierf7c76f92016-09-21 13:28:41 +0000670 if (!OuterLoopHeaderBI)
Karthik Bhat88db86d2015-03-06 10:11:25 +0000671 return false;
Chad Rosierf7c76f92016-09-21 13:28:41 +0000672
Florian Hahnf66efd62017-07-24 11:41:30 +0000673 for (BasicBlock *Succ : OuterLoopHeaderBI->successors())
674 if (Succ != InnerLoopPreHeader && Succ != OuterLoopLatch)
Karthik Bhat88db86d2015-03-06 10:11:25 +0000675 return false;
Karthik Bhat88db86d2015-03-06 10:11:25 +0000676
Chad Rosierf7c76f92016-09-21 13:28:41 +0000677 DEBUG(dbgs() << "Checking instructions in Loop header and Loop latch\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000678 // We do not have any basic block in between now make sure the outer header
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000679 // and outer loop latch doesn't contain any unsafe instructions.
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000680 if (containsUnsafeInstructionsInHeader(OuterLoopHeader) ||
681 containsUnsafeInstructionsInLatch(OuterLoopLatch))
Karthik Bhat88db86d2015-03-06 10:11:25 +0000682 return false;
683
Chad Rosierf7c76f92016-09-21 13:28:41 +0000684 DEBUG(dbgs() << "Loops are perfectly nested\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +0000685 // We have a perfect loop nest.
686 return true;
687}
688
Karthik Bhat88db86d2015-03-06 10:11:25 +0000689bool LoopInterchangeLegality::isLoopStructureUnderstood(
690 PHINode *InnerInduction) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000691 unsigned Num = InnerInduction->getNumOperands();
692 BasicBlock *InnerLoopPreheader = InnerLoop->getLoopPreheader();
693 for (unsigned i = 0; i < Num; ++i) {
694 Value *Val = InnerInduction->getOperand(i);
695 if (isa<Constant>(Val))
696 continue;
697 Instruction *I = dyn_cast<Instruction>(Val);
698 if (!I)
699 return false;
700 // TODO: Handle triangular loops.
701 // e.g. for(int i=0;i<N;i++)
702 // for(int j=i;j<N;j++)
703 unsigned IncomBlockIndx = PHINode::getIncomingValueNumForOperand(i);
704 if (InnerInduction->getIncomingBlock(IncomBlockIndx) ==
705 InnerLoopPreheader &&
706 !OuterLoop->isLoopInvariant(I)) {
707 return false;
708 }
709 }
710 return true;
711}
712
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000713bool LoopInterchangeLegality::findInductionAndReductions(
714 Loop *L, SmallVector<PHINode *, 8> &Inductions,
715 SmallVector<PHINode *, 8> &Reductions) {
716 if (!L->getLoopLatch() || !L->getLoopPredecessor())
717 return false;
718 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
Tyler Nowicki0a913102015-06-16 18:07:34 +0000719 RecurrenceDescriptor RD;
James Molloy1bbf15c2015-08-27 09:53:00 +0000720 InductionDescriptor ID;
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000721 PHINode *PHI = cast<PHINode>(I);
Elena Demikhovsky376a18b2016-07-24 07:24:54 +0000722 if (InductionDescriptor::isInductionPHI(PHI, L, SE, ID))
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000723 Inductions.push_back(PHI);
Tyler Nowicki0a913102015-06-16 18:07:34 +0000724 else if (RecurrenceDescriptor::isReductionPHI(PHI, L, RD))
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000725 Reductions.push_back(PHI);
726 else {
727 DEBUG(
728 dbgs() << "Failed to recognize PHI as an induction or reduction.\n");
729 return false;
730 }
731 }
732 return true;
733}
734
735static bool containsSafePHI(BasicBlock *Block, bool isOuterLoopExitBlock) {
736 for (auto I = Block->begin(); isa<PHINode>(I); ++I) {
737 PHINode *PHI = cast<PHINode>(I);
738 // Reduction lcssa phi will have only 1 incoming block that from loop latch.
739 if (PHI->getNumIncomingValues() > 1)
740 return false;
741 Instruction *Ins = dyn_cast<Instruction>(PHI->getIncomingValue(0));
742 if (!Ins)
743 return false;
744 // Incoming value for lcssa phi's in outer loop exit can only be inner loop
745 // exits lcssa phi else it would not be tightly nested.
746 if (!isa<PHINode>(Ins) && isOuterLoopExitBlock)
747 return false;
748 }
749 return true;
750}
751
752static BasicBlock *getLoopLatchExitBlock(BasicBlock *LatchBlock,
753 BasicBlock *LoopHeader) {
754 if (BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator())) {
Florian Hahnf66efd62017-07-24 11:41:30 +0000755 assert(BI->getNumSuccessors() == 2 &&
756 "Branch leaving loop latch must have 2 successors");
757 for (BasicBlock *Succ : BI->successors()) {
758 if (Succ == LoopHeader)
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000759 continue;
Florian Hahnf66efd62017-07-24 11:41:30 +0000760 return Succ;
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000761 }
762 }
763 return nullptr;
764}
765
Karthik Bhat88db86d2015-03-06 10:11:25 +0000766// This function indicates the current limitations in the transform as a result
767// of which we do not proceed.
768bool LoopInterchangeLegality::currentLimitations() {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000769 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
770 BasicBlock *InnerLoopHeader = InnerLoop->getHeader();
Karthik Bhat88db86d2015-03-06 10:11:25 +0000771 BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
772 BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000773 BasicBlock *OuterLoopHeader = OuterLoop->getHeader();
Karthik Bhat88db86d2015-03-06 10:11:25 +0000774
775 PHINode *InnerInductionVar;
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000776 SmallVector<PHINode *, 8> Inductions;
777 SmallVector<PHINode *, 8> Reductions;
Florian Hahn4eeff392017-07-03 15:32:00 +0000778 if (!findInductionAndReductions(InnerLoop, Inductions, Reductions)) {
779 DEBUG(dbgs() << "Only inner loops with induction or reduction PHI nodes "
780 << "are supported currently.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000781 ORE->emit([&]() {
782 return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedPHIInner",
783 InnerLoop->getStartLoc(),
784 InnerLoop->getHeader())
785 << "Only inner loops with induction or reduction PHI nodes can be"
786 " interchange currently.";
787 });
Karthik Bhat88db86d2015-03-06 10:11:25 +0000788 return true;
Florian Hahn4eeff392017-07-03 15:32:00 +0000789 }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000790
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000791 // TODO: Currently we handle only loops with 1 induction variable.
792 if (Inductions.size() != 1) {
793 DEBUG(dbgs() << "We currently only support loops with 1 induction variable."
794 << "Failed to interchange due to current limitation\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000795 ORE->emit([&]() {
796 return OptimizationRemarkMissed(DEBUG_TYPE, "MultiInductionInner",
797 InnerLoop->getStartLoc(),
798 InnerLoop->getHeader())
799 << "Only inner loops with 1 induction variable can be "
800 "interchanged currently.";
801 });
Karthik Bhat88db86d2015-03-06 10:11:25 +0000802 return true;
803 }
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000804 if (Reductions.size() > 0)
805 InnerLoopHasReduction = true;
806
807 InnerInductionVar = Inductions.pop_back_val();
808 Reductions.clear();
Florian Hahn4eeff392017-07-03 15:32:00 +0000809 if (!findInductionAndReductions(OuterLoop, Inductions, Reductions)) {
810 DEBUG(dbgs() << "Only outer loops with induction or reduction PHI nodes "
811 << "are supported currently.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000812 ORE->emit([&]() {
813 return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedPHIOuter",
814 OuterLoop->getStartLoc(),
815 OuterLoop->getHeader())
816 << "Only outer loops with induction or reduction PHI nodes can be"
817 " interchanged currently.";
818 });
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000819 return true;
Florian Hahn4eeff392017-07-03 15:32:00 +0000820 }
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000821
822 // Outer loop cannot have reduction because then loops will not be tightly
823 // nested.
Florian Hahn4eeff392017-07-03 15:32:00 +0000824 if (!Reductions.empty()) {
825 DEBUG(dbgs() << "Outer loops with reductions are not supported "
826 << "currently.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000827 ORE->emit([&]() {
828 return OptimizationRemarkMissed(DEBUG_TYPE, "ReductionsOuter",
829 OuterLoop->getStartLoc(),
830 OuterLoop->getHeader())
831 << "Outer loops with reductions cannot be interchangeed "
832 "currently.";
833 });
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000834 return true;
Florian Hahn4eeff392017-07-03 15:32:00 +0000835 }
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000836 // TODO: Currently we handle only loops with 1 induction variable.
Florian Hahn4eeff392017-07-03 15:32:00 +0000837 if (Inductions.size() != 1) {
838 DEBUG(dbgs() << "Loops with more than 1 induction variables are not "
839 << "supported currently.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000840 ORE->emit([&]() {
841 return OptimizationRemarkMissed(DEBUG_TYPE, "MultiIndutionOuter",
842 OuterLoop->getStartLoc(),
843 OuterLoop->getHeader())
844 << "Only outer loops with 1 induction variable can be "
845 "interchanged currently.";
846 });
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000847 return true;
Florian Hahn4eeff392017-07-03 15:32:00 +0000848 }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000849
850 // TODO: Triangular loops are not handled for now.
851 if (!isLoopStructureUnderstood(InnerInductionVar)) {
852 DEBUG(dbgs() << "Loop structure not understood by pass\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000853 ORE->emit([&]() {
854 return OptimizationRemarkMissed(DEBUG_TYPE, "UnsupportedStructureInner",
855 InnerLoop->getStartLoc(),
856 InnerLoop->getHeader())
857 << "Inner loop structure not understood currently.";
858 });
Karthik Bhat88db86d2015-03-06 10:11:25 +0000859 return true;
860 }
861
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000862 // TODO: We only handle LCSSA PHI's corresponding to reduction for now.
863 BasicBlock *LoopExitBlock =
864 getLoopLatchExitBlock(OuterLoopLatch, OuterLoopHeader);
Florian Hahn4eeff392017-07-03 15:32:00 +0000865 if (!LoopExitBlock || !containsSafePHI(LoopExitBlock, true)) {
866 DEBUG(dbgs() << "Can only handle LCSSA PHIs in outer loops currently.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000867 ORE->emit([&]() {
868 return OptimizationRemarkMissed(DEBUG_TYPE, "NoLCSSAPHIOuter",
869 OuterLoop->getStartLoc(),
870 OuterLoop->getHeader())
871 << "Only outer loops with LCSSA PHIs can be interchange "
872 "currently.";
873 });
Karthik Bhat88db86d2015-03-06 10:11:25 +0000874 return true;
Florian Hahn4eeff392017-07-03 15:32:00 +0000875 }
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000876
877 LoopExitBlock = getLoopLatchExitBlock(InnerLoopLatch, InnerLoopHeader);
Florian Hahn4eeff392017-07-03 15:32:00 +0000878 if (!LoopExitBlock || !containsSafePHI(LoopExitBlock, false)) {
879 DEBUG(dbgs() << "Can only handle LCSSA PHIs in inner loops currently.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000880 ORE->emit([&]() {
881 return OptimizationRemarkMissed(DEBUG_TYPE, "NoLCSSAPHIOuterInner",
882 InnerLoop->getStartLoc(),
883 InnerLoop->getHeader())
884 << "Only inner loops with LCSSA PHIs can be interchange "
885 "currently.";
886 });
Karthik Bhat88db86d2015-03-06 10:11:25 +0000887 return true;
Florian Hahn4eeff392017-07-03 15:32:00 +0000888 }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000889
890 // TODO: Current limitation: Since we split the inner loop latch at the point
891 // were induction variable is incremented (induction.next); We cannot have
892 // more than 1 user of induction.next since it would result in broken code
893 // after split.
894 // e.g.
895 // for(i=0;i<N;i++) {
896 // for(j = 0;j<M;j++) {
897 // A[j+1][i+2] = A[j][i]+k;
898 // }
899 // }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000900 Instruction *InnerIndexVarInc = nullptr;
901 if (InnerInductionVar->getIncomingBlock(0) == InnerLoopPreHeader)
902 InnerIndexVarInc =
903 dyn_cast<Instruction>(InnerInductionVar->getIncomingValue(1));
904 else
905 InnerIndexVarInc =
906 dyn_cast<Instruction>(InnerInductionVar->getIncomingValue(0));
907
Florian Hahn4eeff392017-07-03 15:32:00 +0000908 if (!InnerIndexVarInc) {
909 DEBUG(dbgs() << "Did not find an instruction to increment the induction "
910 << "variable.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000911 ORE->emit([&]() {
912 return OptimizationRemarkMissed(DEBUG_TYPE, "NoIncrementInInner",
913 InnerLoop->getStartLoc(),
914 InnerLoop->getHeader())
915 << "The inner loop does not increment the induction variable.";
916 });
Pete Cooper11bd9582015-07-27 18:37:58 +0000917 return true;
Florian Hahn4eeff392017-07-03 15:32:00 +0000918 }
Pete Cooper11bd9582015-07-27 18:37:58 +0000919
Karthik Bhat88db86d2015-03-06 10:11:25 +0000920 // Since we split the inner loop latch on this induction variable. Make sure
921 // we do not have any instruction between the induction variable and branch
922 // instruction.
923
David Majnemerd7708772016-06-24 04:05:21 +0000924 bool FoundInduction = false;
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +0000925 for (const Instruction &I : llvm::reverse(*InnerLoopLatch)) {
Florian Hahncd783452017-08-25 16:52:29 +0000926 if (isa<BranchInst>(I) || isa<CmpInst>(I) || isa<TruncInst>(I) ||
927 isa<ZExtInst>(I))
Karthik Bhat88db86d2015-03-06 10:11:25 +0000928 continue;
Florian Hahn4eeff392017-07-03 15:32:00 +0000929
Karthik Bhat88db86d2015-03-06 10:11:25 +0000930 // We found an instruction. If this is not induction variable then it is not
931 // safe to split this loop latch.
Florian Hahn4eeff392017-07-03 15:32:00 +0000932 if (!I.isIdenticalTo(InnerIndexVarInc)) {
933 DEBUG(dbgs() << "Found unsupported instructions between induction "
934 << "variable increment and branch.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000935 ORE->emit([&]() {
936 return OptimizationRemarkMissed(
937 DEBUG_TYPE, "UnsupportedInsBetweenInduction",
938 InnerLoop->getStartLoc(), InnerLoop->getHeader())
939 << "Found unsupported instruction between induction variable "
940 "increment and branch.";
941 });
Karthik Bhat88db86d2015-03-06 10:11:25 +0000942 return true;
Florian Hahn4eeff392017-07-03 15:32:00 +0000943 }
David Majnemerd7708772016-06-24 04:05:21 +0000944
945 FoundInduction = true;
946 break;
Karthik Bhat88db86d2015-03-06 10:11:25 +0000947 }
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000948 // The loop latch ended and we didn't find the induction variable return as
Karthik Bhat88db86d2015-03-06 10:11:25 +0000949 // current limitation.
Florian Hahn4eeff392017-07-03 15:32:00 +0000950 if (!FoundInduction) {
951 DEBUG(dbgs() << "Did not find the induction variable.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000952 ORE->emit([&]() {
953 return OptimizationRemarkMissed(DEBUG_TYPE, "NoIndutionVariable",
954 InnerLoop->getStartLoc(),
955 InnerLoop->getHeader())
956 << "Did not find the induction variable.";
957 });
Karthik Bhat88db86d2015-03-06 10:11:25 +0000958 return true;
Florian Hahn4eeff392017-07-03 15:32:00 +0000959 }
Karthik Bhat88db86d2015-03-06 10:11:25 +0000960 return false;
961}
962
963bool LoopInterchangeLegality::canInterchangeLoops(unsigned InnerLoopId,
964 unsigned OuterLoopId,
965 CharMatrix &DepMatrix) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000966 if (!isLegalToInterChangeLoops(DepMatrix, InnerLoopId, OuterLoopId)) {
967 DEBUG(dbgs() << "Failed interchange InnerLoopId = " << InnerLoopId
Chad Rosier58ede272016-09-14 16:43:19 +0000968 << " and OuterLoopId = " << OuterLoopId
969 << " due to dependence\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000970 ORE->emit([&]() {
971 return OptimizationRemarkMissed(DEBUG_TYPE, "Dependence",
972 InnerLoop->getStartLoc(),
973 InnerLoop->getHeader())
974 << "Cannot interchange loops due to dependences.";
975 });
Karthik Bhat88db86d2015-03-06 10:11:25 +0000976 return false;
977 }
978
Florian Hahn42840492017-07-31 09:00:52 +0000979 // Check if outer and inner loop contain legal instructions only.
980 for (auto *BB : OuterLoop->blocks())
981 for (Instruction &I : *BB)
982 if (CallInst *CI = dyn_cast<CallInst>(&I)) {
983 // readnone functions do not prevent interchanging.
984 if (CI->doesNotReadMemory())
985 continue;
986 DEBUG(dbgs() << "Loops with call instructions cannot be interchanged "
987 << "safely.");
Florian Hahn9467ccf2018-04-03 20:54:04 +0000988 ORE->emit([&]() {
989 return OptimizationRemarkMissed(DEBUG_TYPE, "CallInst",
990 CI->getDebugLoc(),
991 CI->getParent())
992 << "Cannot interchange loops due to call instruction.";
993 });
994
Florian Hahn42840492017-07-31 09:00:52 +0000995 return false;
996 }
997
Karthik Bhat88db86d2015-03-06 10:11:25 +0000998 // Create unique Preheaders if we already do not have one.
999 BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader();
1000 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
1001
1002 // Create a unique outer preheader -
1003 // 1) If OuterLoop preheader is not present.
1004 // 2) If OuterLoop Preheader is same as OuterLoop Header
1005 // 3) If OuterLoop Preheader is same as Header of the previous loop.
1006 // 4) If OuterLoop Preheader is Entry node.
1007 if (!OuterLoopPreHeader || OuterLoopPreHeader == OuterLoop->getHeader() ||
1008 isa<PHINode>(OuterLoopPreHeader->begin()) ||
1009 !OuterLoopPreHeader->getUniquePredecessor()) {
Justin Bogner843fb202015-12-15 19:40:57 +00001010 OuterLoopPreHeader =
1011 InsertPreheaderForLoop(OuterLoop, DT, LI, PreserveLCSSA);
Karthik Bhat88db86d2015-03-06 10:11:25 +00001012 }
1013
1014 if (!InnerLoopPreHeader || InnerLoopPreHeader == InnerLoop->getHeader() ||
1015 InnerLoopPreHeader == OuterLoop->getHeader()) {
Justin Bogner843fb202015-12-15 19:40:57 +00001016 InnerLoopPreHeader =
1017 InsertPreheaderForLoop(InnerLoop, DT, LI, PreserveLCSSA);
Karthik Bhat88db86d2015-03-06 10:11:25 +00001018 }
1019
Karthik Bhat88db86d2015-03-06 10:11:25 +00001020 // TODO: The loops could not be interchanged due to current limitations in the
1021 // transform module.
1022 if (currentLimitations()) {
1023 DEBUG(dbgs() << "Not legal because of current transform limitation\n");
1024 return false;
1025 }
1026
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001027 // Check if the loops are tightly nested.
1028 if (!tightlyNested(OuterLoop, InnerLoop)) {
1029 DEBUG(dbgs() << "Loops not tightly nested\n");
Vivek Pandya95906582017-10-11 17:12:59 +00001030 ORE->emit([&]() {
1031 return OptimizationRemarkMissed(DEBUG_TYPE, "NotTightlyNested",
1032 InnerLoop->getStartLoc(),
1033 InnerLoop->getHeader())
1034 << "Cannot interchange loops because they are not tightly "
1035 "nested.";
1036 });
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001037 return false;
1038 }
1039
Karthik Bhat88db86d2015-03-06 10:11:25 +00001040 return true;
1041}
1042
1043int LoopInterchangeProfitability::getInstrOrderCost() {
1044 unsigned GoodOrder, BadOrder;
1045 BadOrder = GoodOrder = 0;
Florian Hahnf66efd62017-07-24 11:41:30 +00001046 for (BasicBlock *BB : InnerLoop->blocks()) {
1047 for (Instruction &Ins : *BB) {
Karthik Bhat88db86d2015-03-06 10:11:25 +00001048 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&Ins)) {
1049 unsigned NumOp = GEP->getNumOperands();
1050 bool FoundInnerInduction = false;
1051 bool FoundOuterInduction = false;
1052 for (unsigned i = 0; i < NumOp; ++i) {
1053 const SCEV *OperandVal = SE->getSCEV(GEP->getOperand(i));
1054 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(OperandVal);
1055 if (!AR)
1056 continue;
1057
1058 // If we find the inner induction after an outer induction e.g.
1059 // for(int i=0;i<N;i++)
1060 // for(int j=0;j<N;j++)
1061 // A[i][j] = A[i-1][j-1]+k;
1062 // then it is a good order.
1063 if (AR->getLoop() == InnerLoop) {
1064 // We found an InnerLoop induction after OuterLoop induction. It is
1065 // a good order.
1066 FoundInnerInduction = true;
1067 if (FoundOuterInduction) {
1068 GoodOrder++;
1069 break;
1070 }
1071 }
1072 // If we find the outer induction after an inner induction e.g.
1073 // for(int i=0;i<N;i++)
1074 // for(int j=0;j<N;j++)
1075 // A[j][i] = A[j-1][i-1]+k;
1076 // then it is a bad order.
1077 if (AR->getLoop() == OuterLoop) {
1078 // We found an OuterLoop induction after InnerLoop induction. It is
1079 // a bad order.
1080 FoundOuterInduction = true;
1081 if (FoundInnerInduction) {
1082 BadOrder++;
1083 break;
1084 }
1085 }
1086 }
1087 }
1088 }
1089 }
1090 return GoodOrder - BadOrder;
1091}
1092
Chad Rosiere6b3a632016-09-14 17:12:30 +00001093static bool isProfitableForVectorization(unsigned InnerLoopId,
1094 unsigned OuterLoopId,
1095 CharMatrix &DepMatrix) {
Karthik Bhat88db86d2015-03-06 10:11:25 +00001096 // TODO: Improve this heuristic to catch more cases.
1097 // If the inner loop is loop independent or doesn't carry any dependency it is
1098 // profitable to move this to outer position.
Florian Hahnf66efd62017-07-24 11:41:30 +00001099 for (auto &Row : DepMatrix) {
1100 if (Row[InnerLoopId] != 'S' && Row[InnerLoopId] != 'I')
Karthik Bhat88db86d2015-03-06 10:11:25 +00001101 return false;
1102 // TODO: We need to improve this heuristic.
Florian Hahnf66efd62017-07-24 11:41:30 +00001103 if (Row[OuterLoopId] != '=')
Karthik Bhat88db86d2015-03-06 10:11:25 +00001104 return false;
1105 }
1106 // If outer loop has dependence and inner loop is loop independent then it is
1107 // profitable to interchange to enable parallelism.
1108 return true;
1109}
1110
1111bool LoopInterchangeProfitability::isProfitable(unsigned InnerLoopId,
1112 unsigned OuterLoopId,
1113 CharMatrix &DepMatrix) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001114 // TODO: Add better profitability checks.
Karthik Bhat88db86d2015-03-06 10:11:25 +00001115 // e.g
1116 // 1) Construct dependency matrix and move the one with no loop carried dep
1117 // inside to enable vectorization.
1118
1119 // This is rough cost estimation algorithm. It counts the good and bad order
1120 // of induction variables in the instruction and allows reordering if number
1121 // of bad orders is more than good.
Chad Rosier72431892016-09-14 17:07:13 +00001122 int Cost = getInstrOrderCost();
Karthik Bhat88db86d2015-03-06 10:11:25 +00001123 DEBUG(dbgs() << "Cost = " << Cost << "\n");
Chad Rosier72431892016-09-14 17:07:13 +00001124 if (Cost < -LoopInterchangeCostThreshold)
Karthik Bhat88db86d2015-03-06 10:11:25 +00001125 return true;
1126
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001127 // It is not profitable as per current cache profitability model. But check if
Karthik Bhat88db86d2015-03-06 10:11:25 +00001128 // we can move this loop outside to improve parallelism.
Florian Hahnad993522017-07-15 13:13:19 +00001129 if (isProfitableForVectorization(InnerLoopId, OuterLoopId, DepMatrix))
1130 return true;
1131
Vivek Pandya95906582017-10-11 17:12:59 +00001132 ORE->emit([&]() {
1133 return OptimizationRemarkMissed(DEBUG_TYPE, "InterchangeNotProfitable",
1134 InnerLoop->getStartLoc(),
1135 InnerLoop->getHeader())
1136 << "Interchanging loops is too costly (cost="
1137 << ore::NV("Cost", Cost) << ", threshold="
1138 << ore::NV("Threshold", LoopInterchangeCostThreshold)
1139 << ") and it does not improve parallelism.";
1140 });
Florian Hahnad993522017-07-15 13:13:19 +00001141 return false;
Karthik Bhat88db86d2015-03-06 10:11:25 +00001142}
1143
1144void LoopInterchangeTransform::removeChildLoop(Loop *OuterLoop,
1145 Loop *InnerLoop) {
Daniel Jasper6adbd7a2015-03-06 10:39:14 +00001146 for (Loop::iterator I = OuterLoop->begin(), E = OuterLoop->end(); I != E;
1147 ++I) {
Karthik Bhat88db86d2015-03-06 10:11:25 +00001148 if (*I == InnerLoop) {
1149 OuterLoop->removeChildLoop(I);
1150 return;
1151 }
1152 }
Benjamin Kramer8ceb3232015-10-25 22:28:27 +00001153 llvm_unreachable("Couldn't find loop");
Karthik Bhat88db86d2015-03-06 10:11:25 +00001154}
Daniel Jasper6adbd7a2015-03-06 10:39:14 +00001155
Karthik Bhat88db86d2015-03-06 10:11:25 +00001156void LoopInterchangeTransform::restructureLoops(Loop *InnerLoop,
1157 Loop *OuterLoop) {
1158 Loop *OuterLoopParent = OuterLoop->getParentLoop();
1159 if (OuterLoopParent) {
1160 // Remove the loop from its parent loop.
1161 removeChildLoop(OuterLoopParent, OuterLoop);
1162 removeChildLoop(OuterLoop, InnerLoop);
1163 OuterLoopParent->addChildLoop(InnerLoop);
1164 } else {
1165 removeChildLoop(OuterLoop, InnerLoop);
1166 LI->changeTopLevelLoop(OuterLoop, InnerLoop);
1167 }
1168
Andrew Kaylor08c5f1e2015-04-24 17:39:16 +00001169 while (!InnerLoop->empty())
1170 OuterLoop->addChildLoop(InnerLoop->removeChildLoop(InnerLoop->begin()));
Karthik Bhat88db86d2015-03-06 10:11:25 +00001171
1172 InnerLoop->addChildLoop(OuterLoop);
1173}
1174
1175bool LoopInterchangeTransform::transform() {
Karthik Bhat88db86d2015-03-06 10:11:25 +00001176 bool Transformed = false;
1177 Instruction *InnerIndexVar;
1178
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +00001179 if (InnerLoop->getSubLoops().empty()) {
Karthik Bhat88db86d2015-03-06 10:11:25 +00001180 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
1181 DEBUG(dbgs() << "Calling Split Inner Loop\n");
1182 PHINode *InductionPHI = getInductionVariable(InnerLoop, SE);
1183 if (!InductionPHI) {
1184 DEBUG(dbgs() << "Failed to find the point to split loop latch \n");
1185 return false;
1186 }
1187
1188 if (InductionPHI->getIncomingBlock(0) == InnerLoopPreHeader)
1189 InnerIndexVar = dyn_cast<Instruction>(InductionPHI->getIncomingValue(1));
1190 else
1191 InnerIndexVar = dyn_cast<Instruction>(InductionPHI->getIncomingValue(0));
1192
David Green907b60f2017-10-21 13:58:37 +00001193 // Ensure that InductionPHI is the first Phi node as required by
1194 // splitInnerLoopHeader
1195 if (&InductionPHI->getParent()->front() != InductionPHI)
1196 InductionPHI->moveBefore(&InductionPHI->getParent()->front());
1197
Karthik Bhat88db86d2015-03-06 10:11:25 +00001198 // Split at the place were the induction variable is
1199 // incremented/decremented.
1200 // TODO: This splitting logic may not work always. Fix this.
1201 splitInnerLoopLatch(InnerIndexVar);
Chad Rosierf7c76f92016-09-21 13:28:41 +00001202 DEBUG(dbgs() << "splitInnerLoopLatch done\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +00001203
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001204 // Splits the inner loops phi nodes out into a separate basic block.
Karthik Bhat88db86d2015-03-06 10:11:25 +00001205 splitInnerLoopHeader();
Chad Rosierf7c76f92016-09-21 13:28:41 +00001206 DEBUG(dbgs() << "splitInnerLoopHeader done\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +00001207 }
1208
1209 Transformed |= adjustLoopLinks();
1210 if (!Transformed) {
Chad Rosierf7c76f92016-09-21 13:28:41 +00001211 DEBUG(dbgs() << "adjustLoopLinks failed\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +00001212 return false;
1213 }
1214
1215 restructureLoops(InnerLoop, OuterLoop);
1216 return true;
1217}
1218
Benjamin Kramer79442922015-03-06 18:59:14 +00001219void LoopInterchangeTransform::splitInnerLoopLatch(Instruction *Inc) {
Karthik Bhat88db86d2015-03-06 10:11:25 +00001220 BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
Karthik Bhat88db86d2015-03-06 10:11:25 +00001221 BasicBlock *InnerLoopLatchPred = InnerLoopLatch;
Benjamin Kramer79442922015-03-06 18:59:14 +00001222 InnerLoopLatch = SplitBlock(InnerLoopLatchPred, Inc, DT, LI);
Karthik Bhat88db86d2015-03-06 10:11:25 +00001223}
1224
Karthik Bhat88db86d2015-03-06 10:11:25 +00001225void LoopInterchangeTransform::splitInnerLoopHeader() {
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001226 // Split the inner loop header out. Here make sure that the reduction PHI's
1227 // stay in the innerloop body.
Karthik Bhat88db86d2015-03-06 10:11:25 +00001228 BasicBlock *InnerLoopHeader = InnerLoop->getHeader();
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001229 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
Florian Hahne54a20e2018-02-12 11:10:58 +00001230 SplitBlock(InnerLoopHeader, InnerLoopHeader->getFirstNonPHI(), DT, LI);
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001231 if (InnerLoopHasReduction) {
Florian Hahne54a20e2018-02-12 11:10:58 +00001232 // Adjust Reduction PHI's in the block. The induction PHI must be the first
1233 // PHI in InnerLoopHeader for this to work.
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001234 SmallVector<PHINode *, 8> PHIVec;
Florian Hahne54a20e2018-02-12 11:10:58 +00001235 for (auto I = std::next(InnerLoopHeader->begin()); isa<PHINode>(I); ++I) {
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001236 PHINode *PHI = dyn_cast<PHINode>(I);
1237 Value *V = PHI->getIncomingValueForBlock(InnerLoopPreHeader);
1238 PHI->replaceAllUsesWith(V);
1239 PHIVec.push_back((PHI));
1240 }
Benjamin Kramer135f7352016-06-26 12:28:59 +00001241 for (PHINode *P : PHIVec) {
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001242 P->eraseFromParent();
1243 }
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001244 }
Karthik Bhat88db86d2015-03-06 10:11:25 +00001245
1246 DEBUG(dbgs() << "Output of splitInnerLoopHeader InnerLoopHeaderSucc & "
Chad Rosierf7c76f92016-09-21 13:28:41 +00001247 "InnerLoopHeader\n");
Karthik Bhat88db86d2015-03-06 10:11:25 +00001248}
1249
Benjamin Kramer79442922015-03-06 18:59:14 +00001250/// \brief Move all instructions except the terminator from FromBB right before
1251/// InsertBefore
1252static void moveBBContents(BasicBlock *FromBB, Instruction *InsertBefore) {
1253 auto &ToList = InsertBefore->getParent()->getInstList();
1254 auto &FromList = FromBB->getInstList();
1255
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001256 ToList.splice(InsertBefore->getIterator(), FromList, FromList.begin(),
1257 FromBB->getTerminator()->getIterator());
Benjamin Kramer79442922015-03-06 18:59:14 +00001258}
1259
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001260void LoopInterchangeTransform::updateIncomingBlock(BasicBlock *CurrBlock,
1261 BasicBlock *OldPred,
1262 BasicBlock *NewPred) {
1263 for (auto I = CurrBlock->begin(); isa<PHINode>(I); ++I) {
1264 PHINode *PHI = cast<PHINode>(I);
1265 unsigned Num = PHI->getNumIncomingValues();
1266 for (unsigned i = 0; i < Num; ++i) {
1267 if (PHI->getIncomingBlock(i) == OldPred)
1268 PHI->setIncomingBlock(i, NewPred);
1269 }
1270 }
1271}
1272
Florian Hahnc6296fe2018-02-14 13:13:15 +00001273/// \brief Update BI to jump to NewBB instead of OldBB. Records updates to
1274/// the dominator tree in DTUpdates, if DT should be preserved.
1275static void updateSuccessor(BranchInst *BI, BasicBlock *OldBB,
1276 BasicBlock *NewBB,
1277 std::vector<DominatorTree::UpdateType> &DTUpdates) {
1278 assert(llvm::count_if(BI->successors(),
1279 [OldBB](BasicBlock *BB) { return BB == OldBB; }) < 2 &&
1280 "BI must jump to OldBB at most once.");
1281 for (unsigned i = 0, e = BI->getNumSuccessors(); i < e; ++i) {
1282 if (BI->getSuccessor(i) == OldBB) {
1283 BI->setSuccessor(i, NewBB);
1284
1285 DTUpdates.push_back(
1286 {DominatorTree::UpdateKind::Insert, BI->getParent(), NewBB});
1287 DTUpdates.push_back(
1288 {DominatorTree::UpdateKind::Delete, BI->getParent(), OldBB});
1289 break;
1290 }
1291 }
1292}
1293
Karthik Bhat88db86d2015-03-06 10:11:25 +00001294bool LoopInterchangeTransform::adjustLoopBranches() {
Karthik Bhat88db86d2015-03-06 10:11:25 +00001295 DEBUG(dbgs() << "adjustLoopBranches called\n");
Florian Hahnc6296fe2018-02-14 13:13:15 +00001296 std::vector<DominatorTree::UpdateType> DTUpdates;
1297
Karthik Bhat88db86d2015-03-06 10:11:25 +00001298 // Adjust the loop preheader
1299 BasicBlock *InnerLoopHeader = InnerLoop->getHeader();
1300 BasicBlock *OuterLoopHeader = OuterLoop->getHeader();
1301 BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch();
1302 BasicBlock *OuterLoopLatch = OuterLoop->getLoopLatch();
1303 BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader();
1304 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
1305 BasicBlock *OuterLoopPredecessor = OuterLoopPreHeader->getUniquePredecessor();
1306 BasicBlock *InnerLoopLatchPredecessor =
1307 InnerLoopLatch->getUniquePredecessor();
1308 BasicBlock *InnerLoopLatchSuccessor;
1309 BasicBlock *OuterLoopLatchSuccessor;
1310
1311 BranchInst *OuterLoopLatchBI =
1312 dyn_cast<BranchInst>(OuterLoopLatch->getTerminator());
1313 BranchInst *InnerLoopLatchBI =
1314 dyn_cast<BranchInst>(InnerLoopLatch->getTerminator());
1315 BranchInst *OuterLoopHeaderBI =
1316 dyn_cast<BranchInst>(OuterLoopHeader->getTerminator());
1317 BranchInst *InnerLoopHeaderBI =
1318 dyn_cast<BranchInst>(InnerLoopHeader->getTerminator());
1319
1320 if (!OuterLoopPredecessor || !InnerLoopLatchPredecessor ||
1321 !OuterLoopLatchBI || !InnerLoopLatchBI || !OuterLoopHeaderBI ||
1322 !InnerLoopHeaderBI)
1323 return false;
1324
1325 BranchInst *InnerLoopLatchPredecessorBI =
1326 dyn_cast<BranchInst>(InnerLoopLatchPredecessor->getTerminator());
1327 BranchInst *OuterLoopPredecessorBI =
1328 dyn_cast<BranchInst>(OuterLoopPredecessor->getTerminator());
1329
1330 if (!OuterLoopPredecessorBI || !InnerLoopLatchPredecessorBI)
1331 return false;
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001332 BasicBlock *InnerLoopHeaderSuccessor = InnerLoopHeader->getUniqueSuccessor();
1333 if (!InnerLoopHeaderSuccessor)
Karthik Bhat88db86d2015-03-06 10:11:25 +00001334 return false;
1335
1336 // Adjust Loop Preheader and headers
Florian Hahnc6296fe2018-02-14 13:13:15 +00001337 updateSuccessor(OuterLoopPredecessorBI, OuterLoopPreHeader,
1338 InnerLoopPreHeader, DTUpdates);
1339 updateSuccessor(OuterLoopHeaderBI, OuterLoopLatch, LoopExit, DTUpdates);
1340 updateSuccessor(OuterLoopHeaderBI, InnerLoopPreHeader,
1341 InnerLoopHeaderSuccessor, DTUpdates);
Karthik Bhat88db86d2015-03-06 10:11:25 +00001342
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001343 // Adjust reduction PHI's now that the incoming block has changed.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00001344 updateIncomingBlock(InnerLoopHeaderSuccessor, InnerLoopHeader,
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001345 OuterLoopHeader);
1346
Florian Hahnc6296fe2018-02-14 13:13:15 +00001347 updateSuccessor(InnerLoopHeaderBI, InnerLoopHeaderSuccessor,
1348 OuterLoopPreHeader, DTUpdates);
Karthik Bhat88db86d2015-03-06 10:11:25 +00001349
1350 // -------------Adjust loop latches-----------
1351 if (InnerLoopLatchBI->getSuccessor(0) == InnerLoopHeader)
1352 InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(1);
1353 else
1354 InnerLoopLatchSuccessor = InnerLoopLatchBI->getSuccessor(0);
1355
Florian Hahnc6296fe2018-02-14 13:13:15 +00001356 updateSuccessor(InnerLoopLatchPredecessorBI, InnerLoopLatch,
1357 InnerLoopLatchSuccessor, DTUpdates);
Karthik Bhat88db86d2015-03-06 10:11:25 +00001358
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001359 // Adjust PHI nodes in InnerLoopLatchSuccessor. Update all uses of PHI with
1360 // the value and remove this PHI node from inner loop.
1361 SmallVector<PHINode *, 8> LcssaVec;
1362 for (auto I = InnerLoopLatchSuccessor->begin(); isa<PHINode>(I); ++I) {
1363 PHINode *LcssaPhi = cast<PHINode>(I);
1364 LcssaVec.push_back(LcssaPhi);
1365 }
Benjamin Kramer135f7352016-06-26 12:28:59 +00001366 for (PHINode *P : LcssaVec) {
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001367 Value *Incoming = P->getIncomingValueForBlock(InnerLoopLatch);
1368 P->replaceAllUsesWith(Incoming);
1369 P->eraseFromParent();
1370 }
1371
Karthik Bhat88db86d2015-03-06 10:11:25 +00001372 if (OuterLoopLatchBI->getSuccessor(0) == OuterLoopHeader)
1373 OuterLoopLatchSuccessor = OuterLoopLatchBI->getSuccessor(1);
1374 else
1375 OuterLoopLatchSuccessor = OuterLoopLatchBI->getSuccessor(0);
1376
Florian Hahnc6296fe2018-02-14 13:13:15 +00001377 updateSuccessor(InnerLoopLatchBI, InnerLoopLatchSuccessor,
1378 OuterLoopLatchSuccessor, DTUpdates);
1379 updateSuccessor(OuterLoopLatchBI, OuterLoopLatchSuccessor, InnerLoopLatch,
1380 DTUpdates);
Karthik Bhat88db86d2015-03-06 10:11:25 +00001381
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001382 updateIncomingBlock(OuterLoopLatchSuccessor, OuterLoopLatch, InnerLoopLatch);
1383
Florian Hahnc6296fe2018-02-14 13:13:15 +00001384 DT->applyUpdates(DTUpdates);
Karthik Bhat88db86d2015-03-06 10:11:25 +00001385 return true;
1386}
Karthik Bhat88db86d2015-03-06 10:11:25 +00001387
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +00001388void LoopInterchangeTransform::adjustLoopPreheaders() {
Karthik Bhat88db86d2015-03-06 10:11:25 +00001389 // We have interchanged the preheaders so we need to interchange the data in
1390 // the preheader as well.
1391 // This is because the content of inner preheader was previously executed
1392 // inside the outer loop.
1393 BasicBlock *OuterLoopPreHeader = OuterLoop->getLoopPreheader();
1394 BasicBlock *InnerLoopPreHeader = InnerLoop->getLoopPreheader();
1395 BasicBlock *OuterLoopHeader = OuterLoop->getHeader();
1396 BranchInst *InnerTermBI =
1397 cast<BranchInst>(InnerLoopPreHeader->getTerminator());
1398
Karthik Bhat88db86d2015-03-06 10:11:25 +00001399 // These instructions should now be executed inside the loop.
1400 // Move instruction into a new block after outer header.
Karthik Bhat8210fdf2015-04-23 04:51:44 +00001401 moveBBContents(InnerLoopPreHeader, OuterLoopHeader->getTerminator());
Karthik Bhat88db86d2015-03-06 10:11:25 +00001402 // These instructions were not executed previously in the loop so move them to
1403 // the older inner loop preheader.
Benjamin Kramer79442922015-03-06 18:59:14 +00001404 moveBBContents(OuterLoopPreHeader, InnerTermBI);
Karthik Bhat88db86d2015-03-06 10:11:25 +00001405}
1406
1407bool LoopInterchangeTransform::adjustLoopLinks() {
Karthik Bhat88db86d2015-03-06 10:11:25 +00001408 // Adjust all branches in the inner and outer loop.
1409 bool Changed = adjustLoopBranches();
1410 if (Changed)
1411 adjustLoopPreheaders();
1412 return Changed;
1413}
1414
1415char LoopInterchange::ID = 0;
Eugene Zelenkodd40f5e2017-10-16 21:34:24 +00001416
Karthik Bhat88db86d2015-03-06 10:11:25 +00001417INITIALIZE_PASS_BEGIN(LoopInterchange, "loop-interchange",
1418 "Interchanges loops for cache reuse", false, false)
Chandler Carruth7b560d42015-09-09 17:55:00 +00001419INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Chandler Carruth49c22192016-05-12 22:19:39 +00001420INITIALIZE_PASS_DEPENDENCY(DependenceAnalysisWrapperPass)
Karthik Bhat88db86d2015-03-06 10:11:25 +00001421INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +00001422INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Karthik Bhat88db86d2015-03-06 10:11:25 +00001423INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
Easwaran Ramane12c4872016-06-09 19:44:46 +00001424INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
Karthik Bhat88db86d2015-03-06 10:11:25 +00001425INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Florian Hahnad993522017-07-15 13:13:19 +00001426INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
Karthik Bhat88db86d2015-03-06 10:11:25 +00001427
1428INITIALIZE_PASS_END(LoopInterchange, "loop-interchange",
1429 "Interchanges loops for cache reuse", false, false)
1430
1431Pass *llvm::createLoopInterchangePass() { return new LoopInterchange(); }