blob: 3caafd923339c57a1fc2c8da4c699c8cf7c8ade8 [file] [log] [blame]
Chris Lattner8d5a16c2002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner41fbf302001-09-28 00:08:15 +000015#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000016#include "llvm/Instructions.h"
Chris Lattner07a38e72003-10-31 21:05:12 +000017#include "llvm/Support/CallSite.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000018#include "llvm/ADT/STLExtras.h"
Chris Lattneraf8a4242004-08-08 03:27:49 +000019#include <iostream>
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 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000060
Chris Lattnerf52d01b2003-09-15 04:35:16 +000061 // 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 Lattnerb12914b2004-09-20 04:48:05 +0000102bool CallGraph::runOnModule(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;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000116
Chris Lattner93193f82002-01-31 00:42:27 +0000117 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 Lattneraf8a4242004-08-08 03:27:49 +0000129void CallGraphNode::print(std::ostream &OS) const {
130 if (Function *F = getFunction())
131 OS << "Call graph node for function: '" << F->getName() <<"'\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000132 else
Chris Lattneraf8a4242004-08-08 03:27:49 +0000133 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000134
Chris Lattneraf8a4242004-08-08 03:27:49 +0000135 for (const_iterator I = begin(), E = end(); I != E; ++I)
136 if ((*I)->getFunction())
137 OS << " Calls function '" << (*I)->getFunction()->getName() << "'\n";
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000138 else
Chris Lattneraf8a4242004-08-08 03:27:49 +0000139 OS << " Calls external node\n";
140 OS << "\n";
Chris Lattner41fbf302001-09-28 00:08:15 +0000141}
142
Chris Lattneraf8a4242004-08-08 03:27:49 +0000143void CallGraphNode::dump() const { print(std::cerr); }
144
145void CallGraph::print(std::ostream &OS, const Module *M) const {
146 OS << "CallGraph Root is: ";
147 if (Function *F = getRoot()->getFunction())
148 OS << F->getName() << "\n";
Chris Lattnerb31247a2003-09-15 04:29:37 +0000149 else
Chris Lattneraf8a4242004-08-08 03:27:49 +0000150 OS << "<<null function: 0x" << getRoot() << ">>\n";
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000151
Chris Lattner16500152002-11-04 00:21:19 +0000152 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000153 I->second->print(OS);
154}
155
156void CallGraph::dump() const {
157 print(std::cerr, 0);
Chris Lattner41fbf302001-09-28 00:08:15 +0000158}
Vikram S. Advea7edb182001-10-22 13:55:46 +0000159
160
Chris Lattner25e9cad2001-11-26 18:51:25 +0000161//===----------------------------------------------------------------------===//
162// Implementations of public modification methods
163//
164
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000165// removeFunctionFromModule - Unlink the function from this module, returning
166// it. Because this removes the function from the module, the call graph node
167// is destroyed. This is only valid if the function does not call any other
168// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000169// is to dropAllReferences before calling this.
170//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000171Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
172 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
173 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000174 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000175 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000176 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000177
Chris Lattner5714c972003-08-31 20:36:52 +0000178 Mod->getFunctionList().remove(F);
179 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000180}
181
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000182// changeFunction - This method changes the function associated with this
183// CallGraphNode, for use by transformations that need to change the prototype
184// of a Function (thus they must create a new Function and move the old code
185// over).
186void CallGraph::changeFunction(Function *OldF, Function *NewF) {
187 iterator I = FunctionMap.find(OldF);
188 CallGraphNode *&New = FunctionMap[NewF];
189 assert(I != FunctionMap.end() && I->second && !New &&
190 "OldF didn't exist in CG or NewF already does!");
191 New = I->second;
Chris Lattner3795bc92004-09-18 00:27:20 +0000192 New->F = NewF;
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000193 FunctionMap.erase(I);
194}
195
196
Chris Lattner14fffaf2003-10-30 05:17:30 +0000197void CallGraph::stub() {}
Brian Gaeked0fde302003-11-11 22:41:34 +0000198
Chris Lattnerb81c0212004-04-12 05:36:32 +0000199void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
200 for (unsigned i = CalledFunctions.size(); ; --i) {
201 assert(i && "Cannot find callee to remove!");
202 if (CalledFunctions[i-1] == Callee) {
203 CalledFunctions.erase(CalledFunctions.begin()+i-1);
204 return;
205 }
206 }
207}
Chris Lattnercd382a32004-09-18 21:34:34 +0000208
209// removeAnyCallEdgeTo - This method removes any call edges from this node to
210// the specified callee function. This takes more time to execute than
211// removeCallEdgeTo, so it should not be used unless necessary.
212void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000213 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
214 if (CalledFunctions[i] == Callee) {
215 CalledFunctions[i] = CalledFunctions.back();
216 CalledFunctions.pop_back();
217 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000218 }
219}