1 //===- LoopReroll.cpp - Loop rerolling pass -------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass implements a simple loop reroller.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/APInt.h"
14 #include "llvm/ADT/BitVector.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/MapVector.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/AliasSetTracker.h"
24 #include "llvm/Analysis/LoopInfo.h"
25 #include "llvm/Analysis/LoopPass.h"
26 #include "llvm/Analysis/ScalarEvolution.h"
27 #include "llvm/Analysis/ScalarEvolutionExpander.h"
28 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
29 #include "llvm/Analysis/TargetLibraryInfo.h"
30 #include "llvm/Transforms/Utils/Local.h"
31 #include "llvm/Analysis/ValueTracking.h"
32 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/DerivedTypes.h"
36 #include "llvm/IR/Dominators.h"
37 #include "llvm/IR/IRBuilder.h"
38 #include "llvm/IR/InstrTypes.h"
39 #include "llvm/IR/Instruction.h"
40 #include "llvm/IR/Instructions.h"
41 #include "llvm/IR/IntrinsicInst.h"
42 #include "llvm/IR/Intrinsics.h"
43 #include "llvm/IR/Module.h"
44 #include "llvm/IR/Type.h"
45 #include "llvm/IR/Use.h"
46 #include "llvm/IR/User.h"
47 #include "llvm/IR/Value.h"
48 #include "llvm/Pass.h"
49 #include "llvm/Support/Casting.h"
50 #include "llvm/Support/CommandLine.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Transforms/Scalar.h"
54 #include "llvm/Transforms/Utils.h"
55 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
56 #include "llvm/Transforms/Utils/LoopUtils.h"
67 #define DEBUG_TYPE "loop-reroll"
69 STATISTIC(NumRerolledLoops
, "Number of rerolled loops");
71 static cl::opt
<unsigned>
72 NumToleratedFailedMatches("reroll-num-tolerated-failed-matches", cl::init(400),
74 cl::desc("The maximum number of failures to tolerate"
75 " during fuzzy matching. (default: 400)"));
77 // This loop re-rolling transformation aims to transform loops like this:
81 // for (int i = 0; i < 500; i += 3) {
88 // into a loop like this:
91 // for (int i = 0; i < 500; ++i)
95 // It does this by looking for loops that, besides the latch code, are composed
96 // of isomorphic DAGs of instructions, with each DAG rooted at some increment
97 // to the induction variable, and where each DAG is isomorphic to the DAG
98 // rooted at the induction variable (excepting the sub-DAGs which root the
99 // other induction-variable increments). In other words, we're looking for loop
100 // bodies of the form:
102 // %iv = phi [ (preheader, ...), (body, %iv.next) ]
104 // %iv.1 = add %iv, 1 <-- a root increment
106 // %iv.2 = add %iv, 2 <-- a root increment
108 // %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
111 // %iv.next = add %iv, scale
112 // %cmp = icmp(%iv, ...)
113 // br %cmp, header, exit
115 // where each f(i) is a set of instructions that, collectively, are a function
116 // only of i (and other loop-invariant values).
118 // As a special case, we can also reroll loops like this:
121 // void bar(int *x) {
122 // for (int i = 0; i < 500; ++i) {
124 // x[3*i+1] = foo(0);
125 // x[3*i+2] = foo(0);
131 // void bar(int *x) {
132 // for (int i = 0; i < 1500; ++i)
136 // in which case, we're looking for inputs like this:
138 // %iv = phi [ (preheader, ...), (body, %iv.next) ]
139 // %scaled.iv = mul %iv, scale
141 // %scaled.iv.1 = add %scaled.iv, 1
143 // %scaled.iv.2 = add %scaled.iv, 2
145 // %scaled.iv.scale_m_1 = add %scaled.iv, scale-1
146 // f(%scaled.iv.scale_m_1)
148 // %iv.next = add %iv, 1
149 // %cmp = icmp(%iv, ...)
150 // br %cmp, header, exit
154 enum IterationLimits
{
155 /// The maximum number of iterations that we'll try and reroll.
156 IL_MaxRerollIterations
= 32,
157 /// The bitvector index used by loop induction variables and other
158 /// instructions that belong to all iterations.
163 class LoopReroll
: public LoopPass
{
165 static char ID
; // Pass ID, replacement for typeid
167 LoopReroll() : LoopPass(ID
) {
168 initializeLoopRerollPass(*PassRegistry::getPassRegistry());
171 bool runOnLoop(Loop
*L
, LPPassManager
&LPM
) override
;
173 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
174 AU
.addRequired
<TargetLibraryInfoWrapperPass
>();
175 getLoopAnalysisUsage(AU
);
182 TargetLibraryInfo
*TLI
;
186 using SmallInstructionVector
= SmallVector
<Instruction
*, 16>;
187 using SmallInstructionSet
= SmallPtrSet
<Instruction
*, 16>;
189 // Map between induction variable and its increment
190 DenseMap
<Instruction
*, int64_t> IVToIncMap
;
192 // For loop with multiple induction variable, remember the one used only to
194 Instruction
*LoopControlIV
;
196 // A chain of isomorphic instructions, identified by a single-use PHI
197 // representing a reduction. Only the last value may be used outside the
199 struct SimpleLoopReduction
{
200 SimpleLoopReduction(Instruction
*P
, Loop
*L
) : Instructions(1, P
) {
201 assert(isa
<PHINode
>(P
) && "First reduction instruction must be a PHI");
209 Instruction
*getPHI() const {
210 assert(Valid
&& "Using invalid reduction");
211 return Instructions
.front();
214 Instruction
*getReducedValue() const {
215 assert(Valid
&& "Using invalid reduction");
216 return Instructions
.back();
219 Instruction
*get(size_t i
) const {
220 assert(Valid
&& "Using invalid reduction");
221 return Instructions
[i
+1];
224 Instruction
*operator [] (size_t i
) const { return get(i
); }
226 // The size, ignoring the initial PHI.
227 size_t size() const {
228 assert(Valid
&& "Using invalid reduction");
229 return Instructions
.size()-1;
232 using iterator
= SmallInstructionVector::iterator
;
233 using const_iterator
= SmallInstructionVector::const_iterator
;
236 assert(Valid
&& "Using invalid reduction");
237 return std::next(Instructions
.begin());
240 const_iterator
begin() const {
241 assert(Valid
&& "Using invalid reduction");
242 return std::next(Instructions
.begin());
245 iterator
end() { return Instructions
.end(); }
246 const_iterator
end() const { return Instructions
.end(); }
250 SmallInstructionVector Instructions
;
255 // The set of all reductions, and state tracking of possible reductions
256 // during loop instruction processing.
257 struct ReductionTracker
{
258 using SmallReductionVector
= SmallVector
<SimpleLoopReduction
, 16>;
260 // Add a new possible reduction.
261 void addSLR(SimpleLoopReduction
&SLR
) { PossibleReds
.push_back(SLR
); }
263 // Setup to track possible reductions corresponding to the provided
264 // rerolling scale. Only reductions with a number of non-PHI instructions
265 // that is divisible by the scale are considered. Three instructions sets
267 // - A set of all possible instructions in eligible reductions.
268 // - A set of all PHIs in eligible reductions
269 // - A set of all reduced values (last instructions) in eligible
271 void restrictToScale(uint64_t Scale
,
272 SmallInstructionSet
&PossibleRedSet
,
273 SmallInstructionSet
&PossibleRedPHISet
,
274 SmallInstructionSet
&PossibleRedLastSet
) {
275 PossibleRedIdx
.clear();
276 PossibleRedIter
.clear();
279 for (unsigned i
= 0, e
= PossibleReds
.size(); i
!= e
; ++i
)
280 if (PossibleReds
[i
].size() % Scale
== 0) {
281 PossibleRedLastSet
.insert(PossibleReds
[i
].getReducedValue());
282 PossibleRedPHISet
.insert(PossibleReds
[i
].getPHI());
284 PossibleRedSet
.insert(PossibleReds
[i
].getPHI());
285 PossibleRedIdx
[PossibleReds
[i
].getPHI()] = i
;
286 for (Instruction
*J
: PossibleReds
[i
]) {
287 PossibleRedSet
.insert(J
);
288 PossibleRedIdx
[J
] = i
;
293 // The functions below are used while processing the loop instructions.
295 // Are the two instructions both from reductions, and furthermore, from
296 // the same reduction?
297 bool isPairInSame(Instruction
*J1
, Instruction
*J2
) {
298 DenseMap
<Instruction
*, int>::iterator J1I
= PossibleRedIdx
.find(J1
);
299 if (J1I
!= PossibleRedIdx
.end()) {
300 DenseMap
<Instruction
*, int>::iterator J2I
= PossibleRedIdx
.find(J2
);
301 if (J2I
!= PossibleRedIdx
.end() && J1I
->second
== J2I
->second
)
308 // The two provided instructions, the first from the base iteration, and
309 // the second from iteration i, form a matched pair. If these are part of
310 // a reduction, record that fact.
311 void recordPair(Instruction
*J1
, Instruction
*J2
, unsigned i
) {
312 if (PossibleRedIdx
.count(J1
)) {
313 assert(PossibleRedIdx
.count(J2
) &&
314 "Recording reduction vs. non-reduction instruction?");
316 PossibleRedIter
[J1
] = 0;
317 PossibleRedIter
[J2
] = i
;
319 int Idx
= PossibleRedIdx
[J1
];
320 assert(Idx
== PossibleRedIdx
[J2
] &&
321 "Recording pair from different reductions?");
326 // The functions below can be called after we've finished processing all
327 // instructions in the loop, and we know which reductions were selected.
329 bool validateSelected();
330 void replaceSelected();
333 // The vector of all possible reductions (for any scale).
334 SmallReductionVector PossibleReds
;
336 DenseMap
<Instruction
*, int> PossibleRedIdx
;
337 DenseMap
<Instruction
*, int> PossibleRedIter
;
341 // A DAGRootSet models an induction variable being used in a rerollable
342 // loop. For example,
348 // Base instruction -> i*3
351 // ST[y1] +1 +2 <-- Roots
355 // There may be multiple DAGRoots, for example:
357 // x[i*2+0] = ... (1)
358 // x[i*2+1] = ... (1)
359 // x[i*2+4] = ... (2)
360 // x[i*2+5] = ... (2)
361 // x[(i+1234)*2+5678] = ... (3)
362 // x[(i+1234)*2+5679] = ... (3)
364 // The loop will be rerolled by adding a new loop induction variable,
365 // one for the Base instruction in each DAGRootSet.
368 Instruction
*BaseInst
;
369 SmallInstructionVector Roots
;
371 // The instructions between IV and BaseInst (but not including BaseInst).
372 SmallInstructionSet SubsumedInsts
;
375 // The set of all DAG roots, and state tracking of all roots
376 // for a particular induction variable.
377 struct DAGRootTracker
{
378 DAGRootTracker(LoopReroll
*Parent
, Loop
*L
, Instruction
*IV
,
379 ScalarEvolution
*SE
, AliasAnalysis
*AA
,
380 TargetLibraryInfo
*TLI
, DominatorTree
*DT
, LoopInfo
*LI
,
382 DenseMap
<Instruction
*, int64_t> &IncrMap
,
383 Instruction
*LoopCtrlIV
)
384 : Parent(Parent
), L(L
), SE(SE
), AA(AA
), TLI(TLI
), DT(DT
), LI(LI
),
385 PreserveLCSSA(PreserveLCSSA
), IV(IV
), IVToIncMap(IncrMap
),
386 LoopControlIV(LoopCtrlIV
) {}
388 /// Stage 1: Find all the DAG roots for the induction variable.
391 /// Stage 2: Validate if the found roots are valid.
392 bool validate(ReductionTracker
&Reductions
);
394 /// Stage 3: Assuming validate() returned true, perform the
396 /// @param BackedgeTakenCount The backedge-taken count of L.
397 void replace(const SCEV
*BackedgeTakenCount
);
400 using UsesTy
= MapVector
<Instruction
*, BitVector
>;
402 void findRootsRecursive(Instruction
*IVU
,
403 SmallInstructionSet SubsumedInsts
);
404 bool findRootsBase(Instruction
*IVU
, SmallInstructionSet SubsumedInsts
);
405 bool collectPossibleRoots(Instruction
*Base
,
406 std::map
<int64_t,Instruction
*> &Roots
);
407 bool validateRootSet(DAGRootSet
&DRS
);
409 bool collectUsedInstructions(SmallInstructionSet
&PossibleRedSet
);
410 void collectInLoopUserSet(const SmallInstructionVector
&Roots
,
411 const SmallInstructionSet
&Exclude
,
412 const SmallInstructionSet
&Final
,
413 DenseSet
<Instruction
*> &Users
);
414 void collectInLoopUserSet(Instruction
*Root
,
415 const SmallInstructionSet
&Exclude
,
416 const SmallInstructionSet
&Final
,
417 DenseSet
<Instruction
*> &Users
);
419 UsesTy::iterator
nextInstr(int Val
, UsesTy
&In
,
420 const SmallInstructionSet
&Exclude
,
421 UsesTy::iterator
*StartI
=nullptr);
422 bool isBaseInst(Instruction
*I
);
423 bool isRootInst(Instruction
*I
);
424 bool instrDependsOn(Instruction
*I
,
425 UsesTy::iterator Start
,
426 UsesTy::iterator End
);
427 void replaceIV(DAGRootSet
&DRS
, const SCEV
*Start
, const SCEV
*IncrExpr
);
431 // Members of Parent, replicated here for brevity.
435 TargetLibraryInfo
*TLI
;
440 // The loop induction variable.
446 // Loop reroll count; if Inc == 1, this records the scaling applied
447 // to the indvar: a[i*2+0] = ...; a[i*2+1] = ... ;
448 // If Inc is not 1, Scale = Inc.
451 // The roots themselves.
452 SmallVector
<DAGRootSet
,16> RootSets
;
454 // All increment instructions for IV.
455 SmallInstructionVector LoopIncs
;
457 // Map of all instructions in the loop (in order) to the iterations
458 // they are used in (or specially, IL_All for instructions
459 // used in the loop increment mechanism).
462 // Map between induction variable and its increment
463 DenseMap
<Instruction
*, int64_t> &IVToIncMap
;
465 Instruction
*LoopControlIV
;
468 // Check if it is a compare-like instruction whose user is a branch
469 bool isCompareUsedByBranch(Instruction
*I
) {
470 auto *TI
= I
->getParent()->getTerminator();
471 if (!isa
<BranchInst
>(TI
) || !isa
<CmpInst
>(I
))
473 return I
->hasOneUse() && TI
->getOperand(0) == I
;
476 bool isLoopControlIV(Loop
*L
, Instruction
*IV
);
477 void collectPossibleIVs(Loop
*L
, SmallInstructionVector
&PossibleIVs
);
478 void collectPossibleReductions(Loop
*L
,
479 ReductionTracker
&Reductions
);
480 bool reroll(Instruction
*IV
, Loop
*L
, BasicBlock
*Header
,
481 const SCEV
*BackedgeTakenCount
, ReductionTracker
&Reductions
);
484 } // end anonymous namespace
486 char LoopReroll::ID
= 0;
488 INITIALIZE_PASS_BEGIN(LoopReroll
, "loop-reroll", "Reroll loops", false, false)
489 INITIALIZE_PASS_DEPENDENCY(LoopPass
)
490 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass
)
491 INITIALIZE_PASS_END(LoopReroll
, "loop-reroll", "Reroll loops", false, false)
493 Pass
*llvm::createLoopRerollPass() {
494 return new LoopReroll
;
497 // Returns true if the provided instruction is used outside the given loop.
498 // This operates like Instruction::isUsedOutsideOfBlock, but considers PHIs in
499 // non-loop blocks to be outside the loop.
500 static bool hasUsesOutsideLoop(Instruction
*I
, Loop
*L
) {
501 for (User
*U
: I
->users()) {
502 if (!L
->contains(cast
<Instruction
>(U
)))
508 // Check if an IV is only used to control the loop. There are two cases:
509 // 1. It only has one use which is loop increment, and the increment is only
510 // used by comparison and the PHI (could has sext with nsw in between), and the
511 // comparison is only used by branch.
512 // 2. It is used by loop increment and the comparison, the loop increment is
513 // only used by the PHI, and the comparison is used only by the branch.
514 bool LoopReroll::isLoopControlIV(Loop
*L
, Instruction
*IV
) {
515 unsigned IVUses
= IV
->getNumUses();
516 if (IVUses
!= 2 && IVUses
!= 1)
519 for (auto *User
: IV
->users()) {
520 int32_t IncOrCmpUses
= User
->getNumUses();
521 bool IsCompInst
= isCompareUsedByBranch(cast
<Instruction
>(User
));
523 // User can only have one or two uses.
524 if (IncOrCmpUses
!= 2 && IncOrCmpUses
!= 1)
529 // The only user must be the loop increment.
530 // The loop increment must have two uses.
531 if (IsCompInst
|| IncOrCmpUses
!= 2)
536 if (IVUses
== 2 && IncOrCmpUses
!= 1)
539 // The users of the IV must be a binary operation or a comparison
540 if (auto *BO
= dyn_cast
<BinaryOperator
>(User
)) {
541 if (BO
->getOpcode() == Instruction::Add
) {
543 // User of Loop Increment should be either PHI or CMP
544 for (auto *UU
: User
->users()) {
545 if (PHINode
*PN
= dyn_cast
<PHINode
>(UU
)) {
549 // Must be a CMP or an ext (of a value with nsw) then CMP
551 Instruction
*UUser
= dyn_cast
<Instruction
>(UU
);
552 // Skip SExt if we are extending an nsw value
553 // TODO: Allow ZExt too
554 if (BO
->hasNoSignedWrap() && UUser
&& UUser
->hasOneUse() &&
555 isa
<SExtInst
>(UUser
))
556 UUser
= dyn_cast
<Instruction
>(*(UUser
->user_begin()));
557 if (!isCompareUsedByBranch(UUser
))
563 // Compare : can only have one use, and must be branch
564 } else if (!IsCompInst
)
570 // Collect the list of loop induction variables with respect to which it might
571 // be possible to reroll the loop.
572 void LoopReroll::collectPossibleIVs(Loop
*L
,
573 SmallInstructionVector
&PossibleIVs
) {
574 BasicBlock
*Header
= L
->getHeader();
575 for (BasicBlock::iterator I
= Header
->begin(),
576 IE
= Header
->getFirstInsertionPt(); I
!= IE
; ++I
) {
577 if (!isa
<PHINode
>(I
))
579 if (!I
->getType()->isIntegerTy() && !I
->getType()->isPointerTy())
582 if (const SCEVAddRecExpr
*PHISCEV
=
583 dyn_cast
<SCEVAddRecExpr
>(SE
->getSCEV(&*I
))) {
584 if (PHISCEV
->getLoop() != L
)
586 if (!PHISCEV
->isAffine())
588 auto IncSCEV
= dyn_cast
<SCEVConstant
>(PHISCEV
->getStepRecurrence(*SE
));
590 IVToIncMap
[&*I
] = IncSCEV
->getValue()->getSExtValue();
591 LLVM_DEBUG(dbgs() << "LRR: Possible IV: " << *I
<< " = " << *PHISCEV
594 if (isLoopControlIV(L
, &*I
)) {
595 assert(!LoopControlIV
&& "Found two loop control only IV");
596 LoopControlIV
= &(*I
);
597 LLVM_DEBUG(dbgs() << "LRR: Possible loop control only IV: " << *I
598 << " = " << *PHISCEV
<< "\n");
600 PossibleIVs
.push_back(&*I
);
606 // Add the remainder of the reduction-variable chain to the instruction vector
607 // (the initial PHINode has already been added). If successful, the object is
609 void LoopReroll::SimpleLoopReduction::add(Loop
*L
) {
610 assert(!Valid
&& "Cannot add to an already-valid chain");
612 // The reduction variable must be a chain of single-use instructions
613 // (including the PHI), except for the last value (which is used by the PHI
614 // and also outside the loop).
615 Instruction
*C
= Instructions
.front();
620 C
= cast
<Instruction
>(*C
->user_begin());
621 if (C
->hasOneUse()) {
622 if (!C
->isBinaryOp())
625 if (!(isa
<PHINode
>(Instructions
.back()) ||
626 C
->isSameOperationAs(Instructions
.back())))
629 Instructions
.push_back(C
);
631 } while (C
->hasOneUse());
633 if (Instructions
.size() < 2 ||
634 !C
->isSameOperationAs(Instructions
.back()) ||
638 // C is now the (potential) last instruction in the reduction chain.
639 for (User
*U
: C
->users()) {
640 // The only in-loop user can be the initial PHI.
641 if (L
->contains(cast
<Instruction
>(U
)))
642 if (cast
<Instruction
>(U
) != Instructions
.front())
646 Instructions
.push_back(C
);
650 // Collect the vector of possible reduction variables.
651 void LoopReroll::collectPossibleReductions(Loop
*L
,
652 ReductionTracker
&Reductions
) {
653 BasicBlock
*Header
= L
->getHeader();
654 for (BasicBlock::iterator I
= Header
->begin(),
655 IE
= Header
->getFirstInsertionPt(); I
!= IE
; ++I
) {
656 if (!isa
<PHINode
>(I
))
658 if (!I
->getType()->isSingleValueType())
661 SimpleLoopReduction
SLR(&*I
, L
);
665 LLVM_DEBUG(dbgs() << "LRR: Possible reduction: " << *I
<< " (with "
666 << SLR
.size() << " chained instructions)\n");
667 Reductions
.addSLR(SLR
);
671 // Collect the set of all users of the provided root instruction. This set of
672 // users contains not only the direct users of the root instruction, but also
673 // all users of those users, and so on. There are two exceptions:
675 // 1. Instructions in the set of excluded instructions are never added to the
676 // use set (even if they are users). This is used, for example, to exclude
677 // including root increments in the use set of the primary IV.
679 // 2. Instructions in the set of final instructions are added to the use set
680 // if they are users, but their users are not added. This is used, for
681 // example, to prevent a reduction update from forcing all later reduction
682 // updates into the use set.
683 void LoopReroll::DAGRootTracker::collectInLoopUserSet(
684 Instruction
*Root
, const SmallInstructionSet
&Exclude
,
685 const SmallInstructionSet
&Final
,
686 DenseSet
<Instruction
*> &Users
) {
687 SmallInstructionVector
Queue(1, Root
);
688 while (!Queue
.empty()) {
689 Instruction
*I
= Queue
.pop_back_val();
690 if (!Users
.insert(I
).second
)
694 for (Use
&U
: I
->uses()) {
695 Instruction
*User
= cast
<Instruction
>(U
.getUser());
696 if (PHINode
*PN
= dyn_cast
<PHINode
>(User
)) {
697 // Ignore "wrap-around" uses to PHIs of this loop's header.
698 if (PN
->getIncomingBlock(U
) == L
->getHeader())
702 if (L
->contains(User
) && !Exclude
.count(User
)) {
703 Queue
.push_back(User
);
707 // We also want to collect single-user "feeder" values.
708 for (User::op_iterator OI
= I
->op_begin(),
709 OIE
= I
->op_end(); OI
!= OIE
; ++OI
) {
710 if (Instruction
*Op
= dyn_cast
<Instruction
>(*OI
))
711 if (Op
->hasOneUse() && L
->contains(Op
) && !Exclude
.count(Op
) &&
718 // Collect all of the users of all of the provided root instructions (combined
719 // into a single set).
720 void LoopReroll::DAGRootTracker::collectInLoopUserSet(
721 const SmallInstructionVector
&Roots
,
722 const SmallInstructionSet
&Exclude
,
723 const SmallInstructionSet
&Final
,
724 DenseSet
<Instruction
*> &Users
) {
725 for (Instruction
*Root
: Roots
)
726 collectInLoopUserSet(Root
, Exclude
, Final
, Users
);
729 static bool isUnorderedLoadStore(Instruction
*I
) {
730 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(I
))
731 return LI
->isUnordered();
732 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(I
))
733 return SI
->isUnordered();
734 if (MemIntrinsic
*MI
= dyn_cast
<MemIntrinsic
>(I
))
735 return !MI
->isVolatile();
739 /// Return true if IVU is a "simple" arithmetic operation.
740 /// This is used for narrowing the search space for DAGRoots; only arithmetic
741 /// and GEPs can be part of a DAGRoot.
742 static bool isSimpleArithmeticOp(User
*IVU
) {
743 if (Instruction
*I
= dyn_cast
<Instruction
>(IVU
)) {
744 switch (I
->getOpcode()) {
745 default: return false;
746 case Instruction::Add
:
747 case Instruction::Sub
:
748 case Instruction::Mul
:
749 case Instruction::Shl
:
750 case Instruction::AShr
:
751 case Instruction::LShr
:
752 case Instruction::GetElementPtr
:
753 case Instruction::Trunc
:
754 case Instruction::ZExt
:
755 case Instruction::SExt
:
762 static bool isLoopIncrement(User
*U
, Instruction
*IV
) {
763 BinaryOperator
*BO
= dyn_cast
<BinaryOperator
>(U
);
765 if ((BO
&& BO
->getOpcode() != Instruction::Add
) ||
766 (!BO
&& !isa
<GetElementPtrInst
>(U
)))
769 for (auto *UU
: U
->users()) {
770 PHINode
*PN
= dyn_cast
<PHINode
>(UU
);
777 bool LoopReroll::DAGRootTracker::
778 collectPossibleRoots(Instruction
*Base
, std::map
<int64_t,Instruction
*> &Roots
) {
779 SmallInstructionVector BaseUsers
;
781 for (auto *I
: Base
->users()) {
782 ConstantInt
*CI
= nullptr;
784 if (isLoopIncrement(I
, IV
)) {
785 LoopIncs
.push_back(cast
<Instruction
>(I
));
789 // The root nodes must be either GEPs, ORs or ADDs.
790 if (auto *BO
= dyn_cast
<BinaryOperator
>(I
)) {
791 if (BO
->getOpcode() == Instruction::Add
||
792 BO
->getOpcode() == Instruction::Or
)
793 CI
= dyn_cast
<ConstantInt
>(BO
->getOperand(1));
794 } else if (auto *GEP
= dyn_cast
<GetElementPtrInst
>(I
)) {
795 Value
*LastOperand
= GEP
->getOperand(GEP
->getNumOperands()-1);
796 CI
= dyn_cast
<ConstantInt
>(LastOperand
);
800 if (Instruction
*II
= dyn_cast
<Instruction
>(I
)) {
801 BaseUsers
.push_back(II
);
804 LLVM_DEBUG(dbgs() << "LRR: Aborting due to non-instruction: " << *I
810 int64_t V
= std::abs(CI
->getValue().getSExtValue());
811 if (Roots
.find(V
) != Roots
.end())
812 // No duplicates, please.
815 Roots
[V
] = cast
<Instruction
>(I
);
818 // Make sure we have at least two roots.
819 if (Roots
.empty() || (Roots
.size() == 1 && BaseUsers
.empty()))
822 // If we found non-loop-inc, non-root users of Base, assume they are
823 // for the zeroth root index. This is because "add %a, 0" gets optimized
825 if (BaseUsers
.size()) {
826 if (Roots
.find(0) != Roots
.end()) {
827 LLVM_DEBUG(dbgs() << "LRR: Multiple roots found for base - aborting!\n");
833 // Calculate the number of users of the base, or lowest indexed, iteration.
834 unsigned NumBaseUses
= BaseUsers
.size();
835 if (NumBaseUses
== 0)
836 NumBaseUses
= Roots
.begin()->second
->getNumUses();
838 // Check that every node has the same number of users.
839 for (auto &KV
: Roots
) {
842 if (!KV
.second
->hasNUses(NumBaseUses
)) {
843 LLVM_DEBUG(dbgs() << "LRR: Aborting - Root and Base #users not the same: "
844 << "#Base=" << NumBaseUses
845 << ", #Root=" << KV
.second
->getNumUses() << "\n");
853 void LoopReroll::DAGRootTracker::
854 findRootsRecursive(Instruction
*I
, SmallInstructionSet SubsumedInsts
) {
855 // Does the user look like it could be part of a root set?
856 // All its users must be simple arithmetic ops.
857 if (I
->hasNUsesOrMore(IL_MaxRerollIterations
+ 1))
860 if (I
!= IV
&& findRootsBase(I
, SubsumedInsts
))
863 SubsumedInsts
.insert(I
);
865 for (User
*V
: I
->users()) {
866 Instruction
*I
= cast
<Instruction
>(V
);
867 if (is_contained(LoopIncs
, I
))
870 if (!isSimpleArithmeticOp(I
))
873 // The recursive call makes a copy of SubsumedInsts.
874 findRootsRecursive(I
, SubsumedInsts
);
878 bool LoopReroll::DAGRootTracker::validateRootSet(DAGRootSet
&DRS
) {
879 if (DRS
.Roots
.empty())
882 // Consider a DAGRootSet with N-1 roots (so N different values including
884 // Define d = Roots[0] - BaseInst, which should be the same as
885 // Roots[I] - Roots[I-1] for all I in [1..N).
886 // Define D = BaseInst@J - BaseInst@J-1, where "@J" means the value at the
889 // Now, For the loop iterations to be consecutive:
891 const auto *ADR
= dyn_cast
<SCEVAddRecExpr
>(SE
->getSCEV(DRS
.BaseInst
));
895 // Check that the first root is evenly spaced.
896 unsigned N
= DRS
.Roots
.size() + 1;
897 const SCEV
*StepSCEV
= SE
->getMinusSCEV(SE
->getSCEV(DRS
.Roots
[0]), ADR
);
898 const SCEV
*ScaleSCEV
= SE
->getConstant(StepSCEV
->getType(), N
);
899 if (ADR
->getStepRecurrence(*SE
) != SE
->getMulExpr(StepSCEV
, ScaleSCEV
))
902 // Check that the remainling roots are evenly spaced.
903 for (unsigned i
= 1; i
< N
- 1; ++i
) {
904 const SCEV
*NewStepSCEV
= SE
->getMinusSCEV(SE
->getSCEV(DRS
.Roots
[i
]),
905 SE
->getSCEV(DRS
.Roots
[i
-1]));
906 if (NewStepSCEV
!= StepSCEV
)
913 bool LoopReroll::DAGRootTracker::
914 findRootsBase(Instruction
*IVU
, SmallInstructionSet SubsumedInsts
) {
915 // The base of a RootSet must be an AddRec, so it can be erased.
916 const auto *IVU_ADR
= dyn_cast
<SCEVAddRecExpr
>(SE
->getSCEV(IVU
));
917 if (!IVU_ADR
|| IVU_ADR
->getLoop() != L
)
920 std::map
<int64_t, Instruction
*> V
;
921 if (!collectPossibleRoots(IVU
, V
))
924 // If we didn't get a root for index zero, then IVU must be
926 if (V
.find(0) == V
.end())
927 SubsumedInsts
.insert(IVU
);
929 // Partition the vector into monotonically increasing indexes.
931 DRS
.BaseInst
= nullptr;
933 SmallVector
<DAGRootSet
, 16> PotentialRootSets
;
937 DRS
.BaseInst
= KV
.second
;
938 DRS
.SubsumedInsts
= SubsumedInsts
;
939 } else if (DRS
.Roots
.empty()) {
940 DRS
.Roots
.push_back(KV
.second
);
941 } else if (V
.find(KV
.first
- 1) != V
.end()) {
942 DRS
.Roots
.push_back(KV
.second
);
944 // Linear sequence terminated.
945 if (!validateRootSet(DRS
))
948 // Construct a new DAGRootSet with the next sequence.
949 PotentialRootSets
.push_back(DRS
);
950 DRS
.BaseInst
= KV
.second
;
955 if (!validateRootSet(DRS
))
958 PotentialRootSets
.push_back(DRS
);
960 RootSets
.append(PotentialRootSets
.begin(), PotentialRootSets
.end());
965 bool LoopReroll::DAGRootTracker::findRoots() {
966 Inc
= IVToIncMap
[IV
];
968 assert(RootSets
.empty() && "Unclean state!");
969 if (std::abs(Inc
) == 1) {
970 for (auto *IVU
: IV
->users()) {
971 if (isLoopIncrement(IVU
, IV
))
972 LoopIncs
.push_back(cast
<Instruction
>(IVU
));
974 findRootsRecursive(IV
, SmallInstructionSet());
975 LoopIncs
.push_back(IV
);
977 if (!findRootsBase(IV
, SmallInstructionSet()))
981 // Ensure all sets have the same size.
982 if (RootSets
.empty()) {
983 LLVM_DEBUG(dbgs() << "LRR: Aborting because no root sets found!\n");
986 for (auto &V
: RootSets
) {
987 if (V
.Roots
.empty() || V
.Roots
.size() != RootSets
[0].Roots
.size()) {
990 << "LRR: Aborting because not all root sets have the same size\n");
995 Scale
= RootSets
[0].Roots
.size() + 1;
997 if (Scale
> IL_MaxRerollIterations
) {
998 LLVM_DEBUG(dbgs() << "LRR: Aborting - too many iterations found. "
999 << "#Found=" << Scale
1000 << ", #Max=" << IL_MaxRerollIterations
<< "\n");
1004 LLVM_DEBUG(dbgs() << "LRR: Successfully found roots: Scale=" << Scale
1010 bool LoopReroll::DAGRootTracker::collectUsedInstructions(SmallInstructionSet
&PossibleRedSet
) {
1011 // Populate the MapVector with all instructions in the block, in order first,
1012 // so we can iterate over the contents later in perfect order.
1013 for (auto &I
: *L
->getHeader()) {
1014 Uses
[&I
].resize(IL_End
);
1017 SmallInstructionSet Exclude
;
1018 for (auto &DRS
: RootSets
) {
1019 Exclude
.insert(DRS
.Roots
.begin(), DRS
.Roots
.end());
1020 Exclude
.insert(DRS
.SubsumedInsts
.begin(), DRS
.SubsumedInsts
.end());
1021 Exclude
.insert(DRS
.BaseInst
);
1023 Exclude
.insert(LoopIncs
.begin(), LoopIncs
.end());
1025 for (auto &DRS
: RootSets
) {
1026 DenseSet
<Instruction
*> VBase
;
1027 collectInLoopUserSet(DRS
.BaseInst
, Exclude
, PossibleRedSet
, VBase
);
1028 for (auto *I
: VBase
) {
1033 for (auto *Root
: DRS
.Roots
) {
1034 DenseSet
<Instruction
*> V
;
1035 collectInLoopUserSet(Root
, Exclude
, PossibleRedSet
, V
);
1037 // While we're here, check the use sets are the same size.
1038 if (V
.size() != VBase
.size()) {
1039 LLVM_DEBUG(dbgs() << "LRR: Aborting - use sets are different sizes\n");
1049 // Make sure our subsumed instructions are remembered too.
1050 for (auto *I
: DRS
.SubsumedInsts
) {
1051 Uses
[I
].set(IL_All
);
1055 // Make sure the loop increments are also accounted for.
1058 for (auto &DRS
: RootSets
) {
1059 Exclude
.insert(DRS
.Roots
.begin(), DRS
.Roots
.end());
1060 Exclude
.insert(DRS
.SubsumedInsts
.begin(), DRS
.SubsumedInsts
.end());
1061 Exclude
.insert(DRS
.BaseInst
);
1064 DenseSet
<Instruction
*> V
;
1065 collectInLoopUserSet(LoopIncs
, Exclude
, PossibleRedSet
, V
);
1067 Uses
[I
].set(IL_All
);
1073 /// Get the next instruction in "In" that is a member of set Val.
1074 /// Start searching from StartI, and do not return anything in Exclude.
1075 /// If StartI is not given, start from In.begin().
1076 LoopReroll::DAGRootTracker::UsesTy::iterator
1077 LoopReroll::DAGRootTracker::nextInstr(int Val
, UsesTy
&In
,
1078 const SmallInstructionSet
&Exclude
,
1079 UsesTy::iterator
*StartI
) {
1080 UsesTy::iterator I
= StartI
? *StartI
: In
.begin();
1081 while (I
!= In
.end() && (I
->second
.test(Val
) == 0 ||
1082 Exclude
.count(I
->first
) != 0))
1087 bool LoopReroll::DAGRootTracker::isBaseInst(Instruction
*I
) {
1088 for (auto &DRS
: RootSets
) {
1089 if (DRS
.BaseInst
== I
)
1095 bool LoopReroll::DAGRootTracker::isRootInst(Instruction
*I
) {
1096 for (auto &DRS
: RootSets
) {
1097 if (is_contained(DRS
.Roots
, I
))
1103 /// Return true if instruction I depends on any instruction between
1105 bool LoopReroll::DAGRootTracker::instrDependsOn(Instruction
*I
,
1106 UsesTy::iterator Start
,
1107 UsesTy::iterator End
) {
1108 for (auto *U
: I
->users()) {
1109 for (auto It
= Start
; It
!= End
; ++It
)
1116 static bool isIgnorableInst(const Instruction
*I
) {
1117 if (isa
<DbgInfoIntrinsic
>(I
))
1119 const IntrinsicInst
* II
= dyn_cast
<IntrinsicInst
>(I
);
1122 switch (II
->getIntrinsicID()) {
1125 case Intrinsic::annotation
:
1126 case Intrinsic::ptr_annotation
:
1127 case Intrinsic::var_annotation
:
1128 // TODO: the following intrinsics may also be whitelisted:
1129 // lifetime_start, lifetime_end, invariant_start, invariant_end
1135 bool LoopReroll::DAGRootTracker::validate(ReductionTracker
&Reductions
) {
1136 // We now need to check for equivalence of the use graph of each root with
1137 // that of the primary induction variable (excluding the roots). Our goal
1138 // here is not to solve the full graph isomorphism problem, but rather to
1139 // catch common cases without a lot of work. As a result, we will assume
1140 // that the relative order of the instructions in each unrolled iteration
1141 // is the same (although we will not make an assumption about how the
1142 // different iterations are intermixed). Note that while the order must be
1143 // the same, the instructions may not be in the same basic block.
1145 // An array of just the possible reductions for this scale factor. When we
1146 // collect the set of all users of some root instructions, these reduction
1147 // instructions are treated as 'final' (their uses are not considered).
1148 // This is important because we don't want the root use set to search down
1149 // the reduction chain.
1150 SmallInstructionSet PossibleRedSet
;
1151 SmallInstructionSet PossibleRedLastSet
;
1152 SmallInstructionSet PossibleRedPHISet
;
1153 Reductions
.restrictToScale(Scale
, PossibleRedSet
,
1154 PossibleRedPHISet
, PossibleRedLastSet
);
1156 // Populate "Uses" with where each instruction is used.
1157 if (!collectUsedInstructions(PossibleRedSet
))
1160 // Make sure we mark the reduction PHIs as used in all iterations.
1161 for (auto *I
: PossibleRedPHISet
) {
1162 Uses
[I
].set(IL_All
);
1165 // Make sure we mark loop-control-only PHIs as used in all iterations. See
1166 // comment above LoopReroll::isLoopControlIV for more information.
1167 BasicBlock
*Header
= L
->getHeader();
1168 if (LoopControlIV
&& LoopControlIV
!= IV
) {
1169 for (auto *U
: LoopControlIV
->users()) {
1170 Instruction
*IVUser
= dyn_cast
<Instruction
>(U
);
1171 // IVUser could be loop increment or compare
1172 Uses
[IVUser
].set(IL_All
);
1173 for (auto *UU
: IVUser
->users()) {
1174 Instruction
*UUser
= dyn_cast
<Instruction
>(UU
);
1175 // UUser could be compare, PHI or branch
1176 Uses
[UUser
].set(IL_All
);
1178 if (isa
<SExtInst
>(UUser
)) {
1179 UUser
= dyn_cast
<Instruction
>(*(UUser
->user_begin()));
1180 Uses
[UUser
].set(IL_All
);
1182 // Is UUser a compare instruction?
1183 if (UU
->hasOneUse()) {
1184 Instruction
*BI
= dyn_cast
<BranchInst
>(*UUser
->user_begin());
1185 if (BI
== cast
<BranchInst
>(Header
->getTerminator()))
1186 Uses
[BI
].set(IL_All
);
1192 // Make sure all instructions in the loop are in one and only one
1194 for (auto &KV
: Uses
) {
1195 if (KV
.second
.count() != 1 && !isIgnorableInst(KV
.first
)) {
1197 dbgs() << "LRR: Aborting - instruction is not used in 1 iteration: "
1198 << *KV
.first
<< " (#uses=" << KV
.second
.count() << ")\n");
1203 LLVM_DEBUG(for (auto &KV
1205 dbgs() << "LRR: " << KV
.second
.find_first() << "\t" << *KV
.first
<< "\n";
1208 for (unsigned Iter
= 1; Iter
< Scale
; ++Iter
) {
1209 // In addition to regular aliasing information, we need to look for
1210 // instructions from later (future) iterations that have side effects
1211 // preventing us from reordering them past other instructions with side
1213 bool FutureSideEffects
= false;
1214 AliasSetTracker
AST(*AA
);
1215 // The map between instructions in f(%iv.(i+1)) and f(%iv).
1216 DenseMap
<Value
*, Value
*> BaseMap
;
1218 // Compare iteration Iter to the base.
1219 SmallInstructionSet Visited
;
1220 auto BaseIt
= nextInstr(0, Uses
, Visited
);
1221 auto RootIt
= nextInstr(Iter
, Uses
, Visited
);
1222 auto LastRootIt
= Uses
.begin();
1224 while (BaseIt
!= Uses
.end() && RootIt
!= Uses
.end()) {
1225 Instruction
*BaseInst
= BaseIt
->first
;
1226 Instruction
*RootInst
= RootIt
->first
;
1228 // Skip over the IV or root instructions; only match their users.
1229 bool Continue
= false;
1230 if (isBaseInst(BaseInst
)) {
1231 Visited
.insert(BaseInst
);
1232 BaseIt
= nextInstr(0, Uses
, Visited
);
1235 if (isRootInst(RootInst
)) {
1236 LastRootIt
= RootIt
;
1237 Visited
.insert(RootInst
);
1238 RootIt
= nextInstr(Iter
, Uses
, Visited
);
1241 if (Continue
) continue;
1243 if (!BaseInst
->isSameOperationAs(RootInst
)) {
1244 // Last chance saloon. We don't try and solve the full isomorphism
1245 // problem, but try and at least catch the case where two instructions
1246 // *of different types* are round the wrong way. We won't be able to
1247 // efficiently tell, given two ADD instructions, which way around we
1248 // should match them, but given an ADD and a SUB, we can at least infer
1249 // which one is which.
1251 // This should allow us to deal with a greater subset of the isomorphism
1252 // problem. It does however change a linear algorithm into a quadratic
1253 // one, so limit the number of probes we do.
1254 auto TryIt
= RootIt
;
1255 unsigned N
= NumToleratedFailedMatches
;
1256 while (TryIt
!= Uses
.end() &&
1257 !BaseInst
->isSameOperationAs(TryIt
->first
) &&
1260 TryIt
= nextInstr(Iter
, Uses
, Visited
, &TryIt
);
1263 if (TryIt
== Uses
.end() || TryIt
== RootIt
||
1264 instrDependsOn(TryIt
->first
, RootIt
, TryIt
)) {
1265 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at "
1266 << *BaseInst
<< " vs. " << *RootInst
<< "\n");
1271 RootInst
= TryIt
->first
;
1274 // All instructions between the last root and this root
1275 // may belong to some other iteration. If they belong to a
1276 // future iteration, then they're dangerous to alias with.
1278 // Note that because we allow a limited amount of flexibility in the order
1279 // that we visit nodes, LastRootIt might be *before* RootIt, in which
1280 // case we've already checked this set of instructions so we shouldn't
1282 for (; LastRootIt
< RootIt
; ++LastRootIt
) {
1283 Instruction
*I
= LastRootIt
->first
;
1284 if (LastRootIt
->second
.find_first() < (int)Iter
)
1286 if (I
->mayWriteToMemory())
1288 // Note: This is specifically guarded by a check on isa<PHINode>,
1289 // which while a valid (somewhat arbitrary) micro-optimization, is
1290 // needed because otherwise isSafeToSpeculativelyExecute returns
1291 // false on PHI nodes.
1292 if (!isa
<PHINode
>(I
) && !isUnorderedLoadStore(I
) &&
1293 !isSafeToSpeculativelyExecute(I
))
1294 // Intervening instructions cause side effects.
1295 FutureSideEffects
= true;
1298 // Make sure that this instruction, which is in the use set of this
1299 // root instruction, does not also belong to the base set or the set of
1300 // some other root instruction.
1301 if (RootIt
->second
.count() > 1) {
1302 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
1303 << " vs. " << *RootInst
<< " (prev. case overlap)\n");
1307 // Make sure that we don't alias with any instruction in the alias set
1308 // tracker. If we do, then we depend on a future iteration, and we
1310 if (RootInst
->mayReadFromMemory())
1311 for (auto &K
: AST
) {
1312 if (K
.aliasesUnknownInst(RootInst
, *AA
)) {
1313 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at "
1314 << *BaseInst
<< " vs. " << *RootInst
1315 << " (depends on future store)\n");
1320 // If we've past an instruction from a future iteration that may have
1321 // side effects, and this instruction might also, then we can't reorder
1322 // them, and this matching fails. As an exception, we allow the alias
1323 // set tracker to handle regular (unordered) load/store dependencies.
1324 if (FutureSideEffects
&& ((!isUnorderedLoadStore(BaseInst
) &&
1325 !isSafeToSpeculativelyExecute(BaseInst
)) ||
1326 (!isUnorderedLoadStore(RootInst
) &&
1327 !isSafeToSpeculativelyExecute(RootInst
)))) {
1328 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
1329 << " vs. " << *RootInst
1330 << " (side effects prevent reordering)\n");
1334 // For instructions that are part of a reduction, if the operation is
1335 // associative, then don't bother matching the operands (because we
1336 // already know that the instructions are isomorphic, and the order
1337 // within the iteration does not matter). For non-associative reductions,
1338 // we do need to match the operands, because we need to reject
1339 // out-of-order instructions within an iteration!
1340 // For example (assume floating-point addition), we need to reject this:
1341 // x += a[i]; x += b[i];
1342 // x += a[i+1]; x += b[i+1];
1343 // x += b[i+2]; x += a[i+2];
1344 bool InReduction
= Reductions
.isPairInSame(BaseInst
, RootInst
);
1346 if (!(InReduction
&& BaseInst
->isAssociative())) {
1347 bool Swapped
= false, SomeOpMatched
= false;
1348 for (unsigned j
= 0; j
< BaseInst
->getNumOperands(); ++j
) {
1349 Value
*Op2
= RootInst
->getOperand(j
);
1351 // If this is part of a reduction (and the operation is not
1352 // associatve), then we match all operands, but not those that are
1353 // part of the reduction.
1355 if (Instruction
*Op2I
= dyn_cast
<Instruction
>(Op2
))
1356 if (Reductions
.isPairInSame(RootInst
, Op2I
))
1359 DenseMap
<Value
*, Value
*>::iterator BMI
= BaseMap
.find(Op2
);
1360 if (BMI
!= BaseMap
.end()) {
1363 for (auto &DRS
: RootSets
) {
1364 if (DRS
.Roots
[Iter
-1] == (Instruction
*) Op2
) {
1371 if (BaseInst
->getOperand(Swapped
? unsigned(!j
) : j
) != Op2
) {
1372 // If we've not already decided to swap the matched operands, and
1373 // we've not already matched our first operand (note that we could
1374 // have skipped matching the first operand because it is part of a
1375 // reduction above), and the instruction is commutative, then try
1376 // the swapped match.
1377 if (!Swapped
&& BaseInst
->isCommutative() && !SomeOpMatched
&&
1378 BaseInst
->getOperand(!j
) == Op2
) {
1382 << "LRR: iteration root match failed at " << *BaseInst
1383 << " vs. " << *RootInst
<< " (operand " << j
<< ")\n");
1388 SomeOpMatched
= true;
1392 if ((!PossibleRedLastSet
.count(BaseInst
) &&
1393 hasUsesOutsideLoop(BaseInst
, L
)) ||
1394 (!PossibleRedLastSet
.count(RootInst
) &&
1395 hasUsesOutsideLoop(RootInst
, L
))) {
1396 LLVM_DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst
1397 << " vs. " << *RootInst
<< " (uses outside loop)\n");
1401 Reductions
.recordPair(BaseInst
, RootInst
, Iter
);
1402 BaseMap
.insert(std::make_pair(RootInst
, BaseInst
));
1404 LastRootIt
= RootIt
;
1405 Visited
.insert(BaseInst
);
1406 Visited
.insert(RootInst
);
1407 BaseIt
= nextInstr(0, Uses
, Visited
);
1408 RootIt
= nextInstr(Iter
, Uses
, Visited
);
1410 assert(BaseIt
== Uses
.end() && RootIt
== Uses
.end() &&
1411 "Mismatched set sizes!");
1414 LLVM_DEBUG(dbgs() << "LRR: Matched all iteration increments for " << *IV
1420 void LoopReroll::DAGRootTracker::replace(const SCEV
*BackedgeTakenCount
) {
1421 BasicBlock
*Header
= L
->getHeader();
1423 // Compute the start and increment for each BaseInst before we start erasing
1425 SmallVector
<const SCEV
*, 8> StartExprs
;
1426 SmallVector
<const SCEV
*, 8> IncrExprs
;
1427 for (auto &DRS
: RootSets
) {
1428 const SCEVAddRecExpr
*IVSCEV
=
1429 cast
<SCEVAddRecExpr
>(SE
->getSCEV(DRS
.BaseInst
));
1430 StartExprs
.push_back(IVSCEV
->getStart());
1431 IncrExprs
.push_back(SE
->getMinusSCEV(SE
->getSCEV(DRS
.Roots
[0]), IVSCEV
));
1434 // Remove instructions associated with non-base iterations.
1435 for (BasicBlock::reverse_iterator J
= Header
->rbegin(), JE
= Header
->rend();
1437 unsigned I
= Uses
[&*J
].find_first();
1438 if (I
> 0 && I
< IL_All
) {
1439 LLVM_DEBUG(dbgs() << "LRR: removing: " << *J
<< "\n");
1440 J
++->eraseFromParent();
1447 // Rewrite each BaseInst using SCEV.
1448 for (size_t i
= 0, e
= RootSets
.size(); i
!= e
; ++i
)
1449 // Insert the new induction variable.
1450 replaceIV(RootSets
[i
], StartExprs
[i
], IncrExprs
[i
]);
1452 { // Limit the lifetime of SCEVExpander.
1453 BranchInst
*BI
= cast
<BranchInst
>(Header
->getTerminator());
1454 const DataLayout
&DL
= Header
->getModule()->getDataLayout();
1455 SCEVExpander
Expander(*SE
, DL
, "reroll");
1456 auto Zero
= SE
->getZero(BackedgeTakenCount
->getType());
1457 auto One
= SE
->getOne(BackedgeTakenCount
->getType());
1458 auto NewIVSCEV
= SE
->getAddRecExpr(Zero
, One
, L
, SCEV::FlagAnyWrap
);
1460 Expander
.expandCodeFor(NewIVSCEV
, BackedgeTakenCount
->getType(),
1461 Header
->getFirstNonPHIOrDbg());
1462 // FIXME: This arithmetic can overflow.
1463 auto TripCount
= SE
->getAddExpr(BackedgeTakenCount
, One
);
1464 auto ScaledTripCount
= SE
->getMulExpr(
1465 TripCount
, SE
->getConstant(BackedgeTakenCount
->getType(), Scale
));
1466 auto ScaledBECount
= SE
->getMinusSCEV(ScaledTripCount
, One
);
1468 Expander
.expandCodeFor(ScaledBECount
, BackedgeTakenCount
->getType(),
1469 Header
->getFirstNonPHIOrDbg());
1471 new ICmpInst(BI
, CmpInst::ICMP_EQ
, NewIV
, TakenCount
, "exitcond");
1472 BI
->setCondition(Cond
);
1474 if (BI
->getSuccessor(1) != Header
)
1475 BI
->swapSuccessors();
1478 SimplifyInstructionsInBlock(Header
, TLI
);
1479 DeleteDeadPHIs(Header
, TLI
);
1482 void LoopReroll::DAGRootTracker::replaceIV(DAGRootSet
&DRS
,
1484 const SCEV
*IncrExpr
) {
1485 BasicBlock
*Header
= L
->getHeader();
1486 Instruction
*Inst
= DRS
.BaseInst
;
1488 const SCEV
*NewIVSCEV
=
1489 SE
->getAddRecExpr(Start
, IncrExpr
, L
, SCEV::FlagAnyWrap
);
1491 { // Limit the lifetime of SCEVExpander.
1492 const DataLayout
&DL
= Header
->getModule()->getDataLayout();
1493 SCEVExpander
Expander(*SE
, DL
, "reroll");
1494 Value
*NewIV
= Expander
.expandCodeFor(NewIVSCEV
, Inst
->getType(),
1495 Header
->getFirstNonPHIOrDbg());
1497 for (auto &KV
: Uses
)
1498 if (KV
.second
.find_first() == 0)
1499 KV
.first
->replaceUsesOfWith(Inst
, NewIV
);
1503 // Validate the selected reductions. All iterations must have an isomorphic
1504 // part of the reduction chain and, for non-associative reductions, the chain
1505 // entries must appear in order.
1506 bool LoopReroll::ReductionTracker::validateSelected() {
1507 // For a non-associative reduction, the chain entries must appear in order.
1508 for (int i
: Reds
) {
1509 int PrevIter
= 0, BaseCount
= 0, Count
= 0;
1510 for (Instruction
*J
: PossibleReds
[i
]) {
1511 // Note that all instructions in the chain must have been found because
1512 // all instructions in the function must have been assigned to some
1514 int Iter
= PossibleRedIter
[J
];
1515 if (Iter
!= PrevIter
&& Iter
!= PrevIter
+ 1 &&
1516 !PossibleReds
[i
].getReducedValue()->isAssociative()) {
1517 LLVM_DEBUG(dbgs() << "LRR: Out-of-order non-associative reduction: "
1522 if (Iter
!= PrevIter
) {
1523 if (Count
!= BaseCount
) {
1525 << "LRR: Iteration " << PrevIter
<< " reduction use count "
1526 << Count
<< " is not equal to the base use count "
1527 << BaseCount
<< "\n");
1545 // For all selected reductions, remove all parts except those in the first
1546 // iteration (and the PHI). Replace outside uses of the reduced value with uses
1547 // of the first-iteration reduced value (in other words, reroll the selected
1549 void LoopReroll::ReductionTracker::replaceSelected() {
1550 // Fixup reductions to refer to the last instruction associated with the
1551 // first iteration (not the last).
1552 for (int i
: Reds
) {
1554 for (int e
= PossibleReds
[i
].size(); j
!= e
; ++j
)
1555 if (PossibleRedIter
[PossibleReds
[i
][j
]] != 0) {
1560 // Replace users with the new end-of-chain value.
1561 SmallInstructionVector Users
;
1562 for (User
*U
: PossibleReds
[i
].getReducedValue()->users()) {
1563 Users
.push_back(cast
<Instruction
>(U
));
1566 for (Instruction
*User
: Users
)
1567 User
->replaceUsesOfWith(PossibleReds
[i
].getReducedValue(),
1568 PossibleReds
[i
][j
]);
1572 // Reroll the provided loop with respect to the provided induction variable.
1573 // Generally, we're looking for a loop like this:
1575 // %iv = phi [ (preheader, ...), (body, %iv.next) ]
1577 // %iv.1 = add %iv, 1 <-- a root increment
1579 // %iv.2 = add %iv, 2 <-- a root increment
1581 // %iv.scale_m_1 = add %iv, scale-1 <-- a root increment
1584 // %iv.next = add %iv, scale
1585 // %cmp = icmp(%iv, ...)
1586 // br %cmp, header, exit
1588 // Notably, we do not require that f(%iv), f(%iv.1), etc. be isolated groups of
1589 // instructions. In other words, the instructions in f(%iv), f(%iv.1), etc. can
1590 // be intermixed with eachother. The restriction imposed by this algorithm is
1591 // that the relative order of the isomorphic instructions in f(%iv), f(%iv.1),
1592 // etc. be the same.
1594 // First, we collect the use set of %iv, excluding the other increment roots.
1595 // This gives us f(%iv). Then we iterate over the loop instructions (scale-1)
1596 // times, having collected the use set of f(%iv.(i+1)), during which we:
1597 // - Ensure that the next unmatched instruction in f(%iv) is isomorphic to
1598 // the next unmatched instruction in f(%iv.(i+1)).
1599 // - Ensure that both matched instructions don't have any external users
1600 // (with the exception of last-in-chain reduction instructions).
1601 // - Track the (aliasing) write set, and other side effects, of all
1602 // instructions that belong to future iterations that come before the matched
1603 // instructions. If the matched instructions read from that write set, then
1604 // f(%iv) or f(%iv.(i+1)) has some dependency on instructions in
1605 // f(%iv.(j+1)) for some j > i, and we cannot reroll the loop. Similarly,
1606 // if any of these future instructions had side effects (could not be
1607 // speculatively executed), and so do the matched instructions, when we
1608 // cannot reorder those side-effect-producing instructions, and rerolling
1611 // Finally, we make sure that all loop instructions are either loop increment
1612 // roots, belong to simple latch code, parts of validated reductions, part of
1613 // f(%iv) or part of some f(%iv.i). If all of that is true (and all reductions
1614 // have been validated), then we reroll the loop.
1615 bool LoopReroll::reroll(Instruction
*IV
, Loop
*L
, BasicBlock
*Header
,
1616 const SCEV
*BackedgeTakenCount
,
1617 ReductionTracker
&Reductions
) {
1618 DAGRootTracker
DAGRoots(this, L
, IV
, SE
, AA
, TLI
, DT
, LI
, PreserveLCSSA
,
1619 IVToIncMap
, LoopControlIV
);
1621 if (!DAGRoots
.findRoots())
1623 LLVM_DEBUG(dbgs() << "LRR: Found all root induction increments for: " << *IV
1626 if (!DAGRoots
.validate(Reductions
))
1628 if (!Reductions
.validateSelected())
1630 // At this point, we've validated the rerolling, and we're committed to
1633 Reductions
.replaceSelected();
1634 DAGRoots
.replace(BackedgeTakenCount
);
1640 bool LoopReroll::runOnLoop(Loop
*L
, LPPassManager
&LPM
) {
1644 AA
= &getAnalysis
<AAResultsWrapperPass
>().getAAResults();
1645 LI
= &getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
1646 SE
= &getAnalysis
<ScalarEvolutionWrapperPass
>().getSE();
1647 TLI
= &getAnalysis
<TargetLibraryInfoWrapperPass
>().getTLI(
1648 *L
->getHeader()->getParent());
1649 DT
= &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
1650 PreserveLCSSA
= mustPreserveAnalysisID(LCSSAID
);
1652 BasicBlock
*Header
= L
->getHeader();
1653 LLVM_DEBUG(dbgs() << "LRR: F[" << Header
->getParent()->getName() << "] Loop %"
1654 << Header
->getName() << " (" << L
->getNumBlocks()
1657 // For now, we'll handle only single BB loops.
1658 if (L
->getNumBlocks() > 1)
1661 if (!SE
->hasLoopInvariantBackedgeTakenCount(L
))
1664 const SCEV
*BackedgeTakenCount
= SE
->getBackedgeTakenCount(L
);
1665 LLVM_DEBUG(dbgs() << "\n Before Reroll:\n" << *(L
->getHeader()) << "\n");
1666 LLVM_DEBUG(dbgs() << "LRR: backedge-taken count = " << *BackedgeTakenCount
1669 // First, we need to find the induction variable with respect to which we can
1670 // reroll (there may be several possible options).
1671 SmallInstructionVector PossibleIVs
;
1673 LoopControlIV
= nullptr;
1674 collectPossibleIVs(L
, PossibleIVs
);
1676 if (PossibleIVs
.empty()) {
1677 LLVM_DEBUG(dbgs() << "LRR: No possible IVs found\n");
1681 ReductionTracker Reductions
;
1682 collectPossibleReductions(L
, Reductions
);
1683 bool Changed
= false;
1685 // For each possible IV, collect the associated possible set of 'root' nodes
1686 // (i+1, i+2, etc.).
1687 for (Instruction
*PossibleIV
: PossibleIVs
)
1688 if (reroll(PossibleIV
, L
, Header
, BackedgeTakenCount
, Reductions
)) {
1692 LLVM_DEBUG(dbgs() << "\n After Reroll:\n" << *(L
->getHeader()) << "\n");
1694 // Trip count of L has changed so SE must be re-evaluated.