#include "llvm/Analysis/ScopedNoAliasAA.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
using namespace llvm;
static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
cl::init(true), cl::Hidden);
AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
const MemoryLocation &LocB,
AAQueryInfo &AAQI) {
if (!EnableScopedNoAlias)
return AAResultBase::alias(LocA, LocB, AAQI);
const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
if (!mayAliasInScopes(AScopes, BNoAlias))
return AliasResult::NoAlias;
if (!mayAliasInScopes(BScopes, ANoAlias))
return AliasResult::NoAlias;
return AAResultBase::alias(LocA, LocB, AAQI);
}
ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (!EnableScopedNoAlias)
return AAResultBase::getModRefInfo(Call, Loc, AAQI);
if (!mayAliasInScopes(Loc.AATags.Scope,
Call->getMetadata(LLVMContext::MD_noalias)))
return ModRefInfo::NoModRef;
if (!mayAliasInScopes(Call->getMetadata(LLVMContext::MD_alias_scope),
Loc.AATags.NoAlias))
return ModRefInfo::NoModRef;
return AAResultBase::getModRefInfo(Call, Loc, AAQI);
}
ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1,
const CallBase *Call2,
AAQueryInfo &AAQI) {
if (!EnableScopedNoAlias)
return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
if (!mayAliasInScopes(Call1->getMetadata(LLVMContext::MD_alias_scope),
Call2->getMetadata(LLVMContext::MD_noalias)))
return ModRefInfo::NoModRef;
if (!mayAliasInScopes(Call2->getMetadata(LLVMContext::MD_alias_scope),
Call1->getMetadata(LLVMContext::MD_noalias)))
return ModRefInfo::NoModRef;
return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
}
static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
SmallPtrSetImpl<const MDNode *> &Nodes) {
for (const MDOperand &MDOp : List->operands())
if (const MDNode *MD = dyn_cast<MDNode>(MDOp))
if (AliasScopeNode(MD).getDomain() == Domain)
Nodes.insert(MD);
}
bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
const MDNode *NoAlias) const {
if (!Scopes || !NoAlias)
return true;
SmallPtrSet<const MDNode *, 16> Domains;
for (const MDOperand &MDOp : NoAlias->operands())
if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
Domains.insert(Domain);
for (const MDNode *Domain : Domains) {
SmallPtrSet<const MDNode *, 16> ScopeNodes;
collectMDInDomain(Scopes, Domain, ScopeNodes);
if (ScopeNodes.empty())
continue;
SmallPtrSet<const MDNode *, 16> NANodes;
collectMDInDomain(NoAlias, Domain, NANodes);
if (llvm::set_is_subset(ScopeNodes, NANodes))
return false;
}
return true;
}
AnalysisKey ScopedNoAliasAA::Key;
ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
FunctionAnalysisManager &AM) {
return ScopedNoAliasAAResult();
}
char ScopedNoAliasAAWrapperPass::ID = 0;
INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias-aa",
"Scoped NoAlias Alias Analysis", false, true)
ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
return new ScopedNoAliasAAWrapperPass();
}
ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {
initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry());
}
bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
Result.reset(new ScopedNoAliasAAResult());
return false;
}
bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
Result.reset();
return false;
}
void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
}