diff options
author | George Burgess IV <george.burgess.iv@gmail.com> | 2016-07-27 23:07:07 +0000 |
---|---|---|
committer | George Burgess IV <george.burgess.iv@gmail.com> | 2016-07-27 23:07:07 +0000 |
commit | dbd35c44d4fe5e4f092ecdd2d2ca1c2205f7d2c1 (patch) | |
tree | fdc402c7eb75d5c1f48485776e1ce312f04f8e64 /llvm/lib/Analysis | |
parent | 37f4e0e096df8497c5b55d91585c1b7d98954fe2 (diff) | |
download | bcm5719-llvm-dbd35c44d4fe5e4f092ecdd2d2ca1c2205f7d2c1.tar.gz bcm5719-llvm-dbd35c44d4fe5e4f092ecdd2d2ca1c2205f7d2c1.zip |
[CFLAA] Add getModRefBehavior to CFLAnders.
This patch lets CFLAnders respond to mod-ref queries. It also includes
a small bugfix to CFLSteens.
Patch by Jia Chen.
Differential Revision: https://reviews.llvm.org/D22823
llvm-svn: 276939
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r-- | llvm/lib/Analysis/CFLAndersAliasAnalysis.cpp | 106 | ||||
-rw-r--r-- | llvm/lib/Analysis/CFLSteensAliasAnalysis.cpp | 26 |
2 files changed, 124 insertions, 8 deletions
diff --git a/llvm/lib/Analysis/CFLAndersAliasAnalysis.cpp b/llvm/lib/Analysis/CFLAndersAliasAnalysis.cpp index 5aa21d6a509..d699cbb4532 100644 --- a/llvm/lib/Analysis/CFLAndersAliasAnalysis.cpp +++ b/llvm/lib/Analysis/CFLAndersAliasAnalysis.cpp @@ -859,6 +859,112 @@ 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) { diff --git a/llvm/lib/Analysis/CFLSteensAliasAnalysis.cpp b/llvm/lib/Analysis/CFLSteensAliasAnalysis.cpp index 9b211540f4e..f1fac43a5dc 100644 --- a/llvm/lib/Analysis/CFLSteensAliasAnalysis.cpp +++ b/llvm/lib/Analysis/CFLSteensAliasAnalysis.cpp @@ -344,6 +344,9 @@ AliasResult CFLSteensAAResult::query(const MemoryLocation &LocA, 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; @@ -382,6 +385,10 @@ CFLSteensAAResult::getModRefBehavior(ImmutableCallSite CS) { 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()) @@ -397,18 +404,21 @@ FunctionModRefBehavior CFLSteensAAResult::getModRefBehavior(const Function *F) { // 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. - if (RetParamRelations.empty()) + 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 = - std::all_of(RetParamRelations.begin(), RetParamRelations.end(), - [](const ExternalRelation &ExtRelation) { - // Both DerefLevels has to be 0, since we don't know which - // one is a read and which is a write. - return ExtRelation.From.DerefLevel == 0 && - ExtRelation.To.DerefLevel == 0; - }); + 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; } |