blob: 498bc148a0b60fa6e9059f0baad0aa563511fa7a [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"
Chandler Carruth7caea412013-11-09 12:26:54 +000024#include "llvm/IR/LegacyPassManagers.h"
Chris Lattner6fbe7042010-04-21 00:47:40 +000025#include "llvm/Support/CommandLine.h"
Chris Lattnereedcd842009-08-31 07:23:46 +000026#include "llvm/Support/Debug.h"
Chris Lattner707431c2010-03-30 04:03:22 +000027#include "llvm/Support/Timer.h"
Chris Lattner13626022009-08-23 06:03:38 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner8d083812004-04-20 21:30:06 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chandler Carruthf1221bd2014-04-22 02:48:03 +000031#define DEBUG_TYPE "cgscc-passmgr"
32
Chris Lattner6fbe7042010-04-21 00:47:40 +000033static cl::opt<unsigned>
Chris Lattnerfc8d9ee2010-05-01 01:15:56 +000034MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
Chris Lattner6fbe7042010-04-21 00:47:40 +000035
36STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
37
Devang Patel48537a02007-01-17 21:45:01 +000038//===----------------------------------------------------------------------===//
39// CGPassManager
40//
Dale Johannesen50f03762009-09-10 22:01:32 +000041/// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
Devang Patel48537a02007-01-17 21:45:01 +000042
Dan Gohmand78c4002008-05-13 00:00:25 +000043namespace {
44
Devang Patel48537a02007-01-17 21:45:01 +000045class CGPassManager : public ModulePass, public PMDataManager {
Devang Patel48537a02007-01-17 21:45:01 +000046public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000047 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +000048 explicit CGPassManager()
49 : ModulePass(ID), PMDataManager() { }
Devang Patel48537a02007-01-17 21:45:01 +000050
51 /// run - Execute all of the passes scheduled for execution. Keep track of
52 /// whether any of the passes modifies the module, and if so, return true.
Craig Toppere9ba7592014-03-05 07:30:04 +000053 bool runOnModule(Module &M) override;
Devang Patel48537a02007-01-17 21:45:01 +000054
Owen Anderson1aa27512012-11-15 00:14:15 +000055 using ModulePass::doInitialization;
56 using ModulePass::doFinalization;
57
Bill Wendling5f14a012009-02-11 18:19:24 +000058 bool doInitialization(CallGraph &CG);
59 bool doFinalization(CallGraph &CG);
Devang Patel48537a02007-01-17 21:45:01 +000060
61 /// Pass Manager itself does not invalidate any analysis info.
Craig Toppere9ba7592014-03-05 07:30:04 +000062 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel48537a02007-01-17 21:45:01 +000063 // CGPassManager walks SCC and it needs CallGraph.
Chandler Carruth6378cf52013-11-26 04:19:30 +000064 Info.addRequired<CallGraphWrapperPass>();
Devang Patel48537a02007-01-17 21:45:01 +000065 Info.setPreservesAll();
66 }
67
Craig Toppere9ba7592014-03-05 07:30:04 +000068 const char *getPassName() const override {
Devang Patelf7fea8a2007-02-01 22:09:37 +000069 return "CallGraph Pass Manager";
70 }
71
Craig Toppere9ba7592014-03-05 07:30:04 +000072 PMDataManager *getAsPMDataManager() override { return this; }
73 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +000074
Devang Patel48537a02007-01-17 21:45:01 +000075 // Print passes managed by this manager
Craig Toppere9ba7592014-03-05 07:30:04 +000076 void dumpPassStructure(unsigned Offset) override {
David Greene3d646312009-12-23 23:09:39 +000077 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
Devang Patel48537a02007-01-17 21:45:01 +000078 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
79 Pass *P = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +000080 P->dumpPassStructure(Offset + 1);
Devang Patel48537a02007-01-17 21:45:01 +000081 dumpLastUses(P, Offset+1);
82 }
83 }
84
85 Pass *getContainedPass(unsigned N) {
Chris Lattner13626022009-08-23 06:03:38 +000086 assert(N < PassVector.size() && "Pass number out of range!");
87 return static_cast<Pass *>(PassVector[N]);
Devang Patel48537a02007-01-17 21:45:01 +000088 }
89
Craig Toppere9ba7592014-03-05 07:30:04 +000090 PassManagerType getPassManagerType() const override {
Devang Patel48537a02007-01-17 21:45:01 +000091 return PMT_CallGraphPassManager;
92 }
Chris Lattner7e4fbdd2009-08-31 06:01:21 +000093
94private:
Chris Lattner6fbe7042010-04-21 00:47:40 +000095 bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
96 bool &DevirtualizedCall);
97
Chris Lattner4422d312010-04-16 22:42:17 +000098 bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
Chris Lattner6fbe7042010-04-21 00:47:40 +000099 CallGraph &CG, bool &CallGraphUpToDate,
100 bool &DevirtualizedCall);
101 bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG,
Chris Lattnere33167a2009-09-01 18:32:03 +0000102 bool IsCheckingMode);
Devang Patel48537a02007-01-17 21:45:01 +0000103};
104
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000105} // end anonymous namespace.
Dan Gohmand78c4002008-05-13 00:00:25 +0000106
Devang Patel8c78a0b2007-05-03 01:11:54 +0000107char CGPassManager::ID = 0;
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000108
David Greene9b063df2010-04-02 23:17:14 +0000109
Chris Lattner4422d312010-04-16 22:42:17 +0000110bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
Chris Lattner6fbe7042010-04-21 00:47:40 +0000111 CallGraph &CG, bool &CallGraphUpToDate,
112 bool &DevirtualizedCall) {
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000113 bool Changed = false;
Chris Lattner0b1c7232010-01-22 05:46:59 +0000114 PMDataManager *PM = P->getAsPMDataManager();
115
Craig Topper353eda42014-04-24 06:44:33 +0000116 if (!PM) {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000117 CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
Chris Lattnereedcd842009-08-31 07:23:46 +0000118 if (!CallGraphUpToDate) {
Chris Lattner6fbe7042010-04-21 00:47:40 +0000119 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
Chris Lattnereedcd842009-08-31 07:23:46 +0000120 CallGraphUpToDate = true;
121 }
Chris Lattner339c82d2009-09-01 20:33:43 +0000122
Chris Lattner707431c2010-03-30 04:03:22 +0000123 {
124 TimeRegion PassTimer(getPassTimer(CGSP));
125 Changed = CGSP->runOnSCC(CurSCC);
126 }
Chris Lattnere33167a2009-09-01 18:32:03 +0000127
128 // After the CGSCCPass is done, when assertions are enabled, use
129 // RefreshCallGraph to verify that the callgraph was correctly updated.
130#ifndef NDEBUG
131 if (Changed)
132 RefreshCallGraph(CurSCC, CG, true);
133#endif
134
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000135 return Changed;
136 }
137
Chris Lattner0b1c7232010-01-22 05:46:59 +0000138
139 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
140 "Invalid CGPassManager member");
141 FPPassManager *FPP = (FPPassManager*)P;
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000142
143 // Run pass P on all functions in the current SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000144 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
145 I != E; ++I) {
146 if (Function *F = (*I)->getFunction()) {
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000147 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
Chris Lattner707431c2010-03-30 04:03:22 +0000148 TimeRegion PassTimer(getPassTimer(FPP));
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000149 Changed |= FPP->runOnFunction(*F);
150 }
151 }
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000152
Chris Lattnera9262bb2009-08-31 17:08:30 +0000153 // The function pass(es) modified the IR, they may have clobbered the
Chris Lattnereedcd842009-08-31 07:23:46 +0000154 // callgraph.
155 if (Changed && CallGraphUpToDate) {
David Greenef8ed9912009-12-23 20:10:59 +0000156 DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
Chris Lattnereedcd842009-08-31 07:23:46 +0000157 << P->getPassName() << '\n');
158 CallGraphUpToDate = false;
159 }
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000160 return Changed;
161}
162
Chris Lattnere33167a2009-09-01 18:32:03 +0000163
164/// RefreshCallGraph - Scan the functions in the specified CFG and resync the
165/// callgraph with the call sites found in it. This is used after
166/// FunctionPasses have potentially munged the callgraph, and can be used after
167/// CallGraphSCC passes to verify that they correctly updated the callgraph.
168///
Chris Lattner6fbe7042010-04-21 00:47:40 +0000169/// This function returns true if it devirtualized an existing function call,
170/// meaning it turned an indirect call into a direct call. This happens when
171/// a function pass like GVN optimizes away stuff feeding the indirect call.
172/// This never happens in checking mode.
173///
174bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
Chris Lattnere33167a2009-09-01 18:32:03 +0000175 CallGraph &CG, bool CheckingMode) {
Chris Lattner063d0652009-09-01 06:31:31 +0000176 DenseMap<Value*, CallGraphNode*> CallSites;
Chris Lattnereedcd842009-08-31 07:23:46 +0000177
David Greenef8ed9912009-12-23 20:10:59 +0000178 DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
Chris Lattnereedcd842009-08-31 07:23:46 +0000179 << " nodes:\n";
Chris Lattner4422d312010-04-16 22:42:17 +0000180 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
181 I != E; ++I)
182 (*I)->dump();
Chris Lattnereedcd842009-08-31 07:23:46 +0000183 );
184
185 bool MadeChange = false;
Chris Lattner6fbe7042010-04-21 00:47:40 +0000186 bool DevirtualizedCall = false;
Chris Lattnereedcd842009-08-31 07:23:46 +0000187
188 // Scan all functions in the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000189 unsigned FunctionNo = 0;
190 for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
191 SCCIdx != E; ++SCCIdx, ++FunctionNo) {
192 CallGraphNode *CGN = *SCCIdx;
Chris Lattnereedcd842009-08-31 07:23:46 +0000193 Function *F = CGN->getFunction();
Craig Topper353eda42014-04-24 06:44:33 +0000194 if (!F || F->isDeclaration()) continue;
Chris Lattnereedcd842009-08-31 07:23:46 +0000195
196 // Walk the function body looking for call sites. Sync up the call sites in
197 // CGN with those actually in the function.
Chris Lattner532112b2010-05-01 06:38:43 +0000198
199 // Keep track of the number of direct and indirect calls that were
200 // invalidated and removed.
201 unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000202
Chris Lattnereedcd842009-08-31 07:23:46 +0000203 // Get the set of call sites currently in the function.
Duncan Sands5632d962009-09-02 03:48:41 +0000204 for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
Chris Lattner063d0652009-09-01 06:31:31 +0000205 // If this call site is null, then the function pass deleted the call
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000206 // entirely and the WeakVH nulled it out.
Craig Topper353eda42014-04-24 06:44:33 +0000207 if (!I->first ||
Chris Lattner063d0652009-09-01 06:31:31 +0000208 // If we've already seen this call site, then the FunctionPass RAUW'd
209 // one call with another, which resulted in two "uses" in the edge
210 // list of the same call.
211 CallSites.count(I->first) ||
212
213 // If the call edge is not from a call or invoke, then the function
214 // pass RAUW'd a call with another value. This can happen when
215 // constant folding happens of well known functions etc.
Gabor Greif5bf74d62010-07-28 12:35:54 +0000216 !CallSite(I->first)) {
Chris Lattnere33167a2009-09-01 18:32:03 +0000217 assert(!CheckingMode &&
218 "CallGraphSCCPass did not update the CallGraph correctly!");
219
Chris Lattner532112b2010-05-01 06:38:43 +0000220 // If this was an indirect call site, count it.
Craig Topper353eda42014-04-24 06:44:33 +0000221 if (!I->second->getFunction())
Chris Lattner532112b2010-05-01 06:38:43 +0000222 ++NumIndirectRemoved;
223 else
224 ++NumDirectRemoved;
225
Chris Lattner65fb5972009-09-02 04:39:04 +0000226 // Just remove the edge from the set of callees, keep track of whether
227 // I points to the last element of the vector.
228 bool WasLast = I + 1 == E;
Chris Lattner063d0652009-09-01 06:31:31 +0000229 CGN->removeCallEdge(I);
Chris Lattner8f232762009-09-02 04:34:06 +0000230
Chris Lattner65fb5972009-09-02 04:39:04 +0000231 // If I pointed to the last element of the vector, we have to bail out:
232 // iterator checking rejects comparisons of the resultant pointer with
233 // end.
234 if (WasLast)
235 break;
Chris Lattner063d0652009-09-01 06:31:31 +0000236 E = CGN->end();
Chris Lattner063d0652009-09-01 06:31:31 +0000237 continue;
238 }
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000239
Chris Lattner063d0652009-09-01 06:31:31 +0000240 assert(!CallSites.count(I->first) &&
Chris Lattnereedcd842009-08-31 07:23:46 +0000241 "Call site occurs in node multiple times");
Chris Lattner063d0652009-09-01 06:31:31 +0000242 CallSites.insert(std::make_pair(I->first, I->second));
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000243 ++I;
Chris Lattnereedcd842009-08-31 07:23:46 +0000244 }
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000245
Chris Lattnereedcd842009-08-31 07:23:46 +0000246 // Loop over all of the instructions in the function, getting the callsites.
Chris Lattner532112b2010-05-01 06:38:43 +0000247 // Keep track of the number of direct/indirect calls added.
248 unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
249
Chris Lattnereedcd842009-08-31 07:23:46 +0000250 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
251 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
John McCall729c35b2011-06-09 19:46:27 +0000252 CallSite CS(cast<Value>(I));
Nuno Lopes674acc12012-06-29 17:49:32 +0000253 if (!CS) continue;
254 Function *Callee = CS.getCalledFunction();
255 if (Callee && Callee->isIntrinsic()) continue;
Chris Lattnereedcd842009-08-31 07:23:46 +0000256
257 // If this call site already existed in the callgraph, just verify it
258 // matches up to expectations and remove it from CallSites.
Chris Lattner063d0652009-09-01 06:31:31 +0000259 DenseMap<Value*, CallGraphNode*>::iterator ExistingIt =
Chris Lattnereedcd842009-08-31 07:23:46 +0000260 CallSites.find(CS.getInstruction());
261 if (ExistingIt != CallSites.end()) {
262 CallGraphNode *ExistingNode = ExistingIt->second;
263
264 // Remove from CallSites since we have now seen it.
265 CallSites.erase(ExistingIt);
266
267 // Verify that the callee is right.
268 if (ExistingNode->getFunction() == CS.getCalledFunction())
269 continue;
270
Chris Lattnere33167a2009-09-01 18:32:03 +0000271 // If we are in checking mode, we are not allowed to actually mutate
272 // the callgraph. If this is a case where we can infer that the
273 // callgraph is less precise than it could be (e.g. an indirect call
274 // site could be turned direct), don't reject it in checking mode, and
275 // don't tweak it to be more precise.
276 if (CheckingMode && CS.getCalledFunction() &&
Craig Topper353eda42014-04-24 06:44:33 +0000277 ExistingNode->getFunction() == nullptr)
Chris Lattnere33167a2009-09-01 18:32:03 +0000278 continue;
279
280 assert(!CheckingMode &&
281 "CallGraphSCCPass did not update the CallGraph correctly!");
282
Chris Lattnereedcd842009-08-31 07:23:46 +0000283 // If not, we either went from a direct call to indirect, indirect to
284 // direct, or direct to different direct.
285 CallGraphNode *CalleeNode;
Chris Lattner6fbe7042010-04-21 00:47:40 +0000286 if (Function *Callee = CS.getCalledFunction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000287 CalleeNode = CG.getOrInsertFunction(Callee);
Chris Lattner6fbe7042010-04-21 00:47:40 +0000288 // Keep track of whether we turned an indirect call into a direct
289 // one.
Craig Topper353eda42014-04-24 06:44:33 +0000290 if (!ExistingNode->getFunction()) {
Chris Lattner6fbe7042010-04-21 00:47:40 +0000291 DevirtualizedCall = true;
292 DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
293 << Callee->getName() << "'\n");
294 }
295 } else {
Chris Lattnereedcd842009-08-31 07:23:46 +0000296 CalleeNode = CG.getCallsExternalNode();
Chris Lattner6fbe7042010-04-21 00:47:40 +0000297 }
Chris Lattner339c82d2009-09-01 20:33:43 +0000298
299 // Update the edge target in CGN.
Chris Lattner055cf262010-04-22 20:42:33 +0000300 CGN->replaceCallEdge(CS, CS, CalleeNode);
Chris Lattnereedcd842009-08-31 07:23:46 +0000301 MadeChange = true;
302 continue;
303 }
304
Chris Lattnere33167a2009-09-01 18:32:03 +0000305 assert(!CheckingMode &&
306 "CallGraphSCCPass did not update the CallGraph correctly!");
307
Chris Lattner532112b2010-05-01 06:38:43 +0000308 // If the call site didn't exist in the CGN yet, add it.
Chris Lattnereedcd842009-08-31 07:23:46 +0000309 CallGraphNode *CalleeNode;
Chris Lattner532112b2010-05-01 06:38:43 +0000310 if (Function *Callee = CS.getCalledFunction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000311 CalleeNode = CG.getOrInsertFunction(Callee);
Chris Lattner532112b2010-05-01 06:38:43 +0000312 ++NumDirectAdded;
313 } else {
Chris Lattnereedcd842009-08-31 07:23:46 +0000314 CalleeNode = CG.getCallsExternalNode();
Chris Lattner532112b2010-05-01 06:38:43 +0000315 ++NumIndirectAdded;
316 }
Chris Lattnereedcd842009-08-31 07:23:46 +0000317
318 CGN->addCalledFunction(CS, CalleeNode);
319 MadeChange = true;
320 }
321
Chris Lattner532112b2010-05-01 06:38:43 +0000322 // We scanned the old callgraph node, removing invalidated call sites and
323 // then added back newly found call sites. One thing that can happen is
324 // that an old indirect call site was deleted and replaced with a new direct
325 // call. In this case, we have devirtualized a call, and CGSCCPM would like
326 // to iteratively optimize the new code. Unfortunately, we don't really
327 // have a great way to detect when this happens. As an approximation, we
328 // just look at whether the number of indirect calls is reduced and the
329 // number of direct calls is increased. There are tons of ways to fool this
330 // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
331 // direct call) but this is close enough.
332 if (NumIndirectRemoved > NumIndirectAdded &&
333 NumDirectRemoved < NumDirectAdded)
334 DevirtualizedCall = true;
335
Chris Lattnereedcd842009-08-31 07:23:46 +0000336 // After scanning this function, if we still have entries in callsites, then
Chris Lattner063d0652009-09-01 06:31:31 +0000337 // they are dangling pointers. WeakVH should save us for this, so abort if
338 // this happens.
339 assert(CallSites.empty() && "Dangling pointers found in call sites map");
Chris Lattnereedcd842009-08-31 07:23:46 +0000340
Chris Lattner063d0652009-09-01 06:31:31 +0000341 // Periodically do an explicit clear to remove tombstones when processing
342 // large scc's.
Chris Lattner4422d312010-04-16 22:42:17 +0000343 if ((FunctionNo & 15) == 15)
Chris Lattner063d0652009-09-01 06:31:31 +0000344 CallSites.clear();
Chris Lattnereedcd842009-08-31 07:23:46 +0000345 }
346
347 DEBUG(if (MadeChange) {
David Greenef8ed9912009-12-23 20:10:59 +0000348 dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
Chris Lattner4422d312010-04-16 22:42:17 +0000349 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
350 I != E; ++I)
351 (*I)->dump();
Chris Lattner532112b2010-05-01 06:38:43 +0000352 if (DevirtualizedCall)
353 dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
354
Chris Lattnereedcd842009-08-31 07:23:46 +0000355 } else {
David Greenef8ed9912009-12-23 20:10:59 +0000356 dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
Chris Lattnereedcd842009-08-31 07:23:46 +0000357 }
358 );
Duncan Sandsa41634e2011-08-12 14:54:45 +0000359 (void)MadeChange;
Chris Lattner6fbe7042010-04-21 00:47:40 +0000360
361 return DevirtualizedCall;
362}
363
364/// RunAllPassesOnSCC - Execute the body of the entire pass manager on the
365/// specified SCC. This keeps track of whether a function pass devirtualizes
366/// any calls and returns it in DevirtualizedCall.
367bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
368 bool &DevirtualizedCall) {
369 bool Changed = false;
370
371 // CallGraphUpToDate - Keep track of whether the callgraph is known to be
372 // up-to-date or not. The CGSSC pass manager runs two types of passes:
373 // CallGraphSCC Passes and other random function passes. Because other
374 // random function passes are not CallGraph aware, they may clobber the
375 // call graph by introducing new calls or deleting other ones. This flag
376 // is set to false when we run a function pass so that we know to clean up
377 // the callgraph when we need to run a CGSCCPass again.
378 bool CallGraphUpToDate = true;
379
380 // Run all passes on current SCC.
381 for (unsigned PassNo = 0, e = getNumContainedPasses();
382 PassNo != e; ++PassNo) {
383 Pass *P = getContainedPass(PassNo);
384
385 // If we're in -debug-pass=Executions mode, construct the SCC node list,
386 // otherwise avoid constructing this string as it is expensive.
387 if (isPassDebuggingExecutionsOrMore()) {
388 std::string Functions;
389 #ifndef NDEBUG
390 raw_string_ostream OS(Functions);
391 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
392 I != E; ++I) {
393 if (I != CurSCC.begin()) OS << ", ";
394 (*I)->print(OS);
395 }
396 OS.flush();
397 #endif
398 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
399 }
400 dumpRequiredSet(P);
401
402 initializeAnalysisImpl(P);
403
404 // Actually run this pass on the current SCC.
405 Changed |= RunPassOnSCC(P, CurSCC, CG,
406 CallGraphUpToDate, DevirtualizedCall);
407
408 if (Changed)
409 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
410 dumpPreservedSet(P);
411
412 verifyPreservedAnalysis(P);
413 removeNotPreservedAnalysis(P);
414 recordAvailableAnalysis(P);
415 removeDeadPasses(P, "", ON_CG_MSG);
416 }
417
418 // If the callgraph was left out of date (because the last pass run was a
419 // functionpass), refresh it before we move on to the next SCC.
420 if (!CallGraphUpToDate)
421 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
422 return Changed;
Chris Lattnereedcd842009-08-31 07:23:46 +0000423}
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000424
Devang Patel48537a02007-01-17 21:45:01 +0000425/// run - Execute all of the passes scheduled for execution. Keep track of
426/// whether any of the passes modifies the module, and if so, return true.
427bool CGPassManager::runOnModule(Module &M) {
Chandler Carruth6378cf52013-11-26 04:19:30 +0000428 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Bill Wendling5f14a012009-02-11 18:19:24 +0000429 bool Changed = doInitialization(CG);
Chris Lattner6fbe7042010-04-21 00:47:40 +0000430
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000431 // Walk the callgraph in bottom-up SCC order.
Chris Lattner5518b812010-04-16 22:59:24 +0000432 scc_iterator<CallGraph*> CGI = scc_begin(&CG);
433
434 CallGraphSCC CurSCC(&CGI);
435 while (!CGI.isAtEnd()) {
Chris Lattner305b1152009-08-31 00:19:58 +0000436 // Copy the current SCC and increment past it so that the pass can hack
437 // on the SCC if it wants to without invalidating our iterator.
Chris Lattner4422d312010-04-16 22:42:17 +0000438 std::vector<CallGraphNode*> &NodeVec = *CGI;
439 CurSCC.initialize(&NodeVec[0], &NodeVec[0]+NodeVec.size());
Chris Lattner305b1152009-08-31 00:19:58 +0000440 ++CGI;
441
Chris Lattner6fbe7042010-04-21 00:47:40 +0000442 // At the top level, we run all the passes in this pass manager on the
443 // functions in this SCC. However, we support iterative compilation in the
444 // case where a function pass devirtualizes a call to a function. For
445 // example, it is very common for a function pass (often GVN or instcombine)
446 // to eliminate the addressing that feeds into a call. With that improved
447 // information, we would like the call to be an inline candidate, infer
448 // mod-ref information etc.
449 //
450 // Because of this, we allow iteration up to a specified iteration count.
451 // This only happens in the case of a devirtualized call, so we only burn
452 // compile time in the case that we're making progress. We also have a hard
453 // iteration count limit in case there is crazy code.
454 unsigned Iteration = 0;
455 bool DevirtualizedCall = false;
456 do {
Chris Lattner055cf262010-04-22 20:42:33 +0000457 DEBUG(if (Iteration)
458 dbgs() << " SCCPASSMGR: Re-visiting SCC, iteration #"
459 << Iteration << '\n');
Chris Lattner6fbe7042010-04-21 00:47:40 +0000460 DevirtualizedCall = false;
461 Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
462 } while (Iteration++ < MaxIterations && DevirtualizedCall);
Chris Lattnereedcd842009-08-31 07:23:46 +0000463
Chris Lattner6fbe7042010-04-21 00:47:40 +0000464 if (DevirtualizedCall)
465 DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " << Iteration
466 << " times, due to -max-cg-scc-iterations\n");
Chris Lattnereedcd842009-08-31 07:23:46 +0000467
Chris Lattner6fbe7042010-04-21 00:47:40 +0000468 if (Iteration > MaxSCCIterations)
469 MaxSCCIterations = Iteration;
470
Devang Patel48537a02007-01-17 21:45:01 +0000471 }
Bill Wendling5f14a012009-02-11 18:19:24 +0000472 Changed |= doFinalization(CG);
Devang Patel48537a02007-01-17 21:45:01 +0000473 return Changed;
474}
475
Chris Lattner6fbe7042010-04-21 00:47:40 +0000476
Devang Patel48537a02007-01-17 21:45:01 +0000477/// Initialize CG
Bill Wendling5f14a012009-02-11 18:19:24 +0000478bool CGPassManager::doInitialization(CallGraph &CG) {
Devang Patel48537a02007-01-17 21:45:01 +0000479 bool Changed = false;
Chris Lattner0b1c7232010-01-22 05:46:59 +0000480 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
481 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
482 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
483 "Invalid CGPassManager member");
484 Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000485 } else {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000486 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000487 }
Devang Patel48537a02007-01-17 21:45:01 +0000488 }
489 return Changed;
490}
491
492/// Finalize CG
Bill Wendling5f14a012009-02-11 18:19:24 +0000493bool CGPassManager::doFinalization(CallGraph &CG) {
Devang Patel48537a02007-01-17 21:45:01 +0000494 bool Changed = false;
Chris Lattner0b1c7232010-01-22 05:46:59 +0000495 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
496 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
497 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
498 "Invalid CGPassManager member");
499 Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000500 } else {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000501 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000502 }
Devang Patel48537a02007-01-17 21:45:01 +0000503 }
504 return Changed;
505}
506
Chris Lattner6d1208f2010-04-16 21:43:55 +0000507//===----------------------------------------------------------------------===//
Chris Lattner4422d312010-04-16 22:42:17 +0000508// CallGraphSCC Implementation
509//===----------------------------------------------------------------------===//
510
Chris Lattner5518b812010-04-16 22:59:24 +0000511/// ReplaceNode - This informs the SCC and the pass manager that the specified
512/// Old node has been deleted, and New is to be used in its place.
513void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
514 assert(Old != New && "Should not replace node with self");
515 for (unsigned i = 0; ; ++i) {
516 assert(i != Nodes.size() && "Node not in SCC");
517 if (Nodes[i] != Old) continue;
518 Nodes[i] = New;
519 break;
520 }
Chris Lattnerde023a32010-04-16 23:04:30 +0000521
522 // Update the active scc_iterator so that it doesn't contain dangling
523 // pointers to the old CallGraphNode.
524 scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
525 CGI->ReplaceNode(Old, New);
Chris Lattner5518b812010-04-16 22:59:24 +0000526}
Chris Lattner4422d312010-04-16 22:42:17 +0000527
528
529//===----------------------------------------------------------------------===//
Chris Lattner6d1208f2010-04-16 21:43:55 +0000530// CallGraphSCCPass Implementation
531//===----------------------------------------------------------------------===//
David Greene9b063df2010-04-02 23:17:14 +0000532
Devang Patel020f4f22007-01-23 21:55:17 +0000533/// Assign pass manager to manage this pass.
Devang Patel1f8200b2007-01-23 21:52:35 +0000534void CallGraphSCCPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +0000535 PassManagerType PreferredType) {
Devang Patel1f8200b2007-01-23 21:52:35 +0000536 // Find CGPassManager
Duncan Sands60f28bf2007-07-19 09:42:01 +0000537 while (!PMS.empty() &&
538 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
539 PMS.pop();
Devang Patel1f8200b2007-01-23 21:52:35 +0000540
Chris Lattner9efd4fc2010-01-22 05:37:10 +0000541 assert(!PMS.empty() && "Unable to handle Call Graph Pass");
542 CGPassManager *CGP;
543
544 if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
545 CGP = (CGPassManager*)PMS.top();
546 else {
547 // Create new Call Graph SCC Pass Manager if it does not exist.
548 assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
Devang Patel1f8200b2007-01-23 21:52:35 +0000549 PMDataManager *PMD = PMS.top();
550
551 // [1] Create new Call Graph Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +0000552 CGP = new CGPassManager();
Devang Patel1f8200b2007-01-23 21:52:35 +0000553
554 // [2] Set up new manager's top level manager
555 PMTopLevelManager *TPM = PMD->getTopLevelManager();
556 TPM->addIndirectPassManager(CGP);
557
558 // [3] Assign manager to manage this new manager. This may create
559 // and push new managers into PMS
Chris Lattner9efd4fc2010-01-22 05:37:10 +0000560 Pass *P = CGP;
Devang Patel703de8f2007-06-21 22:29:02 +0000561 TPM->schedulePass(P);
Devang Patel1f8200b2007-01-23 21:52:35 +0000562
563 // [4] Push new manager into PMS
564 PMS.push(CGP);
565 }
566
567 CGP->add(this);
568}
569
Chris Lattnerf7e95942003-08-31 01:54:59 +0000570/// getAnalysisUsage - For this class, we declare that we require and preserve
571/// the call graph. If the derived class implements this method, it should
572/// always explicitly call the implementation here.
573void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruth6378cf52013-11-26 04:19:30 +0000574 AU.addRequired<CallGraphWrapperPass>();
575 AU.addPreserved<CallGraphWrapperPass>();
Chris Lattnerf7e95942003-08-31 01:54:59 +0000576}
Chris Lattner6d1208f2010-04-16 21:43:55 +0000577
578
579//===----------------------------------------------------------------------===//
580// PrintCallGraphPass Implementation
581//===----------------------------------------------------------------------===//
582
583namespace {
584 /// PrintCallGraphPass - Print a Module corresponding to a call graph.
585 ///
586 class PrintCallGraphPass : public CallGraphSCCPass {
587 std::string Banner;
588 raw_ostream &Out; // raw_ostream to print on.
589
590 public:
591 static char ID;
Chris Lattner6d1208f2010-04-16 21:43:55 +0000592 PrintCallGraphPass(const std::string &B, raw_ostream &o)
Owen Andersona7aed182010-08-06 18:33:48 +0000593 : CallGraphSCCPass(ID), Banner(B), Out(o) {}
Craig Toppere9ba7592014-03-05 07:30:04 +0000594
595 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner6d1208f2010-04-16 21:43:55 +0000596 AU.setPreservesAll();
597 }
Craig Toppere9ba7592014-03-05 07:30:04 +0000598
599 bool runOnSCC(CallGraphSCC &SCC) override {
Chris Lattner6d1208f2010-04-16 21:43:55 +0000600 Out << Banner;
Chris Lattner4422d312010-04-16 22:42:17 +0000601 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
602 (*I)->getFunction()->print(Out);
Chris Lattner6d1208f2010-04-16 21:43:55 +0000603 return false;
604 }
605 };
606
607} // end anonymous namespace.
608
609char PrintCallGraphPass::ID = 0;
610
611Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
612 const std::string &Banner) const {
613 return new PrintCallGraphPass(Banner, O);
614}
615