ScopedNoAliasAA.cpp revision 280031
117651Speter//===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===//
2131377Stjr//
317651Speter//                     The LLVM Compiler Infrastructure
4206708Sdelphij//
5206708Sdelphij// This file is distributed under the University of Illinois Open Source
6206708Sdelphij// License. See LICENSE.TXT for details.
7206708Sdelphij//
8206708Sdelphij//===----------------------------------------------------------------------===//
9206708Sdelphij//
10206708Sdelphij// This file defines the ScopedNoAlias alias-analysis pass, which implements
11206708Sdelphij// metadata-based scoped no-alias support.
12206708Sdelphij//
13206708Sdelphij// Alias-analysis scopes are defined by an id (which can be a string or some
14206708Sdelphij// other metadata node), a domain node, and an optional descriptive string.
15206708Sdelphij// A domain is defined by an id (which can be a string or some other metadata
16206708Sdelphij// node), and an optional descriptive string.
17206708Sdelphij//
18206708Sdelphij// !dom0 =   metadata !{ metadata !"domain of foo()" }
19206708Sdelphij// !scope1 = metadata !{ metadata !scope1, metadata !dom0, metadata !"scope 1" }
20206708Sdelphij// !scope2 = metadata !{ metadata !scope2, metadata !dom0, metadata !"scope 2" }
21206708Sdelphij//
22206708Sdelphij// Loads and stores can be tagged with an alias-analysis scope, and also, with
23206708Sdelphij// a noalias tag for a specific scope:
24206708Sdelphij//
25206708Sdelphij// ... = load %ptr1, !alias.scope !{ !scope1 }
26206708Sdelphij// ... = load %ptr2, !alias.scope !{ !scope1, !scope2 }, !noalias !{ !scope1 }
27206708Sdelphij//
28206708Sdelphij// When evaluating an aliasing query, if one of the instructions is associated
29206708Sdelphij// has a set of noalias scopes in some domain that is superset of the alias
30206708Sdelphij// scopes in that domain of some other instruction, then the two memory
31206708Sdelphij// accesses are assumed not to alias.
32206002Sdelphij//
33206002Sdelphij//===----------------------------------------------------------------------===//
34206002Sdelphij
35206002Sdelphij#include "llvm/ADT/SmallPtrSet.h"
36206002Sdelphij#include "llvm/Analysis/AliasAnalysis.h"
37206002Sdelphij#include "llvm/Analysis/Passes.h"
38206002Sdelphij#include "llvm/IR/Constants.h"
39206002Sdelphij#include "llvm/IR/LLVMContext.h"
40206002Sdelphij#include "llvm/IR/Metadata.h"
41206002Sdelphij#include "llvm/IR/Module.h"
42206002Sdelphij#include "llvm/Pass.h"
43206002Sdelphij#include "llvm/Support/CommandLine.h"
44206002Sdelphijusing namespace llvm;
45206002Sdelphij
46206002Sdelphij// A handy option for disabling scoped no-alias functionality. The same effect
47206002Sdelphij// can also be achieved by stripping the associated metadata tags from IR, but
48206002Sdelphij// this option is sometimes more convenient.
49206002Sdelphijstatic cl::opt<bool>
50206002SdelphijEnableScopedNoAlias("enable-scoped-noalias", cl::init(true));
51206002Sdelphij
52206002Sdelphijnamespace {
53206002Sdelphij/// AliasScopeNode - This is a simple wrapper around an MDNode which provides
54206002Sdelphij/// a higher-level interface by hiding the details of how alias analysis
55206002Sdelphij/// information is encoded in its operands.
56205471Sdelphijclass AliasScopeNode {
57205471Sdelphij  const MDNode *Node;
58205471Sdelphij
59205471Sdelphijpublic:
60205471Sdelphij  AliasScopeNode() : Node(0) {}
61205471Sdelphij  explicit AliasScopeNode(const MDNode *N) : Node(N) {}
62205471Sdelphij
63205471Sdelphij  /// getNode - Get the MDNode for this AliasScopeNode.
64205471Sdelphij  const MDNode *getNode() const { return Node; }
65205471Sdelphij
66205471Sdelphij  /// getDomain - Get the MDNode for this AliasScopeNode's domain.
67205471Sdelphij  const MDNode *getDomain() const {
68205471Sdelphij    if (Node->getNumOperands() < 2)
69205471Sdelphij      return nullptr;
70205471Sdelphij    return dyn_cast_or_null<MDNode>(Node->getOperand(1));
71205471Sdelphij  }
72205471Sdelphij};
73205471Sdelphij
74205471Sdelphij/// ScopedNoAliasAA - This is a simple alias analysis
75205471Sdelphij/// implementation that uses scoped-noalias metadata to answer queries.
76205471Sdelphijclass ScopedNoAliasAA : public ImmutablePass, public AliasAnalysis {
77205471Sdelphijpublic:
78205471Sdelphij  static char ID; // Class identification, replacement for typeinfo
79205471Sdelphij  ScopedNoAliasAA() : ImmutablePass(ID) {
80205471Sdelphij    initializeScopedNoAliasAAPass(*PassRegistry::getPassRegistry());
81205471Sdelphij  }
82205471Sdelphij
83205471Sdelphij  void initializePass() override { InitializeAliasAnalysis(this); }
84205471Sdelphij
85205471Sdelphij  /// getAdjustedAnalysisPointer - This method is used when a pass implements
86205471Sdelphij  /// an analysis interface through multiple inheritance.  If needed, it
87205471Sdelphij  /// should override this to adjust the this pointer as needed for the
88205471Sdelphij  /// specified pass info.
89205471Sdelphij  void *getAdjustedAnalysisPointer(const void *PI) override {
90205471Sdelphij    if (PI == &AliasAnalysis::ID)
91205471Sdelphij      return (AliasAnalysis*)this;
92205471Sdelphij    return this;
93205471Sdelphij  }
94205471Sdelphij
95205471Sdelphijprotected:
96205471Sdelphij  bool mayAliasInScopes(const MDNode *Scopes, const MDNode *NoAlias) const;
97205471Sdelphij  void collectMDInDomain(const MDNode *List, const MDNode *Domain,
98205471Sdelphij                         SmallPtrSetImpl<const MDNode *> &Nodes) const;
99205471Sdelphij
100205471Sdelphijprivate:
101205471Sdelphij  void getAnalysisUsage(AnalysisUsage &AU) const override;
102205471Sdelphij  AliasResult alias(const Location &LocA, const Location &LocB) override;
103205471Sdelphij  bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override;
104205471Sdelphij  ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
105205471Sdelphij  ModRefBehavior getModRefBehavior(const Function *F) override;
106205471Sdelphij  ModRefResult getModRefInfo(ImmutableCallSite CS,
107205471Sdelphij                             const Location &Loc) override;
108205471Sdelphij  ModRefResult getModRefInfo(ImmutableCallSite CS1,
109205471Sdelphij                             ImmutableCallSite CS2) override;
110205471Sdelphij};
111205471Sdelphij}  // End of anonymous namespace
112205471Sdelphij
113205471Sdelphij// Register this pass...
114205471Sdelphijchar ScopedNoAliasAA::ID = 0;
115205471SdelphijINITIALIZE_AG_PASS(ScopedNoAliasAA, AliasAnalysis, "scoped-noalias",
116205471Sdelphij                   "Scoped NoAlias Alias Analysis", false, true, false)
117205471Sdelphij
118205471SdelphijImmutablePass *llvm::createScopedNoAliasAAPass() {
119205471Sdelphij  return new ScopedNoAliasAA();
120205471Sdelphij}
121205471Sdelphij
122205471Sdelphijvoid
123205471SdelphijScopedNoAliasAA::getAnalysisUsage(AnalysisUsage &AU) const {
124205471Sdelphij  AU.setPreservesAll();
125205471Sdelphij  AliasAnalysis::getAnalysisUsage(AU);
126205471Sdelphij}
127205471Sdelphij
128205471Sdelphijvoid
129205471SdelphijScopedNoAliasAA::collectMDInDomain(const MDNode *List, const MDNode *Domain,
130205471Sdelphij                   SmallPtrSetImpl<const MDNode *> &Nodes) const {
131205471Sdelphij  for (unsigned i = 0, ie = List->getNumOperands(); i != ie; ++i)
132205471Sdelphij    if (const MDNode *MD = dyn_cast<MDNode>(List->getOperand(i)))
133205471Sdelphij      if (AliasScopeNode(MD).getDomain() == Domain)
134205471Sdelphij        Nodes.insert(MD);
135205471Sdelphij}
136205471Sdelphij
137205471Sdelphijbool
138205471SdelphijScopedNoAliasAA::mayAliasInScopes(const MDNode *Scopes,
139205471Sdelphij                                  const MDNode *NoAlias) const {
140205471Sdelphij  if (!Scopes || !NoAlias)
141205471Sdelphij    return true;
142205471Sdelphij
143205471Sdelphij  // Collect the set of scope domains relevant to the noalias scopes.
144205471Sdelphij  SmallPtrSet<const MDNode *, 16> Domains;
145205471Sdelphij  for (unsigned i = 0, ie = NoAlias->getNumOperands(); i != ie; ++i)
146205471Sdelphij    if (const MDNode *NAMD = dyn_cast<MDNode>(NoAlias->getOperand(i)))
147205471Sdelphij      if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
148205471Sdelphij        Domains.insert(Domain);
149205471Sdelphij
150205471Sdelphij  // We alias unless, for some domain, the set of noalias scopes in that domain
151205471Sdelphij  // is a superset of the set of alias scopes in that domain.
152205471Sdelphij  for (const MDNode *Domain : Domains) {
153205471Sdelphij    SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes;
154205471Sdelphij    collectMDInDomain(NoAlias, Domain, NANodes);
155205471Sdelphij    collectMDInDomain(Scopes, Domain, ScopeNodes);
156205471Sdelphij    if (!ScopeNodes.size())
157205471Sdelphij      continue;
158205471Sdelphij
159205471Sdelphij    // To not alias, all of the nodes in ScopeNodes must be in NANodes.
160205471Sdelphij    bool FoundAll = true;
161205471Sdelphij    for (const MDNode *SMD : ScopeNodes)
162205471Sdelphij      if (!NANodes.count(SMD)) {
163205471Sdelphij        FoundAll = false;
164205471Sdelphij        break;
165205471Sdelphij      }
166205471Sdelphij
167205471Sdelphij    if (FoundAll)
168205471Sdelphij      return false;
169205471Sdelphij  }
170205471Sdelphij
171205471Sdelphij  return true;
172205471Sdelphij}
173205471Sdelphij
174205471SdelphijAliasAnalysis::AliasResult
175205471SdelphijScopedNoAliasAA::alias(const Location &LocA, const Location &LocB) {
176205471Sdelphij  if (!EnableScopedNoAlias)
177205471Sdelphij    return AliasAnalysis::alias(LocA, LocB);
178205471Sdelphij
179205471Sdelphij  // Get the attached MDNodes.
180205471Sdelphij  const MDNode *AScopes = LocA.AATags.Scope,
181205471Sdelphij               *BScopes = LocB.AATags.Scope;
182205471Sdelphij
183205471Sdelphij  const MDNode *ANoAlias = LocA.AATags.NoAlias,
184205471Sdelphij               *BNoAlias = LocB.AATags.NoAlias;
185205471Sdelphij
186205471Sdelphij  if (!mayAliasInScopes(AScopes, BNoAlias))
187205471Sdelphij    return NoAlias;
188205471Sdelphij
189205471Sdelphij  if (!mayAliasInScopes(BScopes, ANoAlias))
190205471Sdelphij    return NoAlias;
191205471Sdelphij
192205471Sdelphij  // If they may alias, chain to the next AliasAnalysis.
193205471Sdelphij  return AliasAnalysis::alias(LocA, LocB);
194205471Sdelphij}
195205471Sdelphij
196205471Sdelphijbool ScopedNoAliasAA::pointsToConstantMemory(const Location &Loc,
197205471Sdelphij                                             bool OrLocal) {
198205471Sdelphij  return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
199205471Sdelphij}
200205471Sdelphij
201205471SdelphijAliasAnalysis::ModRefBehavior
202205471SdelphijScopedNoAliasAA::getModRefBehavior(ImmutableCallSite CS) {
203205471Sdelphij  return AliasAnalysis::getModRefBehavior(CS);
204205471Sdelphij}
205205471Sdelphij
206205471SdelphijAliasAnalysis::ModRefBehavior
207205471SdelphijScopedNoAliasAA::getModRefBehavior(const Function *F) {
208205471Sdelphij  return AliasAnalysis::getModRefBehavior(F);
209205471Sdelphij}
210205471Sdelphij
211205471SdelphijAliasAnalysis::ModRefResult
212205471SdelphijScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS, const Location &Loc) {
213205471Sdelphij  if (!EnableScopedNoAlias)
214205471Sdelphij    return AliasAnalysis::getModRefInfo(CS, Loc);
215205471Sdelphij
216205471Sdelphij  if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
217205471Sdelphij                                              LLVMContext::MD_noalias)))
218205471Sdelphij    return NoModRef;
219205471Sdelphij
220205471Sdelphij  if (!mayAliasInScopes(
221205471Sdelphij          CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
222205471Sdelphij          Loc.AATags.NoAlias))
223205471Sdelphij    return NoModRef;
224205471Sdelphij
225205471Sdelphij  return AliasAnalysis::getModRefInfo(CS, Loc);
226205471Sdelphij}
227205471Sdelphij
228205471SdelphijAliasAnalysis::ModRefResult
229205471SdelphijScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
230205471Sdelphij  if (!EnableScopedNoAlias)
231205471Sdelphij    return AliasAnalysis::getModRefInfo(CS1, CS2);
232205471Sdelphij
233205471Sdelphij  if (!mayAliasInScopes(
234205471Sdelphij          CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
235205471Sdelphij          CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
236205471Sdelphij    return NoModRef;
237205471Sdelphij
238205471Sdelphij  if (!mayAliasInScopes(
239205471Sdelphij          CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
240205471Sdelphij          CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
241205471Sdelphij    return NoModRef;
242205471Sdelphij
243205471Sdelphij  return AliasAnalysis::getModRefInfo(CS1, CS2);
244205471Sdelphij}
245205471Sdelphij
246205471Sdelphij