1 //===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ScopedNoAlias alias-analysis pass, which implements
11 // metadata-based scoped no-alias support.
13 // Alias-analysis scopes are defined by an id (which can be a string or some
14 // other metadata node), a domain node, and an optional descriptive string.
15 // A domain is defined by an id (which can be a string or some other metadata
16 // node), and an optional descriptive string.
18 // !dom0 = metadata !{ metadata !"domain of foo()" }
19 // !scope1 = metadata !{ metadata !scope1, metadata !dom0, metadata !"scope 1" }
20 // !scope2 = metadata !{ metadata !scope2, metadata !dom0, metadata !"scope 2" }
22 // Loads and stores can be tagged with an alias-analysis scope, and also, with
23 // a noalias tag for a specific scope:
25 // ... = load %ptr1, !alias.scope !{ !scope1 }
26 // ... = load %ptr2, !alias.scope !{ !scope1, !scope2 }, !noalias !{ !scope1 }
28 // When evaluating an aliasing query, if one of the instructions is associated
29 // has a set of noalias scopes in some domain that is superset of the alias
30 // scopes in that domain of some other instruction, then the two memory
31 // accesses are assumed not to alias.
33 //===----------------------------------------------------------------------===//
35 #include "llvm/Analysis/ScopedNoAliasAA.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/Analysis/TargetLibraryInfo.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/LLVMContext.h"
40 #include "llvm/IR/Metadata.h"
41 #include "llvm/IR/Module.h"
42 #include "llvm/Pass.h"
43 #include "llvm/Support/CommandLine.h"
46 // A handy option for disabling scoped no-alias functionality. The same effect
47 // can also be achieved by stripping the associated metadata tags from IR, but
48 // this option is sometimes more convenient.
49 static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
53 /// AliasScopeNode - This is a simple wrapper around an MDNode which provides
54 /// a higher-level interface by hiding the details of how alias analysis
55 /// information is encoded in its operands.
56 class AliasScopeNode {
60 AliasScopeNode() : Node(0) {}
61 explicit AliasScopeNode(const MDNode *N) : Node(N) {}
63 /// getNode - Get the MDNode for this AliasScopeNode.
64 const MDNode *getNode() const { return Node; }
66 /// getDomain - Get the MDNode for this AliasScopeNode's domain.
67 const MDNode *getDomain() const {
68 if (Node->getNumOperands() < 2)
70 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
73 } // End of anonymous namespace
75 AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
76 const MemoryLocation &LocB) {
77 if (!EnableScopedNoAlias)
78 return AAResultBase::alias(LocA, LocB);
80 // Get the attached MDNodes.
81 const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
83 const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
85 if (!mayAliasInScopes(AScopes, BNoAlias))
88 if (!mayAliasInScopes(BScopes, ANoAlias))
91 // If they may alias, chain to the next AliasAnalysis.
92 return AAResultBase::alias(LocA, LocB);
95 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS,
96 const MemoryLocation &Loc) {
97 if (!EnableScopedNoAlias)
98 return AAResultBase::getModRefInfo(CS, Loc);
100 if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
101 LLVMContext::MD_noalias)))
104 if (!mayAliasInScopes(
105 CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
109 return AAResultBase::getModRefInfo(CS, Loc);
112 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS1,
113 ImmutableCallSite CS2) {
114 if (!EnableScopedNoAlias)
115 return AAResultBase::getModRefInfo(CS1, CS2);
117 if (!mayAliasInScopes(
118 CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
119 CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
122 if (!mayAliasInScopes(
123 CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
124 CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
127 return AAResultBase::getModRefInfo(CS1, CS2);
130 void ScopedNoAliasAAResult::collectMDInDomain(
131 const MDNode *List, const MDNode *Domain,
132 SmallPtrSetImpl<const MDNode *> &Nodes) const {
133 for (unsigned i = 0, ie = List->getNumOperands(); i != ie; ++i)
134 if (const MDNode *MD = dyn_cast<MDNode>(List->getOperand(i)))
135 if (AliasScopeNode(MD).getDomain() == Domain)
139 bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
140 const MDNode *NoAlias) const {
141 if (!Scopes || !NoAlias)
144 // Collect the set of scope domains relevant to the noalias scopes.
145 SmallPtrSet<const MDNode *, 16> Domains;
146 for (unsigned i = 0, ie = NoAlias->getNumOperands(); i != ie; ++i)
147 if (const MDNode *NAMD = dyn_cast<MDNode>(NoAlias->getOperand(i)))
148 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
149 Domains.insert(Domain);
151 // We alias unless, for some domain, the set of noalias scopes in that domain
152 // is a superset of the set of alias scopes in that domain.
153 for (const MDNode *Domain : Domains) {
154 SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes;
155 collectMDInDomain(NoAlias, Domain, NANodes);
156 collectMDInDomain(Scopes, Domain, ScopeNodes);
157 if (!ScopeNodes.size())
160 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
161 bool FoundAll = true;
162 for (const MDNode *SMD : ScopeNodes)
163 if (!NANodes.count(SMD)) {
175 ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
176 AnalysisManager<Function> *AM) {
177 return ScopedNoAliasAAResult(AM->getResult<TargetLibraryAnalysis>(F));
180 char ScopedNoAliasAA::PassID;
182 char ScopedNoAliasAAWrapperPass::ID = 0;
183 INITIALIZE_PASS_BEGIN(ScopedNoAliasAAWrapperPass, "scoped-noalias",
184 "Scoped NoAlias Alias Analysis", false, true)
185 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
186 INITIALIZE_PASS_END(ScopedNoAliasAAWrapperPass, "scoped-noalias",
187 "Scoped NoAlias Alias Analysis", false, true)
189 ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
190 return new ScopedNoAliasAAWrapperPass();
193 ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {
194 initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry());
197 bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
198 Result.reset(new ScopedNoAliasAAResult(
199 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
203 bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
208 void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
209 AU.setPreservesAll();
210 AU.addRequired<TargetLibraryInfoWrapperPass>();