[PM/AA] Hoist ScopedNoAliasAA's interface into a header and move the
[oota-llvm.git] / lib / Analysis / ScopedNoAliasAA.cpp
1 //===- 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 //
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.
17 //
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" }
21 //
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
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.
32 //
33 //===----------------------------------------------------------------------===//
34
35 #include "llvm/Analysis/ScopedNoAliasAA.h"
36 #include "llvm/ADT/SmallPtrSet.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"
44 using 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.
49 static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
50                                          cl::init(true));
51
52 namespace {
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 {
57   const MDNode *Node;
58
59 public:
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
66   /// getDomain - Get the MDNode for this AliasScopeNode's domain.
67   const MDNode *getDomain() const {
68     if (Node->getNumOperands() < 2)
69       return nullptr;
70     return dyn_cast_or_null<MDNode>(Node->getOperand(1));
71   }
72 };
73 } // End of anonymous namespace
74
75 // Register this pass...
76 char ScopedNoAliasAA::ID = 0;
77 INITIALIZE_AG_PASS(ScopedNoAliasAA, AliasAnalysis, "scoped-noalias",
78                    "Scoped NoAlias Alias Analysis", false, true, false)
79
80 ImmutablePass *llvm::createScopedNoAliasAAPass() {
81   return new ScopedNoAliasAA();
82 }
83
84 bool ScopedNoAliasAA::doInitialization(Module &M) {
85   InitializeAliasAnalysis(this, &M.getDataLayout());
86   return true;
87 }
88
89 void ScopedNoAliasAA::getAnalysisUsage(AnalysisUsage &AU) const {
90   AU.setPreservesAll();
91   AliasAnalysis::getAnalysisUsage(AU);
92 }
93
94 void ScopedNoAliasAA::collectMDInDomain(
95     const MDNode *List, const MDNode *Domain,
96     SmallPtrSetImpl<const MDNode *> &Nodes) const {
97   for (unsigned i = 0, ie = List->getNumOperands(); i != ie; ++i)
98     if (const MDNode *MD = dyn_cast<MDNode>(List->getOperand(i)))
99       if (AliasScopeNode(MD).getDomain() == Domain)
100         Nodes.insert(MD);
101 }
102
103 bool ScopedNoAliasAA::mayAliasInScopes(const MDNode *Scopes,
104                                        const MDNode *NoAlias) const {
105   if (!Scopes || !NoAlias)
106     return true;
107
108   // Collect the set of scope domains relevant to the noalias scopes.
109   SmallPtrSet<const MDNode *, 16> Domains;
110   for (unsigned i = 0, ie = NoAlias->getNumOperands(); i != ie; ++i)
111     if (const MDNode *NAMD = dyn_cast<MDNode>(NoAlias->getOperand(i)))
112       if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
113         Domains.insert(Domain);
114
115   // We alias unless, for some domain, the set of noalias scopes in that domain
116   // is a superset of the set of alias scopes in that domain.
117   for (const MDNode *Domain : Domains) {
118     SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes;
119     collectMDInDomain(NoAlias, Domain, NANodes);
120     collectMDInDomain(Scopes, Domain, ScopeNodes);
121     if (!ScopeNodes.size())
122       continue;
123
124     // To not alias, all of the nodes in ScopeNodes must be in NANodes.
125     bool FoundAll = true;
126     for (const MDNode *SMD : ScopeNodes)
127       if (!NANodes.count(SMD)) {
128         FoundAll = false;
129         break;
130       }
131
132     if (FoundAll)
133       return false;
134   }
135
136   return true;
137 }
138
139 AliasResult ScopedNoAliasAA::alias(const MemoryLocation &LocA,
140                                    const MemoryLocation &LocB) {
141   if (!EnableScopedNoAlias)
142     return AliasAnalysis::alias(LocA, LocB);
143
144   // Get the attached MDNodes.
145   const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
146
147   const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
148
149   if (!mayAliasInScopes(AScopes, BNoAlias))
150     return NoAlias;
151
152   if (!mayAliasInScopes(BScopes, ANoAlias))
153     return NoAlias;
154
155   // If they may alias, chain to the next AliasAnalysis.
156   return AliasAnalysis::alias(LocA, LocB);
157 }
158
159 bool ScopedNoAliasAA::pointsToConstantMemory(const MemoryLocation &Loc,
160                                              bool OrLocal) {
161   return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
162 }
163
164 FunctionModRefBehavior
165 ScopedNoAliasAA::getModRefBehavior(ImmutableCallSite CS) {
166   return AliasAnalysis::getModRefBehavior(CS);
167 }
168
169 FunctionModRefBehavior ScopedNoAliasAA::getModRefBehavior(const Function *F) {
170   return AliasAnalysis::getModRefBehavior(F);
171 }
172
173 ModRefInfo ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS,
174                                           const MemoryLocation &Loc) {
175   if (!EnableScopedNoAlias)
176     return AliasAnalysis::getModRefInfo(CS, Loc);
177
178   if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
179                                               LLVMContext::MD_noalias)))
180     return MRI_NoModRef;
181
182   if (!mayAliasInScopes(
183           CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
184           Loc.AATags.NoAlias))
185     return MRI_NoModRef;
186
187   return AliasAnalysis::getModRefInfo(CS, Loc);
188 }
189
190 ModRefInfo ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS1,
191                                           ImmutableCallSite CS2) {
192   if (!EnableScopedNoAlias)
193     return AliasAnalysis::getModRefInfo(CS1, CS2);
194
195   if (!mayAliasInScopes(
196           CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
197           CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
198     return MRI_NoModRef;
199
200   if (!mayAliasInScopes(
201           CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
202           CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
203     return MRI_NoModRef;
204
205   return AliasAnalysis::getModRefInfo(CS1, CS2);
206 }
207