17adc3a2bSChandler Carruth //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===// 27adc3a2bSChandler Carruth // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67adc3a2bSChandler Carruth // 77adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 87adc3a2bSChandler Carruth // 97adc3a2bSChandler Carruth // This file implements the CallGraphSCCPass class, which is used for passes 107adc3a2bSChandler Carruth // which are implemented as bottom-up traversals on the call graph. Because 117adc3a2bSChandler Carruth // there may be cycles in the call graph, passes of this type operate on the 127adc3a2bSChandler Carruth // call-graph in SCC order: that is, they process function bottom-up, except for 137adc3a2bSChandler Carruth // recursive functions, which they process all at once. 147adc3a2bSChandler Carruth // 157adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 167adc3a2bSChandler Carruth 177adc3a2bSChandler Carruth #include "llvm/Analysis/CallGraphSCCPass.h" 18fa6434beSEugene Zelenko #include "llvm/ADT/DenseMap.h" 197adc3a2bSChandler Carruth #include "llvm/ADT/SCCIterator.h" 207adc3a2bSChandler Carruth #include "llvm/ADT/Statistic.h" 217adc3a2bSChandler Carruth #include "llvm/Analysis/CallGraph.h" 227adc3a2bSChandler Carruth #include "llvm/IR/Function.h" 23662e5686SFedor Sergeev #include "llvm/IR/IRPrintingPasses.h" 24fa6434beSEugene Zelenko #include "llvm/IR/Intrinsics.h" 257adc3a2bSChandler Carruth #include "llvm/IR/LLVMContext.h" 267adc3a2bSChandler Carruth #include "llvm/IR/LegacyPassManagers.h" 27fa6434beSEugene Zelenko #include "llvm/IR/Module.h" 28aa641a51SAndrew Kaylor #include "llvm/IR/OptBisect.h" 2943083111SFedor Sergeev #include "llvm/IR/PassTimingInfo.h" 30fa6434beSEugene Zelenko #include "llvm/Pass.h" 317adc3a2bSChandler Carruth #include "llvm/Support/CommandLine.h" 327adc3a2bSChandler Carruth #include "llvm/Support/Debug.h" 337adc3a2bSChandler Carruth #include "llvm/Support/Timer.h" 347adc3a2bSChandler Carruth #include "llvm/Support/raw_ostream.h" 35fa6434beSEugene Zelenko #include <cassert> 36fa6434beSEugene Zelenko #include <string> 37fa6434beSEugene Zelenko #include <utility> 38fa6434beSEugene Zelenko #include <vector> 39fa6434beSEugene Zelenko 407adc3a2bSChandler Carruth using namespace llvm; 417adc3a2bSChandler Carruth 427adc3a2bSChandler Carruth #define DEBUG_TYPE "cgscc-passmgr" 437adc3a2bSChandler Carruth 447adc3a2bSChandler Carruth static cl::opt<unsigned> 457adc3a2bSChandler Carruth MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4)); 467adc3a2bSChandler Carruth 477adc3a2bSChandler Carruth STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC"); 487adc3a2bSChandler Carruth 497adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 507adc3a2bSChandler Carruth // CGPassManager 517adc3a2bSChandler Carruth // 527adc3a2bSChandler Carruth /// CGPassManager manages FPPassManagers and CallGraphSCCPasses. 537adc3a2bSChandler Carruth 547adc3a2bSChandler Carruth namespace { 557adc3a2bSChandler Carruth 567adc3a2bSChandler Carruth class CGPassManager : public ModulePass, public PMDataManager { 577adc3a2bSChandler Carruth public: 587adc3a2bSChandler Carruth static char ID; 59fa6434beSEugene Zelenko 60fa6434beSEugene Zelenko explicit CGPassManager() : ModulePass(ID), PMDataManager() {} 617adc3a2bSChandler Carruth 627adc3a2bSChandler Carruth /// Execute all of the passes scheduled for execution. Keep track of 637adc3a2bSChandler Carruth /// whether any of the passes modifies the module, and if so, return true. 647adc3a2bSChandler Carruth bool runOnModule(Module &M) override; 657adc3a2bSChandler Carruth 667adc3a2bSChandler Carruth using ModulePass::doInitialization; 677adc3a2bSChandler Carruth using ModulePass::doFinalization; 687adc3a2bSChandler Carruth 697adc3a2bSChandler Carruth bool doInitialization(CallGraph &CG); 707adc3a2bSChandler Carruth bool doFinalization(CallGraph &CG); 717adc3a2bSChandler Carruth 727adc3a2bSChandler Carruth /// Pass Manager itself does not invalidate any analysis info. 737adc3a2bSChandler Carruth void getAnalysisUsage(AnalysisUsage &Info) const override { 747adc3a2bSChandler Carruth // CGPassManager walks SCC and it needs CallGraph. 757adc3a2bSChandler Carruth Info.addRequired<CallGraphWrapperPass>(); 767adc3a2bSChandler Carruth Info.setPreservesAll(); 777adc3a2bSChandler Carruth } 787adc3a2bSChandler Carruth 79117296c0SMehdi Amini StringRef getPassName() const override { return "CallGraph Pass Manager"; } 807adc3a2bSChandler Carruth 817adc3a2bSChandler Carruth PMDataManager *getAsPMDataManager() override { return this; } 827adc3a2bSChandler Carruth Pass *getAsPass() override { return this; } 837adc3a2bSChandler Carruth 847adc3a2bSChandler Carruth // Print passes managed by this manager 857adc3a2bSChandler Carruth void dumpPassStructure(unsigned Offset) override { 867adc3a2bSChandler Carruth errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n"; 877adc3a2bSChandler Carruth for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 887adc3a2bSChandler Carruth Pass *P = getContainedPass(Index); 897adc3a2bSChandler Carruth P->dumpPassStructure(Offset + 1); 907adc3a2bSChandler Carruth dumpLastUses(P, Offset+1); 917adc3a2bSChandler Carruth } 927adc3a2bSChandler Carruth } 937adc3a2bSChandler Carruth 947adc3a2bSChandler Carruth Pass *getContainedPass(unsigned N) { 957adc3a2bSChandler Carruth assert(N < PassVector.size() && "Pass number out of range!"); 967adc3a2bSChandler Carruth return static_cast<Pass *>(PassVector[N]); 977adc3a2bSChandler Carruth } 987adc3a2bSChandler Carruth 997adc3a2bSChandler Carruth PassManagerType getPassManagerType() const override { 1007adc3a2bSChandler Carruth return PMT_CallGraphPassManager; 1017adc3a2bSChandler Carruth } 1027adc3a2bSChandler Carruth 1037adc3a2bSChandler Carruth private: 1047adc3a2bSChandler Carruth bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, 1057adc3a2bSChandler Carruth bool &DevirtualizedCall); 1067adc3a2bSChandler Carruth 1077adc3a2bSChandler Carruth bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, 1087adc3a2bSChandler Carruth CallGraph &CG, bool &CallGraphUpToDate, 1097adc3a2bSChandler Carruth bool &DevirtualizedCall); 110c137c28cSMehdi Amini bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG, 1117adc3a2bSChandler Carruth bool IsCheckingMode); 1127adc3a2bSChandler Carruth }; 1137adc3a2bSChandler Carruth 1147adc3a2bSChandler Carruth } // end anonymous namespace. 1157adc3a2bSChandler Carruth 1167adc3a2bSChandler Carruth char CGPassManager::ID = 0; 1177adc3a2bSChandler Carruth 1187adc3a2bSChandler Carruth bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, 1197adc3a2bSChandler Carruth CallGraph &CG, bool &CallGraphUpToDate, 1207adc3a2bSChandler Carruth bool &DevirtualizedCall) { 1217adc3a2bSChandler Carruth bool Changed = false; 1227adc3a2bSChandler Carruth PMDataManager *PM = P->getAsPMDataManager(); 123e49374d0SJessica Paquette Module &M = CG.getModule(); 1247adc3a2bSChandler Carruth 1257adc3a2bSChandler Carruth if (!PM) { 1267adc3a2bSChandler Carruth CallGraphSCCPass *CGSP = (CallGraphSCCPass *)P; 1277adc3a2bSChandler Carruth if (!CallGraphUpToDate) { 1287adc3a2bSChandler Carruth DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); 1297adc3a2bSChandler Carruth CallGraphUpToDate = true; 1307adc3a2bSChandler Carruth } 1317adc3a2bSChandler Carruth 1327adc3a2bSChandler Carruth { 1331fc443b8SJessica Paquette unsigned InstrCount, SCCCount = 0; 134a0aa5b35SJessica Paquette StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; 135023e25adSXin Tong bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); 1367adc3a2bSChandler Carruth TimeRegion PassTimer(getPassTimer(CGSP)); 137023e25adSXin Tong if (EmitICRemark) 138a0aa5b35SJessica Paquette InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); 1397adc3a2bSChandler Carruth Changed = CGSP->runOnSCC(CurSCC); 140e49374d0SJessica Paquette 1411fc443b8SJessica Paquette if (EmitICRemark) { 1421fc443b8SJessica Paquette // FIXME: Add getInstructionCount to CallGraphSCC. 1431fc443b8SJessica Paquette SCCCount = M.getInstructionCount(); 1441fc443b8SJessica Paquette // Is there a difference in the number of instructions in the module? 1451fc443b8SJessica Paquette if (SCCCount != InstrCount) { 1461fc443b8SJessica Paquette // Yep. Emit a remark and update InstrCount. 1479a23c559SJessica Paquette int64_t Delta = 1489a23c559SJessica Paquette static_cast<int64_t>(SCCCount) - static_cast<int64_t>(InstrCount); 149a0aa5b35SJessica Paquette emitInstrCountChangedRemark(P, M, Delta, InstrCount, 150a0aa5b35SJessica Paquette FunctionToInstrCount); 1511fc443b8SJessica Paquette InstrCount = SCCCount; 1521fc443b8SJessica Paquette } 1531fc443b8SJessica Paquette } 1547adc3a2bSChandler Carruth } 1557adc3a2bSChandler Carruth 1567adc3a2bSChandler Carruth // After the CGSCCPass is done, when assertions are enabled, use 1577adc3a2bSChandler Carruth // RefreshCallGraph to verify that the callgraph was correctly updated. 1587adc3a2bSChandler Carruth #ifndef NDEBUG 1597adc3a2bSChandler Carruth if (Changed) 1607adc3a2bSChandler Carruth RefreshCallGraph(CurSCC, CG, true); 1617adc3a2bSChandler Carruth #endif 1627adc3a2bSChandler Carruth 1637adc3a2bSChandler Carruth return Changed; 1647adc3a2bSChandler Carruth } 1657adc3a2bSChandler Carruth 1667adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 1677adc3a2bSChandler Carruth "Invalid CGPassManager member"); 1687adc3a2bSChandler Carruth FPPassManager *FPP = (FPPassManager*)P; 1697adc3a2bSChandler Carruth 1707adc3a2bSChandler Carruth // Run pass P on all functions in the current SCC. 1717adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) { 1727adc3a2bSChandler Carruth if (Function *F = CGN->getFunction()) { 1737adc3a2bSChandler Carruth dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName()); 1747adc3a2bSChandler Carruth { 1757adc3a2bSChandler Carruth TimeRegion PassTimer(getPassTimer(FPP)); 1767adc3a2bSChandler Carruth Changed |= FPP->runOnFunction(*F); 1777adc3a2bSChandler Carruth } 1787adc3a2bSChandler Carruth F->getContext().yield(); 1797adc3a2bSChandler Carruth } 1807adc3a2bSChandler Carruth } 1817adc3a2bSChandler Carruth 1827adc3a2bSChandler Carruth // The function pass(es) modified the IR, they may have clobbered the 1837adc3a2bSChandler Carruth // callgraph. 1847adc3a2bSChandler Carruth if (Changed && CallGraphUpToDate) { 185d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " << P->getPassName() 186d34e60caSNicola Zaghen << '\n'); 1877adc3a2bSChandler Carruth CallGraphUpToDate = false; 1887adc3a2bSChandler Carruth } 1897adc3a2bSChandler Carruth return Changed; 1907adc3a2bSChandler Carruth } 1917adc3a2bSChandler Carruth 1927adc3a2bSChandler Carruth /// Scan the functions in the specified CFG and resync the 1937adc3a2bSChandler Carruth /// callgraph with the call sites found in it. This is used after 1947adc3a2bSChandler Carruth /// FunctionPasses have potentially munged the callgraph, and can be used after 1957adc3a2bSChandler Carruth /// CallGraphSCC passes to verify that they correctly updated the callgraph. 1967adc3a2bSChandler Carruth /// 1977adc3a2bSChandler Carruth /// This function returns true if it devirtualized an existing function call, 1987adc3a2bSChandler Carruth /// meaning it turned an indirect call into a direct call. This happens when 1997adc3a2bSChandler Carruth /// a function pass like GVN optimizes away stuff feeding the indirect call. 2007adc3a2bSChandler Carruth /// This never happens in checking mode. 201c137c28cSMehdi Amini bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG, 202c137c28cSMehdi Amini bool CheckingMode) { 203*ce3f75dfSChandler Carruth DenseMap<Value *, CallGraphNode *> Calls; 2047adc3a2bSChandler Carruth 205d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size() 2067adc3a2bSChandler Carruth << " nodes:\n"; 207d34e60caSNicola Zaghen for (CallGraphNode *CGN 208d34e60caSNicola Zaghen : CurSCC) CGN->dump();); 2097adc3a2bSChandler Carruth 2107adc3a2bSChandler Carruth bool MadeChange = false; 2117adc3a2bSChandler Carruth bool DevirtualizedCall = false; 2127adc3a2bSChandler Carruth 2137adc3a2bSChandler Carruth // Scan all functions in the SCC. 2147adc3a2bSChandler Carruth unsigned FunctionNo = 0; 2157adc3a2bSChandler Carruth for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end(); 2167adc3a2bSChandler Carruth SCCIdx != E; ++SCCIdx, ++FunctionNo) { 2177adc3a2bSChandler Carruth CallGraphNode *CGN = *SCCIdx; 2187adc3a2bSChandler Carruth Function *F = CGN->getFunction(); 2197adc3a2bSChandler Carruth if (!F || F->isDeclaration()) continue; 2207adc3a2bSChandler Carruth 2217adc3a2bSChandler Carruth // Walk the function body looking for call sites. Sync up the call sites in 2227adc3a2bSChandler Carruth // CGN with those actually in the function. 2237adc3a2bSChandler Carruth 2247adc3a2bSChandler Carruth // Keep track of the number of direct and indirect calls that were 2257adc3a2bSChandler Carruth // invalidated and removed. 2267adc3a2bSChandler Carruth unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0; 2277adc3a2bSChandler Carruth 2287adc3a2bSChandler Carruth // Get the set of call sites currently in the function. 2297adc3a2bSChandler Carruth for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) { 2307adc3a2bSChandler Carruth // If this call site is null, then the function pass deleted the call 231e6bca0eeSSanjoy Das // entirely and the WeakTrackingVH nulled it out. 232*ce3f75dfSChandler Carruth auto *Call = dyn_cast_or_null<CallBase>(I->first); 2337adc3a2bSChandler Carruth if (!I->first || 2347adc3a2bSChandler Carruth // If we've already seen this call site, then the FunctionPass RAUW'd 2357adc3a2bSChandler Carruth // one call with another, which resulted in two "uses" in the edge 2367adc3a2bSChandler Carruth // list of the same call. 237*ce3f75dfSChandler Carruth Calls.count(I->first) || 2387adc3a2bSChandler Carruth 2397adc3a2bSChandler Carruth // If the call edge is not from a call or invoke, or it is a 2407adc3a2bSChandler Carruth // instrinsic call, then the function pass RAUW'd a call with 2417adc3a2bSChandler Carruth // another value. This can happen when constant folding happens 2427adc3a2bSChandler Carruth // of well known functions etc. 243*ce3f75dfSChandler Carruth !Call || 244*ce3f75dfSChandler Carruth (Call->getCalledFunction() && 245*ce3f75dfSChandler Carruth Call->getCalledFunction()->isIntrinsic() && 246*ce3f75dfSChandler Carruth Intrinsic::isLeaf(Call->getCalledFunction()->getIntrinsicID()))) { 2477adc3a2bSChandler Carruth assert(!CheckingMode && 2487adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 2497adc3a2bSChandler Carruth 2507adc3a2bSChandler Carruth // If this was an indirect call site, count it. 2517adc3a2bSChandler Carruth if (!I->second->getFunction()) 2527adc3a2bSChandler Carruth ++NumIndirectRemoved; 2537adc3a2bSChandler Carruth else 2547adc3a2bSChandler Carruth ++NumDirectRemoved; 2557adc3a2bSChandler Carruth 2567adc3a2bSChandler Carruth // Just remove the edge from the set of callees, keep track of whether 2577adc3a2bSChandler Carruth // I points to the last element of the vector. 2587adc3a2bSChandler Carruth bool WasLast = I + 1 == E; 2597adc3a2bSChandler Carruth CGN->removeCallEdge(I); 2607adc3a2bSChandler Carruth 2617adc3a2bSChandler Carruth // If I pointed to the last element of the vector, we have to bail out: 2627adc3a2bSChandler Carruth // iterator checking rejects comparisons of the resultant pointer with 2637adc3a2bSChandler Carruth // end. 2647adc3a2bSChandler Carruth if (WasLast) 2657adc3a2bSChandler Carruth break; 2667adc3a2bSChandler Carruth E = CGN->end(); 2677adc3a2bSChandler Carruth continue; 2687adc3a2bSChandler Carruth } 2697adc3a2bSChandler Carruth 270*ce3f75dfSChandler Carruth assert(!Calls.count(I->first) && 2717adc3a2bSChandler Carruth "Call site occurs in node multiple times"); 2727adc3a2bSChandler Carruth 273*ce3f75dfSChandler Carruth if (Call) { 274*ce3f75dfSChandler Carruth Function *Callee = Call->getCalledFunction(); 2757adc3a2bSChandler Carruth // Ignore intrinsics because they're not really function calls. 2767adc3a2bSChandler Carruth if (!Callee || !(Callee->isIntrinsic())) 277*ce3f75dfSChandler Carruth Calls.insert(std::make_pair(I->first, I->second)); 2787adc3a2bSChandler Carruth } 2797adc3a2bSChandler Carruth ++I; 2807adc3a2bSChandler Carruth } 2817adc3a2bSChandler Carruth 2827adc3a2bSChandler Carruth // Loop over all of the instructions in the function, getting the callsites. 2837adc3a2bSChandler Carruth // Keep track of the number of direct/indirect calls added. 2847adc3a2bSChandler Carruth unsigned NumDirectAdded = 0, NumIndirectAdded = 0; 2857adc3a2bSChandler Carruth 286aa209150SBenjamin Kramer for (BasicBlock &BB : *F) 287aa209150SBenjamin Kramer for (Instruction &I : BB) { 288*ce3f75dfSChandler Carruth auto *Call = dyn_cast<CallBase>(&I); 289*ce3f75dfSChandler Carruth if (!Call) 290*ce3f75dfSChandler Carruth continue; 291*ce3f75dfSChandler Carruth Function *Callee = Call->getCalledFunction(); 292*ce3f75dfSChandler Carruth if (Callee && Callee->isIntrinsic()) 293*ce3f75dfSChandler Carruth continue; 2947adc3a2bSChandler Carruth 2957adc3a2bSChandler Carruth // If this call site already existed in the callgraph, just verify it 296*ce3f75dfSChandler Carruth // matches up to expectations and remove it from Calls. 2977adc3a2bSChandler Carruth DenseMap<Value *, CallGraphNode *>::iterator ExistingIt = 298*ce3f75dfSChandler Carruth Calls.find(Call); 299*ce3f75dfSChandler Carruth if (ExistingIt != Calls.end()) { 3007adc3a2bSChandler Carruth CallGraphNode *ExistingNode = ExistingIt->second; 3017adc3a2bSChandler Carruth 302*ce3f75dfSChandler Carruth // Remove from Calls since we have now seen it. 303*ce3f75dfSChandler Carruth Calls.erase(ExistingIt); 3047adc3a2bSChandler Carruth 3057adc3a2bSChandler Carruth // Verify that the callee is right. 306*ce3f75dfSChandler Carruth if (ExistingNode->getFunction() == Call->getCalledFunction()) 3077adc3a2bSChandler Carruth continue; 3087adc3a2bSChandler Carruth 3097adc3a2bSChandler Carruth // If we are in checking mode, we are not allowed to actually mutate 3107adc3a2bSChandler Carruth // the callgraph. If this is a case where we can infer that the 3117adc3a2bSChandler Carruth // callgraph is less precise than it could be (e.g. an indirect call 3127adc3a2bSChandler Carruth // site could be turned direct), don't reject it in checking mode, and 3137adc3a2bSChandler Carruth // don't tweak it to be more precise. 314*ce3f75dfSChandler Carruth if (CheckingMode && Call->getCalledFunction() && 3157adc3a2bSChandler Carruth ExistingNode->getFunction() == nullptr) 3167adc3a2bSChandler Carruth continue; 3177adc3a2bSChandler Carruth 3187adc3a2bSChandler Carruth assert(!CheckingMode && 3197adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 3207adc3a2bSChandler Carruth 3217adc3a2bSChandler Carruth // If not, we either went from a direct call to indirect, indirect to 3227adc3a2bSChandler Carruth // direct, or direct to different direct. 3237adc3a2bSChandler Carruth CallGraphNode *CalleeNode; 324*ce3f75dfSChandler Carruth if (Function *Callee = Call->getCalledFunction()) { 3257adc3a2bSChandler Carruth CalleeNode = CG.getOrInsertFunction(Callee); 3267adc3a2bSChandler Carruth // Keep track of whether we turned an indirect call into a direct 3277adc3a2bSChandler Carruth // one. 3287adc3a2bSChandler Carruth if (!ExistingNode->getFunction()) { 3297adc3a2bSChandler Carruth DevirtualizedCall = true; 330d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '" 3317adc3a2bSChandler Carruth << Callee->getName() << "'\n"); 3327adc3a2bSChandler Carruth } 3337adc3a2bSChandler Carruth } else { 3347adc3a2bSChandler Carruth CalleeNode = CG.getCallsExternalNode(); 3357adc3a2bSChandler Carruth } 3367adc3a2bSChandler Carruth 3377adc3a2bSChandler Carruth // Update the edge target in CGN. 338*ce3f75dfSChandler Carruth CGN->replaceCallEdge(*Call, *Call, CalleeNode); 3397adc3a2bSChandler Carruth MadeChange = true; 3407adc3a2bSChandler Carruth continue; 3417adc3a2bSChandler Carruth } 3427adc3a2bSChandler Carruth 3437adc3a2bSChandler Carruth assert(!CheckingMode && 3447adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 3457adc3a2bSChandler Carruth 3467adc3a2bSChandler Carruth // If the call site didn't exist in the CGN yet, add it. 3477adc3a2bSChandler Carruth CallGraphNode *CalleeNode; 348*ce3f75dfSChandler Carruth if (Function *Callee = Call->getCalledFunction()) { 3497adc3a2bSChandler Carruth CalleeNode = CG.getOrInsertFunction(Callee); 3507adc3a2bSChandler Carruth ++NumDirectAdded; 3517adc3a2bSChandler Carruth } else { 3527adc3a2bSChandler Carruth CalleeNode = CG.getCallsExternalNode(); 3537adc3a2bSChandler Carruth ++NumIndirectAdded; 3547adc3a2bSChandler Carruth } 3557adc3a2bSChandler Carruth 356*ce3f75dfSChandler Carruth CGN->addCalledFunction(Call, CalleeNode); 3577adc3a2bSChandler Carruth MadeChange = true; 3587adc3a2bSChandler Carruth } 3597adc3a2bSChandler Carruth 3607adc3a2bSChandler Carruth // We scanned the old callgraph node, removing invalidated call sites and 3617adc3a2bSChandler Carruth // then added back newly found call sites. One thing that can happen is 3627adc3a2bSChandler Carruth // that an old indirect call site was deleted and replaced with a new direct 3637adc3a2bSChandler Carruth // call. In this case, we have devirtualized a call, and CGSCCPM would like 3647adc3a2bSChandler Carruth // to iteratively optimize the new code. Unfortunately, we don't really 3657adc3a2bSChandler Carruth // have a great way to detect when this happens. As an approximation, we 3667adc3a2bSChandler Carruth // just look at whether the number of indirect calls is reduced and the 3677adc3a2bSChandler Carruth // number of direct calls is increased. There are tons of ways to fool this 3687adc3a2bSChandler Carruth // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a 3697adc3a2bSChandler Carruth // direct call) but this is close enough. 3707adc3a2bSChandler Carruth if (NumIndirectRemoved > NumIndirectAdded && 3717adc3a2bSChandler Carruth NumDirectRemoved < NumDirectAdded) 3727adc3a2bSChandler Carruth DevirtualizedCall = true; 3737adc3a2bSChandler Carruth 3747adc3a2bSChandler Carruth // After scanning this function, if we still have entries in callsites, then 375e6bca0eeSSanjoy Das // they are dangling pointers. WeakTrackingVH should save us for this, so 376e6bca0eeSSanjoy Das // abort if 3772cbeb00fSSanjoy Das // this happens. 378*ce3f75dfSChandler Carruth assert(Calls.empty() && "Dangling pointers found in call sites map"); 3797adc3a2bSChandler Carruth 3807adc3a2bSChandler Carruth // Periodically do an explicit clear to remove tombstones when processing 3817adc3a2bSChandler Carruth // large scc's. 3827adc3a2bSChandler Carruth if ((FunctionNo & 15) == 15) 383*ce3f75dfSChandler Carruth Calls.clear(); 3847adc3a2bSChandler Carruth } 3857adc3a2bSChandler Carruth 386d34e60caSNicola Zaghen LLVM_DEBUG(if (MadeChange) { 3877adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n"; 3887adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) 3897adc3a2bSChandler Carruth CGN->dump(); 3907adc3a2bSChandler Carruth if (DevirtualizedCall) 3917adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n"; 3927adc3a2bSChandler Carruth } else { 3937adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n"; 394d34e60caSNicola Zaghen }); 3957adc3a2bSChandler Carruth (void)MadeChange; 3967adc3a2bSChandler Carruth 3977adc3a2bSChandler Carruth return DevirtualizedCall; 3987adc3a2bSChandler Carruth } 3997adc3a2bSChandler Carruth 4007adc3a2bSChandler Carruth /// Execute the body of the entire pass manager on the specified SCC. 4017adc3a2bSChandler Carruth /// This keeps track of whether a function pass devirtualizes 4027adc3a2bSChandler Carruth /// any calls and returns it in DevirtualizedCall. 4037adc3a2bSChandler Carruth bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, 4047adc3a2bSChandler Carruth bool &DevirtualizedCall) { 4057adc3a2bSChandler Carruth bool Changed = false; 4067adc3a2bSChandler Carruth 4077adc3a2bSChandler Carruth // Keep track of whether the callgraph is known to be up-to-date or not. 4087adc3a2bSChandler Carruth // The CGSSC pass manager runs two types of passes: 4097adc3a2bSChandler Carruth // CallGraphSCC Passes and other random function passes. Because other 4107adc3a2bSChandler Carruth // random function passes are not CallGraph aware, they may clobber the 4117adc3a2bSChandler Carruth // call graph by introducing new calls or deleting other ones. This flag 4127adc3a2bSChandler Carruth // is set to false when we run a function pass so that we know to clean up 4137adc3a2bSChandler Carruth // the callgraph when we need to run a CGSCCPass again. 4147adc3a2bSChandler Carruth bool CallGraphUpToDate = true; 4157adc3a2bSChandler Carruth 4167adc3a2bSChandler Carruth // Run all passes on current SCC. 4177adc3a2bSChandler Carruth for (unsigned PassNo = 0, e = getNumContainedPasses(); 4187adc3a2bSChandler Carruth PassNo != e; ++PassNo) { 4197adc3a2bSChandler Carruth Pass *P = getContainedPass(PassNo); 4207adc3a2bSChandler Carruth 4217adc3a2bSChandler Carruth // If we're in -debug-pass=Executions mode, construct the SCC node list, 4227adc3a2bSChandler Carruth // otherwise avoid constructing this string as it is expensive. 4237adc3a2bSChandler Carruth if (isPassDebuggingExecutionsOrMore()) { 4247adc3a2bSChandler Carruth std::string Functions; 4257adc3a2bSChandler Carruth #ifndef NDEBUG 4267adc3a2bSChandler Carruth raw_string_ostream OS(Functions); 4277adc3a2bSChandler Carruth for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end(); 4287adc3a2bSChandler Carruth I != E; ++I) { 4297adc3a2bSChandler Carruth if (I != CurSCC.begin()) OS << ", "; 4307adc3a2bSChandler Carruth (*I)->print(OS); 4317adc3a2bSChandler Carruth } 4327adc3a2bSChandler Carruth OS.flush(); 4337adc3a2bSChandler Carruth #endif 4347adc3a2bSChandler Carruth dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions); 4357adc3a2bSChandler Carruth } 4367adc3a2bSChandler Carruth dumpRequiredSet(P); 4377adc3a2bSChandler Carruth 4387adc3a2bSChandler Carruth initializeAnalysisImpl(P); 4397adc3a2bSChandler Carruth 4407adc3a2bSChandler Carruth // Actually run this pass on the current SCC. 4417adc3a2bSChandler Carruth Changed |= RunPassOnSCC(P, CurSCC, CG, 4427adc3a2bSChandler Carruth CallGraphUpToDate, DevirtualizedCall); 4437adc3a2bSChandler Carruth 4447adc3a2bSChandler Carruth if (Changed) 4457adc3a2bSChandler Carruth dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, ""); 4467adc3a2bSChandler Carruth dumpPreservedSet(P); 4477adc3a2bSChandler Carruth 4487adc3a2bSChandler Carruth verifyPreservedAnalysis(P); 4497adc3a2bSChandler Carruth removeNotPreservedAnalysis(P); 4507adc3a2bSChandler Carruth recordAvailableAnalysis(P); 4517adc3a2bSChandler Carruth removeDeadPasses(P, "", ON_CG_MSG); 4527adc3a2bSChandler Carruth } 4537adc3a2bSChandler Carruth 4547adc3a2bSChandler Carruth // If the callgraph was left out of date (because the last pass run was a 4557adc3a2bSChandler Carruth // functionpass), refresh it before we move on to the next SCC. 4567adc3a2bSChandler Carruth if (!CallGraphUpToDate) 4577adc3a2bSChandler Carruth DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); 4587adc3a2bSChandler Carruth return Changed; 4597adc3a2bSChandler Carruth } 4607adc3a2bSChandler Carruth 4617adc3a2bSChandler Carruth /// Execute all of the passes scheduled for execution. Keep track of 4627adc3a2bSChandler Carruth /// whether any of the passes modifies the module, and if so, return true. 4637adc3a2bSChandler Carruth bool CGPassManager::runOnModule(Module &M) { 4647adc3a2bSChandler Carruth CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); 4657adc3a2bSChandler Carruth bool Changed = doInitialization(CG); 4667adc3a2bSChandler Carruth 4677adc3a2bSChandler Carruth // Walk the callgraph in bottom-up SCC order. 468ca8c2f76SEvgeniy Stepanov scc_iterator<CallGraph*> CGI = scc_begin(&CG); 4697adc3a2bSChandler Carruth 470aa641a51SAndrew Kaylor CallGraphSCC CurSCC(CG, &CGI); 4717adc3a2bSChandler Carruth while (!CGI.isAtEnd()) { 4727adc3a2bSChandler Carruth // Copy the current SCC and increment past it so that the pass can hack 4737adc3a2bSChandler Carruth // on the SCC if it wants to without invalidating our iterator. 474ca8c2f76SEvgeniy Stepanov const std::vector<CallGraphNode *> &NodeVec = *CGI; 4751665d863SDavid Majnemer CurSCC.initialize(NodeVec); 4767adc3a2bSChandler Carruth ++CGI; 4777adc3a2bSChandler Carruth 4787adc3a2bSChandler Carruth // At the top level, we run all the passes in this pass manager on the 4797adc3a2bSChandler Carruth // functions in this SCC. However, we support iterative compilation in the 4807adc3a2bSChandler Carruth // case where a function pass devirtualizes a call to a function. For 4817adc3a2bSChandler Carruth // example, it is very common for a function pass (often GVN or instcombine) 4827adc3a2bSChandler Carruth // to eliminate the addressing that feeds into a call. With that improved 4837adc3a2bSChandler Carruth // information, we would like the call to be an inline candidate, infer 4847adc3a2bSChandler Carruth // mod-ref information etc. 4857adc3a2bSChandler Carruth // 4867adc3a2bSChandler Carruth // Because of this, we allow iteration up to a specified iteration count. 4877adc3a2bSChandler Carruth // This only happens in the case of a devirtualized call, so we only burn 4887adc3a2bSChandler Carruth // compile time in the case that we're making progress. We also have a hard 4897adc3a2bSChandler Carruth // iteration count limit in case there is crazy code. 4907adc3a2bSChandler Carruth unsigned Iteration = 0; 4917adc3a2bSChandler Carruth bool DevirtualizedCall = false; 4927adc3a2bSChandler Carruth do { 493d34e60caSNicola Zaghen LLVM_DEBUG(if (Iteration) dbgs() 494d34e60caSNicola Zaghen << " SCCPASSMGR: Re-visiting SCC, iteration #" << Iteration 495d34e60caSNicola Zaghen << '\n'); 4967adc3a2bSChandler Carruth DevirtualizedCall = false; 4977adc3a2bSChandler Carruth Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall); 4987adc3a2bSChandler Carruth } while (Iteration++ < MaxIterations && DevirtualizedCall); 4997adc3a2bSChandler Carruth 5007adc3a2bSChandler Carruth if (DevirtualizedCall) 501ca8c2f76SEvgeniy Stepanov LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " 502ca8c2f76SEvgeniy Stepanov << Iteration 5037adc3a2bSChandler Carruth << " times, due to -max-cg-scc-iterations\n"); 5047adc3a2bSChandler Carruth 5058a950275SCraig Topper MaxSCCIterations.updateMax(Iteration); 506ca8c2f76SEvgeniy Stepanov } 507ca8c2f76SEvgeniy Stepanov Changed |= doFinalization(CG); 5087adc3a2bSChandler Carruth return Changed; 5097adc3a2bSChandler Carruth } 5107adc3a2bSChandler Carruth 5117adc3a2bSChandler Carruth /// Initialize CG 5127adc3a2bSChandler Carruth bool CGPassManager::doInitialization(CallGraph &CG) { 5137adc3a2bSChandler Carruth bool Changed = false; 5147adc3a2bSChandler Carruth for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { 5157adc3a2bSChandler Carruth if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { 5167adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 5177adc3a2bSChandler Carruth "Invalid CGPassManager member"); 5187adc3a2bSChandler Carruth Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule()); 5197adc3a2bSChandler Carruth } else { 5207adc3a2bSChandler Carruth Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG); 5217adc3a2bSChandler Carruth } 5227adc3a2bSChandler Carruth } 5237adc3a2bSChandler Carruth return Changed; 5247adc3a2bSChandler Carruth } 5257adc3a2bSChandler Carruth 5267adc3a2bSChandler Carruth /// Finalize CG 5277adc3a2bSChandler Carruth bool CGPassManager::doFinalization(CallGraph &CG) { 5287adc3a2bSChandler Carruth bool Changed = false; 5297adc3a2bSChandler Carruth for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { 5307adc3a2bSChandler Carruth if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { 5317adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 5327adc3a2bSChandler Carruth "Invalid CGPassManager member"); 5337adc3a2bSChandler Carruth Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule()); 5347adc3a2bSChandler Carruth } else { 5357adc3a2bSChandler Carruth Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG); 5367adc3a2bSChandler Carruth } 5377adc3a2bSChandler Carruth } 5387adc3a2bSChandler Carruth return Changed; 5397adc3a2bSChandler Carruth } 5407adc3a2bSChandler Carruth 5417adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5427adc3a2bSChandler Carruth // CallGraphSCC Implementation 5437adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5447adc3a2bSChandler Carruth 5457adc3a2bSChandler Carruth /// This informs the SCC and the pass manager that the specified 5467adc3a2bSChandler Carruth /// Old node has been deleted, and New is to be used in its place. 5477adc3a2bSChandler Carruth void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) { 5487adc3a2bSChandler Carruth assert(Old != New && "Should not replace node with self"); 5497adc3a2bSChandler Carruth for (unsigned i = 0; ; ++i) { 5507adc3a2bSChandler Carruth assert(i != Nodes.size() && "Node not in SCC"); 5517adc3a2bSChandler Carruth if (Nodes[i] != Old) continue; 5527adc3a2bSChandler Carruth Nodes[i] = New; 5537adc3a2bSChandler Carruth break; 5547adc3a2bSChandler Carruth } 5557adc3a2bSChandler Carruth 5567adc3a2bSChandler Carruth // Update the active scc_iterator so that it doesn't contain dangling 5577adc3a2bSChandler Carruth // pointers to the old CallGraphNode. 5587adc3a2bSChandler Carruth scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context; 5597adc3a2bSChandler Carruth CGI->ReplaceNode(Old, New); 5607adc3a2bSChandler Carruth } 5617adc3a2bSChandler Carruth 5627adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5637adc3a2bSChandler Carruth // CallGraphSCCPass Implementation 5647adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5657adc3a2bSChandler Carruth 5667adc3a2bSChandler Carruth /// Assign pass manager to manage this pass. 5677adc3a2bSChandler Carruth void CallGraphSCCPass::assignPassManager(PMStack &PMS, 5687adc3a2bSChandler Carruth PassManagerType PreferredType) { 5697adc3a2bSChandler Carruth // Find CGPassManager 5707adc3a2bSChandler Carruth while (!PMS.empty() && 5717adc3a2bSChandler Carruth PMS.top()->getPassManagerType() > PMT_CallGraphPassManager) 5727adc3a2bSChandler Carruth PMS.pop(); 5737adc3a2bSChandler Carruth 5747adc3a2bSChandler Carruth assert(!PMS.empty() && "Unable to handle Call Graph Pass"); 5757adc3a2bSChandler Carruth CGPassManager *CGP; 5767adc3a2bSChandler Carruth 5777adc3a2bSChandler Carruth if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager) 5787adc3a2bSChandler Carruth CGP = (CGPassManager*)PMS.top(); 5797adc3a2bSChandler Carruth else { 5807adc3a2bSChandler Carruth // Create new Call Graph SCC Pass Manager if it does not exist. 5817adc3a2bSChandler Carruth assert(!PMS.empty() && "Unable to create Call Graph Pass Manager"); 5827adc3a2bSChandler Carruth PMDataManager *PMD = PMS.top(); 5837adc3a2bSChandler Carruth 5847adc3a2bSChandler Carruth // [1] Create new Call Graph Pass Manager 5857adc3a2bSChandler Carruth CGP = new CGPassManager(); 5867adc3a2bSChandler Carruth 5877adc3a2bSChandler Carruth // [2] Set up new manager's top level manager 5887adc3a2bSChandler Carruth PMTopLevelManager *TPM = PMD->getTopLevelManager(); 5897adc3a2bSChandler Carruth TPM->addIndirectPassManager(CGP); 5907adc3a2bSChandler Carruth 5917adc3a2bSChandler Carruth // [3] Assign manager to manage this new manager. This may create 5927adc3a2bSChandler Carruth // and push new managers into PMS 5937adc3a2bSChandler Carruth Pass *P = CGP; 5947adc3a2bSChandler Carruth TPM->schedulePass(P); 5957adc3a2bSChandler Carruth 5967adc3a2bSChandler Carruth // [4] Push new manager into PMS 5977adc3a2bSChandler Carruth PMS.push(CGP); 5987adc3a2bSChandler Carruth } 5997adc3a2bSChandler Carruth 6007adc3a2bSChandler Carruth CGP->add(this); 6017adc3a2bSChandler Carruth } 6027adc3a2bSChandler Carruth 6037adc3a2bSChandler Carruth /// For this class, we declare that we require and preserve the call graph. 6047adc3a2bSChandler Carruth /// If the derived class implements this method, it should 6057adc3a2bSChandler Carruth /// always explicitly call the implementation here. 6067adc3a2bSChandler Carruth void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const { 6077adc3a2bSChandler Carruth AU.addRequired<CallGraphWrapperPass>(); 6087adc3a2bSChandler Carruth AU.addPreserved<CallGraphWrapperPass>(); 6097adc3a2bSChandler Carruth } 6107adc3a2bSChandler Carruth 6117adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 6127adc3a2bSChandler Carruth // PrintCallGraphPass Implementation 6137adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 6147adc3a2bSChandler Carruth 6157adc3a2bSChandler Carruth namespace { 616fa6434beSEugene Zelenko 6177adc3a2bSChandler Carruth /// PrintCallGraphPass - Print a Module corresponding to a call graph. 6187adc3a2bSChandler Carruth /// 6197adc3a2bSChandler Carruth class PrintCallGraphPass : public CallGraphSCCPass { 6207adc3a2bSChandler Carruth std::string Banner; 621fa6434beSEugene Zelenko raw_ostream &OS; // raw_ostream to print on. 6227adc3a2bSChandler Carruth 6237adc3a2bSChandler Carruth public: 6247adc3a2bSChandler Carruth static char ID; 625fa6434beSEugene Zelenko 626fa6434beSEugene Zelenko PrintCallGraphPass(const std::string &B, raw_ostream &OS) 627fa6434beSEugene Zelenko : CallGraphSCCPass(ID), Banner(B), OS(OS) {} 6287adc3a2bSChandler Carruth 6297adc3a2bSChandler Carruth void getAnalysisUsage(AnalysisUsage &AU) const override { 6307adc3a2bSChandler Carruth AU.setPreservesAll(); 6317adc3a2bSChandler Carruth } 6327adc3a2bSChandler Carruth 6337adc3a2bSChandler Carruth bool runOnSCC(CallGraphSCC &SCC) override { 6347d463921SYaron Keren bool BannerPrinted = false; 635062b3fedSMehdi Amini auto PrintBannerOnce = [&]() { 636062b3fedSMehdi Amini if (BannerPrinted) 637062b3fedSMehdi Amini return; 638fa6434beSEugene Zelenko OS << Banner; 639062b3fedSMehdi Amini BannerPrinted = true; 640062b3fedSMehdi Amini }; 6417254d3c5SFedor Sergeev 6427254d3c5SFedor Sergeev bool NeedModule = llvm::forcePrintModuleIR(); 6437254d3c5SFedor Sergeev if (isFunctionInPrintList("*") && NeedModule) { 6447254d3c5SFedor Sergeev PrintBannerOnce(); 6457254d3c5SFedor Sergeev OS << "\n"; 6467254d3c5SFedor Sergeev SCC.getCallGraph().getModule().print(OS, nullptr); 6477254d3c5SFedor Sergeev return false; 6487254d3c5SFedor Sergeev } 6497254d3c5SFedor Sergeev bool FoundFunction = false; 6507adc3a2bSChandler Carruth for (CallGraphNode *CGN : SCC) { 6517d463921SYaron Keren if (Function *F = CGN->getFunction()) { 6527d463921SYaron Keren if (!F->isDeclaration() && isFunctionInPrintList(F->getName())) { 6537254d3c5SFedor Sergeev FoundFunction = true; 6547254d3c5SFedor Sergeev if (!NeedModule) { 655062b3fedSMehdi Amini PrintBannerOnce(); 656fa6434beSEugene Zelenko F->print(OS); 657062b3fedSMehdi Amini } 6587254d3c5SFedor Sergeev } 659fa6434beSEugene Zelenko } else if (isFunctionInPrintList("*")) { 660062b3fedSMehdi Amini PrintBannerOnce(); 661fa6434beSEugene Zelenko OS << "\nPrinting <null> Function\n"; 6627adc3a2bSChandler Carruth } 663062b3fedSMehdi Amini } 6647254d3c5SFedor Sergeev if (NeedModule && FoundFunction) { 6657254d3c5SFedor Sergeev PrintBannerOnce(); 6667254d3c5SFedor Sergeev OS << "\n"; 6677254d3c5SFedor Sergeev SCC.getCallGraph().getModule().print(OS, nullptr); 6687254d3c5SFedor Sergeev } 6697adc3a2bSChandler Carruth return false; 6707adc3a2bSChandler Carruth } 6711de4792cSYaron Keren 6721de4792cSYaron Keren StringRef getPassName() const override { return "Print CallGraph IR"; } 6737adc3a2bSChandler Carruth }; 6747adc3a2bSChandler Carruth 6757adc3a2bSChandler Carruth } // end anonymous namespace. 6767adc3a2bSChandler Carruth 6777adc3a2bSChandler Carruth char PrintCallGraphPass::ID = 0; 6787adc3a2bSChandler Carruth 679fa6434beSEugene Zelenko Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &OS, 6807adc3a2bSChandler Carruth const std::string &Banner) const { 681fa6434beSEugene Zelenko return new PrintCallGraphPass(Banner, OS); 6827adc3a2bSChandler Carruth } 6837adc3a2bSChandler Carruth 684b37a70f4SRichard Trieu static std::string getDescription(const CallGraphSCC &SCC) { 685b37a70f4SRichard Trieu std::string Desc = "SCC ("; 686b37a70f4SRichard Trieu bool First = true; 687b37a70f4SRichard Trieu for (CallGraphNode *CGN : SCC) { 688b37a70f4SRichard Trieu if (First) 689b37a70f4SRichard Trieu First = false; 690b37a70f4SRichard Trieu else 691b37a70f4SRichard Trieu Desc += ", "; 692b37a70f4SRichard Trieu Function *F = CGN->getFunction(); 693b37a70f4SRichard Trieu if (F) 694b37a70f4SRichard Trieu Desc += F->getName(); 695b37a70f4SRichard Trieu else 696b37a70f4SRichard Trieu Desc += "<<null function>>"; 697b37a70f4SRichard Trieu } 698b37a70f4SRichard Trieu Desc += ")"; 699b37a70f4SRichard Trieu return Desc; 700b37a70f4SRichard Trieu } 701b37a70f4SRichard Trieu 702aa641a51SAndrew Kaylor bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const { 703b37a70f4SRichard Trieu OptPassGate &Gate = 704b37a70f4SRichard Trieu SCC.getCallGraph().getModule().getContext().getOptPassGate(); 705b37a70f4SRichard Trieu return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(SCC)); 706aa641a51SAndrew Kaylor } 707bbacddfeSMehdi Amini 708bbacddfeSMehdi Amini char DummyCGSCCPass::ID = 0; 709fa6434beSEugene Zelenko 710bbacddfeSMehdi Amini INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false, 711bbacddfeSMehdi Amini false) 712