For PR950:
[oota-llvm.git] / lib / Analysis / IPA / Andersens.cpp
index 9ed21e06908f8ba15175eebc2cc214d31f858a7b..805e771e542531f2f8b9b586335b47135e65ba06 100644 (file)
@@ -1,10 +1,10 @@
 //===- Andersens.cpp - Andersen's Interprocedural 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 file defines a very simple implementation of Andersen's interprocedural
@@ -43,8 +43,8 @@
 //
 // Future Improvements:
 //   This implementation of Andersen's algorithm is extremely slow.  To make it
-//   scale reasonably well, the inclusion constraints could be sorted (easy), 
-//   offline variable substitution would be a huge win (straight-forward), and 
+//   scale reasonably well, the inclusion constraints could be sorted (easy),
+//   offline variable substitution would be a huge win (straight-forward), and
 //   online cycle elimination (trickier) might help as well.
 //
 //===----------------------------------------------------------------------===//
 #include <set>
 using namespace llvm;
 
-namespace {
-  Statistic<>
-  NumIters("anders-aa", "Number of iterations to reach convergence");
-  Statistic<>
-  NumConstraints("anders-aa", "Number of constraints");
-  Statistic<>
-  NumNodes("anders-aa", "Number of nodes");
-  Statistic<>
-  NumEscapingFunctions("anders-aa", "Number of internal functions that escape");
-  Statistic<>
-  NumIndirectCallees("anders-aa", "Number of indirect callees found");
+STATISTIC(NumIters            , "Number of iterations to reach convergence");
+STATISTIC(NumConstraints      , "Number of constraints");
+STATISTIC(NumNodes            , "Number of nodes");
+STATISTIC(NumEscapingFunctions, "Number of internal functions that escape");
+STATISTIC(NumIndirectCallees  , "Number of indirect callees found");
 
+namespace {
   class Andersens : public ModulePass, public AliasAnalysis,
                     private InstVisitor<Andersens> {
     /// Node class - This class is used to represent a memory object in the
@@ -137,7 +132,7 @@ namespace {
     std::map<Value*, unsigned> ValueNodes;
 
     /// ObjectNodes - This map contains entries for each memory object in the
-    /// program: globals, alloca's and mallocs.  
+    /// program: globals, alloca's and mallocs.
     std::map<Value*, unsigned> ObjectNodes;
 
     /// ReturnNodes - This map contains an entry for each function in the
@@ -161,7 +156,7 @@ namespace {
       Constraint(ConstraintType Ty, Node *D, Node *S)
         : Type(Ty), Dest(D), Src(S) {}
     };
-    
+
     /// Constraints - This vector contains a list of all of the constraints
     /// identified by the program.
     std::vector<Constraint> Constraints;
@@ -191,9 +186,9 @@ namespace {
     enum {
       UniversalSet = 0,
       NullPtr      = 1,
-      NullObject   = 2,
+      NullObject   = 2
     };
-    
+
   public:
     bool runOnModule(Module &M) {
       InitializeAliasAnalysis(this);
@@ -209,7 +204,7 @@ namespace {
       ReturnNodes.clear();
       VarargNodes.clear();
       EscapingInternalFunctions.clear();
-      std::vector<Constraint>().swap(Constraints);      
+      std::vector<Constraint>().swap(Constraints);
       return false;
     }
 
@@ -232,10 +227,11 @@ namespace {
 
     //------------------------------------------------
     // Implement the AliasAnalysis API
-    //  
+    //
     AliasResult alias(const Value *V1, unsigned V1Size,
                       const Value *V2, unsigned V2Size);
-    ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
+    virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
+    virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
     void getMustAliases(Value *P, std::vector<Value*> &RetVals);
     bool pointsToConstantMemory(const Value *P);
 
@@ -259,13 +255,14 @@ namespace {
 
       std::map<Value*, unsigned>::iterator I = ValueNodes.find(V);
       if (I == ValueNodes.end()) {
+#ifndef NDEBUG
         V->dump();
-        assert(I != ValueNodes.end() &&
-               "Value does not have a node in the points-to graph!");
+#endif
+        assert(0 && "Value does not have a node in the points-to graph!");
       }
       return &GraphNodes[I->second];
     }
-    
+
     /// getObject - Return the node corresponding to the memory object for the
     /// specified global or allocation instruction.
     Node *getObject(Value *V) {
@@ -328,16 +325,16 @@ namespace {
     void visitGetElementPtrInst(GetElementPtrInst &GEP);
     void visitPHINode(PHINode &PN);
     void visitCastInst(CastInst &CI);
-    void visitSetCondInst(SetCondInst &SCI) {} // NOOP!
+    void visitICmpInst(ICmpInst &ICI) {} // NOOP!
+    void visitFCmpInst(FCmpInst &ICI) {} // NOOP!
     void visitSelectInst(SelectInst &SI);
-    void visitVANext(VANextInst &I);
     void visitVAArg(VAArgInst &I);
     void visitInstruction(Instruction &I);
   };
 
-  RegisterOpt<Andersens> X("anders-aa",
-                           "Andersen's Interprocedural Alias Analysis");
-  RegisterAnalysisGroup<AliasAnalysis, Andersens> Y;
+  RegisterPass<Andersens> X("anders-aa",
+                            "Andersen's Interprocedural Alias Analysis");
+  RegisterAnalysisGroup<AliasAnalysis> Y(X);
 }
 
 ModulePass *llvm::createAndersensPass() { return new Andersens(); }
@@ -372,27 +369,24 @@ Andersens::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
   if (Function *F = CS.getCalledFunction())
     if (F->isExternal()) {
       Node *N1 = getNode(P);
-      bool PointsToUniversalSet = false;
-
-      for (Node::iterator NI = N1->begin(), E = N1->end(); NI != E; ++NI) {
-        Node *PN = *NI;
-        if (PN->begin() == PN->end())
-          continue;  // P doesn't point to anything.
-        // Get the first pointee.
-        Node *FirstPointee = *PN->begin();
-        if (FirstPointee == &GraphNodes[UniversalSet]) {
-          PointsToUniversalSet = true;
-          break;
-        }
-      }
 
-      if (!PointsToUniversalSet)
+      if (N1->begin() == N1->end())
+        return NoModRef;  // P doesn't point to anything.
+
+      // Get the first pointee.
+      Node *FirstPointee = *N1->begin();
+      if (FirstPointee != &GraphNodes[UniversalSet])
         return NoModRef;  // P doesn't point to the universal set.
     }
 
   return AliasAnalysis::getModRefInfo(CS, P, Size);
 }
 
+AliasAnalysis::ModRefResult
+Andersens::getModRefInfo(CallSite CS1, CallSite CS2) {
+  return AliasAnalysis::getModRefInfo(CS1,CS2);
+}
+
 /// getMustAlias - We can provide must alias information if we know that a
 /// pointer can only point to a specific function or the null pointer.
 /// Unfortunately we cannot determine must-alias information for global
@@ -422,7 +416,7 @@ void Andersens::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
       }
     }
   }
-  
+
   AliasAnalysis::getMustAliases(P, RetVals);
 }
 
@@ -530,13 +524,12 @@ Andersens::Node *Andersens::getNodeForConstantPointer(Constant *C) {
     switch (CE->getOpcode()) {
     case Instruction::GetElementPtr:
       return getNodeForConstantPointer(CE->getOperand(0));
-    case Instruction::Cast:
-      if (isa<PointerType>(CE->getOperand(0)->getType()))
-        return getNodeForConstantPointer(CE->getOperand(0));
-      else
-        return &GraphNodes[UniversalSet];
+    case Instruction::IntToPtr:
+      return &GraphNodes[UniversalSet];
+    case Instruction::BitCast:
+      return getNodeForConstantPointer(CE->getOperand(0));
     default:
-      std::cerr << "Constant Expr not yet handled: " << *CE << "\n";
+      cerr << "Constant Expr not yet handled: " << *CE << "\n";
       assert(0);
     }
   } else {
@@ -558,13 +551,12 @@ Andersens::Node *Andersens::getNodeForConstantPointerTarget(Constant *C) {
     switch (CE->getOpcode()) {
     case Instruction::GetElementPtr:
       return getNodeForConstantPointerTarget(CE->getOperand(0));
-    case Instruction::Cast:
-      if (isa<PointerType>(CE->getOperand(0)->getType()))
-        return getNodeForConstantPointerTarget(CE->getOperand(0));
-      else
-        return &GraphNodes[UniversalSet];
+    case Instruction::IntToPtr:
+      return &GraphNodes[UniversalSet];
+    case Instruction::BitCast:
+      return getNodeForConstantPointerTarget(CE->getOperand(0));
     default:
-      std::cerr << "Constant Expr not yet handled: " << *CE << "\n";
+      cerr << "Constant Expr not yet handled: " << *CE << "\n";
       assert(0);
     }
   } else {
@@ -579,7 +571,7 @@ void Andersens::AddGlobalInitializerConstraints(Node *N, Constant *C) {
   if (C->getType()->isFirstClassType()) {
     if (isa<PointerType>(C->getType()))
       N->copyFrom(getNodeForConstantPointer(C));
-                                       
+
   } else if (C->isNullValue()) {
     N->addPointerTo(&GraphNodes[NullObject]);
     return;
@@ -610,21 +602,51 @@ bool Andersens::AddConstraintsForExternalCall(CallSite CS, Function *F) {
   assert(F->isExternal() && "Not an external function!");
 
   // These functions don't induce any points-to constraints.
-  if (F->getName() == "printf" || F->getName() == "fprintf" ||
-      F->getName() == "sprintf" ||
-      F->getName() == "fgets" || F->getName() == "__assert_fail" ||
-      F->getName() == "open" || F->getName() == "fopen" ||
-      F->getName() == "fclose" || F->getName() == "fflush" ||
-      F->getName() == "rewind" ||
-      F->getName() == "atoi" || F->getName() == "atol" ||
-      F->getName() == "unlink" ||
-      F->getName() == "sscanf" || F->getName() == "fscanf" ||
-      F->getName() == "llvm.memset" || F->getName() == "memcmp" ||
-      F->getName() == "read" || F->getName() == "write")
+  if (F->getName() == "atoi" || F->getName() == "atof" ||
+      F->getName() == "atol" || F->getName() == "atoll" ||
+      F->getName() == "remove" || F->getName() == "unlink" ||
+      F->getName() == "rename" || F->getName() == "memcmp" ||
+      F->getName() == "llvm.memset.i32" ||
+      F->getName() == "llvm.memset.i64" ||
+      F->getName() == "strcmp" || F->getName() == "strncmp" ||
+      F->getName() == "execl" || F->getName() == "execlp" ||
+      F->getName() == "execle" || F->getName() == "execv" ||
+      F->getName() == "execvp" || F->getName() == "chmod" ||
+      F->getName() == "puts" || F->getName() == "write" ||
+      F->getName() == "open" || F->getName() == "create" ||
+      F->getName() == "truncate" || F->getName() == "chdir" ||
+      F->getName() == "mkdir" || F->getName() == "rmdir" ||
+      F->getName() == "read" || F->getName() == "pipe" ||
+      F->getName() == "wait" || F->getName() == "time" ||
+      F->getName() == "stat" || F->getName() == "fstat" ||
+      F->getName() == "lstat" || F->getName() == "strtod" ||
+      F->getName() == "strtof" || F->getName() == "strtold" ||
+      F->getName() == "fopen" || F->getName() == "fdopen" ||
+      F->getName() == "freopen" ||
+      F->getName() == "fflush" || F->getName() == "feof" ||
+      F->getName() == "fileno" || F->getName() == "clearerr" ||
+      F->getName() == "rewind" || F->getName() == "ftell" ||
+      F->getName() == "ferror" || F->getName() == "fgetc" ||
+      F->getName() == "fgetc" || F->getName() == "_IO_getc" ||
+      F->getName() == "fwrite" || F->getName() == "fread" ||
+      F->getName() == "fgets" || F->getName() == "ungetc" ||
+      F->getName() == "fputc" ||
+      F->getName() == "fputs" || F->getName() == "putc" ||
+      F->getName() == "ftell" || F->getName() == "rewind" ||
+      F->getName() == "_IO_putc" || F->getName() == "fseek" ||
+      F->getName() == "fgetpos" || F->getName() == "fsetpos" ||
+      F->getName() == "printf" || F->getName() == "fprintf" ||
+      F->getName() == "sprintf" || F->getName() == "vprintf" ||
+      F->getName() == "vfprintf" || F->getName() == "vsprintf" ||
+      F->getName() == "scanf" || F->getName() == "fscanf" ||
+      F->getName() == "sscanf" || F->getName() == "__assert_fail" ||
+      F->getName() == "modf")
     return true;
 
+
   // These functions do induce points-to edges.
-  if (F->getName() == "llvm.memcpy" || F->getName() == "llvm.memmove" ||
+  if (F->getName() == "llvm.memcpy.i32" || F->getName() == "llvm.memcpy.i64" || 
+      F->getName() == "llvm.memmove.i32" ||F->getName() == "llvm.memmove.i64" ||
       F->getName() == "memmove") {
     // Note: this is a poor approximation, this says Dest = Src, instead of
     // *Dest = *Src.
@@ -682,7 +704,7 @@ void Andersens::CollectConstraints(Module &M) {
                                        &GraphNodes[UniversalSet]));
     }
   }
-  
+
   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
     // Make the function address point to the function object.
     getNodeValue(*F)->addPointerTo(getObject(F)->setValue(F));
@@ -755,11 +777,14 @@ void Andersens::visitInstruction(Instruction &I) {
   case Instruction::Unreachable:
   case Instruction::Free:
   case Instruction::Shl:
-  case Instruction::Shr:
+  case Instruction::LShr:
+  case Instruction::AShr:
+  case Instruction::ICmp:
+  case Instruction::FCmp:
     return;
   default:
     // Is this something we aren't handling yet?
-    std::cerr << "Unknown instruction: " << I;
+    cerr << "Unknown instruction: " << I;
     abort();
   }
 }
@@ -816,14 +841,22 @@ void Andersens::visitCastInst(CastInst &CI) {
                                        getNode(CI.getOperand(0))));
     } else {
       // P1 = cast int --> <Copy/P1/Univ>
+#if 0
       Constraints.push_back(Constraint(Constraint::Copy, getNodeValue(CI),
                                        &GraphNodes[UniversalSet]));
+#else
+      getNodeValue(CI);
+#endif
     }
   } else if (isa<PointerType>(Op->getType())) {
     // int = cast P1 --> <Copy/Univ/P1>
+#if 0
     Constraints.push_back(Constraint(Constraint::Copy,
                                      &GraphNodes[UniversalSet],
                                      getNode(CI.getOperand(0))));
+#else
+    getNode(CI.getOperand(0));
+#endif
   }
 }
 
@@ -838,10 +871,6 @@ void Andersens::visitSelectInst(SelectInst &SI) {
   }
 }
 
-void Andersens::visitVANext(VANextInst &I) {
-  // FIXME: Implement
-  assert(0 && "vanext not handled yet!");
-}
 void Andersens::visitVAArg(VAArgInst &I) {
   assert(0 && "vaarg not handled yet!");
 }
@@ -873,7 +902,7 @@ void Andersens::AddConstraintsForCall(CallSite CS, Function *F) {
                                      &GraphNodes[UniversalSet],
                                      getReturnNode(F)));
   }
-  
+
   Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
   CallSite::arg_iterator ArgI = CS.arg_begin(), ArgE = CS.arg_end();
   for (; AI != AE && ArgI != ArgE; ++AI, ++ArgI)
@@ -891,7 +920,7 @@ void Andersens::AddConstraintsForCall(CallSite CS, Function *F) {
                                        &GraphNodes[UniversalSet],
                                        getNode(*ArgI)));
     }
-  
+
   // Copy all pointers passed through the varargs section to the varargs node.
   if (F->getFunctionType()->isVarArg())
     for (; ArgI != ArgE; ++ArgI)
@@ -1002,7 +1031,7 @@ void Andersens::SolveConstraints() {
   while (Changed) {
     Changed = false;
     ++NumIters;
-    DEBUG(std::cerr << "Starting iteration #" << Iteration++ << "!\n");
+    DOUT << "Starting iteration #" << Iteration++ << "!\n";
 
     // Loop over all of the constraints, applying them in turn.
     for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
@@ -1035,8 +1064,7 @@ void Andersens::SolveConstraints() {
             // We found a function that is just now escaping.  Mark it as if it
             // didn't have internal linkage.
             AddConstraintsForNonInternalLinkage(F);
-            DEBUG(std::cerr << "Found escaping internal function: "
-                            << F->getName() << "\n");
+            DOUT << "Found escaping internal function: " << F->getName() <<"\n";
             ++NumEscapingFunctions;
           }
 
@@ -1054,9 +1082,9 @@ void Andersens::SolveConstraints() {
             if (IP == KnownCallees.end() || *IP != F) {
               // Add the constraints for the call now.
               AddConstraintsForCall(CS, F);
-              DEBUG(std::cerr << "Found actual callee '"
-                              << F->getName() << "' for call: "
-                              << *CS.getInstruction() << "\n");
+              DOUT << "Found actual callee '"
+                   << F->getName() << "' for call: "
+                   << *CS.getInstruction() << "\n";
               ++NumIndirectCallees;
               KnownCallees.insert(IP, F);
             }
@@ -1074,13 +1102,13 @@ void Andersens::SolveConstraints() {
 
 void Andersens::PrintNode(Node *N) {
   if (N == &GraphNodes[UniversalSet]) {
-    std::cerr << "<universal>";
+    cerr << "<universal>";
     return;
   } else if (N == &GraphNodes[NullPtr]) {
-    std::cerr << "<nullptr>";
+    cerr << "<nullptr>";
     return;
   } else if (N == &GraphNodes[NullObject]) {
-    std::cerr << "<null>";
+    cerr << "<null>";
     return;
   }
 
@@ -1089,56 +1117,56 @@ void Andersens::PrintNode(Node *N) {
   if (Function *F = dyn_cast<Function>(V)) {
     if (isa<PointerType>(F->getFunctionType()->getReturnType()) &&
         N == getReturnNode(F)) {
-      std::cerr << F->getName() << ":retval";
+      cerr << F->getName() << ":retval";
       return;
     } else if (F->getFunctionType()->isVarArg() && N == getVarargNode(F)) {
-      std::cerr << F->getName() << ":vararg";
+      cerr << F->getName() << ":vararg";
       return;
     }
   }
 
   if (Instruction *I = dyn_cast<Instruction>(V))
-    std::cerr << I->getParent()->getParent()->getName() << ":";
+    cerr << I->getParent()->getParent()->getName() << ":";
   else if (Argument *Arg = dyn_cast<Argument>(V))
-    std::cerr << Arg->getParent()->getName() << ":";
+    cerr << Arg->getParent()->getName() << ":";
 
   if (V->hasName())
-    std::cerr << V->getName();
+    cerr << V->getName();
   else
-    std::cerr << "(unnamed)";
+    cerr << "(unnamed)";
 
   if (isa<GlobalValue>(V) || isa<AllocationInst>(V))
     if (N == getObject(V))
-      std::cerr << "<mem>";
+      cerr << "<mem>";
 }
 
 void Andersens::PrintConstraints() {
-  std::cerr << "Constraints:\n";
+  cerr << "Constraints:\n";
   for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
-    std::cerr << "  #" << i << ":  ";
+    cerr << "  #" << i << ":  ";
     Constraint &C = Constraints[i];
     if (C.Type == Constraint::Store)
-      std::cerr << "*";
+      cerr << "*";
     PrintNode(C.Dest);
-    std::cerr << " = ";
+    cerr << " = ";
     if (C.Type == Constraint::Load)
-      std::cerr << "*";
+      cerr << "*";
     PrintNode(C.Src);
-    std::cerr << "\n";
+    cerr << "\n";
   }
 }
 
 void Andersens::PrintPointsToGraph() {
-  std::cerr << "Points-to graph:\n";
+  cerr << "Points-to graph:\n";
   for (unsigned i = 0, e = GraphNodes.size(); i != e; ++i) {
     Node *N = &GraphNodes[i];
-    std::cerr << "[" << (N->end() - N->begin()) << "] ";
+    cerr << "[" << (N->end() - N->begin()) << "] ";
     PrintNode(N);
-    std::cerr << "\t--> ";
+    cerr << "\t--> ";
     for (Node::iterator I = N->begin(), E = N->end(); I != E; ++I) {
-      if (I != N->begin()) std::cerr << ", ";
+      if (I != N->begin()) cerr << ", ";
       PrintNode(*I);
     }
-    std::cerr << "\n";
+    cerr << "\n";
   }
 }