annotate llvm/lib/Analysis/IVUsers.cpp @ 207:2e18cbf3894f

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