blob: 32a1e558437a9505fed29134bc91f87655cc9dc7 [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.
Chandler Carruth496b7fb2015-08-14 02:46:07 +000049static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
50 cl::init(true));
Hal Finkel94146652014-07-24 14:25:39 +000051
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
Mehdi Amini46a43552015-03-04 18:43:29 +000083 bool doInitialization(Module &M) override;
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)
Chandler Carruth496b7fb2015-08-14 02:46:07 +000091 return (AliasAnalysis *)this;
Hal Finkel94146652014-07-24 14:25:39 +000092 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;
Chandler Carruthac80dc72015-06-17 07:18:54 +0000102 AliasResult alias(const MemoryLocation &LocA,
103 const MemoryLocation &LocB) override;
104 bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal) override;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000105 FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
106 FunctionModRefBehavior getModRefBehavior(const Function *F) override;
107 ModRefInfo getModRefInfo(ImmutableCallSite CS,
108 const MemoryLocation &Loc) override;
109 ModRefInfo getModRefInfo(ImmutableCallSite CS1,
110 ImmutableCallSite CS2) override;
Hal Finkel94146652014-07-24 14:25:39 +0000111};
Chandler Carruth496b7fb2015-08-14 02:46:07 +0000112} // End of anonymous namespace
Hal Finkel94146652014-07-24 14:25:39 +0000113
114// Register this pass...
115char ScopedNoAliasAA::ID = 0;
116INITIALIZE_AG_PASS(ScopedNoAliasAA, AliasAnalysis, "scoped-noalias",
117 "Scoped NoAlias Alias Analysis", false, true, false)
118
119ImmutablePass *llvm::createScopedNoAliasAAPass() {
120 return new ScopedNoAliasAA();
121}
122
Mehdi Amini46a43552015-03-04 18:43:29 +0000123bool ScopedNoAliasAA::doInitialization(Module &M) {
124 InitializeAliasAnalysis(this, &M.getDataLayout());
125 return true;
126}
127
Chandler Carruth496b7fb2015-08-14 02:46:07 +0000128void ScopedNoAliasAA::getAnalysisUsage(AnalysisUsage &AU) const {
Hal Finkel94146652014-07-24 14:25:39 +0000129 AU.setPreservesAll();
130 AliasAnalysis::getAnalysisUsage(AU);
131}
132
Chandler Carruth496b7fb2015-08-14 02:46:07 +0000133void ScopedNoAliasAA::collectMDInDomain(
134 const MDNode *List, const MDNode *Domain,
135 SmallPtrSetImpl<const MDNode *> &Nodes) const {
Hal Finkel029cde62014-07-25 15:50:02 +0000136 for (unsigned i = 0, ie = List->getNumOperands(); i != ie; ++i)
137 if (const MDNode *MD = dyn_cast<MDNode>(List->getOperand(i)))
138 if (AliasScopeNode(MD).getDomain() == Domain)
139 Nodes.insert(MD);
Hal Finkel94146652014-07-24 14:25:39 +0000140}
141
Chandler Carruth496b7fb2015-08-14 02:46:07 +0000142bool ScopedNoAliasAA::mayAliasInScopes(const MDNode *Scopes,
143 const MDNode *NoAlias) const {
Hal Finkel94146652014-07-24 14:25:39 +0000144 if (!Scopes || !NoAlias)
145 return true;
146
Hal Finkel029cde62014-07-25 15:50:02 +0000147 // Collect the set of scope domains relevant to the noalias scopes.
148 SmallPtrSet<const MDNode *, 16> Domains;
149 for (unsigned i = 0, ie = NoAlias->getNumOperands(); i != ie; ++i)
150 if (const MDNode *NAMD = dyn_cast<MDNode>(NoAlias->getOperand(i)))
151 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
152 Domains.insert(Domain);
Hal Finkel94146652014-07-24 14:25:39 +0000153
Hal Finkel029cde62014-07-25 15:50:02 +0000154 // We alias unless, for some domain, the set of noalias scopes in that domain
155 // is a superset of the set of alias scopes in that domain.
156 for (const MDNode *Domain : Domains) {
157 SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes;
158 collectMDInDomain(NoAlias, Domain, NANodes);
159 collectMDInDomain(Scopes, Domain, ScopeNodes);
160 if (!ScopeNodes.size())
161 continue;
162
163 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
164 bool FoundAll = true;
165 for (const MDNode *SMD : ScopeNodes)
166 if (!NANodes.count(SMD)) {
167 FoundAll = false;
168 break;
169 }
170
171 if (FoundAll)
172 return false;
173 }
174
175 return true;
Hal Finkel94146652014-07-24 14:25:39 +0000176}
177
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000178AliasResult ScopedNoAliasAA::alias(const MemoryLocation &LocA,
179 const MemoryLocation &LocB) {
Hal Finkel94146652014-07-24 14:25:39 +0000180 if (!EnableScopedNoAlias)
181 return AliasAnalysis::alias(LocA, LocB);
182
183 // Get the attached MDNodes.
Chandler Carruth496b7fb2015-08-14 02:46:07 +0000184 const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
Hal Finkel94146652014-07-24 14:25:39 +0000185
Chandler Carruth496b7fb2015-08-14 02:46:07 +0000186 const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
Hal Finkel94146652014-07-24 14:25:39 +0000187
188 if (!mayAliasInScopes(AScopes, BNoAlias))
189 return NoAlias;
190
191 if (!mayAliasInScopes(BScopes, ANoAlias))
192 return NoAlias;
193
194 // If they may alias, chain to the next AliasAnalysis.
195 return AliasAnalysis::alias(LocA, LocB);
196}
197
Chandler Carruthac80dc72015-06-17 07:18:54 +0000198bool ScopedNoAliasAA::pointsToConstantMemory(const MemoryLocation &Loc,
Hal Finkel94146652014-07-24 14:25:39 +0000199 bool OrLocal) {
200 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
201}
202
Chandler Carruth194f59c2015-07-22 23:15:57 +0000203FunctionModRefBehavior
Hal Finkel94146652014-07-24 14:25:39 +0000204ScopedNoAliasAA::getModRefBehavior(ImmutableCallSite CS) {
205 return AliasAnalysis::getModRefBehavior(CS);
206}
207
Chandler Carruth194f59c2015-07-22 23:15:57 +0000208FunctionModRefBehavior ScopedNoAliasAA::getModRefBehavior(const Function *F) {
Hal Finkel94146652014-07-24 14:25:39 +0000209 return AliasAnalysis::getModRefBehavior(F);
210}
211
Chandler Carruth194f59c2015-07-22 23:15:57 +0000212ModRefInfo ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS,
213 const MemoryLocation &Loc) {
Hal Finkel94146652014-07-24 14:25:39 +0000214 if (!EnableScopedNoAlias)
215 return AliasAnalysis::getModRefInfo(CS, Loc);
216
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000217 if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
Duncan P. N. Exon Smith3872d002014-11-01 00:10:31 +0000218 LLVMContext::MD_noalias)))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000219 return MRI_NoModRef;
Hal Finkel94146652014-07-24 14:25:39 +0000220
221 if (!mayAliasInScopes(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000222 CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
Duncan P. N. Exon Smith3872d002014-11-01 00:10:31 +0000223 Loc.AATags.NoAlias))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000224 return MRI_NoModRef;
Hal Finkel94146652014-07-24 14:25:39 +0000225
226 return AliasAnalysis::getModRefInfo(CS, Loc);
227}
228
Chandler Carruth194f59c2015-07-22 23:15:57 +0000229ModRefInfo ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS1,
230 ImmutableCallSite CS2) {
Hal Finkel94146652014-07-24 14:25:39 +0000231 if (!EnableScopedNoAlias)
232 return AliasAnalysis::getModRefInfo(CS1, CS2);
233
234 if (!mayAliasInScopes(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000235 CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
236 CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000237 return MRI_NoModRef;
Hal Finkel94146652014-07-24 14:25:39 +0000238
239 if (!mayAliasInScopes(
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000240 CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
241 CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
Chandler Carruth194f59c2015-07-22 23:15:57 +0000242 return MRI_NoModRef;
Hal Finkel94146652014-07-24 14:25:39 +0000243
244 return AliasAnalysis::getModRefInfo(CS1, CS2);
245}
246