1//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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
18#define DEBUG_TYPE "cgscc-passmgr"
19#include "llvm/Analysis/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/IR/LegacyPassManagers.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"
30using namespace llvm;
31
32static cl::opt<unsigned>
33MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
34
35STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
36
37//===----------------------------------------------------------------------===//
38// CGPassManager
39//
40/// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
41
42namespace {
43
44class CGPassManager : public ModulePass, public PMDataManager {
45public:
46  static char ID;
47  explicit CGPassManager()
48    : ModulePass(ID), PMDataManager() { }
49
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);
53
54  using ModulePass::doInitialization;
55  using ModulePass::doFinalization;
56
57  bool doInitialization(CallGraph &CG);
58  bool doFinalization(CallGraph &CG);
59
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();
65  }
66
67  virtual const char *getPassName() const {
68    return "CallGraph Pass Manager";
69  }
70
71  virtual PMDataManager *getAsPMDataManager() { return this; }
72  virtual Pass *getAsPass() { return this; }
73
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);
81    }
82  }
83
84  Pass *getContainedPass(unsigned N) {
85    assert(N < PassVector.size() && "Pass number out of range!");
86    return static_cast<Pass *>(PassVector[N]);
87  }
88
89  virtual PassManagerType getPassManagerType() const {
90    return PMT_CallGraphPassManager;
91  }
92
93private:
94  bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
95                         bool &DevirtualizedCall);
96
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);
102};
103
104} // end anonymous namespace.
105
106char CGPassManager::ID = 0;
107
108
109bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
110                                 CallGraph &CG, bool &CallGraphUpToDate,
111                                 bool &DevirtualizedCall) {
112  bool Changed = false;
113  PMDataManager *PM = P->getAsPMDataManager();
114
115  if (PM == 0) {
116    CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
117    if (!CallGraphUpToDate) {
118      DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
119      CallGraphUpToDate = true;
120    }
121
122    {
123      TimeRegion PassTimer(getPassTimer(CGSP));
124      Changed = CGSP->runOnSCC(CurSCC);
125    }
126
127    // After the CGSCCPass is done, when assertions are enabled, use
128    // RefreshCallGraph to verify that the callgraph was correctly updated.
129#ifndef NDEBUG
130    if (Changed)
131      RefreshCallGraph(CurSCC, CG, true);
132#endif
133
134    return Changed;
135  }
136
137
138  assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
139         "Invalid CGPassManager member");
140  FPPassManager *FPP = (FPPassManager*)P;
141
142  // Run pass P on all functions in the current SCC.
143  for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
144       I != E; ++I) {
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);
149    }
150  }
151
152  // The function pass(es) modified the IR, they may have clobbered the
153  // callgraph.
154  if (Changed && CallGraphUpToDate) {
155    DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
156                 << P->getPassName() << '\n');
157    CallGraphUpToDate = false;
158  }
159  return Changed;
160}
161
162
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.
167///
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.
172///
173bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
174                                     CallGraph &CG, bool CheckingMode) {
175  DenseMap<Value*, CallGraphNode*> CallSites;
176
177  DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
178               << " nodes:\n";
179        for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
180             I != E; ++I)
181          (*I)->dump();
182        );
183
184  bool MadeChange = false;
185  bool DevirtualizedCall = false;
186
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;
194
195    // Walk the function body looking for call sites.  Sync up the call sites in
196    // CGN with those actually in the function.
197
198    // Keep track of the number of direct and indirect calls that were
199    // invalidated and removed.
200    unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
201
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.
206      if (I->first == 0 ||
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) ||
211
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!");
218
219        // If this was an indirect call site, count it.
220        if (I->second->getFunction() == 0)
221          ++NumIndirectRemoved;
222        else
223          ++NumDirectRemoved;
224
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);
229
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
232        // end.
233        if (WasLast)
234          break;
235        E = CGN->end();
236        continue;
237      }
238
239      assert(!CallSites.count(I->first) &&
240             "Call site occurs in node multiple times");
241      CallSites.insert(std::make_pair(I->first, I->second));
242      ++I;
243    }
244
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;
248
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));
252        if (!CS) continue;
253        Function *Callee = CS.getCalledFunction();
254        if (Callee && Callee->isIntrinsic()) continue;
255
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;
262
263          // Remove from CallSites since we have now seen it.
264          CallSites.erase(ExistingIt);
265
266          // Verify that the callee is right.
267          if (ExistingNode->getFunction() == CS.getCalledFunction())
268            continue;
269
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)
277            continue;
278
279          assert(!CheckingMode &&
280                 "CallGraphSCCPass did not update the CallGraph correctly!");
281
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
288            // one.
289            if (ExistingNode->getFunction() == 0) {
290              DevirtualizedCall = true;
291              DEBUG(dbgs() << "  CGSCCPASSMGR: Devirtualized call to '"
292                           << Callee->getName() << "'\n");
293            }
294          } else {
295            CalleeNode = CG.getCallsExternalNode();
296          }
297
298          // Update the edge target in CGN.
299          CGN->replaceCallEdge(CS, CS, CalleeNode);
300          MadeChange = true;
301          continue;
302        }
303
304        assert(!CheckingMode &&
305               "CallGraphSCCPass did not update the CallGraph correctly!");
306
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);
311          ++NumDirectAdded;
312        } else {
313          CalleeNode = CG.getCallsExternalNode();
314          ++NumIndirectAdded;
315        }
316
317        CGN->addCalledFunction(CS, CalleeNode);
318        MadeChange = true;
319      }
320
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;
334
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
337    // this happens.
338    assert(CallSites.empty() && "Dangling pointers found in call sites map");
339
340    // Periodically do an explicit clear to remove tombstones when processing
341    // large scc's.
342    if ((FunctionNo & 15) == 15)
343      CallSites.clear();
344  }
345
346  DEBUG(if (MadeChange) {
347          dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
348          for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
349            I != E; ++I)
350              (*I)->dump();
351          if (DevirtualizedCall)
352            dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
353
354         } else {
355           dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
356         }
357        );
358  (void)MadeChange;
359
360  return DevirtualizedCall;
361}
362
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.
366bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
367                                      bool &DevirtualizedCall) {
368  bool Changed = false;
369
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;
378
379  // Run all passes on current SCC.
380  for (unsigned PassNo = 0, e = getNumContainedPasses();
381       PassNo != e; ++PassNo) {
382    Pass *P = getContainedPass(PassNo);
383
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;
388  #ifndef NDEBUG
389      raw_string_ostream OS(Functions);
390      for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
391           I != E; ++I) {
392        if (I != CurSCC.begin()) OS << ", ";
393        (*I)->print(OS);
394      }
395      OS.flush();
396  #endif
397      dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
398    }
399    dumpRequiredSet(P);
400
401    initializeAnalysisImpl(P);
402
403    // Actually run this pass on the current SCC.
404    Changed |= RunPassOnSCC(P, CurSCC, CG,
405                            CallGraphUpToDate, DevirtualizedCall);
406
407    if (Changed)
408      dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
409    dumpPreservedSet(P);
410
411    verifyPreservedAnalysis(P);
412    removeNotPreservedAnalysis(P);
413    recordAvailableAnalysis(P);
414    removeDeadPasses(P, "", ON_CG_MSG);
415  }
416
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);
421  return Changed;
422}
423
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.
426bool CGPassManager::runOnModule(Module &M) {
427  CallGraph &CG = getAnalysis<CallGraph>();
428  bool Changed = doInitialization(CG);
429
430  // Walk the callgraph in bottom-up SCC order.
431  scc_iterator<CallGraph*> CGI = scc_begin(&CG);
432
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());
439    ++CGI;
440
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.
448    //
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;
455    do {
456      DEBUG(if (Iteration)
457              dbgs() << "  SCCPASSMGR: Re-visiting SCC, iteration #"
458                     << Iteration << '\n');
459      DevirtualizedCall = false;
460      Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
461    } while (Iteration++ < MaxIterations && DevirtualizedCall);
462
463    if (DevirtualizedCall)
464      DEBUG(dbgs() << "  CGSCCPASSMGR: Stopped iteration after " << Iteration
465                   << " times, due to -max-cg-scc-iterations\n");
466
467    if (Iteration > MaxSCCIterations)
468      MaxSCCIterations = Iteration;
469
470  }
471  Changed |= doFinalization(CG);
472  return Changed;
473}
474
475
476/// Initialize CG
477bool 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());
484    } else {
485      Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
486    }
487  }
488  return Changed;
489}
490
491/// Finalize CG
492bool 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());
499    } else {
500      Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
501    }
502  }
503  return Changed;
504}
505
506//===----------------------------------------------------------------------===//
507// CallGraphSCC Implementation
508//===----------------------------------------------------------------------===//
509
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.
512void 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;
517    Nodes[i] = New;
518    break;
519  }
520
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);
525}
526
527
528//===----------------------------------------------------------------------===//
529// CallGraphSCCPass Implementation
530//===----------------------------------------------------------------------===//
531
532/// Assign pass manager to manage this pass.
533void CallGraphSCCPass::assignPassManager(PMStack &PMS,
534                                         PassManagerType PreferredType) {
535  // Find CGPassManager
536  while (!PMS.empty() &&
537         PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
538    PMS.pop();
539
540  assert(!PMS.empty() && "Unable to handle Call Graph Pass");
541  CGPassManager *CGP;
542
543  if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
544    CGP = (CGPassManager*)PMS.top();
545  else {
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();
549
550    // [1] Create new Call Graph Pass Manager
551    CGP = new CGPassManager();
552
553    // [2] Set up new manager's top level manager
554    PMTopLevelManager *TPM = PMD->getTopLevelManager();
555    TPM->addIndirectPassManager(CGP);
556
557    // [3] Assign manager to manage this new manager. This may create
558    // and push new managers into PMS
559    Pass *P = CGP;
560    TPM->schedulePass(P);
561
562    // [4] Push new manager into PMS
563    PMS.push(CGP);
564  }
565
566  CGP->add(this);
567}
568
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.
572void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
573  AU.addRequired<CallGraph>();
574  AU.addPreserved<CallGraph>();
575}
576
577
578//===----------------------------------------------------------------------===//
579// PrintCallGraphPass Implementation
580//===----------------------------------------------------------------------===//
581
582namespace {
583  /// PrintCallGraphPass - Print a Module corresponding to a call graph.
584  ///
585  class PrintCallGraphPass : public CallGraphSCCPass {
586    std::string Banner;
587    raw_ostream &Out;       // raw_ostream to print on.
588
589  public:
590    static char ID;
591    PrintCallGraphPass(const std::string &B, raw_ostream &o)
592      : CallGraphSCCPass(ID), Banner(B), Out(o) {}
593
594    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
595      AU.setPreservesAll();
596    }
597
598    bool runOnSCC(CallGraphSCC &SCC) {
599      Out << Banner;
600      for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
601        (*I)->getFunction()->print(Out);
602      return false;
603    }
604  };
605
606} // end anonymous namespace.
607
608char PrintCallGraphPass::ID = 0;
609
610Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
611                                          const std::string &Banner) const {
612  return new PrintCallGraphPass(Banner, O);
613}
614
615