llvm.org GIT mirror llvm / 78ee4b6
[PM] Convert IVUsers analysis to new pass manager. Summary: Convert IVUsers analysis to new pass manager. Reviewers: davidxl, silvas Subscribers: junbuml, sanjoy, llvm-commits, mzolotukhin Differential Revision: https://reviews.llvm.org/D22434 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275698 91177308-0d34-0410-b5e6-96231b3b80d8 Dehao Chen 4 years ago
10 changed file(s) with 123 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
419419
420420 this->addNodeToList(New); // Notify traits that we added a node...
421421 return iterator(New);
422 }
423
424 iterator insert(iterator where, const NodeTy &New) {
425 return this->insert(where, new NodeTy(New));
422426 }
423427
424428 iterator insertAfter(iterator where, NodeTy *New) {
667671 typedef typename iplist::iterator iterator;
668672
669673 ilist() {}
670 ilist(const ilist &right) {
674 ilist(const ilist &right) : iplist() {
671675 insert(this->begin(), right.begin(), right.end());
672676 }
673677 explicit ilist(size_type count) {
116116 mutable ilist_node Sentinel;
117117 };
118118
119 class IVUsers : public LoopPass {
119 class IVUsers {
120120 friend class IVStrideUse;
121121 Loop *L;
122122 AssumptionCache *AC;
132132 // Ephemeral values used by @llvm.assume in this function.
133133 SmallPtrSet EphValues;
134134
135 void getAnalysisUsage(AnalysisUsage &AU) const override;
136
137 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
138
139 void releaseMemory() override;
140
141 public:
142 static char ID; // Pass ID, replacement for typeid
143 IVUsers();
135 public:
136 IVUsers(Loop *L, AssumptionCache *AC, LoopInfo *LI, DominatorTree *DT,
137 ScalarEvolution *SE);
144138
145139 Loop *getLoop() const { return L; }
146140
172166 return Processed.count(Inst);
173167 }
174168
175 void print(raw_ostream &OS, const Module* = nullptr) const override;
169 void releaseMemory();
170
171 void print(raw_ostream &OS, const Module * = nullptr) const;
176172
177173 /// dump - This method is used for debugging.
178174 void dump() const;
175
179176 protected:
180177 bool AddUsersImpl(Instruction *I, SmallPtrSetImpl &SimpleLoopNests);
181178 };
182179
183180 Pass *createIVUsersPass();
184181
182 class IVUsersWrapperPass : public LoopPass {
183 std::unique_ptr IU;
184
185 public:
186 static char ID;
187
188 IVUsersWrapperPass();
189
190 IVUsers &getIU() { return *IU; }
191 const IVUsers &getIU() const { return *IU; }
192
193 void getAnalysisUsage(AnalysisUsage &AU) const override;
194
195 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
196
197 void releaseMemory() override;
198
199 void print(raw_ostream &OS, const Module * = nullptr) const override;
200 };
201
202 /// Analysis pass that exposes the \c IVUsers for a loop.
203 class IVUsersAnalysis : public AnalysisInfoMixin {
204 friend AnalysisInfoMixin;
205 static char PassID;
206
207 public:
208 typedef IVUsers Result;
209
210 IVUsers run(Loop &L, AnalysisManager &AM);
211 };
212
213 /// Printer pass for the \c IVUsers for a loop.
214 class IVUsersPrinterPass : public PassInfoMixin {
215 raw_ostream &OS;
216
217 public:
218 explicit IVUsersPrinterPass(raw_ostream &OS) : OS(OS) {}
219 PreservedAnalyses run(Loop &L, AnalysisManager &AM);
220 };
185221 }
186222
187223 #endif
142142 void initializeIPCPPass(PassRegistry&);
143143 void initializeIPSCCPLegacyPassPass(PassRegistry &);
144144 void initializeIRTranslatorPass(PassRegistry &);
145 void initializeIVUsersPass(PassRegistry&);
145 void initializeIVUsersWrapperPassPass(PassRegistry&);
146146 void initializeIfConverterPass(PassRegistry&);
147147 void initializeImplicitNullChecksPass(PassRegistry&);
148148 void initializeIndVarSimplifyLegacyPassPass(PassRegistry&);
5050 initializePostDomOnlyPrinterPass(Registry);
5151 initializeAAResultsWrapperPassPass(Registry);
5252 initializeGlobalsAAWrapperPassPass(Registry);
53 initializeIVUsersPass(Registry);
53 initializeIVUsersWrapperPassPass(Registry);
5454 initializeInstCountPass(Registry);
5555 initializeIntervalPartitionPass(Registry);
5656 initializeLazyBlockFrequencyInfoPassPass(Registry);
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/Analysis/IVUsers.h"
1415 #include "llvm/ADT/STLExtras.h"
1516 #include "llvm/Analysis/AssumptionCache.h"
1617 #include "llvm/Analysis/CodeMetrics.h"
17 #include "llvm/Analysis/IVUsers.h"
1818 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/Analysis/LoopPassManager.h"
1920 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
2021 #include "llvm/Analysis/ValueTracking.h"
2122 #include "llvm/IR/Constants.h"
3233
3334 #define DEBUG_TYPE "iv-users"
3435
35 char IVUsers::ID = 0;
36 INITIALIZE_PASS_BEGIN(IVUsers, "iv-users",
36 char IVUsersAnalysis::PassID;
37
38 IVUsers IVUsersAnalysis::run(Loop &L, AnalysisManager &AM) {
39 const auto &FAM =
40 AM.getResult(L).getManager();
41 Function *F = L.getHeader()->getParent();
42
43 return IVUsers(&L, FAM.getCachedResult(*F),
44 FAM.getCachedResult(*F),
45 FAM.getCachedResult(*F),
46 FAM.getCachedResult(*F));
47 }
48
49 PreservedAnalyses IVUsersPrinterPass::run(Loop &L, AnalysisManager &AM) {
50 AM.getResult(L).print(OS);
51 return PreservedAnalyses::all();
52 }
53
54 char IVUsersWrapperPass::ID = 0;
55 INITIALIZE_PASS_BEGIN(IVUsersWrapperPass, "iv-users",
3756 "Induction Variable Users", false, true)
3857 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
3958 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
4059 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
4160 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
42 INITIALIZE_PASS_END(IVUsers, "iv-users",
43 "Induction Variable Users", false, true)
44
45 Pass *llvm::createIVUsersPass() {
46 return new IVUsers();
47 }
61 INITIALIZE_PASS_END(IVUsersWrapperPass, "iv-users", "Induction Variable Users",
62 false, true)
63
64 Pass *llvm::createIVUsersPass() { return new IVUsersWrapperPass(); }
4865
4966 /// isInteresting - Test whether the given expression is "interesting" when
5067 /// used by the given expression, within the context of analyzing the
245262 return IVUses.back();
246263 }
247264
248 IVUsers::IVUsers()
249 : LoopPass(ID) {
250 initializeIVUsersPass(*PassRegistry::getPassRegistry());
251 }
252
253 void IVUsers::getAnalysisUsage(AnalysisUsage &AU) const {
254 AU.addRequired();
255 AU.addRequired();
256 AU.addRequired();
257 AU.addRequired();
258 AU.setPreservesAll();
259 }
260
261 bool IVUsers::runOnLoop(Loop *l, LPPassManager &LPM) {
262
263 L = l;
264 AC = &getAnalysis().getAssumptionCache(
265 *L->getHeader()->getParent());
266 LI = &getAnalysis().getLoopInfo();
267 DT = &getAnalysis().getDomTree();
268 SE = &getAnalysis().getSE();
269
265 IVUsers::IVUsers(Loop *L, AssumptionCache *AC, LoopInfo *LI, DominatorTree *DT,
266 ScalarEvolution *SE)
267 : L(L), AC(AC), LI(LI), DT(DT), SE(SE), IVUses() {
270268 // Collect ephemeral values so that AddUsersIfInteresting skips them.
271269 EphValues.clear();
272270 CodeMetrics::collectEphemeralValues(L, AC, EphValues);
276274 // this loop. If they are induction variables, inspect their uses.
277275 for (BasicBlock::iterator I = L->getHeader()->begin(); isa(I); ++I)
278276 (void)AddUsersIfInteresting(&*I);
279
280 return false;
281277 }
282278
283279 void IVUsers::print(raw_ostream &OS, const Module *M) const {
284280 OS << "IV Users for loop ";
285281 L->getHeader()->printAsOperand(OS, false);
286282 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
287 OS << " with backedge-taken count "
288 << *SE->getBackedgeTakenCount(L);
283 OS << " with backedge-taken count " << *SE->getBackedgeTakenCount(L);
289284 }
290285 OS << ":\n";
291286
308303 }
309304
310305 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
311 LLVM_DUMP_METHOD void IVUsers::dump() const {
312 print(dbgs());
313 }
306 LLVM_DUMP_METHOD void IVUsers::dump() const { print(dbgs()); }
314307 #endif
315308
316309 void IVUsers::releaseMemory() {
317310 Processed.clear();
318311 IVUses.clear();
319312 }
313
314 IVUsersWrapperPass::IVUsersWrapperPass() : LoopPass(ID) {
315 initializeIVUsersWrapperPassPass(*PassRegistry::getPassRegistry());
316 }
317
318 void IVUsersWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
319 AU.addRequired();
320 AU.addRequired();
321 AU.addRequired();
322 AU.addRequired();
323 AU.setPreservesAll();
324 }
325
326 bool IVUsersWrapperPass::runOnLoop(Loop *L, LPPassManager &LPM) {
327 auto *AC = &getAnalysis().getAssumptionCache(
328 *L->getHeader()->getParent());
329 auto *LI = &getAnalysis().getLoopInfo();
330 auto *DT = &getAnalysis().getDomTree();
331 auto *SE = &getAnalysis().getSE();
332
333 IU.reset(new IVUsers(L, AC, LI, DT, SE));
334 return false;
335 }
336
337 void IVUsersWrapperPass::print(raw_ostream &OS, const Module *M) const {
338 IU->print(OS, M);
339 }
340
341 void IVUsersWrapperPass::releaseMemory() { IU->releaseMemory(); }
320342
321343 /// getReplacementExpr - Return a SCEV expression which computes the
322344 /// value of the OperandValToReplace.
7777 AU.addPreserved();
7878 AU.addPreserved();
7979 AU.addPreserved();
80 AU.addPreserved>();
80 AU.addPreservedWrapperPass>();
8181 AU.addPreserved();
8282 AU.addPreserved();
8383 AU.addPreserved();
3131 #include "llvm/Analysis/DependenceAnalysis.h"
3232 #include "llvm/Analysis/DominanceFrontier.h"
3333 #include "llvm/Analysis/GlobalsModRef.h"
34 #include "llvm/Analysis/IVUsers.h"
3435 #include "llvm/Analysis/LazyCallGraph.h"
3536 #include "llvm/Analysis/LazyValueInfo.h"
3637 #include "llvm/Analysis/LoopAccessAnalysis.h"
181181 #endif
182182 LOOP_ANALYSIS("no-op-loop", NoOpLoopAnalysis())
183183 LOOP_ANALYSIS("access-info", LoopAccessAnalysis())
184 LOOP_ANALYSIS("ivusers", IVUsersAnalysis())
184185 #undef LOOP_ANALYSIS
185186
186187 #ifndef LOOP_PASS
197198 LOOP_PASS("simplify-cfg", LoopSimplifyCFGPass())
198199 LOOP_PASS("indvars", IndVarSimplifyPass())
199200 LOOP_PASS("print-access-info", LoopAccessInfoPrinterPass(dbgs()))
201 LOOP_PASS("print", IVUsersPrinterPass(dbgs()))
200202 #undef LOOP_PASS
49484948 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
49494949 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
49504950 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
4951 INITIALIZE_PASS_DEPENDENCY(IVUsers)
4951 INITIALIZE_PASS_DEPENDENCY(IVUsersWrapperPass)
49524952 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
49534953 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
49544954 INITIALIZE_PASS_END(LoopStrengthReduce, "loop-reduce",
49784978 // Requiring LoopSimplify a second time here prevents IVUsers from running
49794979 // twice, since LoopSimplify was invalidated by running ScalarEvolution.
49804980 AU.addRequiredID(LoopSimplifyID);
4981 AU.addRequired();
4982 AU.addPreserveds>();
4981 AU.addRequireds>();
4982 AU.addPreserved();
49834983 AU.addRequired();
49844984 }
49854985
49874987 if (skipLoop(L))
49884988 return false;
49894989
4990 auto &IU = getAnalysis>();
4990 auto &IU = getAnalysisWrapperPass>().getIU();
49914991 auto &SE = getAnalysis().getSE();
49924992 auto &DT = getAnalysis().getDomTree();
49934993 auto &LI = getAnalysis().getLoopInfo();
0 ; RUN: opt < %s -analyze -iv-users | FileCheck %s
1 ; RUN: opt -passes='function(require),print' -S < %s 2>&1| FileCheck %s
12
23 ; Provide legal integer types.
34 target datalayout = "n8:16:32:64"