//===- 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
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/AliasAnalysis.h"
+#include "llvm/Analysis/Passes.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<TargetData>();
}
-
+
virtual void initializePass() {
TD = &getAnalysis<TargetData>();
}
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!");
+ }
+
virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals) { }
virtual bool pointsToConstantMemory(const Value *P) { return false; }
- virtual bool doesNotAccessMemory(Function *F) { return false; }
- virtual bool onlyReadsMemory(Function *F) { return false; }
virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
return ModRef;
}
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)");
RegisterAnalysisGroup<AliasAnalysis, NoAA> V;
} // End of anonymous namespace
+ImmutablePass *llvm::createNoAAPass() { return new NoAA(); }
namespace {
/// BasicAliasAnalysis - This is the default alias analysis implementation.
const Value *V2, unsigned V2Size);
ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
+ ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
+ return NoAA::getModRefInfo(CS1,CS2);
+ }
/// hasNoModRefInfoForCalls - We can provide mod/ref information against
/// non-escaping allocations.
/// global) or not.
bool pointsToConstantMemory(const Value *P);
- virtual bool doesNotAccessMemory(Function *F);
- virtual bool onlyReadsMemory(Function *F);
+ virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
+ std::vector<PointerAccessInfo> *Info);
private:
// CheckGEPInstructions - Check two GEP instructions with known
const Type *BasePtr2Ty, std::vector<Value*> &GEP2Ops,
unsigned G2Size);
};
-
+
// Register this pass...
RegisterOpt<BasicAliasAnalysis>
X("basicaa", "Basic Alias Analysis (default AA impl)");
RegisterAnalysisGroup<AliasAnalysis, BasicAliasAnalysis, true> Y;
} // End of anonymous namespace
+ImmutablePass *llvm::createBasicAliasAnalysisPass() {
+ return new BasicAliasAnalysis();
+}
+
// hasUniqueAddress - Return true if the specified value points to something
// with a unique, discernable, address.
static inline bool hasUniqueAddress(const Value *V) {
// 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 (const GlobalVariable *GV = dyn_cast<GlobalVariable>(O1))
- if (GV->getType()->getElementType()->isSized()) {
+ 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, it cannot be accessing the global (it's undefined to load or
- // store bytes after the global).
- unsigned GlobalSize =
- getTargetData().getTypeSize(GV->getType()->getElementType());
- if (GlobalSize < V2Size)
+ // global/alloca/malloc, it cannot be accessing the global (it's
+ // undefined to load or store bytes before or after an object).
+ const Type *ElTy = cast<PointerType>(O1->getType())->getElementType();
+ unsigned GlobalSize = getTargetData().getTypeSize(ElTy);
+ if (GlobalSize < V2Size && V2Size != ~0U)
return NoAlias;
}
}
if (!isa<Argument>(O2) && isa<ConstantPointerNull>(V1))
return NoAlias; // Unique values don't alias null
- if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(O2))
- if (GV->getType()->getElementType()->isSized()) {
+ 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, it cannot be accessing the global (it's undefined to load or
- // store bytes after the global).
- unsigned GlobalSize =
- getTargetData().getTypeSize(GV->getType()->getElementType());
- if (GlobalSize < V1Size)
+ // global/alloca/malloc, it cannot be accessing the object (it's
+ // undefined to load or store bytes before or after an object).
+ const Type *ElTy = cast<PointerType>(O2->getType())->getElementType();
+ unsigned GlobalSize = getTargetData().getTypeSize(ElTy);
+ if (GlobalSize < V1Size && V1Size != ~0U)
return NoAlias;
}
}
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
// indexes we find...
- for (unsigned i = 0; i != GEPOperands.size(); ++i)
- if (!isa<ConstantInt>(GEPOperands[i]))
- GEPOperands[i] =Constant::getNullValue(GEPOperands[i]->getType());
- int64_t Offset = getTargetData().getIndexedOffset(BasePtr->getType(),
- GEPOperands);
- if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
- return NoAlias;
+ if (cast<PointerType>(
+ BasePtr->getType())->getElementType()->isSized()) {
+ for (unsigned i = 0; i != GEPOperands.size(); ++i)
+ if (!isa<ConstantInt>(GEPOperands[i]))
+ GEPOperands[i] =
+ Constant::getNullValue(GEPOperands[i]->getType());
+ int64_t Offset =
+ getTargetData().getIndexedOffset(BasePtr->getType(), GEPOperands);
+
+ if (Offset >= (int64_t)V2Size || Offset <= -(int64_t)V1Size)
+ return NoAlias;
+ }
}
}
}
-
+
return MayAlias;
}
if (BasePtr1Ty != BasePtr2Ty)
return MayAlias;
- const Type *GEPPointerTy = BasePtr1Ty;
+ const PointerType *GEPPointerTy = cast<PointerType>(BasePtr1Ty);
// Find the (possibly empty) initial sequence of equal values... which are not
// necessarily constants.
// 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
// A[i][0] != A[j][1] iff (&A[0][1]-&A[0][0] >= std::max(G1S, G2S))
//
unsigned SizeMax = std::max(G1S, G2S);
- if (SizeMax == ~0U) return MayAlias; // Avoid frivolous work...
+ if (SizeMax == ~0U) return MayAlias; // Avoid frivolous work.
// Scan for the first operand that is constant and unequal in the
// two getelementptrs...
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))){
}
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;
}
// Advance BasePtr[12]Ty over this first differing constant operand.
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
GEP1Ops[i] = GEP2Ops[i] = Constant::getNullValue(Type::UIntTy);
// 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;
}
}
-
- 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 ["
- // << SizeMax << " bytes]: \n" << *GEP1 << *GEP2;
- return NoAlias;
+
+ 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 ["
+ // << SizeMax << " bytes]: \n" << *GEP1 << *GEP2;
+ return NoAlias;
+ }
}
return MayAlias;
}
// that set errno on a domain or other error.
static const char *DoesntAccessMemoryTable[] = {
// LLVM intrinsics:
- "llvm.frameaddress", "llvm.returnaddress", "llvm.readport", "llvm.isunordered",
+ "llvm.frameaddress", "llvm.returnaddress", "llvm.readport",
+ "llvm.isunordered", "llvm.sqrt", "llvm.bswap.i16", "llvm.bswap.i32",
+ "llvm.bswap.i64", "llvm.ctpop", "llvm.ctlz", "llvm.cttz",
"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]);
-/// doesNotAccessMemory - Return true if we know that the function does not
-/// access memory at all. Since basicaa does no analysis, we can only do simple
-/// things here. In particular, if we have an external function with the name
-/// of a standard C library function, we are allowed to assume it will be
-/// resolved by libc, so we can hardcode some entries in here.
-bool BasicAliasAnalysis::doesNotAccessMemory(Function *F) {
- if (!F->isExternal()) return false;
-
- static bool Initialized = false;
- if (!Initialized) {
- // Sort the table the first time through.
- std::sort(DoesntAccessMemoryTable, DoesntAccessMemoryTable+DAMTableSize,
- StringCompare());
- Initialized = true;
- }
-
- const char **Ptr = std::lower_bound(DoesntAccessMemoryTable,
- DoesntAccessMemoryTable+DAMTableSize,
- F->getName().c_str(), StringCompare());
- return Ptr != DoesntAccessMemoryTable+DAMTableSize && *Ptr == F->getName();
-}
-
-
static const char *OnlyReadsMemoryTable[] = {
"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",
static const unsigned ORMTableSize =
sizeof(OnlyReadsMemoryTable)/sizeof(OnlyReadsMemoryTable[0]);
-bool BasicAliasAnalysis::onlyReadsMemory(Function *F) {
- if (doesNotAccessMemory(F)) return true;
- if (!F->isExternal()) return false;
+AliasAnalysis::ModRefBehavior
+BasicAliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
+ std::vector<PointerAccessInfo> *Info) {
+ if (!F->isExternal()) return UnknownModRefBehavior;
static bool Initialized = false;
if (!Initialized) {
// Sort the table the first time through.
+ std::sort(DoesntAccessMemoryTable, DoesntAccessMemoryTable+DAMTableSize,
+ StringCompare());
std::sort(OnlyReadsMemoryTable, OnlyReadsMemoryTable+ORMTableSize,
StringCompare());
Initialized = true;
}
- const char **Ptr = std::lower_bound(OnlyReadsMemoryTable,
- OnlyReadsMemoryTable+ORMTableSize,
+ const char **Ptr = std::lower_bound(DoesntAccessMemoryTable,
+ DoesntAccessMemoryTable+DAMTableSize,
F->getName().c_str(), StringCompare());
- return Ptr != OnlyReadsMemoryTable+ORMTableSize && *Ptr == F->getName();
-}
+ if (Ptr != DoesntAccessMemoryTable+DAMTableSize && *Ptr == F->getName())
+ return DoesNotAccessMemory;
+ Ptr = std::lower_bound(OnlyReadsMemoryTable,
+ OnlyReadsMemoryTable+ORMTableSize,
+ F->getName().c_str(), StringCompare());
+ if (Ptr != OnlyReadsMemoryTable+ORMTableSize && *Ptr == F->getName())
+ return OnlyReadsMemory;
+ return UnknownModRefBehavior;
+}