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