17adc3a2bSChandler Carruth //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===// 27adc3a2bSChandler Carruth // 37adc3a2bSChandler Carruth // The LLVM Compiler Infrastructure 47adc3a2bSChandler Carruth // 57adc3a2bSChandler Carruth // This file is distributed under the University of Illinois Open Source 67adc3a2bSChandler Carruth // License. See LICENSE.TXT for details. 77adc3a2bSChandler Carruth // 87adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 97adc3a2bSChandler Carruth // 107adc3a2bSChandler Carruth // This file implements the CallGraphSCCPass class, which is used for passes 117adc3a2bSChandler Carruth // which are implemented as bottom-up traversals on the call graph. Because 127adc3a2bSChandler Carruth // there may be cycles in the call graph, passes of this type operate on the 137adc3a2bSChandler Carruth // call-graph in SCC order: that is, they process function bottom-up, except for 147adc3a2bSChandler Carruth // recursive functions, which they process all at once. 157adc3a2bSChandler Carruth // 167adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 177adc3a2bSChandler Carruth 187adc3a2bSChandler Carruth #include "llvm/Analysis/CallGraphSCCPass.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" 237adc3a2bSChandler Carruth #include "llvm/IR/IntrinsicInst.h" 247adc3a2bSChandler Carruth #include "llvm/IR/LLVMContext.h" 257adc3a2bSChandler Carruth #include "llvm/IR/LegacyPassManagers.h" 26aa641a51SAndrew Kaylor #include "llvm/IR/OptBisect.h" 277adc3a2bSChandler Carruth #include "llvm/Support/CommandLine.h" 287adc3a2bSChandler Carruth #include "llvm/Support/Debug.h" 297adc3a2bSChandler Carruth #include "llvm/Support/Timer.h" 307adc3a2bSChandler Carruth #include "llvm/Support/raw_ostream.h" 317adc3a2bSChandler Carruth using namespace llvm; 327adc3a2bSChandler Carruth 337adc3a2bSChandler Carruth #define DEBUG_TYPE "cgscc-passmgr" 347adc3a2bSChandler Carruth 357adc3a2bSChandler Carruth static cl::opt<unsigned> 367adc3a2bSChandler Carruth MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4)); 377adc3a2bSChandler Carruth 387adc3a2bSChandler Carruth STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC"); 397adc3a2bSChandler Carruth 407adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 417adc3a2bSChandler Carruth // CGPassManager 427adc3a2bSChandler Carruth // 437adc3a2bSChandler Carruth /// CGPassManager manages FPPassManagers and CallGraphSCCPasses. 447adc3a2bSChandler Carruth 457adc3a2bSChandler Carruth namespace { 467adc3a2bSChandler Carruth 477adc3a2bSChandler Carruth class CGPassManager : public ModulePass, public PMDataManager { 487adc3a2bSChandler Carruth public: 497adc3a2bSChandler Carruth static char ID; 507adc3a2bSChandler Carruth explicit CGPassManager() 517adc3a2bSChandler Carruth : ModulePass(ID), PMDataManager() { } 527adc3a2bSChandler Carruth 537adc3a2bSChandler Carruth /// Execute all of the passes scheduled for execution. Keep track of 547adc3a2bSChandler Carruth /// whether any of the passes modifies the module, and if so, return true. 557adc3a2bSChandler Carruth bool runOnModule(Module &M) override; 567adc3a2bSChandler Carruth 577adc3a2bSChandler Carruth using ModulePass::doInitialization; 587adc3a2bSChandler Carruth using ModulePass::doFinalization; 597adc3a2bSChandler Carruth 607adc3a2bSChandler Carruth bool doInitialization(CallGraph &CG); 617adc3a2bSChandler Carruth bool doFinalization(CallGraph &CG); 627adc3a2bSChandler Carruth 637adc3a2bSChandler Carruth /// Pass Manager itself does not invalidate any analysis info. 647adc3a2bSChandler Carruth void getAnalysisUsage(AnalysisUsage &Info) const override { 657adc3a2bSChandler Carruth // CGPassManager walks SCC and it needs CallGraph. 667adc3a2bSChandler Carruth Info.addRequired<CallGraphWrapperPass>(); 677adc3a2bSChandler Carruth Info.setPreservesAll(); 687adc3a2bSChandler Carruth } 697adc3a2bSChandler Carruth 70117296c0SMehdi Amini StringRef getPassName() const override { return "CallGraph Pass Manager"; } 717adc3a2bSChandler Carruth 727adc3a2bSChandler Carruth PMDataManager *getAsPMDataManager() override { return this; } 737adc3a2bSChandler Carruth Pass *getAsPass() override { return this; } 747adc3a2bSChandler Carruth 757adc3a2bSChandler Carruth // Print passes managed by this manager 767adc3a2bSChandler Carruth void dumpPassStructure(unsigned Offset) override { 777adc3a2bSChandler Carruth errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n"; 787adc3a2bSChandler Carruth for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 797adc3a2bSChandler Carruth Pass *P = getContainedPass(Index); 807adc3a2bSChandler Carruth P->dumpPassStructure(Offset + 1); 817adc3a2bSChandler Carruth dumpLastUses(P, Offset+1); 827adc3a2bSChandler Carruth } 837adc3a2bSChandler Carruth } 847adc3a2bSChandler Carruth 857adc3a2bSChandler Carruth Pass *getContainedPass(unsigned N) { 867adc3a2bSChandler Carruth assert(N < PassVector.size() && "Pass number out of range!"); 877adc3a2bSChandler Carruth return static_cast<Pass *>(PassVector[N]); 887adc3a2bSChandler Carruth } 897adc3a2bSChandler Carruth 907adc3a2bSChandler Carruth PassManagerType getPassManagerType() const override { 917adc3a2bSChandler Carruth return PMT_CallGraphPassManager; 927adc3a2bSChandler Carruth } 937adc3a2bSChandler Carruth 947adc3a2bSChandler Carruth private: 957adc3a2bSChandler Carruth bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, 967adc3a2bSChandler Carruth bool &DevirtualizedCall); 977adc3a2bSChandler Carruth 987adc3a2bSChandler Carruth bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, 997adc3a2bSChandler Carruth CallGraph &CG, bool &CallGraphUpToDate, 1007adc3a2bSChandler Carruth bool &DevirtualizedCall); 101c137c28cSMehdi Amini bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG, 1027adc3a2bSChandler Carruth bool IsCheckingMode); 1037adc3a2bSChandler Carruth }; 1047adc3a2bSChandler Carruth 1057adc3a2bSChandler Carruth } // end anonymous namespace. 1067adc3a2bSChandler Carruth 1077adc3a2bSChandler Carruth char CGPassManager::ID = 0; 1087adc3a2bSChandler Carruth 1097adc3a2bSChandler Carruth 1107adc3a2bSChandler Carruth bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, 1117adc3a2bSChandler Carruth CallGraph &CG, bool &CallGraphUpToDate, 1127adc3a2bSChandler Carruth bool &DevirtualizedCall) { 1137adc3a2bSChandler Carruth bool Changed = false; 1147adc3a2bSChandler Carruth PMDataManager *PM = P->getAsPMDataManager(); 1157adc3a2bSChandler Carruth 1167adc3a2bSChandler Carruth if (!PM) { 1177adc3a2bSChandler Carruth CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P; 1187adc3a2bSChandler Carruth if (!CallGraphUpToDate) { 1197adc3a2bSChandler Carruth DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); 1207adc3a2bSChandler Carruth CallGraphUpToDate = true; 1217adc3a2bSChandler Carruth } 1227adc3a2bSChandler Carruth 1237adc3a2bSChandler Carruth { 1247adc3a2bSChandler Carruth TimeRegion PassTimer(getPassTimer(CGSP)); 1257adc3a2bSChandler Carruth Changed = CGSP->runOnSCC(CurSCC); 1267adc3a2bSChandler Carruth } 1277adc3a2bSChandler Carruth 1287adc3a2bSChandler Carruth // After the CGSCCPass is done, when assertions are enabled, use 1297adc3a2bSChandler Carruth // RefreshCallGraph to verify that the callgraph was correctly updated. 1307adc3a2bSChandler Carruth #ifndef NDEBUG 1317adc3a2bSChandler Carruth if (Changed) 1327adc3a2bSChandler Carruth RefreshCallGraph(CurSCC, CG, true); 1337adc3a2bSChandler Carruth #endif 1347adc3a2bSChandler Carruth 1357adc3a2bSChandler Carruth return Changed; 1367adc3a2bSChandler Carruth } 1377adc3a2bSChandler Carruth 1387adc3a2bSChandler Carruth 1397adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 1407adc3a2bSChandler Carruth "Invalid CGPassManager member"); 1417adc3a2bSChandler Carruth FPPassManager *FPP = (FPPassManager*)P; 1427adc3a2bSChandler Carruth 1437adc3a2bSChandler Carruth // Run pass P on all functions in the current SCC. 1447adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) { 1457adc3a2bSChandler Carruth if (Function *F = CGN->getFunction()) { 1467adc3a2bSChandler Carruth dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName()); 1477adc3a2bSChandler Carruth { 1487adc3a2bSChandler Carruth TimeRegion PassTimer(getPassTimer(FPP)); 1497adc3a2bSChandler Carruth Changed |= FPP->runOnFunction(*F); 1507adc3a2bSChandler Carruth } 1517adc3a2bSChandler Carruth F->getContext().yield(); 1527adc3a2bSChandler Carruth } 1537adc3a2bSChandler Carruth } 1547adc3a2bSChandler Carruth 1557adc3a2bSChandler Carruth // The function pass(es) modified the IR, they may have clobbered the 1567adc3a2bSChandler Carruth // callgraph. 1577adc3a2bSChandler Carruth if (Changed && CallGraphUpToDate) { 1587adc3a2bSChandler Carruth DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " 1597adc3a2bSChandler Carruth << P->getPassName() << '\n'); 1607adc3a2bSChandler Carruth CallGraphUpToDate = false; 1617adc3a2bSChandler Carruth } 1627adc3a2bSChandler Carruth return Changed; 1637adc3a2bSChandler Carruth } 1647adc3a2bSChandler Carruth 1657adc3a2bSChandler Carruth 1667adc3a2bSChandler Carruth /// Scan the functions in the specified CFG and resync the 1677adc3a2bSChandler Carruth /// callgraph with the call sites found in it. This is used after 1687adc3a2bSChandler Carruth /// FunctionPasses have potentially munged the callgraph, and can be used after 1697adc3a2bSChandler Carruth /// CallGraphSCC passes to verify that they correctly updated the callgraph. 1707adc3a2bSChandler Carruth /// 1717adc3a2bSChandler Carruth /// This function returns true if it devirtualized an existing function call, 1727adc3a2bSChandler Carruth /// meaning it turned an indirect call into a direct call. This happens when 1737adc3a2bSChandler Carruth /// a function pass like GVN optimizes away stuff feeding the indirect call. 1747adc3a2bSChandler Carruth /// This never happens in checking mode. 1757adc3a2bSChandler Carruth /// 176c137c28cSMehdi Amini bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG, 177c137c28cSMehdi Amini bool CheckingMode) { 1787adc3a2bSChandler Carruth DenseMap<Value*, CallGraphNode*> CallSites; 1797adc3a2bSChandler Carruth 1807adc3a2bSChandler Carruth DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size() 1817adc3a2bSChandler Carruth << " nodes:\n"; 1827adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) 1837adc3a2bSChandler Carruth CGN->dump(); 1847adc3a2bSChandler Carruth ); 1857adc3a2bSChandler Carruth 1867adc3a2bSChandler Carruth bool MadeChange = false; 1877adc3a2bSChandler Carruth bool DevirtualizedCall = false; 1887adc3a2bSChandler Carruth 1897adc3a2bSChandler Carruth // Scan all functions in the SCC. 1907adc3a2bSChandler Carruth unsigned FunctionNo = 0; 1917adc3a2bSChandler Carruth for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end(); 1927adc3a2bSChandler Carruth SCCIdx != E; ++SCCIdx, ++FunctionNo) { 1937adc3a2bSChandler Carruth CallGraphNode *CGN = *SCCIdx; 1947adc3a2bSChandler Carruth Function *F = CGN->getFunction(); 1957adc3a2bSChandler Carruth if (!F || F->isDeclaration()) continue; 1967adc3a2bSChandler Carruth 1977adc3a2bSChandler Carruth // Walk the function body looking for call sites. Sync up the call sites in 1987adc3a2bSChandler Carruth // CGN with those actually in the function. 1997adc3a2bSChandler Carruth 2007adc3a2bSChandler Carruth // Keep track of the number of direct and indirect calls that were 2017adc3a2bSChandler Carruth // invalidated and removed. 2027adc3a2bSChandler Carruth unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0; 2037adc3a2bSChandler Carruth 2047adc3a2bSChandler Carruth // Get the set of call sites currently in the function. 2057adc3a2bSChandler Carruth for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) { 2067adc3a2bSChandler Carruth // If this call site is null, then the function pass deleted the call 2077adc3a2bSChandler Carruth // entirely and the WeakVH nulled it out. 2087adc3a2bSChandler Carruth if (!I->first || 2097adc3a2bSChandler Carruth // If we've already seen this call site, then the FunctionPass RAUW'd 2107adc3a2bSChandler Carruth // one call with another, which resulted in two "uses" in the edge 2117adc3a2bSChandler Carruth // list of the same call. 2127adc3a2bSChandler Carruth CallSites.count(I->first) || 2137adc3a2bSChandler Carruth 2147adc3a2bSChandler Carruth // If the call edge is not from a call or invoke, or it is a 2157adc3a2bSChandler Carruth // instrinsic call, then the function pass RAUW'd a call with 2167adc3a2bSChandler Carruth // another value. This can happen when constant folding happens 2177adc3a2bSChandler Carruth // of well known functions etc. 2187adc3a2bSChandler Carruth !CallSite(I->first) || 2197adc3a2bSChandler Carruth (CallSite(I->first).getCalledFunction() && 2207adc3a2bSChandler Carruth CallSite(I->first).getCalledFunction()->isIntrinsic() && 2217adc3a2bSChandler Carruth Intrinsic::isLeaf( 2227adc3a2bSChandler Carruth CallSite(I->first).getCalledFunction()->getIntrinsicID()))) { 2237adc3a2bSChandler Carruth assert(!CheckingMode && 2247adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 2257adc3a2bSChandler Carruth 2267adc3a2bSChandler Carruth // If this was an indirect call site, count it. 2277adc3a2bSChandler Carruth if (!I->second->getFunction()) 2287adc3a2bSChandler Carruth ++NumIndirectRemoved; 2297adc3a2bSChandler Carruth else 2307adc3a2bSChandler Carruth ++NumDirectRemoved; 2317adc3a2bSChandler Carruth 2327adc3a2bSChandler Carruth // Just remove the edge from the set of callees, keep track of whether 2337adc3a2bSChandler Carruth // I points to the last element of the vector. 2347adc3a2bSChandler Carruth bool WasLast = I + 1 == E; 2357adc3a2bSChandler Carruth CGN->removeCallEdge(I); 2367adc3a2bSChandler Carruth 2377adc3a2bSChandler Carruth // If I pointed to the last element of the vector, we have to bail out: 2387adc3a2bSChandler Carruth // iterator checking rejects comparisons of the resultant pointer with 2397adc3a2bSChandler Carruth // end. 2407adc3a2bSChandler Carruth if (WasLast) 2417adc3a2bSChandler Carruth break; 2427adc3a2bSChandler Carruth E = CGN->end(); 2437adc3a2bSChandler Carruth continue; 2447adc3a2bSChandler Carruth } 2457adc3a2bSChandler Carruth 2467adc3a2bSChandler Carruth assert(!CallSites.count(I->first) && 2477adc3a2bSChandler Carruth "Call site occurs in node multiple times"); 2487adc3a2bSChandler Carruth 2497adc3a2bSChandler Carruth CallSite CS(I->first); 2507adc3a2bSChandler Carruth if (CS) { 2517adc3a2bSChandler Carruth Function *Callee = CS.getCalledFunction(); 2527adc3a2bSChandler Carruth // Ignore intrinsics because they're not really function calls. 2537adc3a2bSChandler Carruth if (!Callee || !(Callee->isIntrinsic())) 2547adc3a2bSChandler Carruth CallSites.insert(std::make_pair(I->first, I->second)); 2557adc3a2bSChandler Carruth } 2567adc3a2bSChandler Carruth ++I; 2577adc3a2bSChandler Carruth } 2587adc3a2bSChandler Carruth 2597adc3a2bSChandler Carruth // Loop over all of the instructions in the function, getting the callsites. 2607adc3a2bSChandler Carruth // Keep track of the number of direct/indirect calls added. 2617adc3a2bSChandler Carruth unsigned NumDirectAdded = 0, NumIndirectAdded = 0; 2627adc3a2bSChandler Carruth 263aa209150SBenjamin Kramer for (BasicBlock &BB : *F) 264aa209150SBenjamin Kramer for (Instruction &I : BB) { 265aa209150SBenjamin Kramer CallSite CS(&I); 2667adc3a2bSChandler Carruth if (!CS) continue; 2677adc3a2bSChandler Carruth Function *Callee = CS.getCalledFunction(); 2687adc3a2bSChandler Carruth if (Callee && Callee->isIntrinsic()) continue; 2697adc3a2bSChandler Carruth 2707adc3a2bSChandler Carruth // If this call site already existed in the callgraph, just verify it 2717adc3a2bSChandler Carruth // matches up to expectations and remove it from CallSites. 2727adc3a2bSChandler Carruth DenseMap<Value*, CallGraphNode*>::iterator ExistingIt = 2737adc3a2bSChandler Carruth CallSites.find(CS.getInstruction()); 2747adc3a2bSChandler Carruth if (ExistingIt != CallSites.end()) { 2757adc3a2bSChandler Carruth CallGraphNode *ExistingNode = ExistingIt->second; 2767adc3a2bSChandler Carruth 2777adc3a2bSChandler Carruth // Remove from CallSites since we have now seen it. 2787adc3a2bSChandler Carruth CallSites.erase(ExistingIt); 2797adc3a2bSChandler Carruth 2807adc3a2bSChandler Carruth // Verify that the callee is right. 2817adc3a2bSChandler Carruth if (ExistingNode->getFunction() == CS.getCalledFunction()) 2827adc3a2bSChandler Carruth continue; 2837adc3a2bSChandler Carruth 2847adc3a2bSChandler Carruth // If we are in checking mode, we are not allowed to actually mutate 2857adc3a2bSChandler Carruth // the callgraph. If this is a case where we can infer that the 2867adc3a2bSChandler Carruth // callgraph is less precise than it could be (e.g. an indirect call 2877adc3a2bSChandler Carruth // site could be turned direct), don't reject it in checking mode, and 2887adc3a2bSChandler Carruth // don't tweak it to be more precise. 2897adc3a2bSChandler Carruth if (CheckingMode && CS.getCalledFunction() && 2907adc3a2bSChandler Carruth ExistingNode->getFunction() == nullptr) 2917adc3a2bSChandler Carruth continue; 2927adc3a2bSChandler Carruth 2937adc3a2bSChandler Carruth assert(!CheckingMode && 2947adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 2957adc3a2bSChandler Carruth 2967adc3a2bSChandler Carruth // If not, we either went from a direct call to indirect, indirect to 2977adc3a2bSChandler Carruth // direct, or direct to different direct. 2987adc3a2bSChandler Carruth CallGraphNode *CalleeNode; 2997adc3a2bSChandler Carruth if (Function *Callee = CS.getCalledFunction()) { 3007adc3a2bSChandler Carruth CalleeNode = CG.getOrInsertFunction(Callee); 3017adc3a2bSChandler Carruth // Keep track of whether we turned an indirect call into a direct 3027adc3a2bSChandler Carruth // one. 3037adc3a2bSChandler Carruth if (!ExistingNode->getFunction()) { 3047adc3a2bSChandler Carruth DevirtualizedCall = true; 3057adc3a2bSChandler Carruth DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '" 3067adc3a2bSChandler Carruth << Callee->getName() << "'\n"); 3077adc3a2bSChandler Carruth } 3087adc3a2bSChandler Carruth } else { 3097adc3a2bSChandler Carruth CalleeNode = CG.getCallsExternalNode(); 3107adc3a2bSChandler Carruth } 3117adc3a2bSChandler Carruth 3127adc3a2bSChandler Carruth // Update the edge target in CGN. 3137adc3a2bSChandler Carruth CGN->replaceCallEdge(CS, CS, CalleeNode); 3147adc3a2bSChandler Carruth MadeChange = true; 3157adc3a2bSChandler Carruth continue; 3167adc3a2bSChandler Carruth } 3177adc3a2bSChandler Carruth 3187adc3a2bSChandler Carruth assert(!CheckingMode && 3197adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 3207adc3a2bSChandler Carruth 3217adc3a2bSChandler Carruth // If the call site didn't exist in the CGN yet, add it. 3227adc3a2bSChandler Carruth CallGraphNode *CalleeNode; 3237adc3a2bSChandler Carruth if (Function *Callee = CS.getCalledFunction()) { 3247adc3a2bSChandler Carruth CalleeNode = CG.getOrInsertFunction(Callee); 3257adc3a2bSChandler Carruth ++NumDirectAdded; 3267adc3a2bSChandler Carruth } else { 3277adc3a2bSChandler Carruth CalleeNode = CG.getCallsExternalNode(); 3287adc3a2bSChandler Carruth ++NumIndirectAdded; 3297adc3a2bSChandler Carruth } 3307adc3a2bSChandler Carruth 3317adc3a2bSChandler Carruth CGN->addCalledFunction(CS, CalleeNode); 3327adc3a2bSChandler Carruth MadeChange = true; 3337adc3a2bSChandler Carruth } 3347adc3a2bSChandler Carruth 3357adc3a2bSChandler Carruth // We scanned the old callgraph node, removing invalidated call sites and 3367adc3a2bSChandler Carruth // then added back newly found call sites. One thing that can happen is 3377adc3a2bSChandler Carruth // that an old indirect call site was deleted and replaced with a new direct 3387adc3a2bSChandler Carruth // call. In this case, we have devirtualized a call, and CGSCCPM would like 3397adc3a2bSChandler Carruth // to iteratively optimize the new code. Unfortunately, we don't really 3407adc3a2bSChandler Carruth // have a great way to detect when this happens. As an approximation, we 3417adc3a2bSChandler Carruth // just look at whether the number of indirect calls is reduced and the 3427adc3a2bSChandler Carruth // number of direct calls is increased. There are tons of ways to fool this 3437adc3a2bSChandler Carruth // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a 3447adc3a2bSChandler Carruth // direct call) but this is close enough. 3457adc3a2bSChandler Carruth if (NumIndirectRemoved > NumIndirectAdded && 3467adc3a2bSChandler Carruth NumDirectRemoved < NumDirectAdded) 3477adc3a2bSChandler Carruth DevirtualizedCall = true; 3487adc3a2bSChandler Carruth 3497adc3a2bSChandler Carruth // After scanning this function, if we still have entries in callsites, then 3507adc3a2bSChandler Carruth // they are dangling pointers. WeakVH should save us for this, so abort if 3517adc3a2bSChandler Carruth // this happens. 3527adc3a2bSChandler Carruth assert(CallSites.empty() && "Dangling pointers found in call sites map"); 3537adc3a2bSChandler Carruth 3547adc3a2bSChandler Carruth // Periodically do an explicit clear to remove tombstones when processing 3557adc3a2bSChandler Carruth // large scc's. 3567adc3a2bSChandler Carruth if ((FunctionNo & 15) == 15) 3577adc3a2bSChandler Carruth CallSites.clear(); 3587adc3a2bSChandler Carruth } 3597adc3a2bSChandler Carruth 3607adc3a2bSChandler Carruth DEBUG(if (MadeChange) { 3617adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n"; 3627adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) 3637adc3a2bSChandler Carruth CGN->dump(); 3647adc3a2bSChandler Carruth if (DevirtualizedCall) 3657adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n"; 3667adc3a2bSChandler Carruth 3677adc3a2bSChandler Carruth } else { 3687adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n"; 3697adc3a2bSChandler Carruth } 3707adc3a2bSChandler Carruth ); 3717adc3a2bSChandler Carruth (void)MadeChange; 3727adc3a2bSChandler Carruth 3737adc3a2bSChandler Carruth return DevirtualizedCall; 3747adc3a2bSChandler Carruth } 3757adc3a2bSChandler Carruth 3767adc3a2bSChandler Carruth /// Execute the body of the entire pass manager on the specified SCC. 3777adc3a2bSChandler Carruth /// This keeps track of whether a function pass devirtualizes 3787adc3a2bSChandler Carruth /// any calls and returns it in DevirtualizedCall. 3797adc3a2bSChandler Carruth bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, 3807adc3a2bSChandler Carruth bool &DevirtualizedCall) { 3817adc3a2bSChandler Carruth bool Changed = false; 3827adc3a2bSChandler Carruth 3837adc3a2bSChandler Carruth // Keep track of whether the callgraph is known to be up-to-date or not. 3847adc3a2bSChandler Carruth // The CGSSC pass manager runs two types of passes: 3857adc3a2bSChandler Carruth // CallGraphSCC Passes and other random function passes. Because other 3867adc3a2bSChandler Carruth // random function passes are not CallGraph aware, they may clobber the 3877adc3a2bSChandler Carruth // call graph by introducing new calls or deleting other ones. This flag 3887adc3a2bSChandler Carruth // is set to false when we run a function pass so that we know to clean up 3897adc3a2bSChandler Carruth // the callgraph when we need to run a CGSCCPass again. 3907adc3a2bSChandler Carruth bool CallGraphUpToDate = true; 3917adc3a2bSChandler Carruth 3927adc3a2bSChandler Carruth // Run all passes on current SCC. 3937adc3a2bSChandler Carruth for (unsigned PassNo = 0, e = getNumContainedPasses(); 3947adc3a2bSChandler Carruth PassNo != e; ++PassNo) { 3957adc3a2bSChandler Carruth Pass *P = getContainedPass(PassNo); 3967adc3a2bSChandler Carruth 3977adc3a2bSChandler Carruth // If we're in -debug-pass=Executions mode, construct the SCC node list, 3987adc3a2bSChandler Carruth // otherwise avoid constructing this string as it is expensive. 3997adc3a2bSChandler Carruth if (isPassDebuggingExecutionsOrMore()) { 4007adc3a2bSChandler Carruth std::string Functions; 4017adc3a2bSChandler Carruth #ifndef NDEBUG 4027adc3a2bSChandler Carruth raw_string_ostream OS(Functions); 4037adc3a2bSChandler Carruth for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end(); 4047adc3a2bSChandler Carruth I != E; ++I) { 4057adc3a2bSChandler Carruth if (I != CurSCC.begin()) OS << ", "; 4067adc3a2bSChandler Carruth (*I)->print(OS); 4077adc3a2bSChandler Carruth } 4087adc3a2bSChandler Carruth OS.flush(); 4097adc3a2bSChandler Carruth #endif 4107adc3a2bSChandler Carruth dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions); 4117adc3a2bSChandler Carruth } 4127adc3a2bSChandler Carruth dumpRequiredSet(P); 4137adc3a2bSChandler Carruth 4147adc3a2bSChandler Carruth initializeAnalysisImpl(P); 4157adc3a2bSChandler Carruth 4167adc3a2bSChandler Carruth // Actually run this pass on the current SCC. 4177adc3a2bSChandler Carruth Changed |= RunPassOnSCC(P, CurSCC, CG, 4187adc3a2bSChandler Carruth CallGraphUpToDate, DevirtualizedCall); 4197adc3a2bSChandler Carruth 4207adc3a2bSChandler Carruth if (Changed) 4217adc3a2bSChandler Carruth dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, ""); 4227adc3a2bSChandler Carruth dumpPreservedSet(P); 4237adc3a2bSChandler Carruth 4247adc3a2bSChandler Carruth verifyPreservedAnalysis(P); 4257adc3a2bSChandler Carruth removeNotPreservedAnalysis(P); 4267adc3a2bSChandler Carruth recordAvailableAnalysis(P); 4277adc3a2bSChandler Carruth removeDeadPasses(P, "", ON_CG_MSG); 4287adc3a2bSChandler Carruth } 4297adc3a2bSChandler Carruth 4307adc3a2bSChandler Carruth // If the callgraph was left out of date (because the last pass run was a 4317adc3a2bSChandler Carruth // functionpass), refresh it before we move on to the next SCC. 4327adc3a2bSChandler Carruth if (!CallGraphUpToDate) 4337adc3a2bSChandler Carruth DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); 4347adc3a2bSChandler Carruth return Changed; 4357adc3a2bSChandler Carruth } 4367adc3a2bSChandler Carruth 4377adc3a2bSChandler Carruth /// Execute all of the passes scheduled for execution. Keep track of 4387adc3a2bSChandler Carruth /// whether any of the passes modifies the module, and if so, return true. 4397adc3a2bSChandler Carruth bool CGPassManager::runOnModule(Module &M) { 4407adc3a2bSChandler Carruth CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); 4417adc3a2bSChandler Carruth bool Changed = doInitialization(CG); 4427adc3a2bSChandler Carruth 4437adc3a2bSChandler Carruth // Walk the callgraph in bottom-up SCC order. 4447adc3a2bSChandler Carruth scc_iterator<CallGraph*> CGI = scc_begin(&CG); 4457adc3a2bSChandler Carruth 446aa641a51SAndrew Kaylor CallGraphSCC CurSCC(CG, &CGI); 4477adc3a2bSChandler Carruth while (!CGI.isAtEnd()) { 4487adc3a2bSChandler Carruth // Copy the current SCC and increment past it so that the pass can hack 4497adc3a2bSChandler Carruth // on the SCC if it wants to without invalidating our iterator. 4507adc3a2bSChandler Carruth const std::vector<CallGraphNode *> &NodeVec = *CGI; 4511665d863SDavid Majnemer CurSCC.initialize(NodeVec); 4527adc3a2bSChandler Carruth ++CGI; 4537adc3a2bSChandler Carruth 4547adc3a2bSChandler Carruth // At the top level, we run all the passes in this pass manager on the 4557adc3a2bSChandler Carruth // functions in this SCC. However, we support iterative compilation in the 4567adc3a2bSChandler Carruth // case where a function pass devirtualizes a call to a function. For 4577adc3a2bSChandler Carruth // example, it is very common for a function pass (often GVN or instcombine) 4587adc3a2bSChandler Carruth // to eliminate the addressing that feeds into a call. With that improved 4597adc3a2bSChandler Carruth // information, we would like the call to be an inline candidate, infer 4607adc3a2bSChandler Carruth // mod-ref information etc. 4617adc3a2bSChandler Carruth // 4627adc3a2bSChandler Carruth // Because of this, we allow iteration up to a specified iteration count. 4637adc3a2bSChandler Carruth // This only happens in the case of a devirtualized call, so we only burn 4647adc3a2bSChandler Carruth // compile time in the case that we're making progress. We also have a hard 4657adc3a2bSChandler Carruth // iteration count limit in case there is crazy code. 4667adc3a2bSChandler Carruth unsigned Iteration = 0; 4677adc3a2bSChandler Carruth bool DevirtualizedCall = false; 4687adc3a2bSChandler Carruth do { 4697adc3a2bSChandler Carruth DEBUG(if (Iteration) 4707adc3a2bSChandler Carruth dbgs() << " SCCPASSMGR: Re-visiting SCC, iteration #" 4717adc3a2bSChandler Carruth << Iteration << '\n'); 4727adc3a2bSChandler Carruth DevirtualizedCall = false; 4737adc3a2bSChandler Carruth Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall); 4747adc3a2bSChandler Carruth } while (Iteration++ < MaxIterations && DevirtualizedCall); 4757adc3a2bSChandler Carruth 4767adc3a2bSChandler Carruth if (DevirtualizedCall) 4777adc3a2bSChandler Carruth DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " << Iteration 4787adc3a2bSChandler Carruth << " times, due to -max-cg-scc-iterations\n"); 4797adc3a2bSChandler Carruth 4807adc3a2bSChandler Carruth if (Iteration > MaxSCCIterations) 4817adc3a2bSChandler Carruth MaxSCCIterations = Iteration; 4827adc3a2bSChandler Carruth 4837adc3a2bSChandler Carruth } 4847adc3a2bSChandler Carruth Changed |= doFinalization(CG); 4857adc3a2bSChandler Carruth return Changed; 4867adc3a2bSChandler Carruth } 4877adc3a2bSChandler Carruth 4887adc3a2bSChandler Carruth 4897adc3a2bSChandler Carruth /// Initialize CG 4907adc3a2bSChandler Carruth bool CGPassManager::doInitialization(CallGraph &CG) { 4917adc3a2bSChandler Carruth bool Changed = false; 4927adc3a2bSChandler Carruth for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { 4937adc3a2bSChandler Carruth if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { 4947adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 4957adc3a2bSChandler Carruth "Invalid CGPassManager member"); 4967adc3a2bSChandler Carruth Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule()); 4977adc3a2bSChandler Carruth } else { 4987adc3a2bSChandler Carruth Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG); 4997adc3a2bSChandler Carruth } 5007adc3a2bSChandler Carruth } 5017adc3a2bSChandler Carruth return Changed; 5027adc3a2bSChandler Carruth } 5037adc3a2bSChandler Carruth 5047adc3a2bSChandler Carruth /// Finalize CG 5057adc3a2bSChandler Carruth bool CGPassManager::doFinalization(CallGraph &CG) { 5067adc3a2bSChandler Carruth bool Changed = false; 5077adc3a2bSChandler Carruth for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { 5087adc3a2bSChandler Carruth if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { 5097adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 5107adc3a2bSChandler Carruth "Invalid CGPassManager member"); 5117adc3a2bSChandler Carruth Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule()); 5127adc3a2bSChandler Carruth } else { 5137adc3a2bSChandler Carruth Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG); 5147adc3a2bSChandler Carruth } 5157adc3a2bSChandler Carruth } 5167adc3a2bSChandler Carruth return Changed; 5177adc3a2bSChandler Carruth } 5187adc3a2bSChandler Carruth 5197adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5207adc3a2bSChandler Carruth // CallGraphSCC Implementation 5217adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5227adc3a2bSChandler Carruth 5237adc3a2bSChandler Carruth /// This informs the SCC and the pass manager that the specified 5247adc3a2bSChandler Carruth /// Old node has been deleted, and New is to be used in its place. 5257adc3a2bSChandler Carruth void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) { 5267adc3a2bSChandler Carruth assert(Old != New && "Should not replace node with self"); 5277adc3a2bSChandler Carruth for (unsigned i = 0; ; ++i) { 5287adc3a2bSChandler Carruth assert(i != Nodes.size() && "Node not in SCC"); 5297adc3a2bSChandler Carruth if (Nodes[i] != Old) continue; 5307adc3a2bSChandler Carruth Nodes[i] = New; 5317adc3a2bSChandler Carruth break; 5327adc3a2bSChandler Carruth } 5337adc3a2bSChandler Carruth 5347adc3a2bSChandler Carruth // Update the active scc_iterator so that it doesn't contain dangling 5357adc3a2bSChandler Carruth // pointers to the old CallGraphNode. 5367adc3a2bSChandler Carruth scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context; 5377adc3a2bSChandler Carruth CGI->ReplaceNode(Old, New); 5387adc3a2bSChandler Carruth } 5397adc3a2bSChandler Carruth 5407adc3a2bSChandler Carruth 5417adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5427adc3a2bSChandler Carruth // CallGraphSCCPass Implementation 5437adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5447adc3a2bSChandler Carruth 5457adc3a2bSChandler Carruth /// Assign pass manager to manage this pass. 5467adc3a2bSChandler Carruth void CallGraphSCCPass::assignPassManager(PMStack &PMS, 5477adc3a2bSChandler Carruth PassManagerType PreferredType) { 5487adc3a2bSChandler Carruth // Find CGPassManager 5497adc3a2bSChandler Carruth while (!PMS.empty() && 5507adc3a2bSChandler Carruth PMS.top()->getPassManagerType() > PMT_CallGraphPassManager) 5517adc3a2bSChandler Carruth PMS.pop(); 5527adc3a2bSChandler Carruth 5537adc3a2bSChandler Carruth assert(!PMS.empty() && "Unable to handle Call Graph Pass"); 5547adc3a2bSChandler Carruth CGPassManager *CGP; 5557adc3a2bSChandler Carruth 5567adc3a2bSChandler Carruth if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager) 5577adc3a2bSChandler Carruth CGP = (CGPassManager*)PMS.top(); 5587adc3a2bSChandler Carruth else { 5597adc3a2bSChandler Carruth // Create new Call Graph SCC Pass Manager if it does not exist. 5607adc3a2bSChandler Carruth assert(!PMS.empty() && "Unable to create Call Graph Pass Manager"); 5617adc3a2bSChandler Carruth PMDataManager *PMD = PMS.top(); 5627adc3a2bSChandler Carruth 5637adc3a2bSChandler Carruth // [1] Create new Call Graph Pass Manager 5647adc3a2bSChandler Carruth CGP = new CGPassManager(); 5657adc3a2bSChandler Carruth 5667adc3a2bSChandler Carruth // [2] Set up new manager's top level manager 5677adc3a2bSChandler Carruth PMTopLevelManager *TPM = PMD->getTopLevelManager(); 5687adc3a2bSChandler Carruth TPM->addIndirectPassManager(CGP); 5697adc3a2bSChandler Carruth 5707adc3a2bSChandler Carruth // [3] Assign manager to manage this new manager. This may create 5717adc3a2bSChandler Carruth // and push new managers into PMS 5727adc3a2bSChandler Carruth Pass *P = CGP; 5737adc3a2bSChandler Carruth TPM->schedulePass(P); 5747adc3a2bSChandler Carruth 5757adc3a2bSChandler Carruth // [4] Push new manager into PMS 5767adc3a2bSChandler Carruth PMS.push(CGP); 5777adc3a2bSChandler Carruth } 5787adc3a2bSChandler Carruth 5797adc3a2bSChandler Carruth CGP->add(this); 5807adc3a2bSChandler Carruth } 5817adc3a2bSChandler Carruth 5827adc3a2bSChandler Carruth /// For this class, we declare that we require and preserve the call graph. 5837adc3a2bSChandler Carruth /// If the derived class implements this method, it should 5847adc3a2bSChandler Carruth /// always explicitly call the implementation here. 5857adc3a2bSChandler Carruth void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const { 5867adc3a2bSChandler Carruth AU.addRequired<CallGraphWrapperPass>(); 5877adc3a2bSChandler Carruth AU.addPreserved<CallGraphWrapperPass>(); 5887adc3a2bSChandler Carruth } 5897adc3a2bSChandler Carruth 5907adc3a2bSChandler Carruth 5917adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5927adc3a2bSChandler Carruth // PrintCallGraphPass Implementation 5937adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 5947adc3a2bSChandler Carruth 5957adc3a2bSChandler Carruth namespace { 5967adc3a2bSChandler Carruth /// PrintCallGraphPass - Print a Module corresponding to a call graph. 5977adc3a2bSChandler Carruth /// 5987adc3a2bSChandler Carruth class PrintCallGraphPass : public CallGraphSCCPass { 5997adc3a2bSChandler Carruth std::string Banner; 6007adc3a2bSChandler Carruth raw_ostream &Out; // raw_ostream to print on. 6017adc3a2bSChandler Carruth 6027adc3a2bSChandler Carruth public: 6037adc3a2bSChandler Carruth static char ID; 6047adc3a2bSChandler Carruth PrintCallGraphPass(const std::string &B, raw_ostream &o) 6057adc3a2bSChandler Carruth : CallGraphSCCPass(ID), Banner(B), Out(o) {} 6067adc3a2bSChandler Carruth 6077adc3a2bSChandler Carruth void getAnalysisUsage(AnalysisUsage &AU) const override { 6087adc3a2bSChandler Carruth AU.setPreservesAll(); 6097adc3a2bSChandler Carruth } 6107adc3a2bSChandler Carruth 6117adc3a2bSChandler Carruth bool runOnSCC(CallGraphSCC &SCC) override { 612062b3fedSMehdi Amini auto PrintBannerOnce = [&] () { 613062b3fedSMehdi Amini static bool BannerPrinted = false; 614062b3fedSMehdi Amini if (BannerPrinted) 615062b3fedSMehdi Amini return; 6167adc3a2bSChandler Carruth Out << Banner; 617062b3fedSMehdi Amini BannerPrinted = true; 618062b3fedSMehdi Amini }; 6197adc3a2bSChandler Carruth for (CallGraphNode *CGN : SCC) { 6200f1762caSWeiming Zhao if (CGN->getFunction()) { 621062b3fedSMehdi Amini if (isFunctionInPrintList(CGN->getFunction()->getName())) { 622062b3fedSMehdi Amini PrintBannerOnce(); 6237adc3a2bSChandler Carruth CGN->getFunction()->print(Out); 624062b3fedSMehdi Amini } 625062b3fedSMehdi Amini } else if (llvm::isFunctionInPrintList("*")) { 626062b3fedSMehdi Amini PrintBannerOnce(); 6277adc3a2bSChandler Carruth Out << "\nPrinting <null> Function\n"; 6287adc3a2bSChandler Carruth } 629062b3fedSMehdi Amini } 6307adc3a2bSChandler Carruth return false; 6317adc3a2bSChandler Carruth } 632*1de4792cSYaron Keren 633*1de4792cSYaron Keren StringRef getPassName() const override { return "Print CallGraph IR"; } 6347adc3a2bSChandler Carruth }; 6357adc3a2bSChandler Carruth 6367adc3a2bSChandler Carruth } // end anonymous namespace. 6377adc3a2bSChandler Carruth 6387adc3a2bSChandler Carruth char PrintCallGraphPass::ID = 0; 6397adc3a2bSChandler Carruth 6407adc3a2bSChandler Carruth Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O, 6417adc3a2bSChandler Carruth const std::string &Banner) const { 6427adc3a2bSChandler Carruth return new PrintCallGraphPass(Banner, O); 6437adc3a2bSChandler Carruth } 6447adc3a2bSChandler Carruth 645aa641a51SAndrew Kaylor bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const { 646aa641a51SAndrew Kaylor return !SCC.getCallGraph().getModule() 647aa641a51SAndrew Kaylor .getContext() 648aa641a51SAndrew Kaylor .getOptBisect() 649aa641a51SAndrew Kaylor .shouldRunPass(this, SCC); 650aa641a51SAndrew Kaylor } 651bbacddfeSMehdi Amini 652bbacddfeSMehdi Amini char DummyCGSCCPass::ID = 0; 653bbacddfeSMehdi Amini INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false, 654bbacddfeSMehdi Amini false) 655