}
static const Value *GetGEPOperands(const Value *V,
- SmallVector<Value*, 16> &GEPOps){
+ SmallVector<Value*, 16> &GEPOps) {
assert(GEPOps.empty() && "Expect empty list to populate!");
GEPOps.insert(GEPOps.end(), cast<User>(V)->op_begin()+1,
cast<User>(V)->op_end());
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))
+ if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V))
return true;
if (const Argument *A = dyn_cast<Argument>(V))
return A->hasNoAliasAttr() || A->hasByValAttr();
/// object that never escapes from the function.
static bool isNonEscapingLocalObject(const Value *V) {
// If this is a local allocation, check to see if it escapes.
- if (isa<AllocationInst>(V))
+ if (isa<AllocationInst>(V) || isNoAliasCall(V))
return !AddressMightEscape(V);
// If this is an argument that corresponds to a byval or noalias argument,
/// by V is smaller than Size.
static bool isObjectSmallerThan(const Value *V, unsigned Size,
const TargetData &TD) {
- const Type *AccessTy = 0;
- if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
+ const Type *AccessTy;
+ if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
AccessTy = GV->getType()->getElementType();
-
- if (const AllocationInst *AI = dyn_cast<AllocationInst>(V))
+ } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(V)) {
if (!AI->isArrayAllocation())
AccessTy = AI->getType()->getElementType();
-
- if (const Argument *A = dyn_cast<Argument>(V))
+ else
+ return false;
+ } else if (const Argument *A = dyn_cast<Argument>(V)) {
if (A->hasByValAttr())
AccessTy = cast<PointerType>(A->getType())->getElementType();
+ else
+ return false;
+ } else {
+ return false;
+ }
- if (AccessTy && AccessTy->isSized())
+ if (AccessTy->isSized())
return TD.getABITypeSize(AccessTy) < Size;
return false;
}
const Value *V2, unsigned V2Size);
ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
- ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
- return NoAA::getModRefInfo(CS1,CS2);
- }
-
+ ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
+
/// hasNoModRefInfoForCalls - We can provide mod/ref information against
/// non-escaping allocations.
virtual bool hasNoModRefInfoForCalls() const { return false; }
}
+AliasAnalysis::ModRefResult
+BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
+ // If CS1 or CS2 are readnone, they don't interact.
+ ModRefBehavior CS1B = AliasAnalysis::getModRefBehavior(CS1);
+ if (CS1B == DoesNotAccessMemory) return NoModRef;
+
+ ModRefBehavior CS2B = AliasAnalysis::getModRefBehavior(CS2);
+ if (CS2B == DoesNotAccessMemory) return NoModRef;
+
+ // If they both only read from memory, just return ref.
+ if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
+ return Ref;
+
+ // Otherwise, fall back to NoAA (mod+ref).
+ return NoAA::getModRefInfo(CS1, CS2);
+}
+
+
// alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
-// as array references. Note that this function is heavily tail recursive.
-// Hopefully we have a smart C++ compiler. :)
+// as array references.
//
AliasAnalysis::AliasResult
BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
// Are we checking for alias of the same value?
if (V1 == V2) return MustAlias;
- if ((!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType())) &&
- V1->getType() != Type::Int64Ty && V2->getType() != Type::Int64Ty)
+ if (!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType()))
return NoAlias; // Scalars cannot alias each other
- // Strip off cast instructions...
+ // Strip off cast instructions. Since V1 and V2 are pointers, they must be
+ // pointer<->pointer bitcasts.
if (const BitCastInst *I = dyn_cast<BitCastInst>(V1))
return alias(I->getOperand(0), V1Size, V2, V2Size);
if (const BitCastInst *I = dyn_cast<BitCastInst>(V2))
return alias(V1, V1Size, I->getOperand(0), V2Size);
- // Figure out what objects these things are pointing to if we can...
+ // Figure out what objects these things are pointing to if we can.
const Value *O1 = V1->getUnderlyingObject();
const Value *O2 = V2->getUnderlyingObject();
if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
return NoAlias;
- // Incoming argument cannot alias locally allocated object!
- if ((isa<Argument>(O1) && isa<AllocationInst>(O2)) ||
- (isa<Argument>(O2) && isa<AllocationInst>(O1)))
+ // Arguments can't alias with local allocations or noalias calls.
+ if ((isa<Argument>(O1) && (isa<AllocationInst>(O2) || isNoAliasCall(O2))) ||
+ (isa<Argument>(O2) && (isa<AllocationInst>(O1) || isNoAliasCall(O1))))
return NoAlias;
-
+
// Most objects can't alias null.
if ((isa<ConstantPointerNull>(V2) && isKnownNonNull(O1)) ||
(isa<ConstantPointerNull>(V1) && isKnownNonNull(O2)))
// constant expression getelementptrs here.
//
if (isGEP(V1) && isGEP(V2)) {
+ const User *GEP1 = cast<User>(V1);
+ const User *GEP2 = cast<User>(V2);
+
+ // If V1 and V2 are identical GEPs, just recurse down on both of them.
+ // This allows us to analyze things like:
+ // P = gep A, 0, i, 1
+ // Q = gep B, 0, i, 1
+ // by just analyzing A and B. This is even safe for variable indices.
+ if (GEP1->getType() == GEP2->getType() &&
+ GEP1->getNumOperands() == GEP2->getNumOperands() &&
+ GEP1->getOperand(0)->getType() == GEP2->getOperand(0)->getType() &&
+ // All operands are the same, ignoring the base.
+ std::equal(GEP1->op_begin()+1, GEP1->op_end(), GEP2->op_begin()+1))
+ return alias(GEP1->getOperand(0), V1Size, GEP2->getOperand(0), V2Size);
+
+
// Drill down into the first non-gep value, to test for must-aliasing of
// the base pointers.
- const User *G = cast<User>(V1);
- while (isGEP(G->getOperand(0)) &&
- G->getOperand(1) ==
- Constant::getNullValue(G->getOperand(1)->getType()))
- G = cast<User>(G->getOperand(0));
- const Value *BasePtr1 = G->getOperand(0);
-
- G = cast<User>(V2);
- while (isGEP(G->getOperand(0)) &&
- G->getOperand(1) ==
- Constant::getNullValue(G->getOperand(1)->getType()))
- G = cast<User>(G->getOperand(0));
- const Value *BasePtr2 = G->getOperand(0);
+ while (isGEP(GEP1->getOperand(0)) &&
+ GEP1->getOperand(1) ==
+ Constant::getNullValue(GEP1->getOperand(1)->getType()))
+ GEP1 = cast<User>(GEP1->getOperand(0));
+ const Value *BasePtr1 = GEP1->getOperand(0);
+
+ while (isGEP(GEP2->getOperand(0)) &&
+ GEP2->getOperand(1) ==
+ Constant::getNullValue(GEP2->getOperand(1)->getType()))
+ GEP2 = cast<User>(GEP2->getOperand(0));
+ const Value *BasePtr2 = GEP2->getOperand(0);
// Do the base pointers alias?
AliasResult BaseAlias = alias(BasePtr1, ~0U, BasePtr2, ~0U);
return MayAlias;
}
-// This function is used to determin if the indices of two GEP instructions are
+// This function is used to determine if the indices of two GEP instructions are
// equal. V1 and V2 are the indices.
static bool IndexOperandsEqual(Value *V1, Value *V2) {
if (V1->getType() == V2->getType())