[llvm-commits] [poolalloc] r74848 - in /poolalloc/trunk/lib/DSA: Steensgaard.cpp SteensgaardAA.cpp

Haohui Mai mai4 at uiuc.edu
Mon Jul 6 12:32:56 PDT 2009


Author: mai4
Date: Mon Jul  6 14:32:55 2009
New Revision: 74848

URL: http://llvm.org/viewvc/llvm-project?rev=74848&view=rev
Log:
Export the DSGraph interface of steensgaard alais analysis.


Added:
    poolalloc/trunk/lib/DSA/SteensgaardAA.cpp
Modified:
    poolalloc/trunk/lib/DSA/Steensgaard.cpp

Modified: poolalloc/trunk/lib/DSA/Steensgaard.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/DSA/Steensgaard.cpp?rev=74848&r1=74847&r2=74848&view=diff

==============================================================================
--- poolalloc/trunk/lib/DSA/Steensgaard.cpp (original)
+++ poolalloc/trunk/lib/DSA/Steensgaard.cpp Mon Jul  6 14:32:55 2009
@@ -1,4 +1,4 @@
-//===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
+//===- Steensgaard.cpp - Context Insensitive Data Structure Analysis ------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -7,10 +7,9 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This pass uses the data structure graphs to implement a simple context
-// insensitive alias analysis.  It does this by computing the local analysis
-// graphs for all of the functions, then merging them together into a single big
-// graph without cloning.
+// This pass computes a context-insensitive data analysis graph.  It does this
+// by computing the local analysis graphs for all of the functions, then merging
+// them together into a single big graph without cloning.
 //
 //===----------------------------------------------------------------------===//
 
@@ -21,116 +20,57 @@
 #include "llvm/Module.h"
 #include "llvm/Support/Debug.h"
 #include <ostream>
-using namespace llvm;
-
-namespace {
-  class Steens : public ModulePass, public AliasAnalysis {
-    DSGraph *ResultGraph;
-
-    EquivalenceClasses<const GlobalValue*> GlobalECs;  // Always empty
-  public:
-    static char ID;
-    Steens() : ModulePass((intptr_t)&ID), ResultGraph(0) {}
-    ~Steens() {
-      releaseMyMemory();
-      assert(ResultGraph == 0 && "releaseMemory not called?");
-    }
-
-    //------------------------------------------------
-    // Implement the Pass API
-    //
-
-    // run - Build up the result graph, representing the pointer graph for the
-    // program.
-    //
-    bool runOnModule(Module &M);
-
-    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
-    }
-
-    // print - Implement the Pass::print method...
-    void print(OStream O, const Module *M) const {
-      if (O.stream()) print(*O.stream(), M);
-    }
-    void print(std::ostream &O, const Module *M) const {
-      assert(ResultGraph && "Result graph has not yet been computed!");
-      ResultGraph->writeGraphToFile(O, "steensgaards");
-    }
 
-    //------------------------------------------------
-    // Implement the AliasAnalysis API
-    //
-
-    AliasResult alias(const Value *V1, unsigned V1Size,
-                      const Value *V2, unsigned V2Size);
-
-    virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
-    virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
-
-  private:
-    void ResolveFunctionCall(const Function *F, const DSCallSite &Call,
-                             DSNodeHandle &RetVal);
-  };
-
-  // Register the pass...
-  RegisterPass<Steens> X("steens-aa",
-                         "Steensgaard's alias analysis (DSGraph based)");
+using namespace llvm;
 
-  // Register as an implementation of AliasAnalysis
-  RegisterAnalysisGroup<AliasAnalysis> Y(X);
+SteensgaardDataStructures::~SteensgaardDataStructures() {
+  releaseMyMemory();
+  assert(ResultGraph == 0 && "releaseMemory not called?");
 }
 
-char Steens::ID;
-
-ModulePass *llvm::createSteensgaardPass() { return new Steens(); }
-
-/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
-/// with the specified call site descriptor.  This function links the arguments
-/// and the return value for the call site context-insensitively.
-///
-void Steens::ResolveFunctionCall(const Function *F, const DSCallSite &Call,
-                                 DSNodeHandle &RetVal) {
-  assert(ResultGraph != 0 && "Result graph not allocated!");
-  DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
-
-  // Handle the return value of the function...
-  if (Call.getRetVal().getNode() && RetVal.getNode())
-    RetVal.mergeWith(Call.getRetVal());
+void
+SteensgaardDataStructures::releaseMyMemory() {
+  delete ResultGraph;
+  ResultGraph = 0;
+}
 
-  // Loop over all pointer arguments, resolving them to their provided pointers
-  unsigned PtrArgIdx = 0;
-  for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
-       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++));
-  }
+// print - Implement the Pass::print method...
+void
+SteensgaardDataStructures::print(OStream O, const Module *M) const {
+  if (O.stream()) print(*O.stream(), M);
 }
 
+void
+SteensgaardDataStructures::print(std::ostream &O, const Module *M) const {
+  assert(ResultGraph && "Result graph has not yet been computed!");
+  ResultGraph->writeGraphToFile(O, "steensgaards");
+}
 
 /// run - Build up the result graph, representing the pointer graph for the
 /// program.
 ///
-bool Steens::runOnModule(Module &M) {
-  InitializeAliasAnalysis(this);
+bool
+SteensgaardDataStructures::runOnModule(Module &M) {
+  DS = &getAnalysis<StdLibDataStructures>();
+  return runOnModuleInternal(M);
+}
+
+bool
+SteensgaardDataStructures::runOnModuleInternal(Module &M) {
   assert(ResultGraph == 0 && "Result graph already allocated!");
-  LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
 
   // Create a new, empty, graph...
   ResultGraph = new DSGraph(GlobalECs, getTargetData());
-  ResultGraph->spliceFrom(LDS.getGlobalsGraph());
+  ResultGraph->spliceFrom(DS->getGlobalsGraph());
 
   // Loop over the rest of the module, merging graphs for non-external functions
   // into this graph.
   //
-  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
-    if (!I->isDeclaration())
-      ResultGraph->spliceFrom(LDS.getDSGraph(*I));
+  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
+    if (!I->isDeclaration()) {
+      ResultGraph->spliceFrom(DS->getDSGraph(*I));
+    }
+  }
 
   ResultGraph->removeTriviallyDeadNodes();
 
@@ -139,11 +79,9 @@
   // Now that we have all of the graphs inlined, we can go about eliminating
   // call nodes...
   //
-  std::list<DSCallSite> &Calls = ResultGraph->getAuxFunctionCalls();
-  assert(Calls.empty() && "Aux call list is already in use??");
 
   // Start with a copy of the original call sites.
-  Calls = ResultGraph->getFunctionCalls();
+  std::list<DSCallSite> & Calls = ResultGraph->getFunctionCalls();
 
   for (std::list<DSCallSite>::iterator CI = Calls.begin(), E = Calls.end();
        CI != E;) {
@@ -187,95 +125,47 @@
   // Update the "incomplete" markers on the nodes, ignoring unknownness due to
   // incoming arguments...
   ResultGraph->maskIncompleteMarkers();
-  ResultGraph->markIncompleteNodes(DSGraph::IgnoreGlobals |
-                                   DSGraph::MarkFormalArgs);
+
+  ResultGraph->markIncompleteNodes(DSGraph::MarkFormalArgs | DSGraph::IgnoreGlobals);
 
   // Remove any nodes that are dead after all of the merging we have done...
   // FIXME: We should be able to disable the globals graph for steens!
-  //ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
+  
+  ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
 
   print(DOUT, &M);
   return false;
 }
 
-AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
-                                         const Value *V2, unsigned V2Size) {
-  assert(ResultGraph && "Result graph has not been computed yet!");
-
-  DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
-
-  DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
-  DSGraph::ScalarMapTy::iterator J = GSM.find(const_cast<Value*>(V2));
-  if (I != GSM.end() && !I->second.isNull() &&
-      J != GSM.end() && !J->second.isNull()) {
-    DSNodeHandle &V1H = I->second;
-    DSNodeHandle &V2H = J->second;
-
-    // If at least one of the nodes is complete, we can say something about
-    // this.  If one is complete and the other isn't, then they are obviously
-    // different nodes.  If they are both complete, we can't say anything
-    // useful.
-    if (I->second.getNode()->isCompleteNode() ||
-        J->second.getNode()->isCompleteNode()) {
-      // If the two pointers point to different data structure graph nodes, they
-      // cannot alias!
-      if (V1H.getNode() != V2H.getNode())
-        return NoAlias;
-
-      // See if they point to different offsets...  if so, we may be able to
-      // determine that they do not alias...
-      unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
-      if (O1 != O2) {
-        if (O2 < O1) {    // Ensure that O1 <= O2
-          std::swap(V1, V2);
-          std::swap(O1, O2);
-          std::swap(V1Size, V2Size);
-        }
-
-        if (O1+V1Size <= O2)
-          return NoAlias;
-      }
-    }
-  }
+/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
+/// with the specified call site descriptor.  This function links the arguments
+/// and the return value for the call site context-insensitively.
+///
+void
+SteensgaardDataStructures::ResolveFunctionCall(const Function *F, 
+                                                const DSCallSite &Call,
+                                                DSNodeHandle &RetVal) {
 
-  // If we cannot determine alias properties based on our graph, fall back on
-  // some other AA implementation.
-  //
-  return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
-}
+  assert(ResultGraph != 0 && "Result graph not allocated!");
+  DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
 
-AliasAnalysis::ModRefResult
-Steens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
-  AliasAnalysis::ModRefResult Result = ModRef;
-
-  // Find the node in question.
-  DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
-  DSGraph::ScalarMapTy::iterator I = GSM.find(P);
-
-  if (I != GSM.end() && !I->second.isNull()) {
-    DSNode *N = I->second.getNode();
-    if (N->isCompleteNode()) {
-      // If this is a direct call to an external function, and if the pointer
-      // points to a complete node, the external function cannot modify or read
-      // the value (we know it's not passed out of the program!).
-      if (Function *F = CS.getCalledFunction())
-        if (F->isDeclaration())
-          return NoModRef;
-
-      // Otherwise, if the node is complete, but it is only M or R, return this.
-      // This can be useful for globals that should be marked const but are not.
-      if (!N->isModifiedNode())
-        Result = (ModRefResult)(Result & ~Mod);
-      if (!N->isReadNode())
-        Result = (ModRefResult)(Result & ~Ref);
-    }
-  }
+  // Handle the return value of the function...
+  if (Call.getRetVal().getNode() && RetVal.getNode())
+    RetVal.mergeWith(Call.getRetVal());
 
-  return (ModRefResult)(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
+  // Loop over all pointer arguments, resolving them to their provided pointers
+  unsigned PtrArgIdx = 0;
+  for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
+       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++));
+  }
 }
 
-AliasAnalysis::ModRefResult 
-Steens::getModRefInfo(CallSite CS1, CallSite CS2)
-{
-  return AliasAnalysis::getModRefInfo(CS1,CS2);
-}
+char SteensgaardDataStructures::ID = 0;
+
+// Register the pass...
+static RegisterPass<SteensgaardDataStructures> X
+("dsa-steens",
+ "Context-insensitive Data Structure Analysis");

Added: poolalloc/trunk/lib/DSA/SteensgaardAA.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/DSA/SteensgaardAA.cpp?rev=74848&view=auto

==============================================================================
--- poolalloc/trunk/lib/DSA/SteensgaardAA.cpp (added)
+++ poolalloc/trunk/lib/DSA/SteensgaardAA.cpp Mon Jul  6 14:32:55 2009
@@ -0,0 +1,162 @@
+//===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass uses the data structure graphs to implement a simple context
+// insensitive alias analysis.  It does this by computing the local analysis
+// graphs for all of the functions, then merging them together into a single big
+// graph without cloning.
+//
+//===----------------------------------------------------------------------===//
+
+#include "dsa/DataStructure.h"
+#include "dsa/DSGraph.h"
+#include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/Analysis/Passes.h"
+#include "llvm/Module.h"
+#include "llvm/Support/Debug.h"
+#include <ostream>
+using namespace llvm;
+
+namespace {
+  class Steens : public ModulePass, public AliasAnalysis {
+    DSGraph * ResultGraph;
+  public:
+    static char ID;
+    Steens() : ModulePass((intptr_t)&ID), ResultGraph(NULL) {}
+    ~Steens() {    }
+
+    //------------------------------------------------
+    // Implement the Pass API
+    //
+
+    // run - Build up the result graph, representing the pointer graph for the
+    // program.
+    //
+    bool runOnModule(Module &M);
+
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AliasAnalysis::getAnalysisUsage(AU);
+      AU.setPreservesAll();                    // Does not transform code...
+      AU.addRequired<SteensgaardDataStructures>();   // Uses steensgaard dsgraph
+    }
+
+    //------------------------------------------------
+    // Implement the AliasAnalysis API
+    //
+
+    AliasResult alias(const Value *V1, unsigned V1Size,
+                      const Value *V2, unsigned V2Size);
+
+    virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
+    virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
+
+  };
+
+  // Register the pass...
+  RegisterPass<Steens> X("steens-aa",
+                         "Steensgaard's alias analysis (DSGraph based)");
+
+  // Register as an implementation of AliasAnalysis
+  RegisterAnalysisGroup<AliasAnalysis> Y(X);
+}
+
+char Steens::ID;
+
+ModulePass *llvm::createSteensgaardPass() { return new Steens(); }
+
+/// run - Build up the result graph, representing the pointer graph for the
+/// program.
+///
+bool Steens::runOnModule(Module &M) {
+  InitializeAliasAnalysis(this);
+  ResultGraph = getAnalysis<SteensgaardDataStructures>().getResultGraph();
+  return false;
+}
+
+AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
+                                         const Value *V2, unsigned V2Size) {
+  assert(ResultGraph && "Result graph has not been computed yet!");
+
+  DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
+
+  DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
+  DSGraph::ScalarMapTy::iterator J = GSM.find(const_cast<Value*>(V2));
+  if (I != GSM.end() && !I->second.isNull() &&
+      J != GSM.end() && !J->second.isNull()) {
+    DSNodeHandle &V1H = I->second;
+    DSNodeHandle &V2H = J->second;
+
+    // If at least one of the nodes is complete, we can say something about
+    // this.  If one is complete and the other isn't, then they are obviously
+    // different nodes.  If they are both complete, we can't say anything
+    // useful.
+    if (I->second.getNode()->isCompleteNode() ||
+        J->second.getNode()->isCompleteNode()) {
+      // If the two pointers point to different data structure graph nodes, they
+      // cannot alias!
+      if (V1H.getNode() != V2H.getNode())
+        return NoAlias;
+
+      // See if they point to different offsets...  if so, we may be able to
+      // determine that they do not alias...
+      unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
+      if (O1 != O2) {
+        if (O2 < O1) {    // Ensure that O1 <= O2
+          std::swap(V1, V2);
+          std::swap(O1, O2);
+          std::swap(V1Size, V2Size);
+        }
+
+        if (O1+V1Size <= O2)
+          return NoAlias;
+      }
+    }
+  }
+
+  // If we cannot determine alias properties based on our graph, fall back on
+  // some other AA implementation.
+  //
+  return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
+}
+
+AliasAnalysis::ModRefResult
+Steens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
+  AliasAnalysis::ModRefResult Result = ModRef;
+
+  // Find the node in question.
+  DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
+  DSGraph::ScalarMapTy::iterator I = GSM.find(P);
+
+  if (I != GSM.end() && !I->second.isNull()) {
+    DSNode *N = I->second.getNode();
+    if (N->isCompleteNode()) {
+      // If this is a direct call to an external function, and if the pointer
+      // points to a complete node, the external function cannot modify or read
+      // the value (we know it's not passed out of the program!).
+      if (Function *F = CS.getCalledFunction())
+        if (F->isDeclaration())
+          return NoModRef;
+
+      // Otherwise, if the node is complete, but it is only M or R, return this.
+      // This can be useful for globals that should be marked const but are not.
+      if (!N->isModifiedNode())
+        Result = (ModRefResult)(Result & ~Mod);
+      if (!N->isReadNode())
+        Result = (ModRefResult)(Result & ~Ref);
+    }
+  }
+
+  return (ModRefResult)(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
+}
+
+AliasAnalysis::ModRefResult 
+Steens::getModRefInfo(CallSite CS1, CallSite CS2)
+{
+  return AliasAnalysis::getModRefInfo(CS1,CS2);
+}





More information about the llvm-commits mailing list