blob: 8b46e5927461269540b756144b6e33dabe90e42e [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/ADT/STLExtras.h"
Chris Lattneraf8a4242004-08-08 03:27:49 +000020#include <iostream>
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 Lattneraf8a4242004-08-08 03:27:49 +0000130void CallGraphNode::print(std::ostream &OS) const {
131 if (Function *F = getFunction())
132 OS << "Call graph node for function: '" << F->getName() <<"'\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000133 else
Chris Lattneraf8a4242004-08-08 03:27:49 +0000134 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000135
Chris Lattneraf8a4242004-08-08 03:27:49 +0000136 for (const_iterator I = begin(), E = end(); I != E; ++I)
137 if ((*I)->getFunction())
138 OS << " Calls function '" << (*I)->getFunction()->getName() << "'\n";
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000139 else
Chris Lattneraf8a4242004-08-08 03:27:49 +0000140 OS << " Calls external node\n";
141 OS << "\n";
Chris Lattner41fbf302001-09-28 00:08:15 +0000142}
143
Chris Lattneraf8a4242004-08-08 03:27:49 +0000144void CallGraphNode::dump() const { print(std::cerr); }
145
146void CallGraph::print(std::ostream &OS, const Module *M) const {
147 OS << "CallGraph Root is: ";
148 if (Function *F = getRoot()->getFunction())
149 OS << F->getName() << "\n";
Chris Lattnerb31247a2003-09-15 04:29:37 +0000150 else
Chris Lattneraf8a4242004-08-08 03:27:49 +0000151 OS << "<<null function: 0x" << getRoot() << ">>\n";
Chris Lattnerb31247a2003-09-15 04:29:37 +0000152
Chris Lattner16500152002-11-04 00:21:19 +0000153 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000154 I->second->print(OS);
155}
156
157void CallGraph::dump() const {
158 print(std::cerr, 0);
Chris Lattner41fbf302001-09-28 00:08:15 +0000159}
Vikram S. Advea7edb182001-10-22 13:55:46 +0000160
161
Chris Lattner25e9cad2001-11-26 18:51:25 +0000162//===----------------------------------------------------------------------===//
163// Implementations of public modification methods
164//
165
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000166// removeFunctionFromModule - Unlink the function from this module, returning
167// it. Because this removes the function from the module, the call graph node
168// is destroyed. This is only valid if the function does not call any other
169// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000170// is to dropAllReferences before calling this.
171//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000172Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
173 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
174 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000175 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000176 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000177 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000178
Chris Lattner5714c972003-08-31 20:36:52 +0000179 Mod->getFunctionList().remove(F);
180 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000181}
182
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000183// changeFunction - This method changes the function associated with this
184// CallGraphNode, for use by transformations that need to change the prototype
185// of a Function (thus they must create a new Function and move the old code
186// over).
187void CallGraph::changeFunction(Function *OldF, Function *NewF) {
188 iterator I = FunctionMap.find(OldF);
189 CallGraphNode *&New = FunctionMap[NewF];
190 assert(I != FunctionMap.end() && I->second && !New &&
191 "OldF didn't exist in CG or NewF already does!");
192 New = I->second;
193 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}