Change the call graph class to have TWO external nodes, making call graph
[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/Constants.h"     // Remove when ConstantPointerRefs are gone
16 #include "llvm/Module.h"
17 #include "llvm/iOther.h"
18 #include "llvm/iTerminators.h"
19 #include "llvm/Support/CallSite.h"
20 #include "Support/STLExtras.h"
21 using namespace llvm;
22
23 static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
24
25 // getNodeFor - Return the node for the specified function or create one if it
26 // does not already exist.
27 //
28 CallGraphNode *CallGraph::getNodeFor(Function *F) {
29   CallGraphNode *&CGN = FunctionMap[F];
30   if (CGN) return CGN;
31
32   assert((!F || F->getParent() == Mod) && "Function not in current module!");
33   return CGN = new CallGraphNode(F);
34 }
35
36 static 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
43 // addToCallGraph - Add a function to the call graph, and link the node to all
44 // of the functions that it calls.
45 //
46 void CallGraph::addToCallGraph(Function *F) {
47   CallGraphNode *Node = getNodeFor(F);
48
49   // If this function has external linkage, anything could call it...
50   if (!F->hasInternalLinkage()) {
51     ExternalCallingNode->addCalledFunction(Node);
52
53     // Found the entry point?
54     if (F->getName() == "main") {
55       if (Root)    // Found multiple external mains?  Don't pick one.
56         Root = ExternalCallingNode;
57       else
58         Root = Node;          // Found a main, keep track of it!
59     }
60   }
61   
62   // If this function is not defined in this translation unit, it could call
63   // anything.
64   if (F->isExternal() && !F->getIntrinsicID())
65     Node->addCalledFunction(CallsExternalNode);
66
67   // Loop over all of the users of the function... looking for callers...
68   //
69   bool isUsedExternally = false;
70   for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
71     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     }
92   }
93   if (isUsedExternally)
94     ExternalCallingNode->addCalledFunction(Node);
95
96   // Look for an indirect function call...
97   for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
98     for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
99       CallSite CS = CallSite::get(II);
100       if (CS.getInstruction() && !CS.getCalledFunction())
101         Node->addCalledFunction(CallsExternalNode);
102     }
103 }
104
105 bool CallGraph::run(Module &M) {
106   destroy();
107
108   Mod = &M;
109   ExternalCallingNode = getNodeFor(0);
110   CallsExternalNode = new CallGraphNode(0);
111   Root = 0;
112
113   // Add every function to the call graph...
114   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
115     addToCallGraph(I);
116
117   // If we didn't find a main function, use the external call graph node
118   if (Root == 0) Root = ExternalCallingNode;
119   
120   return false;
121 }
122
123 void CallGraph::destroy() {
124   for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
125        I != E; ++I)
126     delete I->second;
127   FunctionMap.clear();
128 }
129
130 static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
131   if (CGN->getFunction())
132     o << "Call graph node for function: '"
133       << CGN->getFunction()->getName() <<"'\n";
134   else
135     o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
136
137   for (unsigned i = 0; i < CGN->size(); ++i)
138     if ((*CGN)[i]->getFunction())
139       o << "  Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
140     else
141       o << "  Calls external node\n";
142   o << "\n";
143 }
144
145 void CallGraph::print(std::ostream &o, const Module *M) const {
146   o << "CallGraph Root is: ";
147   if (getRoot()->getFunction())
148     o << getRoot()->getFunction()->getName() << "\n";
149   else
150     o << "<<null function: 0x" << getRoot() << ">>\n";
151   
152   for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
153     WriteToOutput(I->second, o);
154 }
155
156
157 //===----------------------------------------------------------------------===//
158 // Implementations of public modification methods
159 //
160
161 // Functions to keep a call graph up to date with a function that has been
162 // modified
163 //
164 void CallGraph::addFunctionToModule(Function *F) {
165   assert(0 && "not implemented");
166   abort();
167 }
168
169 // 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
173 // is to dropAllReferences before calling this.
174 //
175 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
176   assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
177          "graph if it references other functions!");
178   Function *F = CGN->getFunction(); // Get the function for the call graph node
179   delete CGN;                       // Delete the call graph node for this func
180   FunctionMap.erase(F);             // Remove the call graph node from the map
181
182   Mod->getFunctionList().remove(F);
183   return F;
184 }
185
186 void CallGraph::stub() {}
187
188 void 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 }