blob: 72b4bdbc52088d23a98d6ad3d88ddfa020159909 [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;
Reid Spencere8404342004-07-18 00:18:30 +000076 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
77 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
Chris Lattner07a38e72003-10-31 21:05:12 +000078 I != E; ++I)
79 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
80 if (isOnlyADirectCall(F, CallSite::get(Inst)))
81 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
82 else
83 isUsedExternally = true;
84 } else {
85 isUsedExternally = true;
86 }
87 } else { // Can't classify the user!
88 isUsedExternally = true;
89 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000090 }
Chris Lattner07a38e72003-10-31 21:05:12 +000091 if (isUsedExternally)
Chris Lattnerb81c0212004-04-12 05:36:32 +000092 ExternalCallingNode->addCalledFunction(Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +000093
Chris Lattnerd99d4d72002-07-18 04:43:16 +000094 // Look for an indirect function call...
Chris Lattner5714c972003-08-31 20:36:52 +000095 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
Chris Lattnerd4d427b2002-03-06 20:19:35 +000096 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
Chris Lattner07a38e72003-10-31 21:05:12 +000097 CallSite CS = CallSite::get(II);
98 if (CS.getInstruction() && !CS.getCalledFunction())
Chris Lattnerb81c0212004-04-12 05:36:32 +000099 Node->addCalledFunction(CallsExternalNode);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000100 }
Chris Lattner41fbf302001-09-28 00:08:15 +0000101}
102
Chris Lattner7e708292002-06-25 16:13:24 +0000103bool CallGraph::run(Module &M) {
Chris Lattner93193f82002-01-31 00:42:27 +0000104 destroy();
105
Chris Lattner7e708292002-06-25 16:13:24 +0000106 Mod = &M;
Chris Lattnerb81c0212004-04-12 05:36:32 +0000107 ExternalCallingNode = getNodeFor(0);
108 CallsExternalNode = new CallGraphNode(0);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000109 Root = 0;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000110
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000111 // Add every function to the call graph...
Chris Lattner7e708292002-06-25 16:13:24 +0000112 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
113 addToCallGraph(I);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000114
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000115 // If we didn't find a main function, use the external call graph node
Chris Lattnerb81c0212004-04-12 05:36:32 +0000116 if (Root == 0) Root = ExternalCallingNode;
Chris Lattner93193f82002-01-31 00:42:27 +0000117
118 return false;
Chris Lattner41fbf302001-09-28 00:08:15 +0000119}
120
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000121void CallGraph::destroy() {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000122 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000123 I != E; ++I)
Chris Lattner25e9cad2001-11-26 18:51:25 +0000124 delete I->second;
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000125 FunctionMap.clear();
Chris Lattner224f7e62004-05-02 07:31:34 +0000126 delete CallsExternalNode;
Chris Lattner7b11e332004-05-02 16:06:18 +0000127 CallsExternalNode = 0;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000128}
129
Chris Lattner16500152002-11-04 00:21:19 +0000130static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000131 if (CGN->getFunction())
132 o << "Call graph node for function: '"
133 << CGN->getFunction()->getName() <<"'\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000134 else
Chris Lattnerb31247a2003-09-15 04:29:37 +0000135 o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000136
Chris Lattner41fbf302001-09-28 00:08:15 +0000137 for (unsigned i = 0; i < CGN->size(); ++i)
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000138 if ((*CGN)[i]->getFunction())
139 o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000140 else
141 o << " Calls external node\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000142 o << "\n";
Chris Lattner41fbf302001-09-28 00:08:15 +0000143}
144
Chris Lattner16500152002-11-04 00:21:19 +0000145void CallGraph::print(std::ostream &o, const Module *M) const {
Chris Lattnerb31247a2003-09-15 04:29:37 +0000146 o << "CallGraph Root is: ";
147 if (getRoot()->getFunction())
148 o << getRoot()->getFunction()->getName() << "\n";
149 else
150 o << "<<null function: 0x" << getRoot() << ">>\n";
151
Chris Lattner16500152002-11-04 00:21:19 +0000152 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
153 WriteToOutput(I->second, o);
Chris Lattner41fbf302001-09-28 00:08:15 +0000154}
Vikram S. Advea7edb182001-10-22 13:55:46 +0000155
156
Chris Lattner25e9cad2001-11-26 18:51:25 +0000157//===----------------------------------------------------------------------===//
158// Implementations of public modification methods
159//
160
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000161// Functions to keep a call graph up to date with a function that has been
Chris Lattner25e9cad2001-11-26 18:51:25 +0000162// modified
163//
Chris Lattnerb81c0212004-04-12 05:36:32 +0000164void CallGraph::addFunctionToModule(Function *F) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000165 assert(0 && "not implemented");
166 abort();
167}
168
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000169// removeFunctionFromModule - Unlink the function from this module, returning
170// it. Because this removes the function from the module, the call graph node
171// is destroyed. This is only valid if the function does not call any other
172// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000173// is to dropAllReferences before calling this.
174//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000175Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
176 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
177 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000178 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000179 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000180 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000181
Chris Lattner5714c972003-08-31 20:36:52 +0000182 Mod->getFunctionList().remove(F);
183 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000184}
185
Chris Lattner14fffaf2003-10-30 05:17:30 +0000186void CallGraph::stub() {}
Brian Gaeked0fde302003-11-11 22:41:34 +0000187
Chris Lattnerb81c0212004-04-12 05:36:32 +0000188void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
189 for (unsigned i = CalledFunctions.size(); ; --i) {
190 assert(i && "Cannot find callee to remove!");
191 if (CalledFunctions[i-1] == Callee) {
192 CalledFunctions.erase(CalledFunctions.begin()+i-1);
193 return;
194 }
195 }
196}