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/ADT/SmallPtrSet.h"
36 #include "llvm/Analysis/Passes.h"
37 #include "llvm/Analysis/AliasAnalysis.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.
50 EnableScopedNoAlias("enable-scoped-noalias", cl::init(true));
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));
74 /// ScopedNoAliasAA - This is a simple alias analysis
75 /// implementation that uses scoped-noalias metadata to answer queries.
76 class ScopedNoAliasAA : public ImmutablePass, public AliasAnalysis {
78 static char ID; // Class identification, replacement for typeinfo
79 ScopedNoAliasAA() : ImmutablePass(ID) {
80 initializeScopedNoAliasAAPass(*PassRegistry::getPassRegistry());
83 virtual void initializePass() {
84 InitializeAliasAnalysis(this);
87 /// getAdjustedAnalysisPointer - This method is used when a pass implements
88 /// an analysis interface through multiple inheritance. If needed, it
89 /// should override this to adjust the this pointer as needed for the
90 /// specified pass info.
91 virtual void *getAdjustedAnalysisPointer(const void *PI) {
92 if (PI == &AliasAnalysis::ID)
93 return (AliasAnalysis*)this;
98 bool mayAliasInScopes(const MDNode *Scopes, const MDNode *NoAlias) const;
99 void collectMDInDomain(const MDNode *List, const MDNode *Domain,
100 SmallPtrSetImpl<const MDNode *> &Nodes) const;
103 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
104 virtual AliasResult alias(const Location &LocA, const Location &LocB);
105 virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
106 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
107 virtual ModRefBehavior getModRefBehavior(const Function *F);
108 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
109 const Location &Loc);
110 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
111 ImmutableCallSite CS2);
113 } // End of anonymous namespace
115 // Register this pass...
116 char ScopedNoAliasAA::ID = 0;
117 INITIALIZE_AG_PASS(ScopedNoAliasAA, AliasAnalysis, "scoped-noalias",
118 "Scoped NoAlias Alias Analysis", false, true, false)
120 ImmutablePass *llvm::createScopedNoAliasAAPass() {
121 return new ScopedNoAliasAA();
125 ScopedNoAliasAA::getAnalysisUsage(AnalysisUsage &AU) const {
126 AU.setPreservesAll();
127 AliasAnalysis::getAnalysisUsage(AU);
131 ScopedNoAliasAA::collectMDInDomain(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)
140 ScopedNoAliasAA::mayAliasInScopes(const MDNode *Scopes,
141 const MDNode *NoAlias) const {
142 if (!Scopes || !NoAlias)
145 // Collect the set of scope domains relevant to the noalias scopes.
146 SmallPtrSet<const MDNode *, 16> Domains;
147 for (unsigned i = 0, ie = NoAlias->getNumOperands(); i != ie; ++i)
148 if (const MDNode *NAMD = dyn_cast<MDNode>(NoAlias->getOperand(i)))
149 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
150 Domains.insert(Domain);
152 // We alias unless, for some domain, the set of noalias scopes in that domain
153 // is a superset of the set of alias scopes in that domain.
154 for (const MDNode *Domain : Domains) {
155 SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes;
156 collectMDInDomain(NoAlias, Domain, NANodes);
157 collectMDInDomain(Scopes, Domain, ScopeNodes);
158 if (!ScopeNodes.size())
161 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
162 bool FoundAll = true;
163 for (const MDNode *SMD : ScopeNodes)
164 if (!NANodes.count(SMD)) {
176 AliasAnalysis::AliasResult
177 ScopedNoAliasAA::alias(const Location &LocA, const Location &LocB) {
178 if (!EnableScopedNoAlias)
179 return AliasAnalysis::alias(LocA, LocB);
181 // Get the attached MDNodes.
182 const MDNode *AScopes = LocA.AATags.Scope,
183 *BScopes = LocB.AATags.Scope;
185 const MDNode *ANoAlias = LocA.AATags.NoAlias,
186 *BNoAlias = LocB.AATags.NoAlias;
188 if (!mayAliasInScopes(AScopes, BNoAlias))
191 if (!mayAliasInScopes(BScopes, ANoAlias))
194 // If they may alias, chain to the next AliasAnalysis.
195 return AliasAnalysis::alias(LocA, LocB);
198 bool ScopedNoAliasAA::pointsToConstantMemory(const Location &Loc,
200 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
203 AliasAnalysis::ModRefBehavior
204 ScopedNoAliasAA::getModRefBehavior(ImmutableCallSite CS) {
205 return AliasAnalysis::getModRefBehavior(CS);
208 AliasAnalysis::ModRefBehavior
209 ScopedNoAliasAA::getModRefBehavior(const Function *F) {
210 return AliasAnalysis::getModRefBehavior(F);
213 AliasAnalysis::ModRefResult
214 ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS, const Location &Loc) {
215 if (!EnableScopedNoAlias)
216 return AliasAnalysis::getModRefInfo(CS, Loc);
218 if (!mayAliasInScopes(Loc.AATags.Scope,
219 CS.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
222 if (!mayAliasInScopes(
223 CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
227 return AliasAnalysis::getModRefInfo(CS, Loc);
230 AliasAnalysis::ModRefResult
231 ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
232 if (!EnableScopedNoAlias)
233 return AliasAnalysis::getModRefInfo(CS1, CS2);
235 if (!mayAliasInScopes(
236 CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
237 CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
240 if (!mayAliasInScopes(
241 CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
242 CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
245 return AliasAnalysis::getModRefInfo(CS1, CS2);