blob: 9d607cc0e4fd28e45e162b47994c611ce36048d6 [file] [log] [blame]
Chris Lattnerf7e95942003-08-31 01:54:59 +00001//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf7e95942003-08-31 01:54:59 +00009//
10// This file implements the CallGraphSCCPass class, which is used for passes
11// which are implemented as bottom-up traversals on the call graph. Because
12// there may be cycles in the call graph, passes of this type operate on the
13// call-graph in SCC order: that is, they process function bottom-up, except for
14// recursive functions, which they process all at once.
15//
16//===----------------------------------------------------------------------===//
17
Chandler Carruth839a98e2013-01-07 15:26:48 +000018#include "llvm/Analysis/CallGraphSCCPass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/ADT/SCCIterator.h"
Chris Lattner6fbe7042010-04-21 00:47:40 +000020#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Analysis/CallGraph.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Function.h"
23#include "llvm/IR/IntrinsicInst.h"
Juergen Ributzka34390c72014-05-16 02:33:15 +000024#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000025#include "llvm/IR/LegacyPassManagers.h"
Chris Lattner6fbe7042010-04-21 00:47:40 +000026#include "llvm/Support/CommandLine.h"
Chris Lattnereedcd842009-08-31 07:23:46 +000027#include "llvm/Support/Debug.h"
Chris Lattner707431c2010-03-30 04:03:22 +000028#include "llvm/Support/Timer.h"
Chris Lattner13626022009-08-23 06:03:38 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattner8d083812004-04-20 21:30:06 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chandler Carruthf1221bd2014-04-22 02:48:03 +000032#define DEBUG_TYPE "cgscc-passmgr"
33
Chris Lattner6fbe7042010-04-21 00:47:40 +000034static cl::opt<unsigned>
Chris Lattnerfc8d9ee2010-05-01 01:15:56 +000035MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
Chris Lattner6fbe7042010-04-21 00:47:40 +000036
37STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
38
Devang Patel48537a02007-01-17 21:45:01 +000039//===----------------------------------------------------------------------===//
40// CGPassManager
41//
Dale Johannesen50f03762009-09-10 22:01:32 +000042/// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
Devang Patel48537a02007-01-17 21:45:01 +000043
Dan Gohmand78c4002008-05-13 00:00:25 +000044namespace {
45
Devang Patel48537a02007-01-17 21:45:01 +000046class CGPassManager : public ModulePass, public PMDataManager {
Devang Patel48537a02007-01-17 21:45:01 +000047public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000048 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +000049 explicit CGPassManager()
50 : ModulePass(ID), PMDataManager() { }
Devang Patel48537a02007-01-17 21:45:01 +000051
Sanjay Pateld45a3f12015-03-10 03:48:14 +000052 /// Execute all of the passes scheduled for execution. Keep track of
Devang Patel48537a02007-01-17 21:45:01 +000053 /// whether any of the passes modifies the module, and if so, return true.
Craig Toppere9ba7592014-03-05 07:30:04 +000054 bool runOnModule(Module &M) override;
Devang Patel48537a02007-01-17 21:45:01 +000055
Owen Anderson1aa27512012-11-15 00:14:15 +000056 using ModulePass::doInitialization;
57 using ModulePass::doFinalization;
58
Bill Wendling5f14a012009-02-11 18:19:24 +000059 bool doInitialization(CallGraph &CG);
60 bool doFinalization(CallGraph &CG);
Devang Patel48537a02007-01-17 21:45:01 +000061
62 /// Pass Manager itself does not invalidate any analysis info.
Craig Toppere9ba7592014-03-05 07:30:04 +000063 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel48537a02007-01-17 21:45:01 +000064 // CGPassManager walks SCC and it needs CallGraph.
Chandler Carruth6378cf52013-11-26 04:19:30 +000065 Info.addRequired<CallGraphWrapperPass>();
Devang Patel48537a02007-01-17 21:45:01 +000066 Info.setPreservesAll();
67 }
68
Craig Toppere9ba7592014-03-05 07:30:04 +000069 const char *getPassName() const override {
Devang Patelf7fea8a2007-02-01 22:09:37 +000070 return "CallGraph Pass Manager";
71 }
72
Craig Toppere9ba7592014-03-05 07:30:04 +000073 PMDataManager *getAsPMDataManager() override { return this; }
74 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +000075
Devang Patel48537a02007-01-17 21:45:01 +000076 // Print passes managed by this manager
Craig Toppere9ba7592014-03-05 07:30:04 +000077 void dumpPassStructure(unsigned Offset) override {
David Greene3d646312009-12-23 23:09:39 +000078 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
Devang Patel48537a02007-01-17 21:45:01 +000079 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
80 Pass *P = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +000081 P->dumpPassStructure(Offset + 1);
Devang Patel48537a02007-01-17 21:45:01 +000082 dumpLastUses(P, Offset+1);
83 }
84 }
85
86 Pass *getContainedPass(unsigned N) {
Chris Lattner13626022009-08-23 06:03:38 +000087 assert(N < PassVector.size() && "Pass number out of range!");
88 return static_cast<Pass *>(PassVector[N]);
Devang Patel48537a02007-01-17 21:45:01 +000089 }
90
Craig Toppere9ba7592014-03-05 07:30:04 +000091 PassManagerType getPassManagerType() const override {
Devang Patel48537a02007-01-17 21:45:01 +000092 return PMT_CallGraphPassManager;
93 }
Chris Lattner7e4fbdd2009-08-31 06:01:21 +000094
95private:
Chris Lattner6fbe7042010-04-21 00:47:40 +000096 bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
97 bool &DevirtualizedCall);
98
Chris Lattner4422d312010-04-16 22:42:17 +000099 bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
Chris Lattner6fbe7042010-04-21 00:47:40 +0000100 CallGraph &CG, bool &CallGraphUpToDate,
101 bool &DevirtualizedCall);
102 bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG,
Chris Lattnere33167a2009-09-01 18:32:03 +0000103 bool IsCheckingMode);
Devang Patel48537a02007-01-17 21:45:01 +0000104};
105
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000106} // end anonymous namespace.
Dan Gohmand78c4002008-05-13 00:00:25 +0000107
Devang Patel8c78a0b2007-05-03 01:11:54 +0000108char CGPassManager::ID = 0;
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000109
David Greene9b063df2010-04-02 23:17:14 +0000110
Chris Lattner4422d312010-04-16 22:42:17 +0000111bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
Chris Lattner6fbe7042010-04-21 00:47:40 +0000112 CallGraph &CG, bool &CallGraphUpToDate,
113 bool &DevirtualizedCall) {
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000114 bool Changed = false;
Chris Lattner0b1c7232010-01-22 05:46:59 +0000115 PMDataManager *PM = P->getAsPMDataManager();
116
Craig Topper353eda42014-04-24 06:44:33 +0000117 if (!PM) {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000118 CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
Chris Lattnereedcd842009-08-31 07:23:46 +0000119 if (!CallGraphUpToDate) {
Chris Lattner6fbe7042010-04-21 00:47:40 +0000120 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
Chris Lattnereedcd842009-08-31 07:23:46 +0000121 CallGraphUpToDate = true;
122 }
Chris Lattner339c82d2009-09-01 20:33:43 +0000123
Chris Lattner707431c2010-03-30 04:03:22 +0000124 {
125 TimeRegion PassTimer(getPassTimer(CGSP));
126 Changed = CGSP->runOnSCC(CurSCC);
127 }
Chris Lattnere33167a2009-09-01 18:32:03 +0000128
129 // After the CGSCCPass is done, when assertions are enabled, use
130 // RefreshCallGraph to verify that the callgraph was correctly updated.
131#ifndef NDEBUG
132 if (Changed)
133 RefreshCallGraph(CurSCC, CG, true);
134#endif
135
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000136 return Changed;
137 }
138
Chris Lattner0b1c7232010-01-22 05:46:59 +0000139
140 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
141 "Invalid CGPassManager member");
142 FPPassManager *FPP = (FPPassManager*)P;
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000143
144 // Run pass P on all functions in the current SCC.
Sanjay Patelc6012542015-03-10 03:26:39 +0000145 for (CallGraphNode *CGN : CurSCC) {
146 if (Function *F = CGN->getFunction()) {
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000147 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
Juergen Ributzka34390c72014-05-16 02:33:15 +0000148 {
149 TimeRegion PassTimer(getPassTimer(FPP));
150 Changed |= FPP->runOnFunction(*F);
151 }
152 F->getContext().yield();
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000153 }
154 }
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000155
Chris Lattnera9262bb2009-08-31 17:08:30 +0000156 // The function pass(es) modified the IR, they may have clobbered the
Chris Lattnereedcd842009-08-31 07:23:46 +0000157 // callgraph.
158 if (Changed && CallGraphUpToDate) {
David Greenef8ed9912009-12-23 20:10:59 +0000159 DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
Chris Lattnereedcd842009-08-31 07:23:46 +0000160 << P->getPassName() << '\n');
161 CallGraphUpToDate = false;
162 }
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000163 return Changed;
164}
165
Chris Lattnere33167a2009-09-01 18:32:03 +0000166
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000167/// Scan the functions in the specified CFG and resync the
Chris Lattnere33167a2009-09-01 18:32:03 +0000168/// callgraph with the call sites found in it. This is used after
169/// FunctionPasses have potentially munged the callgraph, and can be used after
170/// CallGraphSCC passes to verify that they correctly updated the callgraph.
171///
Chris Lattner6fbe7042010-04-21 00:47:40 +0000172/// This function returns true if it devirtualized an existing function call,
173/// meaning it turned an indirect call into a direct call. This happens when
174/// a function pass like GVN optimizes away stuff feeding the indirect call.
175/// This never happens in checking mode.
176///
177bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
Chris Lattnere33167a2009-09-01 18:32:03 +0000178 CallGraph &CG, bool CheckingMode) {
Chris Lattner063d0652009-09-01 06:31:31 +0000179 DenseMap<Value*, CallGraphNode*> CallSites;
Chris Lattnereedcd842009-08-31 07:23:46 +0000180
David Greenef8ed9912009-12-23 20:10:59 +0000181 DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
Chris Lattnereedcd842009-08-31 07:23:46 +0000182 << " nodes:\n";
Sanjay Patelc6012542015-03-10 03:26:39 +0000183 for (CallGraphNode *CGN : CurSCC)
184 CGN->dump();
Chris Lattnereedcd842009-08-31 07:23:46 +0000185 );
186
187 bool MadeChange = false;
Chris Lattner6fbe7042010-04-21 00:47:40 +0000188 bool DevirtualizedCall = false;
Chris Lattnereedcd842009-08-31 07:23:46 +0000189
190 // Scan all functions in the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000191 unsigned FunctionNo = 0;
192 for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
193 SCCIdx != E; ++SCCIdx, ++FunctionNo) {
194 CallGraphNode *CGN = *SCCIdx;
Chris Lattnereedcd842009-08-31 07:23:46 +0000195 Function *F = CGN->getFunction();
Craig Topper353eda42014-04-24 06:44:33 +0000196 if (!F || F->isDeclaration()) continue;
Chris Lattnereedcd842009-08-31 07:23:46 +0000197
198 // Walk the function body looking for call sites. Sync up the call sites in
199 // CGN with those actually in the function.
Chris Lattner532112b2010-05-01 06:38:43 +0000200
201 // Keep track of the number of direct and indirect calls that were
202 // invalidated and removed.
203 unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000204
Chris Lattnereedcd842009-08-31 07:23:46 +0000205 // Get the set of call sites currently in the function.
Duncan Sands5632d962009-09-02 03:48:41 +0000206 for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
Chris Lattner063d0652009-09-01 06:31:31 +0000207 // If this call site is null, then the function pass deleted the call
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000208 // entirely and the WeakVH nulled it out.
Craig Topper353eda42014-04-24 06:44:33 +0000209 if (!I->first ||
Chris Lattner063d0652009-09-01 06:31:31 +0000210 // If we've already seen this call site, then the FunctionPass RAUW'd
211 // one call with another, which resulted in two "uses" in the edge
212 // list of the same call.
213 CallSites.count(I->first) ||
214
215 // If the call edge is not from a call or invoke, then the function
216 // pass RAUW'd a call with another value. This can happen when
217 // constant folding happens of well known functions etc.
Gabor Greif5bf74d62010-07-28 12:35:54 +0000218 !CallSite(I->first)) {
Chris Lattnere33167a2009-09-01 18:32:03 +0000219 assert(!CheckingMode &&
220 "CallGraphSCCPass did not update the CallGraph correctly!");
221
Chris Lattner532112b2010-05-01 06:38:43 +0000222 // If this was an indirect call site, count it.
Craig Topper353eda42014-04-24 06:44:33 +0000223 if (!I->second->getFunction())
Chris Lattner532112b2010-05-01 06:38:43 +0000224 ++NumIndirectRemoved;
225 else
226 ++NumDirectRemoved;
227
Chris Lattner65fb5972009-09-02 04:39:04 +0000228 // Just remove the edge from the set of callees, keep track of whether
229 // I points to the last element of the vector.
230 bool WasLast = I + 1 == E;
Chris Lattner063d0652009-09-01 06:31:31 +0000231 CGN->removeCallEdge(I);
Chris Lattner8f232762009-09-02 04:34:06 +0000232
Chris Lattner65fb5972009-09-02 04:39:04 +0000233 // If I pointed to the last element of the vector, we have to bail out:
234 // iterator checking rejects comparisons of the resultant pointer with
235 // end.
236 if (WasLast)
237 break;
Chris Lattner063d0652009-09-01 06:31:31 +0000238 E = CGN->end();
Chris Lattner063d0652009-09-01 06:31:31 +0000239 continue;
240 }
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000241
Chris Lattner063d0652009-09-01 06:31:31 +0000242 assert(!CallSites.count(I->first) &&
Chris Lattnereedcd842009-08-31 07:23:46 +0000243 "Call site occurs in node multiple times");
Sanjay Patel4c219fd2014-11-12 18:25:47 +0000244
245 CallSite CS(I->first);
246 if (CS) {
247 Function *Callee = CS.getCalledFunction();
248 // Ignore intrinsics because they're not really function calls.
249 if (!Callee || !(Callee->isIntrinsic()))
250 CallSites.insert(std::make_pair(I->first, I->second));
251 }
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000252 ++I;
Chris Lattnereedcd842009-08-31 07:23:46 +0000253 }
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000254
Chris Lattnereedcd842009-08-31 07:23:46 +0000255 // Loop over all of the instructions in the function, getting the callsites.
Chris Lattner532112b2010-05-01 06:38:43 +0000256 // Keep track of the number of direct/indirect calls added.
257 unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
258
Chris Lattnereedcd842009-08-31 07:23:46 +0000259 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
260 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
John McCall729c35b2011-06-09 19:46:27 +0000261 CallSite CS(cast<Value>(I));
Nuno Lopes674acc12012-06-29 17:49:32 +0000262 if (!CS) continue;
263 Function *Callee = CS.getCalledFunction();
264 if (Callee && Callee->isIntrinsic()) continue;
Chris Lattnereedcd842009-08-31 07:23:46 +0000265
266 // If this call site already existed in the callgraph, just verify it
267 // matches up to expectations and remove it from CallSites.
Chris Lattner063d0652009-09-01 06:31:31 +0000268 DenseMap<Value*, CallGraphNode*>::iterator ExistingIt =
Chris Lattnereedcd842009-08-31 07:23:46 +0000269 CallSites.find(CS.getInstruction());
270 if (ExistingIt != CallSites.end()) {
271 CallGraphNode *ExistingNode = ExistingIt->second;
272
273 // Remove from CallSites since we have now seen it.
274 CallSites.erase(ExistingIt);
275
276 // Verify that the callee is right.
277 if (ExistingNode->getFunction() == CS.getCalledFunction())
278 continue;
279
Chris Lattnere33167a2009-09-01 18:32:03 +0000280 // If we are in checking mode, we are not allowed to actually mutate
281 // the callgraph. If this is a case where we can infer that the
282 // callgraph is less precise than it could be (e.g. an indirect call
283 // site could be turned direct), don't reject it in checking mode, and
284 // don't tweak it to be more precise.
285 if (CheckingMode && CS.getCalledFunction() &&
Craig Topper353eda42014-04-24 06:44:33 +0000286 ExistingNode->getFunction() == nullptr)
Chris Lattnere33167a2009-09-01 18:32:03 +0000287 continue;
288
289 assert(!CheckingMode &&
290 "CallGraphSCCPass did not update the CallGraph correctly!");
291
Chris Lattnereedcd842009-08-31 07:23:46 +0000292 // If not, we either went from a direct call to indirect, indirect to
293 // direct, or direct to different direct.
294 CallGraphNode *CalleeNode;
Chris Lattner6fbe7042010-04-21 00:47:40 +0000295 if (Function *Callee = CS.getCalledFunction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000296 CalleeNode = CG.getOrInsertFunction(Callee);
Chris Lattner6fbe7042010-04-21 00:47:40 +0000297 // Keep track of whether we turned an indirect call into a direct
298 // one.
Craig Topper353eda42014-04-24 06:44:33 +0000299 if (!ExistingNode->getFunction()) {
Chris Lattner6fbe7042010-04-21 00:47:40 +0000300 DevirtualizedCall = true;
301 DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
302 << Callee->getName() << "'\n");
303 }
304 } else {
Chris Lattnereedcd842009-08-31 07:23:46 +0000305 CalleeNode = CG.getCallsExternalNode();
Chris Lattner6fbe7042010-04-21 00:47:40 +0000306 }
Chris Lattner339c82d2009-09-01 20:33:43 +0000307
308 // Update the edge target in CGN.
Chris Lattner055cf262010-04-22 20:42:33 +0000309 CGN->replaceCallEdge(CS, CS, CalleeNode);
Chris Lattnereedcd842009-08-31 07:23:46 +0000310 MadeChange = true;
311 continue;
312 }
313
Chris Lattnere33167a2009-09-01 18:32:03 +0000314 assert(!CheckingMode &&
315 "CallGraphSCCPass did not update the CallGraph correctly!");
316
Chris Lattner532112b2010-05-01 06:38:43 +0000317 // If the call site didn't exist in the CGN yet, add it.
Chris Lattnereedcd842009-08-31 07:23:46 +0000318 CallGraphNode *CalleeNode;
Chris Lattner532112b2010-05-01 06:38:43 +0000319 if (Function *Callee = CS.getCalledFunction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000320 CalleeNode = CG.getOrInsertFunction(Callee);
Chris Lattner532112b2010-05-01 06:38:43 +0000321 ++NumDirectAdded;
322 } else {
Chris Lattnereedcd842009-08-31 07:23:46 +0000323 CalleeNode = CG.getCallsExternalNode();
Chris Lattner532112b2010-05-01 06:38:43 +0000324 ++NumIndirectAdded;
325 }
Chris Lattnereedcd842009-08-31 07:23:46 +0000326
327 CGN->addCalledFunction(CS, CalleeNode);
328 MadeChange = true;
329 }
330
Chris Lattner532112b2010-05-01 06:38:43 +0000331 // We scanned the old callgraph node, removing invalidated call sites and
332 // then added back newly found call sites. One thing that can happen is
333 // that an old indirect call site was deleted and replaced with a new direct
334 // call. In this case, we have devirtualized a call, and CGSCCPM would like
335 // to iteratively optimize the new code. Unfortunately, we don't really
336 // have a great way to detect when this happens. As an approximation, we
337 // just look at whether the number of indirect calls is reduced and the
338 // number of direct calls is increased. There are tons of ways to fool this
339 // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
340 // direct call) but this is close enough.
341 if (NumIndirectRemoved > NumIndirectAdded &&
342 NumDirectRemoved < NumDirectAdded)
343 DevirtualizedCall = true;
344
Chris Lattnereedcd842009-08-31 07:23:46 +0000345 // After scanning this function, if we still have entries in callsites, then
Chris Lattner063d0652009-09-01 06:31:31 +0000346 // they are dangling pointers. WeakVH should save us for this, so abort if
347 // this happens.
348 assert(CallSites.empty() && "Dangling pointers found in call sites map");
Chris Lattnereedcd842009-08-31 07:23:46 +0000349
Chris Lattner063d0652009-09-01 06:31:31 +0000350 // Periodically do an explicit clear to remove tombstones when processing
351 // large scc's.
Chris Lattner4422d312010-04-16 22:42:17 +0000352 if ((FunctionNo & 15) == 15)
Chris Lattner063d0652009-09-01 06:31:31 +0000353 CallSites.clear();
Chris Lattnereedcd842009-08-31 07:23:46 +0000354 }
355
356 DEBUG(if (MadeChange) {
David Greenef8ed9912009-12-23 20:10:59 +0000357 dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
Sanjay Patelc6012542015-03-10 03:26:39 +0000358 for (CallGraphNode *CGN : CurSCC)
359 CGN->dump();
Chris Lattner532112b2010-05-01 06:38:43 +0000360 if (DevirtualizedCall)
361 dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
362
Chris Lattnereedcd842009-08-31 07:23:46 +0000363 } else {
David Greenef8ed9912009-12-23 20:10:59 +0000364 dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
Chris Lattnereedcd842009-08-31 07:23:46 +0000365 }
366 );
Duncan Sandsa41634e2011-08-12 14:54:45 +0000367 (void)MadeChange;
Chris Lattner6fbe7042010-04-21 00:47:40 +0000368
369 return DevirtualizedCall;
370}
371
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000372/// Execute the body of the entire pass manager on the specified SCC.
373/// This keeps track of whether a function pass devirtualizes
Chris Lattner6fbe7042010-04-21 00:47:40 +0000374/// any calls and returns it in DevirtualizedCall.
375bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
376 bool &DevirtualizedCall) {
377 bool Changed = false;
378
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000379 // Keep track of whether the callgraph is known to be up-to-date or not.
380 // The CGSSC pass manager runs two types of passes:
Chris Lattner6fbe7042010-04-21 00:47:40 +0000381 // CallGraphSCC Passes and other random function passes. Because other
382 // random function passes are not CallGraph aware, they may clobber the
383 // call graph by introducing new calls or deleting other ones. This flag
384 // is set to false when we run a function pass so that we know to clean up
385 // the callgraph when we need to run a CGSCCPass again.
386 bool CallGraphUpToDate = true;
387
388 // Run all passes on current SCC.
389 for (unsigned PassNo = 0, e = getNumContainedPasses();
390 PassNo != e; ++PassNo) {
391 Pass *P = getContainedPass(PassNo);
392
393 // If we're in -debug-pass=Executions mode, construct the SCC node list,
394 // otherwise avoid constructing this string as it is expensive.
395 if (isPassDebuggingExecutionsOrMore()) {
396 std::string Functions;
397 #ifndef NDEBUG
398 raw_string_ostream OS(Functions);
399 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
400 I != E; ++I) {
401 if (I != CurSCC.begin()) OS << ", ";
402 (*I)->print(OS);
403 }
404 OS.flush();
405 #endif
406 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
407 }
408 dumpRequiredSet(P);
409
410 initializeAnalysisImpl(P);
411
412 // Actually run this pass on the current SCC.
413 Changed |= RunPassOnSCC(P, CurSCC, CG,
414 CallGraphUpToDate, DevirtualizedCall);
415
416 if (Changed)
417 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
418 dumpPreservedSet(P);
419
420 verifyPreservedAnalysis(P);
421 removeNotPreservedAnalysis(P);
422 recordAvailableAnalysis(P);
423 removeDeadPasses(P, "", ON_CG_MSG);
424 }
425
426 // If the callgraph was left out of date (because the last pass run was a
427 // functionpass), refresh it before we move on to the next SCC.
428 if (!CallGraphUpToDate)
429 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
430 return Changed;
Chris Lattnereedcd842009-08-31 07:23:46 +0000431}
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000432
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000433/// Execute all of the passes scheduled for execution. Keep track of
Devang Patel48537a02007-01-17 21:45:01 +0000434/// whether any of the passes modifies the module, and if so, return true.
435bool CGPassManager::runOnModule(Module &M) {
Chandler Carruth6378cf52013-11-26 04:19:30 +0000436 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Bill Wendling5f14a012009-02-11 18:19:24 +0000437 bool Changed = doInitialization(CG);
Chris Lattner6fbe7042010-04-21 00:47:40 +0000438
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000439 // Walk the callgraph in bottom-up SCC order.
Chris Lattner5518b812010-04-16 22:59:24 +0000440 scc_iterator<CallGraph*> CGI = scc_begin(&CG);
441
442 CallGraphSCC CurSCC(&CGI);
443 while (!CGI.isAtEnd()) {
Chris Lattner305b1152009-08-31 00:19:58 +0000444 // Copy the current SCC and increment past it so that the pass can hack
445 // on the SCC if it wants to without invalidating our iterator.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000446 const std::vector<CallGraphNode *> &NodeVec = *CGI;
447 CurSCC.initialize(NodeVec.data(), NodeVec.data() + NodeVec.size());
Chris Lattner305b1152009-08-31 00:19:58 +0000448 ++CGI;
449
Chris Lattner6fbe7042010-04-21 00:47:40 +0000450 // At the top level, we run all the passes in this pass manager on the
451 // functions in this SCC. However, we support iterative compilation in the
452 // case where a function pass devirtualizes a call to a function. For
453 // example, it is very common for a function pass (often GVN or instcombine)
454 // to eliminate the addressing that feeds into a call. With that improved
455 // information, we would like the call to be an inline candidate, infer
456 // mod-ref information etc.
457 //
458 // Because of this, we allow iteration up to a specified iteration count.
459 // This only happens in the case of a devirtualized call, so we only burn
460 // compile time in the case that we're making progress. We also have a hard
461 // iteration count limit in case there is crazy code.
462 unsigned Iteration = 0;
463 bool DevirtualizedCall = false;
464 do {
Chris Lattner055cf262010-04-22 20:42:33 +0000465 DEBUG(if (Iteration)
466 dbgs() << " SCCPASSMGR: Re-visiting SCC, iteration #"
467 << Iteration << '\n');
Chris Lattner6fbe7042010-04-21 00:47:40 +0000468 DevirtualizedCall = false;
469 Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
470 } while (Iteration++ < MaxIterations && DevirtualizedCall);
Chris Lattnereedcd842009-08-31 07:23:46 +0000471
Chris Lattner6fbe7042010-04-21 00:47:40 +0000472 if (DevirtualizedCall)
473 DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " << Iteration
474 << " times, due to -max-cg-scc-iterations\n");
Chris Lattnereedcd842009-08-31 07:23:46 +0000475
Chris Lattner6fbe7042010-04-21 00:47:40 +0000476 if (Iteration > MaxSCCIterations)
477 MaxSCCIterations = Iteration;
478
Devang Patel48537a02007-01-17 21:45:01 +0000479 }
Bill Wendling5f14a012009-02-11 18:19:24 +0000480 Changed |= doFinalization(CG);
Devang Patel48537a02007-01-17 21:45:01 +0000481 return Changed;
482}
483
Chris Lattner6fbe7042010-04-21 00:47:40 +0000484
Devang Patel48537a02007-01-17 21:45:01 +0000485/// Initialize CG
Bill Wendling5f14a012009-02-11 18:19:24 +0000486bool CGPassManager::doInitialization(CallGraph &CG) {
Devang Patel48537a02007-01-17 21:45:01 +0000487 bool Changed = false;
Chris Lattner0b1c7232010-01-22 05:46:59 +0000488 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
489 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
490 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
491 "Invalid CGPassManager member");
492 Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000493 } else {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000494 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000495 }
Devang Patel48537a02007-01-17 21:45:01 +0000496 }
497 return Changed;
498}
499
500/// Finalize CG
Bill Wendling5f14a012009-02-11 18:19:24 +0000501bool CGPassManager::doFinalization(CallGraph &CG) {
Devang Patel48537a02007-01-17 21:45:01 +0000502 bool Changed = false;
Chris Lattner0b1c7232010-01-22 05:46:59 +0000503 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
504 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
505 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
506 "Invalid CGPassManager member");
507 Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000508 } else {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000509 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000510 }
Devang Patel48537a02007-01-17 21:45:01 +0000511 }
512 return Changed;
513}
514
Chris Lattner6d1208f2010-04-16 21:43:55 +0000515//===----------------------------------------------------------------------===//
Chris Lattner4422d312010-04-16 22:42:17 +0000516// CallGraphSCC Implementation
517//===----------------------------------------------------------------------===//
518
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000519/// This informs the SCC and the pass manager that the specified
Chris Lattner5518b812010-04-16 22:59:24 +0000520/// Old node has been deleted, and New is to be used in its place.
521void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
522 assert(Old != New && "Should not replace node with self");
523 for (unsigned i = 0; ; ++i) {
524 assert(i != Nodes.size() && "Node not in SCC");
525 if (Nodes[i] != Old) continue;
526 Nodes[i] = New;
527 break;
528 }
Chris Lattnerde023a32010-04-16 23:04:30 +0000529
530 // Update the active scc_iterator so that it doesn't contain dangling
531 // pointers to the old CallGraphNode.
532 scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
533 CGI->ReplaceNode(Old, New);
Chris Lattner5518b812010-04-16 22:59:24 +0000534}
Chris Lattner4422d312010-04-16 22:42:17 +0000535
536
537//===----------------------------------------------------------------------===//
Chris Lattner6d1208f2010-04-16 21:43:55 +0000538// CallGraphSCCPass Implementation
539//===----------------------------------------------------------------------===//
David Greene9b063df2010-04-02 23:17:14 +0000540
Devang Patel020f4f22007-01-23 21:55:17 +0000541/// Assign pass manager to manage this pass.
Devang Patel1f8200b2007-01-23 21:52:35 +0000542void CallGraphSCCPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +0000543 PassManagerType PreferredType) {
Devang Patel1f8200b2007-01-23 21:52:35 +0000544 // Find CGPassManager
Duncan Sands60f28bf2007-07-19 09:42:01 +0000545 while (!PMS.empty() &&
546 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
547 PMS.pop();
Devang Patel1f8200b2007-01-23 21:52:35 +0000548
Chris Lattner9efd4fc2010-01-22 05:37:10 +0000549 assert(!PMS.empty() && "Unable to handle Call Graph Pass");
550 CGPassManager *CGP;
551
552 if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
553 CGP = (CGPassManager*)PMS.top();
554 else {
555 // Create new Call Graph SCC Pass Manager if it does not exist.
556 assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
Devang Patel1f8200b2007-01-23 21:52:35 +0000557 PMDataManager *PMD = PMS.top();
558
559 // [1] Create new Call Graph Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +0000560 CGP = new CGPassManager();
Devang Patel1f8200b2007-01-23 21:52:35 +0000561
562 // [2] Set up new manager's top level manager
563 PMTopLevelManager *TPM = PMD->getTopLevelManager();
564 TPM->addIndirectPassManager(CGP);
565
566 // [3] Assign manager to manage this new manager. This may create
567 // and push new managers into PMS
Chris Lattner9efd4fc2010-01-22 05:37:10 +0000568 Pass *P = CGP;
Devang Patel703de8f2007-06-21 22:29:02 +0000569 TPM->schedulePass(P);
Devang Patel1f8200b2007-01-23 21:52:35 +0000570
571 // [4] Push new manager into PMS
572 PMS.push(CGP);
573 }
574
575 CGP->add(this);
576}
577
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000578/// For this class, we declare that we require and preserve the call graph.
579/// If the derived class implements this method, it should
Chris Lattnerf7e95942003-08-31 01:54:59 +0000580/// always explicitly call the implementation here.
581void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruth6378cf52013-11-26 04:19:30 +0000582 AU.addRequired<CallGraphWrapperPass>();
583 AU.addPreserved<CallGraphWrapperPass>();
Chris Lattnerf7e95942003-08-31 01:54:59 +0000584}
Chris Lattner6d1208f2010-04-16 21:43:55 +0000585
586
587//===----------------------------------------------------------------------===//
588// PrintCallGraphPass Implementation
589//===----------------------------------------------------------------------===//
590
591namespace {
592 /// PrintCallGraphPass - Print a Module corresponding to a call graph.
593 ///
594 class PrintCallGraphPass : public CallGraphSCCPass {
595 std::string Banner;
596 raw_ostream &Out; // raw_ostream to print on.
597
598 public:
599 static char ID;
Chris Lattner6d1208f2010-04-16 21:43:55 +0000600 PrintCallGraphPass(const std::string &B, raw_ostream &o)
Owen Andersona7aed182010-08-06 18:33:48 +0000601 : CallGraphSCCPass(ID), Banner(B), Out(o) {}
Craig Toppere9ba7592014-03-05 07:30:04 +0000602
603 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner6d1208f2010-04-16 21:43:55 +0000604 AU.setPreservesAll();
605 }
Craig Toppere9ba7592014-03-05 07:30:04 +0000606
607 bool runOnSCC(CallGraphSCC &SCC) override {
Chris Lattner6d1208f2010-04-16 21:43:55 +0000608 Out << Banner;
Sanjay Patelc6012542015-03-10 03:26:39 +0000609 for (CallGraphNode *CGN : SCC) {
610 if (CGN->getFunction())
611 CGN->getFunction()->print(Out);
Richard Trieuc1485222014-06-21 02:43:02 +0000612 else
Richard Trieuf2a79522014-07-03 02:11:49 +0000613 Out << "\nPrinting <null> Function\n";
Richard Trieua23043c2014-06-09 22:53:16 +0000614 }
Chris Lattner6d1208f2010-04-16 21:43:55 +0000615 return false;
616 }
617 };
618
619} // end anonymous namespace.
620
621char PrintCallGraphPass::ID = 0;
622
623Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
624 const std::string &Banner) const {
625 return new PrintCallGraphPass(Banner, O);
626}
627