By min, I mean max.
[oota-llvm.git] / lib / Analysis / BasicAliasAnalysis.cpp
index 07f9480a6e614afc899063cf73da980e06cf78eb..22b73b28400c6f2f9e4f456d68575e3f512c7709 100644 (file)
@@ -18,7 +18,6 @@
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
-#include "llvm/ParameterAttributes.h"
 #include "llvm/GlobalVariable.h"
 #include "llvm/Instructions.h"
 #include "llvm/IntrinsicInst.h"
@@ -65,7 +64,8 @@ static bool AddressMightEscape(const Value *V) {
       break; // next use
     case Instruction::Call:
       // If the call is to a few known safe intrinsics, we know that it does
-      // not escape
+      // not escape.
+      // TODO: Eventually just check the 'nocapture' attribute.
       if (!isa<MemIntrinsic>(I))
         return true;
       break;  // next use
@@ -76,30 +76,6 @@ static bool AddressMightEscape(const Value *V) {
   return false;
 }
 
-/// getUnderlyingObject - This traverses the use chain to figure out what object
-/// the specified value points to.  If the value points to, or is derived from,
-/// a unique object or an argument, return it.  This returns:
-///    Arguments, GlobalVariables, Functions, Allocas, Mallocs.
-static const Value *getUnderlyingObject(const Value *V) {
-  if (!isa<PointerType>(V->getType())) return V;
-
-  // If we are at some type of object, return it. GlobalValues and Allocations
-  // have unique addresses. 
-  if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isa<Argument>(V))
-    return V;
-
-  // Traverse through different addressing mechanisms...
-  if (const Instruction *I = dyn_cast<Instruction>(V)) {
-    if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I))
-      return getUnderlyingObject(I->getOperand(0));
-  } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
-    if (CE->getOpcode() == Instruction::BitCast || 
-        CE->getOpcode() == Instruction::GetElementPtr)
-      return getUnderlyingObject(CE->getOperand(0));
-  }
-  return V;
-}
-
 static const User *isGEP(const Value *V) {
   if (isa<GetElementPtrInst>(V) ||
       (isa<ConstantExpr>(V) &&
@@ -207,8 +183,8 @@ namespace {
   ///
   struct VISIBILITY_HIDDEN NoAA : public ImmutablePass, public AliasAnalysis {
     static char ID; // Class identification, replacement for typeinfo
-    NoAA() : ImmutablePass((intptr_t)&ID) {}
-    explicit NoAA(intptr_t PID) : ImmutablePass(PID) { }
+    NoAA() : ImmutablePass(&ID) {}
+    explicit NoAA(void *PID) : ImmutablePass(PID) { }
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.addRequired<TargetData>();
@@ -268,7 +244,7 @@ namespace {
   /// derives from the NoAA class.
   struct VISIBILITY_HIDDEN BasicAliasAnalysis : public NoAA {
     static char ID; // Class identification, replacement for typeinfo
-    BasicAliasAnalysis() : NoAA((intptr_t)&ID) { }
+    BasicAliasAnalysis() : NoAA(&ID) {}
     AliasResult alias(const Value *V1, unsigned V1Size,
                       const Value *V2, unsigned V2Size);
 
@@ -314,7 +290,7 @@ ImmutablePass *llvm::createBasicAliasAnalysisPass() {
 /// global) or not.
 bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
   if (const GlobalVariable *GV = 
-        dyn_cast<GlobalVariable>(getUnderlyingObject(P)))
+        dyn_cast<GlobalVariable>(P->getUnderlyingObject()))
     return GV->isConstant();
   return false;
 }
@@ -327,7 +303,7 @@ bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
 AliasAnalysis::ModRefResult
 BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
   if (!isa<Constant>(P)) {
-    const Value *Object = getUnderlyingObject(P);
+    const Value *Object = P->getUnderlyingObject();
     
     // If this is a tail call and P points to a stack location, we know that
     // the tail call cannot access or modify the local stack.
@@ -339,27 +315,20 @@ BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
         if (CI->isTailCall())
           return NoModRef;
     
-    // Allocations and byval arguments are "new" objects.
-    if (isa<AllocationInst>(Object) || isa<Argument>(Object)) {
-      // Okay, the pointer is to a stack allocated (or effectively so, for 
-      // for noalias parameters) object.  If the address of this object doesn't
-      // escape from this function body to a callee, then we know that no
-      // callees can mod/ref it unless they are actually passed it.
-      if (isa<AllocationInst>(Object) ||
-          cast<Argument>(Object)->hasByValAttr() ||
-          cast<Argument>(Object)->hasNoAliasAttr())
-        if (!AddressMightEscape(Object)) {
-          bool passedAsArg = false;
-          for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
-              CI != CE; ++CI)
-            if (isa<PointerType>((*CI)->getType()) &&
-                (getUnderlyingObject(*CI) == P ||
-                 alias(cast<Value>(CI), ~0U, P, ~0U) != NoAlias))
-              passedAsArg = true;
-          
-          if (!passedAsArg)
-            return NoModRef;
-        }
+    // If the pointer is to a locally allocated object that does not escape,
+    // then the call can not mod/ref the pointer unless the call takes the
+    // argument without capturing it.
+    if (isNonEscapingLocalObject(Object)) {
+      bool passedAsArg = false;
+      // TODO: Eventually only check 'nocapture' arguments.
+      for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
+           CI != CE; ++CI)
+        if (isa<PointerType>((*CI)->getType()) &&
+            alias(cast<Value>(CI), ~0U, P, ~0U) != NoAlias)
+          passedAsArg = true;
+      
+      if (!passedAsArg)
+        return NoModRef;
     }
   }
 
@@ -397,8 +366,8 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
     return alias(V1, V1Size, I->getOperand(0), V2Size);
 
   // Figure out what objects these things are pointing to if we can...
-  const Value *O1 = getUnderlyingObject(V1);
-  const Value *O2 = getUnderlyingObject(V2);
+  const Value *O1 = V1->getUnderlyingObject();
+  const Value *O2 = V2->getUnderlyingObject();
 
   if (O1 != O2) {
     // If V1/V2 point to two different objects we know that we have no alias.