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" 2264d99a1dSJohannes Doerfert #include "llvm/IR/AbstractCallSite.h" 237adc3a2bSChandler Carruth #include "llvm/IR/Function.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" 302f0de582SArthur Eubanks #include "llvm/IR/PrintPasses.h" 31b1f4e597Sserge-sans-paille #include "llvm/IR/StructuralHash.h" 32fa6434beSEugene Zelenko #include "llvm/Pass.h" 337adc3a2bSChandler Carruth #include "llvm/Support/CommandLine.h" 347adc3a2bSChandler Carruth #include "llvm/Support/Debug.h" 357adc3a2bSChandler Carruth #include "llvm/Support/Timer.h" 367adc3a2bSChandler Carruth #include "llvm/Support/raw_ostream.h" 37fa6434beSEugene Zelenko #include <cassert> 38fa6434beSEugene Zelenko #include <string> 39fa6434beSEugene Zelenko #include <utility> 40fa6434beSEugene Zelenko #include <vector> 41fa6434beSEugene Zelenko 427adc3a2bSChandler Carruth using namespace llvm; 437adc3a2bSChandler Carruth 447adc3a2bSChandler Carruth #define DEBUG_TYPE "cgscc-passmgr" 457adc3a2bSChandler Carruth 463c811ce4SArthur Eubanks cl::opt<unsigned> MaxDevirtIterations("max-devirt-iterations", cl::ReallyHidden, 473c811ce4SArthur Eubanks cl::init(4)); 487adc3a2bSChandler Carruth 497adc3a2bSChandler Carruth STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC"); 507adc3a2bSChandler Carruth 517adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 527adc3a2bSChandler Carruth // CGPassManager 537adc3a2bSChandler Carruth // 547adc3a2bSChandler Carruth /// CGPassManager manages FPPassManagers and CallGraphSCCPasses. 557adc3a2bSChandler Carruth 567adc3a2bSChandler Carruth namespace { 577adc3a2bSChandler Carruth 587adc3a2bSChandler Carruth class CGPassManager : public ModulePass, public PMDataManager { 597adc3a2bSChandler Carruth public: 607adc3a2bSChandler Carruth static char ID; 61fa6434beSEugene Zelenko 62fa6434beSEugene Zelenko explicit CGPassManager() : ModulePass(ID), PMDataManager() {} 637adc3a2bSChandler Carruth 647adc3a2bSChandler Carruth /// Execute all of the passes scheduled for execution. Keep track of 657adc3a2bSChandler Carruth /// whether any of the passes modifies the module, and if so, return true. 667adc3a2bSChandler Carruth bool runOnModule(Module &M) override; 677adc3a2bSChandler Carruth 687adc3a2bSChandler Carruth using ModulePass::doInitialization; 697adc3a2bSChandler Carruth using ModulePass::doFinalization; 707adc3a2bSChandler Carruth 717adc3a2bSChandler Carruth bool doInitialization(CallGraph &CG); 727adc3a2bSChandler Carruth bool doFinalization(CallGraph &CG); 737adc3a2bSChandler Carruth 747adc3a2bSChandler Carruth /// Pass Manager itself does not invalidate any analysis info. 757adc3a2bSChandler Carruth void getAnalysisUsage(AnalysisUsage &Info) const override { 767adc3a2bSChandler Carruth // CGPassManager walks SCC and it needs CallGraph. 777adc3a2bSChandler Carruth Info.addRequired<CallGraphWrapperPass>(); 787adc3a2bSChandler Carruth Info.setPreservesAll(); 797adc3a2bSChandler Carruth } 807adc3a2bSChandler Carruth 81117296c0SMehdi Amini StringRef getPassName() const override { return "CallGraph Pass Manager"; } 827adc3a2bSChandler Carruth 837adc3a2bSChandler Carruth PMDataManager *getAsPMDataManager() override { return this; } 847adc3a2bSChandler Carruth Pass *getAsPass() override { return this; } 857adc3a2bSChandler Carruth 867adc3a2bSChandler Carruth // Print passes managed by this manager 877adc3a2bSChandler Carruth void dumpPassStructure(unsigned Offset) override { 887adc3a2bSChandler Carruth errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n"; 897adc3a2bSChandler Carruth for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 907adc3a2bSChandler Carruth Pass *P = getContainedPass(Index); 917adc3a2bSChandler Carruth P->dumpPassStructure(Offset + 1); 927adc3a2bSChandler Carruth dumpLastUses(P, Offset+1); 937adc3a2bSChandler Carruth } 947adc3a2bSChandler Carruth } 957adc3a2bSChandler Carruth 967adc3a2bSChandler Carruth Pass *getContainedPass(unsigned N) { 977adc3a2bSChandler Carruth assert(N < PassVector.size() && "Pass number out of range!"); 987adc3a2bSChandler Carruth return static_cast<Pass *>(PassVector[N]); 997adc3a2bSChandler Carruth } 1007adc3a2bSChandler Carruth 1017adc3a2bSChandler Carruth PassManagerType getPassManagerType() const override { 1027adc3a2bSChandler Carruth return PMT_CallGraphPassManager; 1037adc3a2bSChandler Carruth } 1047adc3a2bSChandler Carruth 1057adc3a2bSChandler Carruth private: 1067adc3a2bSChandler Carruth bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, 1077adc3a2bSChandler Carruth bool &DevirtualizedCall); 1087adc3a2bSChandler Carruth 1097adc3a2bSChandler Carruth bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, 1107adc3a2bSChandler Carruth CallGraph &CG, bool &CallGraphUpToDate, 1117adc3a2bSChandler Carruth bool &DevirtualizedCall); 112c137c28cSMehdi Amini bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG, 1137adc3a2bSChandler Carruth bool IsCheckingMode); 1147adc3a2bSChandler Carruth }; 1157adc3a2bSChandler Carruth 1167adc3a2bSChandler Carruth } // end anonymous namespace. 1177adc3a2bSChandler Carruth 1187adc3a2bSChandler Carruth char CGPassManager::ID = 0; 1197adc3a2bSChandler Carruth 1207adc3a2bSChandler Carruth bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, 1217adc3a2bSChandler Carruth CallGraph &CG, bool &CallGraphUpToDate, 1227adc3a2bSChandler Carruth bool &DevirtualizedCall) { 1237adc3a2bSChandler Carruth bool Changed = false; 1247adc3a2bSChandler Carruth PMDataManager *PM = P->getAsPMDataManager(); 125e49374d0SJessica Paquette Module &M = CG.getModule(); 1267adc3a2bSChandler Carruth 1277adc3a2bSChandler Carruth if (!PM) { 1287adc3a2bSChandler Carruth CallGraphSCCPass *CGSP = (CallGraphSCCPass *)P; 1297adc3a2bSChandler Carruth if (!CallGraphUpToDate) { 1307adc3a2bSChandler Carruth DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); 1317adc3a2bSChandler Carruth CallGraphUpToDate = true; 1327adc3a2bSChandler Carruth } 1337adc3a2bSChandler Carruth 1347adc3a2bSChandler Carruth { 1351fc443b8SJessica Paquette unsigned InstrCount, SCCCount = 0; 136a0aa5b35SJessica Paquette StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount; 137023e25adSXin Tong bool EmitICRemark = M.shouldEmitInstrCountChangedRemark(); 1387adc3a2bSChandler Carruth TimeRegion PassTimer(getPassTimer(CGSP)); 139023e25adSXin Tong if (EmitICRemark) 140a0aa5b35SJessica Paquette InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount); 1417adc3a2bSChandler Carruth Changed = CGSP->runOnSCC(CurSCC); 142e49374d0SJessica Paquette 1431fc443b8SJessica Paquette if (EmitICRemark) { 1441fc443b8SJessica Paquette // FIXME: Add getInstructionCount to CallGraphSCC. 1451fc443b8SJessica Paquette SCCCount = M.getInstructionCount(); 1461fc443b8SJessica Paquette // Is there a difference in the number of instructions in the module? 1471fc443b8SJessica Paquette if (SCCCount != InstrCount) { 1481fc443b8SJessica Paquette // Yep. Emit a remark and update InstrCount. 1499a23c559SJessica Paquette int64_t Delta = 1509a23c559SJessica Paquette static_cast<int64_t>(SCCCount) - static_cast<int64_t>(InstrCount); 151a0aa5b35SJessica Paquette emitInstrCountChangedRemark(P, M, Delta, InstrCount, 152a0aa5b35SJessica Paquette FunctionToInstrCount); 1531fc443b8SJessica Paquette InstrCount = SCCCount; 1541fc443b8SJessica Paquette } 1551fc443b8SJessica Paquette } 1567adc3a2bSChandler Carruth } 1577adc3a2bSChandler Carruth 1587adc3a2bSChandler Carruth // After the CGSCCPass is done, when assertions are enabled, use 1597adc3a2bSChandler Carruth // RefreshCallGraph to verify that the callgraph was correctly updated. 1607adc3a2bSChandler Carruth #ifndef NDEBUG 1617adc3a2bSChandler Carruth if (Changed) 1627adc3a2bSChandler Carruth RefreshCallGraph(CurSCC, CG, true); 1637adc3a2bSChandler Carruth #endif 1647adc3a2bSChandler Carruth 1657adc3a2bSChandler Carruth return Changed; 1667adc3a2bSChandler Carruth } 1677adc3a2bSChandler Carruth 1687adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 1697adc3a2bSChandler Carruth "Invalid CGPassManager member"); 1707adc3a2bSChandler Carruth FPPassManager *FPP = (FPPassManager*)P; 1717adc3a2bSChandler Carruth 1727adc3a2bSChandler Carruth // Run pass P on all functions in the current SCC. 1737adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) { 1747adc3a2bSChandler Carruth if (Function *F = CGN->getFunction()) { 1757adc3a2bSChandler Carruth dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName()); 1767adc3a2bSChandler Carruth { 1777adc3a2bSChandler Carruth TimeRegion PassTimer(getPassTimer(FPP)); 1787adc3a2bSChandler Carruth Changed |= FPP->runOnFunction(*F); 1797adc3a2bSChandler Carruth } 1807adc3a2bSChandler Carruth F->getContext().yield(); 1817adc3a2bSChandler Carruth } 1827adc3a2bSChandler Carruth } 1837adc3a2bSChandler Carruth 1847adc3a2bSChandler Carruth // The function pass(es) modified the IR, they may have clobbered the 1857adc3a2bSChandler Carruth // callgraph. 1867adc3a2bSChandler Carruth if (Changed && CallGraphUpToDate) { 187d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " << P->getPassName() 188d34e60caSNicola Zaghen << '\n'); 1897adc3a2bSChandler Carruth CallGraphUpToDate = false; 1907adc3a2bSChandler Carruth } 1917adc3a2bSChandler Carruth return Changed; 1927adc3a2bSChandler Carruth } 1937adc3a2bSChandler Carruth 1947adc3a2bSChandler Carruth /// Scan the functions in the specified CFG and resync the 1957adc3a2bSChandler Carruth /// callgraph with the call sites found in it. This is used after 1967adc3a2bSChandler Carruth /// FunctionPasses have potentially munged the callgraph, and can be used after 1977adc3a2bSChandler Carruth /// CallGraphSCC passes to verify that they correctly updated the callgraph. 1987adc3a2bSChandler Carruth /// 1997adc3a2bSChandler Carruth /// This function returns true if it devirtualized an existing function call, 2007adc3a2bSChandler Carruth /// meaning it turned an indirect call into a direct call. This happens when 2017adc3a2bSChandler Carruth /// a function pass like GVN optimizes away stuff feeding the indirect call. 2027adc3a2bSChandler Carruth /// This never happens in checking mode. 203c137c28cSMehdi Amini bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG, 204c137c28cSMehdi Amini bool CheckingMode) { 205ce3f75dfSChandler Carruth DenseMap<Value *, CallGraphNode *> Calls; 2067adc3a2bSChandler Carruth 207d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size() 2087adc3a2bSChandler Carruth << " nodes:\n"; 209d34e60caSNicola Zaghen for (CallGraphNode *CGN 210d34e60caSNicola Zaghen : CurSCC) CGN->dump();); 2117adc3a2bSChandler Carruth 2127adc3a2bSChandler Carruth bool MadeChange = false; 2137adc3a2bSChandler Carruth bool DevirtualizedCall = false; 2147adc3a2bSChandler Carruth 2157adc3a2bSChandler Carruth // Scan all functions in the SCC. 2167adc3a2bSChandler Carruth unsigned FunctionNo = 0; 2177adc3a2bSChandler Carruth for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end(); 2187adc3a2bSChandler Carruth SCCIdx != E; ++SCCIdx, ++FunctionNo) { 2197adc3a2bSChandler Carruth CallGraphNode *CGN = *SCCIdx; 2207adc3a2bSChandler Carruth Function *F = CGN->getFunction(); 2217adc3a2bSChandler Carruth if (!F || F->isDeclaration()) continue; 2227adc3a2bSChandler Carruth 2237adc3a2bSChandler Carruth // Walk the function body looking for call sites. Sync up the call sites in 2247adc3a2bSChandler Carruth // CGN with those actually in the function. 2257adc3a2bSChandler Carruth 2267adc3a2bSChandler Carruth // Keep track of the number of direct and indirect calls that were 2277adc3a2bSChandler Carruth // invalidated and removed. 2287adc3a2bSChandler Carruth unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0; 2297adc3a2bSChandler Carruth 23064d99a1dSJohannes Doerfert CallGraphNode::iterator CGNEnd = CGN->end(); 23164d99a1dSJohannes Doerfert 23264d99a1dSJohannes Doerfert auto RemoveAndCheckForDone = [&](CallGraphNode::iterator I) { 23364d99a1dSJohannes Doerfert // Just remove the edge from the set of callees, keep track of whether 23464d99a1dSJohannes Doerfert // I points to the last element of the vector. 23564d99a1dSJohannes Doerfert bool WasLast = I + 1 == CGNEnd; 23664d99a1dSJohannes Doerfert CGN->removeCallEdge(I); 23764d99a1dSJohannes Doerfert 23864d99a1dSJohannes Doerfert // If I pointed to the last element of the vector, we have to bail out: 23964d99a1dSJohannes Doerfert // iterator checking rejects comparisons of the resultant pointer with 24064d99a1dSJohannes Doerfert // end. 24164d99a1dSJohannes Doerfert if (WasLast) 24264d99a1dSJohannes Doerfert return true; 24364d99a1dSJohannes Doerfert 24464d99a1dSJohannes Doerfert CGNEnd = CGN->end(); 24564d99a1dSJohannes Doerfert return false; 24664d99a1dSJohannes Doerfert }; 24764d99a1dSJohannes Doerfert 2487adc3a2bSChandler Carruth // Get the set of call sites currently in the function. 24964d99a1dSJohannes Doerfert for (CallGraphNode::iterator I = CGN->begin(); I != CGNEnd;) { 25064d99a1dSJohannes Doerfert // Delete "reference" call records that do not have call instruction. We 25164d99a1dSJohannes Doerfert // reinsert them as needed later. However, keep them in checking mode. 252cb8faaacSSergey Dmitriev if (!I->first) { 25364d99a1dSJohannes Doerfert if (CheckingMode) { 254cb8faaacSSergey Dmitriev ++I; 255cb8faaacSSergey Dmitriev continue; 256cb8faaacSSergey Dmitriev } 25764d99a1dSJohannes Doerfert if (RemoveAndCheckForDone(I)) 25864d99a1dSJohannes Doerfert break; 25964d99a1dSJohannes Doerfert continue; 26064d99a1dSJohannes Doerfert } 261cb8faaacSSergey Dmitriev 2627adc3a2bSChandler Carruth // If this call site is null, then the function pass deleted the call 263e6bca0eeSSanjoy Das // entirely and the WeakTrackingVH nulled it out. 264cb8faaacSSergey Dmitriev auto *Call = dyn_cast_or_null<CallBase>(*I->first); 2651becd298SSergey Dmitriev if (!Call || 2667adc3a2bSChandler Carruth // If we've already seen this call site, then the FunctionPass RAUW'd 2677adc3a2bSChandler Carruth // one call with another, which resulted in two "uses" in the edge 2687adc3a2bSChandler Carruth // list of the same call. 2691becd298SSergey Dmitriev Calls.count(Call) || 2707adc3a2bSChandler Carruth 2717adc3a2bSChandler Carruth // If the call edge is not from a call or invoke, or it is a 2727adc3a2bSChandler Carruth // instrinsic call, then the function pass RAUW'd a call with 2737adc3a2bSChandler Carruth // another value. This can happen when constant folding happens 2747adc3a2bSChandler Carruth // of well known functions etc. 275ce3f75dfSChandler Carruth (Call->getCalledFunction() && 276ce3f75dfSChandler Carruth Call->getCalledFunction()->isIntrinsic() && 277ce3f75dfSChandler Carruth Intrinsic::isLeaf(Call->getCalledFunction()->getIntrinsicID()))) { 2787adc3a2bSChandler Carruth assert(!CheckingMode && 2797adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 2807adc3a2bSChandler Carruth 2817adc3a2bSChandler Carruth // If this was an indirect call site, count it. 2827adc3a2bSChandler Carruth if (!I->second->getFunction()) 2837adc3a2bSChandler Carruth ++NumIndirectRemoved; 2847adc3a2bSChandler Carruth else 2857adc3a2bSChandler Carruth ++NumDirectRemoved; 2867adc3a2bSChandler Carruth 28764d99a1dSJohannes Doerfert if (RemoveAndCheckForDone(I)) 2887adc3a2bSChandler Carruth break; 2897adc3a2bSChandler Carruth continue; 2907adc3a2bSChandler Carruth } 2917adc3a2bSChandler Carruth 2921becd298SSergey Dmitriev assert(!Calls.count(Call) && "Call site occurs in node multiple times"); 2937adc3a2bSChandler Carruth 294ce3f75dfSChandler Carruth if (Call) { 295ce3f75dfSChandler Carruth Function *Callee = Call->getCalledFunction(); 2967adc3a2bSChandler Carruth // Ignore intrinsics because they're not really function calls. 2977adc3a2bSChandler Carruth if (!Callee || !(Callee->isIntrinsic())) 2981becd298SSergey Dmitriev Calls.insert(std::make_pair(Call, I->second)); 2997adc3a2bSChandler Carruth } 3007adc3a2bSChandler Carruth ++I; 3017adc3a2bSChandler Carruth } 3027adc3a2bSChandler Carruth 3037adc3a2bSChandler Carruth // Loop over all of the instructions in the function, getting the callsites. 3047adc3a2bSChandler Carruth // Keep track of the number of direct/indirect calls added. 3057adc3a2bSChandler Carruth unsigned NumDirectAdded = 0, NumIndirectAdded = 0; 3067adc3a2bSChandler Carruth 307aa209150SBenjamin Kramer for (BasicBlock &BB : *F) 308aa209150SBenjamin Kramer for (Instruction &I : BB) { 309ce3f75dfSChandler Carruth auto *Call = dyn_cast<CallBase>(&I); 310ce3f75dfSChandler Carruth if (!Call) 311ce3f75dfSChandler Carruth continue; 312ce3f75dfSChandler Carruth Function *Callee = Call->getCalledFunction(); 313ce3f75dfSChandler Carruth if (Callee && Callee->isIntrinsic()) 314ce3f75dfSChandler Carruth continue; 3157adc3a2bSChandler Carruth 31664d99a1dSJohannes Doerfert // If we are not in checking mode, insert potential callback calls as 31764d99a1dSJohannes Doerfert // references. This is not a requirement but helps to iterate over the 31864d99a1dSJohannes Doerfert // functions in the right order. 31964d99a1dSJohannes Doerfert if (!CheckingMode) { 32064d99a1dSJohannes Doerfert forEachCallbackFunction(*Call, [&](Function *CB) { 32164d99a1dSJohannes Doerfert CGN->addCalledFunction(nullptr, CG.getOrInsertFunction(CB)); 32264d99a1dSJohannes Doerfert }); 32364d99a1dSJohannes Doerfert } 32464d99a1dSJohannes Doerfert 3257adc3a2bSChandler Carruth // If this call site already existed in the callgraph, just verify it 326ce3f75dfSChandler Carruth // matches up to expectations and remove it from Calls. 3277adc3a2bSChandler Carruth DenseMap<Value *, CallGraphNode *>::iterator ExistingIt = 328ce3f75dfSChandler Carruth Calls.find(Call); 329ce3f75dfSChandler Carruth if (ExistingIt != Calls.end()) { 3307adc3a2bSChandler Carruth CallGraphNode *ExistingNode = ExistingIt->second; 3317adc3a2bSChandler Carruth 332ce3f75dfSChandler Carruth // Remove from Calls since we have now seen it. 333ce3f75dfSChandler Carruth Calls.erase(ExistingIt); 3347adc3a2bSChandler Carruth 3357adc3a2bSChandler Carruth // Verify that the callee is right. 336ce3f75dfSChandler Carruth if (ExistingNode->getFunction() == Call->getCalledFunction()) 3377adc3a2bSChandler Carruth continue; 3387adc3a2bSChandler Carruth 3397adc3a2bSChandler Carruth // If we are in checking mode, we are not allowed to actually mutate 3407adc3a2bSChandler Carruth // the callgraph. If this is a case where we can infer that the 3417adc3a2bSChandler Carruth // callgraph is less precise than it could be (e.g. an indirect call 3427adc3a2bSChandler Carruth // site could be turned direct), don't reject it in checking mode, and 3437adc3a2bSChandler Carruth // don't tweak it to be more precise. 344ce3f75dfSChandler Carruth if (CheckingMode && Call->getCalledFunction() && 3457adc3a2bSChandler Carruth ExistingNode->getFunction() == nullptr) 3467adc3a2bSChandler Carruth continue; 3477adc3a2bSChandler Carruth 3487adc3a2bSChandler Carruth assert(!CheckingMode && 3497adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 3507adc3a2bSChandler Carruth 3517adc3a2bSChandler Carruth // If not, we either went from a direct call to indirect, indirect to 3527adc3a2bSChandler Carruth // direct, or direct to different direct. 3537adc3a2bSChandler Carruth CallGraphNode *CalleeNode; 354ce3f75dfSChandler Carruth if (Function *Callee = Call->getCalledFunction()) { 3557adc3a2bSChandler Carruth CalleeNode = CG.getOrInsertFunction(Callee); 3567adc3a2bSChandler Carruth // Keep track of whether we turned an indirect call into a direct 3577adc3a2bSChandler Carruth // one. 3587adc3a2bSChandler Carruth if (!ExistingNode->getFunction()) { 3597adc3a2bSChandler Carruth DevirtualizedCall = true; 360d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '" 3617adc3a2bSChandler Carruth << Callee->getName() << "'\n"); 3627adc3a2bSChandler Carruth } 3637adc3a2bSChandler Carruth } else { 3647adc3a2bSChandler Carruth CalleeNode = CG.getCallsExternalNode(); 3657adc3a2bSChandler Carruth } 3667adc3a2bSChandler Carruth 3677adc3a2bSChandler Carruth // Update the edge target in CGN. 368ce3f75dfSChandler Carruth CGN->replaceCallEdge(*Call, *Call, CalleeNode); 3697adc3a2bSChandler Carruth MadeChange = true; 3707adc3a2bSChandler Carruth continue; 3717adc3a2bSChandler Carruth } 3727adc3a2bSChandler Carruth 3737adc3a2bSChandler Carruth assert(!CheckingMode && 3747adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 3757adc3a2bSChandler Carruth 3767adc3a2bSChandler Carruth // If the call site didn't exist in the CGN yet, add it. 3777adc3a2bSChandler Carruth CallGraphNode *CalleeNode; 378ce3f75dfSChandler Carruth if (Function *Callee = Call->getCalledFunction()) { 3797adc3a2bSChandler Carruth CalleeNode = CG.getOrInsertFunction(Callee); 3807adc3a2bSChandler Carruth ++NumDirectAdded; 3817adc3a2bSChandler Carruth } else { 3827adc3a2bSChandler Carruth CalleeNode = CG.getCallsExternalNode(); 3837adc3a2bSChandler Carruth ++NumIndirectAdded; 3847adc3a2bSChandler Carruth } 3857adc3a2bSChandler Carruth 386ce3f75dfSChandler Carruth CGN->addCalledFunction(Call, CalleeNode); 3877adc3a2bSChandler Carruth MadeChange = true; 3887adc3a2bSChandler Carruth } 3897adc3a2bSChandler Carruth 3907adc3a2bSChandler Carruth // We scanned the old callgraph node, removing invalidated call sites and 3917adc3a2bSChandler Carruth // then added back newly found call sites. One thing that can happen is 3927adc3a2bSChandler Carruth // that an old indirect call site was deleted and replaced with a new direct 3937adc3a2bSChandler Carruth // call. In this case, we have devirtualized a call, and CGSCCPM would like 3947adc3a2bSChandler Carruth // to iteratively optimize the new code. Unfortunately, we don't really 3957adc3a2bSChandler Carruth // have a great way to detect when this happens. As an approximation, we 3967adc3a2bSChandler Carruth // just look at whether the number of indirect calls is reduced and the 3977adc3a2bSChandler Carruth // number of direct calls is increased. There are tons of ways to fool this 3987adc3a2bSChandler Carruth // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a 3997adc3a2bSChandler Carruth // direct call) but this is close enough. 4007adc3a2bSChandler Carruth if (NumIndirectRemoved > NumIndirectAdded && 4017adc3a2bSChandler Carruth NumDirectRemoved < NumDirectAdded) 4027adc3a2bSChandler Carruth DevirtualizedCall = true; 4037adc3a2bSChandler Carruth 4047adc3a2bSChandler Carruth // After scanning this function, if we still have entries in callsites, then 405e6bca0eeSSanjoy Das // they are dangling pointers. WeakTrackingVH should save us for this, so 406e6bca0eeSSanjoy Das // abort if 4072cbeb00fSSanjoy Das // this happens. 408ce3f75dfSChandler Carruth assert(Calls.empty() && "Dangling pointers found in call sites map"); 4097adc3a2bSChandler Carruth 4107adc3a2bSChandler Carruth // Periodically do an explicit clear to remove tombstones when processing 4117adc3a2bSChandler Carruth // large scc's. 4127adc3a2bSChandler Carruth if ((FunctionNo & 15) == 15) 413ce3f75dfSChandler Carruth Calls.clear(); 4147adc3a2bSChandler Carruth } 4157adc3a2bSChandler Carruth 416d34e60caSNicola Zaghen LLVM_DEBUG(if (MadeChange) { 4177adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n"; 4187adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) 4197adc3a2bSChandler Carruth CGN->dump(); 4207adc3a2bSChandler Carruth if (DevirtualizedCall) 4217adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n"; 4227adc3a2bSChandler Carruth } else { 4237adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n"; 424d34e60caSNicola Zaghen }); 4257adc3a2bSChandler Carruth (void)MadeChange; 4267adc3a2bSChandler Carruth 4277adc3a2bSChandler Carruth return DevirtualizedCall; 4287adc3a2bSChandler Carruth } 4297adc3a2bSChandler Carruth 4307adc3a2bSChandler Carruth /// Execute the body of the entire pass manager on the specified SCC. 4317adc3a2bSChandler Carruth /// This keeps track of whether a function pass devirtualizes 4327adc3a2bSChandler Carruth /// any calls and returns it in DevirtualizedCall. 4337adc3a2bSChandler Carruth bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, 4347adc3a2bSChandler Carruth bool &DevirtualizedCall) { 4357adc3a2bSChandler Carruth bool Changed = false; 4367adc3a2bSChandler Carruth 4377adc3a2bSChandler Carruth // Keep track of whether the callgraph is known to be up-to-date or not. 4387adc3a2bSChandler Carruth // The CGSSC pass manager runs two types of passes: 4397adc3a2bSChandler Carruth // CallGraphSCC Passes and other random function passes. Because other 4407adc3a2bSChandler Carruth // random function passes are not CallGraph aware, they may clobber the 4417adc3a2bSChandler Carruth // call graph by introducing new calls or deleting other ones. This flag 4427adc3a2bSChandler Carruth // is set to false when we run a function pass so that we know to clean up 4437adc3a2bSChandler Carruth // the callgraph when we need to run a CGSCCPass again. 4447adc3a2bSChandler Carruth bool CallGraphUpToDate = true; 4457adc3a2bSChandler Carruth 4467adc3a2bSChandler Carruth // Run all passes on current SCC. 4477adc3a2bSChandler Carruth for (unsigned PassNo = 0, e = getNumContainedPasses(); 4487adc3a2bSChandler Carruth PassNo != e; ++PassNo) { 4497adc3a2bSChandler Carruth Pass *P = getContainedPass(PassNo); 4507adc3a2bSChandler Carruth 4517adc3a2bSChandler Carruth // If we're in -debug-pass=Executions mode, construct the SCC node list, 4527adc3a2bSChandler Carruth // otherwise avoid constructing this string as it is expensive. 4537adc3a2bSChandler Carruth if (isPassDebuggingExecutionsOrMore()) { 4547adc3a2bSChandler Carruth std::string Functions; 4557adc3a2bSChandler Carruth #ifndef NDEBUG 4567adc3a2bSChandler Carruth raw_string_ostream OS(Functions); 457*871affc5SKazu Hirata ListSeparator LS; 458*871affc5SKazu Hirata for (const CallGraphNode *CGN : CurSCC) { 459*871affc5SKazu Hirata OS << LS; 460*871affc5SKazu Hirata CGN->print(OS); 4617adc3a2bSChandler Carruth } 4627adc3a2bSChandler Carruth OS.flush(); 4637adc3a2bSChandler Carruth #endif 4647adc3a2bSChandler Carruth dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions); 4657adc3a2bSChandler Carruth } 4667adc3a2bSChandler Carruth dumpRequiredSet(P); 4677adc3a2bSChandler Carruth 4687adc3a2bSChandler Carruth initializeAnalysisImpl(P); 4697adc3a2bSChandler Carruth 470b1f4e597Sserge-sans-paille #ifdef EXPENSIVE_CHECKS 471b1f4e597Sserge-sans-paille uint64_t RefHash = StructuralHash(CG.getModule()); 472b1f4e597Sserge-sans-paille #endif 4737adc3a2bSChandler Carruth 474b1f4e597Sserge-sans-paille // Actually run this pass on the current SCC. 475b1f4e597Sserge-sans-paille bool LocalChanged = 476b1f4e597Sserge-sans-paille RunPassOnSCC(P, CurSCC, CG, CallGraphUpToDate, DevirtualizedCall); 477b1f4e597Sserge-sans-paille 478b1f4e597Sserge-sans-paille Changed |= LocalChanged; 479b1f4e597Sserge-sans-paille 480b1f4e597Sserge-sans-paille #ifdef EXPENSIVE_CHECKS 481b1f4e597Sserge-sans-paille if (!LocalChanged && (RefHash != StructuralHash(CG.getModule()))) { 482b1f4e597Sserge-sans-paille llvm::errs() << "Pass modifies its input and doesn't report it: " 483b1f4e597Sserge-sans-paille << P->getPassName() << "\n"; 484b1f4e597Sserge-sans-paille llvm_unreachable("Pass modifies its input and doesn't report it"); 485b1f4e597Sserge-sans-paille } 486b1f4e597Sserge-sans-paille #endif 487b1f4e597Sserge-sans-paille if (LocalChanged) 4887adc3a2bSChandler Carruth dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, ""); 4897adc3a2bSChandler Carruth dumpPreservedSet(P); 4907adc3a2bSChandler Carruth 4917adc3a2bSChandler Carruth verifyPreservedAnalysis(P); 49222961821Sserge-sans-paille if (LocalChanged) 4937adc3a2bSChandler Carruth removeNotPreservedAnalysis(P); 4947adc3a2bSChandler Carruth recordAvailableAnalysis(P); 4957adc3a2bSChandler Carruth removeDeadPasses(P, "", ON_CG_MSG); 4967adc3a2bSChandler Carruth } 4977adc3a2bSChandler Carruth 4987adc3a2bSChandler Carruth // If the callgraph was left out of date (because the last pass run was a 4997adc3a2bSChandler Carruth // functionpass), refresh it before we move on to the next SCC. 5007adc3a2bSChandler Carruth if (!CallGraphUpToDate) 5017adc3a2bSChandler Carruth DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); 5027adc3a2bSChandler Carruth return Changed; 5037adc3a2bSChandler Carruth } 5047adc3a2bSChandler Carruth 5057adc3a2bSChandler Carruth /// Execute all of the passes scheduled for execution. Keep track of 5067adc3a2bSChandler Carruth /// whether any of the passes modifies the module, and if so, return true. 5077adc3a2bSChandler Carruth bool CGPassManager::runOnModule(Module &M) { 5087adc3a2bSChandler Carruth CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); 5097adc3a2bSChandler Carruth bool Changed = doInitialization(CG); 5107adc3a2bSChandler Carruth 5117adc3a2bSChandler Carruth // Walk the callgraph in bottom-up SCC order. 512ca8c2f76SEvgeniy Stepanov scc_iterator<CallGraph*> CGI = scc_begin(&CG); 5137adc3a2bSChandler Carruth 514aa641a51SAndrew Kaylor CallGraphSCC CurSCC(CG, &CGI); 5157adc3a2bSChandler Carruth while (!CGI.isAtEnd()) { 5167adc3a2bSChandler Carruth // Copy the current SCC and increment past it so that the pass can hack 5177adc3a2bSChandler Carruth // on the SCC if it wants to without invalidating our iterator. 518ca8c2f76SEvgeniy Stepanov const std::vector<CallGraphNode *> &NodeVec = *CGI; 5191665d863SDavid Majnemer CurSCC.initialize(NodeVec); 5207adc3a2bSChandler Carruth ++CGI; 5217adc3a2bSChandler Carruth 5227adc3a2bSChandler Carruth // At the top level, we run all the passes in this pass manager on the 5237adc3a2bSChandler Carruth // functions in this SCC. However, we support iterative compilation in the 5247adc3a2bSChandler Carruth // case where a function pass devirtualizes a call to a function. For 5257adc3a2bSChandler Carruth // example, it is very common for a function pass (often GVN or instcombine) 5267adc3a2bSChandler Carruth // to eliminate the addressing that feeds into a call. With that improved 5277adc3a2bSChandler Carruth // information, we would like the call to be an inline candidate, infer 5287adc3a2bSChandler Carruth // mod-ref information etc. 5297adc3a2bSChandler Carruth // 5307adc3a2bSChandler Carruth // Because of this, we allow iteration up to a specified iteration count. 5317adc3a2bSChandler Carruth // This only happens in the case of a devirtualized call, so we only burn 5327adc3a2bSChandler Carruth // compile time in the case that we're making progress. We also have a hard 5337adc3a2bSChandler Carruth // iteration count limit in case there is crazy code. 5347adc3a2bSChandler Carruth unsigned Iteration = 0; 5357adc3a2bSChandler Carruth bool DevirtualizedCall = false; 5367adc3a2bSChandler Carruth do { 537d34e60caSNicola Zaghen LLVM_DEBUG(if (Iteration) dbgs() 538d34e60caSNicola Zaghen << " SCCPASSMGR: Re-visiting SCC, iteration #" << Iteration 539d34e60caSNicola Zaghen << '\n'); 5407adc3a2bSChandler Carruth DevirtualizedCall = false; 5417adc3a2bSChandler Carruth Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall); 5423c811ce4SArthur Eubanks } while (Iteration++ < MaxDevirtIterations && DevirtualizedCall); 5437adc3a2bSChandler Carruth 5447adc3a2bSChandler Carruth if (DevirtualizedCall) 545ca8c2f76SEvgeniy Stepanov LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " 546ca8c2f76SEvgeniy Stepanov << Iteration 5473c811ce4SArthur Eubanks << " times, due to -max-devirt-iterations\n"); 5487adc3a2bSChandler Carruth 5498a950275SCraig Topper MaxSCCIterations.updateMax(Iteration); 550ca8c2f76SEvgeniy Stepanov } 551ca8c2f76SEvgeniy Stepanov Changed |= doFinalization(CG); 5527adc3a2bSChandler Carruth return Changed; 5537adc3a2bSChandler Carruth } 5547adc3a2bSChandler Carruth 5557adc3a2bSChandler Carruth /// Initialize CG 5567adc3a2bSChandler Carruth bool CGPassManager::doInitialization(CallGraph &CG) { 5577adc3a2bSChandler Carruth bool Changed = false; 5587adc3a2bSChandler Carruth for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { 5597adc3a2bSChandler Carruth if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { 5607adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 5617adc3a2bSChandler Carruth "Invalid CGPassManager member"); 5627adc3a2bSChandler Carruth Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule()); 5637adc3a2bSChandler Carruth } else { 5647adc3a2bSChandler Carruth Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG); 5657adc3a2bSChandler Carruth } 5667adc3a2bSChandler Carruth } 5677adc3a2bSChandler Carruth return Changed; 5687adc3a2bSChandler Carruth } 5697adc3a2bSChandler Carruth 5707adc3a2bSChandler Carruth /// Finalize CG 5717adc3a2bSChandler Carruth bool CGPassManager::doFinalization(CallGraph &CG) { 5727adc3a2bSChandler Carruth bool Changed = false; 5737adc3a2bSChandler Carruth for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { 5747adc3a2bSChandler Carruth if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { 5757adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 5767adc3a2bSChandler Carruth "Invalid CGPassManager member"); 5777adc3a2bSChandler Carruth Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule()); 5787adc3a2bSChandler Carruth } else { 5797adc3a2bSChandler Carruth Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG); 5807adc3a2bSChandler Carruth } 5817adc3a2bSChandler Carruth } 5827adc3a2bSChandler Carruth return Changed; 5837adc3a2bSChandler Carruth } 5847adc3a2bSChandler Carruth 5857adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5867adc3a2bSChandler Carruth // CallGraphSCC Implementation 5877adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5887adc3a2bSChandler Carruth 5897adc3a2bSChandler Carruth /// This informs the SCC and the pass manager that the specified 5907adc3a2bSChandler Carruth /// Old node has been deleted, and New is to be used in its place. 5917adc3a2bSChandler Carruth void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) { 5927adc3a2bSChandler Carruth assert(Old != New && "Should not replace node with self"); 5937adc3a2bSChandler Carruth for (unsigned i = 0; ; ++i) { 5947adc3a2bSChandler Carruth assert(i != Nodes.size() && "Node not in SCC"); 5957adc3a2bSChandler Carruth if (Nodes[i] != Old) continue; 59672277ecdSJohannes Doerfert if (New) 5977adc3a2bSChandler Carruth Nodes[i] = New; 59872277ecdSJohannes Doerfert else 59972277ecdSJohannes Doerfert Nodes.erase(Nodes.begin() + i); 6007adc3a2bSChandler Carruth break; 6017adc3a2bSChandler Carruth } 6027adc3a2bSChandler Carruth 6037adc3a2bSChandler Carruth // Update the active scc_iterator so that it doesn't contain dangling 6047adc3a2bSChandler Carruth // pointers to the old CallGraphNode. 6057adc3a2bSChandler Carruth scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context; 6067adc3a2bSChandler Carruth CGI->ReplaceNode(Old, New); 6077adc3a2bSChandler Carruth } 6087adc3a2bSChandler Carruth 60993702575SJohannes Doerfert void CallGraphSCC::DeleteNode(CallGraphNode *Old) { 610df675890SJohannes Doerfert ReplaceNode(Old, /*New=*/nullptr); 61193702575SJohannes Doerfert } 61293702575SJohannes Doerfert 6137adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 6147adc3a2bSChandler Carruth // CallGraphSCCPass Implementation 6157adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 6167adc3a2bSChandler Carruth 6177adc3a2bSChandler Carruth /// Assign pass manager to manage this pass. 6187adc3a2bSChandler Carruth void CallGraphSCCPass::assignPassManager(PMStack &PMS, 6197adc3a2bSChandler Carruth PassManagerType PreferredType) { 6207adc3a2bSChandler Carruth // Find CGPassManager 6217adc3a2bSChandler Carruth while (!PMS.empty() && 6227adc3a2bSChandler Carruth PMS.top()->getPassManagerType() > PMT_CallGraphPassManager) 6237adc3a2bSChandler Carruth PMS.pop(); 6247adc3a2bSChandler Carruth 6257adc3a2bSChandler Carruth assert(!PMS.empty() && "Unable to handle Call Graph Pass"); 6267adc3a2bSChandler Carruth CGPassManager *CGP; 6277adc3a2bSChandler Carruth 6287adc3a2bSChandler Carruth if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager) 6297adc3a2bSChandler Carruth CGP = (CGPassManager*)PMS.top(); 6307adc3a2bSChandler Carruth else { 6317adc3a2bSChandler Carruth // Create new Call Graph SCC Pass Manager if it does not exist. 6327adc3a2bSChandler Carruth assert(!PMS.empty() && "Unable to create Call Graph Pass Manager"); 6337adc3a2bSChandler Carruth PMDataManager *PMD = PMS.top(); 6347adc3a2bSChandler Carruth 6357adc3a2bSChandler Carruth // [1] Create new Call Graph Pass Manager 6367adc3a2bSChandler Carruth CGP = new CGPassManager(); 6377adc3a2bSChandler Carruth 6387adc3a2bSChandler Carruth // [2] Set up new manager's top level manager 6397adc3a2bSChandler Carruth PMTopLevelManager *TPM = PMD->getTopLevelManager(); 6407adc3a2bSChandler Carruth TPM->addIndirectPassManager(CGP); 6417adc3a2bSChandler Carruth 6427adc3a2bSChandler Carruth // [3] Assign manager to manage this new manager. This may create 6437adc3a2bSChandler Carruth // and push new managers into PMS 6447adc3a2bSChandler Carruth Pass *P = CGP; 6457adc3a2bSChandler Carruth TPM->schedulePass(P); 6467adc3a2bSChandler Carruth 6477adc3a2bSChandler Carruth // [4] Push new manager into PMS 6487adc3a2bSChandler Carruth PMS.push(CGP); 6497adc3a2bSChandler Carruth } 6507adc3a2bSChandler Carruth 6517adc3a2bSChandler Carruth CGP->add(this); 6527adc3a2bSChandler Carruth } 6537adc3a2bSChandler Carruth 6547adc3a2bSChandler Carruth /// For this class, we declare that we require and preserve the call graph. 6557adc3a2bSChandler Carruth /// If the derived class implements this method, it should 6567adc3a2bSChandler Carruth /// always explicitly call the implementation here. 6577adc3a2bSChandler Carruth void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const { 6587adc3a2bSChandler Carruth AU.addRequired<CallGraphWrapperPass>(); 6597adc3a2bSChandler Carruth AU.addPreserved<CallGraphWrapperPass>(); 6607adc3a2bSChandler Carruth } 6617adc3a2bSChandler Carruth 6627adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 6637adc3a2bSChandler Carruth // PrintCallGraphPass Implementation 6647adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 6657adc3a2bSChandler Carruth 6667adc3a2bSChandler Carruth namespace { 667fa6434beSEugene Zelenko 6687adc3a2bSChandler Carruth /// PrintCallGraphPass - Print a Module corresponding to a call graph. 6697adc3a2bSChandler Carruth /// 6707adc3a2bSChandler Carruth class PrintCallGraphPass : public CallGraphSCCPass { 6717adc3a2bSChandler Carruth std::string Banner; 672fa6434beSEugene Zelenko raw_ostream &OS; // raw_ostream to print on. 6737adc3a2bSChandler Carruth 6747adc3a2bSChandler Carruth public: 6757adc3a2bSChandler Carruth static char ID; 676fa6434beSEugene Zelenko 677fa6434beSEugene Zelenko PrintCallGraphPass(const std::string &B, raw_ostream &OS) 678fa6434beSEugene Zelenko : CallGraphSCCPass(ID), Banner(B), OS(OS) {} 6797adc3a2bSChandler Carruth 6807adc3a2bSChandler Carruth void getAnalysisUsage(AnalysisUsage &AU) const override { 6817adc3a2bSChandler Carruth AU.setPreservesAll(); 6827adc3a2bSChandler Carruth } 6837adc3a2bSChandler Carruth 6847adc3a2bSChandler Carruth bool runOnSCC(CallGraphSCC &SCC) override { 6857d463921SYaron Keren bool BannerPrinted = false; 686062b3fedSMehdi Amini auto PrintBannerOnce = [&]() { 687062b3fedSMehdi Amini if (BannerPrinted) 688062b3fedSMehdi Amini return; 689fa6434beSEugene Zelenko OS << Banner; 690062b3fedSMehdi Amini BannerPrinted = true; 691062b3fedSMehdi Amini }; 6927254d3c5SFedor Sergeev 6937254d3c5SFedor Sergeev bool NeedModule = llvm::forcePrintModuleIR(); 6947254d3c5SFedor Sergeev if (isFunctionInPrintList("*") && NeedModule) { 6957254d3c5SFedor Sergeev PrintBannerOnce(); 6967254d3c5SFedor Sergeev OS << "\n"; 6977254d3c5SFedor Sergeev SCC.getCallGraph().getModule().print(OS, nullptr); 6987254d3c5SFedor Sergeev return false; 6997254d3c5SFedor Sergeev } 7007254d3c5SFedor Sergeev bool FoundFunction = false; 7017adc3a2bSChandler Carruth for (CallGraphNode *CGN : SCC) { 7027d463921SYaron Keren if (Function *F = CGN->getFunction()) { 7037d463921SYaron Keren if (!F->isDeclaration() && isFunctionInPrintList(F->getName())) { 7047254d3c5SFedor Sergeev FoundFunction = true; 7057254d3c5SFedor Sergeev if (!NeedModule) { 706062b3fedSMehdi Amini PrintBannerOnce(); 707fa6434beSEugene Zelenko F->print(OS); 708062b3fedSMehdi Amini } 7097254d3c5SFedor Sergeev } 710fa6434beSEugene Zelenko } else if (isFunctionInPrintList("*")) { 711062b3fedSMehdi Amini PrintBannerOnce(); 712fa6434beSEugene Zelenko OS << "\nPrinting <null> Function\n"; 7137adc3a2bSChandler Carruth } 714062b3fedSMehdi Amini } 7157254d3c5SFedor Sergeev if (NeedModule && FoundFunction) { 7167254d3c5SFedor Sergeev PrintBannerOnce(); 7177254d3c5SFedor Sergeev OS << "\n"; 7187254d3c5SFedor Sergeev SCC.getCallGraph().getModule().print(OS, nullptr); 7197254d3c5SFedor Sergeev } 7207adc3a2bSChandler Carruth return false; 7217adc3a2bSChandler Carruth } 7221de4792cSYaron Keren 7231de4792cSYaron Keren StringRef getPassName() const override { return "Print CallGraph IR"; } 7247adc3a2bSChandler Carruth }; 7257adc3a2bSChandler Carruth 7267adc3a2bSChandler Carruth } // end anonymous namespace. 7277adc3a2bSChandler Carruth 7287adc3a2bSChandler Carruth char PrintCallGraphPass::ID = 0; 7297adc3a2bSChandler Carruth 730fa6434beSEugene Zelenko Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &OS, 7317adc3a2bSChandler Carruth const std::string &Banner) const { 732fa6434beSEugene Zelenko return new PrintCallGraphPass(Banner, OS); 7337adc3a2bSChandler Carruth } 7347adc3a2bSChandler Carruth 735b37a70f4SRichard Trieu static std::string getDescription(const CallGraphSCC &SCC) { 736b37a70f4SRichard Trieu std::string Desc = "SCC ("; 737*871affc5SKazu Hirata ListSeparator LS; 738b37a70f4SRichard Trieu for (CallGraphNode *CGN : SCC) { 739*871affc5SKazu Hirata Desc += LS; 740b37a70f4SRichard Trieu Function *F = CGN->getFunction(); 741b37a70f4SRichard Trieu if (F) 742b37a70f4SRichard Trieu Desc += F->getName(); 743b37a70f4SRichard Trieu else 744b37a70f4SRichard Trieu Desc += "<<null function>>"; 745b37a70f4SRichard Trieu } 746b37a70f4SRichard Trieu Desc += ")"; 747b37a70f4SRichard Trieu return Desc; 748b37a70f4SRichard Trieu } 749b37a70f4SRichard Trieu 750aa641a51SAndrew Kaylor bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const { 751b37a70f4SRichard Trieu OptPassGate &Gate = 752b37a70f4SRichard Trieu SCC.getCallGraph().getModule().getContext().getOptPassGate(); 753b37a70f4SRichard Trieu return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(SCC)); 754aa641a51SAndrew Kaylor } 755bbacddfeSMehdi Amini 756bbacddfeSMehdi Amini char DummyCGSCCPass::ID = 0; 757fa6434beSEugene Zelenko 758bbacddfeSMehdi Amini INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false, 759bbacddfeSMehdi Amini false) 760