blob: 196ef400bc4e6fa089a464d978de9b5051503eee [file] [log] [blame]
Chris Lattnerf7e95942003-08-31 01:54:59 +00001//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman01808ca2005-04-21 21:13:18 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattnerf7e95942003-08-31 01:54:59 +00008//
9// This file implements the CallGraphSCCPass class, which is used for passes
10// which are implemented as bottom-up traversals on the call graph. Because
11// there may be cycles in the call graph, passes of this type operate on the
12// call-graph in SCC order: that is, they process function bottom-up, except for
13// recursive functions, which they process all at once.
14//
15//===----------------------------------------------------------------------===//
16
Chandler Carruth839a98e2013-01-07 15:26:48 +000017#include "llvm/Analysis/CallGraphSCCPass.h"
Eugene Zelenkofa6434b2017-08-31 21:56:16 +000018#include "llvm/ADT/DenseMap.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/ADT/SCCIterator.h"
Chris Lattner6fbe7042010-04-21 00:47:40 +000020#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Analysis/CallGraph.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Function.h"
Fedor Sergeev662e5682018-09-24 16:08:15 +000023#include "llvm/IR/IRPrintingPasses.h"
Eugene Zelenkofa6434b2017-08-31 21:56:16 +000024#include "llvm/IR/Intrinsics.h"
Juergen Ributzka34390c72014-05-16 02:33:15 +000025#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000026#include "llvm/IR/LegacyPassManagers.h"
Eugene Zelenkofa6434b2017-08-31 21:56:16 +000027#include "llvm/IR/Module.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000028#include "llvm/IR/OptBisect.h"
Fedor Sergeev43083112018-08-28 21:06:51 +000029#include "llvm/IR/PassTimingInfo.h"
Eugene Zelenkofa6434b2017-08-31 21:56:16 +000030#include "llvm/Pass.h"
Chris Lattner6fbe7042010-04-21 00:47:40 +000031#include "llvm/Support/CommandLine.h"
Chris Lattnereedcd842009-08-31 07:23:46 +000032#include "llvm/Support/Debug.h"
Chris Lattner707431c2010-03-30 04:03:22 +000033#include "llvm/Support/Timer.h"
Chris Lattner13626022009-08-23 06:03:38 +000034#include "llvm/Support/raw_ostream.h"
Eugene Zelenkofa6434b2017-08-31 21:56:16 +000035#include <cassert>
36#include <string>
37#include <utility>
38#include <vector>
39
Chris Lattner8d083812004-04-20 21:30:06 +000040using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000041
Chandler Carruthf1221bd2014-04-22 02:48:03 +000042#define DEBUG_TYPE "cgscc-passmgr"
43
Fangrui Songf78650a2018-07-30 19:41:25 +000044static cl::opt<unsigned>
Chris Lattnerfc8d9ee2010-05-01 01:15:56 +000045MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
Chris Lattner6fbe7042010-04-21 00:47:40 +000046
47STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
48
Devang Patel48537a02007-01-17 21:45:01 +000049//===----------------------------------------------------------------------===//
50// CGPassManager
51//
Dale Johannesen50f03762009-09-10 22:01:32 +000052/// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
Devang Patel48537a02007-01-17 21:45:01 +000053
Dan Gohmand78c4002008-05-13 00:00:25 +000054namespace {
55
Devang Patel48537a02007-01-17 21:45:01 +000056class CGPassManager : public ModulePass, public PMDataManager {
Devang Patel48537a02007-01-17 21:45:01 +000057public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000058 static char ID;
Eugene Zelenkofa6434b2017-08-31 21:56:16 +000059
60 explicit CGPassManager() : ModulePass(ID), PMDataManager() {}
Devang Patel48537a02007-01-17 21:45:01 +000061
Sanjay Pateld45a3f12015-03-10 03:48:14 +000062 /// Execute all of the passes scheduled for execution. Keep track of
Devang Patel48537a02007-01-17 21:45:01 +000063 /// whether any of the passes modifies the module, and if so, return true.
Craig Toppere9ba7592014-03-05 07:30:04 +000064 bool runOnModule(Module &M) override;
Devang Patel48537a02007-01-17 21:45:01 +000065
Owen Anderson1aa27512012-11-15 00:14:15 +000066 using ModulePass::doInitialization;
67 using ModulePass::doFinalization;
68
Bill Wendling5f14a012009-02-11 18:19:24 +000069 bool doInitialization(CallGraph &CG);
70 bool doFinalization(CallGraph &CG);
Devang Patel48537a02007-01-17 21:45:01 +000071
72 /// Pass Manager itself does not invalidate any analysis info.
Craig Toppere9ba7592014-03-05 07:30:04 +000073 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel48537a02007-01-17 21:45:01 +000074 // CGPassManager walks SCC and it needs CallGraph.
Chandler Carruth6378cf52013-11-26 04:19:30 +000075 Info.addRequired<CallGraphWrapperPass>();
Devang Patel48537a02007-01-17 21:45:01 +000076 Info.setPreservesAll();
77 }
78
Mehdi Amini117296c2016-10-01 02:56:57 +000079 StringRef getPassName() const override { return "CallGraph Pass Manager"; }
Devang Patelf7fea8a2007-02-01 22:09:37 +000080
Craig Toppere9ba7592014-03-05 07:30:04 +000081 PMDataManager *getAsPMDataManager() override { return this; }
82 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +000083
Devang Patel48537a02007-01-17 21:45:01 +000084 // Print passes managed by this manager
Craig Toppere9ba7592014-03-05 07:30:04 +000085 void dumpPassStructure(unsigned Offset) override {
David Greene3d646312009-12-23 23:09:39 +000086 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
Devang Patel48537a02007-01-17 21:45:01 +000087 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
88 Pass *P = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +000089 P->dumpPassStructure(Offset + 1);
Devang Patel48537a02007-01-17 21:45:01 +000090 dumpLastUses(P, Offset+1);
91 }
92 }
93
94 Pass *getContainedPass(unsigned N) {
Chris Lattner13626022009-08-23 06:03:38 +000095 assert(N < PassVector.size() && "Pass number out of range!");
96 return static_cast<Pass *>(PassVector[N]);
Devang Patel48537a02007-01-17 21:45:01 +000097 }
98
Craig Toppere9ba7592014-03-05 07:30:04 +000099 PassManagerType getPassManagerType() const override {
Fangrui Songf78650a2018-07-30 19:41:25 +0000100 return PMT_CallGraphPassManager;
Devang Patel48537a02007-01-17 21:45:01 +0000101 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000102
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000103private:
Chris Lattner6fbe7042010-04-21 00:47:40 +0000104 bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
105 bool &DevirtualizedCall);
Fangrui Songf78650a2018-07-30 19:41:25 +0000106
Chris Lattner4422d312010-04-16 22:42:17 +0000107 bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
Chris Lattner6fbe7042010-04-21 00:47:40 +0000108 CallGraph &CG, bool &CallGraphUpToDate,
109 bool &DevirtualizedCall);
Mehdi Aminic137c282016-08-08 18:51:05 +0000110 bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
Chris Lattnere33167a2009-09-01 18:32:03 +0000111 bool IsCheckingMode);
Devang Patel48537a02007-01-17 21:45:01 +0000112};
113
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000114} // end anonymous namespace.
Dan Gohmand78c4002008-05-13 00:00:25 +0000115
Devang Patel8c78a0b2007-05-03 01:11:54 +0000116char CGPassManager::ID = 0;
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000117
Chris Lattner4422d312010-04-16 22:42:17 +0000118bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
Chris Lattner6fbe7042010-04-21 00:47:40 +0000119 CallGraph &CG, bool &CallGraphUpToDate,
120 bool &DevirtualizedCall) {
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000121 bool Changed = false;
Chris Lattner0b1c7232010-01-22 05:46:59 +0000122 PMDataManager *PM = P->getAsPMDataManager();
Jessica Paquettee49374d2018-05-18 17:26:39 +0000123 Module &M = CG.getModule();
Chris Lattner0b1c7232010-01-22 05:46:59 +0000124
Craig Topper353eda42014-04-24 06:44:33 +0000125 if (!PM) {
Jessica Paquette9a23c552018-08-31 20:20:57 +0000126 CallGraphSCCPass *CGSP = (CallGraphSCCPass *)P;
Chris Lattnereedcd842009-08-31 07:23:46 +0000127 if (!CallGraphUpToDate) {
Chris Lattner6fbe7042010-04-21 00:47:40 +0000128 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
Chris Lattnereedcd842009-08-31 07:23:46 +0000129 CallGraphUpToDate = true;
130 }
Chris Lattner339c82d2009-09-01 20:33:43 +0000131
Chris Lattner707431c2010-03-30 04:03:22 +0000132 {
Jessica Paquette1fc443b2018-08-31 20:20:56 +0000133 unsigned InstrCount, SCCCount = 0;
Jessica Paquettea0aa5b32018-09-06 21:19:54 +0000134 StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
Xin Tong023e25a2018-07-22 05:27:41 +0000135 bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
Chris Lattner707431c2010-03-30 04:03:22 +0000136 TimeRegion PassTimer(getPassTimer(CGSP));
Xin Tong023e25a2018-07-22 05:27:41 +0000137 if (EmitICRemark)
Jessica Paquettea0aa5b32018-09-06 21:19:54 +0000138 InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
Chris Lattner707431c2010-03-30 04:03:22 +0000139 Changed = CGSP->runOnSCC(CurSCC);
Jessica Paquettee49374d2018-05-18 17:26:39 +0000140
Jessica Paquette1fc443b2018-08-31 20:20:56 +0000141 if (EmitICRemark) {
142 // FIXME: Add getInstructionCount to CallGraphSCC.
Jessica Paquette1fc443b2018-08-31 20:20:56 +0000143 SCCCount = M.getInstructionCount();
144 // Is there a difference in the number of instructions in the module?
145 if (SCCCount != InstrCount) {
146 // Yep. Emit a remark and update InstrCount.
Jessica Paquette9a23c552018-08-31 20:20:57 +0000147 int64_t Delta =
148 static_cast<int64_t>(SCCCount) - static_cast<int64_t>(InstrCount);
Jessica Paquettea0aa5b32018-09-06 21:19:54 +0000149 emitInstrCountChangedRemark(P, M, Delta, InstrCount,
150 FunctionToInstrCount);
Jessica Paquette1fc443b2018-08-31 20:20:56 +0000151 InstrCount = SCCCount;
152 }
153 }
Chris Lattner707431c2010-03-30 04:03:22 +0000154 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000155
Chris Lattnere33167a2009-09-01 18:32:03 +0000156 // After the CGSCCPass is done, when assertions are enabled, use
157 // RefreshCallGraph to verify that the callgraph was correctly updated.
158#ifndef NDEBUG
159 if (Changed)
160 RefreshCallGraph(CurSCC, CG, true);
161#endif
Fangrui Songf78650a2018-07-30 19:41:25 +0000162
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000163 return Changed;
164 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000165
Chris Lattner0b1c7232010-01-22 05:46:59 +0000166 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
167 "Invalid CGPassManager member");
168 FPPassManager *FPP = (FPPassManager*)P;
Fangrui Songf78650a2018-07-30 19:41:25 +0000169
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000170 // Run pass P on all functions in the current SCC.
Sanjay Patelc6012542015-03-10 03:26:39 +0000171 for (CallGraphNode *CGN : CurSCC) {
172 if (Function *F = CGN->getFunction()) {
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000173 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
Juergen Ributzka34390c72014-05-16 02:33:15 +0000174 {
175 TimeRegion PassTimer(getPassTimer(FPP));
176 Changed |= FPP->runOnFunction(*F);
177 }
178 F->getContext().yield();
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000179 }
180 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000181
Chris Lattnera9262bb2009-08-31 17:08:30 +0000182 // The function pass(es) modified the IR, they may have clobbered the
Chris Lattnereedcd842009-08-31 07:23:46 +0000183 // callgraph.
184 if (Changed && CallGraphUpToDate) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000185 LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " << P->getPassName()
186 << '\n');
Chris Lattnereedcd842009-08-31 07:23:46 +0000187 CallGraphUpToDate = false;
188 }
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000189 return Changed;
190}
191
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000192/// Scan the functions in the specified CFG and resync the
Chris Lattnere33167a2009-09-01 18:32:03 +0000193/// callgraph with the call sites found in it. This is used after
194/// FunctionPasses have potentially munged the callgraph, and can be used after
195/// CallGraphSCC passes to verify that they correctly updated the callgraph.
196///
Chris Lattner6fbe7042010-04-21 00:47:40 +0000197/// This function returns true if it devirtualized an existing function call,
198/// meaning it turned an indirect call into a direct call. This happens when
199/// a function pass like GVN optimizes away stuff feeding the indirect call.
200/// This never happens in checking mode.
Mehdi Aminic137c282016-08-08 18:51:05 +0000201bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
202 bool CheckingMode) {
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000203 DenseMap<Value *, CallGraphNode *> Calls;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000204
205 LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
206 << " nodes:\n";
207 for (CallGraphNode *CGN
208 : CurSCC) CGN->dump(););
Chris Lattnereedcd842009-08-31 07:23:46 +0000209
210 bool MadeChange = false;
Chris Lattner6fbe7042010-04-21 00:47:40 +0000211 bool DevirtualizedCall = false;
Fangrui Songf78650a2018-07-30 19:41:25 +0000212
Chris Lattnereedcd842009-08-31 07:23:46 +0000213 // Scan all functions in the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000214 unsigned FunctionNo = 0;
215 for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
216 SCCIdx != E; ++SCCIdx, ++FunctionNo) {
217 CallGraphNode *CGN = *SCCIdx;
Chris Lattnereedcd842009-08-31 07:23:46 +0000218 Function *F = CGN->getFunction();
Craig Topper353eda42014-04-24 06:44:33 +0000219 if (!F || F->isDeclaration()) continue;
Fangrui Songf78650a2018-07-30 19:41:25 +0000220
Chris Lattnereedcd842009-08-31 07:23:46 +0000221 // Walk the function body looking for call sites. Sync up the call sites in
222 // CGN with those actually in the function.
Chris Lattner532112b2010-05-01 06:38:43 +0000223
224 // Keep track of the number of direct and indirect calls that were
225 // invalidated and removed.
226 unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
Fangrui Songf78650a2018-07-30 19:41:25 +0000227
Chris Lattnereedcd842009-08-31 07:23:46 +0000228 // Get the set of call sites currently in the function.
Duncan Sands5632d962009-09-02 03:48:41 +0000229 for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
Chris Lattner063d0652009-09-01 06:31:31 +0000230 // If this call site is null, then the function pass deleted the call
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000231 // entirely and the WeakTrackingVH nulled it out.
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000232 auto *Call = dyn_cast_or_null<CallBase>(I->first);
Craig Topper353eda42014-04-24 06:44:33 +0000233 if (!I->first ||
Chris Lattner063d0652009-09-01 06:31:31 +0000234 // If we've already seen this call site, then the FunctionPass RAUW'd
235 // one call with another, which resulted in two "uses" in the edge
236 // list of the same call.
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000237 Calls.count(I->first) ||
Chris Lattner063d0652009-09-01 06:31:31 +0000238
Chad Rosier7a20ed72015-04-14 15:52:57 +0000239 // If the call edge is not from a call or invoke, or it is a
Fangrui Songf78650a2018-07-30 19:41:25 +0000240 // instrinsic call, then the function pass RAUW'd a call with
Chad Rosier7a20ed72015-04-14 15:52:57 +0000241 // another value. This can happen when constant folding happens
242 // of well known functions etc.
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000243 !Call ||
244 (Call->getCalledFunction() &&
245 Call->getCalledFunction()->isIntrinsic() &&
246 Intrinsic::isLeaf(Call->getCalledFunction()->getIntrinsicID()))) {
Chris Lattnere33167a2009-09-01 18:32:03 +0000247 assert(!CheckingMode &&
248 "CallGraphSCCPass did not update the CallGraph correctly!");
Fangrui Songf78650a2018-07-30 19:41:25 +0000249
Chris Lattner532112b2010-05-01 06:38:43 +0000250 // If this was an indirect call site, count it.
Craig Topper353eda42014-04-24 06:44:33 +0000251 if (!I->second->getFunction())
Chris Lattner532112b2010-05-01 06:38:43 +0000252 ++NumIndirectRemoved;
Fangrui Songf78650a2018-07-30 19:41:25 +0000253 else
Chris Lattner532112b2010-05-01 06:38:43 +0000254 ++NumDirectRemoved;
Fangrui Songf78650a2018-07-30 19:41:25 +0000255
Chris Lattner65fb5972009-09-02 04:39:04 +0000256 // Just remove the edge from the set of callees, keep track of whether
257 // I points to the last element of the vector.
258 bool WasLast = I + 1 == E;
Chris Lattner063d0652009-09-01 06:31:31 +0000259 CGN->removeCallEdge(I);
Fangrui Songf78650a2018-07-30 19:41:25 +0000260
Chris Lattner65fb5972009-09-02 04:39:04 +0000261 // If I pointed to the last element of the vector, we have to bail out:
262 // iterator checking rejects comparisons of the resultant pointer with
263 // end.
264 if (WasLast)
265 break;
Chris Lattner063d0652009-09-01 06:31:31 +0000266 E = CGN->end();
Chris Lattner063d0652009-09-01 06:31:31 +0000267 continue;
268 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000269
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000270 assert(!Calls.count(I->first) &&
Chris Lattnereedcd842009-08-31 07:23:46 +0000271 "Call site occurs in node multiple times");
Fangrui Songf78650a2018-07-30 19:41:25 +0000272
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000273 if (Call) {
274 Function *Callee = Call->getCalledFunction();
Sanjay Patel4c219fd2014-11-12 18:25:47 +0000275 // Ignore intrinsics because they're not really function calls.
276 if (!Callee || !(Callee->isIntrinsic()))
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000277 Calls.insert(std::make_pair(I->first, I->second));
Sanjay Patel4c219fd2014-11-12 18:25:47 +0000278 }
Chris Lattner3f7b3d12009-09-01 18:13:40 +0000279 ++I;
Chris Lattnereedcd842009-08-31 07:23:46 +0000280 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000281
Chris Lattnereedcd842009-08-31 07:23:46 +0000282 // Loop over all of the instructions in the function, getting the callsites.
Chris Lattner532112b2010-05-01 06:38:43 +0000283 // Keep track of the number of direct/indirect calls added.
284 unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
Benjamin Krameraa209152016-06-26 17:27:42 +0000285
286 for (BasicBlock &BB : *F)
287 for (Instruction &I : BB) {
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000288 auto *Call = dyn_cast<CallBase>(&I);
289 if (!Call)
290 continue;
291 Function *Callee = Call->getCalledFunction();
292 if (Callee && Callee->isIntrinsic())
293 continue;
Fangrui Songf78650a2018-07-30 19:41:25 +0000294
Chris Lattnereedcd842009-08-31 07:23:46 +0000295 // If this call site already existed in the callgraph, just verify it
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000296 // matches up to expectations and remove it from Calls.
297 DenseMap<Value *, CallGraphNode *>::iterator ExistingIt =
298 Calls.find(Call);
299 if (ExistingIt != Calls.end()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000300 CallGraphNode *ExistingNode = ExistingIt->second;
301
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000302 // Remove from Calls since we have now seen it.
303 Calls.erase(ExistingIt);
Fangrui Songf78650a2018-07-30 19:41:25 +0000304
Chris Lattnereedcd842009-08-31 07:23:46 +0000305 // Verify that the callee is right.
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000306 if (ExistingNode->getFunction() == Call->getCalledFunction())
Chris Lattnereedcd842009-08-31 07:23:46 +0000307 continue;
Fangrui Songf78650a2018-07-30 19:41:25 +0000308
Chris Lattnere33167a2009-09-01 18:32:03 +0000309 // If we are in checking mode, we are not allowed to actually mutate
310 // the callgraph. If this is a case where we can infer that the
311 // callgraph is less precise than it could be (e.g. an indirect call
312 // site could be turned direct), don't reject it in checking mode, and
313 // don't tweak it to be more precise.
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000314 if (CheckingMode && Call->getCalledFunction() &&
Craig Topper353eda42014-04-24 06:44:33 +0000315 ExistingNode->getFunction() == nullptr)
Chris Lattnere33167a2009-09-01 18:32:03 +0000316 continue;
Fangrui Songf78650a2018-07-30 19:41:25 +0000317
Chris Lattnere33167a2009-09-01 18:32:03 +0000318 assert(!CheckingMode &&
319 "CallGraphSCCPass did not update the CallGraph correctly!");
Fangrui Songf78650a2018-07-30 19:41:25 +0000320
Chris Lattnereedcd842009-08-31 07:23:46 +0000321 // If not, we either went from a direct call to indirect, indirect to
322 // direct, or direct to different direct.
323 CallGraphNode *CalleeNode;
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000324 if (Function *Callee = Call->getCalledFunction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000325 CalleeNode = CG.getOrInsertFunction(Callee);
Chris Lattner6fbe7042010-04-21 00:47:40 +0000326 // Keep track of whether we turned an indirect call into a direct
327 // one.
Craig Topper353eda42014-04-24 06:44:33 +0000328 if (!ExistingNode->getFunction()) {
Chris Lattner6fbe7042010-04-21 00:47:40 +0000329 DevirtualizedCall = true;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000330 LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
331 << Callee->getName() << "'\n");
Chris Lattner6fbe7042010-04-21 00:47:40 +0000332 }
333 } else {
Chris Lattnereedcd842009-08-31 07:23:46 +0000334 CalleeNode = CG.getCallsExternalNode();
Chris Lattner6fbe7042010-04-21 00:47:40 +0000335 }
Chris Lattner339c82d2009-09-01 20:33:43 +0000336
337 // Update the edge target in CGN.
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000338 CGN->replaceCallEdge(*Call, *Call, CalleeNode);
Chris Lattnereedcd842009-08-31 07:23:46 +0000339 MadeChange = true;
340 continue;
341 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000342
Chris Lattnere33167a2009-09-01 18:32:03 +0000343 assert(!CheckingMode &&
344 "CallGraphSCCPass did not update the CallGraph correctly!");
345
Chris Lattner532112b2010-05-01 06:38:43 +0000346 // If the call site didn't exist in the CGN yet, add it.
Chris Lattnereedcd842009-08-31 07:23:46 +0000347 CallGraphNode *CalleeNode;
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000348 if (Function *Callee = Call->getCalledFunction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000349 CalleeNode = CG.getOrInsertFunction(Callee);
Chris Lattner532112b2010-05-01 06:38:43 +0000350 ++NumDirectAdded;
351 } else {
Chris Lattnereedcd842009-08-31 07:23:46 +0000352 CalleeNode = CG.getCallsExternalNode();
Chris Lattner532112b2010-05-01 06:38:43 +0000353 ++NumIndirectAdded;
354 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000355
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000356 CGN->addCalledFunction(Call, CalleeNode);
Chris Lattnereedcd842009-08-31 07:23:46 +0000357 MadeChange = true;
358 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000359
Chris Lattner532112b2010-05-01 06:38:43 +0000360 // We scanned the old callgraph node, removing invalidated call sites and
361 // then added back newly found call sites. One thing that can happen is
362 // that an old indirect call site was deleted and replaced with a new direct
363 // call. In this case, we have devirtualized a call, and CGSCCPM would like
364 // to iteratively optimize the new code. Unfortunately, we don't really
365 // have a great way to detect when this happens. As an approximation, we
366 // just look at whether the number of indirect calls is reduced and the
367 // number of direct calls is increased. There are tons of ways to fool this
368 // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
369 // direct call) but this is close enough.
370 if (NumIndirectRemoved > NumIndirectAdded &&
371 NumDirectRemoved < NumDirectAdded)
372 DevirtualizedCall = true;
Fangrui Songf78650a2018-07-30 19:41:25 +0000373
Chris Lattnereedcd842009-08-31 07:23:46 +0000374 // After scanning this function, if we still have entries in callsites, then
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000375 // they are dangling pointers. WeakTrackingVH should save us for this, so
376 // abort if
Sanjoy Das2cbeb002017-04-26 16:37:05 +0000377 // this happens.
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000378 assert(Calls.empty() && "Dangling pointers found in call sites map");
Fangrui Songf78650a2018-07-30 19:41:25 +0000379
Chris Lattner063d0652009-09-01 06:31:31 +0000380 // Periodically do an explicit clear to remove tombstones when processing
381 // large scc's.
Chris Lattner4422d312010-04-16 22:42:17 +0000382 if ((FunctionNo & 15) == 15)
Chandler Carruthce3f75d2019-04-19 05:59:42 +0000383 Calls.clear();
Chris Lattnereedcd842009-08-31 07:23:46 +0000384 }
385
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000386 LLVM_DEBUG(if (MadeChange) {
387 dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
388 for (CallGraphNode *CGN : CurSCC)
389 CGN->dump();
390 if (DevirtualizedCall)
391 dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
392 } else {
393 dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
394 });
Duncan Sandsa41634e2011-08-12 14:54:45 +0000395 (void)MadeChange;
Chris Lattner6fbe7042010-04-21 00:47:40 +0000396
397 return DevirtualizedCall;
398}
399
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000400/// Execute the body of the entire pass manager on the specified SCC.
401/// This keeps track of whether a function pass devirtualizes
Chris Lattner6fbe7042010-04-21 00:47:40 +0000402/// any calls and returns it in DevirtualizedCall.
403bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
404 bool &DevirtualizedCall) {
405 bool Changed = false;
Fangrui Songf78650a2018-07-30 19:41:25 +0000406
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000407 // Keep track of whether the callgraph is known to be up-to-date or not.
408 // The CGSSC pass manager runs two types of passes:
Chris Lattner6fbe7042010-04-21 00:47:40 +0000409 // CallGraphSCC Passes and other random function passes. Because other
410 // random function passes are not CallGraph aware, they may clobber the
411 // call graph by introducing new calls or deleting other ones. This flag
412 // is set to false when we run a function pass so that we know to clean up
413 // the callgraph when we need to run a CGSCCPass again.
414 bool CallGraphUpToDate = true;
415
416 // Run all passes on current SCC.
417 for (unsigned PassNo = 0, e = getNumContainedPasses();
418 PassNo != e; ++PassNo) {
419 Pass *P = getContainedPass(PassNo);
Fangrui Songf78650a2018-07-30 19:41:25 +0000420
Chris Lattner6fbe7042010-04-21 00:47:40 +0000421 // If we're in -debug-pass=Executions mode, construct the SCC node list,
422 // otherwise avoid constructing this string as it is expensive.
423 if (isPassDebuggingExecutionsOrMore()) {
424 std::string Functions;
425 #ifndef NDEBUG
426 raw_string_ostream OS(Functions);
427 for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
428 I != E; ++I) {
429 if (I != CurSCC.begin()) OS << ", ";
430 (*I)->print(OS);
431 }
432 OS.flush();
433 #endif
434 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
435 }
436 dumpRequiredSet(P);
Fangrui Songf78650a2018-07-30 19:41:25 +0000437
Chris Lattner6fbe7042010-04-21 00:47:40 +0000438 initializeAnalysisImpl(P);
Fangrui Songf78650a2018-07-30 19:41:25 +0000439
Chris Lattner6fbe7042010-04-21 00:47:40 +0000440 // Actually run this pass on the current SCC.
441 Changed |= RunPassOnSCC(P, CurSCC, CG,
442 CallGraphUpToDate, DevirtualizedCall);
Fangrui Songf78650a2018-07-30 19:41:25 +0000443
Chris Lattner6fbe7042010-04-21 00:47:40 +0000444 if (Changed)
445 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
446 dumpPreservedSet(P);
Fangrui Songf78650a2018-07-30 19:41:25 +0000447
448 verifyPreservedAnalysis(P);
Chris Lattner6fbe7042010-04-21 00:47:40 +0000449 removeNotPreservedAnalysis(P);
450 recordAvailableAnalysis(P);
451 removeDeadPasses(P, "", ON_CG_MSG);
452 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000453
Chris Lattner6fbe7042010-04-21 00:47:40 +0000454 // If the callgraph was left out of date (because the last pass run was a
455 // functionpass), refresh it before we move on to the next SCC.
456 if (!CallGraphUpToDate)
457 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
458 return Changed;
Chris Lattnereedcd842009-08-31 07:23:46 +0000459}
Chris Lattner7e4fbdd2009-08-31 06:01:21 +0000460
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000461/// Execute all of the passes scheduled for execution. Keep track of
Devang Patel48537a02007-01-17 21:45:01 +0000462/// whether any of the passes modifies the module, and if so, return true.
463bool CGPassManager::runOnModule(Module &M) {
Chandler Carruth6378cf52013-11-26 04:19:30 +0000464 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Bill Wendling5f14a012009-02-11 18:19:24 +0000465 bool Changed = doInitialization(CG);
Fangrui Songf78650a2018-07-30 19:41:25 +0000466
Evgeniy Stepanovca8c2f72018-07-13 16:32:31 +0000467 // Walk the callgraph in bottom-up SCC order.
468 scc_iterator<CallGraph*> CGI = scc_begin(&CG);
Chris Lattner5518b812010-04-16 22:59:24 +0000469
Evgeniy Stepanovca8c2f72018-07-13 16:32:31 +0000470 CallGraphSCC CurSCC(CG, &CGI);
471 while (!CGI.isAtEnd()) {
472 // Copy the current SCC and increment past it so that the pass can hack
473 // on the SCC if it wants to without invalidating our iterator.
474 const std::vector<CallGraphNode *> &NodeVec = *CGI;
475 CurSCC.initialize(NodeVec);
476 ++CGI;
Yaron Kerenafe39832015-07-02 14:17:12 +0000477
Evgeniy Stepanovca8c2f72018-07-13 16:32:31 +0000478 // At the top level, we run all the passes in this pass manager on the
479 // functions in this SCC. However, we support iterative compilation in the
480 // case where a function pass devirtualizes a call to a function. For
481 // example, it is very common for a function pass (often GVN or instcombine)
482 // to eliminate the addressing that feeds into a call. With that improved
483 // information, we would like the call to be an inline candidate, infer
484 // mod-ref information etc.
485 //
486 // Because of this, we allow iteration up to a specified iteration count.
487 // This only happens in the case of a devirtualized call, so we only burn
488 // compile time in the case that we're making progress. We also have a hard
489 // iteration count limit in case there is crazy code.
490 unsigned Iteration = 0;
491 bool DevirtualizedCall = false;
492 do {
493 LLVM_DEBUG(if (Iteration) dbgs()
494 << " SCCPASSMGR: Re-visiting SCC, iteration #" << Iteration
495 << '\n');
496 DevirtualizedCall = false;
497 Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
498 } while (Iteration++ < MaxIterations && DevirtualizedCall);
Fangrui Songf78650a2018-07-30 19:41:25 +0000499
Evgeniy Stepanovca8c2f72018-07-13 16:32:31 +0000500 if (DevirtualizedCall)
501 LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after "
502 << Iteration
503 << " times, due to -max-cg-scc-iterations\n");
Craig Topper8a950272017-05-18 00:51:39 +0000504
Evgeniy Stepanovca8c2f72018-07-13 16:32:31 +0000505 MaxSCCIterations.updateMax(Iteration);
Devang Patel48537a02007-01-17 21:45:01 +0000506 }
Bill Wendling5f14a012009-02-11 18:19:24 +0000507 Changed |= doFinalization(CG);
Devang Patel48537a02007-01-17 21:45:01 +0000508 return Changed;
509}
510
511/// Initialize CG
Bill Wendling5f14a012009-02-11 18:19:24 +0000512bool CGPassManager::doInitialization(CallGraph &CG) {
Devang Patel48537a02007-01-17 21:45:01 +0000513 bool Changed = false;
Fangrui Songf78650a2018-07-30 19:41:25 +0000514 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000515 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
516 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
517 "Invalid CGPassManager member");
518 Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000519 } else {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000520 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000521 }
Devang Patel48537a02007-01-17 21:45:01 +0000522 }
523 return Changed;
524}
525
526/// Finalize CG
Bill Wendling5f14a012009-02-11 18:19:24 +0000527bool CGPassManager::doFinalization(CallGraph &CG) {
Devang Patel48537a02007-01-17 21:45:01 +0000528 bool Changed = false;
Fangrui Songf78650a2018-07-30 19:41:25 +0000529 for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000530 if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
531 assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
532 "Invalid CGPassManager member");
533 Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000534 } else {
Chris Lattner0b1c7232010-01-22 05:46:59 +0000535 Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
Nick Lewyckycdccffe2009-02-13 07:15:53 +0000536 }
Devang Patel48537a02007-01-17 21:45:01 +0000537 }
538 return Changed;
539}
540
Chris Lattner6d1208f2010-04-16 21:43:55 +0000541//===----------------------------------------------------------------------===//
Chris Lattner4422d312010-04-16 22:42:17 +0000542// CallGraphSCC Implementation
543//===----------------------------------------------------------------------===//
544
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000545/// This informs the SCC and the pass manager that the specified
Chris Lattner5518b812010-04-16 22:59:24 +0000546/// Old node has been deleted, and New is to be used in its place.
547void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
548 assert(Old != New && "Should not replace node with self");
549 for (unsigned i = 0; ; ++i) {
550 assert(i != Nodes.size() && "Node not in SCC");
551 if (Nodes[i] != Old) continue;
552 Nodes[i] = New;
553 break;
554 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000555
Chris Lattnerde023a32010-04-16 23:04:30 +0000556 // Update the active scc_iterator so that it doesn't contain dangling
557 // pointers to the old CallGraphNode.
558 scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
559 CGI->ReplaceNode(Old, New);
Chris Lattner5518b812010-04-16 22:59:24 +0000560}
Chris Lattner4422d312010-04-16 22:42:17 +0000561
Chris Lattner4422d312010-04-16 22:42:17 +0000562//===----------------------------------------------------------------------===//
Chris Lattner6d1208f2010-04-16 21:43:55 +0000563// CallGraphSCCPass Implementation
564//===----------------------------------------------------------------------===//
David Greene9b063df2010-04-02 23:17:14 +0000565
Devang Patel020f4f22007-01-23 21:55:17 +0000566/// Assign pass manager to manage this pass.
Devang Patel1f8200b2007-01-23 21:52:35 +0000567void CallGraphSCCPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +0000568 PassManagerType PreferredType) {
Fangrui Songf78650a2018-07-30 19:41:25 +0000569 // Find CGPassManager
Duncan Sands60f28bf2007-07-19 09:42:01 +0000570 while (!PMS.empty() &&
571 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
572 PMS.pop();
Devang Patel1f8200b2007-01-23 21:52:35 +0000573
Chris Lattner9efd4fc2010-01-22 05:37:10 +0000574 assert(!PMS.empty() && "Unable to handle Call Graph Pass");
575 CGPassManager *CGP;
Fangrui Songf78650a2018-07-30 19:41:25 +0000576
Chris Lattner9efd4fc2010-01-22 05:37:10 +0000577 if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
578 CGP = (CGPassManager*)PMS.top();
579 else {
Fangrui Songf78650a2018-07-30 19:41:25 +0000580 // Create new Call Graph SCC Pass Manager if it does not exist.
Chris Lattner9efd4fc2010-01-22 05:37:10 +0000581 assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
Devang Patel1f8200b2007-01-23 21:52:35 +0000582 PMDataManager *PMD = PMS.top();
583
584 // [1] Create new Call Graph Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +0000585 CGP = new CGPassManager();
Devang Patel1f8200b2007-01-23 21:52:35 +0000586
587 // [2] Set up new manager's top level manager
588 PMTopLevelManager *TPM = PMD->getTopLevelManager();
589 TPM->addIndirectPassManager(CGP);
590
591 // [3] Assign manager to manage this new manager. This may create
592 // and push new managers into PMS
Chris Lattner9efd4fc2010-01-22 05:37:10 +0000593 Pass *P = CGP;
Devang Patel703de8f2007-06-21 22:29:02 +0000594 TPM->schedulePass(P);
Devang Patel1f8200b2007-01-23 21:52:35 +0000595
596 // [4] Push new manager into PMS
597 PMS.push(CGP);
598 }
599
600 CGP->add(this);
601}
602
Sanjay Pateld45a3f12015-03-10 03:48:14 +0000603/// For this class, we declare that we require and preserve the call graph.
604/// If the derived class implements this method, it should
Chris Lattnerf7e95942003-08-31 01:54:59 +0000605/// always explicitly call the implementation here.
606void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruth6378cf52013-11-26 04:19:30 +0000607 AU.addRequired<CallGraphWrapperPass>();
608 AU.addPreserved<CallGraphWrapperPass>();
Chris Lattnerf7e95942003-08-31 01:54:59 +0000609}
Chris Lattner6d1208f2010-04-16 21:43:55 +0000610
Chris Lattner6d1208f2010-04-16 21:43:55 +0000611//===----------------------------------------------------------------------===//
612// PrintCallGraphPass Implementation
613//===----------------------------------------------------------------------===//
614
615namespace {
Eugene Zelenkofa6434b2017-08-31 21:56:16 +0000616
Chris Lattner6d1208f2010-04-16 21:43:55 +0000617 /// PrintCallGraphPass - Print a Module corresponding to a call graph.
618 ///
619 class PrintCallGraphPass : public CallGraphSCCPass {
620 std::string Banner;
Eugene Zelenkofa6434b2017-08-31 21:56:16 +0000621 raw_ostream &OS; // raw_ostream to print on.
Fangrui Songf78650a2018-07-30 19:41:25 +0000622
Chris Lattner6d1208f2010-04-16 21:43:55 +0000623 public:
624 static char ID;
Eugene Zelenkofa6434b2017-08-31 21:56:16 +0000625
626 PrintCallGraphPass(const std::string &B, raw_ostream &OS)
627 : CallGraphSCCPass(ID), Banner(B), OS(OS) {}
Craig Toppere9ba7592014-03-05 07:30:04 +0000628
629 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner6d1208f2010-04-16 21:43:55 +0000630 AU.setPreservesAll();
631 }
Craig Toppere9ba7592014-03-05 07:30:04 +0000632
633 bool runOnSCC(CallGraphSCC &SCC) override {
Yaron Keren7d463922017-06-12 02:18:50 +0000634 bool BannerPrinted = false;
Fedor Sergeev7254d3c2018-12-03 14:48:15 +0000635 auto PrintBannerOnce = [&]() {
Mehdi Amini062b3fe2017-01-18 21:37:11 +0000636 if (BannerPrinted)
637 return;
Eugene Zelenkofa6434b2017-08-31 21:56:16 +0000638 OS << Banner;
Mehdi Amini062b3fe2017-01-18 21:37:11 +0000639 BannerPrinted = true;
Fedor Sergeev7254d3c2018-12-03 14:48:15 +0000640 };
641
642 bool NeedModule = llvm::forcePrintModuleIR();
643 if (isFunctionInPrintList("*") && NeedModule) {
644 PrintBannerOnce();
645 OS << "\n";
646 SCC.getCallGraph().getModule().print(OS, nullptr);
647 return false;
648 }
649 bool FoundFunction = false;
Sanjay Patelc6012542015-03-10 03:26:39 +0000650 for (CallGraphNode *CGN : SCC) {
Yaron Keren7d463922017-06-12 02:18:50 +0000651 if (Function *F = CGN->getFunction()) {
652 if (!F->isDeclaration() && isFunctionInPrintList(F->getName())) {
Fedor Sergeev7254d3c2018-12-03 14:48:15 +0000653 FoundFunction = true;
654 if (!NeedModule) {
655 PrintBannerOnce();
656 F->print(OS);
657 }
Mehdi Amini062b3fe2017-01-18 21:37:11 +0000658 }
Eugene Zelenkofa6434b2017-08-31 21:56:16 +0000659 } else if (isFunctionInPrintList("*")) {
Mehdi Amini062b3fe2017-01-18 21:37:11 +0000660 PrintBannerOnce();
Eugene Zelenkofa6434b2017-08-31 21:56:16 +0000661 OS << "\nPrinting <null> Function\n";
Mehdi Amini062b3fe2017-01-18 21:37:11 +0000662 }
Richard Trieua23043c2014-06-09 22:53:16 +0000663 }
Fedor Sergeev7254d3c2018-12-03 14:48:15 +0000664 if (NeedModule && FoundFunction) {
665 PrintBannerOnce();
666 OS << "\n";
667 SCC.getCallGraph().getModule().print(OS, nullptr);
668 }
Chris Lattner6d1208f2010-04-16 21:43:55 +0000669 return false;
670 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000671
Yaron Keren1de47922017-03-10 07:09:20 +0000672 StringRef getPassName() const override { return "Print CallGraph IR"; }
Chris Lattner6d1208f2010-04-16 21:43:55 +0000673 };
Fangrui Songf78650a2018-07-30 19:41:25 +0000674
Chris Lattner6d1208f2010-04-16 21:43:55 +0000675} // end anonymous namespace.
676
677char PrintCallGraphPass::ID = 0;
678
Eugene Zelenkofa6434b2017-08-31 21:56:16 +0000679Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &OS,
Chris Lattner6d1208f2010-04-16 21:43:55 +0000680 const std::string &Banner) const {
Eugene Zelenkofa6434b2017-08-31 21:56:16 +0000681 return new PrintCallGraphPass(Banner, OS);
Chris Lattner6d1208f2010-04-16 21:43:55 +0000682}
683
Richard Trieub37a70f2019-02-28 04:00:55 +0000684static std::string getDescription(const CallGraphSCC &SCC) {
685 std::string Desc = "SCC (";
686 bool First = true;
687 for (CallGraphNode *CGN : SCC) {
688 if (First)
689 First = false;
690 else
691 Desc += ", ";
692 Function *F = CGN->getFunction();
693 if (F)
694 Desc += F->getName();
695 else
696 Desc += "<<null function>>";
697 }
698 Desc += ")";
699 return Desc;
700}
701
Andrew Kayloraa641a52016-04-22 22:06:11 +0000702bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const {
Richard Trieub37a70f2019-02-28 04:00:55 +0000703 OptPassGate &Gate =
704 SCC.getCallGraph().getModule().getContext().getOptPassGate();
705 return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(SCC));
Andrew Kayloraa641a52016-04-22 22:06:11 +0000706}
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000707
708char DummyCGSCCPass::ID = 0;
Eugene Zelenkofa6434b2017-08-31 21:56:16 +0000709
Mehdi Aminibbacddf2016-06-10 16:19:46 +0000710INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false,
711 false)