14a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
22b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
72b37d7cf28b1382420b5e4007042feeb66d21ac8Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
94a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner//
104a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner// This file implements the CallGraphSCCPass class, which is used for passes
114a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner// which are implemented as bottom-up traversals on the call graph.  Because
124a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner// there may be cycles in the call graph, passes of this type operate on the
134a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner// call-graph in SCC order: that is, they process function bottom-up, except for
144a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner// recursive functions, which they process all at once.
154a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner//
164a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner//===----------------------------------------------------------------------===//
174a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner
18be577659d3c1222cc58c33628c0baddb94d241abChris Lattner#define DEBUG_TYPE "cgscc-passmgr"
193251e81d793a293b78f4914be6093b405c24fc2aChandler Carruth#include "llvm/Analysis/CallGraphSCCPass.h"
20551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/SCCIterator.h"
2108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner#include "llvm/ADT/Statistic.h"
22d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Analysis/CallGraph.h"
230b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h"
240b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h"
25d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/PassManagers.h"
2608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner#include "llvm/Support/CommandLine.h"
27be577659d3c1222cc58c33628c0baddb94d241abChris Lattner#include "llvm/Support/Debug.h"
28a782e75d487006cafffdc256b3c623307fee4dcfChris Lattner#include "llvm/Support/Timer.h"
2945cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner#include "llvm/Support/raw_ostream.h"
30a10df5028211fc897751d23e91d035db47d23facChris Lattnerusing namespace llvm;
31d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
3208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattnerstatic cl::opt<unsigned>
336da12e6767100d3f874c2e05aae74e4fe24357b1Chris LattnerMaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
3408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
3508e322db8a544d100f7518cfbd39ef8c13067698Chris LattnerSTATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
3608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
3775f9abf51706242a745e05841598f68e64143494Devang Patel//===----------------------------------------------------------------------===//
3875f9abf51706242a745e05841598f68e64143494Devang Patel// CGPassManager
3975f9abf51706242a745e05841598f68e64143494Devang Patel//
40db2659be58a5d9a820eeb5884f284143f6676f86Dale Johannesen/// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
4175f9abf51706242a745e05841598f68e64143494Devang Patel
42844731a7f1909f55935e3514c9e713a62d67662eDan Gohmannamespace {
43844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
4475f9abf51706242a745e05841598f68e64143494Devang Patelclass CGPassManager : public ModulePass, public PMDataManager {
4575f9abf51706242a745e05841598f68e64143494Devang Patelpublic:
461997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel  static char ID;
470e122d1c2422285c872f68fc0ae1f7e5d2739572Andrew Trick  explicit CGPassManager()
480e122d1c2422285c872f68fc0ae1f7e5d2739572Andrew Trick    : ModulePass(ID), PMDataManager() { }
4975f9abf51706242a745e05841598f68e64143494Devang Patel
5075f9abf51706242a745e05841598f68e64143494Devang Patel  /// run - Execute all of the passes scheduled for execution.  Keep track of
5175f9abf51706242a745e05841598f68e64143494Devang Patel  /// whether any of the passes modifies the module, and if so, return true.
5275f9abf51706242a745e05841598f68e64143494Devang Patel  bool runOnModule(Module &M);
5375f9abf51706242a745e05841598f68e64143494Devang Patel
5440b6fdb81e12b40dd41c9f9f07befb60ec7291c3Owen Anderson  using ModulePass::doInitialization;
5540b6fdb81e12b40dd41c9f9f07befb60ec7291c3Owen Anderson  using ModulePass::doFinalization;
5640b6fdb81e12b40dd41c9f9f07befb60ec7291c3Owen Anderson
57905c7e9a0459ffdffbee3e6fffe0dbf5e5c9583cBill Wendling  bool doInitialization(CallGraph &CG);
58905c7e9a0459ffdffbee3e6fffe0dbf5e5c9583cBill Wendling  bool doFinalization(CallGraph &CG);
5975f9abf51706242a745e05841598f68e64143494Devang Patel
6075f9abf51706242a745e05841598f68e64143494Devang Patel  /// Pass Manager itself does not invalidate any analysis info.
6175f9abf51706242a745e05841598f68e64143494Devang Patel  void getAnalysisUsage(AnalysisUsage &Info) const {
6275f9abf51706242a745e05841598f68e64143494Devang Patel    // CGPassManager walks SCC and it needs CallGraph.
6375f9abf51706242a745e05841598f68e64143494Devang Patel    Info.addRequired<CallGraph>();
6475f9abf51706242a745e05841598f68e64143494Devang Patel    Info.setPreservesAll();
6575f9abf51706242a745e05841598f68e64143494Devang Patel  }
6675f9abf51706242a745e05841598f68e64143494Devang Patel
67505f36aedec873d99bdaabb8836eac704185f086Devang Patel  virtual const char *getPassName() const {
68505f36aedec873d99bdaabb8836eac704185f086Devang Patel    return "CallGraph Pass Manager";
69505f36aedec873d99bdaabb8836eac704185f086Devang Patel  }
70505f36aedec873d99bdaabb8836eac704185f086Devang Patel
713660ecabbb85b31308f38938ce3f56f0a330a84bChris Lattner  virtual PMDataManager *getAsPMDataManager() { return this; }
723660ecabbb85b31308f38938ce3f56f0a330a84bChris Lattner  virtual Pass *getAsPass() { return this; }
733660ecabbb85b31308f38938ce3f56f0a330a84bChris Lattner
7475f9abf51706242a745e05841598f68e64143494Devang Patel  // Print passes managed by this manager
7575f9abf51706242a745e05841598f68e64143494Devang Patel  void dumpPassStructure(unsigned Offset) {
762e48e53d980ee9af1bc96d8ffd9d089cb8d3b5efDavid Greene    errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
7775f9abf51706242a745e05841598f68e64143494Devang Patel    for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
7875f9abf51706242a745e05841598f68e64143494Devang Patel      Pass *P = getContainedPass(Index);
798a757aeac436ecd27e28a39b10032fd6fda33780Dan Gohman      P->dumpPassStructure(Offset + 1);
8075f9abf51706242a745e05841598f68e64143494Devang Patel      dumpLastUses(P, Offset+1);
8175f9abf51706242a745e05841598f68e64143494Devang Patel    }
8275f9abf51706242a745e05841598f68e64143494Devang Patel  }
8375f9abf51706242a745e05841598f68e64143494Devang Patel
8475f9abf51706242a745e05841598f68e64143494Devang Patel  Pass *getContainedPass(unsigned N) {
8545cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner    assert(N < PassVector.size() && "Pass number out of range!");
8645cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner    return static_cast<Pass *>(PassVector[N]);
8775f9abf51706242a745e05841598f68e64143494Devang Patel  }
8875f9abf51706242a745e05841598f68e64143494Devang Patel
8984da80d10b472332d079c58d21aa48b82e636274Devang Patel  virtual PassManagerType getPassManagerType() const {
9075f9abf51706242a745e05841598f68e64143494Devang Patel    return PMT_CallGraphPassManager;
9175f9abf51706242a745e05841598f68e64143494Devang Patel  }
92f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner
93f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattnerprivate:
9408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
9508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner                         bool &DevirtualizedCall);
9608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
972decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner  bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
9808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner                    CallGraph &CG, bool &CallGraphUpToDate,
9908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner                    bool &DevirtualizedCall);
10008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG,
1015a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner                        bool IsCheckingMode);
10275f9abf51706242a745e05841598f68e64143494Devang Patel};
10375f9abf51706242a745e05841598f68e64143494Devang Patel
104f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner} // end anonymous namespace.
105844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
1061997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patelchar CGPassManager::ID = 0;
107f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner
1085c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene
1092decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattnerbool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
11008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner                                 CallGraph &CG, bool &CallGraphUpToDate,
11108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner                                 bool &DevirtualizedCall) {
112f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner  bool Changed = false;
1135a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner  PMDataManager *PM = P->getAsPMDataManager();
1145a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner
1155a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner  if (PM == 0) {
1165a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner    CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
117be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    if (!CallGraphUpToDate) {
11808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
119be577659d3c1222cc58c33628c0baddb94d241abChris Lattner      CallGraphUpToDate = true;
120be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    }
12144a18370e0fe77a1b31698e945a9ce9f691364fbChris Lattner
122a782e75d487006cafffdc256b3c623307fee4dcfChris Lattner    {
123a782e75d487006cafffdc256b3c623307fee4dcfChris Lattner      TimeRegion PassTimer(getPassTimer(CGSP));
124a782e75d487006cafffdc256b3c623307fee4dcfChris Lattner      Changed = CGSP->runOnSCC(CurSCC);
125a782e75d487006cafffdc256b3c623307fee4dcfChris Lattner    }
1265a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner
1275a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner    // After the CGSCCPass is done, when assertions are enabled, use
1285a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner    // RefreshCallGraph to verify that the callgraph was correctly updated.
1295a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner#ifndef NDEBUG
1305a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner    if (Changed)
1315a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner      RefreshCallGraph(CurSCC, CG, true);
1325a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner#endif
1335a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner
134f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner    return Changed;
135f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner  }
136f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner
1375a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner
1385a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner  assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
1395a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner         "Invalid CGPassManager member");
1405a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner  FPPassManager *FPP = (FPPassManager*)P;
141f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner
142f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner  // Run pass P on all functions in the current SCC.
1432decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner  for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
1442decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner       I != E; ++I) {
1452decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner    if (Function *F = (*I)->getFunction()) {
146f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner      dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
147a782e75d487006cafffdc256b3c623307fee4dcfChris Lattner      TimeRegion PassTimer(getPassTimer(FPP));
148f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner      Changed |= FPP->runOnFunction(*F);
149f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner    }
150f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner  }
151f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner
1522038cf31684089fe745a3971e32f4d208b68a0a4Chris Lattner  // The function pass(es) modified the IR, they may have clobbered the
153be577659d3c1222cc58c33628c0baddb94d241abChris Lattner  // callgraph.
154be577659d3c1222cc58c33628c0baddb94d241abChris Lattner  if (Changed && CallGraphUpToDate) {
155c81ce58306ce8d087ed3f5accd3212d2d08eb0a9David Greene    DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
156be577659d3c1222cc58c33628c0baddb94d241abChris Lattner                 << P->getPassName() << '\n');
157be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    CallGraphUpToDate = false;
158be577659d3c1222cc58c33628c0baddb94d241abChris Lattner  }
159f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner  return Changed;
160f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner}
161f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner
1625a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner
1635a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner/// RefreshCallGraph - Scan the functions in the specified CFG and resync the
1645a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner/// callgraph with the call sites found in it.  This is used after
1655a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner/// FunctionPasses have potentially munged the callgraph, and can be used after
1665a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner/// CallGraphSCC passes to verify that they correctly updated the callgraph.
1675a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner///
16808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner/// This function returns true if it devirtualized an existing function call,
16908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner/// meaning it turned an indirect call into a direct call.  This happens when
17008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner/// a function pass like GVN optimizes away stuff feeding the indirect call.
17108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner/// This never happens in checking mode.
17208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner///
17308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattnerbool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
1745a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner                                     CallGraph &CG, bool CheckingMode) {
175a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner  DenseMap<Value*, CallGraphNode*> CallSites;
176be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
177c81ce58306ce8d087ed3f5accd3212d2d08eb0a9David Greene  DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
178be577659d3c1222cc58c33628c0baddb94d241abChris Lattner               << " nodes:\n";
1792decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner        for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
1802decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner             I != E; ++I)
1812decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner          (*I)->dump();
182be577659d3c1222cc58c33628c0baddb94d241abChris Lattner        );
183be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
184be577659d3c1222cc58c33628c0baddb94d241abChris Lattner  bool MadeChange = false;
18508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  bool DevirtualizedCall = false;
186be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
187be577659d3c1222cc58c33628c0baddb94d241abChris Lattner  // Scan all functions in the SCC.
1882decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner  unsigned FunctionNo = 0;
1892decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner  for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
1902decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner       SCCIdx != E; ++SCCIdx, ++FunctionNo) {
1912decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner    CallGraphNode *CGN = *SCCIdx;
192be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    Function *F = CGN->getFunction();
193be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    if (F == 0 || F->isDeclaration()) continue;
194be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
195be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    // Walk the function body looking for call sites.  Sync up the call sites in
196be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    // CGN with those actually in the function.
197bccb41afc8542f01358341451cac85a7115d8763Chris Lattner
198bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // Keep track of the number of direct and indirect calls that were
199bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // invalidated and removed.
200bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
20117146b867f72c2ca020e2ba8b0b6cbba93a1f7c8Chris Lattner
202be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    // Get the set of call sites currently in the function.
203b3020d7a08b951adff8d102b27aaa0bc2a7156daDuncan Sands    for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
204a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner      // If this call site is null, then the function pass deleted the call
20517146b867f72c2ca020e2ba8b0b6cbba93a1f7c8Chris Lattner      // entirely and the WeakVH nulled it out.
206a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner      if (I->first == 0 ||
207a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner          // If we've already seen this call site, then the FunctionPass RAUW'd
208a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner          // one call with another, which resulted in two "uses" in the edge
209a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner          // list of the same call.
210a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner          CallSites.count(I->first) ||
211a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner
212a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner          // If the call edge is not from a call or invoke, then the function
213a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner          // pass RAUW'd a call with another value.  This can happen when
214a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner          // constant folding happens of well known functions etc.
215ce4a62639f64d2f24577ad4e872f01cd7d6bc82fGabor Greif          !CallSite(I->first)) {
2165a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner        assert(!CheckingMode &&
2175a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner               "CallGraphSCCPass did not update the CallGraph correctly!");
2185a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner
219bccb41afc8542f01358341451cac85a7115d8763Chris Lattner        // If this was an indirect call site, count it.
220bccb41afc8542f01358341451cac85a7115d8763Chris Lattner        if (I->second->getFunction() == 0)
221bccb41afc8542f01358341451cac85a7115d8763Chris Lattner          ++NumIndirectRemoved;
222bccb41afc8542f01358341451cac85a7115d8763Chris Lattner        else
223bccb41afc8542f01358341451cac85a7115d8763Chris Lattner          ++NumDirectRemoved;
224bccb41afc8542f01358341451cac85a7115d8763Chris Lattner
225d3bd0821b00f1100ab98454264c8775ace826f0dChris Lattner        // Just remove the edge from the set of callees, keep track of whether
226d3bd0821b00f1100ab98454264c8775ace826f0dChris Lattner        // I points to the last element of the vector.
227d3bd0821b00f1100ab98454264c8775ace826f0dChris Lattner        bool WasLast = I + 1 == E;
228a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner        CGN->removeCallEdge(I);
229b8bcbd61a8a8ea3960da47734579150f514abfbbChris Lattner
230d3bd0821b00f1100ab98454264c8775ace826f0dChris Lattner        // If I pointed to the last element of the vector, we have to bail out:
231d3bd0821b00f1100ab98454264c8775ace826f0dChris Lattner        // iterator checking rejects comparisons of the resultant pointer with
232d3bd0821b00f1100ab98454264c8775ace826f0dChris Lattner        // end.
233d3bd0821b00f1100ab98454264c8775ace826f0dChris Lattner        if (WasLast)
234d3bd0821b00f1100ab98454264c8775ace826f0dChris Lattner          break;
235a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner        E = CGN->end();
236a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner        continue;
237a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner      }
23817146b867f72c2ca020e2ba8b0b6cbba93a1f7c8Chris Lattner
239a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner      assert(!CallSites.count(I->first) &&
240be577659d3c1222cc58c33628c0baddb94d241abChris Lattner             "Call site occurs in node multiple times");
241a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner      CallSites.insert(std::make_pair(I->first, I->second));
24217146b867f72c2ca020e2ba8b0b6cbba93a1f7c8Chris Lattner      ++I;
243be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    }
24417146b867f72c2ca020e2ba8b0b6cbba93a1f7c8Chris Lattner
245be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    // Loop over all of the instructions in the function, getting the callsites.
246bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // Keep track of the number of direct/indirect calls added.
247bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
248bccb41afc8542f01358341451cac85a7115d8763Chris Lattner
249be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
250be577659d3c1222cc58c33628c0baddb94d241abChris Lattner      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
251e669d83a21d7ebf01d3c9e37a20c7348b5a77c11John McCall        CallSite CS(cast<Value>(I));
2527d539716e2ef6c953bed51cfab5edba98b557657Nuno Lopes        if (!CS) continue;
2537d539716e2ef6c953bed51cfab5edba98b557657Nuno Lopes        Function *Callee = CS.getCalledFunction();
2547d539716e2ef6c953bed51cfab5edba98b557657Nuno Lopes        if (Callee && Callee->isIntrinsic()) continue;
255be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
256be577659d3c1222cc58c33628c0baddb94d241abChris Lattner        // If this call site already existed in the callgraph, just verify it
257be577659d3c1222cc58c33628c0baddb94d241abChris Lattner        // matches up to expectations and remove it from CallSites.
258a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner        DenseMap<Value*, CallGraphNode*>::iterator ExistingIt =
259be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          CallSites.find(CS.getInstruction());
260be577659d3c1222cc58c33628c0baddb94d241abChris Lattner        if (ExistingIt != CallSites.end()) {
261be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          CallGraphNode *ExistingNode = ExistingIt->second;
262be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
263be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          // Remove from CallSites since we have now seen it.
264be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          CallSites.erase(ExistingIt);
265be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
266be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          // Verify that the callee is right.
267be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          if (ExistingNode->getFunction() == CS.getCalledFunction())
268be577659d3c1222cc58c33628c0baddb94d241abChris Lattner            continue;
269be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
2705a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner          // If we are in checking mode, we are not allowed to actually mutate
2715a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner          // the callgraph.  If this is a case where we can infer that the
2725a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner          // callgraph is less precise than it could be (e.g. an indirect call
2735a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner          // site could be turned direct), don't reject it in checking mode, and
2745a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner          // don't tweak it to be more precise.
2755a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner          if (CheckingMode && CS.getCalledFunction() &&
2765a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner              ExistingNode->getFunction() == 0)
2775a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner            continue;
2785a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner
2795a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner          assert(!CheckingMode &&
2805a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner                 "CallGraphSCCPass did not update the CallGraph correctly!");
2815a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner
282be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          // If not, we either went from a direct call to indirect, indirect to
283be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          // direct, or direct to different direct.
284be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          CallGraphNode *CalleeNode;
28508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner          if (Function *Callee = CS.getCalledFunction()) {
286be577659d3c1222cc58c33628c0baddb94d241abChris Lattner            CalleeNode = CG.getOrInsertFunction(Callee);
28708e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner            // Keep track of whether we turned an indirect call into a direct
28808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner            // one.
28908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner            if (ExistingNode->getFunction() == 0) {
29008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner              DevirtualizedCall = true;
29108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner              DEBUG(dbgs() << "  CGSCCPASSMGR: Devirtualized call to '"
29208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner                           << Callee->getName() << "'\n");
29308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner            }
29408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner          } else {
295be577659d3c1222cc58c33628c0baddb94d241abChris Lattner            CalleeNode = CG.getCallsExternalNode();
29608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner          }
29744a18370e0fe77a1b31698e945a9ce9f691364fbChris Lattner
29844a18370e0fe77a1b31698e945a9ce9f691364fbChris Lattner          // Update the edge target in CGN.
299b61789d4dd851c2481d0ad95c7a710d567fb86f7Chris Lattner          CGN->replaceCallEdge(CS, CS, CalleeNode);
300be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          MadeChange = true;
301be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          continue;
302be577659d3c1222cc58c33628c0baddb94d241abChris Lattner        }
303be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
3045a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner        assert(!CheckingMode &&
3055a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner               "CallGraphSCCPass did not update the CallGraph correctly!");
3065a6a363527f9f66ff23f2d281f4fdc6c2084f2d3Chris Lattner
307bccb41afc8542f01358341451cac85a7115d8763Chris Lattner        // If the call site didn't exist in the CGN yet, add it.
308be577659d3c1222cc58c33628c0baddb94d241abChris Lattner        CallGraphNode *CalleeNode;
309bccb41afc8542f01358341451cac85a7115d8763Chris Lattner        if (Function *Callee = CS.getCalledFunction()) {
310be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          CalleeNode = CG.getOrInsertFunction(Callee);
311bccb41afc8542f01358341451cac85a7115d8763Chris Lattner          ++NumDirectAdded;
312bccb41afc8542f01358341451cac85a7115d8763Chris Lattner        } else {
313be577659d3c1222cc58c33628c0baddb94d241abChris Lattner          CalleeNode = CG.getCallsExternalNode();
314bccb41afc8542f01358341451cac85a7115d8763Chris Lattner          ++NumIndirectAdded;
315bccb41afc8542f01358341451cac85a7115d8763Chris Lattner        }
316be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
317be577659d3c1222cc58c33628c0baddb94d241abChris Lattner        CGN->addCalledFunction(CS, CalleeNode);
318be577659d3c1222cc58c33628c0baddb94d241abChris Lattner        MadeChange = true;
319be577659d3c1222cc58c33628c0baddb94d241abChris Lattner      }
320be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
321bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // We scanned the old callgraph node, removing invalidated call sites and
322bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // then added back newly found call sites.  One thing that can happen is
323bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // that an old indirect call site was deleted and replaced with a new direct
324bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // call.  In this case, we have devirtualized a call, and CGSCCPM would like
325bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // to iteratively optimize the new code.  Unfortunately, we don't really
326bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // have a great way to detect when this happens.  As an approximation, we
327bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // just look at whether the number of indirect calls is reduced and the
328bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // number of direct calls is increased.  There are tons of ways to fool this
329bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
330bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    // direct call) but this is close enough.
331bccb41afc8542f01358341451cac85a7115d8763Chris Lattner    if (NumIndirectRemoved > NumIndirectAdded &&
332bccb41afc8542f01358341451cac85a7115d8763Chris Lattner        NumDirectRemoved < NumDirectAdded)
333bccb41afc8542f01358341451cac85a7115d8763Chris Lattner      DevirtualizedCall = true;
334bccb41afc8542f01358341451cac85a7115d8763Chris Lattner
335be577659d3c1222cc58c33628c0baddb94d241abChris Lattner    // After scanning this function, if we still have entries in callsites, then
336a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner    // they are dangling pointers.  WeakVH should save us for this, so abort if
337a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner    // this happens.
338a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner    assert(CallSites.empty() && "Dangling pointers found in call sites map");
339be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
340a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner    // Periodically do an explicit clear to remove tombstones when processing
341a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner    // large scc's.
3422decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner    if ((FunctionNo & 15) == 15)
343a541b0fde2ab6b8b037edf113d42da41a2c5aae9Chris Lattner      CallSites.clear();
344be577659d3c1222cc58c33628c0baddb94d241abChris Lattner  }
345be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
346be577659d3c1222cc58c33628c0baddb94d241abChris Lattner  DEBUG(if (MadeChange) {
347c81ce58306ce8d087ed3f5accd3212d2d08eb0a9David Greene          dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
3482decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner          for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
3492decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner            I != E; ++I)
3502decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner              (*I)->dump();
351bccb41afc8542f01358341451cac85a7115d8763Chris Lattner          if (DevirtualizedCall)
352bccb41afc8542f01358341451cac85a7115d8763Chris Lattner            dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
353bccb41afc8542f01358341451cac85a7115d8763Chris Lattner
354be577659d3c1222cc58c33628c0baddb94d241abChris Lattner         } else {
355c81ce58306ce8d087ed3f5accd3212d2d08eb0a9David Greene           dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
356be577659d3c1222cc58c33628c0baddb94d241abChris Lattner         }
357be577659d3c1222cc58c33628c0baddb94d241abChris Lattner        );
3581f6a329f79b3568d379142f921f59c4143ddaa14Duncan Sands  (void)MadeChange;
35908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
36008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  return DevirtualizedCall;
36108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner}
36208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
36308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner/// RunAllPassesOnSCC -  Execute the body of the entire pass manager on the
36408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner/// specified SCC.  This keeps track of whether a function pass devirtualizes
36508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner/// any calls and returns it in DevirtualizedCall.
36608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattnerbool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
36708e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner                                      bool &DevirtualizedCall) {
36808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  bool Changed = false;
36908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
37008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // CallGraphUpToDate - Keep track of whether the callgraph is known to be
37108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // up-to-date or not.  The CGSSC pass manager runs two types of passes:
37208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // CallGraphSCC Passes and other random function passes.  Because other
37308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // random function passes are not CallGraph aware, they may clobber the
37408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // call graph by introducing new calls or deleting other ones.  This flag
37508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // is set to false when we run a function pass so that we know to clean up
37608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // the callgraph when we need to run a CGSCCPass again.
37708e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  bool CallGraphUpToDate = true;
37808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
37908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // Run all passes on current SCC.
38008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  for (unsigned PassNo = 0, e = getNumContainedPasses();
38108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner       PassNo != e; ++PassNo) {
38208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    Pass *P = getContainedPass(PassNo);
38308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
38408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // If we're in -debug-pass=Executions mode, construct the SCC node list,
38508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // otherwise avoid constructing this string as it is expensive.
38608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    if (isPassDebuggingExecutionsOrMore()) {
38708e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      std::string Functions;
38808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  #ifndef NDEBUG
38908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      raw_string_ostream OS(Functions);
39008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
39108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner           I != E; ++I) {
39208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner        if (I != CurSCC.begin()) OS << ", ";
39308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner        (*I)->print(OS);
39408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      }
39508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      OS.flush();
39608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  #endif
39708e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
39808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    }
39908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    dumpRequiredSet(P);
40008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
40108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    initializeAnalysisImpl(P);
40208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
40308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // Actually run this pass on the current SCC.
40408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    Changed |= RunPassOnSCC(P, CurSCC, CG,
40508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner                            CallGraphUpToDate, DevirtualizedCall);
40608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
40708e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    if (Changed)
40808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
40908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    dumpPreservedSet(P);
41008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
41108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    verifyPreservedAnalysis(P);
41208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    removeNotPreservedAnalysis(P);
41308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    recordAvailableAnalysis(P);
41408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    removeDeadPasses(P, "", ON_CG_MSG);
41508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  }
41608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
41708e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // If the callgraph was left out of date (because the last pass run was a
41808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  // functionpass), refresh it before we move on to the next SCC.
41908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  if (!CallGraphUpToDate)
42008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
42108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner  return Changed;
422be577659d3c1222cc58c33628c0baddb94d241abChris Lattner}
423f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner
42475f9abf51706242a745e05841598f68e64143494Devang Patel/// run - Execute all of the passes scheduled for execution.  Keep track of
42575f9abf51706242a745e05841598f68e64143494Devang Patel/// whether any of the passes modifies the module, and if so, return true.
42675f9abf51706242a745e05841598f68e64143494Devang Patelbool CGPassManager::runOnModule(Module &M) {
42775f9abf51706242a745e05841598f68e64143494Devang Patel  CallGraph &CG = getAnalysis<CallGraph>();
428905c7e9a0459ffdffbee3e6fffe0dbf5e5c9583cBill Wendling  bool Changed = doInitialization(CG);
42908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
430f3a1c15b750c63accd3597b42d73792458b247a9Chris Lattner  // Walk the callgraph in bottom-up SCC order.
431a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner  scc_iterator<CallGraph*> CGI = scc_begin(&CG);
432a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner
433a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner  CallGraphSCC CurSCC(&CGI);
434a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner  while (!CGI.isAtEnd()) {
4355095e3d1d1caef8d573534d369e37277c623064cChris Lattner    // Copy the current SCC and increment past it so that the pass can hack
4365095e3d1d1caef8d573534d369e37277c623064cChris Lattner    // on the SCC if it wants to without invalidating our iterator.
4372decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner    std::vector<CallGraphNode*> &NodeVec = *CGI;
4382decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner    CurSCC.initialize(&NodeVec[0], &NodeVec[0]+NodeVec.size());
4395095e3d1d1caef8d573534d369e37277c623064cChris Lattner    ++CGI;
4405095e3d1d1caef8d573534d369e37277c623064cChris Lattner
44108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // At the top level, we run all the passes in this pass manager on the
44208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // functions in this SCC.  However, we support iterative compilation in the
44308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // case where a function pass devirtualizes a call to a function.  For
44408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // example, it is very common for a function pass (often GVN or instcombine)
44508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // to eliminate the addressing that feeds into a call.  With that improved
44608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // information, we would like the call to be an inline candidate, infer
44708e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // mod-ref information etc.
44808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    //
44908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // Because of this, we allow iteration up to a specified iteration count.
45008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // This only happens in the case of a devirtualized call, so we only burn
45108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // compile time in the case that we're making progress.  We also have a hard
45208e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    // iteration count limit in case there is crazy code.
45308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    unsigned Iteration = 0;
45408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    bool DevirtualizedCall = false;
45508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    do {
456b61789d4dd851c2481d0ad95c7a710d567fb86f7Chris Lattner      DEBUG(if (Iteration)
457b61789d4dd851c2481d0ad95c7a710d567fb86f7Chris Lattner              dbgs() << "  SCCPASSMGR: Re-visiting SCC, iteration #"
458b61789d4dd851c2481d0ad95c7a710d567fb86f7Chris Lattner                     << Iteration << '\n');
45908e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      DevirtualizedCall = false;
46008e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
46108e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    } while (Iteration++ < MaxIterations && DevirtualizedCall);
462be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
46308e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    if (DevirtualizedCall)
46408e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      DEBUG(dbgs() << "  CGSCCPASSMGR: Stopped iteration after " << Iteration
46508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner                   << " times, due to -max-cg-scc-iterations\n");
46608e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
46708e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner    if (Iteration > MaxSCCIterations)
46808e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner      MaxSCCIterations = Iteration;
469be577659d3c1222cc58c33628c0baddb94d241abChris Lattner
47075f9abf51706242a745e05841598f68e64143494Devang Patel  }
471905c7e9a0459ffdffbee3e6fffe0dbf5e5c9583cBill Wendling  Changed |= doFinalization(CG);
47275f9abf51706242a745e05841598f68e64143494Devang Patel  return Changed;
47375f9abf51706242a745e05841598f68e64143494Devang Patel}
47475f9abf51706242a745e05841598f68e64143494Devang Patel
47508e322db8a544d100f7518cfbd39ef8c13067698Chris Lattner
47675f9abf51706242a745e05841598f68e64143494Devang Patel/// Initialize CG
477905c7e9a0459ffdffbee3e6fffe0dbf5e5c9583cBill Wendlingbool CGPassManager::doInitialization(CallGraph &CG) {
47875f9abf51706242a745e05841598f68e64143494Devang Patel  bool Changed = false;
4795a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner  for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
4805a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner    if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
4815a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner      assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
4825a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner             "Invalid CGPassManager member");
4835a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner      Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
4848968a0776801c5662863b3a461550aa361f69ba3Nick Lewycky    } else {
4855a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner      Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
4868968a0776801c5662863b3a461550aa361f69ba3Nick Lewycky    }
48775f9abf51706242a745e05841598f68e64143494Devang Patel  }
48875f9abf51706242a745e05841598f68e64143494Devang Patel  return Changed;
48975f9abf51706242a745e05841598f68e64143494Devang Patel}
49075f9abf51706242a745e05841598f68e64143494Devang Patel
49175f9abf51706242a745e05841598f68e64143494Devang Patel/// Finalize CG
492905c7e9a0459ffdffbee3e6fffe0dbf5e5c9583cBill Wendlingbool CGPassManager::doFinalization(CallGraph &CG) {
49375f9abf51706242a745e05841598f68e64143494Devang Patel  bool Changed = false;
4945a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner  for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
4955a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner    if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
4965a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner      assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
4975a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner             "Invalid CGPassManager member");
4985a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner      Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
4998968a0776801c5662863b3a461550aa361f69ba3Nick Lewycky    } else {
5005a66cb9a4450e5b7b0f5f586ab47a529c478223bChris Lattner      Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
5018968a0776801c5662863b3a461550aa361f69ba3Nick Lewycky    }
50275f9abf51706242a745e05841598f68e64143494Devang Patel  }
50375f9abf51706242a745e05841598f68e64143494Devang Patel  return Changed;
50475f9abf51706242a745e05841598f68e64143494Devang Patel}
50575f9abf51706242a745e05841598f68e64143494Devang Patel
506c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner//===----------------------------------------------------------------------===//
5072decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner// CallGraphSCC Implementation
5082decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner//===----------------------------------------------------------------------===//
5092decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner
510a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner/// ReplaceNode - This informs the SCC and the pass manager that the specified
511a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner/// Old node has been deleted, and New is to be used in its place.
512a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattnervoid CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
513a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner  assert(Old != New && "Should not replace node with self");
514a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner  for (unsigned i = 0; ; ++i) {
515a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner    assert(i != Nodes.size() && "Node not in SCC");
516a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner    if (Nodes[i] != Old) continue;
517a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner    Nodes[i] = New;
518a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner    break;
519a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner  }
520bde0bb5f882ae708ca70a42bdd3a9805e63f6fb7Chris Lattner
521bde0bb5f882ae708ca70a42bdd3a9805e63f6fb7Chris Lattner  // Update the active scc_iterator so that it doesn't contain dangling
522bde0bb5f882ae708ca70a42bdd3a9805e63f6fb7Chris Lattner  // pointers to the old CallGraphNode.
523bde0bb5f882ae708ca70a42bdd3a9805e63f6fb7Chris Lattner  scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
524bde0bb5f882ae708ca70a42bdd3a9805e63f6fb7Chris Lattner  CGI->ReplaceNode(Old, New);
525a3dfc646b4d772979f8994c07eeee6af57480d34Chris Lattner}
5262decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner
5272decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner
5282decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner//===----------------------------------------------------------------------===//
529c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner// CallGraphSCCPass Implementation
530c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner//===----------------------------------------------------------------------===//
5315c8aa950fe3484b6e115647328c196f8be64f9edDavid Greene
532d9f10c3d11997bc7e3d1b3da3baabae47445d5c6Devang Patel/// Assign pass manager to manage this pass.
53397fd2439f2b30b575cdd67eef52e03937d483680Devang Patelvoid CallGraphSCCPass::assignPassManager(PMStack &PMS,
534bed2946a96ecb15b0b636fa74cb26ce61b1c648eAnton Korobeynikov                                         PassManagerType PreferredType) {
53597fd2439f2b30b575cdd67eef52e03937d483680Devang Patel  // Find CGPassManager
53620d824b7dfea987775586d9de56c7448cc2cc44dDuncan Sands  while (!PMS.empty() &&
53720d824b7dfea987775586d9de56c7448cc2cc44dDuncan Sands         PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
53820d824b7dfea987775586d9de56c7448cc2cc44dDuncan Sands    PMS.pop();
53997fd2439f2b30b575cdd67eef52e03937d483680Devang Patel
54077c95ed74a4d5f122b3c9810209a0190dbf17504Chris Lattner  assert(!PMS.empty() && "Unable to handle Call Graph Pass");
54177c95ed74a4d5f122b3c9810209a0190dbf17504Chris Lattner  CGPassManager *CGP;
54277c95ed74a4d5f122b3c9810209a0190dbf17504Chris Lattner
54377c95ed74a4d5f122b3c9810209a0190dbf17504Chris Lattner  if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
54477c95ed74a4d5f122b3c9810209a0190dbf17504Chris Lattner    CGP = (CGPassManager*)PMS.top();
54577c95ed74a4d5f122b3c9810209a0190dbf17504Chris Lattner  else {
54677c95ed74a4d5f122b3c9810209a0190dbf17504Chris Lattner    // Create new Call Graph SCC Pass Manager if it does not exist.
54777c95ed74a4d5f122b3c9810209a0190dbf17504Chris Lattner    assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
54897fd2439f2b30b575cdd67eef52e03937d483680Devang Patel    PMDataManager *PMD = PMS.top();
54997fd2439f2b30b575cdd67eef52e03937d483680Devang Patel
55097fd2439f2b30b575cdd67eef52e03937d483680Devang Patel    // [1] Create new Call Graph Pass Manager
5510e122d1c2422285c872f68fc0ae1f7e5d2739572Andrew Trick    CGP = new CGPassManager();
55297fd2439f2b30b575cdd67eef52e03937d483680Devang Patel
55397fd2439f2b30b575cdd67eef52e03937d483680Devang Patel    // [2] Set up new manager's top level manager
55497fd2439f2b30b575cdd67eef52e03937d483680Devang Patel    PMTopLevelManager *TPM = PMD->getTopLevelManager();
55597fd2439f2b30b575cdd67eef52e03937d483680Devang Patel    TPM->addIndirectPassManager(CGP);
55697fd2439f2b30b575cdd67eef52e03937d483680Devang Patel
55797fd2439f2b30b575cdd67eef52e03937d483680Devang Patel    // [3] Assign manager to manage this new manager. This may create
55897fd2439f2b30b575cdd67eef52e03937d483680Devang Patel    // and push new managers into PMS
55977c95ed74a4d5f122b3c9810209a0190dbf17504Chris Lattner    Pass *P = CGP;
56025e681ac221e83a7ebead99b4d3d88f1b3ab49bdDevang Patel    TPM->schedulePass(P);
56197fd2439f2b30b575cdd67eef52e03937d483680Devang Patel
56297fd2439f2b30b575cdd67eef52e03937d483680Devang Patel    // [4] Push new manager into PMS
56397fd2439f2b30b575cdd67eef52e03937d483680Devang Patel    PMS.push(CGP);
56497fd2439f2b30b575cdd67eef52e03937d483680Devang Patel  }
56597fd2439f2b30b575cdd67eef52e03937d483680Devang Patel
56697fd2439f2b30b575cdd67eef52e03937d483680Devang Patel  CGP->add(this);
56797fd2439f2b30b575cdd67eef52e03937d483680Devang Patel}
56897fd2439f2b30b575cdd67eef52e03937d483680Devang Patel
5694a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner/// getAnalysisUsage - For this class, we declare that we require and preserve
5704a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner/// the call graph.  If the derived class implements this method, it should
5714a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner/// always explicitly call the implementation here.
5724a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattnervoid CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
5734a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner  AU.addRequired<CallGraph>();
5744a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner  AU.addPreserved<CallGraph>();
5754a81067a84e18c44898149f5afdbaa3e18b3e821Chris Lattner}
576c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
577c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
578c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner//===----------------------------------------------------------------------===//
579c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner// PrintCallGraphPass Implementation
580c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner//===----------------------------------------------------------------------===//
581c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
582c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattnernamespace {
583c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner  /// PrintCallGraphPass - Print a Module corresponding to a call graph.
584c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner  ///
585c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner  class PrintCallGraphPass : public CallGraphSCCPass {
586c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner    std::string Banner;
587c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner    raw_ostream &Out;       // raw_ostream to print on.
588c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
589c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner  public:
590c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner    static char ID;
591c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner    PrintCallGraphPass(const std::string &B, raw_ostream &o)
59290c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson      : CallGraphSCCPass(ID), Banner(B), Out(o) {}
593c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
594c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
595c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner      AU.setPreservesAll();
596c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner    }
597c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
5982decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner    bool runOnSCC(CallGraphSCC &SCC) {
599c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner      Out << Banner;
6002decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner      for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
6012decb22222cac46bb1d9163e7b89d7e5be8ef65fChris Lattner        (*I)->getFunction()->print(Out);
602c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner      return false;
603c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner    }
604c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner  };
605c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
606c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner} // end anonymous namespace.
607c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
608c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattnerchar PrintCallGraphPass::ID = 0;
609c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
610c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris LattnerPass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
611c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner                                          const std::string &Banner) const {
612c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner  return new PrintCallGraphPass(Banner, O);
613c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner}
614c93760c3e55db7e5fadbbf0f75fa3587cd8bb9dcChris Lattner
615