Clean up the use of static and anonymous namespaces. This turned up
[oota-llvm.git] / lib / Analysis / IPA / Andersens.cpp
index 49c6edd6207fcc91f6cb1fc47fd1b0eb622183dd..823a32f85fe4fed9993cb0a284a7635864c44abe 100644 (file)
@@ -71,6 +71,7 @@
 #include <algorithm>
 #include <set>
 #include <list>
+#include <map>
 #include <stack>
 #include <vector>
 #include <queue>
@@ -88,14 +89,14 @@ STATISTIC(NumNodes      , "Number of nodes");
 STATISTIC(NumUnified    , "Number of variables unified");
 STATISTIC(NumErased     , "Number of redundant constraints erased");
 
-namespace {
-  const unsigned SelfRep = (unsigned)-1;
-  const unsigned Unvisited = (unsigned)-1;
-  // Position of the function return node relative to the function node.
-  const unsigned CallReturnPos = 1;
-  // Position of the function call node relative to the function node.
-  const unsigned CallFirstArgPos = 2;
+static const unsigned SelfRep = (unsigned)-1;
+static const unsigned Unvisited = (unsigned)-1;
+// Position of the function return node relative to the function node.
+static const unsigned CallReturnPos = 1;
+// Position of the function call node relative to the function node.
+static const unsigned CallFirstArgPos = 2;
 
+namespace {
   struct BitmapKeyInfo {
     static inline SparseBitVector<> *getEmptyKey() {
       return reinterpret_cast<SparseBitVector<> *>(-1);
@@ -286,7 +287,7 @@ namespace {
         Timestamp = Counter++;
       }
 
-      bool isRep() {
+      bool isRep() const {
         return( (int) NodeRep < 0 );
       }
     };
@@ -432,9 +433,6 @@ namespace {
     static char ID;
     Andersens() : ModulePass((intptr_t)&ID) {}
 
-    /// isAnalysis - Return true if this pass is  implementing an analysis pass.
-    virtual bool isAnalysis() const { return true; }
-
     bool runOnModule(Module &M) {
       InitializeAliasAnalysis(this);
       IdentifyObjects(M);
@@ -449,10 +447,11 @@ namespace {
 
       // Free the constraints list, as we don't need it to respond to alias
       // requests.
-      ObjectNodes.clear();
-      ReturnNodes.clear();
-      VarargNodes.clear();
       std::vector<Constraint>().swap(Constraints);
+      //These are needed for Print() (-analyze in opt)
+      //ObjectNodes.clear();
+      //ReturnNodes.clear();
+      //VarargNodes.clear();
       return false;
     }
 
@@ -513,7 +512,7 @@ namespace {
 
     /// getObject - Return the node corresponding to the memory object for the
     /// specified global or allocation instruction.
-    unsigned getObject(Value *V) {
+    unsigned getObject(Value *V) const {
       DenseMap<Value*, unsigned>::iterator I = ObjectNodes.find(V);
       assert(I != ObjectNodes.end() &&
              "Value does not have an object in the points-to graph!");
@@ -522,7 +521,7 @@ namespace {
 
     /// getReturnNode - Return the node representing the return value for the
     /// specified function.
-    unsigned getReturnNode(Function *F) {
+    unsigned getReturnNode(Function *F) const {
       DenseMap<Function*, unsigned>::iterator I = ReturnNodes.find(F);
       assert(I != ReturnNodes.end() && "Function does not return a value!");
       return I->second;
@@ -530,7 +529,7 @@ namespace {
 
     /// getVarargNode - Return the node representing the variable arguments
     /// formal for the specified function.
-    unsigned getVarargNode(Function *F) {
+    unsigned getVarargNode(Function *F) const {
       DenseMap<Function*, unsigned>::iterator I = VarargNodes.find(F);
       assert(I != VarargNodes.end() && "Function does not take var args!");
       return I->second;
@@ -547,6 +546,7 @@ namespace {
     unsigned UniteNodes(unsigned First, unsigned Second,
                         bool UnionByRank = true);
     unsigned FindNode(unsigned Node);
+    unsigned FindNode(unsigned Node) const;
 
     void IdentifyObjects(Module &M);
     void CollectConstraints(Module &M);
@@ -575,11 +575,11 @@ namespace {
     bool AddConstraintsForExternalCall(CallSite CS, Function *F);
 
 
-    void PrintNode(Node *N);
-    void PrintConstraints();
-    void PrintConstraint(const Constraint &);
-    void PrintLabels();
-    void PrintPointsToGraph();
+    void PrintNode(const Node *N) const;
+    void PrintConstraints() const ;
+    void PrintConstraint(const Constraint &) const;
+    void PrintLabels() const;
+    void PrintPointsToGraph() const;
 
     //===------------------------------------------------------------------===//
     // Instruction visitation methods for adding constraints
@@ -601,16 +601,22 @@ namespace {
     void visitVAArg(VAArgInst &I);
     void visitInstruction(Instruction &I);
 
+    //===------------------------------------------------------------------===//
+    // Implement Analyize interface
+    //
+    void print(std::ostream &O, const Module* M) const {
+      PrintPointsToGraph();
+    }
   };
+}
 
-  char Andersens::ID = 0;
-  RegisterPass<Andersens> X("anders-aa",
-                            "Andersen's Interprocedural Alias Analysis");
-  RegisterAnalysisGroup<AliasAnalysis> Y(X);
+char Andersens::ID = 0;
+static RegisterPass<Andersens>
+X("anders-aa", "Andersen's Interprocedural Alias Analysis", false, true);
+static RegisterAnalysisGroup<AliasAnalysis> Y(X);
 
-  // Initialize Timestamp Counter (static).
-  unsigned Andersens::Node::Counter = 0;
-}
+// Initialize Timestamp Counter (static).
+unsigned Andersens::Node::Counter = 0;
 
 ModulePass *llvm::createAndersensPass() { return new Andersens(); }
 
@@ -950,6 +956,9 @@ bool Andersens::AddConstraintsForExternalCall(CallSite CS, Function *F) {
                                      FirstArg, TempArg));
     Constraints.push_back(Constraint(Constraint::Load,
                                      TempArg, SecondArg));
+    // In addition, Dest = Src
+    Constraints.push_back(Constraint(Constraint::Copy,
+                                     FirstArg, SecondArg));
     return true;
   }
 
@@ -1996,7 +2005,7 @@ unsigned Andersens::FindEquivalentNode(unsigned NodeIndex,
   return NodeIndex;
 }
 
-void Andersens::PrintLabels() {
+void Andersens::PrintLabels() const {
   for (unsigned i = 0; i < GraphNodes.size(); ++i) {
     if (i < FirstRefNode) {
       PrintNode(&GraphNodes[i]);
@@ -2738,11 +2747,23 @@ unsigned Andersens::FindNode(unsigned NodeIndex) {
     return (N->NodeRep = FindNode(N->NodeRep));
 }
 
+// Find the index into GraphNodes of the node representing Node, 
+// don't perform path compression along the way (for Print)
+unsigned Andersens::FindNode(unsigned NodeIndex) const {
+  assert (NodeIndex < GraphNodes.size()
+          && "Attempting to find a node that can't exist");
+  const Node *N = &GraphNodes[NodeIndex];
+  if (N->isRep())
+    return NodeIndex;
+  else
+    return FindNode(N->NodeRep);
+}
+
 //===----------------------------------------------------------------------===//
 //                               Debugging Output
 //===----------------------------------------------------------------------===//
 
-void Andersens::PrintNode(Node *N) {
+void Andersens::PrintNode(const Node *N) const {
   if (N == &GraphNodes[UniversalSet]) {
     cerr << "<universal>";
     return;
@@ -2786,7 +2807,7 @@ void Andersens::PrintNode(Node *N) {
     if (N == &GraphNodes[getObject(V)])
       cerr << "<mem>";
 }
-void Andersens::PrintConstraint(const Constraint &C) {
+void Andersens::PrintConstraint(const Constraint &C) const {
   if (C.Type == Constraint::Store) {
     cerr << "*";
     if (C.Offset != 0)
@@ -2811,18 +2832,18 @@ void Andersens::PrintConstraint(const Constraint &C) {
   cerr << "\n";
 }
 
-void Andersens::PrintConstraints() {
+void Andersens::PrintConstraints() const {
   cerr << "Constraints:\n";
 
   for (unsigned i = 0, e = Constraints.size(); i != e; ++i)
     PrintConstraint(Constraints[i]);
 }
 
-void Andersens::PrintPointsToGraph() {
+void Andersens::PrintPointsToGraph() const {
   cerr << "Points-to graph:\n";
   for (unsigned i = 0, e = GraphNodes.size(); i != e; ++i) {
-    Node *N = &GraphNodes[i];
-    if (FindNode (i) != i) {
+    const Node *N = &GraphNodes[i];
+    if (FindNode(i) != i) {
       PrintNode(N);
       cerr << "\t--> same as ";
       PrintNode(&GraphNodes[FindNode(i)]);