1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Analysis/CallGraph.h" 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/ADT/SmallVector.h" 12 #include "llvm/Config/llvm-config.h" 13 #include "llvm/IR/AbstractCallSite.h" 14 #include "llvm/IR/Function.h" 15 #include "llvm/IR/IntrinsicInst.h" 16 #include "llvm/IR/Intrinsics.h" 17 #include "llvm/IR/Module.h" 18 #include "llvm/IR/PassManager.h" 19 #include "llvm/InitializePasses.h" 20 #include "llvm/Pass.h" 21 #include "llvm/Support/Compiler.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include <algorithm> 25 #include <cassert> 26 27 using namespace llvm; 28 29 //===----------------------------------------------------------------------===// 30 // Implementations of the CallGraph class methods. 31 // 32 33 CallGraph::CallGraph(Module &M) 34 : M(M), ExternalCallingNode(getOrInsertFunction(nullptr)), 35 CallsExternalNode(std::make_unique<CallGraphNode>(this, nullptr)) { 36 // Add every interesting function to the call graph. 37 for (Function &F : M) 38 if (!isDbgInfoIntrinsic(F.getIntrinsicID())) 39 addToCallGraph(&F); 40 } 41 42 CallGraph::CallGraph(CallGraph &&Arg) 43 : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)), 44 ExternalCallingNode(Arg.ExternalCallingNode), 45 CallsExternalNode(std::move(Arg.CallsExternalNode)) { 46 Arg.FunctionMap.clear(); 47 Arg.ExternalCallingNode = nullptr; 48 49 // Update parent CG for all call graph's nodes. 50 CallsExternalNode->CG = this; 51 for (auto &P : FunctionMap) 52 P.second->CG = this; 53 } 54 55 CallGraph::~CallGraph() { 56 // CallsExternalNode is not in the function map, delete it explicitly. 57 if (CallsExternalNode) 58 CallsExternalNode->allReferencesDropped(); 59 60 // Reset all node's use counts to zero before deleting them to prevent an 61 // assertion from firing. 62 #ifndef NDEBUG 63 for (auto &I : FunctionMap) 64 I.second->allReferencesDropped(); 65 #endif 66 } 67 68 bool CallGraph::invalidate(Module &, const PreservedAnalyses &PA, 69 ModuleAnalysisManager::Invalidator &) { 70 // Check whether the analysis, all analyses on functions, or the function's 71 // CFG have been preserved. 72 auto PAC = PA.getChecker<CallGraphAnalysis>(); 73 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>() || 74 PAC.preservedSet<CFGAnalyses>()); 75 } 76 77 void CallGraph::addToCallGraph(Function *F) { 78 CallGraphNode *Node = getOrInsertFunction(F); 79 80 // If this function has external linkage or has its address taken, anything 81 // could call it. 82 if (!F->hasLocalLinkage() || F->hasAddressTaken()) 83 ExternalCallingNode->addCalledFunction(nullptr, Node); 84 85 populateCallGraphNode(Node); 86 } 87 88 void CallGraph::populateCallGraphNode(CallGraphNode *Node) { 89 Function *F = Node->getFunction(); 90 91 // If this function is not defined in this translation unit, it could call 92 // anything. 93 if (F->isDeclaration() && !F->isIntrinsic()) 94 Node->addCalledFunction(nullptr, CallsExternalNode.get()); 95 96 // Look for calls by this function. 97 for (BasicBlock &BB : *F) 98 for (Instruction &I : BB) { 99 if (auto *Call = dyn_cast<CallBase>(&I)) { 100 const Function *Callee = Call->getCalledFunction(); 101 if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID())) 102 // Indirect calls of intrinsics are not allowed so no need to check. 103 // We can be more precise here by using TargetArg returned by 104 // Intrinsic::isLeaf. 105 Node->addCalledFunction(Call, CallsExternalNode.get()); 106 else if (!Callee->isIntrinsic()) 107 Node->addCalledFunction(Call, getOrInsertFunction(Callee)); 108 109 // Add reference to callback functions. 110 forEachCallbackFunction(*Call, [=](Function *CB) { 111 Node->addCalledFunction(nullptr, getOrInsertFunction(CB)); 112 }); 113 } 114 } 115 } 116 117 void CallGraph::print(raw_ostream &OS) const { 118 // Print in a deterministic order by sorting CallGraphNodes by name. We do 119 // this here to avoid slowing down the non-printing fast path. 120 121 SmallVector<CallGraphNode *, 16> Nodes; 122 Nodes.reserve(FunctionMap.size()); 123 124 for (const auto &I : *this) 125 Nodes.push_back(I.second.get()); 126 127 llvm::sort(Nodes, [](CallGraphNode *LHS, CallGraphNode *RHS) { 128 if (Function *LF = LHS->getFunction()) 129 if (Function *RF = RHS->getFunction()) 130 return LF->getName() < RF->getName(); 131 132 return RHS->getFunction() != nullptr; 133 }); 134 135 for (CallGraphNode *CN : Nodes) 136 CN->print(OS); 137 } 138 139 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 140 LLVM_DUMP_METHOD void CallGraph::dump() const { print(dbgs()); } 141 #endif 142 143 void CallGraph::ReplaceExternalCallEdge(CallGraphNode *Old, 144 CallGraphNode *New) { 145 for (auto &CR : ExternalCallingNode->CalledFunctions) 146 if (CR.second == Old) { 147 CR.second->DropRef(); 148 CR.second = New; 149 CR.second->AddRef(); 150 } 151 } 152 153 // removeFunctionFromModule - Unlink the function from this module, returning 154 // it. Because this removes the function from the module, the call graph node 155 // is destroyed. This is only valid if the function does not call any other 156 // functions (ie, there are no edges in it's CGN). The easiest way to do this 157 // is to dropAllReferences before calling this. 158 // 159 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { 160 assert(CGN->empty() && "Cannot remove function from call " 161 "graph if it references other functions!"); 162 Function *F = CGN->getFunction(); // Get the function for the call graph node 163 FunctionMap.erase(F); // Remove the call graph node from the map 164 165 M.getFunctionList().remove(F); 166 return F; 167 } 168 169 /// spliceFunction - Replace the function represented by this node by another. 170 /// This does not rescan the body of the function, so it is suitable when 171 /// splicing the body of the old function to the new while also updating all 172 /// callers from old to new. 173 void CallGraph::spliceFunction(const Function *From, const Function *To) { 174 assert(FunctionMap.count(From) && "No CallGraphNode for function!"); 175 assert(!FunctionMap.count(To) && 176 "Pointing CallGraphNode at a function that already exists"); 177 FunctionMapTy::iterator I = FunctionMap.find(From); 178 I->second->F = const_cast<Function*>(To); 179 FunctionMap[To] = std::move(I->second); 180 FunctionMap.erase(I); 181 } 182 183 // getOrInsertFunction - This method is identical to calling operator[], but 184 // it will insert a new CallGraphNode for the specified function if one does 185 // not already exist. 186 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) { 187 auto &CGN = FunctionMap[F]; 188 if (CGN) 189 return CGN.get(); 190 191 assert((!F || F->getParent() == &M) && "Function not in current module!"); 192 CGN = std::make_unique<CallGraphNode>(this, const_cast<Function *>(F)); 193 return CGN.get(); 194 } 195 196 //===----------------------------------------------------------------------===// 197 // Implementations of the CallGraphNode class methods. 198 // 199 200 void CallGraphNode::print(raw_ostream &OS) const { 201 if (Function *F = getFunction()) 202 OS << "Call graph node for function: '" << F->getName() << "'"; 203 else 204 OS << "Call graph node <<null function>>"; 205 206 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n'; 207 208 for (const auto &I : *this) { 209 OS << " CS<" << I.first << "> calls "; 210 if (Function *FI = I.second->getFunction()) 211 OS << "function '" << FI->getName() <<"'\n"; 212 else 213 OS << "external node\n"; 214 } 215 OS << '\n'; 216 } 217 218 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 219 LLVM_DUMP_METHOD void CallGraphNode::dump() const { print(dbgs()); } 220 #endif 221 222 /// removeCallEdgeFor - This method removes the edge in the node for the 223 /// specified call site. Note that this method takes linear time, so it 224 /// should be used sparingly. 225 void CallGraphNode::removeCallEdgeFor(CallBase &Call) { 226 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 227 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); 228 if (I->first && *I->first == &Call) { 229 I->second->DropRef(); 230 *I = CalledFunctions.back(); 231 CalledFunctions.pop_back(); 232 233 // Remove all references to callback functions if there are any. 234 forEachCallbackFunction(Call, [=](Function *CB) { 235 removeOneAbstractEdgeTo(CG->getOrInsertFunction(CB)); 236 }); 237 return; 238 } 239 } 240 } 241 242 // removeAnyCallEdgeTo - This method removes any call edges from this node to 243 // the specified callee function. This takes more time to execute than 244 // removeCallEdgeTo, so it should not be used unless necessary. 245 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) { 246 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i) 247 if (CalledFunctions[i].second == Callee) { 248 Callee->DropRef(); 249 CalledFunctions[i] = CalledFunctions.back(); 250 CalledFunctions.pop_back(); 251 --i; --e; 252 } 253 } 254 255 /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite 256 /// from this node to the specified callee function. 257 void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) { 258 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 259 assert(I != CalledFunctions.end() && "Cannot find callee to remove!"); 260 CallRecord &CR = *I; 261 if (CR.second == Callee && !CR.first) { 262 Callee->DropRef(); 263 *I = CalledFunctions.back(); 264 CalledFunctions.pop_back(); 265 return; 266 } 267 } 268 } 269 270 /// replaceCallEdge - This method replaces the edge in the node for the 271 /// specified call site with a new one. Note that this method takes linear 272 /// time, so it should be used sparingly. 273 void CallGraphNode::replaceCallEdge(CallBase &Call, CallBase &NewCall, 274 CallGraphNode *NewNode) { 275 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 276 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); 277 if (I->first && *I->first == &Call) { 278 I->second->DropRef(); 279 I->first = &NewCall; 280 I->second = NewNode; 281 NewNode->AddRef(); 282 283 // Refresh callback references. 284 forEachCallbackFunction(Call, [=](Function *CB) { 285 removeOneAbstractEdgeTo(CG->getOrInsertFunction(CB)); 286 }); 287 forEachCallbackFunction(NewCall, [=](Function *CB) { 288 addCalledFunction(nullptr, CG->getOrInsertFunction(CB)); 289 }); 290 return; 291 } 292 } 293 } 294 295 // Provide an explicit template instantiation for the static ID. 296 AnalysisKey CallGraphAnalysis::Key; 297 298 PreservedAnalyses CallGraphPrinterPass::run(Module &M, 299 ModuleAnalysisManager &AM) { 300 AM.getResult<CallGraphAnalysis>(M).print(OS); 301 return PreservedAnalyses::all(); 302 } 303 304 //===----------------------------------------------------------------------===// 305 // Out-of-line definitions of CallGraphAnalysis class members. 306 // 307 308 //===----------------------------------------------------------------------===// 309 // Implementations of the CallGraphWrapperPass class methods. 310 // 311 312 CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) { 313 initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry()); 314 } 315 316 CallGraphWrapperPass::~CallGraphWrapperPass() = default; 317 318 void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 319 AU.setPreservesAll(); 320 } 321 322 bool CallGraphWrapperPass::runOnModule(Module &M) { 323 // All the real work is done in the constructor for the CallGraph. 324 G.reset(new CallGraph(M)); 325 return false; 326 } 327 328 INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction", 329 false, true) 330 331 char CallGraphWrapperPass::ID = 0; 332 333 void CallGraphWrapperPass::releaseMemory() { G.reset(); } 334 335 void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const { 336 if (!G) { 337 OS << "No call graph has been built!\n"; 338 return; 339 } 340 341 // Just delegate. 342 G->print(OS); 343 } 344 345 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 346 LLVM_DUMP_METHOD 347 void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); } 348 #endif 349 350 namespace { 351 352 struct CallGraphPrinterLegacyPass : public ModulePass { 353 static char ID; // Pass ID, replacement for typeid 354 355 CallGraphPrinterLegacyPass() : ModulePass(ID) { 356 initializeCallGraphPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); 357 } 358 359 void getAnalysisUsage(AnalysisUsage &AU) const override { 360 AU.setPreservesAll(); 361 AU.addRequiredTransitive<CallGraphWrapperPass>(); 362 } 363 364 bool runOnModule(Module &M) override { 365 getAnalysis<CallGraphWrapperPass>().print(errs(), &M); 366 return false; 367 } 368 }; 369 370 } // end anonymous namespace 371 372 char CallGraphPrinterLegacyPass::ID = 0; 373 374 INITIALIZE_PASS_BEGIN(CallGraphPrinterLegacyPass, "print-callgraph", 375 "Print a call graph", true, true) 376 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 377 INITIALIZE_PASS_END(CallGraphPrinterLegacyPass, "print-callgraph", 378 "Print a call graph", true, true) 379