1 //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "cgscc-passmgr"
19 #include "llvm/CallGraphSCCPass.h"
20 #include "llvm/ADT/SCCIterator.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/CallGraph.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/PassManagers.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/Timer.h"
29 #include "llvm/Support/raw_ostream.h"
32 static cl::opt<unsigned>
33 MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
35 STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
37 //===----------------------------------------------------------------------===//
40 /// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
44 class CGPassManager : public ModulePass, public PMDataManager {
47 explicit CGPassManager()
48 : ModulePass(ID), PMDataManager() { }
50 /// run - Execute all of the passes scheduled for execution. Keep track of
51 /// whether any of the passes modifies the module, and if so, return true.
52 bool runOnModule(Module &M);
54 using ModulePass::doInitialization;
55 using ModulePass::doFinalization;
57 bool doInitialization(CallGraph &CG);
58 bool doFinalization(CallGraph &CG);
60 /// Pass Manager itself does not invalidate any analysis info.
61 void getAnalysisUsage(AnalysisUsage &Info) const {
62 // CGPassManager walks SCC and it needs CallGraph.
63 Info.addRequired<CallGraph>();
64 Info.setPreservesAll();
67 virtual const char *getPassName() const {
68 return "CallGraph Pass Manager";
71 virtual PMDataManager *getAsPMDataManager() { return this; }
72 virtual Pass *getAsPass() { return this; }
74 // Print passes managed by this manager
75 void dumpPassStructure(unsigned Offset) {
76 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
77 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
78 Pass *P = getContainedPass(Index);
79 P->dumpPassStructure(Offset + 1);
80 dumpLastUses(P, Offset+1);
84 Pass *getContainedPass(unsigned N) {
85 assert(N < PassVector.size() && "Pass number out of range!");
86 return static_cast<Pass *>(PassVector[N]);
89 virtual PassManagerType getPassManagerType() const {
90 return PMT_CallGraphPassManager;
94 bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
95 bool &DevirtualizedCall);
97 bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
98 CallGraph &CG, bool &CallGraphUpToDate,
99 bool &DevirtualizedCall);
100 bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG,
101 bool IsCheckingMode);
104 } // end anonymous namespace.
106 char CGPassManager::ID = 0;
109 bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
110 CallGraph &CG, bool &CallGraphUpToDate,
111 bool &DevirtualizedCall) {
112 bool Changed = false;
113 PMDataManager *PM = P->getAsPMDataManager();
116 CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
117 if (!CallGraphUpToDate) {
118 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
119 CallGraphUpToDate = true;
123 TimeRegion PassTimer(getPassTimer(CGSP));
124 Changed = CGSP->runOnSCC(CurSCC);
127 // After the CGSCCPass is done, when assertions are enabled, use
128 // RefreshCallGraph to verify that the callgraph was correctly updated.
131 RefreshCallGraph(CurSCC, CG, true);
138 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
139 "Invalid CGPassManager member");
140 FPPassManager *FPP = (FPPassManager*)P;
142 // Run pass P on all functions in the current SCC.
143 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
145 if (Function *F = (*I)->getFunction()) {
146 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
147 TimeRegion PassTimer(getPassTimer(FPP));
148 Changed |= FPP->runOnFunction(*F);
152 // The function pass(es) modified the IR, they may have clobbered the
154 if (Changed && CallGraphUpToDate) {
155 DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
156 << P->getPassName() << '\n');
157 CallGraphUpToDate = false;
163 /// RefreshCallGraph - Scan the functions in the specified CFG and resync the
164 /// callgraph with the call sites found in it. This is used after
165 /// FunctionPasses have potentially munged the callgraph, and can be used after
166 /// CallGraphSCC passes to verify that they correctly updated the callgraph.
168 /// This function returns true if it devirtualized an existing function call,
169 /// meaning it turned an indirect call into a direct call. This happens when
170 /// a function pass like GVN optimizes away stuff feeding the indirect call.
171 /// This never happens in checking mode.
173 bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
174 CallGraph &CG, bool CheckingMode) {
175 DenseMap<Value*, CallGraphNode*> CallSites;
177 DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
179 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
184 bool MadeChange = false;
185 bool DevirtualizedCall = false;
187 // Scan all functions in the SCC.
188 unsigned FunctionNo = 0;
189 for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
190 SCCIdx != E; ++SCCIdx, ++FunctionNo) {
191 CallGraphNode *CGN = *SCCIdx;
192 Function *F = CGN->getFunction();
193 if (F == 0 || F->isDeclaration()) continue;
195 // Walk the function body looking for call sites. Sync up the call sites in
196 // CGN with those actually in the function.
198 // Keep track of the number of direct and indirect calls that were
199 // invalidated and removed.
200 unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
202 // Get the set of call sites currently in the function.
203 for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
204 // If this call site is null, then the function pass deleted the call
205 // entirely and the WeakVH nulled it out.
207 // If we've already seen this call site, then the FunctionPass RAUW'd
208 // one call with another, which resulted in two "uses" in the edge
209 // list of the same call.
210 CallSites.count(I->first) ||
212 // If the call edge is not from a call or invoke, then the function
213 // pass RAUW'd a call with another value. This can happen when
214 // constant folding happens of well known functions etc.
215 !CallSite(I->first)) {
216 assert(!CheckingMode &&
217 "CallGraphSCCPass did not update the CallGraph correctly!");
219 // If this was an indirect call site, count it.
220 if (I->second->getFunction() == 0)
221 ++NumIndirectRemoved;
225 // Just remove the edge from the set of callees, keep track of whether
226 // I points to the last element of the vector.
227 bool WasLast = I + 1 == E;
228 CGN->removeCallEdge(I);
230 // If I pointed to the last element of the vector, we have to bail out:
231 // iterator checking rejects comparisons of the resultant pointer with
239 assert(!CallSites.count(I->first) &&
240 "Call site occurs in node multiple times");
241 CallSites.insert(std::make_pair(I->first, I->second));
245 // Loop over all of the instructions in the function, getting the callsites.
246 // Keep track of the number of direct/indirect calls added.
247 unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
249 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
250 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
251 CallSite CS(cast<Value>(I));
253 Function *Callee = CS.getCalledFunction();
254 if (Callee && Callee->isIntrinsic()) continue;
256 // If this call site already existed in the callgraph, just verify it
257 // matches up to expectations and remove it from CallSites.
258 DenseMap<Value*, CallGraphNode*>::iterator ExistingIt =
259 CallSites.find(CS.getInstruction());
260 if (ExistingIt != CallSites.end()) {
261 CallGraphNode *ExistingNode = ExistingIt->second;
263 // Remove from CallSites since we have now seen it.
264 CallSites.erase(ExistingIt);
266 // Verify that the callee is right.
267 if (ExistingNode->getFunction() == CS.getCalledFunction())
270 // If we are in checking mode, we are not allowed to actually mutate
271 // the callgraph. If this is a case where we can infer that the
272 // callgraph is less precise than it could be (e.g. an indirect call
273 // site could be turned direct), don't reject it in checking mode, and
274 // don't tweak it to be more precise.
275 if (CheckingMode && CS.getCalledFunction() &&
276 ExistingNode->getFunction() == 0)
279 assert(!CheckingMode &&
280 "CallGraphSCCPass did not update the CallGraph correctly!");
282 // If not, we either went from a direct call to indirect, indirect to
283 // direct, or direct to different direct.
284 CallGraphNode *CalleeNode;
285 if (Function *Callee = CS.getCalledFunction()) {
286 CalleeNode = CG.getOrInsertFunction(Callee);
287 // Keep track of whether we turned an indirect call into a direct
289 if (ExistingNode->getFunction() == 0) {
290 DevirtualizedCall = true;
291 DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
292 << Callee->getName() << "'\n");
295 CalleeNode = CG.getCallsExternalNode();
298 // Update the edge target in CGN.
299 CGN->replaceCallEdge(CS, CS, CalleeNode);
304 assert(!CheckingMode &&
305 "CallGraphSCCPass did not update the CallGraph correctly!");
307 // If the call site didn't exist in the CGN yet, add it.
308 CallGraphNode *CalleeNode;
309 if (Function *Callee = CS.getCalledFunction()) {
310 CalleeNode = CG.getOrInsertFunction(Callee);
313 CalleeNode = CG.getCallsExternalNode();
317 CGN->addCalledFunction(CS, CalleeNode);
321 // We scanned the old callgraph node, removing invalidated call sites and
322 // then added back newly found call sites. One thing that can happen is
323 // that an old indirect call site was deleted and replaced with a new direct
324 // call. In this case, we have devirtualized a call, and CGSCCPM would like
325 // to iteratively optimize the new code. Unfortunately, we don't really
326 // have a great way to detect when this happens. As an approximation, we
327 // just look at whether the number of indirect calls is reduced and the
328 // number of direct calls is increased. There are tons of ways to fool this
329 // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
330 // direct call) but this is close enough.
331 if (NumIndirectRemoved > NumIndirectAdded &&
332 NumDirectRemoved < NumDirectAdded)
333 DevirtualizedCall = true;
335 // After scanning this function, if we still have entries in callsites, then
336 // they are dangling pointers. WeakVH should save us for this, so abort if
338 assert(CallSites.empty() && "Dangling pointers found in call sites map");
340 // Periodically do an explicit clear to remove tombstones when processing
342 if ((FunctionNo & 15) == 15)
346 DEBUG(if (MadeChange) {
347 dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
348 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
351 if (DevirtualizedCall)
352 dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
355 dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
360 return DevirtualizedCall;
363 /// RunAllPassesOnSCC - Execute the body of the entire pass manager on the
364 /// specified SCC. This keeps track of whether a function pass devirtualizes
365 /// any calls and returns it in DevirtualizedCall.
366 bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
367 bool &DevirtualizedCall) {
368 bool Changed = false;
370 // CallGraphUpToDate - Keep track of whether the callgraph is known to be
371 // up-to-date or not. The CGSSC pass manager runs two types of passes:
372 // CallGraphSCC Passes and other random function passes. Because other
373 // random function passes are not CallGraph aware, they may clobber the
374 // call graph by introducing new calls or deleting other ones. This flag
375 // is set to false when we run a function pass so that we know to clean up
376 // the callgraph when we need to run a CGSCCPass again.
377 bool CallGraphUpToDate = true;
379 // Run all passes on current SCC.
380 for (unsigned PassNo = 0, e = getNumContainedPasses();
381 PassNo != e; ++PassNo) {
382 Pass *P = getContainedPass(PassNo);
384 // If we're in -debug-pass=Executions mode, construct the SCC node list,
385 // otherwise avoid constructing this string as it is expensive.
386 if (isPassDebuggingExecutionsOrMore()) {
387 std::string Functions;
389 raw_string_ostream OS(Functions);
390 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
392 if (I != CurSCC.begin()) OS << ", ";
397 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
401 initializeAnalysisImpl(P);
403 // Actually run this pass on the current SCC.
404 Changed |= RunPassOnSCC(P, CurSCC, CG,
405 CallGraphUpToDate, DevirtualizedCall);
408 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
411 verifyPreservedAnalysis(P);
412 removeNotPreservedAnalysis(P);
413 recordAvailableAnalysis(P);
414 removeDeadPasses(P, "", ON_CG_MSG);
417 // If the callgraph was left out of date (because the last pass run was a
418 // functionpass), refresh it before we move on to the next SCC.
419 if (!CallGraphUpToDate)
420 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
424 /// run - Execute all of the passes scheduled for execution. Keep track of
425 /// whether any of the passes modifies the module, and if so, return true.
426 bool CGPassManager::runOnModule(Module &M) {
427 CallGraph &CG = getAnalysis<CallGraph>();
428 bool Changed = doInitialization(CG);
430 // Walk the callgraph in bottom-up SCC order.
431 scc_iterator<CallGraph*> CGI = scc_begin(&CG);
433 CallGraphSCC CurSCC(&CGI);
434 while (!CGI.isAtEnd()) {
435 // Copy the current SCC and increment past it so that the pass can hack
436 // on the SCC if it wants to without invalidating our iterator.
437 std::vector<CallGraphNode*> &NodeVec = *CGI;
438 CurSCC.initialize(&NodeVec[0], &NodeVec[0]+NodeVec.size());
441 // At the top level, we run all the passes in this pass manager on the
442 // functions in this SCC. However, we support iterative compilation in the
443 // case where a function pass devirtualizes a call to a function. For
444 // example, it is very common for a function pass (often GVN or instcombine)
445 // to eliminate the addressing that feeds into a call. With that improved
446 // information, we would like the call to be an inline candidate, infer
447 // mod-ref information etc.
449 // Because of this, we allow iteration up to a specified iteration count.
450 // This only happens in the case of a devirtualized call, so we only burn
451 // compile time in the case that we're making progress. We also have a hard
452 // iteration count limit in case there is crazy code.
453 unsigned Iteration = 0;
454 bool DevirtualizedCall = false;
457 dbgs() << " SCCPASSMGR: Re-visiting SCC, iteration #"
458 << Iteration << '\n');
459 DevirtualizedCall = false;
460 Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
461 } while (Iteration++ < MaxIterations && DevirtualizedCall);
463 if (DevirtualizedCall)
464 DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " << Iteration
465 << " times, due to -max-cg-scc-iterations\n");
467 if (Iteration > MaxSCCIterations)
468 MaxSCCIterations = Iteration;
471 Changed |= doFinalization(CG);
477 bool CGPassManager::doInitialization(CallGraph &CG) {
478 bool Changed = false;
479 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
480 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
481 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
482 "Invalid CGPassManager member");
483 Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
485 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
492 bool CGPassManager::doFinalization(CallGraph &CG) {
493 bool Changed = false;
494 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
495 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
496 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
497 "Invalid CGPassManager member");
498 Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
500 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
506 //===----------------------------------------------------------------------===//
507 // CallGraphSCC Implementation
508 //===----------------------------------------------------------------------===//
510 /// ReplaceNode - This informs the SCC and the pass manager that the specified
511 /// Old node has been deleted, and New is to be used in its place.
512 void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
513 assert(Old != New && "Should not replace node with self");
514 for (unsigned i = 0; ; ++i) {
515 assert(i != Nodes.size() && "Node not in SCC");
516 if (Nodes[i] != Old) continue;
521 // Update the active scc_iterator so that it doesn't contain dangling
522 // pointers to the old CallGraphNode.
523 scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
524 CGI->ReplaceNode(Old, New);
528 //===----------------------------------------------------------------------===//
529 // CallGraphSCCPass Implementation
530 //===----------------------------------------------------------------------===//
532 /// Assign pass manager to manage this pass.
533 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
534 PassManagerType PreferredType) {
535 // Find CGPassManager
536 while (!PMS.empty() &&
537 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
540 assert(!PMS.empty() && "Unable to handle Call Graph Pass");
543 if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
544 CGP = (CGPassManager*)PMS.top();
546 // Create new Call Graph SCC Pass Manager if it does not exist.
547 assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
548 PMDataManager *PMD = PMS.top();
550 // [1] Create new Call Graph Pass Manager
551 CGP = new CGPassManager();
553 // [2] Set up new manager's top level manager
554 PMTopLevelManager *TPM = PMD->getTopLevelManager();
555 TPM->addIndirectPassManager(CGP);
557 // [3] Assign manager to manage this new manager. This may create
558 // and push new managers into PMS
560 TPM->schedulePass(P);
562 // [4] Push new manager into PMS
569 /// getAnalysisUsage - For this class, we declare that we require and preserve
570 /// the call graph. If the derived class implements this method, it should
571 /// always explicitly call the implementation here.
572 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
573 AU.addRequired<CallGraph>();
574 AU.addPreserved<CallGraph>();
578 //===----------------------------------------------------------------------===//
579 // PrintCallGraphPass Implementation
580 //===----------------------------------------------------------------------===//
583 /// PrintCallGraphPass - Print a Module corresponding to a call graph.
585 class PrintCallGraphPass : public CallGraphSCCPass {
587 raw_ostream &Out; // raw_ostream to print on.
591 PrintCallGraphPass(const std::string &B, raw_ostream &o)
592 : CallGraphSCCPass(ID), Banner(B), Out(o) {}
594 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
595 AU.setPreservesAll();
598 bool runOnSCC(CallGraphSCC &SCC) {
600 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
601 (*I)->getFunction()->print(Out);
606 } // end anonymous namespace.
608 char PrintCallGraphPass::ID = 0;
610 Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
611 const std::string &Banner) const {
612 return new PrintCallGraphPass(Banner, O);