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<AliasAnalysis>();
151 AU.addRequired<LoopInfoWrapperPass>();
152 AU.addPreserved<LoopInfoWrapperPass>();
153 AU.addRequired<DominatorTreeWrapperPass>();
154 AU.addPreserved<DominatorTreeWrapperPass>();
155 AU.addRequired<ScalarEvolution>();
156 AU.addRequired<TargetLibraryInfoWrapperPass>();
163 TargetLibraryInfo *TLI;
166 typedef SmallVector<Instruction *, 16> SmallInstructionVector;
167 typedef SmallSet<Instruction *, 16> SmallInstructionSet;
169 // A chain of isomorphic instructions, indentified by a single-use PHI,
170 // representing a reduction. Only the last value may be used outside the
172 struct SimpleLoopReduction {
173 SimpleLoopReduction(Instruction *P, Loop *L)
174 : Valid(false), Instructions(1, P) {
175 assert(isa<PHINode>(P) && "First reduction instruction must be a PHI");
183 Instruction *getPHI() const {
184 assert(Valid && "Using invalid reduction");
185 return Instructions.front();
188 Instruction *getReducedValue() const {
189 assert(Valid && "Using invalid reduction");
190 return Instructions.back();
193 Instruction *get(size_t i) const {
194 assert(Valid && "Using invalid reduction");
195 return Instructions[i+1];
198 Instruction *operator [] (size_t i) const { return get(i); }
200 // The size, ignoring the initial PHI.
201 size_t size() const {
202 assert(Valid && "Using invalid reduction");
203 return Instructions.size()-1;
206 typedef SmallInstructionVector::iterator iterator;
207 typedef SmallInstructionVector::const_iterator const_iterator;
210 assert(Valid && "Using invalid reduction");
211 return std::next(Instructions.begin());
214 const_iterator begin() const {
215 assert(Valid && "Using invalid reduction");
216 return std::next(Instructions.begin());
219 iterator end() { return Instructions.end(); }
220 const_iterator end() const { return Instructions.end(); }
224 SmallInstructionVector Instructions;
229 // The set of all reductions, and state tracking of possible reductions
230 // during loop instruction processing.
231 struct ReductionTracker {
232 typedef SmallVector<SimpleLoopReduction, 16> SmallReductionVector;
234 // Add a new possible reduction.
235 void addSLR(SimpleLoopReduction &SLR) { PossibleReds.push_back(SLR); }
237 // Setup to track possible reductions corresponding to the provided
238 // rerolling scale. Only reductions with a number of non-PHI instructions
239 // that is divisible by the scale are considered. Three instructions sets
241 // - A set of all possible instructions in eligible reductions.
242 // - A set of all PHIs in eligible reductions
243 // - A set of all reduced values (last instructions) in eligible
245 void restrictToScale(uint64_t Scale,
246 SmallInstructionSet &PossibleRedSet,
247 SmallInstructionSet &PossibleRedPHISet,
248 SmallInstructionSet &PossibleRedLastSet) {
249 PossibleRedIdx.clear();
250 PossibleRedIter.clear();
253 for (unsigned i = 0, e = PossibleReds.size(); i != e; ++i)
254 if (PossibleReds[i].size() % Scale == 0) {
255 PossibleRedLastSet.insert(PossibleReds[i].getReducedValue());
256 PossibleRedPHISet.insert(PossibleReds[i].getPHI());
258 PossibleRedSet.insert(PossibleReds[i].getPHI());
259 PossibleRedIdx[PossibleReds[i].getPHI()] = i;
260 for (Instruction *J : PossibleReds[i]) {
261 PossibleRedSet.insert(J);
262 PossibleRedIdx[J] = i;
267 // The functions below are used while processing the loop instructions.
269 // Are the two instructions both from reductions, and furthermore, from
270 // the same reduction?
271 bool isPairInSame(Instruction *J1, Instruction *J2) {
272 DenseMap<Instruction *, int>::iterator J1I = PossibleRedIdx.find(J1);
273 if (J1I != PossibleRedIdx.end()) {
274 DenseMap<Instruction *, int>::iterator J2I = PossibleRedIdx.find(J2);
275 if (J2I != PossibleRedIdx.end() && J1I->second == J2I->second)
282 // The two provided instructions, the first from the base iteration, and
283 // the second from iteration i, form a matched pair. If these are part of
284 // a reduction, record that fact.
285 void recordPair(Instruction *J1, Instruction *J2, unsigned i) {
286 if (PossibleRedIdx.count(J1)) {
287 assert(PossibleRedIdx.count(J2) &&
288 "Recording reduction vs. non-reduction instruction?");
290 PossibleRedIter[J1] = 0;
291 PossibleRedIter[J2] = i;
293 int Idx = PossibleRedIdx[J1];
294 assert(Idx == PossibleRedIdx[J2] &&
295 "Recording pair from different reductions?");
300 // The functions below can be called after we've finished processing all
301 // instructions in the loop, and we know which reductions were selected.
303 // Is the provided instruction the PHI of a reduction selected for
305 bool isSelectedPHI(Instruction *J) {
306 if (!isa<PHINode>(J))
309 for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end();
312 if (cast<Instruction>(J) == PossibleReds[i].getPHI())
319 bool validateSelected();
320 void replaceSelected();
323 // The vector of all possible reductions (for any scale).
324 SmallReductionVector PossibleReds;
326 DenseMap<Instruction *, int> PossibleRedIdx;
327 DenseMap<Instruction *, int> PossibleRedIter;
331 // A DAGRootSet models an induction variable being used in a rerollable
332 // loop. For example,
338 // Base instruction -> i*3
341 // ST[y1] +1 +2 <-- Roots
345 // There may be multiple DAGRoots, for example:
347 // x[i*2+0] = ... (1)
348 // x[i*2+1] = ... (1)
349 // x[i*2+4] = ... (2)
350 // x[i*2+5] = ... (2)
351 // x[(i+1234)*2+5678] = ... (3)
352 // x[(i+1234)*2+5679] = ... (3)
354 // The loop will be rerolled by adding a new loop induction variable,
355 // one for the Base instruction in each DAGRootSet.
358 Instruction *BaseInst;
359 SmallInstructionVector Roots;
360 // The instructions between IV and BaseInst (but not including BaseInst).
361 SmallInstructionSet SubsumedInsts;
364 // The set of all DAG roots, and state tracking of all roots
365 // for a particular induction variable.
366 struct DAGRootTracker {
367 DAGRootTracker(LoopReroll *Parent, Loop *L, Instruction *IV,
368 ScalarEvolution *SE, AliasAnalysis *AA,
369 TargetLibraryInfo *TLI)
370 : Parent(Parent), L(L), SE(SE), AA(AA), TLI(TLI), IV(IV) {}
372 /// Stage 1: Find all the DAG roots for the induction variable.
374 /// Stage 2: Validate if the found roots are valid.
375 bool validate(ReductionTracker &Reductions);
376 /// Stage 3: Assuming validate() returned true, perform the
378 /// @param IterCount The maximum iteration count of L.
379 void replace(const SCEV *IterCount);
382 typedef MapVector<Instruction*, SmallBitVector> UsesTy;
384 bool findRootsRecursive(Instruction *IVU,
385 SmallInstructionSet SubsumedInsts);
386 bool findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts);
387 bool collectPossibleRoots(Instruction *Base,
388 std::map<int64_t,Instruction*> &Roots);
390 bool collectUsedInstructions(SmallInstructionSet &PossibleRedSet);
391 void collectInLoopUserSet(const SmallInstructionVector &Roots,
392 const SmallInstructionSet &Exclude,
393 const SmallInstructionSet &Final,
394 DenseSet<Instruction *> &Users);
395 void collectInLoopUserSet(Instruction *Root,
396 const SmallInstructionSet &Exclude,
397 const SmallInstructionSet &Final,
398 DenseSet<Instruction *> &Users);
400 UsesTy::iterator nextInstr(int Val, UsesTy &In,
401 const SmallInstructionSet &Exclude,
402 UsesTy::iterator *StartI=nullptr);
403 bool isBaseInst(Instruction *I);
404 bool isRootInst(Instruction *I);
405 bool instrDependsOn(Instruction *I,
406 UsesTy::iterator Start,
407 UsesTy::iterator End);
411 // Members of Parent, replicated here for brevity.
415 TargetLibraryInfo *TLI;
417 // The loop induction variable.
421 // Loop reroll count; if Inc == 1, this records the scaling applied
422 // to the indvar: a[i*2+0] = ...; a[i*2+1] = ... ;
423 // If Inc is not 1, Scale = Inc.
425 // The roots themselves.
426 SmallVector<DAGRootSet,16> RootSets;
427 // All increment instructions for IV.
428 SmallInstructionVector LoopIncs;
429 // Map of all instructions in the loop (in order) to the iterations
430 // they are used in (or specially, IL_All for instructions
431 // used in the loop increment mechanism).
435 void collectPossibleIVs(Loop *L, SmallInstructionVector &PossibleIVs);
436 void collectPossibleReductions(Loop *L,
437 ReductionTracker &Reductions);
438 bool reroll(Instruction *IV, Loop *L, BasicBlock *Header, const SCEV *IterCount,
439 ReductionTracker &Reductions);
443 char LoopReroll::ID = 0;
444 INITIALIZE_PASS_BEGIN(LoopReroll, "loop-reroll", "Reroll loops", false, false)
445 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
446 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
447 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
448 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
449 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
450 INITIALIZE_PASS_END(LoopReroll, "loop-reroll", "Reroll loops", false, false)
452 Pass *llvm::createLoopRerollPass() {
453 return new LoopReroll;
456 // Returns true if the provided instruction is used outside the given loop.
457 // This operates like Instruction::isUsedOutsideOfBlock, but considers PHIs in
458 // non-loop blocks to be outside the loop.
459 static bool hasUsesOutsideLoop(Instruction *I, Loop *L) {
460 for (User *U : I->users()) {
461 if (!L->contains(cast<Instruction>(U)))
467 // Collect the list of loop induction variables with respect to which it might
468 // be possible to reroll the loop.
469 void LoopReroll::collectPossibleIVs(Loop *L,
470 SmallInstructionVector &PossibleIVs) {
471 BasicBlock *Header = L->getHeader();
472 for (BasicBlock::iterator I = Header->begin(),
473 IE = Header->getFirstInsertionPt(); I != IE; ++I) {
474 if (!isa<PHINode>(I))
476 if (!I->getType()->isIntegerTy())
479 if (const SCEVAddRecExpr *PHISCEV =
480 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(I))) {
481 if (PHISCEV->getLoop() != L)
483 if (!PHISCEV->isAffine())
485 if (const SCEVConstant *IncSCEV =
486 dyn_cast<SCEVConstant>(PHISCEV->getStepRecurrence(*SE))) {
487 if (!IncSCEV->getValue()->getValue().isStrictlyPositive())
489 if (IncSCEV->getValue()->uge(MaxInc))
492 DEBUG(dbgs() << "LRR: Possible IV: " << *I << " = " <<
494 PossibleIVs.push_back(I);
500 // Add the remainder of the reduction-variable chain to the instruction vector
501 // (the initial PHINode has already been added). If successful, the object is
503 void LoopReroll::SimpleLoopReduction::add(Loop *L) {
504 assert(!Valid && "Cannot add to an already-valid chain");
506 // The reduction variable must be a chain of single-use instructions
507 // (including the PHI), except for the last value (which is used by the PHI
508 // and also outside the loop).
509 Instruction *C = Instructions.front();
514 C = cast<Instruction>(*C->user_begin());
515 if (C->hasOneUse()) {
516 if (!C->isBinaryOp())
519 if (!(isa<PHINode>(Instructions.back()) ||
520 C->isSameOperationAs(Instructions.back())))
523 Instructions.push_back(C);
525 } while (C->hasOneUse());
527 if (Instructions.size() < 2 ||
528 !C->isSameOperationAs(Instructions.back()) ||
532 // C is now the (potential) last instruction in the reduction chain.
533 for (User *U : C->users()) {
534 // The only in-loop user can be the initial PHI.
535 if (L->contains(cast<Instruction>(U)))
536 if (cast<Instruction>(U) != Instructions.front())
540 Instructions.push_back(C);
544 // Collect the vector of possible reduction variables.
545 void LoopReroll::collectPossibleReductions(Loop *L,
546 ReductionTracker &Reductions) {
547 BasicBlock *Header = L->getHeader();
548 for (BasicBlock::iterator I = Header->begin(),
549 IE = Header->getFirstInsertionPt(); I != IE; ++I) {
550 if (!isa<PHINode>(I))
552 if (!I->getType()->isSingleValueType())
555 SimpleLoopReduction SLR(I, L);
559 DEBUG(dbgs() << "LRR: Possible reduction: " << *I << " (with " <<
560 SLR.size() << " chained instructions)\n");
561 Reductions.addSLR(SLR);
565 // Collect the set of all users of the provided root instruction. This set of
566 // users contains not only the direct users of the root instruction, but also
567 // all users of those users, and so on. There are two exceptions:
569 // 1. Instructions in the set of excluded instructions are never added to the
570 // use set (even if they are users). This is used, for example, to exclude
571 // including root increments in the use set of the primary IV.
573 // 2. Instructions in the set of final instructions are added to the use set
574 // if they are users, but their users are not added. This is used, for
575 // example, to prevent a reduction update from forcing all later reduction
576 // updates into the use set.
577 void LoopReroll::DAGRootTracker::collectInLoopUserSet(
578 Instruction *Root, const SmallInstructionSet &Exclude,
579 const SmallInstructionSet &Final,
580 DenseSet<Instruction *> &Users) {
581 SmallInstructionVector Queue(1, Root);
582 while (!Queue.empty()) {
583 Instruction *I = Queue.pop_back_val();
584 if (!Users.insert(I).second)
588 for (Use &U : I->uses()) {
589 Instruction *User = cast<Instruction>(U.getUser());
590 if (PHINode *PN = dyn_cast<PHINode>(User)) {
591 // Ignore "wrap-around" uses to PHIs of this loop's header.
592 if (PN->getIncomingBlock(U) == L->getHeader())
596 if (L->contains(User) && !Exclude.count(User)) {
597 Queue.push_back(User);
601 // We also want to collect single-user "feeder" values.
602 for (User::op_iterator OI = I->op_begin(),
603 OIE = I->op_end(); OI != OIE; ++OI) {
604 if (Instruction *Op = dyn_cast<Instruction>(*OI))
605 if (Op->hasOneUse() && L->contains(Op) && !Exclude.count(Op) &&
612 // Collect all of the users of all of the provided root instructions (combined
613 // into a single set).
614 void LoopReroll::DAGRootTracker::collectInLoopUserSet(
615 const SmallInstructionVector &Roots,
616 const SmallInstructionSet &Exclude,
617 const SmallInstructionSet &Final,
618 DenseSet<Instruction *> &Users) {
619 for (SmallInstructionVector::const_iterator I = Roots.begin(),
620 IE = Roots.end(); I != IE; ++I)
621 collectInLoopUserSet(*I, Exclude, Final, Users);
624 static bool isSimpleLoadStore(Instruction *I) {
625 if (LoadInst *LI = dyn_cast<LoadInst>(I))
626 return LI->isSimple();
627 if (StoreInst *SI = dyn_cast<StoreInst>(I))
628 return SI->isSimple();
629 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I))
630 return !MI->isVolatile();
634 /// Return true if IVU is a "simple" arithmetic operation.
635 /// This is used for narrowing the search space for DAGRoots; only arithmetic
636 /// and GEPs can be part of a DAGRoot.
637 static bool isSimpleArithmeticOp(User *IVU) {
638 if (Instruction *I = dyn_cast<Instruction>(IVU)) {
639 switch (I->getOpcode()) {
640 default: return false;
641 case Instruction::Add:
642 case Instruction::Sub:
643 case Instruction::Mul:
644 case Instruction::Shl:
645 case Instruction::AShr:
646 case Instruction::LShr:
647 case Instruction::GetElementPtr:
648 case Instruction::Trunc:
649 case Instruction::ZExt:
650 case Instruction::SExt:
657 static bool isLoopIncrement(User *U, Instruction *IV) {
658 BinaryOperator *BO = dyn_cast<BinaryOperator>(U);
659 if (!BO || BO->getOpcode() != Instruction::Add)
662 for (auto *UU : BO->users()) {
663 PHINode *PN = dyn_cast<PHINode>(UU);
670 bool LoopReroll::DAGRootTracker::
671 collectPossibleRoots(Instruction *Base, std::map<int64_t,Instruction*> &Roots) {
672 SmallInstructionVector BaseUsers;
674 for (auto *I : Base->users()) {
675 ConstantInt *CI = nullptr;
677 if (isLoopIncrement(I, IV)) {
678 LoopIncs.push_back(cast<Instruction>(I));
682 // The root nodes must be either GEPs, ORs or ADDs.
683 if (auto *BO = dyn_cast<BinaryOperator>(I)) {
684 if (BO->getOpcode() == Instruction::Add ||
685 BO->getOpcode() == Instruction::Or)
686 CI = dyn_cast<ConstantInt>(BO->getOperand(1));
687 } else if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
688 Value *LastOperand = GEP->getOperand(GEP->getNumOperands()-1);
689 CI = dyn_cast<ConstantInt>(LastOperand);
693 if (Instruction *II = dyn_cast<Instruction>(I)) {
694 BaseUsers.push_back(II);
697 DEBUG(dbgs() << "LRR: Aborting due to non-instruction: " << *I << "\n");
702 int64_t V = CI->getValue().getSExtValue();
703 if (Roots.find(V) != Roots.end())
704 // No duplicates, please.
707 // FIXME: Add support for negative values.
709 DEBUG(dbgs() << "LRR: Aborting due to negative value: " << V << "\n");
713 Roots[V] = cast<Instruction>(I);
719 // If we found non-loop-inc, non-root users of Base, assume they are
720 // for the zeroth root index. This is because "add %a, 0" gets optimized
722 if (BaseUsers.size()) {
723 if (Roots.find(0) != Roots.end()) {
724 DEBUG(dbgs() << "LRR: Multiple roots found for base - aborting!\n");
730 // Calculate the number of users of the base, or lowest indexed, iteration.
731 unsigned NumBaseUses = BaseUsers.size();
732 if (NumBaseUses == 0)
733 NumBaseUses = Roots.begin()->second->getNumUses();
735 // Check that every node has the same number of users.
736 for (auto &KV : Roots) {
739 if (KV.second->getNumUses() != NumBaseUses) {
740 DEBUG(dbgs() << "LRR: Aborting - Root and Base #users not the same: "
741 << "#Base=" << NumBaseUses << ", #Root=" <<
742 KV.second->getNumUses() << "\n");
750 bool LoopReroll::DAGRootTracker::
751 findRootsRecursive(Instruction *I, SmallInstructionSet SubsumedInsts) {
752 // Does the user look like it could be part of a root set?
753 // All its users must be simple arithmetic ops.
754 if (I->getNumUses() > IL_MaxRerollIterations)
757 if ((I->getOpcode() == Instruction::Mul ||
758 I->getOpcode() == Instruction::PHI) &&
760 findRootsBase(I, SubsumedInsts))
763 SubsumedInsts.insert(I);
765 for (User *V : I->users()) {
766 Instruction *I = dyn_cast<Instruction>(V);
767 if (std::find(LoopIncs.begin(), LoopIncs.end(), I) != LoopIncs.end())
770 if (!I || !isSimpleArithmeticOp(I) ||
771 !findRootsRecursive(I, SubsumedInsts))
777 bool LoopReroll::DAGRootTracker::
778 findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts) {
780 // The base instruction needs to be a multiply so
781 // that we can erase it.
782 if (IVU->getOpcode() != Instruction::Mul &&
783 IVU->getOpcode() != Instruction::PHI)
786 std::map<int64_t, Instruction*> V;
787 if (!collectPossibleRoots(IVU, V))
790 // If we didn't get a root for index zero, then IVU must be
792 if (V.find(0) == V.end())
793 SubsumedInsts.insert(IVU);
795 // Partition the vector into monotonically increasing indexes.
797 DRS.BaseInst = nullptr;
801 DRS.BaseInst = KV.second;
802 DRS.SubsumedInsts = SubsumedInsts;
803 } else if (DRS.Roots.empty()) {
804 DRS.Roots.push_back(KV.second);
805 } else if (V.find(KV.first - 1) != V.end()) {
806 DRS.Roots.push_back(KV.second);
808 // Linear sequence terminated.
809 RootSets.push_back(DRS);
810 DRS.BaseInst = KV.second;
811 DRS.SubsumedInsts = SubsumedInsts;
815 RootSets.push_back(DRS);
820 bool LoopReroll::DAGRootTracker::findRoots() {
822 const SCEVAddRecExpr *RealIVSCEV = cast<SCEVAddRecExpr>(SE->getSCEV(IV));
823 Inc = cast<SCEVConstant>(RealIVSCEV->getOperand(1))->
824 getValue()->getZExtValue();
826 assert(RootSets.empty() && "Unclean state!");
828 for (auto *IVU : IV->users()) {
829 if (isLoopIncrement(IVU, IV))
830 LoopIncs.push_back(cast<Instruction>(IVU));
832 if (!findRootsRecursive(IV, SmallInstructionSet()))
834 LoopIncs.push_back(IV);
836 if (!findRootsBase(IV, SmallInstructionSet()))
840 // Ensure all sets have the same size.
841 if (RootSets.empty()) {
842 DEBUG(dbgs() << "LRR: Aborting because no root sets found!\n");
845 for (auto &V : RootSets) {
846 if (V.Roots.empty() || V.Roots.size() != RootSets[0].Roots.size()) {
848 << "LRR: Aborting because not all root sets have the same size\n");
853 // And ensure all loop iterations are consecutive. We rely on std::map
854 // providing ordered traversal.
855 for (auto &V : RootSets) {
856 const auto *ADR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(V.BaseInst));
860 // Consider a DAGRootSet with N-1 roots (so N different values including
862 // Define d = Roots[0] - BaseInst, which should be the same as
863 // Roots[I] - Roots[I-1] for all I in [1..N).
864 // Define D = BaseInst@J - BaseInst@J-1, where "@J" means the value at the
867 // Now, For the loop iterations to be consecutive:
870 unsigned N = V.Roots.size() + 1;
871 const SCEV *StepSCEV = SE->getMinusSCEV(SE->getSCEV(V.Roots[0]), ADR);
872 const SCEV *ScaleSCEV = SE->getConstant(StepSCEV->getType(), N);
873 if (ADR->getStepRecurrence(*SE) != SE->getMulExpr(StepSCEV, ScaleSCEV)) {
874 DEBUG(dbgs() << "LRR: Aborting because iterations are not consecutive\n");
878 Scale = RootSets[0].Roots.size() + 1;
880 if (Scale > IL_MaxRerollIterations) {
881 DEBUG(dbgs() << "LRR: Aborting - too many iterations found. "
882 << "#Found=" << Scale << ", #Max=" << IL_MaxRerollIterations
887 DEBUG(dbgs() << "LRR: Successfully found roots: Scale=" << Scale << "\n");
892 bool LoopReroll::DAGRootTracker::collectUsedInstructions(SmallInstructionSet &PossibleRedSet) {
893 // Populate the MapVector with all instructions in the block, in order first,
894 // so we can iterate over the contents later in perfect order.
895 for (auto &I : *L->getHeader()) {
896 Uses[&I].resize(IL_End);
899 SmallInstructionSet Exclude;
900 for (auto &DRS : RootSets) {
901 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
902 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
903 Exclude.insert(DRS.BaseInst);
905 Exclude.insert(LoopIncs.begin(), LoopIncs.end());
907 for (auto &DRS : RootSets) {
908 DenseSet<Instruction*> VBase;
909 collectInLoopUserSet(DRS.BaseInst, Exclude, PossibleRedSet, VBase);
910 for (auto *I : VBase) {
915 for (auto *Root : DRS.Roots) {
916 DenseSet<Instruction*> V;
917 collectInLoopUserSet(Root, Exclude, PossibleRedSet, V);
919 // While we're here, check the use sets are the same size.
920 if (V.size() != VBase.size()) {
921 DEBUG(dbgs() << "LRR: Aborting - use sets are different sizes\n");
931 // Make sure our subsumed instructions are remembered too.
932 for (auto *I : DRS.SubsumedInsts) {
937 // Make sure the loop increments are also accounted for.
940 for (auto &DRS : RootSets) {
941 Exclude.insert(DRS.Roots.begin(), DRS.Roots.end());
942 Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end());
943 Exclude.insert(DRS.BaseInst);
946 DenseSet<Instruction*> V;
947 collectInLoopUserSet(LoopIncs, Exclude, PossibleRedSet, V);
956 /// Get the next instruction in "In" that is a member of set Val.
957 /// Start searching from StartI, and do not return anything in Exclude.
958 /// If StartI is not given, start from In.begin().
959 LoopReroll::DAGRootTracker::UsesTy::iterator
960 LoopReroll::DAGRootTracker::nextInstr(int Val, UsesTy &In,
961 const SmallInstructionSet &Exclude,
962 UsesTy::iterator *StartI) {
963 UsesTy::iterator I = StartI ? *StartI : In.begin();
964 while (I != In.end() && (I->second.test(Val) == 0 ||
965 Exclude.count(I->first) != 0))
970 bool LoopReroll::DAGRootTracker::isBaseInst(Instruction *I) {
971 for (auto &DRS : RootSets) {
972 if (DRS.BaseInst == I)
978 bool LoopReroll::DAGRootTracker::isRootInst(Instruction *I) {
979 for (auto &DRS : RootSets) {
980 if (std::find(DRS.Roots.begin(), DRS.Roots.end(), I) != DRS.Roots.end())
986 /// Return true if instruction I depends on any instruction between
988 bool LoopReroll::DAGRootTracker::instrDependsOn(Instruction *I,
989 UsesTy::iterator Start,
990 UsesTy::iterator End) {
991 for (auto *U : I->users()) {
992 for (auto It = Start; It != End; ++It)
999 bool LoopReroll::DAGRootTracker::validate(ReductionTracker &Reductions) {
1000 // 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.
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);
1020 // Populate "Uses" with where each instruction is used.
1021 if (!collectUsedInstructions(PossibleRedSet))
1024 // Make sure we mark the reduction PHIs as used in all iterations.
1025 for (auto *I : PossibleRedPHISet) {
1026 Uses[I].set(IL_All);
1029 // Make sure all instructions in the loop are in one and only one
1031 for (auto &KV : Uses) {
1032 if (KV.second.count() != 1) {
1033 DEBUG(dbgs() << "LRR: Aborting - instruction is not used in 1 iteration: "
1034 << *KV.first << " (#uses=" << KV.second.count() << ")\n");
1040 for (auto &KV : Uses) {
1041 dbgs() << "LRR: " << KV.second.find_first() << "\t" << *KV.first << "\n";
1045 for (unsigned Iter = 1; Iter < Scale; ++Iter) {
1046 // 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
1050 bool FutureSideEffects = false;
1051 AliasSetTracker AST(*AA);
1052 // The map between instructions in f(%iv.(i+1)) and f(%iv).
1053 DenseMap<Value *, Value *> BaseMap;
1055 // Compare iteration Iter to the base.
1056 SmallInstructionSet Visited;
1057 auto BaseIt = nextInstr(0, Uses, Visited);
1058 auto RootIt = nextInstr(Iter, Uses, Visited);
1059 auto LastRootIt = Uses.begin();
1061 while (BaseIt != Uses.end() && RootIt != Uses.end()) {
1062 Instruction *BaseInst = BaseIt->first;
1063 Instruction *RootInst = RootIt->first;
1065 // Skip over the IV or root instructions; only match their users.
1066 bool Continue = false;
1067 if (isBaseInst(BaseInst)) {
1068 Visited.insert(BaseInst);
1069 BaseIt = nextInstr(0, Uses, Visited);
1072 if (isRootInst(RootInst)) {
1073 LastRootIt = RootIt;
1074 Visited.insert(RootInst);
1075 RootIt = nextInstr(Iter, Uses, Visited);
1078 if (Continue) continue;
1080 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.
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) &&
1097 TryIt = nextInstr(Iter, Uses, Visited, &TryIt);
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");
1108 RootInst = TryIt->first;
1111 // All instructions between the last root and this root
1112 // may belong to some other iteration. If they belong to a
1113 // future iteration, then they're dangerous to alias with.
1115 // 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
1119 for (; LastRootIt < RootIt; ++LastRootIt) {
1120 Instruction *I = LastRootIt->first;
1121 if (LastRootIt->second.find_first() < (int)Iter)
1123 if (I->mayWriteToMemory())
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) &&
1130 !isSafeToSpeculativelyExecute(I))
1131 // Intervening instructions cause side effects.
1132 FutureSideEffects = true;
1135 // 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
1137 // 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");
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
1147 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");
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.
1160 if (FutureSideEffects && ((!isSimpleLoadStore(BaseInst) &&
1161 !isSafeToSpeculativelyExecute(BaseInst)) ||
1162 (!isSimpleLoadStore(RootInst) &&
1163 !isSafeToSpeculativelyExecute(RootInst)))) {
1164 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst <<
1165 " vs. " << *RootInst <<
1166 " (side effects prevent reordering)\n");
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];
1180 bool InReduction = Reductions.isPairInSame(BaseInst, RootInst);
1182 if (!(InReduction && BaseInst->isAssociative())) {
1183 bool Swapped = false, SomeOpMatched = false;
1184 for (unsigned j = 0; j < BaseInst->getNumOperands(); ++j) {
1185 Value *Op2 = RootInst->getOperand(j);
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.
1191 if (Instruction *Op2I = dyn_cast<Instruction>(Op2))
1192 if (Reductions.isPairInSame(RootInst, Op2I))
1195 DenseMap<Value *, Value *>::iterator BMI = BaseMap.find(Op2);
1196 if (BMI != BaseMap.end()) {
1199 for (auto &DRS : RootSets) {
1200 if (DRS.Roots[Iter-1] == (Instruction*) Op2) {
1207 if (BaseInst->getOperand(Swapped ? unsigned(!j) : j) != Op2) {
1208 // 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.
1213 if (!Swapped && BaseInst->isCommutative() && !SomeOpMatched &&
1214 BaseInst->getOperand(!j) == Op2) {
1217 DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
1218 << " vs. " << *RootInst << " (operand " << j << ")\n");
1223 SomeOpMatched = true;
1227 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");
1236 Reductions.recordPair(BaseInst, RootInst, Iter);
1237 BaseMap.insert(std::make_pair(RootInst, BaseInst));
1239 LastRootIt = RootIt;
1240 Visited.insert(BaseInst);
1241 Visited.insert(RootInst);
1242 BaseIt = nextInstr(0, Uses, Visited);
1243 RootIt = nextInstr(Iter, Uses, Visited);
1245 assert (BaseIt == Uses.end() && RootIt == Uses.end() &&
1246 "Mismatched set sizes!");
1249 DEBUG(dbgs() << "LRR: Matched all iteration increments for " <<
1255 void 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();) {
1260 unsigned I = Uses[&*J].find_first();
1261 if (I > 0 && I < IL_All) {
1262 Instruction *D = &*J;
1263 DEBUG(dbgs() << "LRR: removing: " << *D << "\n");
1264 D->eraseFromParent();
1270 const DataLayout &DL = Header->getModule()->getDataLayout();
1272 // We need to create a new induction variable for each different BaseInst.
1273 for (auto &DRS : RootSets) {
1274 // Insert the new induction variable.
1275 const SCEVAddRecExpr *RealIVSCEV =
1276 cast<SCEVAddRecExpr>(SE->getSCEV(DRS.BaseInst));
1277 const SCEV *Start = RealIVSCEV->getStart();
1278 const SCEVAddRecExpr *H = cast<SCEVAddRecExpr>
1279 (SE->getAddRecExpr(Start,
1280 SE->getConstant(RealIVSCEV->getType(), 1),
1281 L, SCEV::FlagAnyWrap));
1282 { // Limit the lifetime of SCEVExpander.
1283 SCEVExpander Expander(*SE, DL, "reroll");
1284 Value *NewIV = Expander.expandCodeFor(H, IV->getType(), Header->begin());
1286 for (auto &KV : Uses) {
1287 if (KV.second.find_first() == 0)
1288 KV.first->replaceUsesOfWith(DRS.BaseInst, NewIV);
1291 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);
1296 // Iteration count SCEV minus 1
1297 const SCEV *ICMinus1SCEV =
1298 SE->getMinusSCEV(ICSCEV, SE->getConstant(ICSCEV->getType(), 1));
1300 Value *ICMinus1; // Iteration count minus 1
1301 if (isa<SCEVConstant>(ICMinus1SCEV)) {
1302 ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(), BI);
1304 BasicBlock *Preheader = L->getLoopPreheader();
1306 Preheader = InsertPreheaderForLoop(L, Parent);
1308 ICMinus1 = Expander.expandCodeFor(ICMinus1SCEV, NewIV->getType(),
1309 Preheader->getTerminator());
1313 new ICmpInst(BI, CmpInst::ICMP_EQ, NewIV, ICMinus1, "exitcond");
1314 BI->setCondition(Cond);
1316 if (BI->getSuccessor(1) != Header)
1317 BI->swapSuccessors();
1323 SimplifyInstructionsInBlock(Header, TLI);
1324 DeleteDeadPHIs(Header, TLI);
1327 // 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.
1330 bool 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();
1335 int PrevIter = 0, BaseCount = 0, Count = 0;
1336 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
1340 int Iter = PossibleRedIter[J];
1341 if (Iter != PrevIter && Iter != PrevIter + 1 &&
1342 !PossibleReds[i].getReducedValue()->isAssociative()) {
1343 DEBUG(dbgs() << "LRR: Out-of-order non-associative reduction: " <<
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 " <<
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
1375 void 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();
1382 for (int e = PossibleReds[i].size(); j != e; ++j)
1383 if (PossibleRedIter[PossibleReds[i][j]] != 0) {
1388 // Replace users with the new end-of-chain value.
1389 SmallInstructionVector Users;
1390 for (User *U : PossibleReds[i].getReducedValue()->users()) {
1391 Users.push_back(cast<Instruction>(U));
1394 for (SmallInstructionVector::iterator J = Users.begin(),
1395 JE = Users.end(); J != JE; ++J)
1396 (*J)->replaceUsesOfWith(PossibleReds[i].getReducedValue(),
1397 PossibleReds[i][j]);
1401 // Reroll the provided loop with respect to the provided induction variable.
1402 // Generally, we're looking for a loop like this:
1404 // %iv = phi [ (preheader, ...), (body, %iv.next) ]
1406 // %iv.1 = add %iv, 1 <-- a root increment
1408 // %iv.2 = add %iv, 2 <-- a root increment
1410 // %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
1413 // %iv.next = add %iv, scale
1414 // %cmp = icmp(%iv, ...)
1415 // br %cmp, header, exit
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.
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
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.
1444 bool LoopReroll::reroll(Instruction *IV, Loop *L, BasicBlock *Header,
1445 const SCEV *IterCount,
1446 ReductionTracker &Reductions) {
1447 DAGRootTracker DAGRoots(this, L, IV, SE, AA, TLI);
1449 if (!DAGRoots.findRoots())
1451 DEBUG(dbgs() << "LRR: Found all root induction increments for: " <<
1454 if (!DAGRoots.validate(Reductions))
1456 if (!Reductions.validateSelected())
1458 // At this point, we've validated the rerolling, and we're committed to
1461 Reductions.replaceSelected();
1462 DAGRoots.replace(IterCount);
1468 bool LoopReroll::runOnLoop(Loop *L, LPPassManager &LPM) {
1469 if (skipOptnoneFunction(L))
1472 AA = &getAnalysis<AliasAnalysis>();
1473 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1474 SE = &getAnalysis<ScalarEvolution>();
1475 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1476 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1478 BasicBlock *Header = L->getHeader();
1479 DEBUG(dbgs() << "LRR: F[" << Header->getParent()->getName() <<
1480 "] Loop %" << Header->getName() << " (" <<
1481 L->getNumBlocks() << " block(s))\n");
1483 bool Changed = false;
1485 // For now, we'll handle only single BB loops.
1486 if (L->getNumBlocks() > 1)
1489 if (!SE->hasLoopInvariantBackedgeTakenCount(L))
1492 const SCEV *LIBETC = SE->getBackedgeTakenCount(L);
1493 const SCEV *IterCount =
1494 SE->getAddExpr(LIBETC, SE->getConstant(LIBETC->getType(), 1));
1495 DEBUG(dbgs() << "LRR: iteration count = " << *IterCount << "\n");
1497 // First, we need to find the induction variable with respect to which we can
1498 // reroll (there may be several possible options).
1499 SmallInstructionVector PossibleIVs;
1500 collectPossibleIVs(L, PossibleIVs);
1502 if (PossibleIVs.empty()) {
1503 DEBUG(dbgs() << "LRR: No possible IVs found\n");
1507 ReductionTracker Reductions;
1508 collectPossibleReductions(L, Reductions);
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)) {