| Chris Lattner | f7e9594 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 1 | //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===// | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // | 
| John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // | 
| John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | f7e9594 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 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 |  | 
| Chandler Carruth | 839a98e | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/CallGraphSCCPass.h" | 
| Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SCCIterator.h" | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/CallGraph.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Function.h" | 
|  | 23 | #include "llvm/IR/IntrinsicInst.h" | 
| Juergen Ributzka | 34390c7 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 24 | #include "llvm/IR/LLVMContext.h" | 
| Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 25 | #include "llvm/IR/LegacyPassManagers.h" | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" | 
| Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Timer.h" | 
| Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" | 
| Chris Lattner | 8d08381 | 2004-04-20 21:30:06 +0000 | [diff] [blame] | 30 | using namespace llvm; | 
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 |  | 
| Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 32 | #define DEBUG_TYPE "cgscc-passmgr" | 
|  | 33 |  | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 34 | static cl::opt<unsigned> | 
| Chris Lattner | fc8d9ee | 2010-05-01 01:15:56 +0000 | [diff] [blame] | 35 | MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4)); | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 36 |  | 
|  | 37 | STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC"); | 
|  | 38 |  | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// | 
|  | 40 | // CGPassManager | 
|  | 41 | // | 
| Dale Johannesen | 50f0376 | 2009-09-10 22:01:32 +0000 | [diff] [blame] | 42 | /// CGPassManager manages FPPassManagers and CallGraphSCCPasses. | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 43 |  | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 44 | namespace { | 
|  | 45 |  | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 46 | class CGPassManager : public ModulePass, public PMDataManager { | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 47 | public: | 
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 48 | static char ID; | 
| Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 49 | explicit CGPassManager() | 
|  | 50 | : ModulePass(ID), PMDataManager() { } | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 51 |  | 
| Sanjay Patel | d45a3f1 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 52 | /// Execute all of the passes scheduled for execution.  Keep track of | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 53 | /// whether any of the passes modifies the module, and if so, return true. | 
| Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 54 | bool runOnModule(Module &M) override; | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 55 |  | 
| Owen Anderson | 1aa2751 | 2012-11-15 00:14:15 +0000 | [diff] [blame] | 56 | using ModulePass::doInitialization; | 
|  | 57 | using ModulePass::doFinalization; | 
|  | 58 |  | 
| Bill Wendling | 5f14a01 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 59 | bool doInitialization(CallGraph &CG); | 
|  | 60 | bool doFinalization(CallGraph &CG); | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 61 |  | 
|  | 62 | /// Pass Manager itself does not invalidate any analysis info. | 
| Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 63 | void getAnalysisUsage(AnalysisUsage &Info) const override { | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 64 | // CGPassManager walks SCC and it needs CallGraph. | 
| Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 65 | Info.addRequired<CallGraphWrapperPass>(); | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 66 | Info.setPreservesAll(); | 
|  | 67 | } | 
|  | 68 |  | 
| Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 69 | const char *getPassName() const override { | 
| Devang Patel | f7fea8a | 2007-02-01 22:09:37 +0000 | [diff] [blame] | 70 | return "CallGraph Pass Manager"; | 
|  | 71 | } | 
|  | 72 |  | 
| Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 73 | PMDataManager *getAsPMDataManager() override { return this; } | 
|  | 74 | Pass *getAsPass() override { return this; } | 
| Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 75 |  | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 76 | // Print passes managed by this manager | 
| Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 77 | void dumpPassStructure(unsigned Offset) override { | 
| David Greene | 3d64631 | 2009-12-23 23:09:39 +0000 | [diff] [blame] | 78 | errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n"; | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 79 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { | 
|  | 80 | Pass *P = getContainedPass(Index); | 
| Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 81 | P->dumpPassStructure(Offset + 1); | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 82 | dumpLastUses(P, Offset+1); | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | Pass *getContainedPass(unsigned N) { | 
| Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 87 | assert(N < PassVector.size() && "Pass number out of range!"); | 
|  | 88 | return static_cast<Pass *>(PassVector[N]); | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
| Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 91 | PassManagerType getPassManagerType() const override { | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 92 | return PMT_CallGraphPassManager; | 
|  | 93 | } | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 94 |  | 
|  | 95 | private: | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 96 | bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, | 
|  | 97 | bool &DevirtualizedCall); | 
|  | 98 |  | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 99 | bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 100 | CallGraph &CG, bool &CallGraphUpToDate, | 
|  | 101 | bool &DevirtualizedCall); | 
|  | 102 | bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG, | 
| Chris Lattner | e33167a | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 103 | bool IsCheckingMode); | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 104 | }; | 
|  | 105 |  | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 106 | } // end anonymous namespace. | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 107 |  | 
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 108 | char CGPassManager::ID = 0; | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 109 |  | 
| David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 110 |  | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 111 | bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 112 | CallGraph &CG, bool &CallGraphUpToDate, | 
|  | 113 | bool &DevirtualizedCall) { | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 114 | bool Changed = false; | 
| Chris Lattner | 0b1c723 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 115 | PMDataManager *PM = P->getAsPMDataManager(); | 
|  | 116 |  | 
| Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 117 | if (!PM) { | 
| Chris Lattner | 0b1c723 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 118 | CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P; | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 119 | if (!CallGraphUpToDate) { | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 120 | DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 121 | CallGraphUpToDate = true; | 
|  | 122 | } | 
| Chris Lattner | 339c82d | 2009-09-01 20:33:43 +0000 | [diff] [blame] | 123 |  | 
| Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 124 | { | 
|  | 125 | TimeRegion PassTimer(getPassTimer(CGSP)); | 
|  | 126 | Changed = CGSP->runOnSCC(CurSCC); | 
|  | 127 | } | 
| Chris Lattner | e33167a | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 128 |  | 
|  | 129 | // After the CGSCCPass is done, when assertions are enabled, use | 
|  | 130 | // RefreshCallGraph to verify that the callgraph was correctly updated. | 
|  | 131 | #ifndef NDEBUG | 
|  | 132 | if (Changed) | 
|  | 133 | RefreshCallGraph(CurSCC, CG, true); | 
|  | 134 | #endif | 
|  | 135 |  | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 136 | return Changed; | 
|  | 137 | } | 
|  | 138 |  | 
| Chris Lattner | 0b1c723 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 139 |  | 
|  | 140 | assert(PM->getPassManagerType() == PMT_FunctionPassManager && | 
|  | 141 | "Invalid CGPassManager member"); | 
|  | 142 | FPPassManager *FPP = (FPPassManager*)P; | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 143 |  | 
|  | 144 | // Run pass P on all functions in the current SCC. | 
| Sanjay Patel | c601254 | 2015-03-10 03:26:39 +0000 | [diff] [blame] | 145 | for (CallGraphNode *CGN : CurSCC) { | 
|  | 146 | if (Function *F = CGN->getFunction()) { | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 147 | dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName()); | 
| Juergen Ributzka | 34390c7 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 148 | { | 
|  | 149 | TimeRegion PassTimer(getPassTimer(FPP)); | 
|  | 150 | Changed |= FPP->runOnFunction(*F); | 
|  | 151 | } | 
|  | 152 | F->getContext().yield(); | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 153 | } | 
|  | 154 | } | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 155 |  | 
| Chris Lattner | a9262bb | 2009-08-31 17:08:30 +0000 | [diff] [blame] | 156 | // The function pass(es) modified the IR, they may have clobbered the | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 157 | // callgraph. | 
|  | 158 | if (Changed && CallGraphUpToDate) { | 
| David Greene | f8ed991 | 2009-12-23 20:10:59 +0000 | [diff] [blame] | 159 | DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 160 | << P->getPassName() << '\n'); | 
|  | 161 | CallGraphUpToDate = false; | 
|  | 162 | } | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 163 | return Changed; | 
|  | 164 | } | 
|  | 165 |  | 
| Chris Lattner | e33167a | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 166 |  | 
| Sanjay Patel | d45a3f1 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 167 | /// Scan the functions in the specified CFG and resync the | 
| Chris Lattner | e33167a | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 168 | /// callgraph with the call sites found in it.  This is used after | 
|  | 169 | /// FunctionPasses have potentially munged the callgraph, and can be used after | 
|  | 170 | /// CallGraphSCC passes to verify that they correctly updated the callgraph. | 
|  | 171 | /// | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 172 | /// This function returns true if it devirtualized an existing function call, | 
|  | 173 | /// meaning it turned an indirect call into a direct call.  This happens when | 
|  | 174 | /// a function pass like GVN optimizes away stuff feeding the indirect call. | 
|  | 175 | /// This never happens in checking mode. | 
|  | 176 | /// | 
|  | 177 | bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC, | 
| Chris Lattner | e33167a | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 178 | CallGraph &CG, bool CheckingMode) { | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 179 | DenseMap<Value*, CallGraphNode*> CallSites; | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 180 |  | 
| David Greene | f8ed991 | 2009-12-23 20:10:59 +0000 | [diff] [blame] | 181 | DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size() | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 182 | << " nodes:\n"; | 
| Sanjay Patel | c601254 | 2015-03-10 03:26:39 +0000 | [diff] [blame] | 183 | for (CallGraphNode *CGN : CurSCC) | 
|  | 184 | CGN->dump(); | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 185 | ); | 
|  | 186 |  | 
|  | 187 | bool MadeChange = false; | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 188 | bool DevirtualizedCall = false; | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 189 |  | 
|  | 190 | // Scan all functions in the SCC. | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 191 | unsigned FunctionNo = 0; | 
|  | 192 | for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end(); | 
|  | 193 | SCCIdx != E; ++SCCIdx, ++FunctionNo) { | 
|  | 194 | CallGraphNode *CGN = *SCCIdx; | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 195 | Function *F = CGN->getFunction(); | 
| Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 196 | if (!F || F->isDeclaration()) continue; | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 197 |  | 
|  | 198 | // Walk the function body looking for call sites.  Sync up the call sites in | 
|  | 199 | // CGN with those actually in the function. | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 200 |  | 
|  | 201 | // Keep track of the number of direct and indirect calls that were | 
|  | 202 | // invalidated and removed. | 
|  | 203 | unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0; | 
| Chris Lattner | 3f7b3d1 | 2009-09-01 18:13:40 +0000 | [diff] [blame] | 204 |  | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 205 | // Get the set of call sites currently in the function. | 
| Duncan Sands | 5632d96 | 2009-09-02 03:48:41 +0000 | [diff] [blame] | 206 | for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) { | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 207 | // If this call site is null, then the function pass deleted the call | 
| Chris Lattner | 3f7b3d1 | 2009-09-01 18:13:40 +0000 | [diff] [blame] | 208 | // entirely and the WeakVH nulled it out. | 
| Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 209 | if (!I->first || | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 210 | // If we've already seen this call site, then the FunctionPass RAUW'd | 
|  | 211 | // one call with another, which resulted in two "uses" in the edge | 
|  | 212 | // list of the same call. | 
|  | 213 | CallSites.count(I->first) || | 
|  | 214 |  | 
| Chad Rosier | 7a20ed7 | 2015-04-14 15:52:57 +0000 | [diff] [blame] | 215 | // If the call edge is not from a call or invoke, or it is a | 
|  | 216 | // instrinsic call, then the function pass RAUW'd a call with | 
|  | 217 | // another value. This can happen when constant folding happens | 
|  | 218 | // of well known functions etc. | 
|  | 219 | !CallSite(I->first) || | 
| Sanjoy Das | c65d43e | 2015-06-18 19:28:26 +0000 | [diff] [blame] | 220 | (CallSite(I->first).getCalledFunction() && | 
|  | 221 | CallSite(I->first).getCalledFunction()->isIntrinsic() && | 
|  | 222 | Intrinsic::isLeaf( | 
|  | 223 | CallSite(I->first).getCalledFunction()->getIntrinsicID()))) { | 
| Chris Lattner | e33167a | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 224 | assert(!CheckingMode && | 
|  | 225 | "CallGraphSCCPass did not update the CallGraph correctly!"); | 
|  | 226 |  | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 227 | // If this was an indirect call site, count it. | 
| Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 228 | if (!I->second->getFunction()) | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 229 | ++NumIndirectRemoved; | 
|  | 230 | else | 
|  | 231 | ++NumDirectRemoved; | 
|  | 232 |  | 
| Chris Lattner | 65fb597 | 2009-09-02 04:39:04 +0000 | [diff] [blame] | 233 | // Just remove the edge from the set of callees, keep track of whether | 
|  | 234 | // I points to the last element of the vector. | 
|  | 235 | bool WasLast = I + 1 == E; | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 236 | CGN->removeCallEdge(I); | 
| Chris Lattner | 8f23276 | 2009-09-02 04:34:06 +0000 | [diff] [blame] | 237 |  | 
| Chris Lattner | 65fb597 | 2009-09-02 04:39:04 +0000 | [diff] [blame] | 238 | // If I pointed to the last element of the vector, we have to bail out: | 
|  | 239 | // iterator checking rejects comparisons of the resultant pointer with | 
|  | 240 | // end. | 
|  | 241 | if (WasLast) | 
|  | 242 | break; | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 243 | E = CGN->end(); | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 244 | continue; | 
|  | 245 | } | 
| Chris Lattner | 3f7b3d1 | 2009-09-01 18:13:40 +0000 | [diff] [blame] | 246 |  | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 247 | assert(!CallSites.count(I->first) && | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 248 | "Call site occurs in node multiple times"); | 
| Sanjay Patel | 4c219fd | 2014-11-12 18:25:47 +0000 | [diff] [blame] | 249 |  | 
|  | 250 | CallSite CS(I->first); | 
|  | 251 | if (CS) { | 
|  | 252 | Function *Callee = CS.getCalledFunction(); | 
|  | 253 | // Ignore intrinsics because they're not really function calls. | 
|  | 254 | if (!Callee || !(Callee->isIntrinsic())) | 
|  | 255 | CallSites.insert(std::make_pair(I->first, I->second)); | 
|  | 256 | } | 
| Chris Lattner | 3f7b3d1 | 2009-09-01 18:13:40 +0000 | [diff] [blame] | 257 | ++I; | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 258 | } | 
| Chris Lattner | 3f7b3d1 | 2009-09-01 18:13:40 +0000 | [diff] [blame] | 259 |  | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 260 | // Loop over all of the instructions in the function, getting the callsites. | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 261 | // Keep track of the number of direct/indirect calls added. | 
|  | 262 | unsigned NumDirectAdded = 0, NumIndirectAdded = 0; | 
|  | 263 |  | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 264 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) | 
|  | 265 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { | 
| John McCall | 729c35b | 2011-06-09 19:46:27 +0000 | [diff] [blame] | 266 | CallSite CS(cast<Value>(I)); | 
| Nuno Lopes | 674acc1 | 2012-06-29 17:49:32 +0000 | [diff] [blame] | 267 | if (!CS) continue; | 
|  | 268 | Function *Callee = CS.getCalledFunction(); | 
|  | 269 | if (Callee && Callee->isIntrinsic()) continue; | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 270 |  | 
|  | 271 | // If this call site already existed in the callgraph, just verify it | 
|  | 272 | // matches up to expectations and remove it from CallSites. | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 273 | DenseMap<Value*, CallGraphNode*>::iterator ExistingIt = | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 274 | CallSites.find(CS.getInstruction()); | 
|  | 275 | if (ExistingIt != CallSites.end()) { | 
|  | 276 | CallGraphNode *ExistingNode = ExistingIt->second; | 
|  | 277 |  | 
|  | 278 | // Remove from CallSites since we have now seen it. | 
|  | 279 | CallSites.erase(ExistingIt); | 
|  | 280 |  | 
|  | 281 | // Verify that the callee is right. | 
|  | 282 | if (ExistingNode->getFunction() == CS.getCalledFunction()) | 
|  | 283 | continue; | 
|  | 284 |  | 
| Chris Lattner | e33167a | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 285 | // If we are in checking mode, we are not allowed to actually mutate | 
|  | 286 | // the callgraph.  If this is a case where we can infer that the | 
|  | 287 | // callgraph is less precise than it could be (e.g. an indirect call | 
|  | 288 | // site could be turned direct), don't reject it in checking mode, and | 
|  | 289 | // don't tweak it to be more precise. | 
|  | 290 | if (CheckingMode && CS.getCalledFunction() && | 
| Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 291 | ExistingNode->getFunction() == nullptr) | 
| Chris Lattner | e33167a | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 292 | continue; | 
|  | 293 |  | 
|  | 294 | assert(!CheckingMode && | 
|  | 295 | "CallGraphSCCPass did not update the CallGraph correctly!"); | 
|  | 296 |  | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 297 | // If not, we either went from a direct call to indirect, indirect to | 
|  | 298 | // direct, or direct to different direct. | 
|  | 299 | CallGraphNode *CalleeNode; | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 300 | if (Function *Callee = CS.getCalledFunction()) { | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 301 | CalleeNode = CG.getOrInsertFunction(Callee); | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 302 | // Keep track of whether we turned an indirect call into a direct | 
|  | 303 | // one. | 
| Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 304 | if (!ExistingNode->getFunction()) { | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 305 | DevirtualizedCall = true; | 
|  | 306 | DEBUG(dbgs() << "  CGSCCPASSMGR: Devirtualized call to '" | 
|  | 307 | << Callee->getName() << "'\n"); | 
|  | 308 | } | 
|  | 309 | } else { | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 310 | CalleeNode = CG.getCallsExternalNode(); | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 311 | } | 
| Chris Lattner | 339c82d | 2009-09-01 20:33:43 +0000 | [diff] [blame] | 312 |  | 
|  | 313 | // Update the edge target in CGN. | 
| Chris Lattner | 055cf26 | 2010-04-22 20:42:33 +0000 | [diff] [blame] | 314 | CGN->replaceCallEdge(CS, CS, CalleeNode); | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 315 | MadeChange = true; | 
|  | 316 | continue; | 
|  | 317 | } | 
|  | 318 |  | 
| Chris Lattner | e33167a | 2009-09-01 18:32:03 +0000 | [diff] [blame] | 319 | assert(!CheckingMode && | 
|  | 320 | "CallGraphSCCPass did not update the CallGraph correctly!"); | 
|  | 321 |  | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 322 | // If the call site didn't exist in the CGN yet, add it. | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 323 | CallGraphNode *CalleeNode; | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 324 | if (Function *Callee = CS.getCalledFunction()) { | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 325 | CalleeNode = CG.getOrInsertFunction(Callee); | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 326 | ++NumDirectAdded; | 
|  | 327 | } else { | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 328 | CalleeNode = CG.getCallsExternalNode(); | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 329 | ++NumIndirectAdded; | 
|  | 330 | } | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 331 |  | 
|  | 332 | CGN->addCalledFunction(CS, CalleeNode); | 
|  | 333 | MadeChange = true; | 
|  | 334 | } | 
|  | 335 |  | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 336 | // We scanned the old callgraph node, removing invalidated call sites and | 
|  | 337 | // then added back newly found call sites.  One thing that can happen is | 
|  | 338 | // that an old indirect call site was deleted and replaced with a new direct | 
|  | 339 | // call.  In this case, we have devirtualized a call, and CGSCCPM would like | 
|  | 340 | // to iteratively optimize the new code.  Unfortunately, we don't really | 
|  | 341 | // have a great way to detect when this happens.  As an approximation, we | 
|  | 342 | // just look at whether the number of indirect calls is reduced and the | 
|  | 343 | // number of direct calls is increased.  There are tons of ways to fool this | 
|  | 344 | // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a | 
|  | 345 | // direct call) but this is close enough. | 
|  | 346 | if (NumIndirectRemoved > NumIndirectAdded && | 
|  | 347 | NumDirectRemoved < NumDirectAdded) | 
|  | 348 | DevirtualizedCall = true; | 
|  | 349 |  | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 350 | // After scanning this function, if we still have entries in callsites, then | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 351 | // they are dangling pointers.  WeakVH should save us for this, so abort if | 
|  | 352 | // this happens. | 
|  | 353 | assert(CallSites.empty() && "Dangling pointers found in call sites map"); | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 354 |  | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 355 | // Periodically do an explicit clear to remove tombstones when processing | 
|  | 356 | // large scc's. | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 357 | if ((FunctionNo & 15) == 15) | 
| Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 358 | CallSites.clear(); | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
|  | 361 | DEBUG(if (MadeChange) { | 
| David Greene | f8ed991 | 2009-12-23 20:10:59 +0000 | [diff] [blame] | 362 | dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n"; | 
| Sanjay Patel | c601254 | 2015-03-10 03:26:39 +0000 | [diff] [blame] | 363 | for (CallGraphNode *CGN : CurSCC) | 
|  | 364 | CGN->dump(); | 
| Chris Lattner | 532112b | 2010-05-01 06:38:43 +0000 | [diff] [blame] | 365 | if (DevirtualizedCall) | 
|  | 366 | dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n"; | 
|  | 367 |  | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 368 | } else { | 
| David Greene | f8ed991 | 2009-12-23 20:10:59 +0000 | [diff] [blame] | 369 | dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n"; | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 370 | } | 
|  | 371 | ); | 
| Duncan Sands | a41634e | 2011-08-12 14:54:45 +0000 | [diff] [blame] | 372 | (void)MadeChange; | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 373 |  | 
|  | 374 | return DevirtualizedCall; | 
|  | 375 | } | 
|  | 376 |  | 
| Sanjay Patel | d45a3f1 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 377 | /// Execute the body of the entire pass manager on the specified SCC. | 
|  | 378 | /// This keeps track of whether a function pass devirtualizes | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 379 | /// any calls and returns it in DevirtualizedCall. | 
|  | 380 | bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, | 
|  | 381 | bool &DevirtualizedCall) { | 
|  | 382 | bool Changed = false; | 
|  | 383 |  | 
| Sanjay Patel | d45a3f1 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 384 | // Keep track of whether the callgraph is known to be up-to-date or not. | 
|  | 385 | // The CGSSC pass manager runs two types of passes: | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 386 | // CallGraphSCC Passes and other random function passes.  Because other | 
|  | 387 | // random function passes are not CallGraph aware, they may clobber the | 
|  | 388 | // call graph by introducing new calls or deleting other ones.  This flag | 
|  | 389 | // is set to false when we run a function pass so that we know to clean up | 
|  | 390 | // the callgraph when we need to run a CGSCCPass again. | 
|  | 391 | bool CallGraphUpToDate = true; | 
|  | 392 |  | 
|  | 393 | // Run all passes on current SCC. | 
|  | 394 | for (unsigned PassNo = 0, e = getNumContainedPasses(); | 
|  | 395 | PassNo != e; ++PassNo) { | 
|  | 396 | Pass *P = getContainedPass(PassNo); | 
|  | 397 |  | 
|  | 398 | // If we're in -debug-pass=Executions mode, construct the SCC node list, | 
|  | 399 | // otherwise avoid constructing this string as it is expensive. | 
|  | 400 | if (isPassDebuggingExecutionsOrMore()) { | 
|  | 401 | std::string Functions; | 
|  | 402 | #ifndef NDEBUG | 
|  | 403 | raw_string_ostream OS(Functions); | 
|  | 404 | for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end(); | 
|  | 405 | I != E; ++I) { | 
|  | 406 | if (I != CurSCC.begin()) OS << ", "; | 
|  | 407 | (*I)->print(OS); | 
|  | 408 | } | 
|  | 409 | OS.flush(); | 
|  | 410 | #endif | 
|  | 411 | dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions); | 
|  | 412 | } | 
|  | 413 | dumpRequiredSet(P); | 
|  | 414 |  | 
|  | 415 | initializeAnalysisImpl(P); | 
|  | 416 |  | 
|  | 417 | // Actually run this pass on the current SCC. | 
|  | 418 | Changed |= RunPassOnSCC(P, CurSCC, CG, | 
|  | 419 | CallGraphUpToDate, DevirtualizedCall); | 
|  | 420 |  | 
|  | 421 | if (Changed) | 
|  | 422 | dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, ""); | 
|  | 423 | dumpPreservedSet(P); | 
|  | 424 |  | 
|  | 425 | verifyPreservedAnalysis(P); | 
|  | 426 | removeNotPreservedAnalysis(P); | 
|  | 427 | recordAvailableAnalysis(P); | 
|  | 428 | removeDeadPasses(P, "", ON_CG_MSG); | 
|  | 429 | } | 
|  | 430 |  | 
|  | 431 | // If the callgraph was left out of date (because the last pass run was a | 
|  | 432 | // functionpass), refresh it before we move on to the next SCC. | 
|  | 433 | if (!CallGraphUpToDate) | 
|  | 434 | DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); | 
|  | 435 | return Changed; | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 436 | } | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 437 |  | 
| Sanjay Patel | d45a3f1 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 438 | /// Execute all of the passes scheduled for execution.  Keep track of | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 439 | /// whether any of the passes modifies the module, and if so, return true. | 
|  | 440 | bool CGPassManager::runOnModule(Module &M) { | 
| Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 441 | CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); | 
| Bill Wendling | 5f14a01 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 442 | bool Changed = doInitialization(CG); | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 443 |  | 
| Chris Lattner | 7e4fbdd | 2009-08-31 06:01:21 +0000 | [diff] [blame] | 444 | // Walk the callgraph in bottom-up SCC order. | 
| Chris Lattner | 5518b81 | 2010-04-16 22:59:24 +0000 | [diff] [blame] | 445 | scc_iterator<CallGraph*> CGI = scc_begin(&CG); | 
|  | 446 |  | 
|  | 447 | CallGraphSCC CurSCC(&CGI); | 
|  | 448 | while (!CGI.isAtEnd()) { | 
| Chris Lattner | 305b115 | 2009-08-31 00:19:58 +0000 | [diff] [blame] | 449 | // Copy the current SCC and increment past it so that the pass can hack | 
|  | 450 | // on the SCC if it wants to without invalidating our iterator. | 
| Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 451 | const std::vector<CallGraphNode *> &NodeVec = *CGI; | 
|  | 452 | CurSCC.initialize(NodeVec.data(), NodeVec.data() + NodeVec.size()); | 
| Chris Lattner | 305b115 | 2009-08-31 00:19:58 +0000 | [diff] [blame] | 453 | ++CGI; | 
| Yaron Keren | afe3983 | 2015-07-02 14:17:12 +0000 | [diff] [blame] | 454 |  | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 455 | // At the top level, we run all the passes in this pass manager on the | 
|  | 456 | // functions in this SCC.  However, we support iterative compilation in the | 
|  | 457 | // case where a function pass devirtualizes a call to a function.  For | 
|  | 458 | // example, it is very common for a function pass (often GVN or instcombine) | 
|  | 459 | // to eliminate the addressing that feeds into a call.  With that improved | 
|  | 460 | // information, we would like the call to be an inline candidate, infer | 
|  | 461 | // mod-ref information etc. | 
|  | 462 | // | 
|  | 463 | // Because of this, we allow iteration up to a specified iteration count. | 
|  | 464 | // This only happens in the case of a devirtualized call, so we only burn | 
|  | 465 | // compile time in the case that we're making progress.  We also have a hard | 
|  | 466 | // iteration count limit in case there is crazy code. | 
|  | 467 | unsigned Iteration = 0; | 
|  | 468 | bool DevirtualizedCall = false; | 
|  | 469 | do { | 
| Chris Lattner | 055cf26 | 2010-04-22 20:42:33 +0000 | [diff] [blame] | 470 | DEBUG(if (Iteration) | 
|  | 471 | dbgs() << "  SCCPASSMGR: Re-visiting SCC, iteration #" | 
|  | 472 | << Iteration << '\n'); | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 473 | DevirtualizedCall = false; | 
|  | 474 | Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall); | 
|  | 475 | } while (Iteration++ < MaxIterations && DevirtualizedCall); | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 476 |  | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 477 | if (DevirtualizedCall) | 
|  | 478 | DEBUG(dbgs() << "  CGSCCPASSMGR: Stopped iteration after " << Iteration | 
|  | 479 | << " times, due to -max-cg-scc-iterations\n"); | 
| Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 480 |  | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 481 | if (Iteration > MaxSCCIterations) | 
|  | 482 | MaxSCCIterations = Iteration; | 
|  | 483 |  | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 484 | } | 
| Bill Wendling | 5f14a01 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 485 | Changed |= doFinalization(CG); | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 486 | return Changed; | 
|  | 487 | } | 
|  | 488 |  | 
| Chris Lattner | 6fbe704 | 2010-04-21 00:47:40 +0000 | [diff] [blame] | 489 |  | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 490 | /// Initialize CG | 
| Bill Wendling | 5f14a01 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 491 | bool CGPassManager::doInitialization(CallGraph &CG) { | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 492 | bool Changed = false; | 
| Chris Lattner | 0b1c723 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 493 | for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { | 
|  | 494 | if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { | 
|  | 495 | assert(PM->getPassManagerType() == PMT_FunctionPassManager && | 
|  | 496 | "Invalid CGPassManager member"); | 
|  | 497 | Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule()); | 
| Nick Lewycky | cdccffe | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 498 | } else { | 
| Chris Lattner | 0b1c723 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 499 | Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG); | 
| Nick Lewycky | cdccffe | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 500 | } | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 501 | } | 
|  | 502 | return Changed; | 
|  | 503 | } | 
|  | 504 |  | 
|  | 505 | /// Finalize CG | 
| Bill Wendling | 5f14a01 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 506 | bool CGPassManager::doFinalization(CallGraph &CG) { | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 507 | bool Changed = false; | 
| Chris Lattner | 0b1c723 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 508 | for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { | 
|  | 509 | if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { | 
|  | 510 | assert(PM->getPassManagerType() == PMT_FunctionPassManager && | 
|  | 511 | "Invalid CGPassManager member"); | 
|  | 512 | Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule()); | 
| Nick Lewycky | cdccffe | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 513 | } else { | 
| Chris Lattner | 0b1c723 | 2010-01-22 05:46:59 +0000 | [diff] [blame] | 514 | Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG); | 
| Nick Lewycky | cdccffe | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 515 | } | 
| Devang Patel | 48537a0 | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 516 | } | 
|  | 517 | return Changed; | 
|  | 518 | } | 
|  | 519 |  | 
| Chris Lattner | 6d1208f | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 520 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 521 | // CallGraphSCC Implementation | 
|  | 522 | //===----------------------------------------------------------------------===// | 
|  | 523 |  | 
| Sanjay Patel | d45a3f1 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 524 | /// This informs the SCC and the pass manager that the specified | 
| Chris Lattner | 5518b81 | 2010-04-16 22:59:24 +0000 | [diff] [blame] | 525 | /// Old node has been deleted, and New is to be used in its place. | 
|  | 526 | void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) { | 
|  | 527 | assert(Old != New && "Should not replace node with self"); | 
|  | 528 | for (unsigned i = 0; ; ++i) { | 
|  | 529 | assert(i != Nodes.size() && "Node not in SCC"); | 
|  | 530 | if (Nodes[i] != Old) continue; | 
|  | 531 | Nodes[i] = New; | 
|  | 532 | break; | 
|  | 533 | } | 
| Chris Lattner | de023a3 | 2010-04-16 23:04:30 +0000 | [diff] [blame] | 534 |  | 
|  | 535 | // Update the active scc_iterator so that it doesn't contain dangling | 
|  | 536 | // pointers to the old CallGraphNode. | 
|  | 537 | scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context; | 
|  | 538 | CGI->ReplaceNode(Old, New); | 
| Chris Lattner | 5518b81 | 2010-04-16 22:59:24 +0000 | [diff] [blame] | 539 | } | 
| Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 540 |  | 
|  | 541 |  | 
|  | 542 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 6d1208f | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 543 | // CallGraphSCCPass Implementation | 
|  | 544 | //===----------------------------------------------------------------------===// | 
| David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 545 |  | 
| Devang Patel | 020f4f2 | 2007-01-23 21:55:17 +0000 | [diff] [blame] | 546 | /// Assign pass manager to manage this pass. | 
| Devang Patel | 1f8200b | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 547 | void CallGraphSCCPass::assignPassManager(PMStack &PMS, | 
| Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 548 | PassManagerType PreferredType) { | 
| Devang Patel | 1f8200b | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 549 | // Find CGPassManager | 
| Duncan Sands | 60f28bf | 2007-07-19 09:42:01 +0000 | [diff] [blame] | 550 | while (!PMS.empty() && | 
|  | 551 | PMS.top()->getPassManagerType() > PMT_CallGraphPassManager) | 
|  | 552 | PMS.pop(); | 
| Devang Patel | 1f8200b | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 553 |  | 
| Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 554 | assert(!PMS.empty() && "Unable to handle Call Graph Pass"); | 
|  | 555 | CGPassManager *CGP; | 
|  | 556 |  | 
|  | 557 | if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager) | 
|  | 558 | CGP = (CGPassManager*)PMS.top(); | 
|  | 559 | else { | 
|  | 560 | // Create new Call Graph SCC Pass Manager if it does not exist. | 
|  | 561 | assert(!PMS.empty() && "Unable to create Call Graph Pass Manager"); | 
| Devang Patel | 1f8200b | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 562 | PMDataManager *PMD = PMS.top(); | 
|  | 563 |  | 
|  | 564 | // [1] Create new Call Graph Pass Manager | 
| Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 565 | CGP = new CGPassManager(); | 
| Devang Patel | 1f8200b | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 566 |  | 
|  | 567 | // [2] Set up new manager's top level manager | 
|  | 568 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); | 
|  | 569 | TPM->addIndirectPassManager(CGP); | 
|  | 570 |  | 
|  | 571 | // [3] Assign manager to manage this new manager. This may create | 
|  | 572 | // and push new managers into PMS | 
| Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 573 | Pass *P = CGP; | 
| Devang Patel | 703de8f | 2007-06-21 22:29:02 +0000 | [diff] [blame] | 574 | TPM->schedulePass(P); | 
| Devang Patel | 1f8200b | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 575 |  | 
|  | 576 | // [4] Push new manager into PMS | 
|  | 577 | PMS.push(CGP); | 
|  | 578 | } | 
|  | 579 |  | 
|  | 580 | CGP->add(this); | 
|  | 581 | } | 
|  | 582 |  | 
| Sanjay Patel | d45a3f1 | 2015-03-10 03:48:14 +0000 | [diff] [blame] | 583 | /// For this class, we declare that we require and preserve the call graph. | 
|  | 584 | /// If the derived class implements this method, it should | 
| Chris Lattner | f7e9594 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 585 | /// always explicitly call the implementation here. | 
|  | 586 | void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const { | 
| Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 587 | AU.addRequired<CallGraphWrapperPass>(); | 
|  | 588 | AU.addPreserved<CallGraphWrapperPass>(); | 
| Chris Lattner | f7e9594 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 589 | } | 
| Chris Lattner | 6d1208f | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 590 |  | 
|  | 591 |  | 
|  | 592 | //===----------------------------------------------------------------------===// | 
|  | 593 | // PrintCallGraphPass Implementation | 
|  | 594 | //===----------------------------------------------------------------------===// | 
|  | 595 |  | 
|  | 596 | namespace { | 
|  | 597 | /// PrintCallGraphPass - Print a Module corresponding to a call graph. | 
|  | 598 | /// | 
|  | 599 | class PrintCallGraphPass : public CallGraphSCCPass { | 
|  | 600 | std::string Banner; | 
|  | 601 | raw_ostream &Out;       // raw_ostream to print on. | 
|  | 602 |  | 
|  | 603 | public: | 
|  | 604 | static char ID; | 
| Chris Lattner | 6d1208f | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 605 | PrintCallGraphPass(const std::string &B, raw_ostream &o) | 
| Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 606 | : CallGraphSCCPass(ID), Banner(B), Out(o) {} | 
| Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 607 |  | 
|  | 608 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
| Chris Lattner | 6d1208f | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 609 | AU.setPreservesAll(); | 
|  | 610 | } | 
| Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 611 |  | 
|  | 612 | bool runOnSCC(CallGraphSCC &SCC) override { | 
| Chris Lattner | 6d1208f | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 613 | Out << Banner; | 
| Sanjay Patel | c601254 | 2015-03-10 03:26:39 +0000 | [diff] [blame] | 614 | for (CallGraphNode *CGN : SCC) { | 
|  | 615 | if (CGN->getFunction()) | 
|  | 616 | CGN->getFunction()->print(Out); | 
| Richard Trieu | c148522 | 2014-06-21 02:43:02 +0000 | [diff] [blame] | 617 | else | 
| Richard Trieu | f2a7952 | 2014-07-03 02:11:49 +0000 | [diff] [blame] | 618 | Out << "\nPrinting <null> Function\n"; | 
| Richard Trieu | a23043c | 2014-06-09 22:53:16 +0000 | [diff] [blame] | 619 | } | 
| Chris Lattner | 6d1208f | 2010-04-16 21:43:55 +0000 | [diff] [blame] | 620 | return false; | 
|  | 621 | } | 
|  | 622 | }; | 
|  | 623 |  | 
|  | 624 | } // end anonymous namespace. | 
|  | 625 |  | 
|  | 626 | char PrintCallGraphPass::ID = 0; | 
|  | 627 |  | 
|  | 628 | Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O, | 
|  | 629 | const std::string &Banner) const { | 
|  | 630 | return new PrintCallGraphPass(Banner, O); | 
|  | 631 | } | 
|  | 632 |  |