Add an Intraprocedural form of BasicAliasAnalysis, which aims to

properly handles instructions and arguments defined in different
functions, or across recursive function iterations.

llvm-svn: 107109
This commit is contained in:
Dan Gohman 2010-06-29 00:50:39 +00:00
parent d6a091a4d4
commit 0824affeff
5 changed files with 173 additions and 37 deletions

View File

@ -344,10 +344,10 @@ bool isNoAliasCall(const Value *V);
/// identifiable object. This returns true for: /// identifiable object. This returns true for:
/// Global Variables and Functions (but not Global Aliases) /// Global Variables and Functions (but not Global Aliases)
/// Allocas and Mallocs /// Allocas and Mallocs
/// ByVal and NoAlias Arguments /// ByVal and NoAlias Arguments, if Interprocedural is false
/// NoAlias returns /// NoAlias returns, if Interprocedural is false
/// ///
bool isIdentifiedObject(const Value *V); bool isIdentifiedObject(const Value *V, bool Interprocedural = false);
} // End llvm namespace } // End llvm namespace

View File

@ -71,6 +71,14 @@ namespace llvm {
// //
ImmutablePass *createBasicAliasAnalysisPass(); ImmutablePass *createBasicAliasAnalysisPass();
//===--------------------------------------------------------------------===//
//
// createInterproceduralBasicAliasAnalysisPass - This pass is similar to
// baiscaa, except that it properly supports queries to values which live
// in different functions.
//
ImmutablePass *createInterproceduralBasicAliasAnalysisPass();
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// //
/// createLibCallAliasAnalysisPass - Create an alias analysis pass that knows /// createLibCallAliasAnalysisPass - Create an alias analysis pass that knows

View File

@ -229,16 +229,20 @@ bool llvm::isNoAliasCall(const Value *V) {
/// identifiable object. This returns true for: /// identifiable object. This returns true for:
/// Global Variables and Functions (but not Global Aliases) /// Global Variables and Functions (but not Global Aliases)
/// Allocas and Mallocs /// Allocas and Mallocs
/// ByVal and NoAlias Arguments /// ByVal and NoAlias Arguments, if Interprocedural is false
/// NoAlias returns /// NoAlias returns, if Interprocedural is false
/// ///
bool llvm::isIdentifiedObject(const Value *V) { bool llvm::isIdentifiedObject(const Value *V, bool Interprocedural) {
if (isa<AllocaInst>(V) || isNoAliasCall(V)) if (isa<AllocaInst>(V))
return true; return true;
if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
return true; return true;
if (!Interprocedural) {
if (isNoAliasCall(V))
return true;
if (const Argument *A = dyn_cast<Argument>(V)) if (const Argument *A = dyn_cast<Argument>(V))
return A->hasNoAliasAttr() || A->hasByValAttr(); return A->hasNoAliasAttr() || A->hasByValAttr();
}
return false; return false;
} }

View File

@ -55,9 +55,10 @@ static bool isKnownNonNull(const Value *V) {
/// isNonEscapingLocalObject - Return true if the pointer is to a function-local /// isNonEscapingLocalObject - Return true if the pointer is to a function-local
/// object that never escapes from the function. /// object that never escapes from the function.
static bool isNonEscapingLocalObject(const Value *V) { static bool isNonEscapingLocalObject(const Value *V, bool Interprocedural) {
// If this is a local allocation, check to see if it escapes. // If this is a local allocation, check to see if it escapes.
if (isa<AllocaInst>(V) || isNoAliasCall(V)) if (isa<AllocaInst>(V) ||
(!Interprocedural && isNoAliasCall(V)))
// Set StoreCaptures to True so that we can assume in our callers that the // Set StoreCaptures to True so that we can assume in our callers that the
// pointer is not the result of a load instruction. Currently // pointer is not the result of a load instruction. Currently
// PointerMayBeCaptured doesn't have any special analysis for the // PointerMayBeCaptured doesn't have any special analysis for the
@ -68,6 +69,7 @@ static bool isNonEscapingLocalObject(const Value *V) {
// If this is an argument that corresponds to a byval or noalias argument, // 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 // then it has not escaped before entering the function. Check if it escapes
// inside the function. // inside the function.
if (!Interprocedural)
if (const Argument *A = dyn_cast<Argument>(V)) if (const Argument *A = dyn_cast<Argument>(V))
if (A->hasByValAttr() || A->hasNoAliasAttr()) { if (A->hasByValAttr() || A->hasNoAliasAttr()) {
// Don't bother analyzing arguments already known not to escape. // Don't bother analyzing arguments already known not to escape.
@ -78,6 +80,21 @@ static bool isNonEscapingLocalObject(const Value *V) {
return false; return false;
} }
/// isEscapeSource - Return true if the pointer is one which would have
/// been considered an escape by isNonEscapingLocalObject.
static bool isEscapeSource(const Value *V, bool Interprocedural) {
if (!Interprocedural)
if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V))
return true;
// The load case works because isNonEscapingLocalObject considers all
// stores to be escapes (it passes true for the StoreCaptures argument
// to PointerMayBeCaptured).
if (isa<LoadInst>(V))
return true;
return false;
}
/// isObjectSmallerThan - Return true if we can prove that the object specified /// isObjectSmallerThan - Return true if we can prove that the object specified
/// by V is smaller than Size. /// by V is smaller than Size.
@ -177,19 +194,51 @@ static RegisterAnalysisGroup<AliasAnalysis> V(U);
ImmutablePass *llvm::createNoAAPass() { return new NoAA(); } ImmutablePass *llvm::createNoAAPass() { return new NoAA(); }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// BasicAA Pass // BasicAliasAnalysis Pass
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
static const Function *getParent(const Value *V) {
if(const Instruction *inst = dyn_cast<Instruction>(V))
return inst->getParent()->getParent();
if(const Argument *arg = dyn_cast<Argument>(V))
return arg->getParent();
return NULL;
}
static bool sameParent(const Value *O1, const Value *O2) {
const Function *F1 = getParent(O1);
const Function *F2 = getParent(O2);
return !F1 || !F2 || F1 == F2;
}
namespace { namespace {
/// BasicAliasAnalysis - This is the default alias analysis implementation. /// BasicAliasAnalysis - This is the default alias analysis implementation.
/// Because it doesn't chain to a previous alias analysis (like -no-aa), it /// Because it doesn't chain to a previous alias analysis (like -no-aa), it
/// derives from the NoAA class. /// derives from the NoAA class.
struct BasicAliasAnalysis : public NoAA { struct BasicAliasAnalysis : public NoAA {
/// Interprocedural - Flag for "interprocedural" mode, where we must
/// support queries of values which live in different functions.
bool Interprocedural;
static char ID; // Class identification, replacement for typeinfo static char ID; // Class identification, replacement for typeinfo
BasicAliasAnalysis() : NoAA(&ID) {} BasicAliasAnalysis()
: NoAA(&ID), Interprocedural(false) {}
BasicAliasAnalysis(void *PID, bool interprocedural)
: NoAA(PID), Interprocedural(interprocedural) {}
AliasResult alias(const Value *V1, unsigned V1Size, AliasResult alias(const Value *V1, unsigned V1Size,
const Value *V2, unsigned V2Size) { const Value *V2, unsigned V2Size) {
assert(Visited.empty() && "Visited must be cleared after use!"); assert(Visited.empty() && "Visited must be cleared after use!");
#ifdef XDEBUG
assert((Interprocedural || sameParent(V1, V2)) &&
"BasicAliasAnalysis (-basicaa) doesn't support interprocedural "
"queries; use InterproceduralAliasAnalysis "
"(-interprocedural-basic-aa) instead.");
#endif
AliasResult Alias = aliasCheck(V1, V1Size, V2, V2Size); AliasResult Alias = aliasCheck(V1, V1Size, V2, V2Size);
Visited.clear(); Visited.clear();
return Alias; return Alias;
@ -284,7 +333,7 @@ BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
// then the call can not mod/ref the pointer unless the call takes the pointer // then the call can not mod/ref the pointer unless the call takes the pointer
// as an argument, and itself doesn't capture it. // as an argument, and itself doesn't capture it.
if (!isa<Constant>(Object) && CS.getInstruction() != Object && if (!isa<Constant>(Object) && CS.getInstruction() != Object &&
isNonEscapingLocalObject(Object)) { isNonEscapingLocalObject(Object, Interprocedural)) {
bool PassedAsArg = false; bool PassedAsArg = false;
unsigned ArgNo = 0; unsigned ArgNo = 0;
for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
@ -705,15 +754,22 @@ BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size,
if (O1 != O2) { if (O1 != O2) {
// If V1/V2 point to two different objects we know that we have no alias. // If V1/V2 point to two different objects we know that we have no alias.
if (isIdentifiedObject(O1) && isIdentifiedObject(O2)) if (isIdentifiedObject(O1, Interprocedural) &&
isIdentifiedObject(O2, Interprocedural))
return NoAlias; return NoAlias;
// Constant pointers can't alias with non-const isIdentifiedObject objects. // Constant pointers can't alias with non-const isIdentifiedObject objects.
if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) || if ((isa<Constant>(O1) &&
(isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1))) isIdentifiedObject(O2, Interprocedural) &&
!isa<Constant>(O2)) ||
(isa<Constant>(O2) &&
isIdentifiedObject(O1, Interprocedural) &&
!isa<Constant>(O1)))
return NoAlias; return NoAlias;
// Arguments can't alias with local allocations or noalias calls. // Arguments can't alias with local allocations or noalias calls, unless
// we have to consider interprocedural aliasing.
if (!Interprocedural)
if ((isa<Argument>(O1) && (isa<AllocaInst>(O2) || isNoAliasCall(O2))) || if ((isa<Argument>(O1) && (isa<AllocaInst>(O2) || isNoAliasCall(O2))) ||
(isa<Argument>(O2) && (isa<AllocaInst>(O1) || isNoAliasCall(O1)))) (isa<Argument>(O2) && (isa<AllocaInst>(O1) || isNoAliasCall(O1))))
return NoAlias; return NoAlias;
@ -733,17 +789,13 @@ BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size,
// If one pointer is the result of a call/invoke or load and the other is a // If one pointer is the result of a call/invoke or load and the other is a
// non-escaping local object, then we know the object couldn't escape to a // non-escaping local object, then we know the object couldn't escape to a
// point where the call could return it. The load case works because // point where the call could return it.
// isNonEscapingLocalObject considers all stores to be escapes (it
// passes true for the StoreCaptures argument to PointerMayBeCaptured).
if (O1 != O2) { if (O1 != O2) {
if ((isa<CallInst>(O1) || isa<InvokeInst>(O1) || isa<LoadInst>(O1) || if (isEscapeSource(O1, Interprocedural) &&
isa<Argument>(O1)) && isNonEscapingLocalObject(O2, Interprocedural))
isNonEscapingLocalObject(O2))
return NoAlias; return NoAlias;
if ((isa<CallInst>(O2) || isa<InvokeInst>(O2) || isa<LoadInst>(O2) || if (isEscapeSource(O2, Interprocedural) &&
isa<Argument>(O2)) && isNonEscapingLocalObject(O1, Interprocedural))
isNonEscapingLocalObject(O1))
return NoAlias; return NoAlias;
} }
@ -776,3 +828,33 @@ BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size,
// Make sure that anything that uses AliasAnalysis pulls in this file. // Make sure that anything that uses AliasAnalysis pulls in this file.
DEFINING_FILE_FOR(BasicAliasAnalysis) DEFINING_FILE_FOR(BasicAliasAnalysis)
//===----------------------------------------------------------------------===//
// InterproceduralBasicAliasAnalysis Pass
//===----------------------------------------------------------------------===//
namespace {
/// InterproceduralBasicAliasAnalysis - This is similar to basicaa, except
/// that it properly supports queries to values which live in different
/// functions.
///
/// Note that we don't currently take this to the extreme, analyzing all
/// call sites of a function to answer a query about an Argument.
///
struct InterproceduralBasicAliasAnalysis : public BasicAliasAnalysis {
static char ID; // Class identification, replacement for typeinfo
InterproceduralBasicAliasAnalysis() : BasicAliasAnalysis(&ID, true) {}
};
}
// Register this pass...
char InterproceduralBasicAliasAnalysis::ID = 0;
static RegisterPass<InterproceduralBasicAliasAnalysis>
W("interprocedural-basic-aa", "Interprocedural Basic Alias Analysis", false, true);
// Declare that we implement the AliasAnalysis interface
static RegisterAnalysisGroup<AliasAnalysis> Z(W);
ImmutablePass *llvm::createInterproceduralBasicAliasAnalysisPass() {
return new InterproceduralBasicAliasAnalysis();
}

View File

@ -0,0 +1,42 @@
; RUN: opt -interprocedural-basic-aa -interprocedural-aa-eval -print-all-alias-modref-info -disable-output < %s |& FileCheck %s
; The noalias attribute is not safe in an interprocedural context.
; CHECK: MayAlias: i8* %p, i8* %q
define void @t0(i8* noalias %p) {
store i8 0, i8* %p
ret void
}
define void @t1(i8* noalias %q) {
store i8 0, i8* %q
ret void
}
; An alloca can alias an argument in a different function.
; CHECK: MayAlias: i32* %r, i32* %s
define void @s0(i32* %r) {
store i32 0, i32* %r
ret void
}
define void @s1() {
%s = alloca i32, i32 10
store i32 0, i32* %s
call void @s0(i32* %s)
ret void
}
; An alloca can alias an argument in a recursive function.
; CHECK: MayAlias: i64* %t, i64* %u
; CHECK: MayAlias: i64* %u, i64* %v
; CHECK: MayAlias: i64* %t, i64* %v
define i64* @r0(i64* %u) {
%t = alloca i64, i32 10
%v = call i64* @r0(i64* %t)
store i64 0, i64* %t
store i64 0, i64* %u
store i64 0, i64* %v
ret i64* %t
}