Fix a bug pointed out by Zhongxing Xu
[oota-llvm.git] / lib / Analysis / IPA / CallGraph.cpp
index 03f73fa8d207ae46c3f1c99535f2a9fb503cc9b3..d9e7242695cffb4cf205a001e8512e1b71ae0584 100644 (file)
@@ -19,8 +19,6 @@
 #include <iostream>
 using namespace llvm;
 
-void llvm::BasicCallGraphStub() {}
-
 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)
@@ -56,7 +54,7 @@ public:
     destroy();
     CallGraph::initialize(M);
     
-    ExternalCallingNode = getNodeFor(0);
+    ExternalCallingNode = getOrInsertFunction(0);
     CallsExternalNode = new CallGraphNode(0);
     Root = 0;
   
@@ -107,28 +105,16 @@ private:
   //===---------------------------------------------------------------------
   // Implementation of CallGraph construction
   //
-  // getNodeFor - Return the node for the specified function or create one if it
-  // does not already exist.
-  //
-
-  CallGraphNode *getNodeFor(Function *F) {
-    CallGraphNode *&CGN = FunctionMap[F];
-    if (CGN) return CGN;
 
-    assert((!F || F->getParent() == Mod) && "Function not in current module!");
-    return CGN = new CallGraphNode(F);
-  }
-  
-  //
   // addToCallGraph - Add a function to the call graph, and link the node to all
   // of the functions that it calls.
   //
   void addToCallGraph(Function *F) {
-    CallGraphNode *Node = getNodeFor(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") {
@@ -142,25 +128,29 @@ private:
     // If this function is not defined in this translation unit, it could call
     // anything.
     if (F->isExternal() && !F->getIntrinsicID())
-      Node->addCalledFunction(CallsExternalNode);
+      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)))
-          getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
+        CallSite CS = CallSite::get(Inst);
+        if (isOnlyADirectCall(F, CS))
+          getOrInsertFunction(Inst->getParent()->getParent())
+              ->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)))
-            getNodeFor(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;
           }
@@ -169,15 +159,15 @@ 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);
       }
   }
 
@@ -192,8 +182,8 @@ private:
 };
 
 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
 
@@ -203,7 +193,7 @@ void CallGraph::initialize(Module &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;
@@ -216,6 +206,10 @@ void CallGraph::print(std::ostream &OS, const Module *M) const {
     I->second->print(OS);
 }
 
+void CallGraph::dump() const {
+  print(std::cerr, 0);
+}
+
 //===----------------------------------------------------------------------===//
 // Implementations of public modification methods
 //
@@ -251,7 +245,16 @@ void CallGraph::changeFunction(Function *OldF, Function *NewF) {
   FunctionMap.erase(I);
 }
 
-void CallGraph::stub() {}
+// getOrInsertFunction - This method is identical to calling operator[], but
+// it will insert a new CallGraphNode for the specified function if one does
+// not already exist.
+CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
+  CallGraphNode *&CGN = FunctionMap[F];
+  if (CGN) return CGN;
+  
+  assert((!F || F->getParent() == Mod) && "Function not in current module!");
+  return CGN = new CallGraphNode(const_cast<Function*>(F));
+}
 
 void CallGraphNode::print(std::ostream &OS) const {
   if (Function *F = getFunction())
@@ -260,8 +263,8 @@ 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";
@@ -272,7 +275,7 @@ void CallGraphNode::dump() const { print(std::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;
     }
@@ -284,9 +287,12 @@ 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;
     }
 }
+
+// Enuse that users of CallGraph.h also link with this file
+DEFINING_FILE_FOR(CallGraph)