//===- BasicAliasAnalysis.cpp - Local Alias Analysis Impl -----------------===//
-//
+//
// 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 the default implementation of the Alias Analysis interface
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<TargetData>();
}
-
+
virtual void initializePass() {
TD = &getAnalysis<TargetData>();
}
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!");
virtual void deleteValue(Value *V) {}
virtual void copyValue(Value *From, Value *To) {}
};
-
+
// Register this pass...
RegisterOpt<NoAA>
U("no-aa", "No Alias Analysis (always returns 'may' alias)");
virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
std::vector<PointerAccessInfo> *Info);
-
+
private:
// CheckGEPInstructions - Check two GEP instructions with known
// must-aliasing base pointers. This checks to see if the index expressions
const Type *BasePtr2Ty, std::vector<Value*> &GEP2Ops,
unsigned G2Size);
};
-
+
// Register this pass...
RegisterOpt<BasicAliasAnalysis>
X("basicaa", "Basic Alias Analysis (default AA impl)");
// If we are at some type of object... return it.
if (hasUniqueAddress(V) || isa<Argument>(V)) return V;
-
+
// Traverse through different addressing mechanisms...
if (const Instruction *I = dyn_cast<Instruction>(V)) {
if (isa<CastInst>(I) || isa<GetElementPtrInst>(I))
// because it simply can't get its address.
if (!AddressMightEscape(AI))
return NoModRef;
+
+ // 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.
+ if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
+ if (CI->isTailCall() && isa<AllocaInst>(AI))
+ return NoModRef;
}
// The AliasAnalysis base class has some smarts, lets use them.
if (!isa<Argument>(O1) && isa<ConstantPointerNull>(V2))
return NoAlias; // Unique values don't alias null
- if (isa<GlobalVariable>(O1) || isa<AllocationInst>(O1))
+ if (isa<GlobalVariable>(O1) ||
+ (isa<AllocationInst>(O1) &&
+ !cast<AllocationInst>(O1)->isArrayAllocation()))
if (cast<PointerType>(O1->getType())->getElementType()->isSized()) {
// If the size of the other access is larger than the total size of the
// global/alloca/malloc, it cannot be accessing the global (it's
if (!isa<Argument>(O2) && isa<ConstantPointerNull>(V1))
return NoAlias; // Unique values don't alias null
- if (isa<GlobalVariable>(O2) || isa<AllocationInst>(O2))
+ if (isa<GlobalVariable>(O2) ||
+ (isa<AllocationInst>(O2) &&
+ !cast<AllocationInst>(O2)->isArrayAllocation()))
if (cast<PointerType>(O2->getType())->getElementType()->isSized()) {
// If the size of the other access is larger than the total size of the
// global/alloca/malloc, it cannot be accessing the object (it's
do {
BasePtr1 = cast<User>(BasePtr1)->getOperand(0);
} while (isGEP(BasePtr1) &&
- cast<User>(BasePtr1)->getOperand(1) ==
+ cast<User>(BasePtr1)->getOperand(1) ==
Constant::getNullValue(cast<User>(BasePtr1)->getOperand(1)->getType()));
do {
BasePtr2 = cast<User>(BasePtr2)->getOperand(0);
} while (isGEP(BasePtr2) &&
- cast<User>(BasePtr2)->getOperand(1) ==
+ cast<User>(BasePtr2)->getOperand(1) ==
Constant::getNullValue(cast<User>(BasePtr2)->getOperand(1)->getType()));
// Do the base pointers alias?
if (ConstantFound) {
if (V2Size <= 1 && V1Size <= 1) // Just pointer check?
return NoAlias;
-
+
// Otherwise we have to check to see that the distance is more than
// the size of the argument... build an index vector that is equal to
// the arguments provided, except substitute 0's for any variable
}
}
}
-
+
return MayAlias;
}
// If so, return mustalias.
if (UnequalOper == MinOperands) {
if (GEP1Ops.size() < GEP2Ops.size()) std::swap(GEP1Ops, GEP2Ops);
-
+
bool AllAreZeros = true;
for (unsigned i = UnequalOper; i != MaxOperands; ++i)
if (!isa<Constant>(GEP1Ops[i]) ||
if (AllAreZeros) return MustAlias;
}
-
+
// So now we know that the indexes derived from the base pointers,
// which are known to alias, are different. We can still determine a
// no-alias result if there are differing constant pairs in the index
// chain. For example:
// A[i][0] != A[j][1] iff (&A[0][1]-&A[0][0] >= std::max(G1S, G2S))
//
+ // We have to be careful here about array accesses. In particular, consider:
+ // A[1][0] vs A[0][i]
+ // In this case, we don't *know* that the array will be accessed in bounds:
+ // the index could even be negative. Because of this, we have to
+ // conservatively *give up* and return may alias. We disregard differing
+ // array subscripts that are followed by a variable index without going
+ // through a struct.
+ //
unsigned SizeMax = std::max(G1S, G2S);
if (SizeMax == ~0U) return MayAlias; // Avoid frivolous work.
for (; FirstConstantOper != MinOperands; ++FirstConstantOper) {
const Value *G1Oper = GEP1Ops[FirstConstantOper];
const Value *G2Oper = GEP2Ops[FirstConstantOper];
-
+
if (G1Oper != G2Oper) // Found non-equal constant indexes...
if (Constant *G1OC = dyn_cast<ConstantInt>(const_cast<Value*>(G1Oper)))
if (Constant *G2OC = dyn_cast<ConstantInt>(const_cast<Value*>(G2Oper))){
GEP1Ops[FirstConstantOper] = G1OC;
GEP2Ops[FirstConstantOper] = G2OC;
}
-
+
if (G1OC != G2OC) {
+ // Handle the "be careful" case above: if this is an array
+ // subscript, scan for a subsequent variable array index.
+ if (isa<ArrayType>(BasePtr1Ty)) {
+ const Type *NextTy =cast<ArrayType>(BasePtr1Ty)->getElementType();
+ bool isBadCase = false;
+
+ for (unsigned Idx = FirstConstantOper+1;
+ Idx != MinOperands && isa<ArrayType>(NextTy); ++Idx) {
+ const Value *V1 = GEP1Ops[Idx], *V2 = GEP2Ops[Idx];
+ if (!isa<Constant>(V1) || !isa<Constant>(V2)) {
+ isBadCase = true;
+ break;
+ }
+ NextTy = cast<ArrayType>(NextTy)->getElementType();
+ }
+
+ if (isBadCase) G1OC = 0;
+ }
+
// Make sure they are comparable (ie, not constant expressions), and
// make sure the GEP with the smaller leading constant is GEP1.
- Constant *Compare = ConstantExpr::getSetGT(G1OC, G2OC);
- if (ConstantBool *CV = dyn_cast<ConstantBool>(Compare)) {
- if (CV->getValue()) // If they are comparable and G2 > G1
- std::swap(GEP1Ops, GEP2Ops); // Make GEP1 < GEP2
- break;
+ if (G1OC) {
+ Constant *Compare = ConstantExpr::getSetGT(G1OC, G2OC);
+ if (ConstantBool *CV = dyn_cast<ConstantBool>(Compare)) {
+ if (CV->getValue()) // If they are comparable and G2 > G1
+ std::swap(GEP1Ops, GEP2Ops); // Make GEP1 < GEP2
+ break;
+ }
}
}
}
BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(G1Oper);
}
-
+
// No shared constant operands, and we ran out of common operands. At this
// point, the GEP instructions have run through all of their operands, and we
// haven't found evidence that there are any deltas between the GEP's.
// Now crop off any constants from the end...
GEP1Ops.resize(MinOperands);
int64_t Offset2 = TD.getIndexedOffset(GEPPointerTy, GEP1Ops);
-
+
// If the tail provided a bit enough offset, return noalias!
if ((uint64_t)(Offset2-Offset1) >= SizeMax)
return NoAlias;
}
}
-
+
// Couldn't find anything useful.
return MayAlias;
}
// than the first constant index of GEP2.
// Advance BasePtr[12]Ty over this first differing constant operand.
- BasePtr2Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(GEP2Ops[FirstConstantOper]);
- BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->getTypeAtIndex(GEP1Ops[FirstConstantOper]);
-
+ BasePtr2Ty = cast<CompositeType>(BasePtr1Ty)->
+ getTypeAtIndex(GEP2Ops[FirstConstantOper]);
+ BasePtr1Ty = cast<CompositeType>(BasePtr1Ty)->
+ getTypeAtIndex(GEP1Ops[FirstConstantOper]);
+
// We are going to be using TargetData::getIndexedOffset to determine the
// offset that each of the GEP's is reaching. To do this, we have to convert
// all variable references to constant references. To do this, we convert the
- // initial equal sequence of variables into constant zeros to start with.
- for (unsigned i = 0; i != FirstConstantOper; ++i)
- if (!isa<ConstantInt>(GEP1Ops[i]) || !isa<ConstantInt>(GEP2Ops[i]))
+ // initial sequence of array subscripts into constant zeros to start with.
+ const Type *ZeroIdxTy = GEPPointerTy;
+ for (unsigned i = 0; i != FirstConstantOper; ++i) {
+ if (!isa<StructType>(ZeroIdxTy))
GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Type::UIntTy);
+ if (const CompositeType *CT = dyn_cast<CompositeType>(ZeroIdxTy))
+ ZeroIdxTy = CT->getTypeAtIndex(GEP1Ops[i]);
+ }
+
// We know that GEP1Ops[FirstConstantOper] & GEP2Ops[FirstConstantOper] are ok
-
+
// Loop over the rest of the operands...
for (unsigned i = FirstConstantOper+1; i != MaxOperands; ++i) {
const Value *Op1 = i < GEP1Ops.size() ? GEP1Ops[i] : 0;
if (const ArrayType *AT = dyn_cast<ArrayType>(BasePtr1Ty))
if (Op1C->getRawValue() >= AT->getNumElements())
return MayAlias; // Be conservative with out-of-range accesses
-
+
} else {
// GEP1 is known to produce a value less than GEP2. To be
// conservatively correct, we must assume the largest possible
GEP1Ops[i] = ConstantSInt::get(Type::LongTy,AT->getNumElements()-1);
}
}
-
+
if (Op2) {
if (const ConstantInt *Op2C = dyn_cast<ConstantInt>(Op2)) {
// If this is an array index, make sure the array element is in range.
BasePtr2Ty = 0;
}
}
-
+
if (GEPPointerTy->getElementType()->isSized()) {
int64_t Offset1 = getTargetData().getIndexedOffset(GEPPointerTy, GEP1Ops);
int64_t Offset2 = getTargetData().getIndexedOffset(GEPPointerTy, GEP2Ops);
assert(Offset1<Offset2 && "There is at least one different constant here!");
if ((uint64_t)(Offset2-Offset1) >= SizeMax) {
- //std::cerr << "Determined that these two GEP's don't alias ["
+ //std::cerr << "Determined that these two GEP's don't alias ["
// << SizeMax << " bytes]: \n" << *GEP1 << *GEP2;
return NoAlias;
}
// Note that this list cannot contain libm functions (such as acos and sqrt)
// that set errno on a domain or other error.
-static const char *DoesntAccessMemoryTable[] = {
- // LLVM intrinsics:
- "llvm.frameaddress", "llvm.returnaddress", "llvm.readport",
- "llvm.isunordered",
-
+static const char *DoesntAccessMemoryFns[] = {
"abs", "labs", "llabs", "imaxabs", "fabs", "fabsf", "fabsl",
"trunc", "truncf", "truncl", "ldexp",
-
+
"atan", "atanf", "atanl", "atan2", "atan2f", "atan2l",
"cbrt",
- "cos", "cosf", "cosl", "cosh", "coshf", "coshl",
- "exp", "expf", "expl",
+ "cos", "cosf", "cosl",
+ "exp", "expf", "expl",
"hypot",
- "sin", "sinf", "sinl", "sinh", "sinhf", "sinhl",
+ "sin", "sinf", "sinl",
"tan", "tanf", "tanl", "tanh", "tanhf", "tanhl",
+
+ "floor", "floorf", "floorl", "ceil", "ceilf", "ceill",
// ctype.h
"isalnum", "isalpha", "iscntrl", "isdigit", "isgraph", "islower", "isprint"
"iswalnum", "iswalpha", "iswcntrl", "iswdigit", "iswgraph", "iswlower",
"iswprint", "iswpunct", "iswspace", "iswupper", "iswxdigit",
- "iswctype", "towctrans", "towlower", "towupper",
+ "iswctype", "towctrans", "towlower", "towupper",
- "btowc", "wctob",
+ "btowc", "wctob",
"isinf", "isnan", "finite",
"nexttoward", "nexttowardf", "nexttowardd",
"nextafter", "nextafterf", "nextafterd",
- // glibc functions:
- "__fpclassify", "__fpclassifyf", "__fpclassifyl",
+ // ISO C99:
"__signbit", "__signbitf", "__signbitl",
};
-static const unsigned DAMTableSize =
- sizeof(DoesntAccessMemoryTable)/sizeof(DoesntAccessMemoryTable[0]);
-static const char *OnlyReadsMemoryTable[] = {
+static const char *OnlyReadsMemoryFns[] = {
"atoi", "atol", "atof", "atoll", "atoq", "a64l",
- "bcmp", "memcmp", "memchr", "memrchr", "wmemcmp", "wmemchr",
+ "bcmp", "memcmp", "memchr", "memrchr", "wmemcmp", "wmemchr",
// Strings
"strcmp", "strcasecmp", "strcoll", "strncmp", "strncasecmp",
- "strchr", "strcspn", "strlen", "strpbrk", "strrchr", "strspn", "strstr",
+ "strchr", "strcspn", "strlen", "strpbrk", "strrchr", "strspn", "strstr",
"index", "rindex",
// Wide char strings
"wcschr", "wcscmp", "wcscoll", "wcscspn", "wcslen", "wcsncmp", "wcspbrk",
- "wcsrchr", "wcsspn", "wcsstr",
+ "wcsrchr", "wcsspn", "wcsstr",
// glibc
"alphasort", "alphasort64", "versionsort", "versionsort64",
"feof_unlocked", "ferror_unlocked", "fileno_unlocked"
};
-static const unsigned ORMTableSize =
- sizeof(OnlyReadsMemoryTable)/sizeof(OnlyReadsMemoryTable[0]);
-
-AliasAnalysis::ModRefBehavior
+AliasAnalysis::ModRefBehavior
BasicAliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
std::vector<PointerAccessInfo> *Info) {
if (!F->isExternal()) return UnknownModRefBehavior;
+ static std::vector<const char*> NoMemoryTable, OnlyReadsMemoryTable;
+
static bool Initialized = false;
if (!Initialized) {
+ NoMemoryTable.insert(NoMemoryTable.end(),
+ DoesntAccessMemoryFns,
+ DoesntAccessMemoryFns+
+ sizeof(DoesntAccessMemoryFns)/sizeof(DoesntAccessMemoryFns[0]));
+
+ OnlyReadsMemoryTable.insert(OnlyReadsMemoryTable.end(),
+ OnlyReadsMemoryFns,
+ OnlyReadsMemoryFns+
+ sizeof(OnlyReadsMemoryFns)/sizeof(OnlyReadsMemoryFns[0]));
+#define GET_MODREF_BEHAVIOR
+#include "llvm/Intrinsics.gen"
+#undef GET_MODREF_BEHAVIOR
+
// Sort the table the first time through.
- std::sort(DoesntAccessMemoryTable, DoesntAccessMemoryTable+DAMTableSize,
- StringCompare());
- std::sort(OnlyReadsMemoryTable, OnlyReadsMemoryTable+ORMTableSize,
+ std::sort(NoMemoryTable.begin(), NoMemoryTable.end(), StringCompare());
+ std::sort(OnlyReadsMemoryTable.begin(), OnlyReadsMemoryTable.end(),
StringCompare());
Initialized = true;
}
- const char **Ptr = std::lower_bound(DoesntAccessMemoryTable,
- DoesntAccessMemoryTable+DAMTableSize,
- F->getName().c_str(), StringCompare());
- if (Ptr != DoesntAccessMemoryTable+DAMTableSize && *Ptr == F->getName())
+ std::vector<const char*>::iterator Ptr =
+ std::lower_bound(NoMemoryTable.begin(), NoMemoryTable.end(),
+ F->getName().c_str(), StringCompare());
+ if (Ptr != NoMemoryTable.end() && *Ptr == F->getName())
return DoesNotAccessMemory;
-
- Ptr = std::lower_bound(OnlyReadsMemoryTable,
- OnlyReadsMemoryTable+ORMTableSize,
+
+ Ptr = std::lower_bound(OnlyReadsMemoryTable.begin(),
+ OnlyReadsMemoryTable.end(),
F->getName().c_str(), StringCompare());
- if (Ptr != OnlyReadsMemoryTable+ORMTableSize && *Ptr == F->getName())
+ if (Ptr != OnlyReadsMemoryTable.end() && *Ptr == F->getName())
return OnlyReadsMemory;
return UnknownModRefBehavior;