Move all of the DSA headers into the Analysis/DataStructure subdir.
[oota-llvm.git] / lib / Analysis / DataStructure / DataStructureAA.cpp
index 51df03306f775ad99428c91be904106a06eaec4c..3cff79526f70f7e06ff22503b3e80e9afb6290c1 100644 (file)
@@ -1,18 +1,27 @@
 //===- DataStructureAA.cpp - Data Structure Based 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 top-down data structure graphs to implement a simple
 // context sensitive alias analysis.
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Analysis/DataStructure.h"
-#include "llvm/Analysis/DSGraph.h"
-#include "llvm/Analysis/AliasAnalysis.h"
 #include "llvm/Module.h"
+#include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/Analysis/DataStructure/DataStructure.h"
+#include "llvm/Analysis/DataStructure/DSGraph.h"
+using namespace llvm;
 
 namespace {
   class DSAA : public Pass, public AliasAnalysis {
     TDDataStructures *TD;
+    BUDataStructures *BU;
   public:
     DSAA() : TD(0) {}
 
@@ -26,14 +35,15 @@ namespace {
     bool run(Module &M) {
       InitializeAliasAnalysis(this);
       TD = &getAnalysis<TDDataStructures>();
+      BU = &getAnalysis<BUDataStructures>();
       return false;
     }
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AliasAnalysis::getAnalysisUsage(AU);
-      AU.setPreservesAll();                    // Does not transform code...
-      AU.addRequired<TDDataStructures>();      // Uses TD Datastructures
-      AU.addRequired<AliasAnalysis>();         // Chains to another AA impl...
+      AU.setPreservesAll();                         // Does not transform code
+      AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures
+      AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures
     }
 
     //------------------------------------------------
@@ -45,6 +55,8 @@ namespace {
 
     void getMustAliases(Value *P, std::vector<Value*> &RetVals);
 
+    ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
+
   private:
     DSGraph *getGraphForValue(const Value *V);
   };
@@ -93,51 +105,79 @@ AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
 
   const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
   DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
-  if (I != GSM.end()) {
-    assert(I->second.getNode() && "Scalar map points to null node?");
-    DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
-    if (J != GSM.end()) {
-      assert(J->second.getNode() && "Scalar map points to null node?");
-
-      DSNode  *N1 = I->second.getNode(),  *N2 = J->second.getNode();
-      unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
+  if (I == GSM.end()) return NoAlias;
+
+  assert(I->second.getNode() && "Scalar map points to null node?");
+  DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
+  if (J == GSM.end()) return NoAlias;
+
+  assert(J->second.getNode() && "Scalar map points to null node?");
+
+  DSNode  *N1 = I->second.getNode(),  *N2 = J->second.getNode();
+  unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
         
-      // We can only make a judgment of one of the nodes is complete...
-      if (N1->isComplete() || N2->isComplete()) {
-        if (N1 != N2)
-          return NoAlias;   // Completely different nodes.
+  // We can only make a judgment of one of the nodes is complete...
+  if (N1->isComplete() || N2->isComplete()) {
+    if (N1 != N2)
+      return NoAlias;   // Completely different nodes.
 
 #if 0  // This does not correctly handle arrays!
-        // Both point to the same node and same offset, and there is only one
-        // physical memory object represented in the node, return must alias.
-        //
-        // FIXME: This isn't correct because we do not handle array indexing
-        // correctly.
-
-        if (O1 == O2 && isSinglePhysicalObject(N1))
-          return MustAlias; // Exactly the same object & offset
+    // Both point to the same node and same offset, and there is only one
+    // physical memory object represented in the node, return must alias.
+    //
+    // FIXME: This isn't correct because we do not handle array indexing
+    // correctly.
+
+    if (O1 == O2 && isSinglePhysicalObject(N1))
+      return MustAlias; // Exactly the same object & offset
 #endif
 
-        // See if they point to different offsets...  if so, we may be able to
-        // determine that they do not alias...
-        if (O1 != O2) {
-          if (O2 < O1) {    // Ensure that O1 <= O2
-            std::swap(V1, V2);
-            std::swap(O1, O2);
-            std::swap(V1Size, V2Size);
-          }
-
-          // FIXME: This is not correct because we do not handle array
-          // indexing correctly with this check!
-          //if (O1+V1Size <= O2) return NoAlias;
-        }
+    // See if they point to different offsets...  if so, we may be able to
+    // determine that they do not alias...
+    if (O1 != O2) {
+      if (O2 < O1) {    // Ensure that O1 <= O2
+        std::swap(V1, V2);
+        std::swap(O1, O2);
+        std::swap(V1Size, V2Size);
       }
+
+      // FIXME: This is not correct because we do not handle array
+      // indexing correctly with this check!
+      //if (O1+V1Size <= O2) return NoAlias;
     }
   }
 
   // FIXME: we could improve on this by checking the globals graph for aliased
   // global queries...
-  return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
+  return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
+}
+
+/// getModRefInfo - does a callsite modify or reference a value?
+///
+AliasAnalysis::ModRefResult
+DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
+  Function *F = CS.getCalledFunction();
+  if (!F) return pointsToConstantMemory(P) ? Ref : ModRef;
+  if (F->isExternal()) return ModRef;
+
+  // Clone the function TD graph, clearing off Mod/Ref flags
+  const Function *csParent = CS.getInstruction()->getParent()->getParent();
+  DSGraph TDGraph(TD->getDSGraph(*csParent));
+  TDGraph.maskNodeTypes(0);
+  
+  // Insert the callee's BU graph into the TD graph
+  const DSGraph &BUGraph = BU->getDSGraph(*F);
+  TDGraph.mergeInGraph(TDGraph.getDSCallSiteForCallSite(CS),
+                       *F, BUGraph, 0);
+
+  // Report the flags that have been added
+  const DSNodeHandle &DSH = TDGraph.getNodeForValue(P);
+  if (const DSNode *N = DSH.getNode())
+    if (N->isModified())
+      return N->isRead() ? ModRef : Mod;
+    else
+      return N->isRead() ? Ref : NoModRef;
+  return NoModRef;
 }
 
 
@@ -158,14 +198,14 @@ void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
     
     // The only must alias information we can currently determine occurs when
     // the node for P is a global node with only one entry.
-    const DSGraph::ScalarMapTy &GSM = G->getScalarMap();
-    DSGraph::ScalarMapTy::const_iterator I = GSM.find(P);
-    if (I != GSM.end()) {
+    DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
+    if (I != G->getScalarMap().end()) {
       DSNode *N = I->second.getNode();
       if (N->isComplete() && isSinglePhysicalObject(N))
         RetVals.push_back(N->getGlobals()[0]);
     }
   }
 #endif
-  return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals);
+  return AliasAnalysis::getMustAliases(P, RetVals);
 }
+