Add a newline to the end of this file.
[oota-llvm.git] / lib / Analysis / IPA / CallGraph.cpp
index 78bb735ffa23aa84610125ac260ad575fa32803d..5f9850c93dcbff08dd43d71558e6eed0fec6971a 100644 (file)
 #include "llvm/Module.h"
 #include "llvm/Instructions.h"
 #include "llvm/Support/CallSite.h"
-#include <iostream>
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/Streams.h"
+#include <ostream>
 using namespace llvm;
 
+/// isOnlyADirectCall - Return true if this callsite is *just* a direct call to
+/// the specified function.  Specifically return false if the callsite also
+/// takes the address of the function.
 static bool isOnlyADirectCall(Function *F, CallSite CS) {
   if (!CS.getInstruction()) return false;
   for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
@@ -31,7 +36,7 @@ namespace {
 //===----------------------------------------------------------------------===//
 // BasicCallGraph class definition
 //
-class BasicCallGraph : public CallGraph, public ModulePass {
+class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass {
   // Root is root of the call graph, or the external node if a 'main' function
   // couldn't be found.
   //
@@ -46,12 +51,12 @@ class BasicCallGraph : public CallGraph, public ModulePass {
   CallGraphNode *CallsExternalNode;
 
 public:
-  BasicCallGraph() : Root(0), ExternalCallingNode(0), CallsExternalNode(0) {}
-  ~BasicCallGraph() { destroy(); }
+  static char ID; // Class identification, replacement for typeinfo
+  BasicCallGraph() : ModulePass((intptr_t)&ID), Root(0), 
+    ExternalCallingNode(0), CallsExternalNode(0) {}
 
   // runOnModule - Compute the call graph for the specified module.
   virtual bool runOnModule(Module &M) {
-    destroy();
     CallGraph::initialize(M);
     
     ExternalCallingNode = getOrInsertFunction(0);
@@ -72,6 +77,10 @@ public:
     AU.setPreservesAll();
   }
 
+  void print(std::ostream *o, const Module *M) const {
+    if (o) print(*o, M);
+  }
+
   virtual void print(std::ostream &o, const Module *M) const {
     o << "CallGraph Root is: ";
     if (Function *F = getRoot()->getFunction())
@@ -89,7 +98,7 @@ public:
   /// dump - Print out this call graph.
   ///
   inline void dump() const {
-    print(std::cerr, Mod);
+    print(cerr, Mod);
   }
 
   CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
@@ -112,9 +121,9 @@ private:
   void addToCallGraph(Function *F) {
     CallGraphNode *Node = getOrInsertFunction(F);
 
-    // If this function has external linkage, anything could call it...
+    // If this function has external linkage, anything could call it.
     if (!F->hasInternalLinkage()) {
-      ExternalCallingNode->addCalledFunction(Node);
+      ExternalCallingNode->addCalledFunction(CallSite(), Node);
 
       // Found the entry point?
       if (F->getName() == "main") {
@@ -127,28 +136,30 @@ private:
 
     // If this function is not defined in this translation unit, it could call
     // anything.
-    if (F->isExternal() && !F->getIntrinsicID())
-      Node->addCalledFunction(CallsExternalNode);
+    if (F->isDeclaration() && !F->getIntrinsicID())
+      Node->addCalledFunction(CallSite(), CallsExternalNode);
 
     // Loop over all of the users of the function... looking for callers...
     //
     bool isUsedExternally = false;
     for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
       if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
-        if (isOnlyADirectCall(F, CallSite::get(Inst)))
+        CallSite CS = CallSite::get(Inst);
+        if (isOnlyADirectCall(F, CS))
           getOrInsertFunction(Inst->getParent()->getParent())
-              ->addCalledFunction(Node);
+              ->addCalledFunction(CS, Node);
         else
           isUsedExternally = true;
       } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
         for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
              I != E; ++I)
           if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
-          if (isOnlyADirectCall(F, CallSite::get(Inst)))
-            getOrInsertFunction(Inst->getParent()->getParent())
-                ->addCalledFunction(Node);
-          else
-            isUsedExternally = true;
+            CallSite CS = CallSite::get(Inst);
+            if (isOnlyADirectCall(F, CS))
+              getOrInsertFunction(Inst->getParent()->getParent())
+                ->addCalledFunction(CS, Node);
+            else
+              isUsedExternally = true;
           } else {
             isUsedExternally = true;
           }
@@ -157,41 +168,43 @@ private:
       }
     }
     if (isUsedExternally)
-      ExternalCallingNode->addCalledFunction(Node);
+      ExternalCallingNode->addCalledFunction(CallSite(), Node);
 
-  // Look for an indirect function call...
+    // Look for an indirect function call.
     for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
       for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
            II != IE; ++II) {
       CallSite CS = CallSite::get(II);
       if (CS.getInstruction() && !CS.getCalledFunction())
-        Node->addCalledFunction(CallsExternalNode);
+        Node->addCalledFunction(CS, CallsExternalNode);
       }
   }
 
   //
   // destroy - Release memory for the call graph
   virtual void destroy() {
-    if (!CallsExternalNode) {
-      delete CallsExternalNode;
-      CallsExternalNode = 0;
-    }
+    /// CallsExternalNode is not in the function map, delete it explicitly.
+    delete CallsExternalNode;
+    CallsExternalNode = 0;
+    CallGraph::destroy();
   }
 };
 
 RegisterAnalysisGroup<CallGraph> X("Call Graph");
-RegisterOpt<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction");
-RegisterAnalysisGroup<CallGraph, BasicCallGraph, true> Z;
+RegisterPass<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction");
+RegisterAnalysisGroup<CallGraph, true> Z(Y);
 
 } //End anonymous namespace
 
+char CallGraph::ID = 0;
+char BasicCallGraph::ID = 0;
+
 void CallGraph::initialize(Module &M) {
-  destroy();
   Mod = &M;
 }
 
 void CallGraph::destroy() {
-  if(!FunctionMap.size()) {
+  if (!FunctionMap.empty()) {
     for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
         I != E; ++I)
       delete I->second;
@@ -205,7 +218,7 @@ void CallGraph::print(std::ostream &OS, const Module *M) const {
 }
 
 void CallGraph::dump() const {
-  print(std::cerr, 0);
+  print(cerr, 0);
 }
 
 //===----------------------------------------------------------------------===//
@@ -261,19 +274,19 @@ void CallGraphNode::print(std::ostream &OS) const {
     OS << "Call graph node <<null function: 0x" << this << ">>:\n";
 
   for (const_iterator I = begin(), E = end(); I != E; ++I)
-    if ((*I)->getFunction())
-      OS << "  Calls function '" << (*I)->getFunction()->getName() << "'\n";
+    if (I->second->getFunction())
+      OS << "  Calls function '" << I->second->getFunction()->getName() <<"'\n";
   else
     OS << "  Calls external node\n";
   OS << "\n";
 }
 
-void CallGraphNode::dump() const { print(std::cerr); }
+void CallGraphNode::dump() const { print(cerr); }
 
 void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
   for (unsigned i = CalledFunctions.size(); ; --i) {
     assert(i && "Cannot find callee to remove!");
-    if (CalledFunctions[i-1] == Callee) {
+    if (CalledFunctions[i-1].second == Callee) {
       CalledFunctions.erase(CalledFunctions.begin()+i-1);
       return;
     }
@@ -285,7 +298,7 @@ void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
 // removeCallEdgeTo, so it should not be used unless necessary.
 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
   for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
-    if (CalledFunctions[i] == Callee) {
+    if (CalledFunctions[i].second == Callee) {
       CalledFunctions[i] = CalledFunctions.back();
       CalledFunctions.pop_back();
       --i; --e;