blob: 70bf2c496b01ae8202cd17b4a6069e776378c610 [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"
Chris Lattner41fbf302001-09-28 00:08:15 +000017#include "llvm/iOther.h"
Chris Lattner9f9e2be2001-10-13 06:33:19 +000018#include "llvm/iTerminators.h"
Chris Lattner07a38e72003-10-31 21:05:12 +000019#include "llvm/Support/CallSite.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/STLExtras.h"
Chris Lattnerb81c0212004-04-12 05:36:32 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner1e435162002-07-26 21:12:44 +000023static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
Chris Lattner93193f82002-01-31 00:42:27 +000024
Chris Lattnerd99d4d72002-07-18 04:43:16 +000025// getNodeFor - Return the node for the specified function or create one if it
Chris Lattner41fbf302001-09-28 00:08:15 +000026// does not already exist.
27//
Chris Lattnere590ff22002-03-26 17:55:33 +000028CallGraphNode *CallGraph::getNodeFor(Function *F) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +000029 CallGraphNode *&CGN = FunctionMap[F];
Chris Lattnerd4d427b2002-03-06 20:19:35 +000030 if (CGN) return CGN;
Chris Lattner41fbf302001-09-28 00:08:15 +000031
Chris Lattnere590ff22002-03-26 17:55:33 +000032 assert((!F || F->getParent() == Mod) && "Function not in current module!");
33 return CGN = new CallGraphNode(F);
Chris Lattner41fbf302001-09-28 00:08:15 +000034}
35
Chris Lattner07a38e72003-10-31 21:05:12 +000036static bool isOnlyADirectCall(Function *F, CallSite CS) {
37 if (!CS.getInstruction()) return false;
38 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
39 if (*I == F) return false;
40 return true;
41}
42
Chris Lattnerd99d4d72002-07-18 04:43:16 +000043// addToCallGraph - Add a function to the call graph, and link the node to all
44// of the functions that it calls.
Chris Lattner41fbf302001-09-28 00:08:15 +000045//
Chris Lattner5714c972003-08-31 20:36:52 +000046void CallGraph::addToCallGraph(Function *F) {
47 CallGraphNode *Node = getNodeFor(F);
Chris Lattner41fbf302001-09-28 00:08:15 +000048
Chris Lattnerf52d01b2003-09-15 04:35:16 +000049 // If this function has external linkage, anything could call it...
Chris Lattner5714c972003-08-31 20:36:52 +000050 if (!F->hasInternalLinkage()) {
Chris Lattnerb81c0212004-04-12 05:36:32 +000051 ExternalCallingNode->addCalledFunction(Node);
Chris Lattner25e9cad2001-11-26 18:51:25 +000052
Chris Lattnerd4d427b2002-03-06 20:19:35 +000053 // Found the entry point?
Chris Lattner5714c972003-08-31 20:36:52 +000054 if (F->getName() == "main") {
Chris Lattnerb81c0212004-04-12 05:36:32 +000055 if (Root) // Found multiple external mains? Don't pick one.
56 Root = ExternalCallingNode;
Chris Lattnerd4d427b2002-03-06 20:19:35 +000057 else
58 Root = Node; // Found a main, keep track of it!
59 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000060 }
Chris Lattnerf52d01b2003-09-15 04:35:16 +000061
62 // If this function is not defined in this translation unit, it could call
63 // anything.
Chris Lattnerb81c0212004-04-12 05:36:32 +000064 if (F->isExternal() && !F->getIntrinsicID())
65 Node->addCalledFunction(CallsExternalNode);
Chris Lattnerd4d427b2002-03-06 20:19:35 +000066
Chris Lattnerd99d4d72002-07-18 04:43:16 +000067 // Loop over all of the users of the function... looking for callers...
Chris Lattnerd4d427b2002-03-06 20:19:35 +000068 //
Chris Lattner07a38e72003-10-31 21:05:12 +000069 bool isUsedExternally = false;
Chris Lattner5714c972003-08-31 20:36:52 +000070 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
Chris Lattner07a38e72003-10-31 21:05:12 +000071 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
72 if (isOnlyADirectCall(F, CallSite::get(Inst)))
73 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
74 else
75 isUsedExternally = true;
76 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(*I)) {
77 // THIS IS A DISGUSTING HACK. Brought to you by the power of
78 // ConstantPointerRefs!
79 for (Value::use_iterator I = CPR->use_begin(), E = CPR->use_end();
80 I != E; ++I)
81 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
82 if (isOnlyADirectCall(F, CallSite::get(Inst)))
83 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
84 else
85 isUsedExternally = true;
86 } else {
87 isUsedExternally = true;
88 }
89 } else { // Can't classify the user!
90 isUsedExternally = true;
91 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000092 }
Chris Lattner07a38e72003-10-31 21:05:12 +000093 if (isUsedExternally)
Chris Lattnerb81c0212004-04-12 05:36:32 +000094 ExternalCallingNode->addCalledFunction(Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +000095
Chris Lattnerd99d4d72002-07-18 04:43:16 +000096 // Look for an indirect function call...
Chris Lattner5714c972003-08-31 20:36:52 +000097 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
Chris Lattnerd4d427b2002-03-06 20:19:35 +000098 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
Chris Lattner07a38e72003-10-31 21:05:12 +000099 CallSite CS = CallSite::get(II);
100 if (CS.getInstruction() && !CS.getCalledFunction())
Chris Lattnerb81c0212004-04-12 05:36:32 +0000101 Node->addCalledFunction(CallsExternalNode);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000102 }
Chris Lattner41fbf302001-09-28 00:08:15 +0000103}
104
Chris Lattner7e708292002-06-25 16:13:24 +0000105bool CallGraph::run(Module &M) {
Chris Lattner93193f82002-01-31 00:42:27 +0000106 destroy();
107
Chris Lattner7e708292002-06-25 16:13:24 +0000108 Mod = &M;
Chris Lattnerb81c0212004-04-12 05:36:32 +0000109 ExternalCallingNode = getNodeFor(0);
110 CallsExternalNode = new CallGraphNode(0);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000111 Root = 0;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000112
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000113 // Add every function to the call graph...
Chris Lattner7e708292002-06-25 16:13:24 +0000114 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
115 addToCallGraph(I);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000116
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000117 // If we didn't find a main function, use the external call graph node
Chris Lattnerb81c0212004-04-12 05:36:32 +0000118 if (Root == 0) Root = ExternalCallingNode;
Chris Lattner93193f82002-01-31 00:42:27 +0000119
120 return false;
Chris Lattner41fbf302001-09-28 00:08:15 +0000121}
122
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000123void CallGraph::destroy() {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000124 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000125 I != E; ++I)
Chris Lattner25e9cad2001-11-26 18:51:25 +0000126 delete I->second;
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000127 FunctionMap.clear();
Chris Lattner224f7e62004-05-02 07:31:34 +0000128 delete CallsExternalNode;
Chris Lattner7b11e332004-05-02 16:06:18 +0000129 CallsExternalNode = 0;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000130}
131
Chris Lattner16500152002-11-04 00:21:19 +0000132static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000133 if (CGN->getFunction())
134 o << "Call graph node for function: '"
135 << CGN->getFunction()->getName() <<"'\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000136 else
Chris Lattnerb31247a2003-09-15 04:29:37 +0000137 o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000138
Chris Lattner41fbf302001-09-28 00:08:15 +0000139 for (unsigned i = 0; i < CGN->size(); ++i)
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000140 if ((*CGN)[i]->getFunction())
141 o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000142 else
143 o << " Calls external node\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000144 o << "\n";
Chris Lattner41fbf302001-09-28 00:08:15 +0000145}
146
Chris Lattner16500152002-11-04 00:21:19 +0000147void CallGraph::print(std::ostream &o, const Module *M) const {
Chris Lattnerb31247a2003-09-15 04:29:37 +0000148 o << "CallGraph Root is: ";
149 if (getRoot()->getFunction())
150 o << getRoot()->getFunction()->getName() << "\n";
151 else
152 o << "<<null function: 0x" << getRoot() << ">>\n";
153
Chris Lattner16500152002-11-04 00:21:19 +0000154 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
155 WriteToOutput(I->second, o);
Chris Lattner41fbf302001-09-28 00:08:15 +0000156}
Vikram S. Advea7edb182001-10-22 13:55:46 +0000157
158
Chris Lattner25e9cad2001-11-26 18:51:25 +0000159//===----------------------------------------------------------------------===//
160// Implementations of public modification methods
161//
162
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000163// Functions to keep a call graph up to date with a function that has been
Chris Lattner25e9cad2001-11-26 18:51:25 +0000164// modified
165//
Chris Lattnerb81c0212004-04-12 05:36:32 +0000166void CallGraph::addFunctionToModule(Function *F) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000167 assert(0 && "not implemented");
168 abort();
169}
170
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000171// removeFunctionFromModule - Unlink the function from this module, returning
172// it. Because this removes the function from the module, the call graph node
173// is destroyed. This is only valid if the function does not call any other
174// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000175// is to dropAllReferences before calling this.
176//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000177Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
178 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
179 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000180 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000181 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000182 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000183
Chris Lattner5714c972003-08-31 20:36:52 +0000184 Mod->getFunctionList().remove(F);
185 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000186}
187
Chris Lattner14fffaf2003-10-30 05:17:30 +0000188void CallGraph::stub() {}
Brian Gaeked0fde302003-11-11 22:41:34 +0000189
Chris Lattnerb81c0212004-04-12 05:36:32 +0000190void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
191 for (unsigned i = CalledFunctions.size(); ; --i) {
192 assert(i && "Cannot find callee to remove!");
193 if (CalledFunctions[i-1] == Callee) {
194 CalledFunctions.erase(CalledFunctions.begin()+i-1);
195 return;
196 }
197 }
198}