1*7adc3a2bSChandler Carruth //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===// 2*7adc3a2bSChandler Carruth // 3*7adc3a2bSChandler Carruth // The LLVM Compiler Infrastructure 4*7adc3a2bSChandler Carruth // 5*7adc3a2bSChandler Carruth // This file is distributed under the University of Illinois Open Source 6*7adc3a2bSChandler Carruth // License. See LICENSE.TXT for details. 7*7adc3a2bSChandler Carruth // 8*7adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 9*7adc3a2bSChandler Carruth // 10*7adc3a2bSChandler Carruth // This file implements the CallGraphSCCPass class, which is used for passes 11*7adc3a2bSChandler Carruth // which are implemented as bottom-up traversals on the call graph. Because 12*7adc3a2bSChandler Carruth // there may be cycles in the call graph, passes of this type operate on the 13*7adc3a2bSChandler Carruth // call-graph in SCC order: that is, they process function bottom-up, except for 14*7adc3a2bSChandler Carruth // recursive functions, which they process all at once. 15*7adc3a2bSChandler Carruth // 16*7adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 17*7adc3a2bSChandler Carruth 18*7adc3a2bSChandler Carruth #include "llvm/Analysis/CallGraphSCCPass.h" 19*7adc3a2bSChandler Carruth #include "llvm/ADT/SCCIterator.h" 20*7adc3a2bSChandler Carruth #include "llvm/ADT/Statistic.h" 21*7adc3a2bSChandler Carruth #include "llvm/Analysis/CallGraph.h" 22*7adc3a2bSChandler Carruth #include "llvm/IR/Function.h" 23*7adc3a2bSChandler Carruth #include "llvm/IR/IntrinsicInst.h" 24*7adc3a2bSChandler Carruth #include "llvm/IR/LLVMContext.h" 25*7adc3a2bSChandler Carruth #include "llvm/IR/LegacyPassManagers.h" 26*7adc3a2bSChandler Carruth #include "llvm/Support/CommandLine.h" 27*7adc3a2bSChandler Carruth #include "llvm/Support/Debug.h" 28*7adc3a2bSChandler Carruth #include "llvm/Support/Timer.h" 29*7adc3a2bSChandler Carruth #include "llvm/Support/raw_ostream.h" 30*7adc3a2bSChandler Carruth using namespace llvm; 31*7adc3a2bSChandler Carruth 32*7adc3a2bSChandler Carruth #define DEBUG_TYPE "cgscc-passmgr" 33*7adc3a2bSChandler Carruth 34*7adc3a2bSChandler Carruth static cl::opt<unsigned> 35*7adc3a2bSChandler Carruth MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4)); 36*7adc3a2bSChandler Carruth 37*7adc3a2bSChandler Carruth STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC"); 38*7adc3a2bSChandler Carruth 39*7adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 40*7adc3a2bSChandler Carruth // CGPassManager 41*7adc3a2bSChandler Carruth // 42*7adc3a2bSChandler Carruth /// CGPassManager manages FPPassManagers and CallGraphSCCPasses. 43*7adc3a2bSChandler Carruth 44*7adc3a2bSChandler Carruth namespace { 45*7adc3a2bSChandler Carruth 46*7adc3a2bSChandler Carruth class CGPassManager : public ModulePass, public PMDataManager { 47*7adc3a2bSChandler Carruth public: 48*7adc3a2bSChandler Carruth static char ID; 49*7adc3a2bSChandler Carruth explicit CGPassManager() 50*7adc3a2bSChandler Carruth : ModulePass(ID), PMDataManager() { } 51*7adc3a2bSChandler Carruth 52*7adc3a2bSChandler Carruth /// Execute all of the passes scheduled for execution. Keep track of 53*7adc3a2bSChandler Carruth /// whether any of the passes modifies the module, and if so, return true. 54*7adc3a2bSChandler Carruth bool runOnModule(Module &M) override; 55*7adc3a2bSChandler Carruth 56*7adc3a2bSChandler Carruth using ModulePass::doInitialization; 57*7adc3a2bSChandler Carruth using ModulePass::doFinalization; 58*7adc3a2bSChandler Carruth 59*7adc3a2bSChandler Carruth bool doInitialization(CallGraph &CG); 60*7adc3a2bSChandler Carruth bool doFinalization(CallGraph &CG); 61*7adc3a2bSChandler Carruth 62*7adc3a2bSChandler Carruth /// Pass Manager itself does not invalidate any analysis info. 63*7adc3a2bSChandler Carruth void getAnalysisUsage(AnalysisUsage &Info) const override { 64*7adc3a2bSChandler Carruth // CGPassManager walks SCC and it needs CallGraph. 65*7adc3a2bSChandler Carruth Info.addRequired<CallGraphWrapperPass>(); 66*7adc3a2bSChandler Carruth Info.setPreservesAll(); 67*7adc3a2bSChandler Carruth } 68*7adc3a2bSChandler Carruth 69*7adc3a2bSChandler Carruth const char *getPassName() const override { 70*7adc3a2bSChandler Carruth return "CallGraph Pass Manager"; 71*7adc3a2bSChandler Carruth } 72*7adc3a2bSChandler Carruth 73*7adc3a2bSChandler Carruth PMDataManager *getAsPMDataManager() override { return this; } 74*7adc3a2bSChandler Carruth Pass *getAsPass() override { return this; } 75*7adc3a2bSChandler Carruth 76*7adc3a2bSChandler Carruth // Print passes managed by this manager 77*7adc3a2bSChandler Carruth void dumpPassStructure(unsigned Offset) override { 78*7adc3a2bSChandler Carruth errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n"; 79*7adc3a2bSChandler Carruth for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 80*7adc3a2bSChandler Carruth Pass *P = getContainedPass(Index); 81*7adc3a2bSChandler Carruth P->dumpPassStructure(Offset + 1); 82*7adc3a2bSChandler Carruth dumpLastUses(P, Offset+1); 83*7adc3a2bSChandler Carruth } 84*7adc3a2bSChandler Carruth } 85*7adc3a2bSChandler Carruth 86*7adc3a2bSChandler Carruth Pass *getContainedPass(unsigned N) { 87*7adc3a2bSChandler Carruth assert(N < PassVector.size() && "Pass number out of range!"); 88*7adc3a2bSChandler Carruth return static_cast<Pass *>(PassVector[N]); 89*7adc3a2bSChandler Carruth } 90*7adc3a2bSChandler Carruth 91*7adc3a2bSChandler Carruth PassManagerType getPassManagerType() const override { 92*7adc3a2bSChandler Carruth return PMT_CallGraphPassManager; 93*7adc3a2bSChandler Carruth } 94*7adc3a2bSChandler Carruth 95*7adc3a2bSChandler Carruth private: 96*7adc3a2bSChandler Carruth bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, 97*7adc3a2bSChandler Carruth bool &DevirtualizedCall); 98*7adc3a2bSChandler Carruth 99*7adc3a2bSChandler Carruth bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, 100*7adc3a2bSChandler Carruth CallGraph &CG, bool &CallGraphUpToDate, 101*7adc3a2bSChandler Carruth bool &DevirtualizedCall); 102*7adc3a2bSChandler Carruth bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG, 103*7adc3a2bSChandler Carruth bool IsCheckingMode); 104*7adc3a2bSChandler Carruth }; 105*7adc3a2bSChandler Carruth 106*7adc3a2bSChandler Carruth } // end anonymous namespace. 107*7adc3a2bSChandler Carruth 108*7adc3a2bSChandler Carruth char CGPassManager::ID = 0; 109*7adc3a2bSChandler Carruth 110*7adc3a2bSChandler Carruth 111*7adc3a2bSChandler Carruth bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC, 112*7adc3a2bSChandler Carruth CallGraph &CG, bool &CallGraphUpToDate, 113*7adc3a2bSChandler Carruth bool &DevirtualizedCall) { 114*7adc3a2bSChandler Carruth bool Changed = false; 115*7adc3a2bSChandler Carruth PMDataManager *PM = P->getAsPMDataManager(); 116*7adc3a2bSChandler Carruth 117*7adc3a2bSChandler Carruth if (!PM) { 118*7adc3a2bSChandler Carruth CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P; 119*7adc3a2bSChandler Carruth if (!CallGraphUpToDate) { 120*7adc3a2bSChandler Carruth DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); 121*7adc3a2bSChandler Carruth CallGraphUpToDate = true; 122*7adc3a2bSChandler Carruth } 123*7adc3a2bSChandler Carruth 124*7adc3a2bSChandler Carruth { 125*7adc3a2bSChandler Carruth TimeRegion PassTimer(getPassTimer(CGSP)); 126*7adc3a2bSChandler Carruth Changed = CGSP->runOnSCC(CurSCC); 127*7adc3a2bSChandler Carruth } 128*7adc3a2bSChandler Carruth 129*7adc3a2bSChandler Carruth // After the CGSCCPass is done, when assertions are enabled, use 130*7adc3a2bSChandler Carruth // RefreshCallGraph to verify that the callgraph was correctly updated. 131*7adc3a2bSChandler Carruth #ifndef NDEBUG 132*7adc3a2bSChandler Carruth if (Changed) 133*7adc3a2bSChandler Carruth RefreshCallGraph(CurSCC, CG, true); 134*7adc3a2bSChandler Carruth #endif 135*7adc3a2bSChandler Carruth 136*7adc3a2bSChandler Carruth return Changed; 137*7adc3a2bSChandler Carruth } 138*7adc3a2bSChandler Carruth 139*7adc3a2bSChandler Carruth 140*7adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 141*7adc3a2bSChandler Carruth "Invalid CGPassManager member"); 142*7adc3a2bSChandler Carruth FPPassManager *FPP = (FPPassManager*)P; 143*7adc3a2bSChandler Carruth 144*7adc3a2bSChandler Carruth // Run pass P on all functions in the current SCC. 145*7adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) { 146*7adc3a2bSChandler Carruth if (Function *F = CGN->getFunction()) { 147*7adc3a2bSChandler Carruth dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName()); 148*7adc3a2bSChandler Carruth { 149*7adc3a2bSChandler Carruth TimeRegion PassTimer(getPassTimer(FPP)); 150*7adc3a2bSChandler Carruth Changed |= FPP->runOnFunction(*F); 151*7adc3a2bSChandler Carruth } 152*7adc3a2bSChandler Carruth F->getContext().yield(); 153*7adc3a2bSChandler Carruth } 154*7adc3a2bSChandler Carruth } 155*7adc3a2bSChandler Carruth 156*7adc3a2bSChandler Carruth // The function pass(es) modified the IR, they may have clobbered the 157*7adc3a2bSChandler Carruth // callgraph. 158*7adc3a2bSChandler Carruth if (Changed && CallGraphUpToDate) { 159*7adc3a2bSChandler Carruth DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " 160*7adc3a2bSChandler Carruth << P->getPassName() << '\n'); 161*7adc3a2bSChandler Carruth CallGraphUpToDate = false; 162*7adc3a2bSChandler Carruth } 163*7adc3a2bSChandler Carruth return Changed; 164*7adc3a2bSChandler Carruth } 165*7adc3a2bSChandler Carruth 166*7adc3a2bSChandler Carruth 167*7adc3a2bSChandler Carruth /// Scan the functions in the specified CFG and resync the 168*7adc3a2bSChandler Carruth /// callgraph with the call sites found in it. This is used after 169*7adc3a2bSChandler Carruth /// FunctionPasses have potentially munged the callgraph, and can be used after 170*7adc3a2bSChandler Carruth /// CallGraphSCC passes to verify that they correctly updated the callgraph. 171*7adc3a2bSChandler Carruth /// 172*7adc3a2bSChandler Carruth /// This function returns true if it devirtualized an existing function call, 173*7adc3a2bSChandler Carruth /// meaning it turned an indirect call into a direct call. This happens when 174*7adc3a2bSChandler Carruth /// a function pass like GVN optimizes away stuff feeding the indirect call. 175*7adc3a2bSChandler Carruth /// This never happens in checking mode. 176*7adc3a2bSChandler Carruth /// 177*7adc3a2bSChandler Carruth bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC, 178*7adc3a2bSChandler Carruth CallGraph &CG, bool CheckingMode) { 179*7adc3a2bSChandler Carruth DenseMap<Value*, CallGraphNode*> CallSites; 180*7adc3a2bSChandler Carruth 181*7adc3a2bSChandler Carruth DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size() 182*7adc3a2bSChandler Carruth << " nodes:\n"; 183*7adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) 184*7adc3a2bSChandler Carruth CGN->dump(); 185*7adc3a2bSChandler Carruth ); 186*7adc3a2bSChandler Carruth 187*7adc3a2bSChandler Carruth bool MadeChange = false; 188*7adc3a2bSChandler Carruth bool DevirtualizedCall = false; 189*7adc3a2bSChandler Carruth 190*7adc3a2bSChandler Carruth // Scan all functions in the SCC. 191*7adc3a2bSChandler Carruth unsigned FunctionNo = 0; 192*7adc3a2bSChandler Carruth for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end(); 193*7adc3a2bSChandler Carruth SCCIdx != E; ++SCCIdx, ++FunctionNo) { 194*7adc3a2bSChandler Carruth CallGraphNode *CGN = *SCCIdx; 195*7adc3a2bSChandler Carruth Function *F = CGN->getFunction(); 196*7adc3a2bSChandler Carruth if (!F || F->isDeclaration()) continue; 197*7adc3a2bSChandler Carruth 198*7adc3a2bSChandler Carruth // Walk the function body looking for call sites. Sync up the call sites in 199*7adc3a2bSChandler Carruth // CGN with those actually in the function. 200*7adc3a2bSChandler Carruth 201*7adc3a2bSChandler Carruth // Keep track of the number of direct and indirect calls that were 202*7adc3a2bSChandler Carruth // invalidated and removed. 203*7adc3a2bSChandler Carruth unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0; 204*7adc3a2bSChandler Carruth 205*7adc3a2bSChandler Carruth // Get the set of call sites currently in the function. 206*7adc3a2bSChandler Carruth for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) { 207*7adc3a2bSChandler Carruth // If this call site is null, then the function pass deleted the call 208*7adc3a2bSChandler Carruth // entirely and the WeakVH nulled it out. 209*7adc3a2bSChandler Carruth if (!I->first || 210*7adc3a2bSChandler Carruth // If we've already seen this call site, then the FunctionPass RAUW'd 211*7adc3a2bSChandler Carruth // one call with another, which resulted in two "uses" in the edge 212*7adc3a2bSChandler Carruth // list of the same call. 213*7adc3a2bSChandler Carruth CallSites.count(I->first) || 214*7adc3a2bSChandler Carruth 215*7adc3a2bSChandler Carruth // If the call edge is not from a call or invoke, or it is a 216*7adc3a2bSChandler Carruth // instrinsic call, then the function pass RAUW'd a call with 217*7adc3a2bSChandler Carruth // another value. This can happen when constant folding happens 218*7adc3a2bSChandler Carruth // of well known functions etc. 219*7adc3a2bSChandler Carruth !CallSite(I->first) || 220*7adc3a2bSChandler Carruth (CallSite(I->first).getCalledFunction() && 221*7adc3a2bSChandler Carruth CallSite(I->first).getCalledFunction()->isIntrinsic() && 222*7adc3a2bSChandler Carruth Intrinsic::isLeaf( 223*7adc3a2bSChandler Carruth CallSite(I->first).getCalledFunction()->getIntrinsicID()))) { 224*7adc3a2bSChandler Carruth assert(!CheckingMode && 225*7adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 226*7adc3a2bSChandler Carruth 227*7adc3a2bSChandler Carruth // If this was an indirect call site, count it. 228*7adc3a2bSChandler Carruth if (!I->second->getFunction()) 229*7adc3a2bSChandler Carruth ++NumIndirectRemoved; 230*7adc3a2bSChandler Carruth else 231*7adc3a2bSChandler Carruth ++NumDirectRemoved; 232*7adc3a2bSChandler Carruth 233*7adc3a2bSChandler Carruth // Just remove the edge from the set of callees, keep track of whether 234*7adc3a2bSChandler Carruth // I points to the last element of the vector. 235*7adc3a2bSChandler Carruth bool WasLast = I + 1 == E; 236*7adc3a2bSChandler Carruth CGN->removeCallEdge(I); 237*7adc3a2bSChandler Carruth 238*7adc3a2bSChandler Carruth // If I pointed to the last element of the vector, we have to bail out: 239*7adc3a2bSChandler Carruth // iterator checking rejects comparisons of the resultant pointer with 240*7adc3a2bSChandler Carruth // end. 241*7adc3a2bSChandler Carruth if (WasLast) 242*7adc3a2bSChandler Carruth break; 243*7adc3a2bSChandler Carruth E = CGN->end(); 244*7adc3a2bSChandler Carruth continue; 245*7adc3a2bSChandler Carruth } 246*7adc3a2bSChandler Carruth 247*7adc3a2bSChandler Carruth assert(!CallSites.count(I->first) && 248*7adc3a2bSChandler Carruth "Call site occurs in node multiple times"); 249*7adc3a2bSChandler Carruth 250*7adc3a2bSChandler Carruth CallSite CS(I->first); 251*7adc3a2bSChandler Carruth if (CS) { 252*7adc3a2bSChandler Carruth Function *Callee = CS.getCalledFunction(); 253*7adc3a2bSChandler Carruth // Ignore intrinsics because they're not really function calls. 254*7adc3a2bSChandler Carruth if (!Callee || !(Callee->isIntrinsic())) 255*7adc3a2bSChandler Carruth CallSites.insert(std::make_pair(I->first, I->second)); 256*7adc3a2bSChandler Carruth } 257*7adc3a2bSChandler Carruth ++I; 258*7adc3a2bSChandler Carruth } 259*7adc3a2bSChandler Carruth 260*7adc3a2bSChandler Carruth // Loop over all of the instructions in the function, getting the callsites. 261*7adc3a2bSChandler Carruth // Keep track of the number of direct/indirect calls added. 262*7adc3a2bSChandler Carruth unsigned NumDirectAdded = 0, NumIndirectAdded = 0; 263*7adc3a2bSChandler Carruth 264*7adc3a2bSChandler Carruth for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 265*7adc3a2bSChandler Carruth for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 266*7adc3a2bSChandler Carruth CallSite CS(cast<Value>(I)); 267*7adc3a2bSChandler Carruth if (!CS) continue; 268*7adc3a2bSChandler Carruth Function *Callee = CS.getCalledFunction(); 269*7adc3a2bSChandler Carruth if (Callee && Callee->isIntrinsic()) continue; 270*7adc3a2bSChandler Carruth 271*7adc3a2bSChandler Carruth // If this call site already existed in the callgraph, just verify it 272*7adc3a2bSChandler Carruth // matches up to expectations and remove it from CallSites. 273*7adc3a2bSChandler Carruth DenseMap<Value*, CallGraphNode*>::iterator ExistingIt = 274*7adc3a2bSChandler Carruth CallSites.find(CS.getInstruction()); 275*7adc3a2bSChandler Carruth if (ExistingIt != CallSites.end()) { 276*7adc3a2bSChandler Carruth CallGraphNode *ExistingNode = ExistingIt->second; 277*7adc3a2bSChandler Carruth 278*7adc3a2bSChandler Carruth // Remove from CallSites since we have now seen it. 279*7adc3a2bSChandler Carruth CallSites.erase(ExistingIt); 280*7adc3a2bSChandler Carruth 281*7adc3a2bSChandler Carruth // Verify that the callee is right. 282*7adc3a2bSChandler Carruth if (ExistingNode->getFunction() == CS.getCalledFunction()) 283*7adc3a2bSChandler Carruth continue; 284*7adc3a2bSChandler Carruth 285*7adc3a2bSChandler Carruth // If we are in checking mode, we are not allowed to actually mutate 286*7adc3a2bSChandler Carruth // the callgraph. If this is a case where we can infer that the 287*7adc3a2bSChandler Carruth // callgraph is less precise than it could be (e.g. an indirect call 288*7adc3a2bSChandler Carruth // site could be turned direct), don't reject it in checking mode, and 289*7adc3a2bSChandler Carruth // don't tweak it to be more precise. 290*7adc3a2bSChandler Carruth if (CheckingMode && CS.getCalledFunction() && 291*7adc3a2bSChandler Carruth ExistingNode->getFunction() == nullptr) 292*7adc3a2bSChandler Carruth continue; 293*7adc3a2bSChandler Carruth 294*7adc3a2bSChandler Carruth assert(!CheckingMode && 295*7adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 296*7adc3a2bSChandler Carruth 297*7adc3a2bSChandler Carruth // If not, we either went from a direct call to indirect, indirect to 298*7adc3a2bSChandler Carruth // direct, or direct to different direct. 299*7adc3a2bSChandler Carruth CallGraphNode *CalleeNode; 300*7adc3a2bSChandler Carruth if (Function *Callee = CS.getCalledFunction()) { 301*7adc3a2bSChandler Carruth CalleeNode = CG.getOrInsertFunction(Callee); 302*7adc3a2bSChandler Carruth // Keep track of whether we turned an indirect call into a direct 303*7adc3a2bSChandler Carruth // one. 304*7adc3a2bSChandler Carruth if (!ExistingNode->getFunction()) { 305*7adc3a2bSChandler Carruth DevirtualizedCall = true; 306*7adc3a2bSChandler Carruth DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '" 307*7adc3a2bSChandler Carruth << Callee->getName() << "'\n"); 308*7adc3a2bSChandler Carruth } 309*7adc3a2bSChandler Carruth } else { 310*7adc3a2bSChandler Carruth CalleeNode = CG.getCallsExternalNode(); 311*7adc3a2bSChandler Carruth } 312*7adc3a2bSChandler Carruth 313*7adc3a2bSChandler Carruth // Update the edge target in CGN. 314*7adc3a2bSChandler Carruth CGN->replaceCallEdge(CS, CS, CalleeNode); 315*7adc3a2bSChandler Carruth MadeChange = true; 316*7adc3a2bSChandler Carruth continue; 317*7adc3a2bSChandler Carruth } 318*7adc3a2bSChandler Carruth 319*7adc3a2bSChandler Carruth assert(!CheckingMode && 320*7adc3a2bSChandler Carruth "CallGraphSCCPass did not update the CallGraph correctly!"); 321*7adc3a2bSChandler Carruth 322*7adc3a2bSChandler Carruth // If the call site didn't exist in the CGN yet, add it. 323*7adc3a2bSChandler Carruth CallGraphNode *CalleeNode; 324*7adc3a2bSChandler Carruth if (Function *Callee = CS.getCalledFunction()) { 325*7adc3a2bSChandler Carruth CalleeNode = CG.getOrInsertFunction(Callee); 326*7adc3a2bSChandler Carruth ++NumDirectAdded; 327*7adc3a2bSChandler Carruth } else { 328*7adc3a2bSChandler Carruth CalleeNode = CG.getCallsExternalNode(); 329*7adc3a2bSChandler Carruth ++NumIndirectAdded; 330*7adc3a2bSChandler Carruth } 331*7adc3a2bSChandler Carruth 332*7adc3a2bSChandler Carruth CGN->addCalledFunction(CS, CalleeNode); 333*7adc3a2bSChandler Carruth MadeChange = true; 334*7adc3a2bSChandler Carruth } 335*7adc3a2bSChandler Carruth 336*7adc3a2bSChandler Carruth // We scanned the old callgraph node, removing invalidated call sites and 337*7adc3a2bSChandler Carruth // then added back newly found call sites. One thing that can happen is 338*7adc3a2bSChandler Carruth // that an old indirect call site was deleted and replaced with a new direct 339*7adc3a2bSChandler Carruth // call. In this case, we have devirtualized a call, and CGSCCPM would like 340*7adc3a2bSChandler Carruth // to iteratively optimize the new code. Unfortunately, we don't really 341*7adc3a2bSChandler Carruth // have a great way to detect when this happens. As an approximation, we 342*7adc3a2bSChandler Carruth // just look at whether the number of indirect calls is reduced and the 343*7adc3a2bSChandler Carruth // number of direct calls is increased. There are tons of ways to fool this 344*7adc3a2bSChandler Carruth // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a 345*7adc3a2bSChandler Carruth // direct call) but this is close enough. 346*7adc3a2bSChandler Carruth if (NumIndirectRemoved > NumIndirectAdded && 347*7adc3a2bSChandler Carruth NumDirectRemoved < NumDirectAdded) 348*7adc3a2bSChandler Carruth DevirtualizedCall = true; 349*7adc3a2bSChandler Carruth 350*7adc3a2bSChandler Carruth // After scanning this function, if we still have entries in callsites, then 351*7adc3a2bSChandler Carruth // they are dangling pointers. WeakVH should save us for this, so abort if 352*7adc3a2bSChandler Carruth // this happens. 353*7adc3a2bSChandler Carruth assert(CallSites.empty() && "Dangling pointers found in call sites map"); 354*7adc3a2bSChandler Carruth 355*7adc3a2bSChandler Carruth // Periodically do an explicit clear to remove tombstones when processing 356*7adc3a2bSChandler Carruth // large scc's. 357*7adc3a2bSChandler Carruth if ((FunctionNo & 15) == 15) 358*7adc3a2bSChandler Carruth CallSites.clear(); 359*7adc3a2bSChandler Carruth } 360*7adc3a2bSChandler Carruth 361*7adc3a2bSChandler Carruth DEBUG(if (MadeChange) { 362*7adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n"; 363*7adc3a2bSChandler Carruth for (CallGraphNode *CGN : CurSCC) 364*7adc3a2bSChandler Carruth CGN->dump(); 365*7adc3a2bSChandler Carruth if (DevirtualizedCall) 366*7adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n"; 367*7adc3a2bSChandler Carruth 368*7adc3a2bSChandler Carruth } else { 369*7adc3a2bSChandler Carruth dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n"; 370*7adc3a2bSChandler Carruth } 371*7adc3a2bSChandler Carruth ); 372*7adc3a2bSChandler Carruth (void)MadeChange; 373*7adc3a2bSChandler Carruth 374*7adc3a2bSChandler Carruth return DevirtualizedCall; 375*7adc3a2bSChandler Carruth } 376*7adc3a2bSChandler Carruth 377*7adc3a2bSChandler Carruth /// Execute the body of the entire pass manager on the specified SCC. 378*7adc3a2bSChandler Carruth /// This keeps track of whether a function pass devirtualizes 379*7adc3a2bSChandler Carruth /// any calls and returns it in DevirtualizedCall. 380*7adc3a2bSChandler Carruth bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG, 381*7adc3a2bSChandler Carruth bool &DevirtualizedCall) { 382*7adc3a2bSChandler Carruth bool Changed = false; 383*7adc3a2bSChandler Carruth 384*7adc3a2bSChandler Carruth // Keep track of whether the callgraph is known to be up-to-date or not. 385*7adc3a2bSChandler Carruth // The CGSSC pass manager runs two types of passes: 386*7adc3a2bSChandler Carruth // CallGraphSCC Passes and other random function passes. Because other 387*7adc3a2bSChandler Carruth // random function passes are not CallGraph aware, they may clobber the 388*7adc3a2bSChandler Carruth // call graph by introducing new calls or deleting other ones. This flag 389*7adc3a2bSChandler Carruth // is set to false when we run a function pass so that we know to clean up 390*7adc3a2bSChandler Carruth // the callgraph when we need to run a CGSCCPass again. 391*7adc3a2bSChandler Carruth bool CallGraphUpToDate = true; 392*7adc3a2bSChandler Carruth 393*7adc3a2bSChandler Carruth // Run all passes on current SCC. 394*7adc3a2bSChandler Carruth for (unsigned PassNo = 0, e = getNumContainedPasses(); 395*7adc3a2bSChandler Carruth PassNo != e; ++PassNo) { 396*7adc3a2bSChandler Carruth Pass *P = getContainedPass(PassNo); 397*7adc3a2bSChandler Carruth 398*7adc3a2bSChandler Carruth // If we're in -debug-pass=Executions mode, construct the SCC node list, 399*7adc3a2bSChandler Carruth // otherwise avoid constructing this string as it is expensive. 400*7adc3a2bSChandler Carruth if (isPassDebuggingExecutionsOrMore()) { 401*7adc3a2bSChandler Carruth std::string Functions; 402*7adc3a2bSChandler Carruth #ifndef NDEBUG 403*7adc3a2bSChandler Carruth raw_string_ostream OS(Functions); 404*7adc3a2bSChandler Carruth for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end(); 405*7adc3a2bSChandler Carruth I != E; ++I) { 406*7adc3a2bSChandler Carruth if (I != CurSCC.begin()) OS << ", "; 407*7adc3a2bSChandler Carruth (*I)->print(OS); 408*7adc3a2bSChandler Carruth } 409*7adc3a2bSChandler Carruth OS.flush(); 410*7adc3a2bSChandler Carruth #endif 411*7adc3a2bSChandler Carruth dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions); 412*7adc3a2bSChandler Carruth } 413*7adc3a2bSChandler Carruth dumpRequiredSet(P); 414*7adc3a2bSChandler Carruth 415*7adc3a2bSChandler Carruth initializeAnalysisImpl(P); 416*7adc3a2bSChandler Carruth 417*7adc3a2bSChandler Carruth // Actually run this pass on the current SCC. 418*7adc3a2bSChandler Carruth Changed |= RunPassOnSCC(P, CurSCC, CG, 419*7adc3a2bSChandler Carruth CallGraphUpToDate, DevirtualizedCall); 420*7adc3a2bSChandler Carruth 421*7adc3a2bSChandler Carruth if (Changed) 422*7adc3a2bSChandler Carruth dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, ""); 423*7adc3a2bSChandler Carruth dumpPreservedSet(P); 424*7adc3a2bSChandler Carruth 425*7adc3a2bSChandler Carruth verifyPreservedAnalysis(P); 426*7adc3a2bSChandler Carruth removeNotPreservedAnalysis(P); 427*7adc3a2bSChandler Carruth recordAvailableAnalysis(P); 428*7adc3a2bSChandler Carruth removeDeadPasses(P, "", ON_CG_MSG); 429*7adc3a2bSChandler Carruth } 430*7adc3a2bSChandler Carruth 431*7adc3a2bSChandler Carruth // If the callgraph was left out of date (because the last pass run was a 432*7adc3a2bSChandler Carruth // functionpass), refresh it before we move on to the next SCC. 433*7adc3a2bSChandler Carruth if (!CallGraphUpToDate) 434*7adc3a2bSChandler Carruth DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false); 435*7adc3a2bSChandler Carruth return Changed; 436*7adc3a2bSChandler Carruth } 437*7adc3a2bSChandler Carruth 438*7adc3a2bSChandler Carruth /// Execute all of the passes scheduled for execution. Keep track of 439*7adc3a2bSChandler Carruth /// whether any of the passes modifies the module, and if so, return true. 440*7adc3a2bSChandler Carruth bool CGPassManager::runOnModule(Module &M) { 441*7adc3a2bSChandler Carruth CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); 442*7adc3a2bSChandler Carruth bool Changed = doInitialization(CG); 443*7adc3a2bSChandler Carruth 444*7adc3a2bSChandler Carruth // Walk the callgraph in bottom-up SCC order. 445*7adc3a2bSChandler Carruth scc_iterator<CallGraph*> CGI = scc_begin(&CG); 446*7adc3a2bSChandler Carruth 447*7adc3a2bSChandler Carruth CallGraphSCC CurSCC(&CGI); 448*7adc3a2bSChandler Carruth while (!CGI.isAtEnd()) { 449*7adc3a2bSChandler Carruth // Copy the current SCC and increment past it so that the pass can hack 450*7adc3a2bSChandler Carruth // on the SCC if it wants to without invalidating our iterator. 451*7adc3a2bSChandler Carruth const std::vector<CallGraphNode *> &NodeVec = *CGI; 452*7adc3a2bSChandler Carruth CurSCC.initialize(NodeVec.data(), NodeVec.data() + NodeVec.size()); 453*7adc3a2bSChandler Carruth ++CGI; 454*7adc3a2bSChandler Carruth 455*7adc3a2bSChandler Carruth // At the top level, we run all the passes in this pass manager on the 456*7adc3a2bSChandler Carruth // functions in this SCC. However, we support iterative compilation in the 457*7adc3a2bSChandler Carruth // case where a function pass devirtualizes a call to a function. For 458*7adc3a2bSChandler Carruth // example, it is very common for a function pass (often GVN or instcombine) 459*7adc3a2bSChandler Carruth // to eliminate the addressing that feeds into a call. With that improved 460*7adc3a2bSChandler Carruth // information, we would like the call to be an inline candidate, infer 461*7adc3a2bSChandler Carruth // mod-ref information etc. 462*7adc3a2bSChandler Carruth // 463*7adc3a2bSChandler Carruth // Because of this, we allow iteration up to a specified iteration count. 464*7adc3a2bSChandler Carruth // This only happens in the case of a devirtualized call, so we only burn 465*7adc3a2bSChandler Carruth // compile time in the case that we're making progress. We also have a hard 466*7adc3a2bSChandler Carruth // iteration count limit in case there is crazy code. 467*7adc3a2bSChandler Carruth unsigned Iteration = 0; 468*7adc3a2bSChandler Carruth bool DevirtualizedCall = false; 469*7adc3a2bSChandler Carruth do { 470*7adc3a2bSChandler Carruth DEBUG(if (Iteration) 471*7adc3a2bSChandler Carruth dbgs() << " SCCPASSMGR: Re-visiting SCC, iteration #" 472*7adc3a2bSChandler Carruth << Iteration << '\n'); 473*7adc3a2bSChandler Carruth DevirtualizedCall = false; 474*7adc3a2bSChandler Carruth Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall); 475*7adc3a2bSChandler Carruth } while (Iteration++ < MaxIterations && DevirtualizedCall); 476*7adc3a2bSChandler Carruth 477*7adc3a2bSChandler Carruth if (DevirtualizedCall) 478*7adc3a2bSChandler Carruth DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after " << Iteration 479*7adc3a2bSChandler Carruth << " times, due to -max-cg-scc-iterations\n"); 480*7adc3a2bSChandler Carruth 481*7adc3a2bSChandler Carruth if (Iteration > MaxSCCIterations) 482*7adc3a2bSChandler Carruth MaxSCCIterations = Iteration; 483*7adc3a2bSChandler Carruth 484*7adc3a2bSChandler Carruth } 485*7adc3a2bSChandler Carruth Changed |= doFinalization(CG); 486*7adc3a2bSChandler Carruth return Changed; 487*7adc3a2bSChandler Carruth } 488*7adc3a2bSChandler Carruth 489*7adc3a2bSChandler Carruth 490*7adc3a2bSChandler Carruth /// Initialize CG 491*7adc3a2bSChandler Carruth bool CGPassManager::doInitialization(CallGraph &CG) { 492*7adc3a2bSChandler Carruth bool Changed = false; 493*7adc3a2bSChandler Carruth for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { 494*7adc3a2bSChandler Carruth if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { 495*7adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 496*7adc3a2bSChandler Carruth "Invalid CGPassManager member"); 497*7adc3a2bSChandler Carruth Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule()); 498*7adc3a2bSChandler Carruth } else { 499*7adc3a2bSChandler Carruth Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG); 500*7adc3a2bSChandler Carruth } 501*7adc3a2bSChandler Carruth } 502*7adc3a2bSChandler Carruth return Changed; 503*7adc3a2bSChandler Carruth } 504*7adc3a2bSChandler Carruth 505*7adc3a2bSChandler Carruth /// Finalize CG 506*7adc3a2bSChandler Carruth bool CGPassManager::doFinalization(CallGraph &CG) { 507*7adc3a2bSChandler Carruth bool Changed = false; 508*7adc3a2bSChandler Carruth for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) { 509*7adc3a2bSChandler Carruth if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) { 510*7adc3a2bSChandler Carruth assert(PM->getPassManagerType() == PMT_FunctionPassManager && 511*7adc3a2bSChandler Carruth "Invalid CGPassManager member"); 512*7adc3a2bSChandler Carruth Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule()); 513*7adc3a2bSChandler Carruth } else { 514*7adc3a2bSChandler Carruth Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG); 515*7adc3a2bSChandler Carruth } 516*7adc3a2bSChandler Carruth } 517*7adc3a2bSChandler Carruth return Changed; 518*7adc3a2bSChandler Carruth } 519*7adc3a2bSChandler Carruth 520*7adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 521*7adc3a2bSChandler Carruth // CallGraphSCC Implementation 522*7adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 523*7adc3a2bSChandler Carruth 524*7adc3a2bSChandler Carruth /// This informs the SCC and the pass manager that the specified 525*7adc3a2bSChandler Carruth /// Old node has been deleted, and New is to be used in its place. 526*7adc3a2bSChandler Carruth void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) { 527*7adc3a2bSChandler Carruth assert(Old != New && "Should not replace node with self"); 528*7adc3a2bSChandler Carruth for (unsigned i = 0; ; ++i) { 529*7adc3a2bSChandler Carruth assert(i != Nodes.size() && "Node not in SCC"); 530*7adc3a2bSChandler Carruth if (Nodes[i] != Old) continue; 531*7adc3a2bSChandler Carruth Nodes[i] = New; 532*7adc3a2bSChandler Carruth break; 533*7adc3a2bSChandler Carruth } 534*7adc3a2bSChandler Carruth 535*7adc3a2bSChandler Carruth // Update the active scc_iterator so that it doesn't contain dangling 536*7adc3a2bSChandler Carruth // pointers to the old CallGraphNode. 537*7adc3a2bSChandler Carruth scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context; 538*7adc3a2bSChandler Carruth CGI->ReplaceNode(Old, New); 539*7adc3a2bSChandler Carruth } 540*7adc3a2bSChandler Carruth 541*7adc3a2bSChandler Carruth 542*7adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 543*7adc3a2bSChandler Carruth // CallGraphSCCPass Implementation 544*7adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 545*7adc3a2bSChandler Carruth 546*7adc3a2bSChandler Carruth /// Assign pass manager to manage this pass. 547*7adc3a2bSChandler Carruth void CallGraphSCCPass::assignPassManager(PMStack &PMS, 548*7adc3a2bSChandler Carruth PassManagerType PreferredType) { 549*7adc3a2bSChandler Carruth // Find CGPassManager 550*7adc3a2bSChandler Carruth while (!PMS.empty() && 551*7adc3a2bSChandler Carruth PMS.top()->getPassManagerType() > PMT_CallGraphPassManager) 552*7adc3a2bSChandler Carruth PMS.pop(); 553*7adc3a2bSChandler Carruth 554*7adc3a2bSChandler Carruth assert(!PMS.empty() && "Unable to handle Call Graph Pass"); 555*7adc3a2bSChandler Carruth CGPassManager *CGP; 556*7adc3a2bSChandler Carruth 557*7adc3a2bSChandler Carruth if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager) 558*7adc3a2bSChandler Carruth CGP = (CGPassManager*)PMS.top(); 559*7adc3a2bSChandler Carruth else { 560*7adc3a2bSChandler Carruth // Create new Call Graph SCC Pass Manager if it does not exist. 561*7adc3a2bSChandler Carruth assert(!PMS.empty() && "Unable to create Call Graph Pass Manager"); 562*7adc3a2bSChandler Carruth PMDataManager *PMD = PMS.top(); 563*7adc3a2bSChandler Carruth 564*7adc3a2bSChandler Carruth // [1] Create new Call Graph Pass Manager 565*7adc3a2bSChandler Carruth CGP = new CGPassManager(); 566*7adc3a2bSChandler Carruth 567*7adc3a2bSChandler Carruth // [2] Set up new manager's top level manager 568*7adc3a2bSChandler Carruth PMTopLevelManager *TPM = PMD->getTopLevelManager(); 569*7adc3a2bSChandler Carruth TPM->addIndirectPassManager(CGP); 570*7adc3a2bSChandler Carruth 571*7adc3a2bSChandler Carruth // [3] Assign manager to manage this new manager. This may create 572*7adc3a2bSChandler Carruth // and push new managers into PMS 573*7adc3a2bSChandler Carruth Pass *P = CGP; 574*7adc3a2bSChandler Carruth TPM->schedulePass(P); 575*7adc3a2bSChandler Carruth 576*7adc3a2bSChandler Carruth // [4] Push new manager into PMS 577*7adc3a2bSChandler Carruth PMS.push(CGP); 578*7adc3a2bSChandler Carruth } 579*7adc3a2bSChandler Carruth 580*7adc3a2bSChandler Carruth CGP->add(this); 581*7adc3a2bSChandler Carruth } 582*7adc3a2bSChandler Carruth 583*7adc3a2bSChandler Carruth /// For this class, we declare that we require and preserve the call graph. 584*7adc3a2bSChandler Carruth /// If the derived class implements this method, it should 585*7adc3a2bSChandler Carruth /// always explicitly call the implementation here. 586*7adc3a2bSChandler Carruth void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const { 587*7adc3a2bSChandler Carruth AU.addRequired<CallGraphWrapperPass>(); 588*7adc3a2bSChandler Carruth AU.addPreserved<CallGraphWrapperPass>(); 589*7adc3a2bSChandler Carruth } 590*7adc3a2bSChandler Carruth 591*7adc3a2bSChandler Carruth 592*7adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 593*7adc3a2bSChandler Carruth // PrintCallGraphPass Implementation 594*7adc3a2bSChandler Carruth //===----------------------------------------------------------------------===// 595*7adc3a2bSChandler Carruth 596*7adc3a2bSChandler Carruth namespace { 597*7adc3a2bSChandler Carruth /// PrintCallGraphPass - Print a Module corresponding to a call graph. 598*7adc3a2bSChandler Carruth /// 599*7adc3a2bSChandler Carruth class PrintCallGraphPass : public CallGraphSCCPass { 600*7adc3a2bSChandler Carruth std::string Banner; 601*7adc3a2bSChandler Carruth raw_ostream &Out; // raw_ostream to print on. 602*7adc3a2bSChandler Carruth 603*7adc3a2bSChandler Carruth public: 604*7adc3a2bSChandler Carruth static char ID; 605*7adc3a2bSChandler Carruth PrintCallGraphPass(const std::string &B, raw_ostream &o) 606*7adc3a2bSChandler Carruth : CallGraphSCCPass(ID), Banner(B), Out(o) {} 607*7adc3a2bSChandler Carruth 608*7adc3a2bSChandler Carruth void getAnalysisUsage(AnalysisUsage &AU) const override { 609*7adc3a2bSChandler Carruth AU.setPreservesAll(); 610*7adc3a2bSChandler Carruth } 611*7adc3a2bSChandler Carruth 612*7adc3a2bSChandler Carruth bool runOnSCC(CallGraphSCC &SCC) override { 613*7adc3a2bSChandler Carruth Out << Banner; 614*7adc3a2bSChandler Carruth for (CallGraphNode *CGN : SCC) { 615*7adc3a2bSChandler Carruth if (CGN->getFunction()) 616*7adc3a2bSChandler Carruth CGN->getFunction()->print(Out); 617*7adc3a2bSChandler Carruth else 618*7adc3a2bSChandler Carruth Out << "\nPrinting <null> Function\n"; 619*7adc3a2bSChandler Carruth } 620*7adc3a2bSChandler Carruth return false; 621*7adc3a2bSChandler Carruth } 622*7adc3a2bSChandler Carruth }; 623*7adc3a2bSChandler Carruth 624*7adc3a2bSChandler Carruth } // end anonymous namespace. 625*7adc3a2bSChandler Carruth 626*7adc3a2bSChandler Carruth char PrintCallGraphPass::ID = 0; 627*7adc3a2bSChandler Carruth 628*7adc3a2bSChandler Carruth Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O, 629*7adc3a2bSChandler Carruth const std::string &Banner) const { 630*7adc3a2bSChandler Carruth return new PrintCallGraphPass(Banner, O); 631*7adc3a2bSChandler Carruth } 632*7adc3a2bSChandler Carruth 633