blob: 0aed57a39387cc89b2ea218c1fd5515868b14737 [file] [log] [blame]
Chris Lattner4a810672003-08-31 01:54:59 +00001//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a810672003-08-31 01:54:59 +00009//
10// This file implements the CallGraphSCCPass class, which is used for passes
11// which are implemented as bottom-up traversals on the call graph. Because
12// there may be cycles in the call graph, passes of this type operate on the
13// call-graph in SCC order: that is, they process function bottom-up, except for
14// recursive functions, which they process all at once.
15//
16//===----------------------------------------------------------------------===//
17
Chandler Carruth3251e812013-01-07 15:26:48 +000018#include "llvm/Analysis/CallGraphSCCPass.h"
Eugene Zelenko046ca042017-08-31 21:56:16 +000019#include "llvm/ADT/DenseMap.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/SCCIterator.h"
Chris Lattner08e322d2010-04-21 00:47:40 +000021#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/Analysis/CallGraph.h"
Eugene Zelenko046ca042017-08-31 21:56:16 +000023#include "llvm/IR/CallSite.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Function.h"
Fedor Sergeeva7c23702018-09-24 16:08:15 +000025#include "llvm/IR/IRPrintingPasses.h"
Eugene Zelenko046ca042017-08-31 21:56:16 +000026#include "llvm/IR/Intrinsics.h"
Juergen Ributzka9bc1b732014-05-16 02:33:15 +000027#include "llvm/IR/LLVMContext.h"
Chandler Carruth1b279142015-01-14 11:23:27 +000028#include "llvm/IR/LegacyPassManagers.h"
Eugene Zelenko046ca042017-08-31 21:56:16 +000029#include "llvm/IR/Module.h"
Andrew Kaylor1e455c52016-04-22 22:06:11 +000030#include "llvm/IR/OptBisect.h"
Fedor Sergeev9f6be222018-08-28 21:06:51 +000031#include "llvm/IR/PassTimingInfo.h"
Eugene Zelenko046ca042017-08-31 21:56:16 +000032#include "llvm/Pass.h"
Chris Lattner08e322d2010-04-21 00:47:40 +000033#include "llvm/Support/CommandLine.h"
Chris Lattnerbe577652009-08-31 07:23:46 +000034#include "llvm/Support/Debug.h"
Chris Lattnera782e752010-03-30 04:03:22 +000035#include "llvm/Support/Timer.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000036#include "llvm/Support/raw_ostream.h"
Eugene Zelenko046ca042017-08-31 21:56:16 +000037#include <cassert>
38#include <string>
39#include <utility>
40#include <vector>
41
Chris Lattnera10df502004-04-20 21:30:06 +000042using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000043
Chandler Carruth4da25372014-04-22 02:48:03 +000044#define DEBUG_TYPE "cgscc-passmgr"
45
Fangrui Songaf7b1832018-07-30 19:41:25 +000046static cl::opt<unsigned>
Chris Lattner6da12e62010-05-01 01:15:56 +000047MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
Chris Lattner08e322d2010-04-21 00:47:40 +000048
49STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
50
Devang Patel75f9abf2007-01-17 21:45:01 +000051//===----------------------------------------------------------------------===//
52// CGPassManager
53//
Dale Johannesendb2659b2009-09-10 22:01:32 +000054/// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
Devang Patel75f9abf2007-01-17 21:45:01 +000055
Dan Gohman844731a2008-05-13 00:00:25 +000056namespace {
57
Devang Patel75f9abf2007-01-17 21:45:01 +000058class CGPassManager : public ModulePass, public PMDataManager {
Devang Patel75f9abf2007-01-17 21:45:01 +000059public:
Devang Patel19974732007-05-03 01:11:54 +000060 static char ID;
Eugene Zelenko046ca042017-08-31 21:56:16 +000061
62 explicit CGPassManager() : ModulePass(ID), PMDataManager() {}
Devang Patel75f9abf2007-01-17 21:45:01 +000063
Sanjay Patelec53c652015-03-10 03:48:14 +000064 /// Execute all of the passes scheduled for execution. Keep track of
Devang Patel75f9abf2007-01-17 21:45:01 +000065 /// whether any of the passes modifies the module, and if so, return true.
Craig Topperc37e6c02014-03-05 07:30:04 +000066 bool runOnModule(Module &M) override;
Devang Patel75f9abf2007-01-17 21:45:01 +000067
Owen Anderson40b6fdb2012-11-15 00:14:15 +000068 using ModulePass::doInitialization;
69 using ModulePass::doFinalization;
70
Bill Wendling905c7e92009-02-11 18:19:24 +000071 bool doInitialization(CallGraph &CG);
72 bool doFinalization(CallGraph &CG);
Devang Patel75f9abf2007-01-17 21:45:01 +000073
74 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperc37e6c02014-03-05 07:30:04 +000075 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel75f9abf2007-01-17 21:45:01 +000076 // CGPassManager walks SCC and it needs CallGraph.
Chandler Carruth54fec072013-11-26 04:19:30 +000077 Info.addRequired<CallGraphWrapperPass>();
Devang Patel75f9abf2007-01-17 21:45:01 +000078 Info.setPreservesAll();
79 }
80
Mehdi Amini67f335d2016-10-01 02:56:57 +000081 StringRef getPassName() const override { return "CallGraph Pass Manager"; }
Devang Patel505f36a2007-02-01 22:09:37 +000082
Craig Topperc37e6c02014-03-05 07:30:04 +000083 PMDataManager *getAsPMDataManager() override { return this; }
84 Pass *getAsPass() override { return this; }
Chris Lattner3660eca2010-01-22 05:24:46 +000085
Devang Patel75f9abf2007-01-17 21:45:01 +000086 // Print passes managed by this manager
Craig Topperc37e6c02014-03-05 07:30:04 +000087 void dumpPassStructure(unsigned Offset) override {
David Greene2e48e532009-12-23 23:09:39 +000088 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
Devang Patel75f9abf2007-01-17 21:45:01 +000089 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
90 Pass *P = getContainedPass(Index);
Dan Gohman8a757ae2010-08-19 01:29:07 +000091 P->dumpPassStructure(Offset + 1);
Devang Patel75f9abf2007-01-17 21:45:01 +000092 dumpLastUses(P, Offset+1);
93 }
94 }
95
96 Pass *getContainedPass(unsigned N) {
Chris Lattner45cfe542009-08-23 06:03:38 +000097 assert(N < PassVector.size() && "Pass number out of range!");
98 return static_cast<Pass *>(PassVector[N]);
Devang Patel75f9abf2007-01-17 21:45:01 +000099 }
100
Craig Topperc37e6c02014-03-05 07:30:04 +0000101 PassManagerType getPassManagerType() const override {
Fangrui Songaf7b1832018-07-30 19:41:25 +0000102 return PMT_CallGraphPassManager;
Devang Patel75f9abf2007-01-17 21:45:01 +0000103 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000104
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000105private:
Chris Lattner08e322d2010-04-21 00:47:40 +0000106 bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
107 bool &DevirtualizedCall);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000108
Chris Lattner2decb222010-04-16 22:42:17 +0000109 bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
Chris Lattner08e322d2010-04-21 00:47:40 +0000110 CallGraph &CG, bool &CallGraphUpToDate,
111 bool &DevirtualizedCall);
Mehdi Aminia27deb32016-08-08 18:51:05 +0000112 bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
Chris Lattner5a6a3632009-09-01 18:32:03 +0000113 bool IsCheckingMode);
Devang Patel75f9abf2007-01-17 21:45:01 +0000114};
115
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000116} // end anonymous namespace.
Dan Gohman844731a2008-05-13 00:00:25 +0000117
Devang Patel19974732007-05-03 01:11:54 +0000118char CGPassManager::ID = 0;
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000119
Chris Lattner2decb222010-04-16 22:42:17 +0000120bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
Chris Lattner08e322d2010-04-21 00:47:40 +0000121 CallGraph &CG, bool &CallGraphUpToDate,
122 bool &DevirtualizedCall) {
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000123 bool Changed = false;
Chris Lattner5a66cb92010-01-22 05:46:59 +0000124 PMDataManager *PM = P->getAsPMDataManager();
Jessica Paquette849da552018-05-18 17:26:39 +0000125 Module &M = CG.getModule();
Chris Lattner5a66cb92010-01-22 05:46:59 +0000126
Craig Toppere703fcb2014-04-24 06:44:33 +0000127 if (!PM) {
Jessica Paquette4f3a2192018-08-31 20:20:57 +0000128 CallGraphSCCPass *CGSP = (CallGraphSCCPass *)P;
Chris Lattnerbe577652009-08-31 07:23:46 +0000129 if (!CallGraphUpToDate) {
Chris Lattner08e322d2010-04-21 00:47:40 +0000130 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
Chris Lattnerbe577652009-08-31 07:23:46 +0000131 CallGraphUpToDate = true;
132 }
Chris Lattner44a18372009-09-01 20:33:43 +0000133
Chris Lattnera782e752010-03-30 04:03:22 +0000134 {
Jessica Paquette14667072018-08-31 20:20:56 +0000135 unsigned InstrCount, SCCCount = 0;
Jessica Paquette9e212802018-09-06 21:19:54 +0000136 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
Xin Tonga747d612018-07-22 05:27:41 +0000137 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Chris Lattnera782e752010-03-30 04:03:22 +0000138 TimeRegion PassTimer(getPassTimer(CGSP));
Xin Tonga747d612018-07-22 05:27:41 +0000139 if (EmitICRemark)
Jessica Paquette9e212802018-09-06 21:19:54 +0000140 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
Chris Lattnera782e752010-03-30 04:03:22 +0000141 Changed = CGSP->runOnSCC(CurSCC);
Jessica Paquette849da552018-05-18 17:26:39 +0000142
Jessica Paquette14667072018-08-31 20:20:56 +0000143 if (EmitICRemark) {
144 // FIXME: Add getInstructionCount to CallGraphSCC.
Jessica Paquette14667072018-08-31 20:20:56 +0000145 SCCCount = M.getInstructionCount();
146 // Is there a difference in the number of instructions in the module?
147 if (SCCCount != InstrCount) {
148 // Yep. Emit a remark and update InstrCount.
Jessica Paquette4f3a2192018-08-31 20:20:57 +0000149 int64_t Delta =
150 static_cast<int64_t>(SCCCount) - static_cast<int64_t>(InstrCount);
Jessica Paquette9e212802018-09-06 21:19:54 +0000151 emitInstrCountChangedRemark(P, M, Delta, InstrCount,
152 FunctionToInstrCount);
Jessica Paquette14667072018-08-31 20:20:56 +0000153 InstrCount = SCCCount;
154 }
155 }
Chris Lattnera782e752010-03-30 04:03:22 +0000156 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000157
Chris Lattner5a6a3632009-09-01 18:32:03 +0000158 // After the CGSCCPass is done, when assertions are enabled, use
159 // RefreshCallGraph to verify that the callgraph was correctly updated.
160#ifndef NDEBUG
161 if (Changed)
162 RefreshCallGraph(CurSCC, CG, true);
163#endif
Fangrui Songaf7b1832018-07-30 19:41:25 +0000164
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000165 return Changed;
166 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000167
Chris Lattner5a66cb92010-01-22 05:46:59 +0000168 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
169 "Invalid CGPassManager member");
170 FPPassManager *FPP = (FPPassManager*)P;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000171
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000172 // Run pass P on all functions in the current SCC.
Sanjay Patel9e894a92015-03-10 03:26:39 +0000173 for (CallGraphNode *CGN : CurSCC) {
174 if (Function *F = CGN->getFunction()) {
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000175 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
Juergen Ributzka9bc1b732014-05-16 02:33:15 +0000176 {
177 TimeRegion PassTimer(getPassTimer(FPP));
178 Changed |= FPP->runOnFunction(*F);
179 }
180 F->getContext().yield();
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000181 }
182 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000183
Chris Lattner2038cf32009-08-31 17:08:30 +0000184 // The function pass(es) modified the IR, they may have clobbered the
Chris Lattnerbe577652009-08-31 07:23:46 +0000185 // callgraph.
186 if (Changed && CallGraphUpToDate) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000187 LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " << P->getPassName()
188 << '\n');
Chris Lattnerbe577652009-08-31 07:23:46 +0000189 CallGraphUpToDate = false;
190 }
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000191 return Changed;
192}
193
Sanjay Patelec53c652015-03-10 03:48:14 +0000194/// Scan the functions in the specified CFG and resync the
Chris Lattner5a6a3632009-09-01 18:32:03 +0000195/// callgraph with the call sites found in it. This is used after
196/// FunctionPasses have potentially munged the callgraph, and can be used after
197/// CallGraphSCC passes to verify that they correctly updated the callgraph.
198///
Chris Lattner08e322d2010-04-21 00:47:40 +0000199/// This function returns true if it devirtualized an existing function call,
200/// meaning it turned an indirect call into a direct call. This happens when
201/// a function pass like GVN optimizes away stuff feeding the indirect call.
202/// This never happens in checking mode.
Mehdi Aminia27deb32016-08-08 18:51:05 +0000203bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
204 bool CheckingMode) {
Chris Lattnera541b0f2009-09-01 06:31:31 +0000205 DenseMap<Value*, CallGraphNode*> CallSites;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000206
207 LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
208 << " nodes:\n";
209 for (CallGraphNode *CGN
210 : CurSCC) CGN->dump(););
Chris Lattnerbe577652009-08-31 07:23:46 +0000211
212 bool MadeChange = false;
Chris Lattner08e322d2010-04-21 00:47:40 +0000213 bool DevirtualizedCall = false;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000214
Chris Lattnerbe577652009-08-31 07:23:46 +0000215 // Scan all functions in the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000216 unsigned FunctionNo = 0;
217 for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
218 SCCIdx != E; ++SCCIdx, ++FunctionNo) {
219 CallGraphNode *CGN = *SCCIdx;
Chris Lattnerbe577652009-08-31 07:23:46 +0000220 Function *F = CGN->getFunction();
Craig Toppere703fcb2014-04-24 06:44:33 +0000221 if (!F || F->isDeclaration()) continue;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000222
Chris Lattnerbe577652009-08-31 07:23:46 +0000223 // Walk the function body looking for call sites. Sync up the call sites in
224 // CGN with those actually in the function.
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000225
226 // Keep track of the number of direct and indirect calls that were
227 // invalidated and removed.
228 unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000229
Chris Lattnerbe577652009-08-31 07:23:46 +0000230 // Get the set of call sites currently in the function.
Duncan Sandsb3020d72009-09-02 03:48:41 +0000231 for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
Chris Lattnera541b0f2009-09-01 06:31:31 +0000232 // If this call site is null, then the function pass deleted the call
Sanjoy Das399b4d02017-05-01 17:07:49 +0000233 // entirely and the WeakTrackingVH nulled it out.
Craig Toppere703fcb2014-04-24 06:44:33 +0000234 if (!I->first ||
Chris Lattnera541b0f2009-09-01 06:31:31 +0000235 // If we've already seen this call site, then the FunctionPass RAUW'd
236 // one call with another, which resulted in two "uses" in the edge
237 // list of the same call.
238 CallSites.count(I->first) ||
239
Chad Rosier2fbccf62015-04-14 15:52:57 +0000240 // If the call edge is not from a call or invoke, or it is a
Fangrui Songaf7b1832018-07-30 19:41:25 +0000241 // instrinsic call, then the function pass RAUW'd a call with
Chad Rosier2fbccf62015-04-14 15:52:57 +0000242 // another value. This can happen when constant folding happens
243 // of well known functions etc.
244 !CallSite(I->first) ||
Sanjoy Dasaabacb62015-06-18 19:28:26 +0000245 (CallSite(I->first).getCalledFunction() &&
246 CallSite(I->first).getCalledFunction()->isIntrinsic() &&
247 Intrinsic::isLeaf(
248 CallSite(I->first).getCalledFunction()->getIntrinsicID()))) {
Chris Lattner5a6a3632009-09-01 18:32:03 +0000249 assert(!CheckingMode &&
250 "CallGraphSCCPass did not update the CallGraph correctly!");
Fangrui Songaf7b1832018-07-30 19:41:25 +0000251
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000252 // If this was an indirect call site, count it.
Craig Toppere703fcb2014-04-24 06:44:33 +0000253 if (!I->second->getFunction())
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000254 ++NumIndirectRemoved;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000255 else
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000256 ++NumDirectRemoved;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000257
Chris Lattnerd3bd0822009-09-02 04:39:04 +0000258 // Just remove the edge from the set of callees, keep track of whether
259 // I points to the last element of the vector.
260 bool WasLast = I + 1 == E;
Chris Lattnera541b0f2009-09-01 06:31:31 +0000261 CGN->removeCallEdge(I);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000262
Chris Lattnerd3bd0822009-09-02 04:39:04 +0000263 // If I pointed to the last element of the vector, we have to bail out:
264 // iterator checking rejects comparisons of the resultant pointer with
265 // end.
266 if (WasLast)
267 break;
Chris Lattnera541b0f2009-09-01 06:31:31 +0000268 E = CGN->end();
Chris Lattnera541b0f2009-09-01 06:31:31 +0000269 continue;
270 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000271
Chris Lattnera541b0f2009-09-01 06:31:31 +0000272 assert(!CallSites.count(I->first) &&
Chris Lattnerbe577652009-08-31 07:23:46 +0000273 "Call site occurs in node multiple times");
Fangrui Songaf7b1832018-07-30 19:41:25 +0000274
Sanjay Patelbc8114f2014-11-12 18:25:47 +0000275 CallSite CS(I->first);
276 if (CS) {
277 Function *Callee = CS.getCalledFunction();
278 // Ignore intrinsics because they're not really function calls.
279 if (!Callee || !(Callee->isIntrinsic()))
280 CallSites.insert(std::make_pair(I->first, I->second));
281 }
Chris Lattner17146b82009-09-01 18:13:40 +0000282 ++I;
Chris Lattnerbe577652009-08-31 07:23:46 +0000283 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000284
Chris Lattnerbe577652009-08-31 07:23:46 +0000285 // Loop over all of the instructions in the function, getting the callsites.
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000286 // Keep track of the number of direct/indirect calls added.
287 unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
Benjamin Kramer8d0d2b62016-06-26 17:27:42 +0000288
289 for (BasicBlock &BB : *F)
290 for (Instruction &I : BB) {
291 CallSite CS(&I);
Nuno Lopes7d539712012-06-29 17:49:32 +0000292 if (!CS) continue;
293 Function *Callee = CS.getCalledFunction();
294 if (Callee && Callee->isIntrinsic()) continue;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000295
Chris Lattnerbe577652009-08-31 07:23:46 +0000296 // If this call site already existed in the callgraph, just verify it
297 // matches up to expectations and remove it from CallSites.
Chris Lattnera541b0f2009-09-01 06:31:31 +0000298 DenseMap<Value*, CallGraphNode*>::iterator ExistingIt =
Chris Lattnerbe577652009-08-31 07:23:46 +0000299 CallSites.find(CS.getInstruction());
300 if (ExistingIt != CallSites.end()) {
301 CallGraphNode *ExistingNode = ExistingIt->second;
302
303 // Remove from CallSites since we have now seen it.
304 CallSites.erase(ExistingIt);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000305
Chris Lattnerbe577652009-08-31 07:23:46 +0000306 // Verify that the callee is right.
307 if (ExistingNode->getFunction() == CS.getCalledFunction())
308 continue;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000309
Chris Lattner5a6a3632009-09-01 18:32:03 +0000310 // If we are in checking mode, we are not allowed to actually mutate
311 // the callgraph. If this is a case where we can infer that the
312 // callgraph is less precise than it could be (e.g. an indirect call
313 // site could be turned direct), don't reject it in checking mode, and
314 // don't tweak it to be more precise.
315 if (CheckingMode && CS.getCalledFunction() &&
Craig Toppere703fcb2014-04-24 06:44:33 +0000316 ExistingNode->getFunction() == nullptr)
Chris Lattner5a6a3632009-09-01 18:32:03 +0000317 continue;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000318
Chris Lattner5a6a3632009-09-01 18:32:03 +0000319 assert(!CheckingMode &&
320 "CallGraphSCCPass did not update the CallGraph correctly!");
Fangrui Songaf7b1832018-07-30 19:41:25 +0000321
Chris Lattnerbe577652009-08-31 07:23:46 +0000322 // If not, we either went from a direct call to indirect, indirect to
323 // direct, or direct to different direct.
324 CallGraphNode *CalleeNode;
Chris Lattner08e322d2010-04-21 00:47:40 +0000325 if (Function *Callee = CS.getCalledFunction()) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000326 CalleeNode = CG.getOrInsertFunction(Callee);
Chris Lattner08e322d2010-04-21 00:47:40 +0000327 // Keep track of whether we turned an indirect call into a direct
328 // one.
Craig Toppere703fcb2014-04-24 06:44:33 +0000329 if (!ExistingNode->getFunction()) {
Chris Lattner08e322d2010-04-21 00:47:40 +0000330 DevirtualizedCall = true;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000331 LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
332 << Callee->getName() << "'\n");
Chris Lattner08e322d2010-04-21 00:47:40 +0000333 }
334 } else {
Chris Lattnerbe577652009-08-31 07:23:46 +0000335 CalleeNode = CG.getCallsExternalNode();
Chris Lattner08e322d2010-04-21 00:47:40 +0000336 }
Chris Lattner44a18372009-09-01 20:33:43 +0000337
338 // Update the edge target in CGN.
Chris Lattnerb61789d2010-04-22 20:42:33 +0000339 CGN->replaceCallEdge(CS, CS, CalleeNode);
Chris Lattnerbe577652009-08-31 07:23:46 +0000340 MadeChange = true;
341 continue;
342 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000343
Chris Lattner5a6a3632009-09-01 18:32:03 +0000344 assert(!CheckingMode &&
345 "CallGraphSCCPass did not update the CallGraph correctly!");
346
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000347 // If the call site didn't exist in the CGN yet, add it.
Chris Lattnerbe577652009-08-31 07:23:46 +0000348 CallGraphNode *CalleeNode;
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000349 if (Function *Callee = CS.getCalledFunction()) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000350 CalleeNode = CG.getOrInsertFunction(Callee);
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000351 ++NumDirectAdded;
352 } else {
Chris Lattnerbe577652009-08-31 07:23:46 +0000353 CalleeNode = CG.getCallsExternalNode();
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000354 ++NumIndirectAdded;
355 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000356
Chris Lattnerbe577652009-08-31 07:23:46 +0000357 CGN->addCalledFunction(CS, CalleeNode);
358 MadeChange = true;
359 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000360
Chris Lattnerbccb41a2010-05-01 06:38:43 +0000361 // We scanned the old callgraph node, removing invalidated call sites and
362 // then added back newly found call sites. One thing that can happen is
363 // that an old indirect call site was deleted and replaced with a new direct
364 // call. In this case, we have devirtualized a call, and CGSCCPM would like
365 // to iteratively optimize the new code. Unfortunately, we don't really
366 // have a great way to detect when this happens. As an approximation, we
367 // just look at whether the number of indirect calls is reduced and the
368 // number of direct calls is increased. There are tons of ways to fool this
369 // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
370 // direct call) but this is close enough.
371 if (NumIndirectRemoved > NumIndirectAdded &&
372 NumDirectRemoved < NumDirectAdded)
373 DevirtualizedCall = true;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000374
Chris Lattnerbe577652009-08-31 07:23:46 +0000375 // After scanning this function, if we still have entries in callsites, then
Sanjoy Das399b4d02017-05-01 17:07:49 +0000376 // they are dangling pointers. WeakTrackingVH should save us for this, so
377 // abort if
Sanjoy Das263da122017-04-26 16:37:05 +0000378 // this happens.
Chris Lattnera541b0f2009-09-01 06:31:31 +0000379 assert(CallSites.empty() && "Dangling pointers found in call sites map");
Fangrui Songaf7b1832018-07-30 19:41:25 +0000380
Chris Lattnera541b0f2009-09-01 06:31:31 +0000381 // Periodically do an explicit clear to remove tombstones when processing
382 // large scc's.
Chris Lattner2decb222010-04-16 22:42:17 +0000383 if ((FunctionNo & 15) == 15)
Chris Lattnera541b0f2009-09-01 06:31:31 +0000384 CallSites.clear();
Chris Lattnerbe577652009-08-31 07:23:46 +0000385 }
386
Nicola Zaghen0818e782018-05-14 12:53:11 +0000387 LLVM_DEBUG(if (MadeChange) {
388 dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
389 for (CallGraphNode *CGN : CurSCC)
390 CGN->dump();
391 if (DevirtualizedCall)
392 dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
393 } else {
394 dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
395 });
Duncan Sands1f6a3292011-08-12 14:54:45 +0000396 (void)MadeChange;
Chris Lattner08e322d2010-04-21 00:47:40 +0000397
398 return DevirtualizedCall;
399}
400
Sanjay Patelec53c652015-03-10 03:48:14 +0000401/// Execute the body of the entire pass manager on the specified SCC.
402/// This keeps track of whether a function pass devirtualizes
Chris Lattner08e322d2010-04-21 00:47:40 +0000403/// any calls and returns it in DevirtualizedCall.
404bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
405 bool &DevirtualizedCall) {
406 bool Changed = false;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000407
Sanjay Patelec53c652015-03-10 03:48:14 +0000408 // Keep track of whether the callgraph is known to be up-to-date or not.
409 // The CGSSC pass manager runs two types of passes:
Chris Lattner08e322d2010-04-21 00:47:40 +0000410 // CallGraphSCC Passes and other random function passes. Because other
411 // random function passes are not CallGraph aware, they may clobber the
412 // call graph by introducing new calls or deleting other ones. This flag
413 // is set to false when we run a function pass so that we know to clean up
414 // the callgraph when we need to run a CGSCCPass again.
415 bool CallGraphUpToDate = true;
416
417 // Run all passes on current SCC.
418 for (unsigned PassNo = 0, e = getNumContainedPasses();
419 PassNo != e; ++PassNo) {
420 Pass *P = getContainedPass(PassNo);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000421
Chris Lattner08e322d2010-04-21 00:47:40 +0000422 // If we're in -debug-pass=Executions mode, construct the SCC node list,
423 // otherwise avoid constructing this string as it is expensive.
424 if (isPassDebuggingExecutionsOrMore()) {
425 std::string Functions;
426 #ifndef NDEBUG
427 raw_string_ostream OS(Functions);
428 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
429 I != E; ++I) {
430 if (I != CurSCC.begin()) OS << ", ";
431 (*I)->print(OS);
432 }
433 OS.flush();
434 #endif
435 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
436 }
437 dumpRequiredSet(P);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000438
Chris Lattner08e322d2010-04-21 00:47:40 +0000439 initializeAnalysisImpl(P);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000440
Chris Lattner08e322d2010-04-21 00:47:40 +0000441 // Actually run this pass on the current SCC.
442 Changed |= RunPassOnSCC(P, CurSCC, CG,
443 CallGraphUpToDate, DevirtualizedCall);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000444
Chris Lattner08e322d2010-04-21 00:47:40 +0000445 if (Changed)
446 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
447 dumpPreservedSet(P);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000448
449 verifyPreservedAnalysis(P);
Chris Lattner08e322d2010-04-21 00:47:40 +0000450 removeNotPreservedAnalysis(P);
451 recordAvailableAnalysis(P);
452 removeDeadPasses(P, "", ON_CG_MSG);
453 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000454
Chris Lattner08e322d2010-04-21 00:47:40 +0000455 // If the callgraph was left out of date (because the last pass run was a
456 // functionpass), refresh it before we move on to the next SCC.
457 if (!CallGraphUpToDate)
458 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
459 return Changed;
Chris Lattnerbe577652009-08-31 07:23:46 +0000460}
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000461
Sanjay Patelec53c652015-03-10 03:48:14 +0000462/// Execute all of the passes scheduled for execution. Keep track of
Devang Patel75f9abf2007-01-17 21:45:01 +0000463/// whether any of the passes modifies the module, and if so, return true.
464bool CGPassManager::runOnModule(Module &M) {
Chandler Carruth54fec072013-11-26 04:19:30 +0000465 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Bill Wendling905c7e92009-02-11 18:19:24 +0000466 bool Changed = doInitialization(CG);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000467
Evgeniy Stepanovb7219f92018-07-13 16:32:31 +0000468 // Walk the callgraph in bottom-up SCC order.
469 scc_iterator<CallGraph*> CGI = scc_begin(&CG);
Chris Lattnera3dfc642010-04-16 22:59:24 +0000470
Evgeniy Stepanovb7219f92018-07-13 16:32:31 +0000471 CallGraphSCC CurSCC(CG, &CGI);
472 while (!CGI.isAtEnd()) {
473 // Copy the current SCC and increment past it so that the pass can hack
474 // on the SCC if it wants to without invalidating our iterator.
475 const std::vector<CallGraphNode *> &NodeVec = *CGI;
476 CurSCC.initialize(NodeVec);
477 ++CGI;
Yaron Keren016970f2015-07-02 14:17:12 +0000478
Evgeniy Stepanovb7219f92018-07-13 16:32:31 +0000479 // At the top level, we run all the passes in this pass manager on the
480 // functions in this SCC. However, we support iterative compilation in the
481 // case where a function pass devirtualizes a call to a function. For
482 // example, it is very common for a function pass (often GVN or instcombine)
483 // to eliminate the addressing that feeds into a call. With that improved
484 // information, we would like the call to be an inline candidate, infer
485 // mod-ref information etc.
486 //
487 // Because of this, we allow iteration up to a specified iteration count.
488 // This only happens in the case of a devirtualized call, so we only burn
489 // compile time in the case that we're making progress. We also have a hard
490 // iteration count limit in case there is crazy code.
491 unsigned Iteration = 0;
492 bool DevirtualizedCall = false;
493 do {
494 LLVM_DEBUG(if (Iteration) dbgs()
495 << " SCCPASSMGR: Re-visiting SCC, iteration #" << Iteration
496 << '\n');
497 DevirtualizedCall = false;
498 Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
499 } while (Iteration++ < MaxIterations && DevirtualizedCall);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000500
Evgeniy Stepanovb7219f92018-07-13 16:32:31 +0000501 if (DevirtualizedCall)
502 LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after "
503 << Iteration
504 << " times, due to -max-cg-scc-iterations\n");
Craig Topperad0e6662017-05-18 00:51:39 +0000505
Evgeniy Stepanovb7219f92018-07-13 16:32:31 +0000506 MaxSCCIterations.updateMax(Iteration);
Devang Patel75f9abf2007-01-17 21:45:01 +0000507 }
Bill Wendling905c7e92009-02-11 18:19:24 +0000508 Changed |= doFinalization(CG);
Devang Patel75f9abf2007-01-17 21:45:01 +0000509 return Changed;
510}
511
512/// Initialize CG
Bill Wendling905c7e92009-02-11 18:19:24 +0000513bool CGPassManager::doInitialization(CallGraph &CG) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000514 bool Changed = false;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000515 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
Chris Lattner5a66cb92010-01-22 05:46:59 +0000516 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
517 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
518 "Invalid CGPassManager member");
519 Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
Nick Lewycky8968a072009-02-13 07:15:53 +0000520 } else {
Chris Lattner5a66cb92010-01-22 05:46:59 +0000521 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
Nick Lewycky8968a072009-02-13 07:15:53 +0000522 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000523 }
524 return Changed;
525}
526
527/// Finalize CG
Bill Wendling905c7e92009-02-11 18:19:24 +0000528bool CGPassManager::doFinalization(CallGraph &CG) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000529 bool Changed = false;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000530 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
Chris Lattner5a66cb92010-01-22 05:46:59 +0000531 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
532 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
533 "Invalid CGPassManager member");
534 Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
Nick Lewycky8968a072009-02-13 07:15:53 +0000535 } else {
Chris Lattner5a66cb92010-01-22 05:46:59 +0000536 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
Nick Lewycky8968a072009-02-13 07:15:53 +0000537 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000538 }
539 return Changed;
540}
541
Chris Lattnerc93760c2010-04-16 21:43:55 +0000542//===----------------------------------------------------------------------===//
Chris Lattner2decb222010-04-16 22:42:17 +0000543// CallGraphSCC Implementation
544//===----------------------------------------------------------------------===//
545
Sanjay Patelec53c652015-03-10 03:48:14 +0000546/// This informs the SCC and the pass manager that the specified
Chris Lattnera3dfc642010-04-16 22:59:24 +0000547/// Old node has been deleted, and New is to be used in its place.
548void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
549 assert(Old != New && "Should not replace node with self");
550 for (unsigned i = 0; ; ++i) {
551 assert(i != Nodes.size() && "Node not in SCC");
552 if (Nodes[i] != Old) continue;
553 Nodes[i] = New;
554 break;
555 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000556
Chris Lattnerbde0bb52010-04-16 23:04:30 +0000557 // Update the active scc_iterator so that it doesn't contain dangling
558 // pointers to the old CallGraphNode.
559 scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
560 CGI->ReplaceNode(Old, New);
Chris Lattnera3dfc642010-04-16 22:59:24 +0000561}
Chris Lattner2decb222010-04-16 22:42:17 +0000562
Chris Lattner2decb222010-04-16 22:42:17 +0000563//===----------------------------------------------------------------------===//
Chris Lattnerc93760c2010-04-16 21:43:55 +0000564// CallGraphSCCPass Implementation
565//===----------------------------------------------------------------------===//
David Greene5c8aa952010-04-02 23:17:14 +0000566
Devang Pateld9f10c32007-01-23 21:55:17 +0000567/// Assign pass manager to manage this pass.
Devang Patel97fd2432007-01-23 21:52:35 +0000568void CallGraphSCCPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000569 PassManagerType PreferredType) {
Fangrui Songaf7b1832018-07-30 19:41:25 +0000570 // Find CGPassManager
Duncan Sands20d824b2007-07-19 09:42:01 +0000571 while (!PMS.empty() &&
572 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
573 PMS.pop();
Devang Patel97fd2432007-01-23 21:52:35 +0000574
Chris Lattner77c95ed2010-01-22 05:37:10 +0000575 assert(!PMS.empty() && "Unable to handle Call Graph Pass");
576 CGPassManager *CGP;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000577
Chris Lattner77c95ed2010-01-22 05:37:10 +0000578 if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
579 CGP = (CGPassManager*)PMS.top();
580 else {
Fangrui Songaf7b1832018-07-30 19:41:25 +0000581 // Create new Call Graph SCC Pass Manager if it does not exist.
Chris Lattner77c95ed2010-01-22 05:37:10 +0000582 assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
Devang Patel97fd2432007-01-23 21:52:35 +0000583 PMDataManager *PMD = PMS.top();
584
585 // [1] Create new Call Graph Pass Manager
Andrew Trick0e122d12011-08-29 17:07:00 +0000586 CGP = new CGPassManager();
Devang Patel97fd2432007-01-23 21:52:35 +0000587
588 // [2] Set up new manager's top level manager
589 PMTopLevelManager *TPM = PMD->getTopLevelManager();
590 TPM->addIndirectPassManager(CGP);
591
592 // [3] Assign manager to manage this new manager. This may create
593 // and push new managers into PMS
Chris Lattner77c95ed2010-01-22 05:37:10 +0000594 Pass *P = CGP;
Devang Patel25e681a2007-06-21 22:29:02 +0000595 TPM->schedulePass(P);
Devang Patel97fd2432007-01-23 21:52:35 +0000596
597 // [4] Push new manager into PMS
598 PMS.push(CGP);
599 }
600
601 CGP->add(this);
602}
603
Sanjay Patelec53c652015-03-10 03:48:14 +0000604/// For this class, we declare that we require and preserve the call graph.
605/// If the derived class implements this method, it should
Chris Lattner4a810672003-08-31 01:54:59 +0000606/// always explicitly call the implementation here.
607void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruth54fec072013-11-26 04:19:30 +0000608 AU.addRequired<CallGraphWrapperPass>();
609 AU.addPreserved<CallGraphWrapperPass>();
Chris Lattner4a810672003-08-31 01:54:59 +0000610}
Chris Lattnerc93760c2010-04-16 21:43:55 +0000611
Chris Lattnerc93760c2010-04-16 21:43:55 +0000612//===----------------------------------------------------------------------===//
613// PrintCallGraphPass Implementation
614//===----------------------------------------------------------------------===//
615
616namespace {
Eugene Zelenko046ca042017-08-31 21:56:16 +0000617
Chris Lattnerc93760c2010-04-16 21:43:55 +0000618 /// PrintCallGraphPass - Print a Module corresponding to a call graph.
619 ///
620 class PrintCallGraphPass : public CallGraphSCCPass {
621 std::string Banner;
Eugene Zelenko046ca042017-08-31 21:56:16 +0000622 raw_ostream &OS; // raw_ostream to print on.
Fangrui Songaf7b1832018-07-30 19:41:25 +0000623
Chris Lattnerc93760c2010-04-16 21:43:55 +0000624 public:
625 static char ID;
Eugene Zelenko046ca042017-08-31 21:56:16 +0000626
627 PrintCallGraphPass(const std::string &B, raw_ostream &OS)
628 : CallGraphSCCPass(ID), Banner(B), OS(OS) {}
Craig Topperc37e6c02014-03-05 07:30:04 +0000629
630 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattnerc93760c2010-04-16 21:43:55 +0000631 AU.setPreservesAll();
632 }
Craig Topperc37e6c02014-03-05 07:30:04 +0000633
634 bool runOnSCC(CallGraphSCC &SCC) override {
Yaron Kereneb2b31d2017-06-12 02:18:50 +0000635 bool BannerPrinted = false;
Fedor Sergeev90f49122018-12-03 14:48:15 +0000636 auto PrintBannerOnce = [&]() {
Mehdi Amini01355e82017-01-18 21:37:11 +0000637 if (BannerPrinted)
638 return;
Eugene Zelenko046ca042017-08-31 21:56:16 +0000639 OS << Banner;
Mehdi Amini01355e82017-01-18 21:37:11 +0000640 BannerPrinted = true;
Fedor Sergeev90f49122018-12-03 14:48:15 +0000641 };
642
643 bool NeedModule = llvm::forcePrintModuleIR();
644 if (isFunctionInPrintList("*") && NeedModule) {
645 PrintBannerOnce();
646 OS << "\n";
647 SCC.getCallGraph().getModule().print(OS, nullptr);
648 return false;
649 }
650 bool FoundFunction = false;
Sanjay Patel9e894a92015-03-10 03:26:39 +0000651 for (CallGraphNode *CGN : SCC) {
Yaron Kereneb2b31d2017-06-12 02:18:50 +0000652 if (Function *F = CGN->getFunction()) {
653 if (!F->isDeclaration() && isFunctionInPrintList(F->getName())) {
Fedor Sergeev90f49122018-12-03 14:48:15 +0000654 FoundFunction = true;
655 if (!NeedModule) {
656 PrintBannerOnce();
657 F->print(OS);
658 }
Mehdi Amini01355e82017-01-18 21:37:11 +0000659 }
Eugene Zelenko046ca042017-08-31 21:56:16 +0000660 } else if (isFunctionInPrintList("*")) {
Mehdi Amini01355e82017-01-18 21:37:11 +0000661 PrintBannerOnce();
Eugene Zelenko046ca042017-08-31 21:56:16 +0000662 OS << "\nPrinting <null> Function\n";
Mehdi Amini01355e82017-01-18 21:37:11 +0000663 }
Richard Trieuf31ecd32014-06-09 22:53:16 +0000664 }
Fedor Sergeev90f49122018-12-03 14:48:15 +0000665 if (NeedModule && FoundFunction) {
666 PrintBannerOnce();
667 OS << "\n";
668 SCC.getCallGraph().getModule().print(OS, nullptr);
669 }
Chris Lattnerc93760c2010-04-16 21:43:55 +0000670 return false;
671 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000672
Yaron Kerenea563682017-03-10 07:09:20 +0000673 StringRef getPassName() const override { return "Print CallGraph IR"; }
Chris Lattnerc93760c2010-04-16 21:43:55 +0000674 };
Fangrui Songaf7b1832018-07-30 19:41:25 +0000675
Chris Lattnerc93760c2010-04-16 21:43:55 +0000676} // end anonymous namespace.
677
678char PrintCallGraphPass::ID = 0;
679
Eugene Zelenko046ca042017-08-31 21:56:16 +0000680Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &OS,
Chris Lattnerc93760c2010-04-16 21:43:55 +0000681 const std::string &Banner) const {
Eugene Zelenko046ca042017-08-31 21:56:16 +0000682 return new PrintCallGraphPass(Banner, OS);
Chris Lattnerc93760c2010-04-16 21:43:55 +0000683}
684
Andrew Kaylor1e455c52016-04-22 22:06:11 +0000685bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const {
686 return !SCC.getCallGraph().getModule()
687 .getContext()
Fedor Sergeev558f76f2018-03-27 16:57:20 +0000688 .getOptPassGate()
Andrew Kaylor1e455c52016-04-22 22:06:11 +0000689 .shouldRunPass(this, SCC);
690}
Mehdi Amini32b9ed82016-06-10 16:19:46 +0000691
692char DummyCGSCCPass::ID = 0;
Eugene Zelenko046ca042017-08-31 21:56:16 +0000693
Mehdi Amini32b9ed82016-06-10 16:19:46 +0000694INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false,
695 false)