Remove trailing whitespace
[oota-llvm.git] / lib / Analysis / IPA / CallGraph.cpp
1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
2 //
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 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the CallGraph class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/CallGraph.h"
15 #include "llvm/Module.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/Support/CallSite.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include <iostream>
20 using namespace llvm;
21
22 static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
23
24 // getNodeFor - Return the node for the specified function or create one if it
25 // does not already exist.
26 //
27 CallGraphNode *CallGraph::getNodeFor(Function *F) {
28   CallGraphNode *&CGN = FunctionMap[F];
29   if (CGN) return CGN;
30
31   assert((!F || F->getParent() == Mod) && "Function not in current module!");
32   return CGN = new CallGraphNode(F);
33 }
34
35 static 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
42 // addToCallGraph - Add a function to the call graph, and link the node to all
43 // of the functions that it calls.
44 //
45 void CallGraph::addToCallGraph(Function *F) {
46   CallGraphNode *Node = getNodeFor(F);
47
48   // If this function has external linkage, anything could call it...
49   if (!F->hasInternalLinkage()) {
50     ExternalCallingNode->addCalledFunction(Node);
51
52     // Found the entry point?
53     if (F->getName() == "main") {
54       if (Root)    // Found multiple external mains?  Don't pick one.
55         Root = ExternalCallingNode;
56       else
57         Root = Node;          // Found a main, keep track of it!
58     }
59   }
60
61   // If this function is not defined in this translation unit, it could call
62   // anything.
63   if (F->isExternal() && !F->getIntrinsicID())
64     Node->addCalledFunction(CallsExternalNode);
65
66   // Loop over all of the users of the function... looking for callers...
67   //
68   bool isUsedExternally = false;
69   for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
70     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;
75     } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
76       for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
77            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     }
89   }
90   if (isUsedExternally)
91     ExternalCallingNode->addCalledFunction(Node);
92
93   // Look for an indirect function call...
94   for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
95     for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
96       CallSite CS = CallSite::get(II);
97       if (CS.getInstruction() && !CS.getCalledFunction())
98         Node->addCalledFunction(CallsExternalNode);
99     }
100 }
101
102 bool CallGraph::runOnModule(Module &M) {
103   destroy();
104
105   Mod = &M;
106   ExternalCallingNode = getNodeFor(0);
107   CallsExternalNode = new CallGraphNode(0);
108   Root = 0;
109
110   // Add every function to the call graph...
111   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
112     addToCallGraph(I);
113
114   // If we didn't find a main function, use the external call graph node
115   if (Root == 0) Root = ExternalCallingNode;
116
117   return false;
118 }
119
120 void CallGraph::destroy() {
121   for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
122        I != E; ++I)
123     delete I->second;
124   FunctionMap.clear();
125   delete CallsExternalNode;
126   CallsExternalNode = 0;
127 }
128
129 void CallGraphNode::print(std::ostream &OS) const {
130   if (Function *F = getFunction())
131     OS << "Call graph node for function: '" << F->getName() <<"'\n";
132   else
133     OS << "Call graph node <<null function: 0x" << this << ">>:\n";
134
135   for (const_iterator I = begin(), E = end(); I != E; ++I)
136     if ((*I)->getFunction())
137       OS << "  Calls function '" << (*I)->getFunction()->getName() << "'\n";
138     else
139       OS << "  Calls external node\n";
140   OS << "\n";
141 }
142
143 void CallGraphNode::dump() const { print(std::cerr); }
144
145 void 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";
149   else
150     OS << "<<null function: 0x" << getRoot() << ">>\n";
151
152   for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
153     I->second->print(OS);
154 }
155
156 void CallGraph::dump() const {
157   print(std::cerr, 0);
158 }
159
160
161 //===----------------------------------------------------------------------===//
162 // Implementations of public modification methods
163 //
164
165 // 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
169 // is to dropAllReferences before calling this.
170 //
171 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
172   assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
173          "graph if it references other functions!");
174   Function *F = CGN->getFunction(); // Get the function for the call graph node
175   delete CGN;                       // Delete the call graph node for this func
176   FunctionMap.erase(F);             // Remove the call graph node from the map
177
178   Mod->getFunctionList().remove(F);
179   return F;
180 }
181
182 // 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).
186 void 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;
192   New->F = NewF;
193   FunctionMap.erase(I);
194 }
195
196
197 void CallGraph::stub() {}
198
199 void 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 }
208
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.
212 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
213   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;
218     }
219 }