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 and 81 // it is not a callback, then anything could call it. 82 if (!F->hasLocalLinkage() || 83 F->hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true, 84 /* IgnoreAssumeLikeCalls */ true)) 85 ExternalCallingNode->addCalledFunction(nullptr, Node); 86 87 populateCallGraphNode(Node); 88 } 89 90 void CallGraph::populateCallGraphNode(CallGraphNode *Node) { 91 Function *F = Node->getFunction(); 92 93 // If this function is not defined in this translation unit, it could call 94 // anything. 95 if (F->isDeclaration() && !F->isIntrinsic()) 96 Node->addCalledFunction(nullptr, CallsExternalNode.get()); 97 98 // Look for calls by this function. 99 for (BasicBlock &BB : *F) 100 for (Instruction &I : BB) { 101 if (auto *Call = dyn_cast<CallBase>(&I)) { 102 const Function *Callee = Call->getCalledFunction(); 103 if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID())) 104 // Indirect calls of intrinsics are not allowed so no need to check. 105 // We can be more precise here by using TargetArg returned by 106 // Intrinsic::isLeaf. 107 Node->addCalledFunction(Call, CallsExternalNode.get()); 108 else if (!Callee->isIntrinsic()) 109 Node->addCalledFunction(Call, getOrInsertFunction(Callee)); 110 111 // Add reference to callback functions. 112 forEachCallbackFunction(*Call, [=](Function *CB) { 113 Node->addCalledFunction(nullptr, getOrInsertFunction(CB)); 114 }); 115 } 116 } 117 } 118 119 void CallGraph::print(raw_ostream &OS) const { 120 // Print in a deterministic order by sorting CallGraphNodes by name. We do 121 // this here to avoid slowing down the non-printing fast path. 122 123 SmallVector<CallGraphNode *, 16> Nodes; 124 Nodes.reserve(FunctionMap.size()); 125 126 for (const auto &I : *this) 127 Nodes.push_back(I.second.get()); 128 129 llvm::sort(Nodes, [](CallGraphNode *LHS, CallGraphNode *RHS) { 130 if (Function *LF = LHS->getFunction()) 131 if (Function *RF = RHS->getFunction()) 132 return LF->getName() < RF->getName(); 133 134 return RHS->getFunction() != nullptr; 135 }); 136 137 for (CallGraphNode *CN : Nodes) 138 CN->print(OS); 139 } 140 141 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 142 LLVM_DUMP_METHOD void CallGraph::dump() const { print(dbgs()); } 143 #endif 144 145 void CallGraph::ReplaceExternalCallEdge(CallGraphNode *Old, 146 CallGraphNode *New) { 147 for (auto &CR : ExternalCallingNode->CalledFunctions) 148 if (CR.second == Old) { 149 CR.second->DropRef(); 150 CR.second = New; 151 CR.second->AddRef(); 152 } 153 } 154 155 // removeFunctionFromModule - Unlink the function from this module, returning 156 // it. Because this removes the function from the module, the call graph node 157 // is destroyed. This is only valid if the function does not call any other 158 // functions (ie, there are no edges in it's CGN). The easiest way to do this 159 // is to dropAllReferences before calling this. 160 // 161 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { 162 assert(CGN->empty() && "Cannot remove function from call " 163 "graph if it references other functions!"); 164 Function *F = CGN->getFunction(); // Get the function for the call graph node 165 FunctionMap.erase(F); // Remove the call graph node from the map 166 167 M.getFunctionList().remove(F); 168 return F; 169 } 170 171 // getOrInsertFunction - This method is identical to calling operator[], but 172 // it will insert a new CallGraphNode for the specified function if one does 173 // not already exist. 174 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) { 175 auto &CGN = FunctionMap[F]; 176 if (CGN) 177 return CGN.get(); 178 179 assert((!F || F->getParent() == &M) && "Function not in current module!"); 180 CGN = std::make_unique<CallGraphNode>(this, const_cast<Function *>(F)); 181 return CGN.get(); 182 } 183 184 //===----------------------------------------------------------------------===// 185 // Implementations of the CallGraphNode class methods. 186 // 187 188 void CallGraphNode::print(raw_ostream &OS) const { 189 if (Function *F = getFunction()) 190 OS << "Call graph node for function: '" << F->getName() << "'"; 191 else 192 OS << "Call graph node <<null function>>"; 193 194 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n'; 195 196 for (const auto &I : *this) { 197 OS << " CS<" << I.first << "> calls "; 198 if (Function *FI = I.second->getFunction()) 199 OS << "function '" << FI->getName() <<"'\n"; 200 else 201 OS << "external node\n"; 202 } 203 OS << '\n'; 204 } 205 206 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 207 LLVM_DUMP_METHOD void CallGraphNode::dump() const { print(dbgs()); } 208 #endif 209 210 /// removeCallEdgeFor - This method removes the edge in the node for the 211 /// specified call site. Note that this method takes linear time, so it 212 /// should be used sparingly. 213 void CallGraphNode::removeCallEdgeFor(CallBase &Call) { 214 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 215 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); 216 if (I->first && *I->first == &Call) { 217 I->second->DropRef(); 218 *I = CalledFunctions.back(); 219 CalledFunctions.pop_back(); 220 221 // Remove all references to callback functions if there are any. 222 forEachCallbackFunction(Call, [=](Function *CB) { 223 removeOneAbstractEdgeTo(CG->getOrInsertFunction(CB)); 224 }); 225 return; 226 } 227 } 228 } 229 230 // removeAnyCallEdgeTo - This method removes any call edges from this node to 231 // the specified callee function. This takes more time to execute than 232 // removeCallEdgeTo, so it should not be used unless necessary. 233 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) { 234 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i) 235 if (CalledFunctions[i].second == Callee) { 236 Callee->DropRef(); 237 CalledFunctions[i] = CalledFunctions.back(); 238 CalledFunctions.pop_back(); 239 --i; --e; 240 } 241 } 242 243 /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite 244 /// from this node to the specified callee function. 245 void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) { 246 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 247 assert(I != CalledFunctions.end() && "Cannot find callee to remove!"); 248 CallRecord &CR = *I; 249 if (CR.second == Callee && !CR.first) { 250 Callee->DropRef(); 251 *I = CalledFunctions.back(); 252 CalledFunctions.pop_back(); 253 return; 254 } 255 } 256 } 257 258 /// replaceCallEdge - This method replaces the edge in the node for the 259 /// specified call site with a new one. Note that this method takes linear 260 /// time, so it should be used sparingly. 261 void CallGraphNode::replaceCallEdge(CallBase &Call, CallBase &NewCall, 262 CallGraphNode *NewNode) { 263 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 264 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); 265 if (I->first && *I->first == &Call) { 266 I->second->DropRef(); 267 I->first = &NewCall; 268 I->second = NewNode; 269 NewNode->AddRef(); 270 271 // Refresh callback references. Do not resize CalledFunctions if the 272 // number of callbacks is the same for new and old call sites. 273 SmallVector<CallGraphNode *, 4u> OldCBs; 274 SmallVector<CallGraphNode *, 4u> NewCBs; 275 forEachCallbackFunction(Call, [this, &OldCBs](Function *CB) { 276 OldCBs.push_back(CG->getOrInsertFunction(CB)); 277 }); 278 forEachCallbackFunction(NewCall, [this, &NewCBs](Function *CB) { 279 NewCBs.push_back(CG->getOrInsertFunction(CB)); 280 }); 281 if (OldCBs.size() == NewCBs.size()) { 282 for (unsigned N = 0; N < OldCBs.size(); ++N) { 283 CallGraphNode *OldNode = OldCBs[N]; 284 CallGraphNode *NewNode = NewCBs[N]; 285 for (auto J = CalledFunctions.begin();; ++J) { 286 assert(J != CalledFunctions.end() && 287 "Cannot find callsite to update!"); 288 if (!J->first && J->second == OldNode) { 289 J->second = NewNode; 290 OldNode->DropRef(); 291 NewNode->AddRef(); 292 break; 293 } 294 } 295 } 296 } else { 297 for (auto *CGN : OldCBs) 298 removeOneAbstractEdgeTo(CGN); 299 for (auto *CGN : NewCBs) 300 addCalledFunction(nullptr, CGN); 301 } 302 return; 303 } 304 } 305 } 306 307 // Provide an explicit template instantiation for the static ID. 308 AnalysisKey CallGraphAnalysis::Key; 309 310 PreservedAnalyses CallGraphPrinterPass::run(Module &M, 311 ModuleAnalysisManager &AM) { 312 AM.getResult<CallGraphAnalysis>(M).print(OS); 313 return PreservedAnalyses::all(); 314 } 315 316 //===----------------------------------------------------------------------===// 317 // Out-of-line definitions of CallGraphAnalysis class members. 318 // 319 320 //===----------------------------------------------------------------------===// 321 // Implementations of the CallGraphWrapperPass class methods. 322 // 323 324 CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) { 325 initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry()); 326 } 327 328 CallGraphWrapperPass::~CallGraphWrapperPass() = default; 329 330 void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 331 AU.setPreservesAll(); 332 } 333 334 bool CallGraphWrapperPass::runOnModule(Module &M) { 335 // All the real work is done in the constructor for the CallGraph. 336 G.reset(new CallGraph(M)); 337 return false; 338 } 339 340 INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction", 341 false, true) 342 343 char CallGraphWrapperPass::ID = 0; 344 345 void CallGraphWrapperPass::releaseMemory() { G.reset(); } 346 347 void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const { 348 if (!G) { 349 OS << "No call graph has been built!\n"; 350 return; 351 } 352 353 // Just delegate. 354 G->print(OS); 355 } 356 357 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 358 LLVM_DUMP_METHOD 359 void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); } 360 #endif 361 362 namespace { 363 364 struct CallGraphPrinterLegacyPass : public ModulePass { 365 static char ID; // Pass ID, replacement for typeid 366 367 CallGraphPrinterLegacyPass() : ModulePass(ID) { 368 initializeCallGraphPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); 369 } 370 371 void getAnalysisUsage(AnalysisUsage &AU) const override { 372 AU.setPreservesAll(); 373 AU.addRequiredTransitive<CallGraphWrapperPass>(); 374 } 375 376 bool runOnModule(Module &M) override { 377 getAnalysis<CallGraphWrapperPass>().print(errs(), &M); 378 return false; 379 } 380 }; 381 382 } // end anonymous namespace 383 384 char CallGraphPrinterLegacyPass::ID = 0; 385 386 INITIALIZE_PASS_BEGIN(CallGraphPrinterLegacyPass, "print-callgraph", 387 "Print a call graph", true, true) 388 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 389 INITIALIZE_PASS_END(CallGraphPrinterLegacyPass, "print-callgraph", 390 "Print a call graph", true, true) 391