#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
#include "llvm/Analysis/CaptureTracking.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/Analysis/ObjCARCAliasAnalysis.h"
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
#include "llvm/Analysis/ScopedNoAliasAA.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/AtomicOrdering.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include <algorithm>
#include <cassert>
#include <functional>
#include <iterator>
#define DEBUG_TYPE "aa"
using namespace llvm;
STATISTIC(NumNoAlias, "Number of NoAlias results");
STATISTIC(NumMayAlias, "Number of MayAlias results");
STATISTIC(NumMustAlias, "Number of MustAlias results");
namespace llvm {
cl::opt<bool> DisableBasicAA("disable-basic-aa", cl::Hidden, cl::init(false));
}
#ifndef NDEBUG
static cl::opt<bool> EnableAATrace("aa-trace", cl::Hidden, cl::init(false));
#else
static const bool EnableAATrace = false;
#endif
AAResults::AAResults(AAResults &&Arg)
: TLI(Arg.TLI), AAs(std::move(Arg.AAs)), AADeps(std::move(Arg.AADeps)) {
for (auto &AA : AAs)
AA->setAAResults(this);
}
AAResults::~AAResults() {
#if 0#endif
}
bool AAResults::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &Inv) {
auto PAC = PA.getChecker<AAManager>();
if (!PAC.preservedWhenStateless())
return true;
for (AnalysisKey *ID : AADeps)
if (Inv.invalidate(ID, F, PA))
return true;
return false;
}
AliasResult AAResults::alias(const MemoryLocation &LocA,
const MemoryLocation &LocB) {
SimpleAAQueryInfo AAQIP;
return alias(LocA, LocB, AAQIP);
}
AliasResult AAResults::alias(const MemoryLocation &LocA,
const MemoryLocation &LocB, AAQueryInfo &AAQI) {
AliasResult Result = AliasResult::MayAlias;
if (EnableAATrace) {
for (unsigned I = 0; I < AAQI.Depth; ++I)
dbgs() << " ";
dbgs() << "Start " << *LocA.Ptr << " @ " << LocA.Size << ", "
<< *LocB.Ptr << " @ " << LocB.Size << "\n";
}
AAQI.Depth++;
for (const auto &AA : AAs) {
Result = AA->alias(LocA, LocB, AAQI);
if (Result != AliasResult::MayAlias)
break;
}
AAQI.Depth--;
if (EnableAATrace) {
for (unsigned I = 0; I < AAQI.Depth; ++I)
dbgs() << " ";
dbgs() << "End " << *LocA.Ptr << " @ " << LocA.Size << ", "
<< *LocB.Ptr << " @ " << LocB.Size << " = " << Result << "\n";
}
if (AAQI.Depth == 0) {
if (Result == AliasResult::NoAlias)
++NumNoAlias;
else if (Result == AliasResult::MustAlias)
++NumMustAlias;
else
++NumMayAlias;
}
return Result;
}
bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
bool OrLocal) {
SimpleAAQueryInfo AAQIP;
return pointsToConstantMemory(Loc, AAQIP, OrLocal);
}
bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
AAQueryInfo &AAQI, bool OrLocal) {
for (const auto &AA : AAs)
if (AA->pointsToConstantMemory(Loc, AAQI, OrLocal))
return true;
return false;
}
ModRefInfo AAResults::getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
ModRefInfo Result = ModRefInfo::ModRef;
for (const auto &AA : AAs) {
Result = intersectModRef(Result, AA->getArgModRefInfo(Call, ArgIdx));
if (isNoModRef(Result))
return ModRefInfo::NoModRef;
}
return Result;
}
ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(I, Call2, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2,
AAQueryInfo &AAQI) {
if (const auto *Call1 = dyn_cast<CallBase>(I)) {
return getModRefInfo(Call1, Call2, AAQI);
}
if (I->isFenceLike())
return ModRefInfo::ModRef;
const MemoryLocation DefLoc = MemoryLocation::get(I);
ModRefInfo MR = getModRefInfo(Call2, DefLoc, AAQI);
if (isModOrRefSet(MR))
return setModAndRef(MR);
return ModRefInfo::NoModRef;
}
ModRefInfo AAResults::getModRefInfo(const CallBase *Call,
const MemoryLocation &Loc) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(Call, Loc, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const CallBase *Call,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
ModRefInfo Result = ModRefInfo::ModRef;
for (const auto &AA : AAs) {
Result = intersectModRef(Result, AA->getModRefInfo(Call, Loc, AAQI));
if (isNoModRef(Result))
return ModRefInfo::NoModRef;
}
auto MRB = getModRefBehavior(Call);
if (onlyAccessesInaccessibleMem(MRB))
return ModRefInfo::NoModRef;
if (onlyReadsMemory(MRB))
Result = clearMod(Result);
else if (onlyWritesMemory(MRB))
Result = clearRef(Result);
if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) {
bool IsMustAlias = true;
ModRefInfo AllArgsMask = ModRefInfo::NoModRef;
if (doesAccessArgPointees(MRB)) {
for (const auto &I : llvm::enumerate(Call->args())) {
const Value *Arg = I.value();
if (!Arg->getType()->isPointerTy())
continue;
unsigned ArgIdx = I.index();
MemoryLocation ArgLoc =
MemoryLocation::getForArgument(Call, ArgIdx, TLI);
AliasResult ArgAlias = alias(ArgLoc, Loc, AAQI);
if (ArgAlias != AliasResult::NoAlias) {
ModRefInfo ArgMask = getArgModRefInfo(Call, ArgIdx);
AllArgsMask = unionModRef(AllArgsMask, ArgMask);
}
IsMustAlias &= (ArgAlias == AliasResult::MustAlias);
}
}
if (isNoModRef(AllArgsMask))
return ModRefInfo::NoModRef;
Result = intersectModRef(Result, AllArgsMask);
Result = IsMustAlias ? setMust(Result) : clearMust(Result);
}
if (isModSet(Result) && pointsToConstantMemory(Loc, AAQI, false))
Result = clearMod(Result);
return Result;
}
ModRefInfo AAResults::getModRefInfo(const CallBase *Call1,
const CallBase *Call2) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(Call1, Call2, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const CallBase *Call1,
const CallBase *Call2, AAQueryInfo &AAQI) {
ModRefInfo Result = ModRefInfo::ModRef;
for (const auto &AA : AAs) {
Result = intersectModRef(Result, AA->getModRefInfo(Call1, Call2, AAQI));
if (isNoModRef(Result))
return ModRefInfo::NoModRef;
}
auto Call1B = getModRefBehavior(Call1);
if (Call1B == FMRB_DoesNotAccessMemory)
return ModRefInfo::NoModRef;
auto Call2B = getModRefBehavior(Call2);
if (Call2B == FMRB_DoesNotAccessMemory)
return ModRefInfo::NoModRef;
if (onlyReadsMemory(Call1B) && onlyReadsMemory(Call2B))
return ModRefInfo::NoModRef;
if (onlyReadsMemory(Call1B))
Result = clearMod(Result);
else if (onlyWritesMemory(Call1B))
Result = clearRef(Result);
if (onlyAccessesArgPointees(Call2B)) {
if (!doesAccessArgPointees(Call2B))
return ModRefInfo::NoModRef;
ModRefInfo R = ModRefInfo::NoModRef;
bool IsMustAlias = true;
for (auto I = Call2->arg_begin(), E = Call2->arg_end(); I != E; ++I) {
const Value *Arg = *I;
if (!Arg->getType()->isPointerTy())
continue;
unsigned Call2ArgIdx = std::distance(Call2->arg_begin(), I);
auto Call2ArgLoc =
MemoryLocation::getForArgument(Call2, Call2ArgIdx, TLI);
ModRefInfo ArgModRefC2 = getArgModRefInfo(Call2, Call2ArgIdx);
ModRefInfo ArgMask = ModRefInfo::NoModRef;
if (isModSet(ArgModRefC2))
ArgMask = ModRefInfo::ModRef;
else if (isRefSet(ArgModRefC2))
ArgMask = ModRefInfo::Mod;
ModRefInfo ModRefC1 = getModRefInfo(Call1, Call2ArgLoc, AAQI);
ArgMask = intersectModRef(ArgMask, ModRefC1);
IsMustAlias &= isMustSet(ModRefC1);
R = intersectModRef(unionModRef(R, ArgMask), Result);
if (R == Result) {
if (I + 1 != E)
IsMustAlias = false;
break;
}
}
if (isNoModRef(R))
return ModRefInfo::NoModRef;
return IsMustAlias ? setMust(R) : clearMust(R);
}
if (onlyAccessesArgPointees(Call1B)) {
if (!doesAccessArgPointees(Call1B))
return ModRefInfo::NoModRef;
ModRefInfo R = ModRefInfo::NoModRef;
bool IsMustAlias = true;
for (auto I = Call1->arg_begin(), E = Call1->arg_end(); I != E; ++I) {
const Value *Arg = *I;
if (!Arg->getType()->isPointerTy())
continue;
unsigned Call1ArgIdx = std::distance(Call1->arg_begin(), I);
auto Call1ArgLoc =
MemoryLocation::getForArgument(Call1, Call1ArgIdx, TLI);
ModRefInfo ArgModRefC1 = getArgModRefInfo(Call1, Call1ArgIdx);
ModRefInfo ModRefC2 = getModRefInfo(Call2, Call1ArgLoc, AAQI);
if ((isModSet(ArgModRefC1) && isModOrRefSet(ModRefC2)) ||
(isRefSet(ArgModRefC1) && isModSet(ModRefC2)))
R = intersectModRef(unionModRef(R, ArgModRefC1), Result);
IsMustAlias &= isMustSet(ModRefC2);
if (R == Result) {
if (I + 1 != E)
IsMustAlias = false;
break;
}
}
if (isNoModRef(R))
return ModRefInfo::NoModRef;
return IsMustAlias ? setMust(R) : clearMust(R);
}
return Result;
}
FunctionModRefBehavior AAResults::getModRefBehavior(const CallBase *Call) {
FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
for (const auto &AA : AAs) {
Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(Call));
if (Result == FMRB_DoesNotAccessMemory)
return Result;
}
return Result;
}
FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
for (const auto &AA : AAs) {
Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
if (Result == FMRB_DoesNotAccessMemory)
return Result;
}
return Result;
}
raw_ostream &llvm::operator<<(raw_ostream &OS, AliasResult AR) {
switch (AR) {
case AliasResult::NoAlias:
OS << "NoAlias";
break;
case AliasResult::MustAlias:
OS << "MustAlias";
break;
case AliasResult::MayAlias:
OS << "MayAlias";
break;
case AliasResult::PartialAlias:
OS << "PartialAlias";
if (AR.hasOffset())
OS << " (off " << AR.getOffset() << ")";
break;
}
return OS;
}
ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
const MemoryLocation &Loc) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(L, Loc, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered))
return ModRefInfo::ModRef;
if (Loc.Ptr) {
AliasResult AR = alias(MemoryLocation::get(L), Loc, AAQI);
if (AR == AliasResult::NoAlias)
return ModRefInfo::NoModRef;
if (AR == AliasResult::MustAlias)
return ModRefInfo::MustRef;
}
return ModRefInfo::Ref;
}
ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
const MemoryLocation &Loc) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(S, Loc, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered))
return ModRefInfo::ModRef;
if (Loc.Ptr) {
AliasResult AR = alias(MemoryLocation::get(S), Loc, AAQI);
if (AR == AliasResult::NoAlias)
return ModRefInfo::NoModRef;
if (pointsToConstantMemory(Loc, AAQI))
return ModRefInfo::NoModRef;
if (AR == AliasResult::MustAlias)
return ModRefInfo::MustMod;
}
return ModRefInfo::Mod;
}
ModRefInfo AAResults::getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(S, Loc, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const FenceInst *S,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (Loc.Ptr && pointsToConstantMemory(Loc, AAQI))
return ModRefInfo::Ref;
return ModRefInfo::ModRef;
}
ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
const MemoryLocation &Loc) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(V, Loc, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (Loc.Ptr) {
AliasResult AR = alias(MemoryLocation::get(V), Loc, AAQI);
if (AR == AliasResult::NoAlias)
return ModRefInfo::NoModRef;
if (pointsToConstantMemory(Loc, AAQI))
return ModRefInfo::NoModRef;
if (AR == AliasResult::MustAlias)
return ModRefInfo::MustModRef;
}
return ModRefInfo::ModRef;
}
ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
const MemoryLocation &Loc) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(CatchPad, Loc, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (Loc.Ptr) {
if (pointsToConstantMemory(Loc, AAQI))
return ModRefInfo::NoModRef;
}
return ModRefInfo::ModRef;
}
ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
const MemoryLocation &Loc) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(CatchRet, Loc, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (Loc.Ptr) {
if (pointsToConstantMemory(Loc, AAQI))
return ModRefInfo::NoModRef;
}
return ModRefInfo::ModRef;
}
ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
const MemoryLocation &Loc) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(CX, Loc, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
return ModRefInfo::ModRef;
if (Loc.Ptr) {
AliasResult AR = alias(MemoryLocation::get(CX), Loc, AAQI);
if (AR == AliasResult::NoAlias)
return ModRefInfo::NoModRef;
if (AR == AliasResult::MustAlias)
return ModRefInfo::MustModRef;
}
return ModRefInfo::ModRef;
}
ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
const MemoryLocation &Loc) {
SimpleAAQueryInfo AAQIP;
return getModRefInfo(RMW, Loc, AAQIP);
}
ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (isStrongerThanMonotonic(RMW->getOrdering()))
return ModRefInfo::ModRef;
if (Loc.Ptr) {
AliasResult AR = alias(MemoryLocation::get(RMW), Loc, AAQI);
if (AR == AliasResult::NoAlias)
return ModRefInfo::NoModRef;
if (AR == AliasResult::MustAlias)
return ModRefInfo::MustModRef;
}
return ModRefInfo::ModRef;
}
ModRefInfo AAResults::getModRefInfo(const Instruction *I,
const Optional<MemoryLocation> &OptLoc,
AAQueryInfo &AAQIP) {
if (OptLoc == None) {
if (const auto *Call = dyn_cast<CallBase>(I)) {
return createModRefInfo(getModRefBehavior(Call));
}
}
const MemoryLocation &Loc = OptLoc.value_or(MemoryLocation());
switch (I->getOpcode()) {
case Instruction::VAArg:
return getModRefInfo((const VAArgInst *)I, Loc, AAQIP);
case Instruction::Load:
return getModRefInfo((const LoadInst *)I, Loc, AAQIP);
case Instruction::Store:
return getModRefInfo((const StoreInst *)I, Loc, AAQIP);
case Instruction::Fence:
return getModRefInfo((const FenceInst *)I, Loc, AAQIP);
case Instruction::AtomicCmpXchg:
return getModRefInfo((const AtomicCmpXchgInst *)I, Loc, AAQIP);
case Instruction::AtomicRMW:
return getModRefInfo((const AtomicRMWInst *)I, Loc, AAQIP);
case Instruction::Call:
case Instruction::CallBr:
case Instruction::Invoke:
return getModRefInfo((const CallBase *)I, Loc, AAQIP);
case Instruction::CatchPad:
return getModRefInfo((const CatchPadInst *)I, Loc, AAQIP);
case Instruction::CatchRet:
return getModRefInfo((const CatchReturnInst *)I, Loc, AAQIP);
default:
assert(!I->mayReadOrWriteMemory() &&
"Unhandled memory access instruction!");
return ModRefInfo::NoModRef;
}
}
ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
const MemoryLocation &MemLoc,
DominatorTree *DT,
AAQueryInfo &AAQI) {
if (!DT)
return ModRefInfo::ModRef;
const Value *Object = getUnderlyingObject(MemLoc.Ptr);
if (!isIdentifiedFunctionLocal(Object))
return ModRefInfo::ModRef;
const auto *Call = dyn_cast<CallBase>(I);
if (!Call || Call == Object)
return ModRefInfo::ModRef;
if (PointerMayBeCapturedBefore(Object, true,
true, I, DT,
true))
return ModRefInfo::ModRef;
unsigned ArgNo = 0;
ModRefInfo R = ModRefInfo::NoModRef;
bool IsMustAlias = true;
for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
CI != CE; ++CI, ++ArgNo) {
if (!(*CI)->getType()->isPointerTy() ||
(!Call->doesNotCapture(ArgNo) && ArgNo < Call->arg_size() &&
!Call->isByValArgument(ArgNo)))
continue;
AliasResult AR = alias(
MemoryLocation::getBeforeOrAfter(*CI),
MemoryLocation::getBeforeOrAfter(Object), AAQI);
if (AR != AliasResult::MustAlias)
IsMustAlias = false;
if (AR == AliasResult::NoAlias)
continue;
if (Call->doesNotAccessMemory(ArgNo))
continue;
if (Call->onlyReadsMemory(ArgNo)) {
R = ModRefInfo::Ref;
continue;
}
return ModRefInfo::ModRef;
}
return IsMustAlias ? setMust(R) : clearMust(R);
}
bool AAResults::canBasicBlockModify(const BasicBlock &BB,
const MemoryLocation &Loc) {
return canInstructionRangeModRef(BB.front(), BB.back(), Loc, ModRefInfo::Mod);
}
bool AAResults::canInstructionRangeModRef(const Instruction &I1,
const Instruction &I2,
const MemoryLocation &Loc,
const ModRefInfo Mode) {
assert(I1.getParent() == I2.getParent() &&
"Instructions not in same basic block!");
BasicBlock::const_iterator I = I1.getIterator();
BasicBlock::const_iterator E = I2.getIterator();
++E;
for (; I != E; ++I) if (isModOrRefSet(intersectModRef(getModRefInfo(&*I, Loc), Mode)))
return true;
return false;
}
AAResults::Concept::~Concept() = default;
AnalysisKey AAManager::Key;
ExternalAAWrapperPass::ExternalAAWrapperPass() : ImmutablePass(ID) {
initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
}
ExternalAAWrapperPass::ExternalAAWrapperPass(CallbackT CB)
: ImmutablePass(ID), CB(std::move(CB)) {
initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
}
char ExternalAAWrapperPass::ID = 0;
INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
false, true)
ImmutablePass *
llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
return new ExternalAAWrapperPass(std::move(Callback));
}
AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
}
char AAResultsWrapperPass::ID = 0;
INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
"Function Alias Analysis Results", false, true)
INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass)
INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
"Function Alias Analysis Results", false, true)
FunctionPass *llvm::createAAResultsWrapperPass() {
return new AAResultsWrapperPass();
}
bool AAResultsWrapperPass::runOnFunction(Function &F) {
AAR.reset(
new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F)));
if (!DisableBasicAA)
AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
AAR->addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
AAR->addAAResult(WrapperPass->getResult());
if (auto *WrapperPass =
getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
AAR->addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
AAR->addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
AAR->addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
AAR->addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
AAR->addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
if (WrapperPass->CB)
WrapperPass->CB(*this, F, *AAR);
return false;
}
void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequiredTransitive<BasicAAWrapperPass>();
AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
AU.addUsedIfAvailable<SCEVAAWrapperPass>();
AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
AU.addUsedIfAvailable<ExternalAAWrapperPass>();
}
AAManager::Result AAManager::run(Function &F, FunctionAnalysisManager &AM) {
Result R(AM.getResult<TargetLibraryAnalysis>(F));
for (auto &Getter : ResultGetters)
(*Getter)(F, AM, R);
return R;
}
AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
BasicAAResult &BAR) {
AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F));
if (!DisableBasicAA)
AAR.addAAResult(BAR);
if (auto *WrapperPass =
P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
AAR.addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
AAR.addAAResult(WrapperPass->getResult());
if (auto *WrapperPass =
P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
AAR.addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
AAR.addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
AAR.addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
AAR.addAAResult(WrapperPass->getResult());
if (auto *WrapperPass = P.getAnalysisIfAvailable<ExternalAAWrapperPass>())
if (WrapperPass->CB)
WrapperPass->CB(P, F, AAR);
return AAR;
}
bool llvm::isNoAliasCall(const Value *V) {
if (const auto *Call = dyn_cast<CallBase>(V))
return Call->hasRetAttr(Attribute::NoAlias);
return false;
}
static bool isNoAliasOrByValArgument(const Value *V) {
if (const Argument *A = dyn_cast<Argument>(V))
return A->hasNoAliasAttr() || A->hasByValAttr();
return false;
}
bool llvm::isIdentifiedObject(const Value *V) {
if (isa<AllocaInst>(V))
return true;
if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
return true;
if (isNoAliasCall(V))
return true;
if (isNoAliasOrByValArgument(V))
return true;
return false;
}
bool llvm::isIdentifiedFunctionLocal(const Value *V) {
return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasOrByValArgument(V);
}
bool llvm::isEscapeSource(const Value *V) {
if (auto *CB = dyn_cast<CallBase>(V))
return !isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(CB,
true);
if (isa<LoadInst>(V))
return true;
if (isa<IntToPtrInst>(V))
return true;
return false;
}
bool llvm::isNotVisibleOnUnwind(const Value *Object,
bool &RequiresNoCaptureBeforeUnwind) {
RequiresNoCaptureBeforeUnwind = false;
if (isa<AllocaInst>(Object))
return true;
if (auto *A = dyn_cast<Argument>(Object))
return A->hasByValAttr();
if (isNoAliasCall(Object)) {
RequiresNoCaptureBeforeUnwind = true;
return true;
}
return false;
}
void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
AU.addUsedIfAvailable<ExternalAAWrapperPass>();
}