1*9880d681SAndroid Build Coastguard Worker //===- IVUsers.cpp - Induction Variable Users -------------------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements bookkeeping for "interesting" users of expressions
11*9880d681SAndroid Build Coastguard Worker // computed from induction variables.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AssumptionCache.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CodeMetrics.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/IVUsers.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/LoopPass.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolutionExpressions.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
31*9880d681SAndroid Build Coastguard Worker #include <algorithm>
32*9880d681SAndroid Build Coastguard Worker using namespace llvm;
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "iv-users"
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker char IVUsers::ID = 0;
37*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(IVUsers, "iv-users",
38*9880d681SAndroid Build Coastguard Worker "Induction Variable Users", false, true)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)39*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
40*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
41*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
42*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
43*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(IVUsers, "iv-users",
44*9880d681SAndroid Build Coastguard Worker "Induction Variable Users", false, true)
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker Pass *llvm::createIVUsersPass() {
47*9880d681SAndroid Build Coastguard Worker return new IVUsers();
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker /// isInteresting - Test whether the given expression is "interesting" when
51*9880d681SAndroid Build Coastguard Worker /// used by the given expression, within the context of analyzing the
52*9880d681SAndroid Build Coastguard Worker /// given loop.
isInteresting(const SCEV * S,const Instruction * I,const Loop * L,ScalarEvolution * SE,LoopInfo * LI)53*9880d681SAndroid Build Coastguard Worker static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L,
54*9880d681SAndroid Build Coastguard Worker ScalarEvolution *SE, LoopInfo *LI) {
55*9880d681SAndroid Build Coastguard Worker // An addrec is interesting if it's affine or if it has an interesting start.
56*9880d681SAndroid Build Coastguard Worker if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
57*9880d681SAndroid Build Coastguard Worker // Keep things simple. Don't touch loop-variant strides unless they're
58*9880d681SAndroid Build Coastguard Worker // only used outside the loop and we can simplify them.
59*9880d681SAndroid Build Coastguard Worker if (AR->getLoop() == L)
60*9880d681SAndroid Build Coastguard Worker return AR->isAffine() ||
61*9880d681SAndroid Build Coastguard Worker (!L->contains(I) &&
62*9880d681SAndroid Build Coastguard Worker SE->getSCEVAtScope(AR, LI->getLoopFor(I->getParent())) != AR);
63*9880d681SAndroid Build Coastguard Worker // Otherwise recurse to see if the start value is interesting, and that
64*9880d681SAndroid Build Coastguard Worker // the step value is not interesting, since we don't yet know how to
65*9880d681SAndroid Build Coastguard Worker // do effective SCEV expansions for addrecs with interesting steps.
66*9880d681SAndroid Build Coastguard Worker return isInteresting(AR->getStart(), I, L, SE, LI) &&
67*9880d681SAndroid Build Coastguard Worker !isInteresting(AR->getStepRecurrence(*SE), I, L, SE, LI);
68*9880d681SAndroid Build Coastguard Worker }
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Worker // An add is interesting if exactly one of its operands is interesting.
71*9880d681SAndroid Build Coastguard Worker if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
72*9880d681SAndroid Build Coastguard Worker bool AnyInterestingYet = false;
73*9880d681SAndroid Build Coastguard Worker for (SCEVAddExpr::op_iterator OI = Add->op_begin(), OE = Add->op_end();
74*9880d681SAndroid Build Coastguard Worker OI != OE; ++OI)
75*9880d681SAndroid Build Coastguard Worker if (isInteresting(*OI, I, L, SE, LI)) {
76*9880d681SAndroid Build Coastguard Worker if (AnyInterestingYet)
77*9880d681SAndroid Build Coastguard Worker return false;
78*9880d681SAndroid Build Coastguard Worker AnyInterestingYet = true;
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker return AnyInterestingYet;
81*9880d681SAndroid Build Coastguard Worker }
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker // Nothing else is interesting here.
84*9880d681SAndroid Build Coastguard Worker return false;
85*9880d681SAndroid Build Coastguard Worker }
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker /// Return true if all loop headers that dominate this block are in simplified
88*9880d681SAndroid Build Coastguard Worker /// form.
isSimplifiedLoopNest(BasicBlock * BB,const DominatorTree * DT,const LoopInfo * LI,SmallPtrSetImpl<Loop * > & SimpleLoopNests)89*9880d681SAndroid Build Coastguard Worker static bool isSimplifiedLoopNest(BasicBlock *BB, const DominatorTree *DT,
90*9880d681SAndroid Build Coastguard Worker const LoopInfo *LI,
91*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<Loop*> &SimpleLoopNests) {
92*9880d681SAndroid Build Coastguard Worker Loop *NearestLoop = nullptr;
93*9880d681SAndroid Build Coastguard Worker for (DomTreeNode *Rung = DT->getNode(BB);
94*9880d681SAndroid Build Coastguard Worker Rung; Rung = Rung->getIDom()) {
95*9880d681SAndroid Build Coastguard Worker BasicBlock *DomBB = Rung->getBlock();
96*9880d681SAndroid Build Coastguard Worker Loop *DomLoop = LI->getLoopFor(DomBB);
97*9880d681SAndroid Build Coastguard Worker if (DomLoop && DomLoop->getHeader() == DomBB) {
98*9880d681SAndroid Build Coastguard Worker // If the domtree walk reaches a loop with no preheader, return false.
99*9880d681SAndroid Build Coastguard Worker if (!DomLoop->isLoopSimplifyForm())
100*9880d681SAndroid Build Coastguard Worker return false;
101*9880d681SAndroid Build Coastguard Worker // If we have already checked this loop nest, stop checking.
102*9880d681SAndroid Build Coastguard Worker if (SimpleLoopNests.count(DomLoop))
103*9880d681SAndroid Build Coastguard Worker break;
104*9880d681SAndroid Build Coastguard Worker // If we have not already checked this loop nest, remember the loop
105*9880d681SAndroid Build Coastguard Worker // header nearest to BB. The nearest loop may not contain BB.
106*9880d681SAndroid Build Coastguard Worker if (!NearestLoop)
107*9880d681SAndroid Build Coastguard Worker NearestLoop = DomLoop;
108*9880d681SAndroid Build Coastguard Worker }
109*9880d681SAndroid Build Coastguard Worker }
110*9880d681SAndroid Build Coastguard Worker if (NearestLoop)
111*9880d681SAndroid Build Coastguard Worker SimpleLoopNests.insert(NearestLoop);
112*9880d681SAndroid Build Coastguard Worker return true;
113*9880d681SAndroid Build Coastguard Worker }
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker /// AddUsersImpl - Inspect the specified instruction. If it is a
116*9880d681SAndroid Build Coastguard Worker /// reducible SCEV, recursively add its users to the IVUsesByStride set and
117*9880d681SAndroid Build Coastguard Worker /// return true. Otherwise, return false.
AddUsersImpl(Instruction * I,SmallPtrSetImpl<Loop * > & SimpleLoopNests)118*9880d681SAndroid Build Coastguard Worker bool IVUsers::AddUsersImpl(Instruction *I,
119*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<Loop*> &SimpleLoopNests) {
120*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = I->getModule()->getDataLayout();
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker // Add this IV user to the Processed set before returning false to ensure that
123*9880d681SAndroid Build Coastguard Worker // all IV users are members of the set. See IVUsers::isIVUserOrOperand.
124*9880d681SAndroid Build Coastguard Worker if (!Processed.insert(I).second)
125*9880d681SAndroid Build Coastguard Worker return true; // Instruction already handled.
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker if (!SE->isSCEVable(I->getType()))
128*9880d681SAndroid Build Coastguard Worker return false; // Void and FP expressions cannot be reduced.
129*9880d681SAndroid Build Coastguard Worker
130*9880d681SAndroid Build Coastguard Worker // IVUsers is used by LSR which assumes that all SCEV expressions are safe to
131*9880d681SAndroid Build Coastguard Worker // pass to SCEVExpander. Expressions are not safe to expand if they represent
132*9880d681SAndroid Build Coastguard Worker // operations that are not safe to speculate, namely integer division.
133*9880d681SAndroid Build Coastguard Worker if (!isa<PHINode>(I) && !isSafeToSpeculativelyExecute(I))
134*9880d681SAndroid Build Coastguard Worker return false;
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker // LSR is not APInt clean, do not touch integers bigger than 64-bits.
137*9880d681SAndroid Build Coastguard Worker // Also avoid creating IVs of non-native types. For example, we don't want a
138*9880d681SAndroid Build Coastguard Worker // 64-bit IV in 32-bit code just because the loop has one 64-bit cast.
139*9880d681SAndroid Build Coastguard Worker uint64_t Width = SE->getTypeSizeInBits(I->getType());
140*9880d681SAndroid Build Coastguard Worker if (Width > 64 || !DL.isLegalInteger(Width))
141*9880d681SAndroid Build Coastguard Worker return false;
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker // Don't attempt to promote ephemeral values to indvars. They will be removed
144*9880d681SAndroid Build Coastguard Worker // later anyway.
145*9880d681SAndroid Build Coastguard Worker if (EphValues.count(I))
146*9880d681SAndroid Build Coastguard Worker return false;
147*9880d681SAndroid Build Coastguard Worker
148*9880d681SAndroid Build Coastguard Worker // Get the symbolic expression for this instruction.
149*9880d681SAndroid Build Coastguard Worker const SCEV *ISE = SE->getSCEV(I);
150*9880d681SAndroid Build Coastguard Worker
151*9880d681SAndroid Build Coastguard Worker // If we've come to an uninteresting expression, stop the traversal and
152*9880d681SAndroid Build Coastguard Worker // call this a user.
153*9880d681SAndroid Build Coastguard Worker if (!isInteresting(ISE, I, L, SE, LI))
154*9880d681SAndroid Build Coastguard Worker return false;
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker SmallPtrSet<Instruction *, 4> UniqueUsers;
157*9880d681SAndroid Build Coastguard Worker for (Use &U : I->uses()) {
158*9880d681SAndroid Build Coastguard Worker Instruction *User = cast<Instruction>(U.getUser());
159*9880d681SAndroid Build Coastguard Worker if (!UniqueUsers.insert(User).second)
160*9880d681SAndroid Build Coastguard Worker continue;
161*9880d681SAndroid Build Coastguard Worker
162*9880d681SAndroid Build Coastguard Worker // Do not infinitely recurse on PHI nodes.
163*9880d681SAndroid Build Coastguard Worker if (isa<PHINode>(User) && Processed.count(User))
164*9880d681SAndroid Build Coastguard Worker continue;
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker // Only consider IVUsers that are dominated by simplified loop
167*9880d681SAndroid Build Coastguard Worker // headers. Otherwise, SCEVExpander will crash.
168*9880d681SAndroid Build Coastguard Worker BasicBlock *UseBB = User->getParent();
169*9880d681SAndroid Build Coastguard Worker // A phi's use is live out of its predecessor block.
170*9880d681SAndroid Build Coastguard Worker if (PHINode *PHI = dyn_cast<PHINode>(User)) {
171*9880d681SAndroid Build Coastguard Worker unsigned OperandNo = U.getOperandNo();
172*9880d681SAndroid Build Coastguard Worker unsigned ValNo = PHINode::getIncomingValueNumForOperand(OperandNo);
173*9880d681SAndroid Build Coastguard Worker UseBB = PHI->getIncomingBlock(ValNo);
174*9880d681SAndroid Build Coastguard Worker }
175*9880d681SAndroid Build Coastguard Worker if (!isSimplifiedLoopNest(UseBB, DT, LI, SimpleLoopNests))
176*9880d681SAndroid Build Coastguard Worker return false;
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker // Descend recursively, but not into PHI nodes outside the current loop.
179*9880d681SAndroid Build Coastguard Worker // It's important to see the entire expression outside the loop to get
180*9880d681SAndroid Build Coastguard Worker // choices that depend on addressing mode use right, although we won't
181*9880d681SAndroid Build Coastguard Worker // consider references outside the loop in all cases.
182*9880d681SAndroid Build Coastguard Worker // If User is already in Processed, we don't want to recurse into it again,
183*9880d681SAndroid Build Coastguard Worker // but do want to record a second reference in the same instruction.
184*9880d681SAndroid Build Coastguard Worker bool AddUserToIVUsers = false;
185*9880d681SAndroid Build Coastguard Worker if (LI->getLoopFor(User->getParent()) != L) {
186*9880d681SAndroid Build Coastguard Worker if (isa<PHINode>(User) || Processed.count(User) ||
187*9880d681SAndroid Build Coastguard Worker !AddUsersImpl(User, SimpleLoopNests)) {
188*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n'
189*9880d681SAndroid Build Coastguard Worker << " OF SCEV: " << *ISE << '\n');
190*9880d681SAndroid Build Coastguard Worker AddUserToIVUsers = true;
191*9880d681SAndroid Build Coastguard Worker }
192*9880d681SAndroid Build Coastguard Worker } else if (Processed.count(User) || !AddUsersImpl(User, SimpleLoopNests)) {
193*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "FOUND USER: " << *User << '\n'
194*9880d681SAndroid Build Coastguard Worker << " OF SCEV: " << *ISE << '\n');
195*9880d681SAndroid Build Coastguard Worker AddUserToIVUsers = true;
196*9880d681SAndroid Build Coastguard Worker }
197*9880d681SAndroid Build Coastguard Worker
198*9880d681SAndroid Build Coastguard Worker if (AddUserToIVUsers) {
199*9880d681SAndroid Build Coastguard Worker // Okay, we found a user that we cannot reduce.
200*9880d681SAndroid Build Coastguard Worker IVStrideUse &NewUse = AddUser(User, I);
201*9880d681SAndroid Build Coastguard Worker // Autodetect the post-inc loop set, populating NewUse.PostIncLoops.
202*9880d681SAndroid Build Coastguard Worker // The regular return value here is discarded; instead of recording
203*9880d681SAndroid Build Coastguard Worker // it, we just recompute it when we need it.
204*9880d681SAndroid Build Coastguard Worker const SCEV *OriginalISE = ISE;
205*9880d681SAndroid Build Coastguard Worker ISE = TransformForPostIncUse(NormalizeAutodetect,
206*9880d681SAndroid Build Coastguard Worker ISE, User, I,
207*9880d681SAndroid Build Coastguard Worker NewUse.PostIncLoops,
208*9880d681SAndroid Build Coastguard Worker *SE, *DT);
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker // PostIncNormalization effectively simplifies the expression under
211*9880d681SAndroid Build Coastguard Worker // pre-increment assumptions. Those assumptions (no wrapping) might not
212*9880d681SAndroid Build Coastguard Worker // hold for the post-inc value. Catch such cases by making sure the
213*9880d681SAndroid Build Coastguard Worker // transformation is invertible.
214*9880d681SAndroid Build Coastguard Worker if (OriginalISE != ISE) {
215*9880d681SAndroid Build Coastguard Worker const SCEV *DenormalizedISE =
216*9880d681SAndroid Build Coastguard Worker TransformForPostIncUse(Denormalize, ISE, User, I,
217*9880d681SAndroid Build Coastguard Worker NewUse.PostIncLoops, *SE, *DT);
218*9880d681SAndroid Build Coastguard Worker
219*9880d681SAndroid Build Coastguard Worker // If we normalized the expression, but denormalization doesn't give the
220*9880d681SAndroid Build Coastguard Worker // original one, discard this user.
221*9880d681SAndroid Build Coastguard Worker if (OriginalISE != DenormalizedISE) {
222*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << " DISCARDING (NORMALIZATION ISN'T INVERTIBLE): "
223*9880d681SAndroid Build Coastguard Worker << *ISE << '\n');
224*9880d681SAndroid Build Coastguard Worker IVUses.pop_back();
225*9880d681SAndroid Build Coastguard Worker return false;
226*9880d681SAndroid Build Coastguard Worker }
227*9880d681SAndroid Build Coastguard Worker }
228*9880d681SAndroid Build Coastguard Worker DEBUG(if (SE->getSCEV(I) != ISE)
229*9880d681SAndroid Build Coastguard Worker dbgs() << " NORMALIZED TO: " << *ISE << '\n');
230*9880d681SAndroid Build Coastguard Worker }
231*9880d681SAndroid Build Coastguard Worker }
232*9880d681SAndroid Build Coastguard Worker return true;
233*9880d681SAndroid Build Coastguard Worker }
234*9880d681SAndroid Build Coastguard Worker
AddUsersIfInteresting(Instruction * I)235*9880d681SAndroid Build Coastguard Worker bool IVUsers::AddUsersIfInteresting(Instruction *I) {
236*9880d681SAndroid Build Coastguard Worker // SCEVExpander can only handle users that are dominated by simplified loop
237*9880d681SAndroid Build Coastguard Worker // entries. Keep track of all loops that are only dominated by other simple
238*9880d681SAndroid Build Coastguard Worker // loops so we don't traverse the domtree for each user.
239*9880d681SAndroid Build Coastguard Worker SmallPtrSet<Loop*,16> SimpleLoopNests;
240*9880d681SAndroid Build Coastguard Worker
241*9880d681SAndroid Build Coastguard Worker return AddUsersImpl(I, SimpleLoopNests);
242*9880d681SAndroid Build Coastguard Worker }
243*9880d681SAndroid Build Coastguard Worker
AddUser(Instruction * User,Value * Operand)244*9880d681SAndroid Build Coastguard Worker IVStrideUse &IVUsers::AddUser(Instruction *User, Value *Operand) {
245*9880d681SAndroid Build Coastguard Worker IVUses.push_back(new IVStrideUse(this, User, Operand));
246*9880d681SAndroid Build Coastguard Worker return IVUses.back();
247*9880d681SAndroid Build Coastguard Worker }
248*9880d681SAndroid Build Coastguard Worker
IVUsers()249*9880d681SAndroid Build Coastguard Worker IVUsers::IVUsers()
250*9880d681SAndroid Build Coastguard Worker : LoopPass(ID) {
251*9880d681SAndroid Build Coastguard Worker initializeIVUsersPass(*PassRegistry::getPassRegistry());
252*9880d681SAndroid Build Coastguard Worker }
253*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const254*9880d681SAndroid Build Coastguard Worker void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const {
255*9880d681SAndroid Build Coastguard Worker AU.addRequired<AssumptionCacheTracker>();
256*9880d681SAndroid Build Coastguard Worker AU.addRequired<LoopInfoWrapperPass>();
257*9880d681SAndroid Build Coastguard Worker AU.addRequired<DominatorTreeWrapperPass>();
258*9880d681SAndroid Build Coastguard Worker AU.addRequired<ScalarEvolutionWrapperPass>();
259*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll();
260*9880d681SAndroid Build Coastguard Worker }
261*9880d681SAndroid Build Coastguard Worker
runOnLoop(Loop * l,LPPassManager & LPM)262*9880d681SAndroid Build Coastguard Worker bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
263*9880d681SAndroid Build Coastguard Worker
264*9880d681SAndroid Build Coastguard Worker L = l;
265*9880d681SAndroid Build Coastguard Worker AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
266*9880d681SAndroid Build Coastguard Worker *L->getHeader()->getParent());
267*9880d681SAndroid Build Coastguard Worker LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
268*9880d681SAndroid Build Coastguard Worker DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
269*9880d681SAndroid Build Coastguard Worker SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard Worker // Collect ephemeral values so that AddUsersIfInteresting skips them.
272*9880d681SAndroid Build Coastguard Worker EphValues.clear();
273*9880d681SAndroid Build Coastguard Worker CodeMetrics::collectEphemeralValues(L, AC, EphValues);
274*9880d681SAndroid Build Coastguard Worker
275*9880d681SAndroid Build Coastguard Worker // Find all uses of induction variables in this loop, and categorize
276*9880d681SAndroid Build Coastguard Worker // them by stride. Start by finding all of the PHI nodes in the header for
277*9880d681SAndroid Build Coastguard Worker // this loop. If they are induction variables, inspect their uses.
278*9880d681SAndroid Build Coastguard Worker for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
279*9880d681SAndroid Build Coastguard Worker (void)AddUsersIfInteresting(&*I);
280*9880d681SAndroid Build Coastguard Worker
281*9880d681SAndroid Build Coastguard Worker return false;
282*9880d681SAndroid Build Coastguard Worker }
283*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS,const Module * M) const284*9880d681SAndroid Build Coastguard Worker void IVUsers::print(raw_ostream &OS, const Module *M) const {
285*9880d681SAndroid Build Coastguard Worker OS << "IV Users for loop ";
286*9880d681SAndroid Build Coastguard Worker L->getHeader()->printAsOperand(OS, false);
287*9880d681SAndroid Build Coastguard Worker if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
288*9880d681SAndroid Build Coastguard Worker OS << " with backedge-taken count "
289*9880d681SAndroid Build Coastguard Worker << *SE->getBackedgeTakenCount(L);
290*9880d681SAndroid Build Coastguard Worker }
291*9880d681SAndroid Build Coastguard Worker OS << ":\n";
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker for (const IVStrideUse &IVUse : IVUses) {
294*9880d681SAndroid Build Coastguard Worker OS << " ";
295*9880d681SAndroid Build Coastguard Worker IVUse.getOperandValToReplace()->printAsOperand(OS, false);
296*9880d681SAndroid Build Coastguard Worker OS << " = " << *getReplacementExpr(IVUse);
297*9880d681SAndroid Build Coastguard Worker for (auto PostIncLoop : IVUse.PostIncLoops) {
298*9880d681SAndroid Build Coastguard Worker OS << " (post-inc with loop ";
299*9880d681SAndroid Build Coastguard Worker PostIncLoop->getHeader()->printAsOperand(OS, false);
300*9880d681SAndroid Build Coastguard Worker OS << ")";
301*9880d681SAndroid Build Coastguard Worker }
302*9880d681SAndroid Build Coastguard Worker OS << " in ";
303*9880d681SAndroid Build Coastguard Worker if (IVUse.getUser())
304*9880d681SAndroid Build Coastguard Worker IVUse.getUser()->print(OS);
305*9880d681SAndroid Build Coastguard Worker else
306*9880d681SAndroid Build Coastguard Worker OS << "Printing <null> User";
307*9880d681SAndroid Build Coastguard Worker OS << '\n';
308*9880d681SAndroid Build Coastguard Worker }
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker
311*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const312*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD void IVUsers::dump() const {
313*9880d681SAndroid Build Coastguard Worker print(dbgs());
314*9880d681SAndroid Build Coastguard Worker }
315*9880d681SAndroid Build Coastguard Worker #endif
316*9880d681SAndroid Build Coastguard Worker
releaseMemory()317*9880d681SAndroid Build Coastguard Worker void IVUsers::releaseMemory() {
318*9880d681SAndroid Build Coastguard Worker Processed.clear();
319*9880d681SAndroid Build Coastguard Worker IVUses.clear();
320*9880d681SAndroid Build Coastguard Worker }
321*9880d681SAndroid Build Coastguard Worker
322*9880d681SAndroid Build Coastguard Worker /// getReplacementExpr - Return a SCEV expression which computes the
323*9880d681SAndroid Build Coastguard Worker /// value of the OperandValToReplace.
getReplacementExpr(const IVStrideUse & IU) const324*9880d681SAndroid Build Coastguard Worker const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const {
325*9880d681SAndroid Build Coastguard Worker return SE->getSCEV(IU.getOperandValToReplace());
326*9880d681SAndroid Build Coastguard Worker }
327*9880d681SAndroid Build Coastguard Worker
328*9880d681SAndroid Build Coastguard Worker /// getExpr - Return the expression for the use.
getExpr(const IVStrideUse & IU) const329*9880d681SAndroid Build Coastguard Worker const SCEV *IVUsers::getExpr(const IVStrideUse &IU) const {
330*9880d681SAndroid Build Coastguard Worker return
331*9880d681SAndroid Build Coastguard Worker TransformForPostIncUse(Normalize, getReplacementExpr(IU),
332*9880d681SAndroid Build Coastguard Worker IU.getUser(), IU.getOperandValToReplace(),
333*9880d681SAndroid Build Coastguard Worker const_cast<PostIncLoopSet &>(IU.getPostIncLoops()),
334*9880d681SAndroid Build Coastguard Worker *SE, *DT);
335*9880d681SAndroid Build Coastguard Worker }
336*9880d681SAndroid Build Coastguard Worker
findAddRecForLoop(const SCEV * S,const Loop * L)337*9880d681SAndroid Build Coastguard Worker static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {
338*9880d681SAndroid Build Coastguard Worker if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
339*9880d681SAndroid Build Coastguard Worker if (AR->getLoop() == L)
340*9880d681SAndroid Build Coastguard Worker return AR;
341*9880d681SAndroid Build Coastguard Worker return findAddRecForLoop(AR->getStart(), L);
342*9880d681SAndroid Build Coastguard Worker }
343*9880d681SAndroid Build Coastguard Worker
344*9880d681SAndroid Build Coastguard Worker if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
345*9880d681SAndroid Build Coastguard Worker for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
346*9880d681SAndroid Build Coastguard Worker I != E; ++I)
347*9880d681SAndroid Build Coastguard Worker if (const SCEVAddRecExpr *AR = findAddRecForLoop(*I, L))
348*9880d681SAndroid Build Coastguard Worker return AR;
349*9880d681SAndroid Build Coastguard Worker return nullptr;
350*9880d681SAndroid Build Coastguard Worker }
351*9880d681SAndroid Build Coastguard Worker
352*9880d681SAndroid Build Coastguard Worker return nullptr;
353*9880d681SAndroid Build Coastguard Worker }
354*9880d681SAndroid Build Coastguard Worker
getStride(const IVStrideUse & IU,const Loop * L) const355*9880d681SAndroid Build Coastguard Worker const SCEV *IVUsers::getStride(const IVStrideUse &IU, const Loop *L) const {
356*9880d681SAndroid Build Coastguard Worker if (const SCEVAddRecExpr *AR = findAddRecForLoop(getExpr(IU), L))
357*9880d681SAndroid Build Coastguard Worker return AR->getStepRecurrence(*SE);
358*9880d681SAndroid Build Coastguard Worker return nullptr;
359*9880d681SAndroid Build Coastguard Worker }
360*9880d681SAndroid Build Coastguard Worker
transformToPostInc(const Loop * L)361*9880d681SAndroid Build Coastguard Worker void IVStrideUse::transformToPostInc(const Loop *L) {
362*9880d681SAndroid Build Coastguard Worker PostIncLoops.insert(L);
363*9880d681SAndroid Build Coastguard Worker }
364*9880d681SAndroid Build Coastguard Worker
deleted()365*9880d681SAndroid Build Coastguard Worker void IVStrideUse::deleted() {
366*9880d681SAndroid Build Coastguard Worker // Remove this user from the list.
367*9880d681SAndroid Build Coastguard Worker Parent->Processed.erase(this->getUser());
368*9880d681SAndroid Build Coastguard Worker Parent->IVUses.erase(this);
369*9880d681SAndroid Build Coastguard Worker // this now dangles!
370*9880d681SAndroid Build Coastguard Worker }
371