blob: 7f73baa6a8ffed24ae64f2a27a5db21bca6201b1 [file] [log] [blame]
Hal Finkelbf45efd2013-11-16 23:59:05 +00001//===-- LoopReroll.cpp - Loop rerolling pass ------------------------------===//
2//
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 implements a simple loop reroller.
11//
12//===----------------------------------------------------------------------===//
13
Hal Finkelbf45efd2013-11-16 23:59:05 +000014#include "llvm/Transforms/Scalar.h"
James Molloy64419d42015-01-29 21:52:03 +000015#include "llvm/ADT/MapVector.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "llvm/ADT/STLExtras.h"
James Molloy64419d42015-01-29 21:52:03 +000017#include "llvm/ADT/SmallBitVector.h"
Hal Finkelbf45efd2013-11-16 23:59:05 +000018#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/Statistic.h"
Hal Finkelbf45efd2013-11-16 23:59:05 +000020#include "llvm/Analysis/AliasAnalysis.h"
21#include "llvm/Analysis/AliasSetTracker.h"
22#include "llvm/Analysis/LoopPass.h"
23#include "llvm/Analysis/ScalarEvolution.h"
24#include "llvm/Analysis/ScalarEvolutionExpander.h"
25#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000026#include "llvm/Analysis/TargetLibraryInfo.h"
Hal Finkelbf45efd2013-11-16 23:59:05 +000027#include "llvm/Analysis/ValueTracking.h"
28#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000029#include "llvm/IR/Dominators.h"
Hal Finkelbf45efd2013-11-16 23:59:05 +000030#include "llvm/IR/IntrinsicInst.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/raw_ostream.h"
Hal Finkelbf45efd2013-11-16 23:59:05 +000034#include "llvm/Transforms/Utils/BasicBlockUtils.h"
35#include "llvm/Transforms/Utils/Local.h"
36#include "llvm/Transforms/Utils/LoopUtils.h"
37
38using namespace llvm;
39
Chandler Carruth964daaa2014-04-22 02:55:47 +000040#define DEBUG_TYPE "loop-reroll"
41
Hal Finkelbf45efd2013-11-16 23:59:05 +000042STATISTIC(NumRerolledLoops, "Number of rerolled loops");
43
44static cl::opt<unsigned>
45MaxInc("max-reroll-increment", cl::init(2048), cl::Hidden,
46 cl::desc("The maximum increment for loop rerolling"));
47
James Molloye805ad92015-02-12 15:54:14 +000048static cl::opt<unsigned>
49NumToleratedFailedMatches("reroll-num-tolerated-failed-matches", cl::init(400),
50 cl::Hidden,
51 cl::desc("The maximum number of failures to tolerate"
52 " during fuzzy matching. (default: 400)"));
53
Hal Finkelbf45efd2013-11-16 23:59:05 +000054// This loop re-rolling transformation aims to transform loops like this:
55//
56// int foo(int a);
57// void bar(int *x) {
58// for (int i = 0; i < 500; i += 3) {
59// foo(i);
60// foo(i+1);
61// foo(i+2);
62// }
63// }
64//
65// into a loop like this:
66//
67// void bar(int *x) {
68// for (int i = 0; i < 500; ++i)
69// foo(i);
70// }
71//
72// It does this by looking for loops that, besides the latch code, are composed
73// of isomorphic DAGs of instructions, with each DAG rooted at some increment
74// to the induction variable, and where each DAG is isomorphic to the DAG
75// rooted at the induction variable (excepting the sub-DAGs which root the
76// other induction-variable increments). In other words, we're looking for loop
77// bodies of the form:
78//
79// %iv = phi [ (preheader, ...), (body, %iv.next) ]
80// f(%iv)
81// %iv.1 = add %iv, 1 <-- a root increment
82// f(%iv.1)
83// %iv.2 = add %iv, 2 <-- a root increment
84// f(%iv.2)
85// %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
86// f(%iv.scale_m_1)
87// ...
88// %iv.next = add %iv, scale
89// %cmp = icmp(%iv, ...)
90// br %cmp, header, exit
91//
92// where each f(i) is a set of instructions that, collectively, are a function
93// only of i (and other loop-invariant values).
94//
95// As a special case, we can also reroll loops like this:
96//
97// int foo(int);
98// void bar(int *x) {
99// for (int i = 0; i < 500; ++i) {
100// x[3*i] = foo(0);
101// x[3*i+1] = foo(0);
102// x[3*i+2] = foo(0);
103// }
104// }
105//
106// into this:
107//
108// void bar(int *x) {
109// for (int i = 0; i < 1500; ++i)
110// x[i] = foo(0);
111// }
112//
113// in which case, we're looking for inputs like this:
114//
115// %iv = phi [ (preheader, ...), (body, %iv.next) ]
116// %scaled.iv = mul %iv, scale
117// f(%scaled.iv)
118// %scaled.iv.1 = add %scaled.iv, 1
119// f(%scaled.iv.1)
120// %scaled.iv.2 = add %scaled.iv, 2
121// f(%scaled.iv.2)
122// %scaled.iv.scale_m_1 = add %scaled.iv, scale-1
123// f(%scaled.iv.scale_m_1)
124// ...
125// %iv.next = add %iv, 1
126// %cmp = icmp(%iv, ...)
127// br %cmp, header, exit
128
129namespace {
James Molloy64419d42015-01-29 21:52:03 +0000130 enum IterationLimits {
131 /// The maximum number of iterations that we'll try and reroll. This
132 /// has to be less than 25 in order to fit into a SmallBitVector.
133 IL_MaxRerollIterations = 16,
134 /// The bitvector index used by loop induction variables and other
James Molloyf1473592015-02-11 09:19:47 +0000135 /// instructions that belong to all iterations.
136 IL_All,
James Molloy64419d42015-01-29 21:52:03 +0000137 IL_End
138 };
139
Hal Finkelbf45efd2013-11-16 23:59:05 +0000140 class LoopReroll : public LoopPass {
141 public:
142 static char ID; // Pass ID, replacement for typeid
143 LoopReroll() : LoopPass(ID) {
144 initializeLoopRerollPass(*PassRegistry::getPassRegistry());
145 }
146
Craig Topper3e4c6972014-03-05 09:10:37 +0000147 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
Hal Finkelbf45efd2013-11-16 23:59:05 +0000148
Craig Topper3e4c6972014-03-05 09:10:37 +0000149 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000150 AU.addRequired<AAResultsWrapperPass>();
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000151 AU.addRequired<LoopInfoWrapperPass>();
152 AU.addPreserved<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000153 AU.addRequired<DominatorTreeWrapperPass>();
154 AU.addPreserved<DominatorTreeWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000155 AU.addRequired<ScalarEvolutionWrapperPass>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000156 AU.addRequired<TargetLibraryInfoWrapperPass>();
Hal Finkelbf45efd2013-11-16 23:59:05 +0000157 }
158
James Molloy64419d42015-01-29 21:52:03 +0000159 protected:
Hal Finkelbf45efd2013-11-16 23:59:05 +0000160 AliasAnalysis *AA;
161 LoopInfo *LI;
162 ScalarEvolution *SE;
Hal Finkelbf45efd2013-11-16 23:59:05 +0000163 TargetLibraryInfo *TLI;
164 DominatorTree *DT;
165
166 typedef SmallVector<Instruction *, 16> SmallInstructionVector;
167 typedef SmallSet<Instruction *, 16> SmallInstructionSet;
168
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000169 // Map between induction variable and its increment
170 DenseMap<Instruction *, int64_t> IVToIncMap;
171
172 // A chain of isomorphic instructions, identified by a single-use PHI
Hal Finkelbf45efd2013-11-16 23:59:05 +0000173 // representing a reduction. Only the last value may be used outside the
174 // loop.
175 struct SimpleLoopReduction {
176 SimpleLoopReduction(Instruction *P, Loop *L)
177 : Valid(false), Instructions(1, P) {
178 assert(isa<PHINode>(P) && "First reduction instruction must be a PHI");
179 add(L);
180 }
181
182 bool valid() const {
183 return Valid;
184 }
185
186 Instruction *getPHI() const {
187 assert(Valid && "Using invalid reduction");
188 return Instructions.front();
189 }
190
191 Instruction *getReducedValue() const {
192 assert(Valid && "Using invalid reduction");
193 return Instructions.back();
194 }
195
196 Instruction *get(size_t i) const {
197 assert(Valid && "Using invalid reduction");
198 return Instructions[i+1];
199 }
200
201 Instruction *operator [] (size_t i) const { return get(i); }
202
203 // The size, ignoring the initial PHI.
204 size_t size() const {
205 assert(Valid && "Using invalid reduction");
206 return Instructions.size()-1;
207 }
208
209 typedef SmallInstructionVector::iterator iterator;
210 typedef SmallInstructionVector::const_iterator const_iterator;
211
212 iterator begin() {
213 assert(Valid && "Using invalid reduction");
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000214 return std::next(Instructions.begin());
Hal Finkelbf45efd2013-11-16 23:59:05 +0000215 }
216
217 const_iterator begin() const {
218 assert(Valid && "Using invalid reduction");
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000219 return std::next(Instructions.begin());
Hal Finkelbf45efd2013-11-16 23:59:05 +0000220 }
221
222 iterator end() { return Instructions.end(); }
223 const_iterator end() const { return Instructions.end(); }
224
225 protected:
226 bool Valid;
227 SmallInstructionVector Instructions;
228
229 void add(Loop *L);
230 };
231
232 // The set of all reductions, and state tracking of possible reductions
233 // during loop instruction processing.
234 struct ReductionTracker {
235 typedef SmallVector<SimpleLoopReduction, 16> SmallReductionVector;
236
237 // Add a new possible reduction.
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +0000238 void addSLR(SimpleLoopReduction &SLR) { PossibleReds.push_back(SLR); }
Hal Finkelbf45efd2013-11-16 23:59:05 +0000239
240 // Setup to track possible reductions corresponding to the provided
241 // rerolling scale. Only reductions with a number of non-PHI instructions
242 // that is divisible by the scale are considered. Three instructions sets
243 // are filled in:
244 // - A set of all possible instructions in eligible reductions.
245 // - A set of all PHIs in eligible reductions
NAKAMURA Takumid0e13af2014-10-28 11:54:52 +0000246 // - A set of all reduced values (last instructions) in eligible
247 // reductions.
Hal Finkelbf45efd2013-11-16 23:59:05 +0000248 void restrictToScale(uint64_t Scale,
249 SmallInstructionSet &PossibleRedSet,
250 SmallInstructionSet &PossibleRedPHISet,
251 SmallInstructionSet &PossibleRedLastSet) {
252 PossibleRedIdx.clear();
253 PossibleRedIter.clear();
254 Reds.clear();
255
256 for (unsigned i = 0, e = PossibleReds.size(); i != e; ++i)
257 if (PossibleReds[i].size() % Scale == 0) {
258 PossibleRedLastSet.insert(PossibleReds[i].getReducedValue());
259 PossibleRedPHISet.insert(PossibleReds[i].getPHI());
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000260
Hal Finkelbf45efd2013-11-16 23:59:05 +0000261 PossibleRedSet.insert(PossibleReds[i].getPHI());
262 PossibleRedIdx[PossibleReds[i].getPHI()] = i;
NAKAMURA Takumi5af50a52014-10-28 11:54:05 +0000263 for (Instruction *J : PossibleReds[i]) {
264 PossibleRedSet.insert(J);
265 PossibleRedIdx[J] = i;
Hal Finkelbf45efd2013-11-16 23:59:05 +0000266 }
267 }
268 }
269
270 // The functions below are used while processing the loop instructions.
271
272 // Are the two instructions both from reductions, and furthermore, from
273 // the same reduction?
274 bool isPairInSame(Instruction *J1, Instruction *J2) {
275 DenseMap<Instruction *, int>::iterator J1I = PossibleRedIdx.find(J1);
276 if (J1I != PossibleRedIdx.end()) {
277 DenseMap<Instruction *, int>::iterator J2I = PossibleRedIdx.find(J2);
278 if (J2I != PossibleRedIdx.end() && J1I->second == J2I->second)
279 return true;
280 }
281
282 return false;
283 }
284
285 // The two provided instructions, the first from the base iteration, and
286 // the second from iteration i, form a matched pair. If these are part of
287 // a reduction, record that fact.
288 void recordPair(Instruction *J1, Instruction *J2, unsigned i) {
289 if (PossibleRedIdx.count(J1)) {
290 assert(PossibleRedIdx.count(J2) &&
291 "Recording reduction vs. non-reduction instruction?");
292
293 PossibleRedIter[J1] = 0;
294 PossibleRedIter[J2] = i;
295
296 int Idx = PossibleRedIdx[J1];
297 assert(Idx == PossibleRedIdx[J2] &&
298 "Recording pair from different reductions?");
Hal Finkel67107ea2013-11-17 01:21:54 +0000299 Reds.insert(Idx);
Hal Finkelbf45efd2013-11-16 23:59:05 +0000300 }
301 }
302
303 // The functions below can be called after we've finished processing all
304 // instructions in the loop, and we know which reductions were selected.
305
Hal Finkelbf45efd2013-11-16 23:59:05 +0000306 bool validateSelected();
307 void replaceSelected();
308
309 protected:
310 // The vector of all possible reductions (for any scale).
311 SmallReductionVector PossibleReds;
312
313 DenseMap<Instruction *, int> PossibleRedIdx;
314 DenseMap<Instruction *, int> PossibleRedIter;
315 DenseSet<int> Reds;
316 };
317
James Molloyf1473592015-02-11 09:19:47 +0000318 // A DAGRootSet models an induction variable being used in a rerollable
319 // loop. For example,
320 //
321 // x[i*3+0] = y1
322 // x[i*3+1] = y2
323 // x[i*3+2] = y3
324 //
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000325 // Base instruction -> i*3
James Molloyf1473592015-02-11 09:19:47 +0000326 // +---+----+
327 // / | \
328 // ST[y1] +1 +2 <-- Roots
329 // | |
330 // ST[y2] ST[y3]
331 //
332 // There may be multiple DAGRoots, for example:
333 //
334 // x[i*2+0] = ... (1)
335 // x[i*2+1] = ... (1)
336 // x[i*2+4] = ... (2)
337 // x[i*2+5] = ... (2)
338 // x[(i+1234)*2+5678] = ... (3)
339 // x[(i+1234)*2+5679] = ... (3)
340 //
341 // The loop will be rerolled by adding a new loop induction variable,
342 // one for the Base instruction in each DAGRootSet.
343 //
344 struct DAGRootSet {
345 Instruction *BaseInst;
346 SmallInstructionVector Roots;
347 // The instructions between IV and BaseInst (but not including BaseInst).
348 SmallInstructionSet SubsumedInsts;
349 };
350
James Molloy5f255eb2015-01-29 13:48:05 +0000351 // The set of all DAG roots, and state tracking of all roots
352 // for a particular induction variable.
353 struct DAGRootTracker {
354 DAGRootTracker(LoopReroll *Parent, Loop *L, Instruction *IV,
355 ScalarEvolution *SE, AliasAnalysis *AA,
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000356 TargetLibraryInfo *TLI,
357 DenseMap<Instruction *, int64_t> &IncrMap)
358 : Parent(Parent), L(L), SE(SE), AA(AA), TLI(TLI), IV(IV),
359 IVToIncMap(IncrMap) {}
James Molloy5f255eb2015-01-29 13:48:05 +0000360
361 /// Stage 1: Find all the DAG roots for the induction variable.
362 bool findRoots();
363 /// Stage 2: Validate if the found roots are valid.
364 bool validate(ReductionTracker &Reductions);
365 /// Stage 3: Assuming validate() returned true, perform the
366 /// replacement.
367 /// @param IterCount The maximum iteration count of L.
368 void replace(const SCEV *IterCount);
369
370 protected:
James Molloy64419d42015-01-29 21:52:03 +0000371 typedef MapVector<Instruction*, SmallBitVector> UsesTy;
372
James Molloyf1473592015-02-11 09:19:47 +0000373 bool findRootsRecursive(Instruction *IVU,
374 SmallInstructionSet SubsumedInsts);
375 bool findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts);
376 bool collectPossibleRoots(Instruction *Base,
377 std::map<int64_t,Instruction*> &Roots);
James Molloy5f255eb2015-01-29 13:48:05 +0000378
James Molloy64419d42015-01-29 21:52:03 +0000379 bool collectUsedInstructions(SmallInstructionSet &PossibleRedSet);
James Molloy5f255eb2015-01-29 13:48:05 +0000380 void collectInLoopUserSet(const SmallInstructionVector &Roots,
381 const SmallInstructionSet &Exclude,
382 const SmallInstructionSet &Final,
383 DenseSet<Instruction *> &Users);
384 void collectInLoopUserSet(Instruction *Root,
385 const SmallInstructionSet &Exclude,
386 const SmallInstructionSet &Final,
387 DenseSet<Instruction *> &Users);
388
James Molloye805ad92015-02-12 15:54:14 +0000389 UsesTy::iterator nextInstr(int Val, UsesTy &In,
390 const SmallInstructionSet &Exclude,
391 UsesTy::iterator *StartI=nullptr);
James Molloyf1473592015-02-11 09:19:47 +0000392 bool isBaseInst(Instruction *I);
393 bool isRootInst(Instruction *I);
James Molloye805ad92015-02-12 15:54:14 +0000394 bool instrDependsOn(Instruction *I,
395 UsesTy::iterator Start,
396 UsesTy::iterator End);
James Molloy64419d42015-01-29 21:52:03 +0000397
James Molloy5f255eb2015-01-29 13:48:05 +0000398 LoopReroll *Parent;
399
400 // Members of Parent, replicated here for brevity.
401 Loop *L;
402 ScalarEvolution *SE;
403 AliasAnalysis *AA;
404 TargetLibraryInfo *TLI;
James Molloy5f255eb2015-01-29 13:48:05 +0000405
406 // The loop induction variable.
407 Instruction *IV;
408 // Loop step amount.
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000409 int64_t Inc;
James Molloy5f255eb2015-01-29 13:48:05 +0000410 // Loop reroll count; if Inc == 1, this records the scaling applied
411 // to the indvar: a[i*2+0] = ...; a[i*2+1] = ... ;
412 // If Inc is not 1, Scale = Inc.
413 uint64_t Scale;
James Molloy5f255eb2015-01-29 13:48:05 +0000414 // The roots themselves.
James Molloyf1473592015-02-11 09:19:47 +0000415 SmallVector<DAGRootSet,16> RootSets;
James Molloy5f255eb2015-01-29 13:48:05 +0000416 // All increment instructions for IV.
417 SmallInstructionVector LoopIncs;
James Molloy64419d42015-01-29 21:52:03 +0000418 // Map of all instructions in the loop (in order) to the iterations
James Molloyf1473592015-02-11 09:19:47 +0000419 // they are used in (or specially, IL_All for instructions
James Molloy64419d42015-01-29 21:52:03 +0000420 // used in the loop increment mechanism).
421 UsesTy Uses;
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000422 // Map between induction variable and its increment
423 DenseMap<Instruction *, int64_t> &IVToIncMap;
James Molloy5f255eb2015-01-29 13:48:05 +0000424 };
425
Hal Finkelbf45efd2013-11-16 23:59:05 +0000426 void collectPossibleIVs(Loop *L, SmallInstructionVector &PossibleIVs);
427 void collectPossibleReductions(Loop *L,
428 ReductionTracker &Reductions);
Hal Finkelbf45efd2013-11-16 23:59:05 +0000429 bool reroll(Instruction *IV, Loop *L, BasicBlock *Header, const SCEV *IterCount,
430 ReductionTracker &Reductions);
431 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000432}
Hal Finkelbf45efd2013-11-16 23:59:05 +0000433
434char LoopReroll::ID = 0;
435INITIALIZE_PASS_BEGIN(LoopReroll, "loop-reroll", "Reroll loops", false, false)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000436INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000437INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Chandler Carruth73523022014-01-13 13:07:17 +0000438INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000439INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000440INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000441INITIALIZE_PASS_END(LoopReroll, "loop-reroll", "Reroll loops", false, false)
442
443Pass *llvm::createLoopRerollPass() {
444 return new LoopReroll;
445}
446
447// Returns true if the provided instruction is used outside the given loop.
448// This operates like Instruction::isUsedOutsideOfBlock, but considers PHIs in
449// non-loop blocks to be outside the loop.
450static bool hasUsesOutsideLoop(Instruction *I, Loop *L) {
James Molloy64419d42015-01-29 21:52:03 +0000451 for (User *U : I->users()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000452 if (!L->contains(cast<Instruction>(U)))
Hal Finkelbf45efd2013-11-16 23:59:05 +0000453 return true;
James Molloy64419d42015-01-29 21:52:03 +0000454 }
Hal Finkelbf45efd2013-11-16 23:59:05 +0000455 return false;
456}
457
458// Collect the list of loop induction variables with respect to which it might
459// be possible to reroll the loop.
460void LoopReroll::collectPossibleIVs(Loop *L,
461 SmallInstructionVector &PossibleIVs) {
462 BasicBlock *Header = L->getHeader();
463 for (BasicBlock::iterator I = Header->begin(),
464 IE = Header->getFirstInsertionPt(); I != IE; ++I) {
465 if (!isa<PHINode>(I))
466 continue;
467 if (!I->getType()->isIntegerTy())
468 continue;
469
470 if (const SCEVAddRecExpr *PHISCEV =
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000471 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(&*I))) {
Hal Finkelbf45efd2013-11-16 23:59:05 +0000472 if (PHISCEV->getLoop() != L)
473 continue;
474 if (!PHISCEV->isAffine())
475 continue;
476 if (const SCEVConstant *IncSCEV =
477 dyn_cast<SCEVConstant>(PHISCEV->getStepRecurrence(*SE))) {
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000478 const APInt &AInt = IncSCEV->getValue()->getValue().abs();
479 if (IncSCEV->getValue()->isZero() || AInt.uge(MaxInc))
Hal Finkelbf45efd2013-11-16 23:59:05 +0000480 continue;
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000481 IVToIncMap[&*I] = IncSCEV->getValue()->getSExtValue();
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000482 DEBUG(dbgs() << "LRR: Possible IV: " << *I << " = " << *PHISCEV
483 << "\n");
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000484 PossibleIVs.push_back(&*I);
Hal Finkelbf45efd2013-11-16 23:59:05 +0000485 }
486 }
487 }
488}
489
490// Add the remainder of the reduction-variable chain to the instruction vector
491// (the initial PHINode has already been added). If successful, the object is
492// marked as valid.
493void LoopReroll::SimpleLoopReduction::add(Loop *L) {
494 assert(!Valid && "Cannot add to an already-valid chain");
495
496 // The reduction variable must be a chain of single-use instructions
497 // (including the PHI), except for the last value (which is used by the PHI
498 // and also outside the loop).
499 Instruction *C = Instructions.front();
James Molloy4c7deb22015-02-16 17:01:52 +0000500 if (C->user_empty())
501 return;
Hal Finkelbf45efd2013-11-16 23:59:05 +0000502
503 do {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000504 C = cast<Instruction>(*C->user_begin());
Hal Finkelbf45efd2013-11-16 23:59:05 +0000505 if (C->hasOneUse()) {
506 if (!C->isBinaryOp())
507 return;
508
509 if (!(isa<PHINode>(Instructions.back()) ||
510 C->isSameOperationAs(Instructions.back())))
511 return;
512
513 Instructions.push_back(C);
514 }
515 } while (C->hasOneUse());
516
517 if (Instructions.size() < 2 ||
518 !C->isSameOperationAs(Instructions.back()) ||
Chandler Carruthcdf47882014-03-09 03:16:01 +0000519 C->use_empty())
Hal Finkelbf45efd2013-11-16 23:59:05 +0000520 return;
521
522 // C is now the (potential) last instruction in the reduction chain.
James Molloy64419d42015-01-29 21:52:03 +0000523 for (User *U : C->users()) {
Hal Finkelbf45efd2013-11-16 23:59:05 +0000524 // The only in-loop user can be the initial PHI.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000525 if (L->contains(cast<Instruction>(U)))
526 if (cast<Instruction>(U) != Instructions.front())
Hal Finkelbf45efd2013-11-16 23:59:05 +0000527 return;
James Molloy64419d42015-01-29 21:52:03 +0000528 }
Hal Finkelbf45efd2013-11-16 23:59:05 +0000529
530 Instructions.push_back(C);
531 Valid = true;
532}
533
534// Collect the vector of possible reduction variables.
535void LoopReroll::collectPossibleReductions(Loop *L,
536 ReductionTracker &Reductions) {
537 BasicBlock *Header = L->getHeader();
538 for (BasicBlock::iterator I = Header->begin(),
539 IE = Header->getFirstInsertionPt(); I != IE; ++I) {
540 if (!isa<PHINode>(I))
541 continue;
542 if (!I->getType()->isSingleValueType())
543 continue;
544
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000545 SimpleLoopReduction SLR(&*I, L);
Hal Finkelbf45efd2013-11-16 23:59:05 +0000546 if (!SLR.valid())
547 continue;
548
549 DEBUG(dbgs() << "LRR: Possible reduction: " << *I << " (with " <<
550 SLR.size() << " chained instructions)\n");
551 Reductions.addSLR(SLR);
552 }
553}
554
555// Collect the set of all users of the provided root instruction. This set of
556// users contains not only the direct users of the root instruction, but also
557// all users of those users, and so on. There are two exceptions:
558//
559// 1. Instructions in the set of excluded instructions are never added to the
560// use set (even if they are users). This is used, for example, to exclude
561// including root increments in the use set of the primary IV.
562//
563// 2. Instructions in the set of final instructions are added to the use set
564// if they are users, but their users are not added. This is used, for
565// example, to prevent a reduction update from forcing all later reduction
566// updates into the use set.
James Molloy5f255eb2015-01-29 13:48:05 +0000567void LoopReroll::DAGRootTracker::collectInLoopUserSet(
Hal Finkelbf45efd2013-11-16 23:59:05 +0000568 Instruction *Root, const SmallInstructionSet &Exclude,
569 const SmallInstructionSet &Final,
570 DenseSet<Instruction *> &Users) {
571 SmallInstructionVector Queue(1, Root);
572 while (!Queue.empty()) {
573 Instruction *I = Queue.pop_back_val();
574 if (!Users.insert(I).second)
575 continue;
576
577 if (!Final.count(I))
Chandler Carruthcdf47882014-03-09 03:16:01 +0000578 for (Use &U : I->uses()) {
579 Instruction *User = cast<Instruction>(U.getUser());
Hal Finkelbf45efd2013-11-16 23:59:05 +0000580 if (PHINode *PN = dyn_cast<PHINode>(User)) {
581 // Ignore "wrap-around" uses to PHIs of this loop's header.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000582 if (PN->getIncomingBlock(U) == L->getHeader())
Hal Finkelbf45efd2013-11-16 23:59:05 +0000583 continue;
584 }
NAKAMURA Takumi335a7bc2014-10-28 11:53:30 +0000585
Hal Finkelbf45efd2013-11-16 23:59:05 +0000586 if (L->contains(User) && !Exclude.count(User)) {
587 Queue.push_back(User);
588 }
589 }
590
591 // We also want to collect single-user "feeder" values.
592 for (User::op_iterator OI = I->op_begin(),
593 OIE = I->op_end(); OI != OIE; ++OI) {
594 if (Instruction *Op = dyn_cast<Instruction>(*OI))
595 if (Op->hasOneUse() && L->contains(Op) && !Exclude.count(Op) &&
596 !Final.count(Op))
597 Queue.push_back(Op);
598 }
599 }
600}
601
602// Collect all of the users of all of the provided root instructions (combined
603// into a single set).
James Molloy5f255eb2015-01-29 13:48:05 +0000604void LoopReroll::DAGRootTracker::collectInLoopUserSet(
Hal Finkelbf45efd2013-11-16 23:59:05 +0000605 const SmallInstructionVector &Roots,
606 const SmallInstructionSet &Exclude,
607 const SmallInstructionSet &Final,
608 DenseSet<Instruction *> &Users) {
609 for (SmallInstructionVector::const_iterator I = Roots.begin(),
610 IE = Roots.end(); I != IE; ++I)
James Molloy5f255eb2015-01-29 13:48:05 +0000611 collectInLoopUserSet(*I, Exclude, Final, Users);
Hal Finkelbf45efd2013-11-16 23:59:05 +0000612}
613
614static bool isSimpleLoadStore(Instruction *I) {
615 if (LoadInst *LI = dyn_cast<LoadInst>(I))
616 return LI->isSimple();
617 if (StoreInst *SI = dyn_cast<StoreInst>(I))
618 return SI->isSimple();
619 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
620 return !MI->isVolatile();
621 return false;
622}
623
James Molloyf1473592015-02-11 09:19:47 +0000624/// Return true if IVU is a "simple" arithmetic operation.
625/// This is used for narrowing the search space for DAGRoots; only arithmetic
626/// and GEPs can be part of a DAGRoot.
627static bool isSimpleArithmeticOp(User *IVU) {
628 if (Instruction *I = dyn_cast<Instruction>(IVU)) {
629 switch (I->getOpcode()) {
630 default: return false;
631 case Instruction::Add:
632 case Instruction::Sub:
633 case Instruction::Mul:
634 case Instruction::Shl:
635 case Instruction::AShr:
636 case Instruction::LShr:
637 case Instruction::GetElementPtr:
638 case Instruction::Trunc:
639 case Instruction::ZExt:
640 case Instruction::SExt:
641 return true;
642 }
643 }
644 return false;
645}
646
647static bool isLoopIncrement(User *U, Instruction *IV) {
648 BinaryOperator *BO = dyn_cast<BinaryOperator>(U);
649 if (!BO || BO->getOpcode() != Instruction::Add)
650 return false;
651
652 for (auto *UU : BO->users()) {
653 PHINode *PN = dyn_cast<PHINode>(UU);
654 if (PN && PN == IV)
655 return true;
656 }
657 return false;
658}
659
660bool LoopReroll::DAGRootTracker::
661collectPossibleRoots(Instruction *Base, std::map<int64_t,Instruction*> &Roots) {
662 SmallInstructionVector BaseUsers;
663
664 for (auto *I : Base->users()) {
665 ConstantInt *CI = nullptr;
666
667 if (isLoopIncrement(I, IV)) {
668 LoopIncs.push_back(cast<Instruction>(I));
669 continue;
670 }
671
672 // The root nodes must be either GEPs, ORs or ADDs.
673 if (auto *BO = dyn_cast<BinaryOperator>(I)) {
674 if (BO->getOpcode() == Instruction::Add ||
675 BO->getOpcode() == Instruction::Or)
676 CI = dyn_cast<ConstantInt>(BO->getOperand(1));
677 } else if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
678 Value *LastOperand = GEP->getOperand(GEP->getNumOperands()-1);
679 CI = dyn_cast<ConstantInt>(LastOperand);
680 }
681
682 if (!CI) {
683 if (Instruction *II = dyn_cast<Instruction>(I)) {
684 BaseUsers.push_back(II);
685 continue;
686 } else {
687 DEBUG(dbgs() << "LRR: Aborting due to non-instruction: " << *I << "\n");
688 return false;
689 }
690 }
691
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000692 int64_t V = std::abs(CI->getValue().getSExtValue());
James Molloyf1473592015-02-11 09:19:47 +0000693 if (Roots.find(V) != Roots.end())
694 // No duplicates, please.
695 return false;
696
James Molloyf1473592015-02-11 09:19:47 +0000697 Roots[V] = cast<Instruction>(I);
698 }
699
700 if (Roots.empty())
701 return false;
James Molloyf1473592015-02-11 09:19:47 +0000702
703 // If we found non-loop-inc, non-root users of Base, assume they are
704 // for the zeroth root index. This is because "add %a, 0" gets optimized
705 // away.
James Molloye32d8062015-02-16 17:02:00 +0000706 if (BaseUsers.size()) {
707 if (Roots.find(0) != Roots.end()) {
708 DEBUG(dbgs() << "LRR: Multiple roots found for base - aborting!\n");
709 return false;
710 }
James Molloyf1473592015-02-11 09:19:47 +0000711 Roots[0] = Base;
James Molloye32d8062015-02-16 17:02:00 +0000712 }
James Molloyf1473592015-02-11 09:19:47 +0000713
714 // Calculate the number of users of the base, or lowest indexed, iteration.
715 unsigned NumBaseUses = BaseUsers.size();
716 if (NumBaseUses == 0)
717 NumBaseUses = Roots.begin()->second->getNumUses();
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000718
James Molloyf1473592015-02-11 09:19:47 +0000719 // Check that every node has the same number of users.
720 for (auto &KV : Roots) {
721 if (KV.first == 0)
722 continue;
723 if (KV.second->getNumUses() != NumBaseUses) {
724 DEBUG(dbgs() << "LRR: Aborting - Root and Base #users not the same: "
725 << "#Base=" << NumBaseUses << ", #Root=" <<
726 KV.second->getNumUses() << "\n");
727 return false;
728 }
729 }
730
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000731 return true;
James Molloyf1473592015-02-11 09:19:47 +0000732}
733
734bool LoopReroll::DAGRootTracker::
735findRootsRecursive(Instruction *I, SmallInstructionSet SubsumedInsts) {
736 // Does the user look like it could be part of a root set?
737 // All its users must be simple arithmetic ops.
738 if (I->getNumUses() > IL_MaxRerollIterations)
739 return false;
740
741 if ((I->getOpcode() == Instruction::Mul ||
742 I->getOpcode() == Instruction::PHI) &&
743 I != IV &&
744 findRootsBase(I, SubsumedInsts))
745 return true;
746
747 SubsumedInsts.insert(I);
748
749 for (User *V : I->users()) {
750 Instruction *I = dyn_cast<Instruction>(V);
751 if (std::find(LoopIncs.begin(), LoopIncs.end(), I) != LoopIncs.end())
752 continue;
753
754 if (!I || !isSimpleArithmeticOp(I) ||
755 !findRootsRecursive(I, SubsumedInsts))
756 return false;
757 }
758 return true;
759}
760
761bool LoopReroll::DAGRootTracker::
762findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts) {
763
764 // The base instruction needs to be a multiply so
765 // that we can erase it.
766 if (IVU->getOpcode() != Instruction::Mul &&
767 IVU->getOpcode() != Instruction::PHI)
768 return false;
769
770 std::map<int64_t, Instruction*> V;
771 if (!collectPossibleRoots(IVU, V))
772 return false;
773
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000774 // If we didn't get a root for index zero, then IVU must be
James Molloyf1473592015-02-11 09:19:47 +0000775 // subsumed.
776 if (V.find(0) == V.end())
777 SubsumedInsts.insert(IVU);
778
779 // Partition the vector into monotonically increasing indexes.
780 DAGRootSet DRS;
781 DRS.BaseInst = nullptr;
782
783 for (auto &KV : V) {
784 if (!DRS.BaseInst) {
785 DRS.BaseInst = KV.second;
786 DRS.SubsumedInsts = SubsumedInsts;
787 } else if (DRS.Roots.empty()) {
788 DRS.Roots.push_back(KV.second);
789 } else if (V.find(KV.first - 1) != V.end()) {
790 DRS.Roots.push_back(KV.second);
791 } else {
792 // Linear sequence terminated.
793 RootSets.push_back(DRS);
794 DRS.BaseInst = KV.second;
795 DRS.SubsumedInsts = SubsumedInsts;
796 DRS.Roots.clear();
797 }
798 }
799 RootSets.push_back(DRS);
800
801 return true;
802}
803
James Molloy5f255eb2015-01-29 13:48:05 +0000804bool LoopReroll::DAGRootTracker::findRoots() {
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000805 Inc = IVToIncMap[IV];
James Molloy5f255eb2015-01-29 13:48:05 +0000806
James Molloyf1473592015-02-11 09:19:47 +0000807 assert(RootSets.empty() && "Unclean state!");
Lawrence Hudc8a83b2015-07-24 22:01:49 +0000808 if (std::abs(Inc) == 1) {
James Molloyf1473592015-02-11 09:19:47 +0000809 for (auto *IVU : IV->users()) {
810 if (isLoopIncrement(IVU, IV))
811 LoopIncs.push_back(cast<Instruction>(IVU));
812 }
813 if (!findRootsRecursive(IV, SmallInstructionSet()))
814 return false;
815 LoopIncs.push_back(IV);
816 } else {
817 if (!findRootsBase(IV, SmallInstructionSet()))
818 return false;
819 }
James Molloy5f255eb2015-01-29 13:48:05 +0000820
James Molloyf1473592015-02-11 09:19:47 +0000821 // Ensure all sets have the same size.
822 if (RootSets.empty()) {
823 DEBUG(dbgs() << "LRR: Aborting because no root sets found!\n");
James Molloy5f255eb2015-01-29 13:48:05 +0000824 return false;
James Molloyf1473592015-02-11 09:19:47 +0000825 }
826 for (auto &V : RootSets) {
827 if (V.Roots.empty() || V.Roots.size() != RootSets[0].Roots.size()) {
828 DEBUG(dbgs()
829 << "LRR: Aborting because not all root sets have the same size\n");
830 return false;
831 }
832 }
James Molloy5f255eb2015-01-29 13:48:05 +0000833
James Molloyf1473592015-02-11 09:19:47 +0000834 // And ensure all loop iterations are consecutive. We rely on std::map
835 // providing ordered traversal.
836 for (auto &V : RootSets) {
837 const auto *ADR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(V.BaseInst));
838 if (!ADR)
839 return false;
840
841 // Consider a DAGRootSet with N-1 roots (so N different values including
842 // BaseInst).
843 // Define d = Roots[0] - BaseInst, which should be the same as
844 // Roots[I] - Roots[I-1] for all I in [1..N).
845 // Define D = BaseInst@J - BaseInst@J-1, where "@J" means the value at the
846 // loop iteration J.
847 //
848 // Now, For the loop iterations to be consecutive:
849 // D = d * N
850
851 unsigned N = V.Roots.size() + 1;
852 const SCEV *StepSCEV = SE->getMinusSCEV(SE->getSCEV(V.Roots[0]), ADR);
853 const SCEV *ScaleSCEV = SE->getConstant(StepSCEV->getType(), N);
854 if (ADR->getStepRecurrence(*SE) != SE->getMulExpr(StepSCEV, ScaleSCEV)) {
855 DEBUG(dbgs() << "LRR: Aborting because iterations are not consecutive\n");
856 return false;
857 }
858 }
859 Scale = RootSets[0].Roots.size() + 1;
860
861 if (Scale > IL_MaxRerollIterations) {
James Molloy64419d42015-01-29 21:52:03 +0000862 DEBUG(dbgs() << "LRR: Aborting - too many iterations found. "
James Molloyf1473592015-02-11 09:19:47 +0000863 << "#Found=" << Scale << ", #Max=" << IL_MaxRerollIterations
James Molloy64419d42015-01-29 21:52:03 +0000864 << "\n");
865 return false;
866 }
867
James Molloyf1473592015-02-11 09:19:47 +0000868 DEBUG(dbgs() << "LRR: Successfully found roots: Scale=" << Scale << "\n");
James Molloy5f255eb2015-01-29 13:48:05 +0000869
870 return true;
871}
872
James Molloy64419d42015-01-29 21:52:03 +0000873bool LoopReroll::DAGRootTracker::collectUsedInstructions(SmallInstructionSet &PossibleRedSet) {
874 // Populate the MapVector with all instructions in the block, in order first,
875 // so we can iterate over the contents later in perfect order.
876 for (auto &I : *L->getHeader()) {
877 Uses[&I].resize(IL_End);
878 }
James Molloy5f255eb2015-01-29 13:48:05 +0000879
James Molloy64419d42015-01-29 21:52:03 +0000880 SmallInstructionSet Exclude;
James Molloyf1473592015-02-11 09:19:47 +0000881 for (auto &DRS : RootSets) {
882 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
883 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
884 Exclude.insert(DRS.BaseInst);
885 }
James Molloy64419d42015-01-29 21:52:03 +0000886 Exclude.insert(LoopIncs.begin(), LoopIncs.end());
887
James Molloyf1473592015-02-11 09:19:47 +0000888 for (auto &DRS : RootSets) {
889 DenseSet<Instruction*> VBase;
890 collectInLoopUserSet(DRS.BaseInst, Exclude, PossibleRedSet, VBase);
891 for (auto *I : VBase) {
892 Uses[I].set(0);
James Molloy64419d42015-01-29 21:52:03 +0000893 }
894
James Molloyf1473592015-02-11 09:19:47 +0000895 unsigned Idx = 1;
896 for (auto *Root : DRS.Roots) {
897 DenseSet<Instruction*> V;
898 collectInLoopUserSet(Root, Exclude, PossibleRedSet, V);
899
900 // While we're here, check the use sets are the same size.
901 if (V.size() != VBase.size()) {
902 DEBUG(dbgs() << "LRR: Aborting - use sets are different sizes\n");
903 return false;
904 }
905
906 for (auto *I : V) {
907 Uses[I].set(Idx);
908 }
909 ++Idx;
James Molloy64419d42015-01-29 21:52:03 +0000910 }
James Molloyf1473592015-02-11 09:19:47 +0000911
912 // Make sure our subsumed instructions are remembered too.
913 for (auto *I : DRS.SubsumedInsts) {
914 Uses[I].set(IL_All);
915 }
James Molloy64419d42015-01-29 21:52:03 +0000916 }
917
918 // Make sure the loop increments are also accounted for.
James Molloyf1473592015-02-11 09:19:47 +0000919
James Molloy64419d42015-01-29 21:52:03 +0000920 Exclude.clear();
James Molloyf1473592015-02-11 09:19:47 +0000921 for (auto &DRS : RootSets) {
922 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
923 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
924 Exclude.insert(DRS.BaseInst);
925 }
James Molloy64419d42015-01-29 21:52:03 +0000926
927 DenseSet<Instruction*> V;
928 collectInLoopUserSet(LoopIncs, Exclude, PossibleRedSet, V);
929 for (auto *I : V) {
James Molloyf1473592015-02-11 09:19:47 +0000930 Uses[I].set(IL_All);
James Molloy64419d42015-01-29 21:52:03 +0000931 }
James Molloy64419d42015-01-29 21:52:03 +0000932
933 return true;
934
935}
936
James Molloye805ad92015-02-12 15:54:14 +0000937/// Get the next instruction in "In" that is a member of set Val.
938/// Start searching from StartI, and do not return anything in Exclude.
939/// If StartI is not given, start from In.begin().
James Molloy64419d42015-01-29 21:52:03 +0000940LoopReroll::DAGRootTracker::UsesTy::iterator
941LoopReroll::DAGRootTracker::nextInstr(int Val, UsesTy &In,
James Molloye805ad92015-02-12 15:54:14 +0000942 const SmallInstructionSet &Exclude,
943 UsesTy::iterator *StartI) {
944 UsesTy::iterator I = StartI ? *StartI : In.begin();
945 while (I != In.end() && (I->second.test(Val) == 0 ||
946 Exclude.count(I->first) != 0))
James Molloy64419d42015-01-29 21:52:03 +0000947 ++I;
948 return I;
949}
950
James Molloyf1473592015-02-11 09:19:47 +0000951bool LoopReroll::DAGRootTracker::isBaseInst(Instruction *I) {
952 for (auto &DRS : RootSets) {
953 if (DRS.BaseInst == I)
954 return true;
955 }
956 return false;
957}
958
959bool LoopReroll::DAGRootTracker::isRootInst(Instruction *I) {
960 for (auto &DRS : RootSets) {
961 if (std::find(DRS.Roots.begin(), DRS.Roots.end(), I) != DRS.Roots.end())
962 return true;
963 }
964 return false;
965}
966
James Molloye805ad92015-02-12 15:54:14 +0000967/// Return true if instruction I depends on any instruction between
968/// Start and End.
969bool LoopReroll::DAGRootTracker::instrDependsOn(Instruction *I,
970 UsesTy::iterator Start,
971 UsesTy::iterator End) {
972 for (auto *U : I->users()) {
973 for (auto It = Start; It != End; ++It)
974 if (U == It->first)
975 return true;
976 }
977 return false;
978}
979
Weiming Zhao310770a2015-09-28 17:03:23 +0000980static bool isIgnorableInst(const Instruction *I) {
981 if (isa<DbgInfoIntrinsic>(I))
982 return true;
983 const IntrinsicInst* II = dyn_cast<IntrinsicInst>(I);
984 if (!II)
985 return false;
986 switch (II->getIntrinsicID()) {
987 default:
988 return false;
989 case llvm::Intrinsic::annotation:
990 case Intrinsic::ptr_annotation:
991 case Intrinsic::var_annotation:
992 // TODO: the following intrinsics may also be whitelisted:
993 // lifetime_start, lifetime_end, invariant_start, invariant_end
994 return true;
995 }
996 return false;
997}
998
James Molloy64419d42015-01-29 21:52:03 +0000999bool LoopReroll::DAGRootTracker::validate(ReductionTracker &Reductions) {
James Molloy5f255eb2015-01-29 13:48:05 +00001000 // We now need to check for equivalence of the use graph of each root with
1001 // that of the primary induction variable (excluding the roots). Our goal
1002 // here is not to solve the full graph isomorphism problem, but rather to
1003 // catch common cases without a lot of work. As a result, we will assume
1004 // that the relative order of the instructions in each unrolled iteration
1005 // is the same (although we will not make an assumption about how the
1006 // different iterations are intermixed). Note that while the order must be
1007 // the same, the instructions may not be in the same basic block.
James Molloy5f255eb2015-01-29 13:48:05 +00001008
1009 // An array of just the possible reductions for this scale factor. When we
1010 // collect the set of all users of some root instructions, these reduction
1011 // instructions are treated as 'final' (their uses are not considered).
1012 // This is important because we don't want the root use set to search down
1013 // the reduction chain.
1014 SmallInstructionSet PossibleRedSet;
1015 SmallInstructionSet PossibleRedLastSet;
1016 SmallInstructionSet PossibleRedPHISet;
1017 Reductions.restrictToScale(Scale, PossibleRedSet,
1018 PossibleRedPHISet, PossibleRedLastSet);
James Molloy5f255eb2015-01-29 13:48:05 +00001019
James Molloy64419d42015-01-29 21:52:03 +00001020 // Populate "Uses" with where each instruction is used.
1021 if (!collectUsedInstructions(PossibleRedSet))
1022 return false;
James Molloy5f255eb2015-01-29 13:48:05 +00001023
James Molloy64419d42015-01-29 21:52:03 +00001024 // Make sure we mark the reduction PHIs as used in all iterations.
1025 for (auto *I : PossibleRedPHISet) {
James Molloyf1473592015-02-11 09:19:47 +00001026 Uses[I].set(IL_All);
James Molloy64419d42015-01-29 21:52:03 +00001027 }
James Molloy5f255eb2015-01-29 13:48:05 +00001028
James Molloy64419d42015-01-29 21:52:03 +00001029 // Make sure all instructions in the loop are in one and only one
1030 // set.
1031 for (auto &KV : Uses) {
Weiming Zhao310770a2015-09-28 17:03:23 +00001032 if (KV.second.count() != 1 && !isIgnorableInst(KV.first)) {
James Molloy64419d42015-01-29 21:52:03 +00001033 DEBUG(dbgs() << "LRR: Aborting - instruction is not used in 1 iteration: "
1034 << *KV.first << " (#uses=" << KV.second.count() << ")\n");
1035 return false;
Hal Finkelbf45efd2013-11-16 23:59:05 +00001036 }
James Molloy64419d42015-01-29 21:52:03 +00001037 }
Hal Finkelbf45efd2013-11-16 23:59:05 +00001038
James Molloy64419d42015-01-29 21:52:03 +00001039 DEBUG(
1040 for (auto &KV : Uses) {
1041 dbgs() << "LRR: " << KV.second.find_first() << "\t" << *KV.first << "\n";
1042 }
1043 );
1044
1045 for (unsigned Iter = 1; Iter < Scale; ++Iter) {
James Molloy5f255eb2015-01-29 13:48:05 +00001046 // In addition to regular aliasing information, we need to look for
1047 // instructions from later (future) iterations that have side effects
1048 // preventing us from reordering them past other instructions with side
1049 // effects.
1050 bool FutureSideEffects = false;
1051 AliasSetTracker AST(*AA);
James Molloy5f255eb2015-01-29 13:48:05 +00001052 // The map between instructions in f(%iv.(i+1)) and f(%iv).
1053 DenseMap<Value *, Value *> BaseMap;
1054
James Molloy64419d42015-01-29 21:52:03 +00001055 // Compare iteration Iter to the base.
James Molloye805ad92015-02-12 15:54:14 +00001056 SmallInstructionSet Visited;
1057 auto BaseIt = nextInstr(0, Uses, Visited);
1058 auto RootIt = nextInstr(Iter, Uses, Visited);
James Molloy64419d42015-01-29 21:52:03 +00001059 auto LastRootIt = Uses.begin();
James Molloy5f255eb2015-01-29 13:48:05 +00001060
James Molloy64419d42015-01-29 21:52:03 +00001061 while (BaseIt != Uses.end() && RootIt != Uses.end()) {
1062 Instruction *BaseInst = BaseIt->first;
1063 Instruction *RootInst = RootIt->first;
James Molloy5f255eb2015-01-29 13:48:05 +00001064
James Molloy64419d42015-01-29 21:52:03 +00001065 // Skip over the IV or root instructions; only match their users.
1066 bool Continue = false;
James Molloyf1473592015-02-11 09:19:47 +00001067 if (isBaseInst(BaseInst)) {
James Molloye805ad92015-02-12 15:54:14 +00001068 Visited.insert(BaseInst);
1069 BaseIt = nextInstr(0, Uses, Visited);
James Molloy64419d42015-01-29 21:52:03 +00001070 Continue = true;
1071 }
James Molloyf1473592015-02-11 09:19:47 +00001072 if (isRootInst(RootInst)) {
James Molloy64419d42015-01-29 21:52:03 +00001073 LastRootIt = RootIt;
James Molloye805ad92015-02-12 15:54:14 +00001074 Visited.insert(RootInst);
1075 RootIt = nextInstr(Iter, Uses, Visited);
James Molloy64419d42015-01-29 21:52:03 +00001076 Continue = true;
1077 }
1078 if (Continue) continue;
James Molloy5f255eb2015-01-29 13:48:05 +00001079
James Molloye805ad92015-02-12 15:54:14 +00001080 if (!BaseInst->isSameOperationAs(RootInst)) {
1081 // Last chance saloon. We don't try and solve the full isomorphism
1082 // problem, but try and at least catch the case where two instructions
1083 // *of different types* are round the wrong way. We won't be able to
1084 // efficiently tell, given two ADD instructions, which way around we
1085 // should match them, but given an ADD and a SUB, we can at least infer
1086 // which one is which.
1087 //
1088 // This should allow us to deal with a greater subset of the isomorphism
1089 // problem. It does however change a linear algorithm into a quadratic
1090 // one, so limit the number of probes we do.
1091 auto TryIt = RootIt;
1092 unsigned N = NumToleratedFailedMatches;
1093 while (TryIt != Uses.end() &&
1094 !BaseInst->isSameOperationAs(TryIt->first) &&
1095 N--) {
1096 ++TryIt;
1097 TryIt = nextInstr(Iter, Uses, Visited, &TryIt);
1098 }
1099
1100 if (TryIt == Uses.end() || TryIt == RootIt ||
1101 instrDependsOn(TryIt->first, RootIt, TryIt)) {
1102 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1103 " vs. " << *RootInst << "\n");
1104 return false;
1105 }
Lawrence Hudc8a83b2015-07-24 22:01:49 +00001106
James Molloye805ad92015-02-12 15:54:14 +00001107 RootIt = TryIt;
1108 RootInst = TryIt->first;
1109 }
1110
James Molloy64419d42015-01-29 21:52:03 +00001111 // All instructions between the last root and this root
Lawrence Hudc8a83b2015-07-24 22:01:49 +00001112 // may belong to some other iteration. If they belong to a
James Molloy64419d42015-01-29 21:52:03 +00001113 // future iteration, then they're dangerous to alias with.
Lawrence Hudc8a83b2015-07-24 22:01:49 +00001114 //
James Molloye805ad92015-02-12 15:54:14 +00001115 // Note that because we allow a limited amount of flexibility in the order
1116 // that we visit nodes, LastRootIt might be *before* RootIt, in which
1117 // case we've already checked this set of instructions so we shouldn't
1118 // do anything.
1119 for (; LastRootIt < RootIt; ++LastRootIt) {
James Molloy64419d42015-01-29 21:52:03 +00001120 Instruction *I = LastRootIt->first;
1121 if (LastRootIt->second.find_first() < (int)Iter)
1122 continue;
1123 if (I->mayWriteToMemory())
1124 AST.add(I);
1125 // Note: This is specifically guarded by a check on isa<PHINode>,
1126 // which while a valid (somewhat arbitrary) micro-optimization, is
1127 // needed because otherwise isSafeToSpeculativelyExecute returns
1128 // false on PHI nodes.
1129 if (!isa<PHINode>(I) && !isSimpleLoadStore(I) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001130 !isSafeToSpeculativelyExecute(I))
James Molloy64419d42015-01-29 21:52:03 +00001131 // Intervening instructions cause side effects.
1132 FutureSideEffects = true;
James Molloy5f255eb2015-01-29 13:48:05 +00001133 }
1134
James Molloy5f255eb2015-01-29 13:48:05 +00001135 // Make sure that this instruction, which is in the use set of this
1136 // root instruction, does not also belong to the base set or the set of
James Molloy64419d42015-01-29 21:52:03 +00001137 // some other root instruction.
1138 if (RootIt->second.count() > 1) {
1139 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1140 " vs. " << *RootInst << " (prev. case overlap)\n");
1141 return false;
James Molloy5f255eb2015-01-29 13:48:05 +00001142 }
1143
1144 // Make sure that we don't alias with any instruction in the alias set
1145 // tracker. If we do, then we depend on a future iteration, and we
1146 // can't reroll.
James Molloy64419d42015-01-29 21:52:03 +00001147 if (RootInst->mayReadFromMemory())
1148 for (auto &K : AST) {
1149 if (K.aliasesUnknownInst(RootInst, *AA)) {
1150 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1151 " vs. " << *RootInst << " (depends on future store)\n");
1152 return false;
James Molloy5f255eb2015-01-29 13:48:05 +00001153 }
1154 }
James Molloy5f255eb2015-01-29 13:48:05 +00001155
1156 // If we've past an instruction from a future iteration that may have
1157 // side effects, and this instruction might also, then we can't reorder
1158 // them, and this matching fails. As an exception, we allow the alias
1159 // set tracker to handle regular (simple) load/store dependencies.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001160 if (FutureSideEffects && ((!isSimpleLoadStore(BaseInst) &&
1161 !isSafeToSpeculativelyExecute(BaseInst)) ||
1162 (!isSimpleLoadStore(RootInst) &&
1163 !isSafeToSpeculativelyExecute(RootInst)))) {
James Molloy64419d42015-01-29 21:52:03 +00001164 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1165 " vs. " << *RootInst <<
James Molloy5f255eb2015-01-29 13:48:05 +00001166 " (side effects prevent reordering)\n");
James Molloy64419d42015-01-29 21:52:03 +00001167 return false;
James Molloy5f255eb2015-01-29 13:48:05 +00001168 }
1169
1170 // For instructions that are part of a reduction, if the operation is
1171 // associative, then don't bother matching the operands (because we
1172 // already know that the instructions are isomorphic, and the order
1173 // within the iteration does not matter). For non-associative reductions,
1174 // we do need to match the operands, because we need to reject
1175 // out-of-order instructions within an iteration!
1176 // For example (assume floating-point addition), we need to reject this:
1177 // x += a[i]; x += b[i];
1178 // x += a[i+1]; x += b[i+1];
1179 // x += b[i+2]; x += a[i+2];
James Molloy64419d42015-01-29 21:52:03 +00001180 bool InReduction = Reductions.isPairInSame(BaseInst, RootInst);
James Molloy5f255eb2015-01-29 13:48:05 +00001181
James Molloy64419d42015-01-29 21:52:03 +00001182 if (!(InReduction && BaseInst->isAssociative())) {
James Molloy5f255eb2015-01-29 13:48:05 +00001183 bool Swapped = false, SomeOpMatched = false;
James Molloy64419d42015-01-29 21:52:03 +00001184 for (unsigned j = 0; j < BaseInst->getNumOperands(); ++j) {
1185 Value *Op2 = RootInst->getOperand(j);
James Molloy5f255eb2015-01-29 13:48:05 +00001186
1187 // If this is part of a reduction (and the operation is not
1188 // associatve), then we match all operands, but not those that are
1189 // part of the reduction.
1190 if (InReduction)
1191 if (Instruction *Op2I = dyn_cast<Instruction>(Op2))
James Molloy64419d42015-01-29 21:52:03 +00001192 if (Reductions.isPairInSame(RootInst, Op2I))
James Molloy5f255eb2015-01-29 13:48:05 +00001193 continue;
1194
1195 DenseMap<Value *, Value *>::iterator BMI = BaseMap.find(Op2);
James Molloyf1473592015-02-11 09:19:47 +00001196 if (BMI != BaseMap.end()) {
James Molloy5f255eb2015-01-29 13:48:05 +00001197 Op2 = BMI->second;
James Molloyf1473592015-02-11 09:19:47 +00001198 } else {
1199 for (auto &DRS : RootSets) {
1200 if (DRS.Roots[Iter-1] == (Instruction*) Op2) {
1201 Op2 = DRS.BaseInst;
1202 break;
1203 }
1204 }
1205 }
James Molloy5f255eb2015-01-29 13:48:05 +00001206
James Molloy64419d42015-01-29 21:52:03 +00001207 if (BaseInst->getOperand(Swapped ? unsigned(!j) : j) != Op2) {
James Molloy5f255eb2015-01-29 13:48:05 +00001208 // If we've not already decided to swap the matched operands, and
1209 // we've not already matched our first operand (note that we could
1210 // have skipped matching the first operand because it is part of a
1211 // reduction above), and the instruction is commutative, then try
1212 // the swapped match.
James Molloy64419d42015-01-29 21:52:03 +00001213 if (!Swapped && BaseInst->isCommutative() && !SomeOpMatched &&
1214 BaseInst->getOperand(!j) == Op2) {
James Molloy5f255eb2015-01-29 13:48:05 +00001215 Swapped = true;
1216 } else {
James Molloy64419d42015-01-29 21:52:03 +00001217 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
1218 << " vs. " << *RootInst << " (operand " << j << ")\n");
1219 return false;
James Molloy5f255eb2015-01-29 13:48:05 +00001220 }
1221 }
1222
1223 SomeOpMatched = true;
1224 }
1225 }
1226
James Molloy64419d42015-01-29 21:52:03 +00001227 if ((!PossibleRedLastSet.count(BaseInst) &&
1228 hasUsesOutsideLoop(BaseInst, L)) ||
1229 (!PossibleRedLastSet.count(RootInst) &&
1230 hasUsesOutsideLoop(RootInst, L))) {
1231 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1232 " vs. " << *RootInst << " (uses outside loop)\n");
1233 return false;
James Molloy5f255eb2015-01-29 13:48:05 +00001234 }
1235
James Molloy64419d42015-01-29 21:52:03 +00001236 Reductions.recordPair(BaseInst, RootInst, Iter);
1237 BaseMap.insert(std::make_pair(RootInst, BaseInst));
James Molloy5f255eb2015-01-29 13:48:05 +00001238
James Molloy64419d42015-01-29 21:52:03 +00001239 LastRootIt = RootIt;
James Molloye805ad92015-02-12 15:54:14 +00001240 Visited.insert(BaseInst);
1241 Visited.insert(RootInst);
1242 BaseIt = nextInstr(0, Uses, Visited);
1243 RootIt = nextInstr(Iter, Uses, Visited);
James Molloy5f255eb2015-01-29 13:48:05 +00001244 }
James Molloy64419d42015-01-29 21:52:03 +00001245 assert (BaseIt == Uses.end() && RootIt == Uses.end() &&
1246 "Mismatched set sizes!");
James Molloy5f255eb2015-01-29 13:48:05 +00001247 }
1248
James Molloy5f255eb2015-01-29 13:48:05 +00001249 DEBUG(dbgs() << "LRR: Matched all iteration increments for " <<
James Molloyf1473592015-02-11 09:19:47 +00001250 *IV << "\n");
James Molloy5f255eb2015-01-29 13:48:05 +00001251
Hal Finkelbf45efd2013-11-16 23:59:05 +00001252 return true;
1253}
1254
James Molloy5f255eb2015-01-29 13:48:05 +00001255void LoopReroll::DAGRootTracker::replace(const SCEV *IterCount) {
1256 BasicBlock *Header = L->getHeader();
1257 // Remove instructions associated with non-base iterations.
1258 for (BasicBlock::reverse_iterator J = Header->rbegin();
1259 J != Header->rend();) {
James Molloy64419d42015-01-29 21:52:03 +00001260 unsigned I = Uses[&*J].find_first();
James Molloyf1473592015-02-11 09:19:47 +00001261 if (I > 0 && I < IL_All) {
James Molloy5f255eb2015-01-29 13:48:05 +00001262 Instruction *D = &*J;
1263 DEBUG(dbgs() << "LRR: removing: " << *D << "\n");
1264 D->eraseFromParent();
1265 continue;
1266 }
1267
1268 ++J;
1269 }
Lawrence Hudc8a83b2015-07-24 22:01:49 +00001270 bool Negative = IVToIncMap[IV] < 0;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001271 const DataLayout &DL = Header->getModule()->getDataLayout();
James Molloy5f255eb2015-01-29 13:48:05 +00001272
James Molloyf1473592015-02-11 09:19:47 +00001273 // We need to create a new induction variable for each different BaseInst.
1274 for (auto &DRS : RootSets) {
1275 // Insert the new induction variable.
1276 const SCEVAddRecExpr *RealIVSCEV =
1277 cast<SCEVAddRecExpr>(SE->getSCEV(DRS.BaseInst));
1278 const SCEV *Start = RealIVSCEV->getStart();
Lawrence Hudc8a83b2015-07-24 22:01:49 +00001279 const SCEVAddRecExpr *H = cast<SCEVAddRecExpr>(SE->getAddRecExpr(
1280 Start, SE->getConstant(RealIVSCEV->getType(), Negative ? -1 : 1), L,
1281 SCEV::FlagAnyWrap));
James Molloyf1473592015-02-11 09:19:47 +00001282 { // Limit the lifetime of SCEVExpander.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001283 SCEVExpander Expander(*SE, DL, "reroll");
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001284 Value *NewIV = Expander.expandCodeFor(H, IV->getType(), &Header->front());
James Molloy5f255eb2015-01-29 13:48:05 +00001285
James Molloyf1473592015-02-11 09:19:47 +00001286 for (auto &KV : Uses) {
1287 if (KV.second.find_first() == 0)
1288 KV.first->replaceUsesOfWith(DRS.BaseInst, NewIV);
1289 }
James Molloy5f255eb2015-01-29 13:48:05 +00001290
James Molloyf1473592015-02-11 09:19:47 +00001291 if (BranchInst *BI = dyn_cast<BranchInst>(Header->getTerminator())) {
1292 // FIXME: Why do we need this check?
1293 if (Uses[BI].find_first() == IL_All) {
1294 const SCEV *ICSCEV = RealIVSCEV->evaluateAtIteration(IterCount, *SE);
James Molloy5f255eb2015-01-29 13:48:05 +00001295
James Molloyf1473592015-02-11 09:19:47 +00001296 // Iteration count SCEV minus 1
Lawrence Hudc8a83b2015-07-24 22:01:49 +00001297 const SCEV *ICMinus1SCEV = SE->getMinusSCEV(
1298 ICSCEV, SE->getConstant(ICSCEV->getType(), Negative ? -1 : 1));
James Molloy5f255eb2015-01-29 13:48:05 +00001299
James Molloyf1473592015-02-11 09:19:47 +00001300 Value *ICMinus1; // Iteration count minus 1
1301 if (isa<SCEVConstant>(ICMinus1SCEV)) {
1302 ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(), BI);
1303 } else {
1304 BasicBlock *Preheader = L->getLoopPreheader();
1305 if (!Preheader)
1306 Preheader = InsertPreheaderForLoop(L, Parent);
James Molloy5f255eb2015-01-29 13:48:05 +00001307
James Molloyf1473592015-02-11 09:19:47 +00001308 ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(),
1309 Preheader->getTerminator());
1310 }
1311
1312 Value *Cond =
James Molloy5f255eb2015-01-29 13:48:05 +00001313 new ICmpInst(BI, CmpInst::ICMP_EQ, NewIV, ICMinus1, "exitcond");
James Molloyf1473592015-02-11 09:19:47 +00001314 BI->setCondition(Cond);
James Molloy5f255eb2015-01-29 13:48:05 +00001315
James Molloyf1473592015-02-11 09:19:47 +00001316 if (BI->getSuccessor(1) != Header)
1317 BI->swapSuccessors();
1318 }
James Molloy5f255eb2015-01-29 13:48:05 +00001319 }
1320 }
1321 }
1322
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001323 SimplifyInstructionsInBlock(Header, TLI);
James Molloy5f255eb2015-01-29 13:48:05 +00001324 DeleteDeadPHIs(Header, TLI);
1325}
1326
Hal Finkelbf45efd2013-11-16 23:59:05 +00001327// Validate the selected reductions. All iterations must have an isomorphic
1328// part of the reduction chain and, for non-associative reductions, the chain
1329// entries must appear in order.
1330bool LoopReroll::ReductionTracker::validateSelected() {
1331 // For a non-associative reduction, the chain entries must appear in order.
1332 for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end();
1333 RI != RIE; ++RI) {
1334 int i = *RI;
1335 int PrevIter = 0, BaseCount = 0, Count = 0;
NAKAMURA Takumi5af50a52014-10-28 11:54:05 +00001336 for (Instruction *J : PossibleReds[i]) {
1337 // Note that all instructions in the chain must have been found because
1338 // all instructions in the function must have been assigned to some
1339 // iteration.
1340 int Iter = PossibleRedIter[J];
Hal Finkelbf45efd2013-11-16 23:59:05 +00001341 if (Iter != PrevIter && Iter != PrevIter + 1 &&
1342 !PossibleReds[i].getReducedValue()->isAssociative()) {
1343 DEBUG(dbgs() << "LRR: Out-of-order non-associative reduction: " <<
NAKAMURA Takumi5af50a52014-10-28 11:54:05 +00001344 J << "\n");
Hal Finkelbf45efd2013-11-16 23:59:05 +00001345 return false;
1346 }
1347
1348 if (Iter != PrevIter) {
1349 if (Count != BaseCount) {
1350 DEBUG(dbgs() << "LRR: Iteration " << PrevIter <<
1351 " reduction use count " << Count <<
1352 " is not equal to the base use count " <<
1353 BaseCount << "\n");
1354 return false;
1355 }
1356
1357 Count = 0;
1358 }
1359
1360 ++Count;
1361 if (Iter == 0)
1362 ++BaseCount;
1363
1364 PrevIter = Iter;
1365 }
1366 }
1367
1368 return true;
1369}
1370
1371// For all selected reductions, remove all parts except those in the first
1372// iteration (and the PHI). Replace outside uses of the reduced value with uses
1373// of the first-iteration reduced value (in other words, reroll the selected
1374// reductions).
1375void LoopReroll::ReductionTracker::replaceSelected() {
1376 // Fixup reductions to refer to the last instruction associated with the
1377 // first iteration (not the last).
1378 for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end();
1379 RI != RIE; ++RI) {
1380 int i = *RI;
1381 int j = 0;
1382 for (int e = PossibleReds[i].size(); j != e; ++j)
1383 if (PossibleRedIter[PossibleReds[i][j]] != 0) {
1384 --j;
1385 break;
1386 }
1387
1388 // Replace users with the new end-of-chain value.
1389 SmallInstructionVector Users;
James Molloy64419d42015-01-29 21:52:03 +00001390 for (User *U : PossibleReds[i].getReducedValue()->users()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001391 Users.push_back(cast<Instruction>(U));
James Molloy64419d42015-01-29 21:52:03 +00001392 }
Hal Finkelbf45efd2013-11-16 23:59:05 +00001393
1394 for (SmallInstructionVector::iterator J = Users.begin(),
1395 JE = Users.end(); J != JE; ++J)
1396 (*J)->replaceUsesOfWith(PossibleReds[i].getReducedValue(),
1397 PossibleReds[i][j]);
1398 }
1399}
1400
1401// Reroll the provided loop with respect to the provided induction variable.
1402// Generally, we're looking for a loop like this:
1403//
1404// %iv = phi [ (preheader, ...), (body, %iv.next) ]
1405// f(%iv)
1406// %iv.1 = add %iv, 1 <-- a root increment
1407// f(%iv.1)
1408// %iv.2 = add %iv, 2 <-- a root increment
1409// f(%iv.2)
1410// %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
1411// f(%iv.scale_m_1)
1412// ...
1413// %iv.next = add %iv, scale
1414// %cmp = icmp(%iv, ...)
1415// br %cmp, header, exit
1416//
1417// Notably, we do not require that f(%iv), f(%iv.1), etc. be isolated groups of
1418// instructions. In other words, the instructions in f(%iv), f(%iv.1), etc. can
1419// be intermixed with eachother. The restriction imposed by this algorithm is
1420// that the relative order of the isomorphic instructions in f(%iv), f(%iv.1),
1421// etc. be the same.
1422//
1423// First, we collect the use set of %iv, excluding the other increment roots.
1424// This gives us f(%iv). Then we iterate over the loop instructions (scale-1)
1425// times, having collected the use set of f(%iv.(i+1)), during which we:
1426// - Ensure that the next unmatched instruction in f(%iv) is isomorphic to
1427// the next unmatched instruction in f(%iv.(i+1)).
1428// - Ensure that both matched instructions don't have any external users
1429// (with the exception of last-in-chain reduction instructions).
1430// - Track the (aliasing) write set, and other side effects, of all
1431// instructions that belong to future iterations that come before the matched
1432// instructions. If the matched instructions read from that write set, then
1433// f(%iv) or f(%iv.(i+1)) has some dependency on instructions in
1434// f(%iv.(j+1)) for some j > i, and we cannot reroll the loop. Similarly,
1435// if any of these future instructions had side effects (could not be
1436// speculatively executed), and so do the matched instructions, when we
1437// cannot reorder those side-effect-producing instructions, and rerolling
1438// fails.
1439//
1440// Finally, we make sure that all loop instructions are either loop increment
1441// roots, belong to simple latch code, parts of validated reductions, part of
1442// f(%iv) or part of some f(%iv.i). If all of that is true (and all reductions
1443// have been validated), then we reroll the loop.
1444bool LoopReroll::reroll(Instruction *IV, Loop *L, BasicBlock *Header,
1445 const SCEV *IterCount,
1446 ReductionTracker &Reductions) {
Lawrence Hudc8a83b2015-07-24 22:01:49 +00001447 DAGRootTracker DAGRoots(this, L, IV, SE, AA, TLI, IVToIncMap);
Hal Finkelbf45efd2013-11-16 23:59:05 +00001448
James Molloy5f255eb2015-01-29 13:48:05 +00001449 if (!DAGRoots.findRoots())
Hal Finkelbf45efd2013-11-16 23:59:05 +00001450 return false;
Hal Finkelbf45efd2013-11-16 23:59:05 +00001451 DEBUG(dbgs() << "LRR: Found all root induction increments for: " <<
James Molloy5f255eb2015-01-29 13:48:05 +00001452 *IV << "\n");
Lawrence Hudc8a83b2015-07-24 22:01:49 +00001453
James Molloy5f255eb2015-01-29 13:48:05 +00001454 if (!DAGRoots.validate(Reductions))
Hal Finkelbf45efd2013-11-16 23:59:05 +00001455 return false;
Hal Finkelbf45efd2013-11-16 23:59:05 +00001456 if (!Reductions.validateSelected())
1457 return false;
Hal Finkelbf45efd2013-11-16 23:59:05 +00001458 // At this point, we've validated the rerolling, and we're committed to
1459 // making changes!
1460
1461 Reductions.replaceSelected();
James Molloy5f255eb2015-01-29 13:48:05 +00001462 DAGRoots.replace(IterCount);
Hal Finkelbf45efd2013-11-16 23:59:05 +00001463
Hal Finkelbf45efd2013-11-16 23:59:05 +00001464 ++NumRerolledLoops;
1465 return true;
1466}
1467
1468bool LoopReroll::runOnLoop(Loop *L, LPPassManager &LPM) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +00001469 if (skipOptnoneFunction(L))
1470 return false;
1471
Chandler Carruth7b560d42015-09-09 17:55:00 +00001472 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Chandler Carruth4f8f3072015-01-17 14:16:18 +00001473 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruth2f1fd162015-08-17 02:08:17 +00001474 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001475 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruth73523022014-01-13 13:07:17 +00001476 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Hal Finkelbf45efd2013-11-16 23:59:05 +00001477
1478 BasicBlock *Header = L->getHeader();
1479 DEBUG(dbgs() << "LRR: F[" << Header->getParent()->getName() <<
1480 "] Loop %" << Header->getName() << " (" <<
1481 L->getNumBlocks() << " block(s))\n");
1482
1483 bool Changed = false;
1484
1485 // For now, we'll handle only single BB loops.
1486 if (L->getNumBlocks() > 1)
1487 return Changed;
1488
1489 if (!SE->hasLoopInvariantBackedgeTakenCount(L))
1490 return Changed;
1491
1492 const SCEV *LIBETC = SE->getBackedgeTakenCount(L);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001493 const SCEV *IterCount = SE->getAddExpr(LIBETC, SE->getOne(LIBETC->getType()));
Hal Finkelbf45efd2013-11-16 23:59:05 +00001494 DEBUG(dbgs() << "LRR: iteration count = " << *IterCount << "\n");
1495
1496 // First, we need to find the induction variable with respect to which we can
1497 // reroll (there may be several possible options).
1498 SmallInstructionVector PossibleIVs;
Lawrence Hudc8a83b2015-07-24 22:01:49 +00001499 IVToIncMap.clear();
Hal Finkelbf45efd2013-11-16 23:59:05 +00001500 collectPossibleIVs(L, PossibleIVs);
1501
1502 if (PossibleIVs.empty()) {
1503 DEBUG(dbgs() << "LRR: No possible IVs found\n");
1504 return Changed;
1505 }
1506
1507 ReductionTracker Reductions;
1508 collectPossibleReductions(L, Reductions);
1509
1510 // For each possible IV, collect the associated possible set of 'root' nodes
1511 // (i+1, i+2, etc.).
1512 for (SmallInstructionVector::iterator I = PossibleIVs.begin(),
1513 IE = PossibleIVs.end(); I != IE; ++I)
1514 if (reroll(*I, L, Header, IterCount, Reductions)) {
1515 Changed = true;
1516 break;
1517 }
1518
1519 return Changed;
1520}