[CFLAA] Remove modref queries from CFLAA.

As it turns out, modref queries are broken with CFLAA. Specifically,
the data source we were using for determining modref behaviors
explicitly ignores operations on non-pointer values. So, it wouldn't
note e.g. storing an i32 to an i32* (or loading an i64 from an i64*).
It also ignores external function calls, rather than acting
conservatively for them.

(N.B. These operations, where necessary, *are* tracked by CFLAA; we just
use a different mechanism to do so. Said mechanism is relatively
imprecise, so it's unlikely that we can provide reasonably good modref
answers with it as implemented.)

Patch by Jia Chen.

Differential Revision: https://reviews.llvm.org/D22978

llvm-svn: 277366
This commit is contained in:
George Burgess IV 2016-08-01 18:47:28 +00:00
parent ec133b3d20
commit 5f0e76dca6
11 changed files with 26 additions and 227 deletions

View File

@ -53,16 +53,6 @@ public:
AliasResult query(const MemoryLocation &, const MemoryLocation &);
AliasResult alias(const MemoryLocation &, const MemoryLocation &);
/// Get the location associated with a pointer argument of a callsite.
ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx);
/// Returns the behavior when calling the given call site.
FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS);
/// Returns the behavior when calling the given function. For use when the
/// call site is not known.
FunctionModRefBehavior getModRefBehavior(const Function *F);
private:
struct FunctionHandle final : public CallbackVH {
FunctionHandle(Function *Fn, CFLAndersAAResult *Result)

View File

@ -81,16 +81,6 @@ public:
return QueryResult;
}
/// Get the location associated with a pointer argument of a callsite.
ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx);
/// Returns the behavior when calling the given call site.
FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS);
/// Returns the behavior when calling the given function. For use when the
/// call site is not known.
FunctionModRefBehavior getModRefBehavior(const Function *F);
private:
struct FunctionHandle final : public CallbackVH {
FunctionHandle(Function *Fn, CFLSteensAAResult *Result)

View File

@ -862,112 +862,6 @@ AliasResult CFLAndersAAResult::alias(const MemoryLocation &LocA,
return QueryResult;
}
ModRefInfo CFLAndersAAResult::getArgModRefInfo(ImmutableCallSite CS,
unsigned ArgIdx) {
if (auto CalledFunc = CS.getCalledFunction()) {
if (!CalledFunc->hasExactDefinition())
return MRI_ModRef;
auto &MaybeInfo = ensureCached(*CalledFunc);
if (!MaybeInfo.hasValue())
return MRI_ModRef;
auto &RetParamAttributes = MaybeInfo->getAliasSummary().RetParamAttributes;
auto &RetParamRelations = MaybeInfo->getAliasSummary().RetParamRelations;
bool ArgAttributeIsWritten =
any_of(RetParamAttributes, [ArgIdx](const ExternalAttribute &ExtAttr) {
return ExtAttr.IValue.Index == ArgIdx + 1;
});
// If the argument is unknown, escaped, or alias global, be conservative.
// FIXME: Do we really need to be conservative for AttrGlobal?
if (ArgAttributeIsWritten)
return MRI_ModRef;
bool ArgIsRead = any_of(RetParamRelations,
[ArgIdx](const ExternalRelation &ExtRelation) {
return ExtRelation.From.Index == ArgIdx + 1;
});
bool ArgIsWritten = any_of(RetParamRelations,
[ArgIdx](const ExternalRelation &ExtRelation) {
return ExtRelation.To.Index == ArgIdx + 1;
});
if (ArgIsRead)
return ArgIsWritten ? MRI_ModRef : MRI_Ref;
return ArgIsWritten ? MRI_Mod : MRI_NoModRef;
}
return MRI_ModRef;
}
FunctionModRefBehavior
CFLAndersAAResult::getModRefBehavior(ImmutableCallSite CS) {
// If we know the callee, try analyzing it
if (auto CalledFunc = CS.getCalledFunction())
return getModRefBehavior(CalledFunc);
// Otherwise, be conservative
return FMRB_UnknownModRefBehavior;
}
FunctionModRefBehavior CFLAndersAAResult::getModRefBehavior(const Function *F) {
assert(F != nullptr);
// We cannot process external functions
if (!F->hasExactDefinition())
return FMRB_UnknownModRefBehavior;
auto &MaybeInfo = ensureCached(*F);
if (!MaybeInfo.hasValue())
return FMRB_UnknownModRefBehavior;
auto &RetParamAttributes = MaybeInfo->getAliasSummary().RetParamAttributes;
auto &RetParamRelations = MaybeInfo->getAliasSummary().RetParamRelations;
// First, if any argument is marked Escpaed, Unknown or Global, anything may
// happen to them and thus we can't draw any conclusion.
// FIXME: Do we really need to be conservative for AttrGlobal?
if (!RetParamAttributes.empty())
return FMRB_UnknownModRefBehavior;
// Check if memory gets touched.
bool MemIsRead =
any_of(RetParamRelations, [](const ExternalRelation &ExtRelation) {
return ExtRelation.From.DerefLevel > 0;
});
bool MemIsWritten =
any_of(RetParamRelations, [](const ExternalRelation &ExtRelation) {
return ExtRelation.To.DerefLevel > 0;
});
if (!MemIsRead && !MemIsWritten)
return FMRB_DoesNotAccessMemory;
// Check if only argmem gets touched.
bool ArgMemIsAccessed =
all_of(RetParamRelations, [](const ExternalRelation &ExtRelation) {
return ExtRelation.From.Index > 0 && ExtRelation.From.DerefLevel <= 1 &&
ExtRelation.To.Index > 0 && ExtRelation.To.DerefLevel <= 1;
});
if (ArgMemIsAccessed)
return FMRB_OnlyAccessesArgumentPointees;
if (!MemIsWritten) {
// Check if something beyond argmem gets read.
bool ArgMemReadOnly =
all_of(RetParamRelations, [](const ExternalRelation &ExtRelation) {
return ExtRelation.From.Index > 0 && ExtRelation.From.DerefLevel <= 1;
});
return ArgMemReadOnly ? FMRB_OnlyReadsArgumentPointees
: FMRB_OnlyReadsMemory;
}
if (!MemIsRead)
return FMRB_DoesNotReadMemory;
return FMRB_UnknownModRefBehavior;
}
char CFLAndersAA::PassID;
CFLAndersAAResult CFLAndersAA::run(Function &F, AnalysisManager<Function> &AM) {

View File

@ -341,88 +341,6 @@ AliasResult CFLSteensAAResult::query(const MemoryLocation &LocA,
return NoAlias;
}
ModRefInfo CFLSteensAAResult::getArgModRefInfo(ImmutableCallSite CS,
unsigned ArgIdx) {
if (auto CalledFunc = CS.getCalledFunction()) {
if (!CalledFunc->hasExactDefinition())
return MRI_ModRef;
auto &MaybeInfo = ensureCached(const_cast<Function *>(CalledFunc));
if (!MaybeInfo.hasValue())
return MRI_ModRef;
auto &RetParamAttributes = MaybeInfo->getAliasSummary().RetParamAttributes;
auto &RetParamRelations = MaybeInfo->getAliasSummary().RetParamRelations;
bool ArgAttributeIsWritten =
std::any_of(RetParamAttributes.begin(), RetParamAttributes.end(),
[ArgIdx](const ExternalAttribute &ExtAttr) {
return ExtAttr.IValue.Index == ArgIdx + 1;
});
bool ArgIsAccessed =
std::any_of(RetParamRelations.begin(), RetParamRelations.end(),
[ArgIdx](const ExternalRelation &ExtRelation) {
return ExtRelation.To.Index == ArgIdx + 1 ||
ExtRelation.From.Index == ArgIdx + 1;
});
return (!ArgIsAccessed && !ArgAttributeIsWritten) ? MRI_NoModRef
: MRI_ModRef;
}
return MRI_ModRef;
}
FunctionModRefBehavior
CFLSteensAAResult::getModRefBehavior(ImmutableCallSite CS) {
// If we know the callee, try analyzing it
if (auto CalledFunc = CS.getCalledFunction())
return getModRefBehavior(CalledFunc);
// Otherwise, be conservative
return FMRB_UnknownModRefBehavior;
}
FunctionModRefBehavior CFLSteensAAResult::getModRefBehavior(const Function *F) {
assert(F != nullptr);
// We cannot process external functions
if (!F->hasExactDefinition())
return FMRB_UnknownModRefBehavior;
// TODO: Remove the const_cast
auto &MaybeInfo = ensureCached(const_cast<Function *>(F));
if (!MaybeInfo.hasValue())
return FMRB_UnknownModRefBehavior;
auto &RetParamAttributes = MaybeInfo->getAliasSummary().RetParamAttributes;
auto &RetParamRelations = MaybeInfo->getAliasSummary().RetParamRelations;
// First, if any argument is marked Escpaed, Unknown or Global, anything may
// happen to them and thus we can't draw any conclusion.
if (!RetParamAttributes.empty())
return FMRB_UnknownModRefBehavior;
// Currently we don't (and can't) distinguish reads from writes in
// RetParamRelations. All we can say is whether there may be memory access or
// not.
bool AccessNoMemory =
all_of(RetParamRelations, [](const ExternalRelation &ExtRelation) {
return ExtRelation.From.DerefLevel == 0 &&
ExtRelation.To.DerefLevel == 0;
});
if (AccessNoMemory)
return FMRB_DoesNotAccessMemory;
// Check if something beyond argmem gets touched.
bool AccessArgMemoryOnly =
all_of(RetParamRelations, [](const ExternalRelation &ExtRelation) {
return ExtRelation.From.Index > 0 && ExtRelation.To.Index > 0 &&
ExtRelation.From.DerefLevel <= 1 &&
ExtRelation.To.DerefLevel <= 1;
});
return AccessArgMemoryOnly ? FMRB_OnlyAccessesArgumentPointees
: FMRB_UnknownModRefBehavior;
}
char CFLSteensAA::PassID;
CFLSteensAAResult CFLSteensAA::run(Function &F, AnalysisManager<Function> &AM) {

View File

@ -12,9 +12,10 @@ define i32* @return_arg_callee(i32* %arg1, i32* %arg2) {
; CHECK: MayAlias: i32* %a, i32* %c
; CHECK: NoAlias: i32* %b, i32* %c
; CHECK: NoModRef: Ptr: i32* %a <-> %c = call i32* @return_arg_callee(i32* %a, i32* %b)
; CHECK: NoModRef: Ptr: i32* %b <-> %c = call i32* @return_arg_callee(i32* %a, i32* %b)
; CHECK: NoModRef: Ptr: i32* %c <-> %c = call i32* @return_arg_callee(i32* %a, i32* %b)
; Temporarily disable modref checks
; NoModRef: Ptr: i32* %a <-> %c = call i32* @return_arg_callee(i32* %a, i32* %b)
; NoModRef: Ptr: i32* %b <-> %c = call i32* @return_arg_callee(i32* %a, i32* %b)
; NoModRef: Ptr: i32* %c <-> %c = call i32* @return_arg_callee(i32* %a, i32* %b)
define void @test_return_arg() {
%a = alloca i32, align 4
%b = alloca i32, align 4

View File

@ -29,9 +29,10 @@ define i32* @return_deref_arg_multilevel_callee(i32*** %arg1) {
; CHECK: NoAlias: i32* %lp, i32** %lpp
; CHECK: MayAlias: i32* %lp, i32* %lpp_deref
; CHECK: Just Ref: Ptr: i32** %p <-> %c = call i32* @return_deref_arg_multilevel_callee(i32*** %pp)
; CHECK: Just Ref: Ptr: i32*** %pp <-> %c = call i32* @return_deref_arg_multilevel_callee(i32*** %pp)
; CHECK: Just Ref: Ptr: i32** %lpp <-> %c = call i32* @return_deref_arg_multilevel_callee(i32*** %pp)
; Temporarily disable modref checks
; Just Ref: Ptr: i32** %p <-> %c = call i32* @return_deref_arg_multilevel_callee(i32*** %pp)
; Just Ref: Ptr: i32*** %pp <-> %c = call i32* @return_deref_arg_multilevel_callee(i32*** %pp)
; Just Ref: Ptr: i32** %lpp <-> %c = call i32* @return_deref_arg_multilevel_callee(i32*** %pp)
define void @test_return_deref_arg_multilevel() {
%a = alloca i32, align 4

View File

@ -17,11 +17,12 @@ define i32* @return_deref_arg_callee(i32** %arg1) {
; CHECK: NoAlias: i32* %lp, i32** %p
; CHECK: MayAlias: i32* %c, i32* %lp
; CHECK: NoModRef: Ptr: i32* %a <-> %c = call i32* @return_deref_arg_callee(i32** %p)
; CHECK: NoModRef: Ptr: i32* %b <-> %c = call i32* @return_deref_arg_callee(i32** %p)
; CHECK: Just Ref: Ptr: i32** %p <-> %c = call i32* @return_deref_arg_callee(i32** %p)
; CHECK: NoModRef: Ptr: i32* %c <-> %c = call i32* @return_deref_arg_callee(i32** %p)
; CHECK: NoModRef: Ptr: i32* %lp <-> %c = call i32* @return_deref_arg_callee(i32** %p)
; Temporarily disable modref checks
; NoModRef: Ptr: i32* %a <-> %c = call i32* @return_deref_arg_callee(i32** %p)
; NoModRef: Ptr: i32* %b <-> %c = call i32* @return_deref_arg_callee(i32** %p)
; Just Ref: Ptr: i32** %p <-> %c = call i32* @return_deref_arg_callee(i32** %p)
; NoModRef: Ptr: i32* %c <-> %c = call i32* @return_deref_arg_callee(i32** %p)
; NoModRef: Ptr: i32* %lp <-> %c = call i32* @return_deref_arg_callee(i32** %p)
define void @test_return_deref_arg() {
%a = alloca i32, align 4
%b = alloca i32, align 4

View File

@ -31,8 +31,9 @@ define i32*** @return_ref_arg_multilevel_callee(i32* %arg1) {
; CHECK: NoAlias: i32* %lp, i32** %lpp
; CHECK: MayAlias: i32* %lp, i32* %lpp_deref
; CHECK: Just Mod: Ptr: i32*** %b <-> %b = call i32*** @return_ref_arg_multilevel_callee(i32* %a)
; CHECK: Just Mod: Ptr: i32** %lb <-> %b = call i32*** @return_ref_arg_multilevel_callee(i32* %a)
; Temporarily disable modref checks
; Just Mod: Ptr: i32*** %b <-> %b = call i32*** @return_ref_arg_multilevel_callee(i32* %a)
; Just Mod: Ptr: i32** %lb <-> %b = call i32*** @return_ref_arg_multilevel_callee(i32* %a)
define void @test_return_ref_arg_multilevel() {
%a = alloca i32, align 4
%p = alloca i32*, align 8

View File

@ -21,7 +21,8 @@ define i32** @return_ref_arg_callee(i32* %arg1) {
; CHECK: NoAlias: i32* %lp, i32** %b
; CHECK: MayAlias: i32* %lb, i32* %lp
; CHECK: Just Mod: Ptr: i32** %b <-> %b = call i32** @return_ref_arg_callee(i32* %a)
; Temporarily disable modref checks
; Just Mod: Ptr: i32** %b <-> %b = call i32** @return_ref_arg_callee(i32* %a)
define void @test_return_ref_arg() {
%a = alloca i32, align 4
%p = alloca i32*, align 8

View File

@ -18,10 +18,11 @@ define void @store_arg_callee(i32** %arg1, i32* %arg2) {
; CHECK: MayAlias: i32* %b, i32* %lq
; CHECK: MayAlias: i32* %lp, i32* %lq
; CHECK: NoModRef: Ptr: i32* %a <-> call void @store_arg_callee(i32** %p, i32* %b)
; CHECK: Just Ref: Ptr: i32* %b <-> call void @store_arg_callee(i32** %p, i32* %b)
; CHECK: Just Mod: Ptr: i32** %p <-> call void @store_arg_callee(i32** %p, i32* %b)
; CHECK: NoModRef: Ptr: i32** %q <-> call void @store_arg_callee(i32** %p, i32* %b)
; Temporarily disable modref checks
; NoModRef: Ptr: i32* %a <-> call void @store_arg_callee(i32** %p, i32* %b)
; Just Ref: Ptr: i32* %b <-> call void @store_arg_callee(i32** %p, i32* %b)
; Just Mod: Ptr: i32** %p <-> call void @store_arg_callee(i32** %p, i32* %b)
; NoModRef: Ptr: i32** %q <-> call void @store_arg_callee(i32** %p, i32* %b)
define void @test_store_arg() {
%a = alloca i32, align 4
%b = alloca i32, align 4

View File

@ -12,7 +12,8 @@ define i32* @return_arg_callee(i32* %arg1, i32* %arg2) {
; CHECK: MayAlias: i32* %a, i32* %c
; CHECK: NoAlias: i32* %b, i32* %c
; CHECK: NoModRef: Ptr: i32* %b <-> %c = call i32* @return_arg_callee(i32* %a, i32* %b)
; Temporarily disable modref checks
; NoModRef: Ptr: i32* %b <-> %c = call i32* @return_arg_callee(i32* %a, i32* %b)
define void @test_return_arg() {
%a = alloca i32, align 4
%b = alloca i32, align 4