gdb uses DW_AT_prototyped to identify K&R style in C based languages.
[oota-llvm.git] / lib / Analysis / BasicAliasAnalysis.cpp
index 9608a28edd89ff1105dacc57c09dd516a280b15c..fe71f04098b13061a0b04c4dcf2a99bc3fd7f9dd 100644 (file)
@@ -14,6 +14,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/Analysis/CaptureTracking.h"
 #include "llvm/Analysis/Passes.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
@@ -35,59 +36,6 @@ using namespace llvm;
 // Useful predicates
 //===----------------------------------------------------------------------===//
 
-// Determine if a value escapes from the function it is contained in (being
-// returned by the function does not count as escaping here).  If a value local
-// to the function does not escape, there is no way another function can mod/ref
-// it.  We do this by looking at its uses and determining if they can escape
-// (recursively).
-static bool AddressMightEscape(const Value *V) {
-  for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
-       UI != E; ++UI) {
-    const Instruction *I = cast<Instruction>(*UI);
-    switch (I->getOpcode()) {
-    case Instruction::Load: 
-      break; //next use.
-    case Instruction::Store:
-      if (I->getOperand(0) == V)
-        return true; // Escapes if the pointer is stored.
-      break; // next use.
-    case Instruction::GetElementPtr:
-      if (AddressMightEscape(I))
-        return true;
-      break; // next use.
-    case Instruction::BitCast:
-      if (AddressMightEscape(I))
-        return true;
-      break; // next use
-    case Instruction::Ret:
-      // If returned, the address will escape to calling functions, but no
-      // callees could modify it.
-      break; // next use
-    case Instruction::Call:
-      // If the argument to the call has the nocapture attribute, then the call
-      // may store or load to the pointer, but it cannot escape.
-      if (cast<CallInst>(I)->paramHasAttr(UI.getOperandNo(), 
-                                          Attribute::NoCapture))
-        continue;
-
-      // FIXME: MemIntrinsics should have their operands marked nocapture!
-      if (isa<MemIntrinsic>(I))
-        continue;  // next use
-      return true;
-    case Instruction::Invoke:
-      // If the argument to the call has the nocapture attribute, then the call
-      // may store or load to the pointer, but it cannot escape.
-      if (cast<InvokeInst>(I)->paramHasAttr(UI.getOperandNo()-2,
-                                            Attribute::NoCapture))
-        continue;
-      return true;
-    default:
-      return true;
-    }
-  }
-  return false;
-}
-
 static const User *isGEP(const Value *V) {
   if (isa<GetElementPtrInst>(V) ||
       (isa<ConstantExpr>(V) &&
@@ -116,30 +64,6 @@ static const Value *GetGEPOperands(const Value *V,
   return V;
 }
 
-/// isNoAliasCall - Return true if this pointer is returned by a noalias
-/// function.
-static bool isNoAliasCall(const Value *V) {
-  if (isa<CallInst>(V) || isa<InvokeInst>(V))
-    return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
-      .paramHasAttr(0, Attribute::NoAlias);
-  return false;
-}
-
-/// isIdentifiedObject - Return true if this pointer refers to a distinct and
-/// identifiable object.  This returns true for:
-///    Global Variables and Functions
-///    Allocas and Mallocs
-///    ByVal and NoAlias Arguments
-///    NoAlias returns
-///
-static bool isIdentifiedObject(const Value *V) {
-  if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V))
-    return true;
-  if (const Argument *A = dyn_cast<Argument>(V))
-    return A->hasNoAliasAttr() || A->hasByValAttr();
-  return false;
-}
-
 /// isKnownNonNull - Return true if we know that the specified value is never
 /// null.
 static bool isKnownNonNull(const Value *V) {
@@ -161,7 +85,7 @@ static bool isKnownNonNull(const Value *V) {
 static bool isNonEscapingLocalObject(const Value *V) {
   // If this is a local allocation, check to see if it escapes.
   if (isa<AllocationInst>(V) || isNoAliasCall(V))
-    return !AddressMightEscape(V);
+    return !PointerMayBeCaptured(V, false);
 
   // If this is an argument that corresponds to a byval or noalias argument,
   // then it has not escaped before entering the function.  Check if it escapes
@@ -171,7 +95,7 @@ static bool isNonEscapingLocalObject(const Value *V) {
       // Don't bother analyzing arguments already known not to escape.
       if (A->hasNoCaptureAttr())
         return true;
-      return !AddressMightEscape(V);
+      return !PointerMayBeCaptured(V, false);
     }
   return false;
 }
@@ -199,7 +123,7 @@ static bool isObjectSmallerThan(const Value *V, unsigned Size,
   }
   
   if (AccessTy->isSized())
-    return TD.getABITypeSize(AccessTy) < Size;
+    return TD.getTypePaddedSize(AccessTy) < Size;
   return false;
 }
 
@@ -231,11 +155,6 @@ namespace {
       return MayAlias;
     }
 
-    virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
-                                         std::vector<PointerAccessInfo> *Info) {
-      return UnknownModRefBehavior;
-    }
-
     virtual void getArgumentAccesses(Function *F, CallSite CS,
                                      std::vector<PointerAccessInfo> &Info) {
       assert(0 && "This method may not be called on this function!");
@@ -282,7 +201,7 @@ namespace {
 
     ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
     ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
-    
+
     /// hasNoModRefInfoForCalls - We can provide mod/ref information against
     /// non-escaping allocations.
     virtual bool hasNoModRefInfoForCalls() const { return false; }
@@ -325,6 +244,7 @@ bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) {
   return false;
 }
 
+
 // getModRefInfo - Check to see if the specified callsite can clobber the
 // specified memory object.  Since we only look at local properties of this
 // function, we really can't say much about this query.  We do, however, use
@@ -348,7 +268,7 @@ BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
     // 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)) {
+    if (isNonEscapingLocalObject(Object) && CS.getInstruction() != Object) {
       bool passedAsArg = false;
       // TODO: Eventually only check 'nocapture' arguments.
       for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
@@ -443,10 +363,10 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
   // non-escaping local object, then we know the object couldn't escape to a
   // point where the call could return it.
   if ((isa<CallInst>(O1) || isa<InvokeInst>(O1)) &&
-      isNonEscapingLocalObject(O2))
+      isNonEscapingLocalObject(O2) && O1 != O2)
     return NoAlias;
   if ((isa<CallInst>(O2) || isa<InvokeInst>(O2)) &&
-      isNonEscapingLocalObject(O1))
+      isNonEscapingLocalObject(O1) && O1 != O2)
     return NoAlias;
   
   // If we have two gep instructions with must-alias'ing base pointers, figure