1009b1c42SEd Schouten //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file implements the CallGraphSCCPass class, which is used for passes
10009b1c42SEd Schouten // which are implemented as bottom-up traversals on the call graph. Because
11009b1c42SEd Schouten // there may be cycles in the call graph, passes of this type operate on the
12009b1c42SEd Schouten // call-graph in SCC order: that is, they process function bottom-up, except for
13009b1c42SEd Schouten // recursive functions, which they process all at once.
14009b1c42SEd Schouten //
15009b1c42SEd Schouten //===----------------------------------------------------------------------===//
16009b1c42SEd Schouten
174a16efa3SDimitry Andric #include "llvm/Analysis/CallGraphSCCPass.h"
18044eb2f6SDimitry Andric #include "llvm/ADT/DenseMap.h"
19009b1c42SEd Schouten #include "llvm/ADT/SCCIterator.h"
20d7f7719eSRoman Divacky #include "llvm/ADT/Statistic.h"
217fa27ce4SDimitry Andric #include "llvm/ADT/StringExtras.h"
224a16efa3SDimitry Andric #include "llvm/Analysis/CallGraph.h"
23cfca06d7SDimitry Andric #include "llvm/IR/AbstractCallSite.h"
244a16efa3SDimitry Andric #include "llvm/IR/Function.h"
25044eb2f6SDimitry Andric #include "llvm/IR/Intrinsics.h"
265ca98fd9SDimitry Andric #include "llvm/IR/LLVMContext.h"
2767c32a98SDimitry Andric #include "llvm/IR/LegacyPassManagers.h"
28044eb2f6SDimitry Andric #include "llvm/IR/Module.h"
2901095a5dSDimitry Andric #include "llvm/IR/OptBisect.h"
30d8e91e46SDimitry Andric #include "llvm/IR/PassTimingInfo.h"
31b60736ecSDimitry Andric #include "llvm/IR/PrintPasses.h"
32044eb2f6SDimitry Andric #include "llvm/Pass.h"
33d7f7719eSRoman Divacky #include "llvm/Support/CommandLine.h"
3459850d08SRoman Divacky #include "llvm/Support/Debug.h"
35104bd817SRoman Divacky #include "llvm/Support/Timer.h"
3659850d08SRoman Divacky #include "llvm/Support/raw_ostream.h"
37044eb2f6SDimitry Andric #include <cassert>
38044eb2f6SDimitry Andric #include <string>
39044eb2f6SDimitry Andric #include <utility>
40044eb2f6SDimitry Andric #include <vector>
41044eb2f6SDimitry Andric
42009b1c42SEd Schouten using namespace llvm;
43009b1c42SEd Schouten
445ca98fd9SDimitry Andric #define DEBUG_TYPE "cgscc-passmgr"
455ca98fd9SDimitry Andric
46344a3780SDimitry Andric namespace llvm {
47b60736ecSDimitry Andric cl::opt<unsigned> MaxDevirtIterations("max-devirt-iterations", cl::ReallyHidden,
48b60736ecSDimitry Andric cl::init(4));
49ac9a064cSDimitry Andric } // namespace llvm
50d7f7719eSRoman Divacky
51d7f7719eSRoman Divacky STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
52d7f7719eSRoman Divacky
53009b1c42SEd Schouten //===----------------------------------------------------------------------===//
54009b1c42SEd Schouten // CGPassManager
55009b1c42SEd Schouten //
5659850d08SRoman Divacky /// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
57009b1c42SEd Schouten
58009b1c42SEd Schouten namespace {
59009b1c42SEd Schouten
60009b1c42SEd Schouten class CGPassManager : public ModulePass, public PMDataManager {
61009b1c42SEd Schouten public:
62009b1c42SEd Schouten static char ID;
63044eb2f6SDimitry Andric
CGPassManager()646f8fc217SDimitry Andric explicit CGPassManager() : ModulePass(ID) {}
65009b1c42SEd Schouten
665a5ac124SDimitry Andric /// Execute all of the passes scheduled for execution. Keep track of
67009b1c42SEd Schouten /// whether any of the passes modifies the module, and if so, return true.
685ca98fd9SDimitry Andric bool runOnModule(Module &M) override;
69009b1c42SEd Schouten
704a16efa3SDimitry Andric using ModulePass::doInitialization;
714a16efa3SDimitry Andric using ModulePass::doFinalization;
724a16efa3SDimitry Andric
73009b1c42SEd Schouten bool doInitialization(CallGraph &CG);
74009b1c42SEd Schouten bool doFinalization(CallGraph &CG);
75009b1c42SEd Schouten
76009b1c42SEd Schouten /// Pass Manager itself does not invalidate any analysis info.
getAnalysisUsage(AnalysisUsage & Info) const775ca98fd9SDimitry Andric void getAnalysisUsage(AnalysisUsage &Info) const override {
78009b1c42SEd Schouten // CGPassManager walks SCC and it needs CallGraph.
795ca98fd9SDimitry Andric Info.addRequired<CallGraphWrapperPass>();
80009b1c42SEd Schouten Info.setPreservesAll();
81009b1c42SEd Schouten }
82009b1c42SEd Schouten
getPassName() const83b915e9e0SDimitry Andric StringRef getPassName() const override { return "CallGraph Pass Manager"; }
84009b1c42SEd Schouten
getAsPMDataManager()855ca98fd9SDimitry Andric PMDataManager *getAsPMDataManager() override { return this; }
getAsPass()865ca98fd9SDimitry Andric Pass *getAsPass() override { return this; }
87989df958SRoman Divacky
88009b1c42SEd Schouten // Print passes managed by this manager
dumpPassStructure(unsigned Offset)895ca98fd9SDimitry Andric void dumpPassStructure(unsigned Offset) override {
9059850d08SRoman Divacky errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
91009b1c42SEd Schouten for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
92009b1c42SEd Schouten Pass *P = getContainedPass(Index);
93009b1c42SEd Schouten P->dumpPassStructure(Offset + 1);
94009b1c42SEd Schouten dumpLastUses(P, Offset+1);
95009b1c42SEd Schouten }
96009b1c42SEd Schouten }
97009b1c42SEd Schouten
getContainedPass(unsigned N)98009b1c42SEd Schouten Pass *getContainedPass(unsigned N) {
99009b1c42SEd Schouten assert(N < PassVector.size() && "Pass number out of range!");
10059850d08SRoman Divacky return static_cast<Pass *>(PassVector[N]);
101009b1c42SEd Schouten }
102009b1c42SEd Schouten
getPassManagerType() const1035ca98fd9SDimitry Andric PassManagerType getPassManagerType() const override {
104009b1c42SEd Schouten return PMT_CallGraphPassManager;
105009b1c42SEd Schouten }
10659850d08SRoman Divacky
10759850d08SRoman Divacky private:
108d7f7719eSRoman Divacky bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
109d7f7719eSRoman Divacky bool &DevirtualizedCall);
110d7f7719eSRoman Divacky
111d7f7719eSRoman Divacky bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
112d7f7719eSRoman Divacky CallGraph &CG, bool &CallGraphUpToDate,
113d7f7719eSRoman Divacky bool &DevirtualizedCall);
114b915e9e0SDimitry Andric bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
11559850d08SRoman Divacky bool IsCheckingMode);
116009b1c42SEd Schouten };
117009b1c42SEd Schouten
11859850d08SRoman Divacky } // end anonymous namespace.
119009b1c42SEd Schouten
120009b1c42SEd Schouten char CGPassManager::ID = 0;
12159850d08SRoman Divacky
RunPassOnSCC(Pass * P,CallGraphSCC & CurSCC,CallGraph & CG,bool & CallGraphUpToDate,bool & DevirtualizedCall)122d7f7719eSRoman Divacky bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
123d7f7719eSRoman Divacky CallGraph &CG, bool &CallGraphUpToDate,
124d7f7719eSRoman Divacky bool &DevirtualizedCall) {
12559850d08SRoman Divacky bool Changed = false;
126989df958SRoman Divacky PMDataManager *PM = P->getAsPMDataManager();
127eb11fae6SDimitry Andric Module &M = CG.getModule();
128989df958SRoman Divacky
1295ca98fd9SDimitry Andric if (!PM) {
130989df958SRoman Divacky CallGraphSCCPass *CGSP = (CallGraphSCCPass *)P;
13159850d08SRoman Divacky if (!CallGraphUpToDate) {
132d7f7719eSRoman Divacky DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
13359850d08SRoman Divacky CallGraphUpToDate = true;
13459850d08SRoman Divacky }
13559850d08SRoman Divacky
136104bd817SRoman Divacky {
137d8e91e46SDimitry Andric unsigned InstrCount, SCCCount = 0;
138d8e91e46SDimitry Andric StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
139eb11fae6SDimitry Andric bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
140104bd817SRoman Divacky TimeRegion PassTimer(getPassTimer(CGSP));
141eb11fae6SDimitry Andric if (EmitICRemark)
142d8e91e46SDimitry Andric InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
14359850d08SRoman Divacky Changed = CGSP->runOnSCC(CurSCC);
144eb11fae6SDimitry Andric
145d8e91e46SDimitry Andric if (EmitICRemark) {
146d8e91e46SDimitry Andric // FIXME: Add getInstructionCount to CallGraphSCC.
147d8e91e46SDimitry Andric SCCCount = M.getInstructionCount();
148d8e91e46SDimitry Andric // Is there a difference in the number of instructions in the module?
149d8e91e46SDimitry Andric if (SCCCount != InstrCount) {
150d8e91e46SDimitry Andric // Yep. Emit a remark and update InstrCount.
151d8e91e46SDimitry Andric int64_t Delta =
152d8e91e46SDimitry Andric static_cast<int64_t>(SCCCount) - static_cast<int64_t>(InstrCount);
153d8e91e46SDimitry Andric emitInstrCountChangedRemark(P, M, Delta, InstrCount,
154d8e91e46SDimitry Andric FunctionToInstrCount);
155d8e91e46SDimitry Andric InstrCount = SCCCount;
156d8e91e46SDimitry Andric }
157d8e91e46SDimitry Andric }
158104bd817SRoman Divacky }
15959850d08SRoman Divacky
16059850d08SRoman Divacky // After the CGSCCPass is done, when assertions are enabled, use
16159850d08SRoman Divacky // RefreshCallGraph to verify that the callgraph was correctly updated.
16259850d08SRoman Divacky #ifndef NDEBUG
16359850d08SRoman Divacky if (Changed)
16459850d08SRoman Divacky RefreshCallGraph(CurSCC, CG, true);
16559850d08SRoman Divacky #endif
16659850d08SRoman Divacky
16759850d08SRoman Divacky return Changed;
16859850d08SRoman Divacky }
16959850d08SRoman Divacky
170989df958SRoman Divacky assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
171989df958SRoman Divacky "Invalid CGPassManager member");
172989df958SRoman Divacky FPPassManager *FPP = (FPPassManager*)P;
17359850d08SRoman Divacky
17459850d08SRoman Divacky // Run pass P on all functions in the current SCC.
1755a5ac124SDimitry Andric for (CallGraphNode *CGN : CurSCC) {
1765a5ac124SDimitry Andric if (Function *F = CGN->getFunction()) {
17759850d08SRoman Divacky dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
1785ca98fd9SDimitry Andric {
179104bd817SRoman Divacky TimeRegion PassTimer(getPassTimer(FPP));
18059850d08SRoman Divacky Changed |= FPP->runOnFunction(*F);
18159850d08SRoman Divacky }
1825ca98fd9SDimitry Andric F->getContext().yield();
1835ca98fd9SDimitry Andric }
18459850d08SRoman Divacky }
18559850d08SRoman Divacky
18659850d08SRoman Divacky // The function pass(es) modified the IR, they may have clobbered the
18759850d08SRoman Divacky // callgraph.
18859850d08SRoman Divacky if (Changed && CallGraphUpToDate) {
189eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " << P->getPassName()
190eb11fae6SDimitry Andric << '\n');
19159850d08SRoman Divacky CallGraphUpToDate = false;
19259850d08SRoman Divacky }
19359850d08SRoman Divacky return Changed;
19459850d08SRoman Divacky }
19559850d08SRoman Divacky
1965a5ac124SDimitry Andric /// Scan the functions in the specified CFG and resync the
19759850d08SRoman Divacky /// callgraph with the call sites found in it. This is used after
19859850d08SRoman Divacky /// FunctionPasses have potentially munged the callgraph, and can be used after
19959850d08SRoman Divacky /// CallGraphSCC passes to verify that they correctly updated the callgraph.
20059850d08SRoman Divacky ///
201d7f7719eSRoman Divacky /// This function returns true if it devirtualized an existing function call,
202d7f7719eSRoman Divacky /// meaning it turned an indirect call into a direct call. This happens when
203d7f7719eSRoman Divacky /// a function pass like GVN optimizes away stuff feeding the indirect call.
204d7f7719eSRoman Divacky /// This never happens in checking mode.
RefreshCallGraph(const CallGraphSCC & CurSCC,CallGraph & CG,bool CheckingMode)205b915e9e0SDimitry Andric bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
206b915e9e0SDimitry Andric bool CheckingMode) {
207e6d15924SDimitry Andric DenseMap<Value *, CallGraphNode *> Calls;
20859850d08SRoman Divacky
209eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
21059850d08SRoman Divacky << " nodes:\n";
211eb11fae6SDimitry Andric for (CallGraphNode *CGN
212eb11fae6SDimitry Andric : CurSCC) CGN->dump(););
21359850d08SRoman Divacky
21459850d08SRoman Divacky bool MadeChange = false;
215d7f7719eSRoman Divacky bool DevirtualizedCall = false;
21659850d08SRoman Divacky
21759850d08SRoman Divacky // Scan all functions in the SCC.
218d7f7719eSRoman Divacky unsigned FunctionNo = 0;
219d7f7719eSRoman Divacky for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
220d7f7719eSRoman Divacky SCCIdx != E; ++SCCIdx, ++FunctionNo) {
221d7f7719eSRoman Divacky CallGraphNode *CGN = *SCCIdx;
22259850d08SRoman Divacky Function *F = CGN->getFunction();
2235ca98fd9SDimitry Andric if (!F || F->isDeclaration()) continue;
22459850d08SRoman Divacky
22559850d08SRoman Divacky // Walk the function body looking for call sites. Sync up the call sites in
22659850d08SRoman Divacky // CGN with those actually in the function.
22759850d08SRoman Divacky
228d7f7719eSRoman Divacky // Keep track of the number of direct and indirect calls that were
229d7f7719eSRoman Divacky // invalidated and removed.
230d7f7719eSRoman Divacky unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
231d7f7719eSRoman Divacky
232cfca06d7SDimitry Andric CallGraphNode::iterator CGNEnd = CGN->end();
233cfca06d7SDimitry Andric
234cfca06d7SDimitry Andric auto RemoveAndCheckForDone = [&](CallGraphNode::iterator I) {
235cfca06d7SDimitry Andric // Just remove the edge from the set of callees, keep track of whether
236cfca06d7SDimitry Andric // I points to the last element of the vector.
237cfca06d7SDimitry Andric bool WasLast = I + 1 == CGNEnd;
238cfca06d7SDimitry Andric CGN->removeCallEdge(I);
239cfca06d7SDimitry Andric
240cfca06d7SDimitry Andric // If I pointed to the last element of the vector, we have to bail out:
241cfca06d7SDimitry Andric // iterator checking rejects comparisons of the resultant pointer with
242cfca06d7SDimitry Andric // end.
243cfca06d7SDimitry Andric if (WasLast)
244cfca06d7SDimitry Andric return true;
245cfca06d7SDimitry Andric
246cfca06d7SDimitry Andric CGNEnd = CGN->end();
247cfca06d7SDimitry Andric return false;
248cfca06d7SDimitry Andric };
249cfca06d7SDimitry Andric
25059850d08SRoman Divacky // Get the set of call sites currently in the function.
251cfca06d7SDimitry Andric for (CallGraphNode::iterator I = CGN->begin(); I != CGNEnd;) {
252cfca06d7SDimitry Andric // Delete "reference" call records that do not have call instruction. We
253cfca06d7SDimitry Andric // reinsert them as needed later. However, keep them in checking mode.
254cfca06d7SDimitry Andric if (!I->first) {
255cfca06d7SDimitry Andric if (CheckingMode) {
256cfca06d7SDimitry Andric ++I;
257cfca06d7SDimitry Andric continue;
258cfca06d7SDimitry Andric }
259cfca06d7SDimitry Andric if (RemoveAndCheckForDone(I))
260cfca06d7SDimitry Andric break;
261cfca06d7SDimitry Andric continue;
262cfca06d7SDimitry Andric }
263cfca06d7SDimitry Andric
26459850d08SRoman Divacky // If this call site is null, then the function pass deleted the call
265a303c417SDimitry Andric // entirely and the WeakTrackingVH nulled it out.
266cfca06d7SDimitry Andric auto *Call = dyn_cast_or_null<CallBase>(*I->first);
267cfca06d7SDimitry Andric if (!Call ||
26859850d08SRoman Divacky // If we've already seen this call site, then the FunctionPass RAUW'd
26959850d08SRoman Divacky // one call with another, which resulted in two "uses" in the edge
27059850d08SRoman Divacky // list of the same call.
271e3b55780SDimitry Andric Calls.count(Call)) {
27259850d08SRoman Divacky assert(!CheckingMode &&
27359850d08SRoman Divacky "CallGraphSCCPass did not update the CallGraph correctly!");
27459850d08SRoman Divacky
275d7f7719eSRoman Divacky // If this was an indirect call site, count it.
2765ca98fd9SDimitry Andric if (!I->second->getFunction())
277d7f7719eSRoman Divacky ++NumIndirectRemoved;
278d7f7719eSRoman Divacky else
279d7f7719eSRoman Divacky ++NumDirectRemoved;
280d7f7719eSRoman Divacky
281cfca06d7SDimitry Andric if (RemoveAndCheckForDone(I))
28259850d08SRoman Divacky break;
28359850d08SRoman Divacky continue;
28459850d08SRoman Divacky }
28559850d08SRoman Divacky
286cfca06d7SDimitry Andric assert(!Calls.count(Call) && "Call site occurs in node multiple times");
28767c32a98SDimitry Andric
288e6d15924SDimitry Andric if (Call) {
289e6d15924SDimitry Andric Function *Callee = Call->getCalledFunction();
29067c32a98SDimitry Andric // Ignore intrinsics because they're not really function calls.
29167c32a98SDimitry Andric if (!Callee || !(Callee->isIntrinsic()))
292cfca06d7SDimitry Andric Calls.insert(std::make_pair(Call, I->second));
29367c32a98SDimitry Andric }
29459850d08SRoman Divacky ++I;
29559850d08SRoman Divacky }
29659850d08SRoman Divacky
29759850d08SRoman Divacky // Loop over all of the instructions in the function, getting the callsites.
298d7f7719eSRoman Divacky // Keep track of the number of direct/indirect calls added.
299d7f7719eSRoman Divacky unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
300d7f7719eSRoman Divacky
30101095a5dSDimitry Andric for (BasicBlock &BB : *F)
30201095a5dSDimitry Andric for (Instruction &I : BB) {
303e6d15924SDimitry Andric auto *Call = dyn_cast<CallBase>(&I);
304e6d15924SDimitry Andric if (!Call)
305e6d15924SDimitry Andric continue;
306e6d15924SDimitry Andric Function *Callee = Call->getCalledFunction();
307e6d15924SDimitry Andric if (Callee && Callee->isIntrinsic())
308e6d15924SDimitry Andric continue;
30959850d08SRoman Divacky
310cfca06d7SDimitry Andric // If we are not in checking mode, insert potential callback calls as
311cfca06d7SDimitry Andric // references. This is not a requirement but helps to iterate over the
312cfca06d7SDimitry Andric // functions in the right order.
313cfca06d7SDimitry Andric if (!CheckingMode) {
314cfca06d7SDimitry Andric forEachCallbackFunction(*Call, [&](Function *CB) {
315cfca06d7SDimitry Andric CGN->addCalledFunction(nullptr, CG.getOrInsertFunction(CB));
316cfca06d7SDimitry Andric });
317cfca06d7SDimitry Andric }
318cfca06d7SDimitry Andric
31959850d08SRoman Divacky // If this call site already existed in the callgraph, just verify it
320e6d15924SDimitry Andric // matches up to expectations and remove it from Calls.
32159850d08SRoman Divacky DenseMap<Value *, CallGraphNode *>::iterator ExistingIt =
322e6d15924SDimitry Andric Calls.find(Call);
323e6d15924SDimitry Andric if (ExistingIt != Calls.end()) {
32459850d08SRoman Divacky CallGraphNode *ExistingNode = ExistingIt->second;
32559850d08SRoman Divacky
326e6d15924SDimitry Andric // Remove from Calls since we have now seen it.
327e6d15924SDimitry Andric Calls.erase(ExistingIt);
32859850d08SRoman Divacky
32959850d08SRoman Divacky // Verify that the callee is right.
330e6d15924SDimitry Andric if (ExistingNode->getFunction() == Call->getCalledFunction())
33159850d08SRoman Divacky continue;
33259850d08SRoman Divacky
33359850d08SRoman Divacky // If we are in checking mode, we are not allowed to actually mutate
33459850d08SRoman Divacky // the callgraph. If this is a case where we can infer that the
33559850d08SRoman Divacky // callgraph is less precise than it could be (e.g. an indirect call
33659850d08SRoman Divacky // site could be turned direct), don't reject it in checking mode, and
33759850d08SRoman Divacky // don't tweak it to be more precise.
338e6d15924SDimitry Andric if (CheckingMode && Call->getCalledFunction() &&
3395ca98fd9SDimitry Andric ExistingNode->getFunction() == nullptr)
34059850d08SRoman Divacky continue;
34159850d08SRoman Divacky
34259850d08SRoman Divacky assert(!CheckingMode &&
34359850d08SRoman Divacky "CallGraphSCCPass did not update the CallGraph correctly!");
34459850d08SRoman Divacky
34559850d08SRoman Divacky // If not, we either went from a direct call to indirect, indirect to
34659850d08SRoman Divacky // direct, or direct to different direct.
34759850d08SRoman Divacky CallGraphNode *CalleeNode;
348e6d15924SDimitry Andric if (Function *Callee = Call->getCalledFunction()) {
34959850d08SRoman Divacky CalleeNode = CG.getOrInsertFunction(Callee);
350d7f7719eSRoman Divacky // Keep track of whether we turned an indirect call into a direct
351d7f7719eSRoman Divacky // one.
3525ca98fd9SDimitry Andric if (!ExistingNode->getFunction()) {
353d7f7719eSRoman Divacky DevirtualizedCall = true;
354eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
355d7f7719eSRoman Divacky << Callee->getName() << "'\n");
356d7f7719eSRoman Divacky }
357d7f7719eSRoman Divacky } else {
35859850d08SRoman Divacky CalleeNode = CG.getCallsExternalNode();
359d7f7719eSRoman Divacky }
36059850d08SRoman Divacky
36159850d08SRoman Divacky // Update the edge target in CGN.
362e6d15924SDimitry Andric CGN->replaceCallEdge(*Call, *Call, CalleeNode);
36359850d08SRoman Divacky MadeChange = true;
36459850d08SRoman Divacky continue;
36559850d08SRoman Divacky }
36659850d08SRoman Divacky
36759850d08SRoman Divacky assert(!CheckingMode &&
36859850d08SRoman Divacky "CallGraphSCCPass did not update the CallGraph correctly!");
36959850d08SRoman Divacky
370d7f7719eSRoman Divacky // If the call site didn't exist in the CGN yet, add it.
37159850d08SRoman Divacky CallGraphNode *CalleeNode;
372e6d15924SDimitry Andric if (Function *Callee = Call->getCalledFunction()) {
37359850d08SRoman Divacky CalleeNode = CG.getOrInsertFunction(Callee);
374d7f7719eSRoman Divacky ++NumDirectAdded;
375d7f7719eSRoman Divacky } else {
37659850d08SRoman Divacky CalleeNode = CG.getCallsExternalNode();
377d7f7719eSRoman Divacky ++NumIndirectAdded;
378d7f7719eSRoman Divacky }
37959850d08SRoman Divacky
380e6d15924SDimitry Andric CGN->addCalledFunction(Call, CalleeNode);
38159850d08SRoman Divacky MadeChange = true;
38259850d08SRoman Divacky }
38359850d08SRoman Divacky
384d7f7719eSRoman Divacky // We scanned the old callgraph node, removing invalidated call sites and
385d7f7719eSRoman Divacky // then added back newly found call sites. One thing that can happen is
386d7f7719eSRoman Divacky // that an old indirect call site was deleted and replaced with a new direct
387d7f7719eSRoman Divacky // call. In this case, we have devirtualized a call, and CGSCCPM would like
388d7f7719eSRoman Divacky // to iteratively optimize the new code. Unfortunately, we don't really
389d7f7719eSRoman Divacky // have a great way to detect when this happens. As an approximation, we
390d7f7719eSRoman Divacky // just look at whether the number of indirect calls is reduced and the
391d7f7719eSRoman Divacky // number of direct calls is increased. There are tons of ways to fool this
392d7f7719eSRoman Divacky // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
393d7f7719eSRoman Divacky // direct call) but this is close enough.
394d7f7719eSRoman Divacky if (NumIndirectRemoved > NumIndirectAdded &&
395d7f7719eSRoman Divacky NumDirectRemoved < NumDirectAdded)
396d7f7719eSRoman Divacky DevirtualizedCall = true;
397d7f7719eSRoman Divacky
39859850d08SRoman Divacky // After scanning this function, if we still have entries in callsites, then
399a303c417SDimitry Andric // they are dangling pointers. WeakTrackingVH should save us for this, so
400a303c417SDimitry Andric // abort if
40159850d08SRoman Divacky // this happens.
402e6d15924SDimitry Andric assert(Calls.empty() && "Dangling pointers found in call sites map");
40359850d08SRoman Divacky
40459850d08SRoman Divacky // Periodically do an explicit clear to remove tombstones when processing
40559850d08SRoman Divacky // large scc's.
406d7f7719eSRoman Divacky if ((FunctionNo & 15) == 15)
407e6d15924SDimitry Andric Calls.clear();
40859850d08SRoman Divacky }
40959850d08SRoman Divacky
410eb11fae6SDimitry Andric LLVM_DEBUG(if (MadeChange) {
4111e7804dbSRoman Divacky dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
4125a5ac124SDimitry Andric for (CallGraphNode *CGN : CurSCC)
4135a5ac124SDimitry Andric CGN->dump();
414d7f7719eSRoman Divacky if (DevirtualizedCall)
415d7f7719eSRoman Divacky dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
41659850d08SRoman Divacky } else {
4171e7804dbSRoman Divacky dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
418eb11fae6SDimitry Andric });
41930815c53SDimitry Andric (void)MadeChange;
420d7f7719eSRoman Divacky
421d7f7719eSRoman Divacky return DevirtualizedCall;
42259850d08SRoman Divacky }
42359850d08SRoman Divacky
4245a5ac124SDimitry Andric /// Execute the body of the entire pass manager on the specified SCC.
4255a5ac124SDimitry Andric /// This keeps track of whether a function pass devirtualizes
426d7f7719eSRoman Divacky /// any calls and returns it in DevirtualizedCall.
RunAllPassesOnSCC(CallGraphSCC & CurSCC,CallGraph & CG,bool & DevirtualizedCall)427d7f7719eSRoman Divacky bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
428d7f7719eSRoman Divacky bool &DevirtualizedCall) {
429d7f7719eSRoman Divacky bool Changed = false;
43059850d08SRoman Divacky
4315a5ac124SDimitry Andric // Keep track of whether the callgraph is known to be up-to-date or not.
4325a5ac124SDimitry Andric // The CGSSC pass manager runs two types of passes:
43359850d08SRoman Divacky // CallGraphSCC Passes and other random function passes. Because other
43459850d08SRoman Divacky // random function passes are not CallGraph aware, they may clobber the
43559850d08SRoman Divacky // call graph by introducing new calls or deleting other ones. This flag
43659850d08SRoman Divacky // is set to false when we run a function pass so that we know to clean up
43759850d08SRoman Divacky // the callgraph when we need to run a CGSCCPass again.
43859850d08SRoman Divacky bool CallGraphUpToDate = true;
43959850d08SRoman Divacky
44059850d08SRoman Divacky // Run all passes on current SCC.
44159850d08SRoman Divacky for (unsigned PassNo = 0, e = getNumContainedPasses();
44259850d08SRoman Divacky PassNo != e; ++PassNo) {
44359850d08SRoman Divacky Pass *P = getContainedPass(PassNo);
44459850d08SRoman Divacky
44559850d08SRoman Divacky // If we're in -debug-pass=Executions mode, construct the SCC node list,
44659850d08SRoman Divacky // otherwise avoid constructing this string as it is expensive.
44759850d08SRoman Divacky if (isPassDebuggingExecutionsOrMore()) {
44859850d08SRoman Divacky std::string Functions;
44959850d08SRoman Divacky #ifndef NDEBUG
45059850d08SRoman Divacky raw_string_ostream OS(Functions);
451344a3780SDimitry Andric ListSeparator LS;
452344a3780SDimitry Andric for (const CallGraphNode *CGN : CurSCC) {
453344a3780SDimitry Andric OS << LS;
454344a3780SDimitry Andric CGN->print(OS);
45559850d08SRoman Divacky }
45659850d08SRoman Divacky OS.flush();
45759850d08SRoman Divacky #endif
45859850d08SRoman Divacky dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
45959850d08SRoman Divacky }
460009b1c42SEd Schouten dumpRequiredSet(P);
461009b1c42SEd Schouten
462009b1c42SEd Schouten initializeAnalysisImpl(P);
463009b1c42SEd Schouten
464b60736ecSDimitry Andric #ifdef EXPENSIVE_CHECKS
465145449b1SDimitry Andric uint64_t RefHash = P->structuralHash(CG.getModule());
466b60736ecSDimitry Andric #endif
467009b1c42SEd Schouten
468b60736ecSDimitry Andric // Actually run this pass on the current SCC.
469b60736ecSDimitry Andric bool LocalChanged =
470b60736ecSDimitry Andric RunPassOnSCC(P, CurSCC, CG, CallGraphUpToDate, DevirtualizedCall);
471b60736ecSDimitry Andric
472b60736ecSDimitry Andric Changed |= LocalChanged;
473b60736ecSDimitry Andric
474b60736ecSDimitry Andric #ifdef EXPENSIVE_CHECKS
475145449b1SDimitry Andric if (!LocalChanged && (RefHash != P->structuralHash(CG.getModule()))) {
476b60736ecSDimitry Andric llvm::errs() << "Pass modifies its input and doesn't report it: "
477b60736ecSDimitry Andric << P->getPassName() << "\n";
478b60736ecSDimitry Andric llvm_unreachable("Pass modifies its input and doesn't report it");
479b60736ecSDimitry Andric }
480b60736ecSDimitry Andric #endif
481b60736ecSDimitry Andric if (LocalChanged)
482009b1c42SEd Schouten dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
483009b1c42SEd Schouten dumpPreservedSet(P);
484009b1c42SEd Schouten
485009b1c42SEd Schouten verifyPreservedAnalysis(P);
486b60736ecSDimitry Andric if (LocalChanged)
487009b1c42SEd Schouten removeNotPreservedAnalysis(P);
488009b1c42SEd Schouten recordAvailableAnalysis(P);
489009b1c42SEd Schouten removeDeadPasses(P, "", ON_CG_MSG);
490009b1c42SEd Schouten }
49159850d08SRoman Divacky
49259850d08SRoman Divacky // If the callgraph was left out of date (because the last pass run was a
49359850d08SRoman Divacky // functionpass), refresh it before we move on to the next SCC.
49459850d08SRoman Divacky if (!CallGraphUpToDate)
495d7f7719eSRoman Divacky DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
496d7f7719eSRoman Divacky return Changed;
497d7f7719eSRoman Divacky }
498d7f7719eSRoman Divacky
4995a5ac124SDimitry Andric /// Execute all of the passes scheduled for execution. Keep track of
500d7f7719eSRoman Divacky /// whether any of the passes modifies the module, and if so, return true.
runOnModule(Module & M)501d7f7719eSRoman Divacky bool CGPassManager::runOnModule(Module &M) {
5025ca98fd9SDimitry Andric CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
503d7f7719eSRoman Divacky bool Changed = doInitialization(CG);
504d7f7719eSRoman Divacky
505d7f7719eSRoman Divacky // Walk the callgraph in bottom-up SCC order.
506d7f7719eSRoman Divacky scc_iterator<CallGraph*> CGI = scc_begin(&CG);
507d7f7719eSRoman Divacky
50801095a5dSDimitry Andric CallGraphSCC CurSCC(CG, &CGI);
509d7f7719eSRoman Divacky while (!CGI.isAtEnd()) {
510d7f7719eSRoman Divacky // Copy the current SCC and increment past it so that the pass can hack
511d7f7719eSRoman Divacky // on the SCC if it wants to without invalidating our iterator.
5125ca98fd9SDimitry Andric const std::vector<CallGraphNode *> &NodeVec = *CGI;
513b915e9e0SDimitry Andric CurSCC.initialize(NodeVec);
514d7f7719eSRoman Divacky ++CGI;
515d7f7719eSRoman Divacky
516d7f7719eSRoman Divacky // At the top level, we run all the passes in this pass manager on the
517d7f7719eSRoman Divacky // functions in this SCC. However, we support iterative compilation in the
518d7f7719eSRoman Divacky // case where a function pass devirtualizes a call to a function. For
519d7f7719eSRoman Divacky // example, it is very common for a function pass (often GVN or instcombine)
520d7f7719eSRoman Divacky // to eliminate the addressing that feeds into a call. With that improved
521d7f7719eSRoman Divacky // information, we would like the call to be an inline candidate, infer
522d7f7719eSRoman Divacky // mod-ref information etc.
523d7f7719eSRoman Divacky //
524d7f7719eSRoman Divacky // Because of this, we allow iteration up to a specified iteration count.
525d7f7719eSRoman Divacky // This only happens in the case of a devirtualized call, so we only burn
526d7f7719eSRoman Divacky // compile time in the case that we're making progress. We also have a hard
527d7f7719eSRoman Divacky // iteration count limit in case there is crazy code.
528d7f7719eSRoman Divacky unsigned Iteration = 0;
529d7f7719eSRoman Divacky bool DevirtualizedCall = false;
530d7f7719eSRoman Divacky do {
531eb11fae6SDimitry Andric LLVM_DEBUG(if (Iteration) dbgs()
532eb11fae6SDimitry Andric << " SCCPASSMGR: Re-visiting SCC, iteration #" << Iteration
533eb11fae6SDimitry Andric << '\n');
534d7f7719eSRoman Divacky DevirtualizedCall = false;
535d7f7719eSRoman Divacky Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
536b60736ecSDimitry Andric } while (Iteration++ < MaxDevirtIterations && DevirtualizedCall);
537d7f7719eSRoman Divacky
538d7f7719eSRoman Divacky if (DevirtualizedCall)
539eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after "
540eb11fae6SDimitry Andric << Iteration
541b60736ecSDimitry Andric << " times, due to -max-devirt-iterations\n");
542d7f7719eSRoman Divacky
543b5630dbaSDimitry Andric MaxSCCIterations.updateMax(Iteration);
544009b1c42SEd Schouten }
545009b1c42SEd Schouten Changed |= doFinalization(CG);
546009b1c42SEd Schouten return Changed;
547009b1c42SEd Schouten }
548009b1c42SEd Schouten
549009b1c42SEd Schouten /// Initialize CG
doInitialization(CallGraph & CG)550009b1c42SEd Schouten bool CGPassManager::doInitialization(CallGraph &CG) {
551009b1c42SEd Schouten bool Changed = false;
552989df958SRoman Divacky for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
553989df958SRoman Divacky if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
554989df958SRoman Divacky assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
555989df958SRoman Divacky "Invalid CGPassManager member");
556989df958SRoman Divacky Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
557009b1c42SEd Schouten } else {
558989df958SRoman Divacky Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
559009b1c42SEd Schouten }
560009b1c42SEd Schouten }
561009b1c42SEd Schouten return Changed;
562009b1c42SEd Schouten }
563009b1c42SEd Schouten
564009b1c42SEd Schouten /// Finalize CG
doFinalization(CallGraph & CG)565009b1c42SEd Schouten bool CGPassManager::doFinalization(CallGraph &CG) {
566009b1c42SEd Schouten bool Changed = false;
567989df958SRoman Divacky for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
568989df958SRoman Divacky if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
569989df958SRoman Divacky assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
570989df958SRoman Divacky "Invalid CGPassManager member");
571989df958SRoman Divacky Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
572009b1c42SEd Schouten } else {
573989df958SRoman Divacky Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
574009b1c42SEd Schouten }
575009b1c42SEd Schouten }
576009b1c42SEd Schouten return Changed;
577009b1c42SEd Schouten }
578009b1c42SEd Schouten
579d7f7719eSRoman Divacky //===----------------------------------------------------------------------===//
580d7f7719eSRoman Divacky // CallGraphSCC Implementation
581d7f7719eSRoman Divacky //===----------------------------------------------------------------------===//
582d7f7719eSRoman Divacky
5835a5ac124SDimitry Andric /// This informs the SCC and the pass manager that the specified
584d7f7719eSRoman Divacky /// Old node has been deleted, and New is to be used in its place.
ReplaceNode(CallGraphNode * Old,CallGraphNode * New)585d7f7719eSRoman Divacky void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
586d7f7719eSRoman Divacky assert(Old != New && "Should not replace node with self");
587d7f7719eSRoman Divacky for (unsigned i = 0; ; ++i) {
588d7f7719eSRoman Divacky assert(i != Nodes.size() && "Node not in SCC");
589d7f7719eSRoman Divacky if (Nodes[i] != Old) continue;
590cfca06d7SDimitry Andric if (New)
591d7f7719eSRoman Divacky Nodes[i] = New;
592cfca06d7SDimitry Andric else
593cfca06d7SDimitry Andric Nodes.erase(Nodes.begin() + i);
594d7f7719eSRoman Divacky break;
595b5efedafSRoman Divacky }
596b5efedafSRoman Divacky
597d7f7719eSRoman Divacky // Update the active scc_iterator so that it doesn't contain dangling
598d7f7719eSRoman Divacky // pointers to the old CallGraphNode.
599d7f7719eSRoman Divacky scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
600d7f7719eSRoman Divacky CGI->ReplaceNode(Old, New);
601d7f7719eSRoman Divacky }
602d7f7719eSRoman Divacky
DeleteNode(CallGraphNode * Old)603cfca06d7SDimitry Andric void CallGraphSCC::DeleteNode(CallGraphNode *Old) {
604cfca06d7SDimitry Andric ReplaceNode(Old, /*New=*/nullptr);
605cfca06d7SDimitry Andric }
606cfca06d7SDimitry Andric
607d7f7719eSRoman Divacky //===----------------------------------------------------------------------===//
608d7f7719eSRoman Divacky // CallGraphSCCPass Implementation
609d7f7719eSRoman Divacky //===----------------------------------------------------------------------===//
610d7f7719eSRoman Divacky
611009b1c42SEd Schouten /// Assign pass manager to manage this pass.
assignPassManager(PMStack & PMS,PassManagerType PreferredType)612009b1c42SEd Schouten void CallGraphSCCPass::assignPassManager(PMStack &PMS,
613009b1c42SEd Schouten PassManagerType PreferredType) {
614009b1c42SEd Schouten // Find CGPassManager
615009b1c42SEd Schouten while (!PMS.empty() &&
616009b1c42SEd Schouten PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
617009b1c42SEd Schouten PMS.pop();
618009b1c42SEd Schouten
619009b1c42SEd Schouten assert(!PMS.empty() && "Unable to handle Call Graph Pass");
620989df958SRoman Divacky CGPassManager *CGP;
621009b1c42SEd Schouten
622989df958SRoman Divacky if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
623989df958SRoman Divacky CGP = (CGPassManager*)PMS.top();
624989df958SRoman Divacky else {
625009b1c42SEd Schouten // Create new Call Graph SCC Pass Manager if it does not exist.
626009b1c42SEd Schouten assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
627009b1c42SEd Schouten PMDataManager *PMD = PMS.top();
628009b1c42SEd Schouten
629009b1c42SEd Schouten // [1] Create new Call Graph Pass Manager
63030815c53SDimitry Andric CGP = new CGPassManager();
631009b1c42SEd Schouten
632009b1c42SEd Schouten // [2] Set up new manager's top level manager
633009b1c42SEd Schouten PMTopLevelManager *TPM = PMD->getTopLevelManager();
634009b1c42SEd Schouten TPM->addIndirectPassManager(CGP);
635009b1c42SEd Schouten
636009b1c42SEd Schouten // [3] Assign manager to manage this new manager. This may create
637009b1c42SEd Schouten // and push new managers into PMS
638989df958SRoman Divacky Pass *P = CGP;
639009b1c42SEd Schouten TPM->schedulePass(P);
640009b1c42SEd Schouten
641009b1c42SEd Schouten // [4] Push new manager into PMS
642009b1c42SEd Schouten PMS.push(CGP);
643009b1c42SEd Schouten }
644009b1c42SEd Schouten
645009b1c42SEd Schouten CGP->add(this);
646009b1c42SEd Schouten }
647009b1c42SEd Schouten
6485a5ac124SDimitry Andric /// For this class, we declare that we require and preserve the call graph.
6495a5ac124SDimitry Andric /// If the derived class implements this method, it should
650009b1c42SEd Schouten /// always explicitly call the implementation here.
getAnalysisUsage(AnalysisUsage & AU) const651009b1c42SEd Schouten void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
6525ca98fd9SDimitry Andric AU.addRequired<CallGraphWrapperPass>();
6535ca98fd9SDimitry Andric AU.addPreserved<CallGraphWrapperPass>();
654009b1c42SEd Schouten }
655d7f7719eSRoman Divacky
656d7f7719eSRoman Divacky //===----------------------------------------------------------------------===//
657d7f7719eSRoman Divacky // PrintCallGraphPass Implementation
658d7f7719eSRoman Divacky //===----------------------------------------------------------------------===//
659d7f7719eSRoman Divacky
660d7f7719eSRoman Divacky namespace {
661044eb2f6SDimitry Andric
662d7f7719eSRoman Divacky /// PrintCallGraphPass - Print a Module corresponding to a call graph.
663d7f7719eSRoman Divacky ///
664d7f7719eSRoman Divacky class PrintCallGraphPass : public CallGraphSCCPass {
665d7f7719eSRoman Divacky std::string Banner;
666044eb2f6SDimitry Andric raw_ostream &OS; // raw_ostream to print on.
667d7f7719eSRoman Divacky
668d7f7719eSRoman Divacky public:
669d7f7719eSRoman Divacky static char ID;
670044eb2f6SDimitry Andric
PrintCallGraphPass(const std::string & B,raw_ostream & OS)671044eb2f6SDimitry Andric PrintCallGraphPass(const std::string &B, raw_ostream &OS)
672044eb2f6SDimitry Andric : CallGraphSCCPass(ID), Banner(B), OS(OS) {}
673d7f7719eSRoman Divacky
getAnalysisUsage(AnalysisUsage & AU) const6745ca98fd9SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
675d7f7719eSRoman Divacky AU.setPreservesAll();
676d7f7719eSRoman Divacky }
677d7f7719eSRoman Divacky
runOnSCC(CallGraphSCC & SCC)6785ca98fd9SDimitry Andric bool runOnSCC(CallGraphSCC &SCC) override {
6797c7aba6eSDimitry Andric bool BannerPrinted = false;
68071d5a254SDimitry Andric auto PrintBannerOnce = [&]() {
68171d5a254SDimitry Andric if (BannerPrinted)
68271d5a254SDimitry Andric return;
683044eb2f6SDimitry Andric OS << Banner;
68471d5a254SDimitry Andric BannerPrinted = true;
68571d5a254SDimitry Andric };
686d8e91e46SDimitry Andric
687d8e91e46SDimitry Andric bool NeedModule = llvm::forcePrintModuleIR();
688d8e91e46SDimitry Andric if (isFunctionInPrintList("*") && NeedModule) {
689d8e91e46SDimitry Andric PrintBannerOnce();
690d8e91e46SDimitry Andric OS << "\n";
691d8e91e46SDimitry Andric SCC.getCallGraph().getModule().print(OS, nullptr);
692d8e91e46SDimitry Andric return false;
693d8e91e46SDimitry Andric }
694d8e91e46SDimitry Andric bool FoundFunction = false;
6955a5ac124SDimitry Andric for (CallGraphNode *CGN : SCC) {
6967c7aba6eSDimitry Andric if (Function *F = CGN->getFunction()) {
6977c7aba6eSDimitry Andric if (!F->isDeclaration() && isFunctionInPrintList(F->getName())) {
698d8e91e46SDimitry Andric FoundFunction = true;
699d8e91e46SDimitry Andric if (!NeedModule) {
70071d5a254SDimitry Andric PrintBannerOnce();
701044eb2f6SDimitry Andric F->print(OS);
70271d5a254SDimitry Andric }
703d8e91e46SDimitry Andric }
704044eb2f6SDimitry Andric } else if (isFunctionInPrintList("*")) {
70571d5a254SDimitry Andric PrintBannerOnce();
706044eb2f6SDimitry Andric OS << "\nPrinting <null> Function\n";
7075ca98fd9SDimitry Andric }
70871d5a254SDimitry Andric }
709d8e91e46SDimitry Andric if (NeedModule && FoundFunction) {
710d8e91e46SDimitry Andric PrintBannerOnce();
711d8e91e46SDimitry Andric OS << "\n";
712d8e91e46SDimitry Andric SCC.getCallGraph().getModule().print(OS, nullptr);
713d8e91e46SDimitry Andric }
714d7f7719eSRoman Divacky return false;
715d7f7719eSRoman Divacky }
71671d5a254SDimitry Andric
getPassName() const71771d5a254SDimitry Andric StringRef getPassName() const override { return "Print CallGraph IR"; }
718d7f7719eSRoman Divacky };
719d7f7719eSRoman Divacky
720d7f7719eSRoman Divacky } // end anonymous namespace.
721d7f7719eSRoman Divacky
722d7f7719eSRoman Divacky char PrintCallGraphPass::ID = 0;
723d7f7719eSRoman Divacky
createPrinterPass(raw_ostream & OS,const std::string & Banner) const724044eb2f6SDimitry Andric Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &OS,
725d7f7719eSRoman Divacky const std::string &Banner) const {
726044eb2f6SDimitry Andric return new PrintCallGraphPass(Banner, OS);
727d7f7719eSRoman Divacky }
728d7f7719eSRoman Divacky
getDescription(const CallGraphSCC & SCC)729e6d15924SDimitry Andric static std::string getDescription(const CallGraphSCC &SCC) {
730e6d15924SDimitry Andric std::string Desc = "SCC (";
731344a3780SDimitry Andric ListSeparator LS;
732e6d15924SDimitry Andric for (CallGraphNode *CGN : SCC) {
733344a3780SDimitry Andric Desc += LS;
734e6d15924SDimitry Andric Function *F = CGN->getFunction();
735e6d15924SDimitry Andric if (F)
736e6d15924SDimitry Andric Desc += F->getName();
737e6d15924SDimitry Andric else
738e6d15924SDimitry Andric Desc += "<<null function>>";
739e6d15924SDimitry Andric }
740e6d15924SDimitry Andric Desc += ")";
741e6d15924SDimitry Andric return Desc;
742e6d15924SDimitry Andric }
743e6d15924SDimitry Andric
skipSCC(CallGraphSCC & SCC) const74401095a5dSDimitry Andric bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const {
745e6d15924SDimitry Andric OptPassGate &Gate =
746e6d15924SDimitry Andric SCC.getCallGraph().getModule().getContext().getOptPassGate();
747e3b55780SDimitry Andric return Gate.isEnabled() &&
748e3b55780SDimitry Andric !Gate.shouldRunPass(this->getPassName(), getDescription(SCC));
74901095a5dSDimitry Andric }
75001095a5dSDimitry Andric
75101095a5dSDimitry Andric char DummyCGSCCPass::ID = 0;
752044eb2f6SDimitry Andric
75301095a5dSDimitry Andric INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false,
75401095a5dSDimitry Andric false)
755