blob: c6ea3af3d0c34906686317a123fe2fa8f29ab04e [file] [log] [blame]
Hal Finkel94146652014-07-24 14:25:39 +00001//===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ScopedNoAlias alias-analysis pass, which implements
11// metadata-based scoped no-alias support.
12//
Hal Finkel029cde62014-07-25 15:50:02 +000013// 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.
Hal Finkel94146652014-07-24 14:25:39 +000017//
Hal Finkel029cde62014-07-25 15:50:02 +000018// !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" }
Hal Finkel94146652014-07-24 14:25:39 +000021//
22// Loads and stores can be tagged with an alias-analysis scope, and also, with
23// a noalias tag for a specific scope:
24//
25// ... = load %ptr1, !alias.scope !{ !scope1 }
26// ... = load %ptr2, !alias.scope !{ !scope1, !scope2 }, !noalias !{ !scope1 }
27//
28// When evaluating an aliasing query, if one of the instructions is associated
Hal Finkel029cde62014-07-25 15:50:02 +000029// 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
Hal Finkel94146652014-07-24 14:25:39 +000031// accesses are assumed not to alias.
32//
Hal Finkel94146652014-07-24 14:25:39 +000033//===----------------------------------------------------------------------===//
34
Hal Finkel029cde62014-07-25 15:50:02 +000035#include "llvm/ADT/SmallPtrSet.h"
Hal Finkel94146652014-07-24 14:25:39 +000036#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000037#include "llvm/Analysis/Passes.h"
Hal Finkel94146652014-07-24 14:25:39 +000038#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"
44using namespace llvm;
45
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.
49static cl::opt<bool>
50EnableScopedNoAlias("enable-scoped-noalias", cl::init(true));
51
52namespace {
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.
56class AliasScopeNode {
57 const MDNode *Node;
58
59public:
60 AliasScopeNode() : Node(0) {}
61 explicit AliasScopeNode(const MDNode *N) : Node(N) {}
62
63 /// getNode - Get the MDNode for this AliasScopeNode.
64 const MDNode *getNode() const { return Node; }
65
Hal Finkel029cde62014-07-25 15:50:02 +000066 /// getDomain - Get the MDNode for this AliasScopeNode's domain.
67 const MDNode *getDomain() const {
Hal Finkel94146652014-07-24 14:25:39 +000068 if (Node->getNumOperands() < 2)
Hal Finkel029cde62014-07-25 15:50:02 +000069 return nullptr;
70 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
Hal Finkel94146652014-07-24 14:25:39 +000071 }
72};
73
74/// ScopedNoAliasAA - This is a simple alias analysis
75/// implementation that uses scoped-noalias metadata to answer queries.
76class ScopedNoAliasAA : public ImmutablePass, public AliasAnalysis {
77public:
78 static char ID; // Class identification, replacement for typeinfo
79 ScopedNoAliasAA() : ImmutablePass(ID) {
80 initializeScopedNoAliasAAPass(*PassRegistry::getPassRegistry());
81 }
82
Benjamin Kramer8c90fd72014-09-03 11:41:21 +000083 void initializePass() override { InitializeAliasAnalysis(this); }
Hal Finkel94146652014-07-24 14:25:39 +000084
85 /// getAdjustedAnalysisPointer - This method is used when a pass implements
86 /// an analysis interface through multiple inheritance. If needed, it
87 /// should override this to adjust the this pointer as needed for the
88 /// specified pass info.
Benjamin Kramer8c90fd72014-09-03 11:41:21 +000089 void *getAdjustedAnalysisPointer(const void *PI) override {
Hal Finkel94146652014-07-24 14:25:39 +000090 if (PI == &AliasAnalysis::ID)
91 return (AliasAnalysis*)this;
92 return this;
93 }
94
95protected:
Hal Finkel94146652014-07-24 14:25:39 +000096 bool mayAliasInScopes(const MDNode *Scopes, const MDNode *NoAlias) const;
Hal Finkel029cde62014-07-25 15:50:02 +000097 void collectMDInDomain(const MDNode *List, const MDNode *Domain,
98 SmallPtrSetImpl<const MDNode *> &Nodes) const;
Hal Finkel94146652014-07-24 14:25:39 +000099
100private:
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000101 void getAnalysisUsage(AnalysisUsage &AU) const override;
102 AliasResult alias(const Location &LocA, const Location &LocB) override;
103 bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override;
104 ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
105 ModRefBehavior getModRefBehavior(const Function *F) override;
106 ModRefResult getModRefInfo(ImmutableCallSite CS,
107 const Location &Loc) override;
108 ModRefResult getModRefInfo(ImmutableCallSite CS1,
109 ImmutableCallSite CS2) override;
Hal Finkel94146652014-07-24 14:25:39 +0000110};
111} // End of anonymous namespace
112
113// Register this pass...
114char ScopedNoAliasAA::ID = 0;
115INITIALIZE_AG_PASS(ScopedNoAliasAA, AliasAnalysis, "scoped-noalias",
116 "Scoped NoAlias Alias Analysis", false, true, false)
117
118ImmutablePass *llvm::createScopedNoAliasAAPass() {
119 return new ScopedNoAliasAA();
120}
121
122void
123ScopedNoAliasAA::getAnalysisUsage(AnalysisUsage &AU) const {
124 AU.setPreservesAll();
125 AliasAnalysis::getAnalysisUsage(AU);
126}
127
Hal Finkel029cde62014-07-25 15:50:02 +0000128void
129ScopedNoAliasAA::collectMDInDomain(const MDNode *List, const MDNode *Domain,
130 SmallPtrSetImpl<const MDNode *> &Nodes) const {
131 for (unsigned i = 0, ie = List->getNumOperands(); i != ie; ++i)
132 if (const MDNode *MD = dyn_cast<MDNode>(List->getOperand(i)))
133 if (AliasScopeNode(MD).getDomain() == Domain)
134 Nodes.insert(MD);
Hal Finkel94146652014-07-24 14:25:39 +0000135}
136
137bool
138ScopedNoAliasAA::mayAliasInScopes(const MDNode *Scopes,
139 const MDNode *NoAlias) const {
140 if (!Scopes || !NoAlias)
141 return true;
142
Hal Finkel029cde62014-07-25 15:50:02 +0000143 // Collect the set of scope domains relevant to the noalias scopes.
144 SmallPtrSet<const MDNode *, 16> Domains;
145 for (unsigned i = 0, ie = NoAlias->getNumOperands(); i != ie; ++i)
146 if (const MDNode *NAMD = dyn_cast<MDNode>(NoAlias->getOperand(i)))
147 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
148 Domains.insert(Domain);
Hal Finkel94146652014-07-24 14:25:39 +0000149
Hal Finkel029cde62014-07-25 15:50:02 +0000150 // We alias unless, for some domain, the set of noalias scopes in that domain
151 // is a superset of the set of alias scopes in that domain.
152 for (const MDNode *Domain : Domains) {
153 SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes;
154 collectMDInDomain(NoAlias, Domain, NANodes);
155 collectMDInDomain(Scopes, Domain, ScopeNodes);
156 if (!ScopeNodes.size())
157 continue;
158
159 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
160 bool FoundAll = true;
161 for (const MDNode *SMD : ScopeNodes)
162 if (!NANodes.count(SMD)) {
163 FoundAll = false;
164 break;
165 }
166
167 if (FoundAll)
168 return false;
169 }
170
171 return true;
Hal Finkel94146652014-07-24 14:25:39 +0000172}
173
174AliasAnalysis::AliasResult
175ScopedNoAliasAA::alias(const Location &LocA, const Location &LocB) {
176 if (!EnableScopedNoAlias)
177 return AliasAnalysis::alias(LocA, LocB);
178
179 // Get the attached MDNodes.
180 const MDNode *AScopes = LocA.AATags.Scope,
181 *BScopes = LocB.AATags.Scope;
182
183 const MDNode *ANoAlias = LocA.AATags.NoAlias,
184 *BNoAlias = LocB.AATags.NoAlias;
185
186 if (!mayAliasInScopes(AScopes, BNoAlias))
187 return NoAlias;
188
189 if (!mayAliasInScopes(BScopes, ANoAlias))
190 return NoAlias;
191
192 // If they may alias, chain to the next AliasAnalysis.
193 return AliasAnalysis::alias(LocA, LocB);
194}
195
196bool ScopedNoAliasAA::pointsToConstantMemory(const Location &Loc,
197 bool OrLocal) {
198 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
199}
200
201AliasAnalysis::ModRefBehavior
202ScopedNoAliasAA::getModRefBehavior(ImmutableCallSite CS) {
203 return AliasAnalysis::getModRefBehavior(CS);
204}
205
206AliasAnalysis::ModRefBehavior
207ScopedNoAliasAA::getModRefBehavior(const Function *F) {
208 return AliasAnalysis::getModRefBehavior(F);
209}
210
211AliasAnalysis::ModRefResult
212ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS, const Location &Loc) {
213 if (!EnableScopedNoAlias)
214 return AliasAnalysis::getModRefInfo(CS, Loc);
215
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000216 if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
Duncan P. N. Exon Smith3872d002014-11-01 00:10:31 +0000217 LLVMContext::MD_noalias)))
Hal Finkel94146652014-07-24 14:25:39 +0000218 return NoModRef;
219
220 if (!mayAliasInScopes(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000221 CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
Duncan P. N. Exon Smith3872d002014-11-01 00:10:31 +0000222 Loc.AATags.NoAlias))
Hal Finkel94146652014-07-24 14:25:39 +0000223 return NoModRef;
224
225 return AliasAnalysis::getModRefInfo(CS, Loc);
226}
227
228AliasAnalysis::ModRefResult
229ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
230 if (!EnableScopedNoAlias)
231 return AliasAnalysis::getModRefInfo(CS1, CS2);
232
233 if (!mayAliasInScopes(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000234 CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
235 CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
Hal Finkel94146652014-07-24 14:25:39 +0000236 return NoModRef;
237
238 if (!mayAliasInScopes(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000239 CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
240 CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
Hal Finkel94146652014-07-24 14:25:39 +0000241 return NoModRef;
242
243 return AliasAnalysis::getModRefInfo(CS1, CS2);
244}
245