Changes For Bug 352
[oota-llvm.git] / lib / Analysis / DataStructure / IPModRef.cpp
index 8c507e9323a0654d969d285e153bc423386b9d27..ccc15f74e499937f479ced2d1e6ac9b5a123ab44 100644 (file)
@@ -1,21 +1,28 @@
 //===- IPModRef.cpp - Compute IP Mod/Ref information ------------*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
 //
-// See high-level comments in include/llvm/Analysis/IPModRef.h
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
+//
+// See high-level comments in IPModRef.h
 // 
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Analysis/IPModRef.h"
-#include "llvm/Analysis/DataStructure.h"
-#include "llvm/Analysis/DSGraph.h"
+#include "IPModRef.h"
+#include "llvm/Analysis/DataStructure/DataStructure.h"
+#include "llvm/Analysis/DataStructure/DSGraph.h"
 #include "llvm/Module.h"
-#include "llvm/Function.h"
-#include "llvm/iMemory.h"
-#include "llvm/iOther.h"
-#include "Support/Statistic.h"
-#include "Support/STLExtras.h"
-#include "Support/StringExtras.h"
+#include "llvm/Instructions.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include <vector>
 
+namespace llvm {
+
 //----------------------------------------------------------------------------
 // Private constants and data
 //----------------------------------------------------------------------------
@@ -54,14 +61,16 @@ FunctionModRefInfo::FunctionModRefInfo(const Function& func,
     funcTDGraph(tdgClone),
     funcModRefInfo(tdgClone->getGraphSize())
 {
-  for (unsigned i=0, N = funcTDGraph->getGraphSize(); i < N; ++i)
-    NodeIds[funcTDGraph->getNodes()[i]] = i;
+  unsigned i = 0;
+  for (DSGraph::node_iterator NI = funcTDGraph->node_begin(),
+         E = funcTDGraph->node_end(); NI != E; ++NI)
+    NodeIds[*NI] = i++;
 }
 
 
 FunctionModRefInfo::~FunctionModRefInfo()
 {
-  for(std::map<const CallInst*, ModRefInfo*>::iterator
+  for(std::map<const Instruction*, ModRefInfo*>::iterator
         I=callSiteModRefInfo.begin(), E=callSiteModRefInfo.end(); I != E; ++I)
     delete(I->second);
 
@@ -86,19 +95,18 @@ void FunctionModRefInfo::computeModRef(const Function &func)
 {
   // Mark all nodes in the graph that are marked MOD as being mod
   // and all those marked REF as being ref.
-  for (unsigned i = 0, N = funcTDGraph->getGraphSize(); i < N; ++i)
-    {
-      if (funcTDGraph->getNodes()[i]->isModified())
-        funcModRefInfo.setNodeIsMod(i);
-      if (funcTDGraph->getNodes()[i]->isRead())
-        funcModRefInfo.setNodeIsRef(i);
-    }
+  unsigned i = 0;
+  for (DSGraph::node_iterator NI = funcTDGraph->node_begin(),
+         E = funcTDGraph->node_end(); NI != E; ++NI, ++i) {
+    if ((*NI)->isModified()) funcModRefInfo.setNodeIsMod(i);
+    if ((*NI)->isRead())     funcModRefInfo.setNodeIsRef(i);
+  }
 
   // Compute the Mod/Ref info for all call sites within the function.
   // The call sites are recorded in the TD graph.
   const std::vector<DSCallSite>& callSites = funcTDGraph->getFunctionCalls();
   for (unsigned i = 0, N = callSites.size(); i < N; ++i)
-    computeModRef(callSites[i].getCallInst());
+    computeModRef(callSites[i].getCallSite());
 }
 
 
@@ -117,25 +125,22 @@ void FunctionModRefInfo::computeModRef(const Function &func)
 //       requested information (because the call site calls an external
 //       function or we cannot determine the complete set of functions invoked).
 //
-DSGraph* FunctionModRefInfo::ResolveCallSiteModRefInfo(CallInst &CI,
-                               std::map<const DSNode*, DSNodeHandle> &NodeMap)
+DSGraph* FunctionModRefInfo::ResolveCallSiteModRefInfo(CallSite CS,
+                               hash_map<const DSNode*, DSNodeHandle> &NodeMap)
 {
   // Step #0: Quick check if we are going to fail anyway: avoid
   // all the graph cloning and map copying in steps #1 and #2.
   // 
-  if (const Function *F = CI.getCalledFunction())
-    {
-      if (F->isExternal())
-        return 0;   // We cannot compute Mod/Ref info for this callsite...
-    }
-  else
-    {
-      // Eventually, should check here if any callee is external.
-      // For now we are not handling this case anyway.
-      std::cerr << "IP Mod/Ref indirect call not implemented yet: "
-              << "Being conservative\n";
+  if (const Function *F = CS.getCalledFunction()) {
+    if (F->isExternal())
       return 0;   // We cannot compute Mod/Ref info for this callsite...
-    }
+  } else {
+    // Eventually, should check here if any callee is external.
+    // For now we are not handling this case anyway.
+    std::cerr << "IP Mod/Ref indirect call not implemented yet: "
+              << "Being conservative\n";
+    return 0;   // We cannot compute Mod/Ref info for this callsite...
+  }
 
   // Step #1: Clone the top-down graph...
   DSGraph *Result = new DSGraph(*funcTDGraph, NodeMap);
@@ -144,7 +149,7 @@ DSGraph* FunctionModRefInfo::ResolveCallSiteModRefInfo(CallInst &CI,
   Result->maskNodeTypes(~(DSNode::Modified | DSNode::Read));
 
   // Step #3: clone the bottom up graphs for the callees into the caller graph
-  if (const Function *F = CI.getCalledFunction())
+  if (Function *F = CS.getCalledFunction())
     {
       assert(!F->isExternal());
 
@@ -152,21 +157,22 @@ DSGraph* FunctionModRefInfo::ResolveCallSiteModRefInfo(CallInst &CI,
 
       // If the call returns a value, make sure to merge the nodes...
       DSNodeHandle RetVal;
-      if (DS::isPointerType(CI.getType()))
-        RetVal = Result->getNodeForValue(&CI);
+      if (DS::isPointerType(CS.getInstruction()->getType()))
+        RetVal = Result->getNodeForValue(CS.getInstruction());
 
       // Populate the arguments list...
       std::vector<DSNodeHandle> Args;
-      for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
-        if (DS::isPointerType(CI.getOperand(i)->getType()))
-          Args.push_back(Result->getNodeForValue(CI.getOperand(i)));
+      for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
+           I != E; ++I)
+        if (DS::isPointerType((*I)->getType()))
+          Args.push_back(Result->getNodeForValue(*I));
 
       // Build the call site...
-      DSCallSite CS(CI, RetVal, 0, Args);
+      DSCallSite NCS(CS, RetVal, F, Args);
 
       // Perform the merging now of the graph for the callee, which will
       // come with mod/ref bits set...
-      Result->mergeInGraph(CS, IPModRefObj.getBUDSGraph(*F),
+      Result->mergeInGraph(NCS, *F, IPModRefObj.getBUDSGraph(*F),
                            DSGraph::StripAllocaBit
                            | DSGraph::DontCloneCallNodes
                            | DSGraph::DontCloneAuxCallNodes);
@@ -175,7 +181,7 @@ DSGraph* FunctionModRefInfo::ResolveCallSiteModRefInfo(CallInst &CI,
     assert(0 && "See error message");
 
   // Remove dead nodes aggressively to match the caller's original graph.
-  Result->removeDeadNodes();
+  Result->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
 
   // Step #4: Return the clone + the mapping (by ref)
   return Result;
@@ -183,20 +189,19 @@ DSGraph* FunctionModRefInfo::ResolveCallSiteModRefInfo(CallInst &CI,
 
 // Compute Mod/Ref bit vectors for a single call site.
 // These are copies of the Read/Write flags from the nodes of
-// the graph produced by clearing all flags in teh caller's TD graph
+// the graph produced by clearing all flags in the caller's TD graph
 // and then inlining the callee's BU graph into the caller's TD graph.
 // 
 void
-FunctionModRefInfo::computeModRef(const CallInst& callInst)
+FunctionModRefInfo::computeModRef(CallSite CS)
 {
   // Allocate the mod/ref info for the call site.  Bits automatically cleared.
   ModRefInfo* callModRefInfo = new ModRefInfo(funcTDGraph->getGraphSize());
-  callSiteModRefInfo[&callInst] = callModRefInfo;
+  callSiteModRefInfo[CS.getInstruction()] = callModRefInfo;
 
   // Get a copy of the graph for the callee with the callee inlined
-  std::map<const DSNode*, DSNodeHandle> NodeMap;
-  DSGraph* csgp = ResolveCallSiteModRefInfo(const_cast<CallInst&>(callInst),
-                                            NodeMap);
+  hash_map<const DSNode*, DSNodeHandle> NodeMap;
+  DSGraph* csgp = ResolveCallSiteModRefInfo(CS, NodeMap);
   if (!csgp)
     { // Callee's side effects are unknown: mark all nodes Mod and Ref.
       // Eventually this should only mark nodes visible to the callee, i.e.,
@@ -208,18 +213,15 @@ FunctionModRefInfo::computeModRef(const CallInst& callInst)
     }
 
   // For all nodes in the graph, extract the mod/ref information
-  const std::vector<DSNode*>& csgNodes = csgp->getNodes();
-  const std::vector<DSNode*>& origNodes = funcTDGraph->getNodes();
-  assert(csgNodes.size() == origNodes.size());
-  for (unsigned i=0, N = origNodes.size(); i < N; ++i)
-    { 
-      DSNode* csgNode = NodeMap[origNodes[i]].getNode();
-      assert(csgNode && "Inlined and original graphs do not correspond!");
-      if (csgNode->isModified())
-        callModRefInfo->setNodeIsMod(getNodeId(origNodes[i]));
-      if (csgNode->isRead())
-        callModRefInfo->setNodeIsRef(getNodeId(origNodes[i]));
-    }
+  for (DSGraph::node_iterator NI = funcTDGraph->node_begin(),
+         E = funcTDGraph->node_end(); NI != E; ++NI) { 
+    DSNode* csgNode = NodeMap[*NI].getNode();
+    assert(csgNode && "Inlined and original graphs do not correspond!");
+    if (csgNode->isModified())
+      callModRefInfo->setNodeIsMod(getNodeId(*NI));
+    if (csgNode->isRead())
+      callModRefInfo->setNodeIsRef(getNodeId(*NI));
+  }
 
   // Drop nodemap before we delete the graph...
   NodeMap.clear();
@@ -238,7 +240,7 @@ public:
     knownValues.resize(tdGraph.getGraphSize());
 
     // For every identifiable value, save Value pointer in knownValues[i]
-    for (std::map<Value*, DSNodeHandle>::const_iterator
+    for (hash_map<Value*, DSNodeHandle>::const_iterator
            I = tdGraph.getScalarMap().begin(),
            E = tdGraph.getScalarMap().end(); I != E; ++I)
       if (isa<GlobalValue>(I->first) ||
@@ -289,8 +291,10 @@ public:
 
                 O << std::string((j < NV-1)? "; " : "\n");
               }
+#if 0
           else
             tdGraph.getNodes()[i]->print(O, /*graph*/ NULL);
+#endif
         }
   }
 };
@@ -323,11 +327,11 @@ void FunctionModRefInfo::print(std::ostream &O) const
 
   // Second: Print Globals and Locals modified at each call site in function
   // 
-  for (std::map<const CallInst*, ModRefInfo*>::const_iterator
+  for (std::map<const Instruction *, ModRefInfo*>::const_iterator
          CI = callSiteModRefInfo.begin(), CE = callSiteModRefInfo.end();
        CI != CE; ++CI)
     {
-      O << "  ----Mod/Ref information for call site\n" << CI->first;
+      O << "  ----Mod/Ref information for call site\n" << *CI->first;
 
       O << "    --Objects modified at call site:\n";
       DPH.printValuesInBitVec(O, CI->second->getModSet());
@@ -393,7 +397,7 @@ FunctionModRefInfo& IPModRef::getFuncInfo(const Function& func,
       // The memory for this graph clone will be freed by FunctionModRefInfo.
       DSGraph* funcTDGraph =
         new DSGraph(getAnalysis<TDDataStructures>().getDSGraph(func));
-      funcTDGraph->removeDeadNodes();
+      funcTDGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
 
       funcInfo = new FunctionModRefInfo(func, *this, funcTDGraph); //auto-insert
       funcInfo->computeModRef(func);  // computes the mod/ref info
@@ -437,3 +441,5 @@ void IPModRef::dump() const
 {
   print(std::cerr);
 }
+
+} // End llvm namespace