1 //===-- LoopReroll.cpp - Loop rerolling pass ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass implements a simple loop reroller.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Scalar.h"
15 #include "llvm/ADT/MapVector.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallBitVector.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/Statistic.h"
20 #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"
26 #include "llvm/Analysis/TargetLibraryInfo.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/Dominators.h"
30 #include "llvm/IR/IntrinsicInst.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
35 #include "llvm/Transforms/Utils/Local.h"
36 #include "llvm/Transforms/Utils/LoopUtils.h"
40 #define DEBUG_TYPE "loop-reroll"
42 STATISTIC(NumRerolledLoops, "Number of rerolled loops");
44 static cl::opt<unsigned>
45 MaxInc("max-reroll-increment", cl::init(2048), cl::Hidden,
46 cl::desc("The maximum increment for loop rerolling"));
48 static cl::opt<unsigned>
49 NumToleratedFailedMatches("reroll-num-tolerated-failed-matches", cl::init(400),
51 cl::desc("The maximum number of failures to tolerate"
52 " during fuzzy matching. (default: 400)"));
54 // This loop re-rolling transformation aims to transform loops like this:
58 // for (int i = 0; i < 500; i += 3) {
65 // into a loop like this:
68 // for (int i = 0; i < 500; ++i)
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:
79 // %iv = phi [ (preheader, ...), (body, %iv.next) ]
81 // %iv.1 = add %iv, 1 <-- a root increment
83 // %iv.2 = add %iv, 2 <-- a root increment
85 // %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
88 // %iv.next = add %iv, scale
89 // %cmp = icmp(%iv, ...)
90 // br %cmp, header, exit
92 // where each f(i) is a set of instructions that, collectively, are a function
93 // only of i (and other loop-invariant values).
95 // As a special case, we can also reroll loops like this:
99 // for (int i = 0; i < 500; ++i) {
101 // x[3*i+1] = foo(0);
102 // x[3*i+2] = foo(0);
108 // void bar(int *x) {
109 // for (int i = 0; i < 1500; ++i)
113 // in which case, we're looking for inputs like this:
115 // %iv = phi [ (preheader, ...), (body, %iv.next) ]
116 // %scaled.iv = mul %iv, scale
118 // %scaled.iv.1 = add %scaled.iv, 1
120 // %scaled.iv.2 = add %scaled.iv, 2
122 // %scaled.iv.scale_m_1 = add %scaled.iv, scale-1
123 // f(%scaled.iv.scale_m_1)
125 // %iv.next = add %iv, 1
126 // %cmp = icmp(%iv, ...)
127 // br %cmp, header, exit
130 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
135 /// instructions that belong to all iterations.
140 class LoopReroll : public LoopPass {
142 static char ID; // Pass ID, replacement for typeid
143 LoopReroll() : LoopPass(ID) {
144 initializeLoopRerollPass(*PassRegistry::getPassRegistry());
147 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
149 void getAnalysisUsage(AnalysisUsage &AU) const override {
150 AU.addRequired<AAResultsWrapperPass>();
151 AU.addRequired<LoopInfoWrapperPass>();
152 AU.addPreserved<LoopInfoWrapperPass>();
153 AU.addRequired<DominatorTreeWrapperPass>();
154 AU.addPreserved<DominatorTreeWrapperPass>();
155 AU.addRequired<ScalarEvolutionWrapperPass>();
156 AU.addRequired<TargetLibraryInfoWrapperPass>();
163 TargetLibraryInfo *TLI;
167 typedef SmallVector<Instruction *, 16> SmallInstructionVector;
168 typedef SmallSet<Instruction *, 16> SmallInstructionSet;
170 // Map between induction variable and its increment
171 DenseMap<Instruction *, int64_t> IVToIncMap;
173 // A chain of isomorphic instructions, identified by a single-use PHI
174 // representing a reduction. Only the last value may be used outside the
176 struct SimpleLoopReduction {
177 SimpleLoopReduction(Instruction *P, Loop *L)
178 : Valid(false), Instructions(1, P) {
179 assert(isa<PHINode>(P) && "First reduction instruction must be a PHI");
187 Instruction *getPHI() const {
188 assert(Valid && "Using invalid reduction");
189 return Instructions.front();
192 Instruction *getReducedValue() const {
193 assert(Valid && "Using invalid reduction");
194 return Instructions.back();
197 Instruction *get(size_t i) const {
198 assert(Valid && "Using invalid reduction");
199 return Instructions[i+1];
202 Instruction *operator [] (size_t i) const { return get(i); }
204 // The size, ignoring the initial PHI.
205 size_t size() const {
206 assert(Valid && "Using invalid reduction");
207 return Instructions.size()-1;
210 typedef SmallInstructionVector::iterator iterator;
211 typedef SmallInstructionVector::const_iterator const_iterator;
214 assert(Valid && "Using invalid reduction");
215 return std::next(Instructions.begin());
218 const_iterator begin() const {
219 assert(Valid && "Using invalid reduction");
220 return std::next(Instructions.begin());
223 iterator end() { return Instructions.end(); }
224 const_iterator end() const { return Instructions.end(); }
228 SmallInstructionVector Instructions;
233 // The set of all reductions, and state tracking of possible reductions
234 // during loop instruction processing.
235 struct ReductionTracker {
236 typedef SmallVector<SimpleLoopReduction, 16> SmallReductionVector;
238 // Add a new possible reduction.
239 void addSLR(SimpleLoopReduction &SLR) { PossibleReds.push_back(SLR); }
241 // Setup to track possible reductions corresponding to the provided
242 // rerolling scale. Only reductions with a number of non-PHI instructions
243 // that is divisible by the scale are considered. Three instructions sets
245 // - A set of all possible instructions in eligible reductions.
246 // - A set of all PHIs in eligible reductions
247 // - A set of all reduced values (last instructions) in eligible
249 void restrictToScale(uint64_t Scale,
250 SmallInstructionSet &PossibleRedSet,
251 SmallInstructionSet &PossibleRedPHISet,
252 SmallInstructionSet &PossibleRedLastSet) {
253 PossibleRedIdx.clear();
254 PossibleRedIter.clear();
257 for (unsigned i = 0, e = PossibleReds.size(); i != e; ++i)
258 if (PossibleReds[i].size() % Scale == 0) {
259 PossibleRedLastSet.insert(PossibleReds[i].getReducedValue());
260 PossibleRedPHISet.insert(PossibleReds[i].getPHI());
262 PossibleRedSet.insert(PossibleReds[i].getPHI());
263 PossibleRedIdx[PossibleReds[i].getPHI()] = i;
264 for (Instruction *J : PossibleReds[i]) {
265 PossibleRedSet.insert(J);
266 PossibleRedIdx[J] = i;
271 // The functions below are used while processing the loop instructions.
273 // Are the two instructions both from reductions, and furthermore, from
274 // the same reduction?
275 bool isPairInSame(Instruction *J1, Instruction *J2) {
276 DenseMap<Instruction *, int>::iterator J1I = PossibleRedIdx.find(J1);
277 if (J1I != PossibleRedIdx.end()) {
278 DenseMap<Instruction *, int>::iterator J2I = PossibleRedIdx.find(J2);
279 if (J2I != PossibleRedIdx.end() && J1I->second == J2I->second)
286 // The two provided instructions, the first from the base iteration, and
287 // the second from iteration i, form a matched pair. If these are part of
288 // a reduction, record that fact.
289 void recordPair(Instruction *J1, Instruction *J2, unsigned i) {
290 if (PossibleRedIdx.count(J1)) {
291 assert(PossibleRedIdx.count(J2) &&
292 "Recording reduction vs. non-reduction instruction?");
294 PossibleRedIter[J1] = 0;
295 PossibleRedIter[J2] = i;
297 int Idx = PossibleRedIdx[J1];
298 assert(Idx == PossibleRedIdx[J2] &&
299 "Recording pair from different reductions?");
304 // The functions below can be called after we've finished processing all
305 // instructions in the loop, and we know which reductions were selected.
307 bool validateSelected();
308 void replaceSelected();
311 // The vector of all possible reductions (for any scale).
312 SmallReductionVector PossibleReds;
314 DenseMap<Instruction *, int> PossibleRedIdx;
315 DenseMap<Instruction *, int> PossibleRedIter;
319 // A DAGRootSet models an induction variable being used in a rerollable
320 // loop. For example,
326 // Base instruction -> i*3
329 // ST[y1] +1 +2 <-- Roots
333 // There may be multiple DAGRoots, for example:
335 // x[i*2+0] = ... (1)
336 // x[i*2+1] = ... (1)
337 // x[i*2+4] = ... (2)
338 // x[i*2+5] = ... (2)
339 // x[(i+1234)*2+5678] = ... (3)
340 // x[(i+1234)*2+5679] = ... (3)
342 // The loop will be rerolled by adding a new loop induction variable,
343 // one for the Base instruction in each DAGRootSet.
346 Instruction *BaseInst;
347 SmallInstructionVector Roots;
348 // The instructions between IV and BaseInst (but not including BaseInst).
349 SmallInstructionSet SubsumedInsts;
352 // The set of all DAG roots, and state tracking of all roots
353 // for a particular induction variable.
354 struct DAGRootTracker {
355 DAGRootTracker(LoopReroll *Parent, Loop *L, Instruction *IV,
356 ScalarEvolution *SE, AliasAnalysis *AA,
357 TargetLibraryInfo *TLI, DominatorTree *DT, LoopInfo *LI,
359 DenseMap<Instruction *, int64_t> &IncrMap)
360 : Parent(Parent), L(L), SE(SE), AA(AA), TLI(TLI), DT(DT), LI(LI),
361 PreserveLCSSA(PreserveLCSSA), IV(IV), IVToIncMap(IncrMap) {}
363 /// Stage 1: Find all the DAG roots for the induction variable.
365 /// Stage 2: Validate if the found roots are valid.
366 bool validate(ReductionTracker &Reductions);
367 /// Stage 3: Assuming validate() returned true, perform the
369 /// @param IterCount The maximum iteration count of L.
370 void replace(const SCEV *IterCount);
373 typedef MapVector<Instruction*, SmallBitVector> UsesTy;
375 bool findRootsRecursive(Instruction *IVU,
376 SmallInstructionSet SubsumedInsts);
377 bool findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts);
378 bool collectPossibleRoots(Instruction *Base,
379 std::map<int64_t,Instruction*> &Roots);
381 bool collectUsedInstructions(SmallInstructionSet &PossibleRedSet);
382 void collectInLoopUserSet(const SmallInstructionVector &Roots,
383 const SmallInstructionSet &Exclude,
384 const SmallInstructionSet &Final,
385 DenseSet<Instruction *> &Users);
386 void collectInLoopUserSet(Instruction *Root,
387 const SmallInstructionSet &Exclude,
388 const SmallInstructionSet &Final,
389 DenseSet<Instruction *> &Users);
391 UsesTy::iterator nextInstr(int Val, UsesTy &In,
392 const SmallInstructionSet &Exclude,
393 UsesTy::iterator *StartI=nullptr);
394 bool isBaseInst(Instruction *I);
395 bool isRootInst(Instruction *I);
396 bool instrDependsOn(Instruction *I,
397 UsesTy::iterator Start,
398 UsesTy::iterator End);
402 // Members of Parent, replicated here for brevity.
406 TargetLibraryInfo *TLI;
411 // The loop induction variable.
415 // Loop reroll count; if Inc == 1, this records the scaling applied
416 // to the indvar: a[i*2+0] = ...; a[i*2+1] = ... ;
417 // If Inc is not 1, Scale = Inc.
419 // The roots themselves.
420 SmallVector<DAGRootSet,16> RootSets;
421 // All increment instructions for IV.
422 SmallInstructionVector LoopIncs;
423 // Map of all instructions in the loop (in order) to the iterations
424 // they are used in (or specially, IL_All for instructions
425 // used in the loop increment mechanism).
427 // Map between induction variable and its increment
428 DenseMap<Instruction *, int64_t> &IVToIncMap;
431 void collectPossibleIVs(Loop *L, SmallInstructionVector &PossibleIVs);
432 void collectPossibleReductions(Loop *L,
433 ReductionTracker &Reductions);
434 bool reroll(Instruction *IV, Loop *L, BasicBlock *Header, const SCEV *IterCount,
435 ReductionTracker &Reductions);
439 char LoopReroll::ID = 0;
440 INITIALIZE_PASS_BEGIN(LoopReroll, "loop-reroll", "Reroll loops", false, false)
441 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
442 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
443 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
444 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
445 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
446 INITIALIZE_PASS_END(LoopReroll, "loop-reroll", "Reroll loops", false, false)
448 Pass *llvm::createLoopRerollPass() {
449 return new LoopReroll;
452 // Returns true if the provided instruction is used outside the given loop.
453 // This operates like Instruction::isUsedOutsideOfBlock, but considers PHIs in
454 // non-loop blocks to be outside the loop.
455 static bool hasUsesOutsideLoop(Instruction *I, Loop *L) {
456 for (User *U : I->users()) {
457 if (!L->contains(cast<Instruction>(U)))
463 // Collect the list of loop induction variables with respect to which it might
464 // be possible to reroll the loop.
465 void LoopReroll::collectPossibleIVs(Loop *L,
466 SmallInstructionVector &PossibleIVs) {
467 BasicBlock *Header = L->getHeader();
468 for (BasicBlock::iterator I = Header->begin(),
469 IE = Header->getFirstInsertionPt(); I != IE; ++I) {
470 if (!isa<PHINode>(I))
472 if (!I->getType()->isIntegerTy())
475 if (const SCEVAddRecExpr *PHISCEV =
476 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(&*I))) {
477 if (PHISCEV->getLoop() != L)
479 if (!PHISCEV->isAffine())
481 if (const SCEVConstant *IncSCEV =
482 dyn_cast<SCEVConstant>(PHISCEV->getStepRecurrence(*SE))) {
483 const APInt &AInt = IncSCEV->getAPInt().abs();
484 if (IncSCEV->getValue()->isZero() || AInt.uge(MaxInc))
486 IVToIncMap[&*I] = IncSCEV->getValue()->getSExtValue();
487 DEBUG(dbgs() << "LRR: Possible IV: " << *I << " = " << *PHISCEV
489 PossibleIVs.push_back(&*I);
495 // Add the remainder of the reduction-variable chain to the instruction vector
496 // (the initial PHINode has already been added). If successful, the object is
498 void LoopReroll::SimpleLoopReduction::add(Loop *L) {
499 assert(!Valid && "Cannot add to an already-valid chain");
501 // The reduction variable must be a chain of single-use instructions
502 // (including the PHI), except for the last value (which is used by the PHI
503 // and also outside the loop).
504 Instruction *C = Instructions.front();
509 C = cast<Instruction>(*C->user_begin());
510 if (C->hasOneUse()) {
511 if (!C->isBinaryOp())
514 if (!(isa<PHINode>(Instructions.back()) ||
515 C->isSameOperationAs(Instructions.back())))
518 Instructions.push_back(C);
520 } while (C->hasOneUse());
522 if (Instructions.size() < 2 ||
523 !C->isSameOperationAs(Instructions.back()) ||
527 // C is now the (potential) last instruction in the reduction chain.
528 for (User *U : C->users()) {
529 // The only in-loop user can be the initial PHI.
530 if (L->contains(cast<Instruction>(U)))
531 if (cast<Instruction>(U) != Instructions.front())
535 Instructions.push_back(C);
539 // Collect the vector of possible reduction variables.
540 void LoopReroll::collectPossibleReductions(Loop *L,
541 ReductionTracker &Reductions) {
542 BasicBlock *Header = L->getHeader();
543 for (BasicBlock::iterator I = Header->begin(),
544 IE = Header->getFirstInsertionPt(); I != IE; ++I) {
545 if (!isa<PHINode>(I))
547 if (!I->getType()->isSingleValueType())
550 SimpleLoopReduction SLR(&*I, L);
554 DEBUG(dbgs() << "LRR: Possible reduction: " << *I << " (with " <<
555 SLR.size() << " chained instructions)\n");
556 Reductions.addSLR(SLR);
560 // Collect the set of all users of the provided root instruction. This set of
561 // users contains not only the direct users of the root instruction, but also
562 // all users of those users, and so on. There are two exceptions:
564 // 1. Instructions in the set of excluded instructions are never added to the
565 // use set (even if they are users). This is used, for example, to exclude
566 // including root increments in the use set of the primary IV.
568 // 2. Instructions in the set of final instructions are added to the use set
569 // if they are users, but their users are not added. This is used, for
570 // example, to prevent a reduction update from forcing all later reduction
571 // updates into the use set.
572 void LoopReroll::DAGRootTracker::collectInLoopUserSet(
573 Instruction *Root, const SmallInstructionSet &Exclude,
574 const SmallInstructionSet &Final,
575 DenseSet<Instruction *> &Users) {
576 SmallInstructionVector Queue(1, Root);
577 while (!Queue.empty()) {
578 Instruction *I = Queue.pop_back_val();
579 if (!Users.insert(I).second)
583 for (Use &U : I->uses()) {
584 Instruction *User = cast<Instruction>(U.getUser());
585 if (PHINode *PN = dyn_cast<PHINode>(User)) {
586 // Ignore "wrap-around" uses to PHIs of this loop's header.
587 if (PN->getIncomingBlock(U) == L->getHeader())
591 if (L->contains(User) && !Exclude.count(User)) {
592 Queue.push_back(User);
596 // We also want to collect single-user "feeder" values.
597 for (User::op_iterator OI = I->op_begin(),
598 OIE = I->op_end(); OI != OIE; ++OI) {
599 if (Instruction *Op = dyn_cast<Instruction>(*OI))
600 if (Op->hasOneUse() && L->contains(Op) && !Exclude.count(Op) &&
607 // Collect all of the users of all of the provided root instructions (combined
608 // into a single set).
609 void LoopReroll::DAGRootTracker::collectInLoopUserSet(
610 const SmallInstructionVector &Roots,
611 const SmallInstructionSet &Exclude,
612 const SmallInstructionSet &Final,
613 DenseSet<Instruction *> &Users) {
614 for (SmallInstructionVector::const_iterator I = Roots.begin(),
615 IE = Roots.end(); I != IE; ++I)
616 collectInLoopUserSet(*I, Exclude, Final, Users);
619 static bool isSimpleLoadStore(Instruction *I) {
620 if (LoadInst *LI = dyn_cast<LoadInst>(I))
621 return LI->isSimple();
622 if (StoreInst *SI = dyn_cast<StoreInst>(I))
623 return SI->isSimple();
624 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
625 return !MI->isVolatile();
629 /// Return true if IVU is a "simple" arithmetic operation.
630 /// This is used for narrowing the search space for DAGRoots; only arithmetic
631 /// and GEPs can be part of a DAGRoot.
632 static bool isSimpleArithmeticOp(User *IVU) {
633 if (Instruction *I = dyn_cast<Instruction>(IVU)) {
634 switch (I->getOpcode()) {
635 default: return false;
636 case Instruction::Add:
637 case Instruction::Sub:
638 case Instruction::Mul:
639 case Instruction::Shl:
640 case Instruction::AShr:
641 case Instruction::LShr:
642 case Instruction::GetElementPtr:
643 case Instruction::Trunc:
644 case Instruction::ZExt:
645 case Instruction::SExt:
652 static bool isLoopIncrement(User *U, Instruction *IV) {
653 BinaryOperator *BO = dyn_cast<BinaryOperator>(U);
654 if (!BO || BO->getOpcode() != Instruction::Add)
657 for (auto *UU : BO->users()) {
658 PHINode *PN = dyn_cast<PHINode>(UU);
665 bool LoopReroll::DAGRootTracker::
666 collectPossibleRoots(Instruction *Base, std::map<int64_t,Instruction*> &Roots) {
667 SmallInstructionVector BaseUsers;
669 for (auto *I : Base->users()) {
670 ConstantInt *CI = nullptr;
672 if (isLoopIncrement(I, IV)) {
673 LoopIncs.push_back(cast<Instruction>(I));
677 // The root nodes must be either GEPs, ORs or ADDs.
678 if (auto *BO = dyn_cast<BinaryOperator>(I)) {
679 if (BO->getOpcode() == Instruction::Add ||
680 BO->getOpcode() == Instruction::Or)
681 CI = dyn_cast<ConstantInt>(BO->getOperand(1));
682 } else if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
683 Value *LastOperand = GEP->getOperand(GEP->getNumOperands()-1);
684 CI = dyn_cast<ConstantInt>(LastOperand);
688 if (Instruction *II = dyn_cast<Instruction>(I)) {
689 BaseUsers.push_back(II);
692 DEBUG(dbgs() << "LRR: Aborting due to non-instruction: " << *I << "\n");
697 int64_t V = std::abs(CI->getValue().getSExtValue());
698 if (Roots.find(V) != Roots.end())
699 // No duplicates, please.
702 Roots[V] = cast<Instruction>(I);
708 // If we found non-loop-inc, non-root users of Base, assume they are
709 // for the zeroth root index. This is because "add %a, 0" gets optimized
711 if (BaseUsers.size()) {
712 if (Roots.find(0) != Roots.end()) {
713 DEBUG(dbgs() << "LRR: Multiple roots found for base - aborting!\n");
719 // Calculate the number of users of the base, or lowest indexed, iteration.
720 unsigned NumBaseUses = BaseUsers.size();
721 if (NumBaseUses == 0)
722 NumBaseUses = Roots.begin()->second->getNumUses();
724 // Check that every node has the same number of users.
725 for (auto &KV : Roots) {
728 if (KV.second->getNumUses() != NumBaseUses) {
729 DEBUG(dbgs() << "LRR: Aborting - Root and Base #users not the same: "
730 << "#Base=" << NumBaseUses << ", #Root=" <<
731 KV.second->getNumUses() << "\n");
739 bool LoopReroll::DAGRootTracker::
740 findRootsRecursive(Instruction *I, SmallInstructionSet SubsumedInsts) {
741 // Does the user look like it could be part of a root set?
742 // All its users must be simple arithmetic ops.
743 if (I->getNumUses() > IL_MaxRerollIterations)
746 if ((I->getOpcode() == Instruction::Mul ||
747 I->getOpcode() == Instruction::PHI) &&
749 findRootsBase(I, SubsumedInsts))
752 SubsumedInsts.insert(I);
754 for (User *V : I->users()) {
755 Instruction *I = dyn_cast<Instruction>(V);
756 if (std::find(LoopIncs.begin(), LoopIncs.end(), I) != LoopIncs.end())
759 if (!I || !isSimpleArithmeticOp(I) ||
760 !findRootsRecursive(I, SubsumedInsts))
766 bool LoopReroll::DAGRootTracker::
767 findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts) {
769 // The base instruction needs to be a multiply so
770 // that we can erase it.
771 if (IVU->getOpcode() != Instruction::Mul &&
772 IVU->getOpcode() != Instruction::PHI)
775 std::map<int64_t, Instruction*> V;
776 if (!collectPossibleRoots(IVU, V))
779 // If we didn't get a root for index zero, then IVU must be
781 if (V.find(0) == V.end())
782 SubsumedInsts.insert(IVU);
784 // Partition the vector into monotonically increasing indexes.
786 DRS.BaseInst = nullptr;
790 DRS.BaseInst = KV.second;
791 DRS.SubsumedInsts = SubsumedInsts;
792 } else if (DRS.Roots.empty()) {
793 DRS.Roots.push_back(KV.second);
794 } else if (V.find(KV.first - 1) != V.end()) {
795 DRS.Roots.push_back(KV.second);
797 // Linear sequence terminated.
798 RootSets.push_back(DRS);
799 DRS.BaseInst = KV.second;
800 DRS.SubsumedInsts = SubsumedInsts;
804 RootSets.push_back(DRS);
809 bool LoopReroll::DAGRootTracker::findRoots() {
810 Inc = IVToIncMap[IV];
812 assert(RootSets.empty() && "Unclean state!");
813 if (std::abs(Inc) == 1) {
814 for (auto *IVU : IV->users()) {
815 if (isLoopIncrement(IVU, IV))
816 LoopIncs.push_back(cast<Instruction>(IVU));
818 if (!findRootsRecursive(IV, SmallInstructionSet()))
820 LoopIncs.push_back(IV);
822 if (!findRootsBase(IV, SmallInstructionSet()))
826 // Ensure all sets have the same size.
827 if (RootSets.empty()) {
828 DEBUG(dbgs() << "LRR: Aborting because no root sets found!\n");
831 for (auto &V : RootSets) {
832 if (V.Roots.empty() || V.Roots.size() != RootSets[0].Roots.size()) {
834 << "LRR: Aborting because not all root sets have the same size\n");
839 // And ensure all loop iterations are consecutive. We rely on std::map
840 // providing ordered traversal.
841 for (auto &V : RootSets) {
842 const auto *ADR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(V.BaseInst));
846 // Consider a DAGRootSet with N-1 roots (so N different values including
848 // Define d = Roots[0] - BaseInst, which should be the same as
849 // Roots[I] - Roots[I-1] for all I in [1..N).
850 // Define D = BaseInst@J - BaseInst@J-1, where "@J" means the value at the
853 // Now, For the loop iterations to be consecutive:
856 unsigned N = V.Roots.size() + 1;
857 const SCEV *StepSCEV = SE->getMinusSCEV(SE->getSCEV(V.Roots[0]), ADR);
858 const SCEV *ScaleSCEV = SE->getConstant(StepSCEV->getType(), N);
859 if (ADR->getStepRecurrence(*SE) != SE->getMulExpr(StepSCEV, ScaleSCEV)) {
860 DEBUG(dbgs() << "LRR: Aborting because iterations are not consecutive\n");
864 Scale = RootSets[0].Roots.size() + 1;
866 if (Scale > IL_MaxRerollIterations) {
867 DEBUG(dbgs() << "LRR: Aborting - too many iterations found. "
868 << "#Found=" << Scale << ", #Max=" << IL_MaxRerollIterations
873 DEBUG(dbgs() << "LRR: Successfully found roots: Scale=" << Scale << "\n");
878 bool LoopReroll::DAGRootTracker::collectUsedInstructions(SmallInstructionSet &PossibleRedSet) {
879 // Populate the MapVector with all instructions in the block, in order first,
880 // so we can iterate over the contents later in perfect order.
881 for (auto &I : *L->getHeader()) {
882 Uses[&I].resize(IL_End);
885 SmallInstructionSet Exclude;
886 for (auto &DRS : RootSets) {
887 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
888 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
889 Exclude.insert(DRS.BaseInst);
891 Exclude.insert(LoopIncs.begin(), LoopIncs.end());
893 for (auto &DRS : RootSets) {
894 DenseSet<Instruction*> VBase;
895 collectInLoopUserSet(DRS.BaseInst, Exclude, PossibleRedSet, VBase);
896 for (auto *I : VBase) {
901 for (auto *Root : DRS.Roots) {
902 DenseSet<Instruction*> V;
903 collectInLoopUserSet(Root, Exclude, PossibleRedSet, V);
905 // While we're here, check the use sets are the same size.
906 if (V.size() != VBase.size()) {
907 DEBUG(dbgs() << "LRR: Aborting - use sets are different sizes\n");
917 // Make sure our subsumed instructions are remembered too.
918 for (auto *I : DRS.SubsumedInsts) {
923 // Make sure the loop increments are also accounted for.
926 for (auto &DRS : RootSets) {
927 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
928 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
929 Exclude.insert(DRS.BaseInst);
932 DenseSet<Instruction*> V;
933 collectInLoopUserSet(LoopIncs, Exclude, PossibleRedSet, V);
942 /// Get the next instruction in "In" that is a member of set Val.
943 /// Start searching from StartI, and do not return anything in Exclude.
944 /// If StartI is not given, start from In.begin().
945 LoopReroll::DAGRootTracker::UsesTy::iterator
946 LoopReroll::DAGRootTracker::nextInstr(int Val, UsesTy &In,
947 const SmallInstructionSet &Exclude,
948 UsesTy::iterator *StartI) {
949 UsesTy::iterator I = StartI ? *StartI : In.begin();
950 while (I != In.end() && (I->second.test(Val) == 0 ||
951 Exclude.count(I->first) != 0))
956 bool LoopReroll::DAGRootTracker::isBaseInst(Instruction *I) {
957 for (auto &DRS : RootSets) {
958 if (DRS.BaseInst == I)
964 bool LoopReroll::DAGRootTracker::isRootInst(Instruction *I) {
965 for (auto &DRS : RootSets) {
966 if (std::find(DRS.Roots.begin(), DRS.Roots.end(), I) != DRS.Roots.end())
972 /// Return true if instruction I depends on any instruction between
974 bool LoopReroll::DAGRootTracker::instrDependsOn(Instruction *I,
975 UsesTy::iterator Start,
976 UsesTy::iterator End) {
977 for (auto *U : I->users()) {
978 for (auto It = Start; It != End; ++It)
985 static bool isIgnorableInst(const Instruction *I) {
986 if (isa<DbgInfoIntrinsic>(I))
988 const IntrinsicInst* II = dyn_cast<IntrinsicInst>(I);
991 switch (II->getIntrinsicID()) {
994 case llvm::Intrinsic::annotation:
995 case Intrinsic::ptr_annotation:
996 case Intrinsic::var_annotation:
997 // TODO: the following intrinsics may also be whitelisted:
998 // lifetime_start, lifetime_end, invariant_start, invariant_end
1004 bool LoopReroll::DAGRootTracker::validate(ReductionTracker &Reductions) {
1005 // We now need to check for equivalence of the use graph of each root with
1006 // that of the primary induction variable (excluding the roots). Our goal
1007 // here is not to solve the full graph isomorphism problem, but rather to
1008 // catch common cases without a lot of work. As a result, we will assume
1009 // that the relative order of the instructions in each unrolled iteration
1010 // is the same (although we will not make an assumption about how the
1011 // different iterations are intermixed). Note that while the order must be
1012 // the same, the instructions may not be in the same basic block.
1014 // An array of just the possible reductions for this scale factor. When we
1015 // collect the set of all users of some root instructions, these reduction
1016 // instructions are treated as 'final' (their uses are not considered).
1017 // This is important because we don't want the root use set to search down
1018 // the reduction chain.
1019 SmallInstructionSet PossibleRedSet;
1020 SmallInstructionSet PossibleRedLastSet;
1021 SmallInstructionSet PossibleRedPHISet;
1022 Reductions.restrictToScale(Scale, PossibleRedSet,
1023 PossibleRedPHISet, PossibleRedLastSet);
1025 // Populate "Uses" with where each instruction is used.
1026 if (!collectUsedInstructions(PossibleRedSet))
1029 // Make sure we mark the reduction PHIs as used in all iterations.
1030 for (auto *I : PossibleRedPHISet) {
1031 Uses[I].set(IL_All);
1034 // Make sure all instructions in the loop are in one and only one
1036 for (auto &KV : Uses) {
1037 if (KV.second.count() != 1 && !isIgnorableInst(KV.first)) {
1038 DEBUG(dbgs() << "LRR: Aborting - instruction is not used in 1 iteration: "
1039 << *KV.first << " (#uses=" << KV.second.count() << ")\n");
1045 for (auto &KV : Uses) {
1046 dbgs() << "LRR: " << KV.second.find_first() << "\t" << *KV.first << "\n";
1050 for (unsigned Iter = 1; Iter < Scale; ++Iter) {
1051 // In addition to regular aliasing information, we need to look for
1052 // instructions from later (future) iterations that have side effects
1053 // preventing us from reordering them past other instructions with side
1055 bool FutureSideEffects = false;
1056 AliasSetTracker AST(*AA);
1057 // The map between instructions in f(%iv.(i+1)) and f(%iv).
1058 DenseMap<Value *, Value *> BaseMap;
1060 // Compare iteration Iter to the base.
1061 SmallInstructionSet Visited;
1062 auto BaseIt = nextInstr(0, Uses, Visited);
1063 auto RootIt = nextInstr(Iter, Uses, Visited);
1064 auto LastRootIt = Uses.begin();
1066 while (BaseIt != Uses.end() && RootIt != Uses.end()) {
1067 Instruction *BaseInst = BaseIt->first;
1068 Instruction *RootInst = RootIt->first;
1070 // Skip over the IV or root instructions; only match their users.
1071 bool Continue = false;
1072 if (isBaseInst(BaseInst)) {
1073 Visited.insert(BaseInst);
1074 BaseIt = nextInstr(0, Uses, Visited);
1077 if (isRootInst(RootInst)) {
1078 LastRootIt = RootIt;
1079 Visited.insert(RootInst);
1080 RootIt = nextInstr(Iter, Uses, Visited);
1083 if (Continue) continue;
1085 if (!BaseInst->isSameOperationAs(RootInst)) {
1086 // Last chance saloon. We don't try and solve the full isomorphism
1087 // problem, but try and at least catch the case where two instructions
1088 // *of different types* are round the wrong way. We won't be able to
1089 // efficiently tell, given two ADD instructions, which way around we
1090 // should match them, but given an ADD and a SUB, we can at least infer
1091 // which one is which.
1093 // This should allow us to deal with a greater subset of the isomorphism
1094 // problem. It does however change a linear algorithm into a quadratic
1095 // one, so limit the number of probes we do.
1096 auto TryIt = RootIt;
1097 unsigned N = NumToleratedFailedMatches;
1098 while (TryIt != Uses.end() &&
1099 !BaseInst->isSameOperationAs(TryIt->first) &&
1102 TryIt = nextInstr(Iter, Uses, Visited, &TryIt);
1105 if (TryIt == Uses.end() || TryIt == RootIt ||
1106 instrDependsOn(TryIt->first, RootIt, TryIt)) {
1107 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1108 " vs. " << *RootInst << "\n");
1113 RootInst = TryIt->first;
1116 // All instructions between the last root and this root
1117 // may belong to some other iteration. If they belong to a
1118 // future iteration, then they're dangerous to alias with.
1120 // Note that because we allow a limited amount of flexibility in the order
1121 // that we visit nodes, LastRootIt might be *before* RootIt, in which
1122 // case we've already checked this set of instructions so we shouldn't
1124 for (; LastRootIt < RootIt; ++LastRootIt) {
1125 Instruction *I = LastRootIt->first;
1126 if (LastRootIt->second.find_first() < (int)Iter)
1128 if (I->mayWriteToMemory())
1130 // Note: This is specifically guarded by a check on isa<PHINode>,
1131 // which while a valid (somewhat arbitrary) micro-optimization, is
1132 // needed because otherwise isSafeToSpeculativelyExecute returns
1133 // false on PHI nodes.
1134 if (!isa<PHINode>(I) && !isSimpleLoadStore(I) &&
1135 !isSafeToSpeculativelyExecute(I))
1136 // Intervening instructions cause side effects.
1137 FutureSideEffects = true;
1140 // Make sure that this instruction, which is in the use set of this
1141 // root instruction, does not also belong to the base set or the set of
1142 // some other root instruction.
1143 if (RootIt->second.count() > 1) {
1144 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1145 " vs. " << *RootInst << " (prev. case overlap)\n");
1149 // Make sure that we don't alias with any instruction in the alias set
1150 // tracker. If we do, then we depend on a future iteration, and we
1152 if (RootInst->mayReadFromMemory())
1153 for (auto &K : AST) {
1154 if (K.aliasesUnknownInst(RootInst, *AA)) {
1155 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1156 " vs. " << *RootInst << " (depends on future store)\n");
1161 // If we've past an instruction from a future iteration that may have
1162 // side effects, and this instruction might also, then we can't reorder
1163 // them, and this matching fails. As an exception, we allow the alias
1164 // set tracker to handle regular (simple) load/store dependencies.
1165 if (FutureSideEffects && ((!isSimpleLoadStore(BaseInst) &&
1166 !isSafeToSpeculativelyExecute(BaseInst)) ||
1167 (!isSimpleLoadStore(RootInst) &&
1168 !isSafeToSpeculativelyExecute(RootInst)))) {
1169 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1170 " vs. " << *RootInst <<
1171 " (side effects prevent reordering)\n");
1175 // For instructions that are part of a reduction, if the operation is
1176 // associative, then don't bother matching the operands (because we
1177 // already know that the instructions are isomorphic, and the order
1178 // within the iteration does not matter). For non-associative reductions,
1179 // we do need to match the operands, because we need to reject
1180 // out-of-order instructions within an iteration!
1181 // For example (assume floating-point addition), we need to reject this:
1182 // x += a[i]; x += b[i];
1183 // x += a[i+1]; x += b[i+1];
1184 // x += b[i+2]; x += a[i+2];
1185 bool InReduction = Reductions.isPairInSame(BaseInst, RootInst);
1187 if (!(InReduction && BaseInst->isAssociative())) {
1188 bool Swapped = false, SomeOpMatched = false;
1189 for (unsigned j = 0; j < BaseInst->getNumOperands(); ++j) {
1190 Value *Op2 = RootInst->getOperand(j);
1192 // If this is part of a reduction (and the operation is not
1193 // associatve), then we match all operands, but not those that are
1194 // part of the reduction.
1196 if (Instruction *Op2I = dyn_cast<Instruction>(Op2))
1197 if (Reductions.isPairInSame(RootInst, Op2I))
1200 DenseMap<Value *, Value *>::iterator BMI = BaseMap.find(Op2);
1201 if (BMI != BaseMap.end()) {
1204 for (auto &DRS : RootSets) {
1205 if (DRS.Roots[Iter-1] == (Instruction*) Op2) {
1212 if (BaseInst->getOperand(Swapped ? unsigned(!j) : j) != Op2) {
1213 // If we've not already decided to swap the matched operands, and
1214 // we've not already matched our first operand (note that we could
1215 // have skipped matching the first operand because it is part of a
1216 // reduction above), and the instruction is commutative, then try
1217 // the swapped match.
1218 if (!Swapped && BaseInst->isCommutative() && !SomeOpMatched &&
1219 BaseInst->getOperand(!j) == Op2) {
1222 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
1223 << " vs. " << *RootInst << " (operand " << j << ")\n");
1228 SomeOpMatched = true;
1232 if ((!PossibleRedLastSet.count(BaseInst) &&
1233 hasUsesOutsideLoop(BaseInst, L)) ||
1234 (!PossibleRedLastSet.count(RootInst) &&
1235 hasUsesOutsideLoop(RootInst, L))) {
1236 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1237 " vs. " << *RootInst << " (uses outside loop)\n");
1241 Reductions.recordPair(BaseInst, RootInst, Iter);
1242 BaseMap.insert(std::make_pair(RootInst, BaseInst));
1244 LastRootIt = RootIt;
1245 Visited.insert(BaseInst);
1246 Visited.insert(RootInst);
1247 BaseIt = nextInstr(0, Uses, Visited);
1248 RootIt = nextInstr(Iter, Uses, Visited);
1250 assert (BaseIt == Uses.end() && RootIt == Uses.end() &&
1251 "Mismatched set sizes!");
1254 DEBUG(dbgs() << "LRR: Matched all iteration increments for " <<
1260 void LoopReroll::DAGRootTracker::replace(const SCEV *IterCount) {
1261 BasicBlock *Header = L->getHeader();
1262 // Remove instructions associated with non-base iterations.
1263 for (BasicBlock::reverse_iterator J = Header->rbegin();
1264 J != Header->rend();) {
1265 unsigned I = Uses[&*J].find_first();
1266 if (I > 0 && I < IL_All) {
1267 Instruction *D = &*J;
1268 DEBUG(dbgs() << "LRR: removing: " << *D << "\n");
1269 D->eraseFromParent();
1275 bool Negative = IVToIncMap[IV] < 0;
1276 const DataLayout &DL = Header->getModule()->getDataLayout();
1278 // We need to create a new induction variable for each different BaseInst.
1279 for (auto &DRS : RootSets) {
1280 // Insert the new induction variable.
1281 const SCEVAddRecExpr *RealIVSCEV =
1282 cast<SCEVAddRecExpr>(SE->getSCEV(DRS.BaseInst));
1283 const SCEV *Start = RealIVSCEV->getStart();
1284 const SCEVAddRecExpr *H = cast<SCEVAddRecExpr>(SE->getAddRecExpr(
1285 Start, SE->getConstant(RealIVSCEV->getType(), Negative ? -1 : 1), L,
1286 SCEV::FlagAnyWrap));
1287 { // Limit the lifetime of SCEVExpander.
1288 SCEVExpander Expander(*SE, DL, "reroll");
1289 Value *NewIV = Expander.expandCodeFor(H, IV->getType(), &Header->front());
1291 for (auto &KV : Uses) {
1292 if (KV.second.find_first() == 0)
1293 KV.first->replaceUsesOfWith(DRS.BaseInst, NewIV);
1296 if (BranchInst *BI = dyn_cast<BranchInst>(Header->getTerminator())) {
1297 // FIXME: Why do we need this check?
1298 if (Uses[BI].find_first() == IL_All) {
1299 const SCEV *ICSCEV = RealIVSCEV->evaluateAtIteration(IterCount, *SE);
1301 // Iteration count SCEV minus 1
1302 const SCEV *ICMinus1SCEV = SE->getMinusSCEV(
1303 ICSCEV, SE->getConstant(ICSCEV->getType(), Negative ? -1 : 1));
1305 Value *ICMinus1; // Iteration count minus 1
1306 if (isa<SCEVConstant>(ICMinus1SCEV)) {
1307 ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(), BI);
1309 BasicBlock *Preheader = L->getLoopPreheader();
1311 Preheader = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA);
1313 ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(),
1314 Preheader->getTerminator());
1318 new ICmpInst(BI, CmpInst::ICMP_EQ, NewIV, ICMinus1, "exitcond");
1319 BI->setCondition(Cond);
1321 if (BI->getSuccessor(1) != Header)
1322 BI->swapSuccessors();
1328 SimplifyInstructionsInBlock(Header, TLI);
1329 DeleteDeadPHIs(Header, TLI);
1332 // Validate the selected reductions. All iterations must have an isomorphic
1333 // part of the reduction chain and, for non-associative reductions, the chain
1334 // entries must appear in order.
1335 bool LoopReroll::ReductionTracker::validateSelected() {
1336 // For a non-associative reduction, the chain entries must appear in order.
1337 for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end();
1340 int PrevIter = 0, BaseCount = 0, Count = 0;
1341 for (Instruction *J : PossibleReds[i]) {
1342 // Note that all instructions in the chain must have been found because
1343 // all instructions in the function must have been assigned to some
1345 int Iter = PossibleRedIter[J];
1346 if (Iter != PrevIter && Iter != PrevIter + 1 &&
1347 !PossibleReds[i].getReducedValue()->isAssociative()) {
1348 DEBUG(dbgs() << "LRR: Out-of-order non-associative reduction: " <<
1353 if (Iter != PrevIter) {
1354 if (Count != BaseCount) {
1355 DEBUG(dbgs() << "LRR: Iteration " << PrevIter <<
1356 " reduction use count " << Count <<
1357 " is not equal to the base use count " <<
1376 // For all selected reductions, remove all parts except those in the first
1377 // iteration (and the PHI). Replace outside uses of the reduced value with uses
1378 // of the first-iteration reduced value (in other words, reroll the selected
1380 void LoopReroll::ReductionTracker::replaceSelected() {
1381 // Fixup reductions to refer to the last instruction associated with the
1382 // first iteration (not the last).
1383 for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end();
1387 for (int e = PossibleReds[i].size(); j != e; ++j)
1388 if (PossibleRedIter[PossibleReds[i][j]] != 0) {
1393 // Replace users with the new end-of-chain value.
1394 SmallInstructionVector Users;
1395 for (User *U : PossibleReds[i].getReducedValue()->users()) {
1396 Users.push_back(cast<Instruction>(U));
1399 for (SmallInstructionVector::iterator J = Users.begin(),
1400 JE = Users.end(); J != JE; ++J)
1401 (*J)->replaceUsesOfWith(PossibleReds[i].getReducedValue(),
1402 PossibleReds[i][j]);
1406 // Reroll the provided loop with respect to the provided induction variable.
1407 // Generally, we're looking for a loop like this:
1409 // %iv = phi [ (preheader, ...), (body, %iv.next) ]
1411 // %iv.1 = add %iv, 1 <-- a root increment
1413 // %iv.2 = add %iv, 2 <-- a root increment
1415 // %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
1418 // %iv.next = add %iv, scale
1419 // %cmp = icmp(%iv, ...)
1420 // br %cmp, header, exit
1422 // Notably, we do not require that f(%iv), f(%iv.1), etc. be isolated groups of
1423 // instructions. In other words, the instructions in f(%iv), f(%iv.1), etc. can
1424 // be intermixed with eachother. The restriction imposed by this algorithm is
1425 // that the relative order of the isomorphic instructions in f(%iv), f(%iv.1),
1426 // etc. be the same.
1428 // First, we collect the use set of %iv, excluding the other increment roots.
1429 // This gives us f(%iv). Then we iterate over the loop instructions (scale-1)
1430 // times, having collected the use set of f(%iv.(i+1)), during which we:
1431 // - Ensure that the next unmatched instruction in f(%iv) is isomorphic to
1432 // the next unmatched instruction in f(%iv.(i+1)).
1433 // - Ensure that both matched instructions don't have any external users
1434 // (with the exception of last-in-chain reduction instructions).
1435 // - Track the (aliasing) write set, and other side effects, of all
1436 // instructions that belong to future iterations that come before the matched
1437 // instructions. If the matched instructions read from that write set, then
1438 // f(%iv) or f(%iv.(i+1)) has some dependency on instructions in
1439 // f(%iv.(j+1)) for some j > i, and we cannot reroll the loop. Similarly,
1440 // if any of these future instructions had side effects (could not be
1441 // speculatively executed), and so do the matched instructions, when we
1442 // cannot reorder those side-effect-producing instructions, and rerolling
1445 // Finally, we make sure that all loop instructions are either loop increment
1446 // roots, belong to simple latch code, parts of validated reductions, part of
1447 // f(%iv) or part of some f(%iv.i). If all of that is true (and all reductions
1448 // have been validated), then we reroll the loop.
1449 bool LoopReroll::reroll(Instruction *IV, Loop *L, BasicBlock *Header,
1450 const SCEV *IterCount,
1451 ReductionTracker &Reductions) {
1452 DAGRootTracker DAGRoots(this, L, IV, SE, AA, TLI, DT, LI, PreserveLCSSA,
1455 if (!DAGRoots.findRoots())
1457 DEBUG(dbgs() << "LRR: Found all root induction increments for: " <<
1460 if (!DAGRoots.validate(Reductions))
1462 if (!Reductions.validateSelected())
1464 // At this point, we've validated the rerolling, and we're committed to
1467 Reductions.replaceSelected();
1468 DAGRoots.replace(IterCount);
1474 bool LoopReroll::runOnLoop(Loop *L, LPPassManager &LPM) {
1475 if (skipOptnoneFunction(L))
1478 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
1479 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1480 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1481 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1482 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1483 PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
1485 BasicBlock *Header = L->getHeader();
1486 DEBUG(dbgs() << "LRR: F[" << Header->getParent()->getName() <<
1487 "] Loop %" << Header->getName() << " (" <<
1488 L->getNumBlocks() << " block(s))\n");
1490 bool Changed = false;
1492 // For now, we'll handle only single BB loops.
1493 if (L->getNumBlocks() > 1)
1496 if (!SE->hasLoopInvariantBackedgeTakenCount(L))
1499 const SCEV *LIBETC = SE->getBackedgeTakenCount(L);
1500 const SCEV *IterCount = SE->getAddExpr(LIBETC, SE->getOne(LIBETC->getType()));
1501 DEBUG(dbgs() << "LRR: iteration count = " << *IterCount << "\n");
1503 // First, we need to find the induction variable with respect to which we can
1504 // reroll (there may be several possible options).
1505 SmallInstructionVector PossibleIVs;
1507 collectPossibleIVs(L, PossibleIVs);
1509 if (PossibleIVs.empty()) {
1510 DEBUG(dbgs() << "LRR: No possible IVs found\n");
1514 ReductionTracker Reductions;
1515 collectPossibleReductions(L, Reductions);
1517 // For each possible IV, collect the associated possible set of 'root' nodes
1518 // (i+1, i+2, etc.).
1519 for (SmallInstructionVector::iterator I = PossibleIVs.begin(),
1520 IE = PossibleIVs.end(); I != IE; ++I)
1521 if (reroll(*I, L, Header, IterCount, Reductions)) {