77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // The LLVM Compiler Infrastructure
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // This file defines the ScopedNoAlias alias-analysis pass, which implements
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 // metadata-based scoped no-alias support.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 // Alias-analysis scopes are defined by an id (which can be a string or some
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 // other metadata node), a domain node, and an optional descriptive string.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 // A domain is defined by an id (which can be a string or some other metadata
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 // node), and an optional descriptive string.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 // !dom0 = metadata !{ metadata !"domain of foo()" }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 // !scope1 = metadata !{ metadata !scope1, metadata !dom0, metadata !"scope 1" }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 // !scope2 = metadata !{ metadata !scope2, metadata !dom0, metadata !"scope 2" }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 // Loads and stores can be tagged with an alias-analysis scope, and also, with
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 // a noalias tag for a specific scope:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 // ... = load %ptr1, !alias.scope !{ !scope1 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 // ... = load %ptr2, !alias.scope !{ !scope1, !scope2 }, !noalias !{ !scope1 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 // When evaluating an aliasing query, if one of the instructions is associated
|
100
|
29 // has a set of noalias scopes in some domain that is a superset of the alias
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 // scopes in that domain of some other instruction, then the two memory
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 // accesses are assumed not to alias.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34
|
95
|
35 #include "llvm/Analysis/ScopedNoAliasAA.h"
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 #include "llvm/ADT/SmallPtrSet.h"
|
95
|
37 #include "llvm/Analysis/TargetLibraryInfo.h"
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 #include "llvm/IR/Constants.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 #include "llvm/IR/LLVMContext.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 #include "llvm/IR/Metadata.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 #include "llvm/IR/Module.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 #include "llvm/Pass.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 #include "llvm/Support/CommandLine.h"
|
95
|
44
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 // A handy option for disabling scoped no-alias functionality. The same effect
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 // can also be achieved by stripping the associated metadata tags from IR, but
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 // this option is sometimes more convenient.
|
95
|
50 static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
|
|
51 cl::init(true));
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 namespace {
|
100
|
54 /// This is a simple wrapper around an MDNode which provides a higher-level
|
|
55 /// interface by hiding the details of how alias analysis information is encoded
|
|
56 /// in its operands.
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 class AliasScopeNode {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 const MDNode *Node;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 public:
|
95
|
61 AliasScopeNode() : Node(nullptr) {}
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 explicit AliasScopeNode(const MDNode *N) : Node(N) {}
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63
|
100
|
64 /// Get the MDNode for this AliasScopeNode.
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 const MDNode *getNode() const { return Node; }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66
|
100
|
67 /// Get the MDNode for this AliasScopeNode's domain.
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 const MDNode *getDomain() const {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 if (Node->getNumOperands() < 2)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 return nullptr;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 };
|
95
|
74 } // end of anonymous namespace
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75
|
95
|
76 AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
|
|
77 const MemoryLocation &LocB) {
|
|
78 if (!EnableScopedNoAlias)
|
|
79 return AAResultBase::alias(LocA, LocB);
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80
|
95
|
81 // Get the attached MDNodes.
|
|
82 const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83
|
95
|
84 const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
|
|
85
|
|
86 if (!mayAliasInScopes(AScopes, BNoAlias))
|
|
87 return NoAlias;
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88
|
95
|
89 if (!mayAliasInScopes(BScopes, ANoAlias))
|
|
90 return NoAlias;
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91
|
95
|
92 // If they may alias, chain to the next AliasAnalysis.
|
|
93 return AAResultBase::alias(LocA, LocB);
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
94 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
95
|
95
|
96 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS,
|
|
97 const MemoryLocation &Loc) {
|
|
98 if (!EnableScopedNoAlias)
|
|
99 return AAResultBase::getModRefInfo(CS, Loc);
|
|
100
|
|
101 if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
|
|
102 LLVMContext::MD_noalias)))
|
|
103 return MRI_NoModRef;
|
|
104
|
|
105 if (!mayAliasInScopes(
|
|
106 CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
|
|
107 Loc.AATags.NoAlias))
|
|
108 return MRI_NoModRef;
|
|
109
|
|
110 return AAResultBase::getModRefInfo(CS, Loc);
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
111 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
112
|
95
|
113 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS1,
|
|
114 ImmutableCallSite CS2) {
|
|
115 if (!EnableScopedNoAlias)
|
|
116 return AAResultBase::getModRefInfo(CS1, CS2);
|
|
117
|
|
118 if (!mayAliasInScopes(
|
|
119 CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
|
|
120 CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
|
|
121 return MRI_NoModRef;
|
|
122
|
|
123 if (!mayAliasInScopes(
|
|
124 CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
|
|
125 CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
|
|
126 return MRI_NoModRef;
|
|
127
|
|
128 return AAResultBase::getModRefInfo(CS1, CS2);
|
|
129 }
|
|
130
|
|
131 void ScopedNoAliasAAResult::collectMDInDomain(
|
|
132 const MDNode *List, const MDNode *Domain,
|
|
133 SmallPtrSetImpl<const MDNode *> &Nodes) const {
|
100
|
134 for (const MDOperand &MDOp : List->operands())
|
|
135 if (const MDNode *MD = dyn_cast<MDNode>(MDOp))
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
136 if (AliasScopeNode(MD).getDomain() == Domain)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
137 Nodes.insert(MD);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
138 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
139
|
95
|
140 bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
|
|
141 const MDNode *NoAlias) const {
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
142 if (!Scopes || !NoAlias)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
143 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
144
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
145 // Collect the set of scope domains relevant to the noalias scopes.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
146 SmallPtrSet<const MDNode *, 16> Domains;
|
100
|
147 for (const MDOperand &MDOp : NoAlias->operands())
|
|
148 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
149 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
150 Domains.insert(Domain);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
151
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
152 // We alias unless, for some domain, the set of noalias scopes in that domain
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
153 // is a superset of the set of alias scopes in that domain.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
154 for (const MDNode *Domain : Domains) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155 SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
156 collectMDInDomain(NoAlias, Domain, NANodes);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
157 collectMDInDomain(Scopes, Domain, ScopeNodes);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
158 if (!ScopeNodes.size())
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
159 continue;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
160
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
161 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
162 bool FoundAll = true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
163 for (const MDNode *SMD : ScopeNodes)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
164 if (!NANodes.count(SMD)) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
165 FoundAll = false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
166 break;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
167 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
168
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 if (FoundAll)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
171 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
173 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
174 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
175
|
95
|
176 ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
|
|
177 AnalysisManager<Function> *AM) {
|
|
178 return ScopedNoAliasAAResult(AM->getResult<TargetLibraryAnalysis>(F));
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
179 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
180
|
95
|
181 char ScopedNoAliasAA::PassID;
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182
|
95
|
183 char ScopedNoAliasAAWrapperPass::ID = 0;
|
|
184 INITIALIZE_PASS_BEGIN(ScopedNoAliasAAWrapperPass, "scoped-noalias",
|
|
185 "Scoped NoAlias Alias Analysis", false, true)
|
|
186 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
187 INITIALIZE_PASS_END(ScopedNoAliasAAWrapperPass, "scoped-noalias",
|
|
188 "Scoped NoAlias Alias Analysis", false, true)
|
|
189
|
|
190 ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
|
|
191 return new ScopedNoAliasAAWrapperPass();
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
192 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
193
|
95
|
194 ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {
|
|
195 initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
196 }
|
|
197
|
|
198 bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
|
|
199 Result.reset(new ScopedNoAliasAAResult(
|
|
200 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()));
|
|
201 return false;
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
202 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
203
|
95
|
204 bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
|
|
205 Result.reset();
|
|
206 return false;
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
207 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
208
|
95
|
209 void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
210 AU.setPreservesAll();
|
|
211 AU.addRequired<TargetLibraryInfoWrapperPass>();
|
77
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
212 }
|