If "These should be used only by the auto-parallelization pass", we might as
[oota-llvm.git] / lib / Analysis / DataStructure / Steensgaard.cpp
index 6ea2449483780a6a2be0cc100eddab4795f3d232..a3034ddeac2da6d65054825a31e2f492318365de 100644 (file)
 #include "llvm/Analysis/DSGraph.h"
 #include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/Module.h"
+#include "Support/Debug.h"
 
 namespace {
   class Steens : public Pass, public AliasAnalysis {
     DSGraph *ResultGraph;
     DSGraph *GlobalsGraph;  // FIXME: Eliminate globals graph stuff from DNE
   public:
-    Steens() : ResultGraph(0) {}
-    ~Steens() { assert(ResultGraph == 0 && "releaseMemory not called?"); }
+    Steens() : ResultGraph(0), GlobalsGraph(0) {}
+    ~Steens() {
+      releaseMyMemory();
+      assert(ResultGraph == 0 && "releaseMemory not called?");
+    }
 
     //------------------------------------------------
     // Implement the Pass API
@@ -29,9 +33,10 @@ namespace {
     //
     bool run(Module &M);
 
-    virtual void releaseMemory() { delete ResultGraph; ResultGraph = 0; }
+    virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AliasAnalysis::getAnalysisUsage(AU);
       AU.setPreservesAll();                    // Does not transform code...
       AU.addRequired<LocalDataStructures>();   // Uses local dsgraph
       AU.addRequired<AliasAnalysis>();         // Chains to another AA impl...
@@ -48,20 +53,9 @@ namespace {
     //  
 
     // alias - This is the only method here that does anything interesting...
-    Result alias(const Value *V1, const Value *V2);
-    
-    /// canCallModify - Not implemented yet: FIXME
-    ///
-    Result canCallModify(const CallInst &CI, const Value *Ptr) {
-      return MayAlias;
-    }
+    AliasResult alias(const Value *V1, unsigned V1Size,
+                      const Value *V2, unsigned V2Size);
     
-    /// canInvokeModify - Not implemented yet: FIXME
-    ///
-    Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
-      return MayAlias;
-    }
-
   private:
     void ResolveFunctionCall(Function *F, const DSCallSite &Call,
                              DSNodeHandle &RetVal);
@@ -83,7 +77,7 @@ namespace {
 void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
                                  DSNodeHandle &RetVal) {
   assert(ResultGraph != 0 && "Result graph not allocated!");
-  hash_map<Value*, DSNodeHandle> &ValMap = ResultGraph->getScalarMap();
+  DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
 
   // Handle the return value of the function...
   if (Call.getRetVal().getNode() && RetVal.getNode())
@@ -91,8 +85,9 @@ void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
 
   // Loop over all pointer arguments, resolving them to their provided pointers
   unsigned PtrArgIdx = 0;
-  for (Function::aiterator AI = F->abegin(), AE = F->aend(); AI != AE; ++AI) {
-    hash_map<Value*, DSNodeHandle>::iterator I = ValMap.find(AI);
+  for (Function::aiterator AI = F->abegin(), AE = F->aend();
+       AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
+    DSGraph::ScalarMapTy::iterator I = ValMap.find(AI);
     if (I != ValMap.end())    // If its a pointer argument...
       I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
   }
@@ -103,6 +98,7 @@ void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
 /// program.
 ///
 bool Steens::run(Module &M) {
+  InitializeAliasAnalysis(this);
   assert(ResultGraph == 0 && "Result graph already allocated!");
   LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
 
@@ -110,35 +106,35 @@ bool Steens::run(Module &M) {
   ResultGraph = new DSGraph();
   GlobalsGraph = new DSGraph();
   ResultGraph->setGlobalsGraph(GlobalsGraph);
+  ResultGraph->setPrintAuxCalls();
+
   // RetValMap - Keep track of the return values for all functions that return
   // valid pointers.
   //
-  hash_map<Function*, DSNodeHandle> RetValMap;
+  DSGraph::ReturnNodesTy RetValMap;
 
   // Loop over the rest of the module, merging graphs for non-external functions
   // into this graph.
   //
+  unsigned Count = 0;
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
     if (!I->isExternal()) {
-      hash_map<Value*, DSNodeHandle> ValMap;
+      DSGraph::ScalarMapTy ValMap;
       {  // Scope to free NodeMap memory ASAP
-        hash_map<const DSNode*, DSNodeHandle> NodeMap;
+        DSGraph::NodeMapTy NodeMap;
         const DSGraph &FDSG = LDS.getDSGraph(*I);
-        DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
-
-        // Keep track of the return node of the function's graph if it returns a
-        // value...
-        //
-        if (RetNode.getNode())
-          RetValMap[I] = RetNode;
+        ResultGraph->cloneInto(FDSG, ValMap, RetValMap, NodeMap);
       }
 
       // Incorporate the inlined Function's ScalarMap into the global
       // ScalarMap...
-      hash_map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
-      for (hash_map<Value*, DSNodeHandle>::iterator I = ValMap.begin(),
+      DSGraph::ScalarMapTy &GVM = ResultGraph->getScalarMap();
+      for (DSGraph::ScalarMapTy::iterator I = ValMap.begin(),
              E = ValMap.end(); I != E; ++I)
         GVM[I->first].mergeWith(I->second);
+
+      if ((++Count & 1) == 0)   // Prune nodes out every other time...
+        ResultGraph->removeTriviallyDeadNodes();
     }
 
   // FIXME: Must recalculate and use the Incomplete markers!!
@@ -171,18 +167,22 @@ bool Steens::run(Module &M) {
           ResolveFunctionCall(F, CurCall, RetValMap[F]);
           Eliminated = true;
         }
-      if (Eliminated)
-        CallTargets.erase(CallTargets.begin()+c);
-      else
+      if (Eliminated) {
+        CallTargets[c] = CallTargets.back();
+        CallTargets.pop_back();
+      } else
         ++c;  // Cannot eliminate this call, skip over it...
     }
 
-    if (CallTargets.empty())          // Eliminated all calls?
-      Calls.erase(Calls.begin()+i);   // Remove from call list...
-    else
+    if (CallTargets.empty()) {        // Eliminated all calls?
+      CurCall = Calls.back();         // Remove entry
+      Calls.pop_back();
+    } else
       ++i;                            // Skip this call site...
   }
 
+  RetValMap.clear();
+
   // Update the "incomplete" markers on the nodes, ignoring unknownness due to
   // incoming arguments...
   ResultGraph->maskIncompleteMarkers();
@@ -192,20 +192,22 @@ bool Steens::run(Module &M) {
   // FIXME: We should be able to disable the globals graph for steens!
   ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
 
-  //print(std::cerr, &M);
+  DEBUG(print(std::cerr, &M));
   return false;
 }
 
 // alias - This is the only method here that does anything interesting...
-AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) {
+AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
+                                         const Value *V2, unsigned V2Size) {
+  // FIXME: HANDLE Size argument!
   assert(ResultGraph && "Result graph has not been computed yet!");
 
-  hash_map<Value*, DSNodeHandle> &GSM = ResultGraph->getScalarMap();
+  DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
 
-  hash_map<Value*, DSNodeHandle>::iterator I = GSM.find(const_cast<Value*>(V1));
+  DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
   if (I != GSM.end() && I->second.getNode()) {
     DSNodeHandle &V1H = I->second;
-    hash_map<Value*, DSNodeHandle>::iterator J=GSM.find(const_cast<Value*>(V2));
+    DSGraph::ScalarMapTy::iterator J=GSM.find(const_cast<Value*>(V2));
     if (J != GSM.end() && J->second.getNode()) {
       DSNodeHandle &V2H = J->second;
       // If the two pointers point to different data structure graph nodes, they
@@ -224,5 +226,5 @@ AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) {
   // If we cannot determine alias properties based on our graph, fall back on
   // some other AA implementation.
   //
-  return getAnalysis<AliasAnalysis>().alias(V1, V2);
+  return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
 }