blob: 197c7c53d60e3cca50733582b981ebedb8f42bfd [file] [log] [blame]
Chris Lattner8d5a16c2002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner41fbf302001-09-28 00:08:15 +00009//
Chris Lattnerb81c0212004-04-12 05:36:32 +000010// This file implements the CallGraph class.
Chris Lattner9f9e2be2001-10-13 06:33:19 +000011//
Chris Lattner41fbf302001-09-28 00:08:15 +000012//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/CallGraph.h"
Chris Lattner07a38e72003-10-31 21:05:12 +000015#include "llvm/Constants.h" // Remove when ConstantPointerRefs are gone
Chris Lattner41fbf302001-09-28 00:08:15 +000016#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Chris Lattner07a38e72003-10-31 21:05:12 +000018#include "llvm/Support/CallSite.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/STLExtras.h"
Chris Lattnerb81c0212004-04-12 05:36:32 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner1e435162002-07-26 21:12:44 +000022static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
Chris Lattner93193f82002-01-31 00:42:27 +000023
Chris Lattnerd99d4d72002-07-18 04:43:16 +000024// getNodeFor - Return the node for the specified function or create one if it
Chris Lattner41fbf302001-09-28 00:08:15 +000025// does not already exist.
26//
Chris Lattnere590ff22002-03-26 17:55:33 +000027CallGraphNode *CallGraph::getNodeFor(Function *F) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +000028 CallGraphNode *&CGN = FunctionMap[F];
Chris Lattnerd4d427b2002-03-06 20:19:35 +000029 if (CGN) return CGN;
Chris Lattner41fbf302001-09-28 00:08:15 +000030
Chris Lattnere590ff22002-03-26 17:55:33 +000031 assert((!F || F->getParent() == Mod) && "Function not in current module!");
32 return CGN = new CallGraphNode(F);
Chris Lattner41fbf302001-09-28 00:08:15 +000033}
34
Chris Lattner07a38e72003-10-31 21:05:12 +000035static bool isOnlyADirectCall(Function *F, CallSite CS) {
36 if (!CS.getInstruction()) return false;
37 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
38 if (*I == F) return false;
39 return true;
40}
41
Chris Lattnerd99d4d72002-07-18 04:43:16 +000042// addToCallGraph - Add a function to the call graph, and link the node to all
43// of the functions that it calls.
Chris Lattner41fbf302001-09-28 00:08:15 +000044//
Chris Lattner5714c972003-08-31 20:36:52 +000045void CallGraph::addToCallGraph(Function *F) {
46 CallGraphNode *Node = getNodeFor(F);
Chris Lattner41fbf302001-09-28 00:08:15 +000047
Chris Lattnerf52d01b2003-09-15 04:35:16 +000048 // If this function has external linkage, anything could call it...
Chris Lattner5714c972003-08-31 20:36:52 +000049 if (!F->hasInternalLinkage()) {
Chris Lattnerb81c0212004-04-12 05:36:32 +000050 ExternalCallingNode->addCalledFunction(Node);
Chris Lattner25e9cad2001-11-26 18:51:25 +000051
Chris Lattnerd4d427b2002-03-06 20:19:35 +000052 // Found the entry point?
Chris Lattner5714c972003-08-31 20:36:52 +000053 if (F->getName() == "main") {
Chris Lattnerb81c0212004-04-12 05:36:32 +000054 if (Root) // Found multiple external mains? Don't pick one.
55 Root = ExternalCallingNode;
Chris Lattnerd4d427b2002-03-06 20:19:35 +000056 else
57 Root = Node; // Found a main, keep track of it!
58 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000059 }
Chris Lattnerf52d01b2003-09-15 04:35:16 +000060
61 // If this function is not defined in this translation unit, it could call
62 // anything.
Chris Lattnerb81c0212004-04-12 05:36:32 +000063 if (F->isExternal() && !F->getIntrinsicID())
64 Node->addCalledFunction(CallsExternalNode);
Chris Lattnerd4d427b2002-03-06 20:19:35 +000065
Chris Lattnerd99d4d72002-07-18 04:43:16 +000066 // Loop over all of the users of the function... looking for callers...
Chris Lattnerd4d427b2002-03-06 20:19:35 +000067 //
Chris Lattner07a38e72003-10-31 21:05:12 +000068 bool isUsedExternally = false;
Chris Lattner5714c972003-08-31 20:36:52 +000069 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
Chris Lattner07a38e72003-10-31 21:05:12 +000070 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
71 if (isOnlyADirectCall(F, CallSite::get(Inst)))
72 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
73 else
74 isUsedExternally = true;
Reid Spencere8404342004-07-18 00:18:30 +000075 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
76 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
Chris Lattner07a38e72003-10-31 21:05:12 +000077 I != E; ++I)
78 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
79 if (isOnlyADirectCall(F, CallSite::get(Inst)))
80 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
81 else
82 isUsedExternally = true;
83 } else {
84 isUsedExternally = true;
85 }
86 } else { // Can't classify the user!
87 isUsedExternally = true;
88 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000089 }
Chris Lattner07a38e72003-10-31 21:05:12 +000090 if (isUsedExternally)
Chris Lattnerb81c0212004-04-12 05:36:32 +000091 ExternalCallingNode->addCalledFunction(Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +000092
Chris Lattnerd99d4d72002-07-18 04:43:16 +000093 // Look for an indirect function call...
Chris Lattner5714c972003-08-31 20:36:52 +000094 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
Chris Lattnerd4d427b2002-03-06 20:19:35 +000095 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
Chris Lattner07a38e72003-10-31 21:05:12 +000096 CallSite CS = CallSite::get(II);
97 if (CS.getInstruction() && !CS.getCalledFunction())
Chris Lattnerb81c0212004-04-12 05:36:32 +000098 Node->addCalledFunction(CallsExternalNode);
Chris Lattnerd4d427b2002-03-06 20:19:35 +000099 }
Chris Lattner41fbf302001-09-28 00:08:15 +0000100}
101
Chris Lattner7e708292002-06-25 16:13:24 +0000102bool CallGraph::run(Module &M) {
Chris Lattner93193f82002-01-31 00:42:27 +0000103 destroy();
104
Chris Lattner7e708292002-06-25 16:13:24 +0000105 Mod = &M;
Chris Lattnerb81c0212004-04-12 05:36:32 +0000106 ExternalCallingNode = getNodeFor(0);
107 CallsExternalNode = new CallGraphNode(0);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000108 Root = 0;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000109
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000110 // Add every function to the call graph...
Chris Lattner7e708292002-06-25 16:13:24 +0000111 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
112 addToCallGraph(I);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000113
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000114 // If we didn't find a main function, use the external call graph node
Chris Lattnerb81c0212004-04-12 05:36:32 +0000115 if (Root == 0) Root = ExternalCallingNode;
Chris Lattner93193f82002-01-31 00:42:27 +0000116
117 return false;
Chris Lattner41fbf302001-09-28 00:08:15 +0000118}
119
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000120void CallGraph::destroy() {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000121 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000122 I != E; ++I)
Chris Lattner25e9cad2001-11-26 18:51:25 +0000123 delete I->second;
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000124 FunctionMap.clear();
Chris Lattner224f7e62004-05-02 07:31:34 +0000125 delete CallsExternalNode;
Chris Lattner7b11e332004-05-02 16:06:18 +0000126 CallsExternalNode = 0;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000127}
128
Chris Lattner16500152002-11-04 00:21:19 +0000129static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000130 if (CGN->getFunction())
131 o << "Call graph node for function: '"
132 << CGN->getFunction()->getName() <<"'\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000133 else
Chris Lattnerb31247a2003-09-15 04:29:37 +0000134 o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000135
Chris Lattner41fbf302001-09-28 00:08:15 +0000136 for (unsigned i = 0; i < CGN->size(); ++i)
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000137 if ((*CGN)[i]->getFunction())
138 o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000139 else
140 o << " Calls external node\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000141 o << "\n";
Chris Lattner41fbf302001-09-28 00:08:15 +0000142}
143
Chris Lattner16500152002-11-04 00:21:19 +0000144void CallGraph::print(std::ostream &o, const Module *M) const {
Chris Lattnerb31247a2003-09-15 04:29:37 +0000145 o << "CallGraph Root is: ";
146 if (getRoot()->getFunction())
147 o << getRoot()->getFunction()->getName() << "\n";
148 else
149 o << "<<null function: 0x" << getRoot() << ">>\n";
150
Chris Lattner16500152002-11-04 00:21:19 +0000151 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
152 WriteToOutput(I->second, o);
Chris Lattner41fbf302001-09-28 00:08:15 +0000153}
Vikram S. Advea7edb182001-10-22 13:55:46 +0000154
155
Chris Lattner25e9cad2001-11-26 18:51:25 +0000156//===----------------------------------------------------------------------===//
157// Implementations of public modification methods
158//
159
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000160// Functions to keep a call graph up to date with a function that has been
Chris Lattner25e9cad2001-11-26 18:51:25 +0000161// modified
162//
Chris Lattnerb81c0212004-04-12 05:36:32 +0000163void CallGraph::addFunctionToModule(Function *F) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000164 assert(0 && "not implemented");
165 abort();
166}
167
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000168// removeFunctionFromModule - Unlink the function from this module, returning
169// it. Because this removes the function from the module, the call graph node
170// is destroyed. This is only valid if the function does not call any other
171// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000172// is to dropAllReferences before calling this.
173//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000174Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
175 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
176 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000177 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000178 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000179 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000180
Chris Lattner5714c972003-08-31 20:36:52 +0000181 Mod->getFunctionList().remove(F);
182 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000183}
184
Chris Lattner14fffaf2003-10-30 05:17:30 +0000185void CallGraph::stub() {}
Brian Gaeked0fde302003-11-11 22:41:34 +0000186
Chris Lattnerb81c0212004-04-12 05:36:32 +0000187void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
188 for (unsigned i = CalledFunctions.size(); ; --i) {
189 assert(i && "Cannot find callee to remove!");
190 if (CalledFunctions[i-1] == Callee) {
191 CalledFunctions.erase(CalledFunctions.begin()+i-1);
192 return;
193 }
194 }
195}