LLVM  3.7.0
ScopedNoAliasAA.cpp
Go to the documentation of this file.
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/ADT/SmallPtrSet.h"
37 #include "llvm/Analysis/Passes.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"
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>
50 EnableScopedNoAlias("enable-scoped-noalias", 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 
74 /// ScopedNoAliasAA - This is a simple alias analysis
75 /// implementation that uses scoped-noalias metadata to answer queries.
76 class ScopedNoAliasAA : public ImmutablePass, public AliasAnalysis {
77 public:
78  static char ID; // Class identification, replacement for typeinfo
79  ScopedNoAliasAA() : ImmutablePass(ID) {
81  }
82 
83  bool doInitialization(Module &M) override;
84 
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.
89  void *getAdjustedAnalysisPointer(const void *PI) override {
90  if (PI == &AliasAnalysis::ID)
91  return (AliasAnalysis*)this;
92  return this;
93  }
94 
95 protected:
96  bool mayAliasInScopes(const MDNode *Scopes, const MDNode *NoAlias) const;
97  void collectMDInDomain(const MDNode *List, const MDNode *Domain,
98  SmallPtrSetImpl<const MDNode *> &Nodes) const;
99 
100 private:
101  void getAnalysisUsage(AnalysisUsage &AU) const override;
102  AliasResult alias(const MemoryLocation &LocA,
103  const MemoryLocation &LocB) override;
104  bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal) override;
105  ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
106  ModRefBehavior getModRefBehavior(const Function *F) override;
107  ModRefResult getModRefInfo(ImmutableCallSite CS,
108  const MemoryLocation &Loc) override;
109  ModRefResult getModRefInfo(ImmutableCallSite CS1,
110  ImmutableCallSite CS2) override;
111 };
112 } // End of anonymous namespace
113 
114 // Register this pass...
115 char ScopedNoAliasAA::ID = 0;
116 INITIALIZE_AG_PASS(ScopedNoAliasAA, AliasAnalysis, "scoped-noalias",
117  "Scoped NoAlias Alias Analysis", false, true, false)
118 
120  return new ScopedNoAliasAA();
121 }
122 
123 bool ScopedNoAliasAA::doInitialization(Module &M) {
124  InitializeAliasAnalysis(this, &M.getDataLayout());
125  return true;
126 }
127 
128 void
129 ScopedNoAliasAA::getAnalysisUsage(AnalysisUsage &AU) const {
130  AU.setPreservesAll();
132 }
133 
134 void
135 ScopedNoAliasAA::collectMDInDomain(const MDNode *List, const MDNode *Domain,
136  SmallPtrSetImpl<const MDNode *> &Nodes) const {
137  for (unsigned i = 0, ie = List->getNumOperands(); i != ie; ++i)
138  if (const MDNode *MD = dyn_cast<MDNode>(List->getOperand(i)))
139  if (AliasScopeNode(MD).getDomain() == Domain)
140  Nodes.insert(MD);
141 }
142 
143 bool
144 ScopedNoAliasAA::mayAliasInScopes(const MDNode *Scopes,
145  const MDNode *NoAlias) const {
146  if (!Scopes || !NoAlias)
147  return true;
148 
149  // Collect the set of scope domains relevant to the noalias scopes.
151  for (unsigned i = 0, ie = NoAlias->getNumOperands(); i != ie; ++i)
152  if (const MDNode *NAMD = dyn_cast<MDNode>(NoAlias->getOperand(i)))
153  if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
154  Domains.insert(Domain);
155 
156  // We alias unless, for some domain, the set of noalias scopes in that domain
157  // is a superset of the set of alias scopes in that domain.
158  for (const MDNode *Domain : Domains) {
159  SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes;
160  collectMDInDomain(NoAlias, Domain, NANodes);
161  collectMDInDomain(Scopes, Domain, ScopeNodes);
162  if (!ScopeNodes.size())
163  continue;
164 
165  // To not alias, all of the nodes in ScopeNodes must be in NANodes.
166  bool FoundAll = true;
167  for (const MDNode *SMD : ScopeNodes)
168  if (!NANodes.count(SMD)) {
169  FoundAll = false;
170  break;
171  }
172 
173  if (FoundAll)
174  return false;
175  }
176 
177  return true;
178 }
179 
180 AliasResult ScopedNoAliasAA::alias(const MemoryLocation &LocA,
181  const MemoryLocation &LocB) {
182  if (!EnableScopedNoAlias)
183  return AliasAnalysis::alias(LocA, LocB);
184 
185  // Get the attached MDNodes.
186  const MDNode *AScopes = LocA.AATags.Scope,
187  *BScopes = LocB.AATags.Scope;
188 
189  const MDNode *ANoAlias = LocA.AATags.NoAlias,
190  *BNoAlias = LocB.AATags.NoAlias;
191 
192  if (!mayAliasInScopes(AScopes, BNoAlias))
193  return NoAlias;
194 
195  if (!mayAliasInScopes(BScopes, ANoAlias))
196  return NoAlias;
197 
198  // If they may alias, chain to the next AliasAnalysis.
199  return AliasAnalysis::alias(LocA, LocB);
200 }
201 
202 bool ScopedNoAliasAA::pointsToConstantMemory(const MemoryLocation &Loc,
203  bool OrLocal) {
204  return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
205 }
206 
208 ScopedNoAliasAA::getModRefBehavior(ImmutableCallSite CS) {
210 }
211 
213 ScopedNoAliasAA::getModRefBehavior(const Function *F) {
215 }
216 
218 ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS,
219  const MemoryLocation &Loc) {
220  if (!EnableScopedNoAlias)
221  return AliasAnalysis::getModRefInfo(CS, Loc);
222 
223  if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
225  return NoModRef;
226 
227  if (!mayAliasInScopes(
229  Loc.AATags.NoAlias))
230  return NoModRef;
231 
232  return AliasAnalysis::getModRefInfo(CS, Loc);
233 }
234 
236 ScopedNoAliasAA::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
237  if (!EnableScopedNoAlias)
238  return AliasAnalysis::getModRefInfo(CS1, CS2);
239 
240  if (!mayAliasInScopes(
243  return NoModRef;
244 
245  if (!mayAliasInScopes(
248  return NoModRef;
249 
250  return AliasAnalysis::getModRefInfo(CS1, CS2);
251 }
252 
INITIALIZE_AG_PASS(ScopedNoAliasAA, AliasAnalysis,"scoped-noalias","Scoped NoAlias Alias Analysis", false, true, false) ImmutablePass *llvm
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition: Metadata.h:565
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
InstrTy * getInstruction() const
Definition: CallSite.h:82
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:942
ModRefBehavior
ModRefBehavior - Summary of how a function affects memory in the program.
This file contains the declarations for metadata subclasses.
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:276
Metadata node.
Definition: Metadata.h:740
The two locations do not alias at all.
Definition: AliasAnalysis.h:78
F(f)
virtual bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
pointsToConstantMemory - If the specified memory location is known to be constant, return true.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:242
static cl::opt< bool > EnableScopedNoAlias("enable-scoped-noalias", cl::init(true))
ImmutablePass * createScopedNoAliasAAPass()
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
size_type size() const
Definition: SmallPtrSet.h:79
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define ModRefBehavior
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:264
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:72
Represent the analysis usage information of a pass.
virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS)
getModRefBehavior - Return the behavior when calling the given call site.
void initializeScopedNoAliasAAPass(PassRegistry &)
Representation for a specific memory location.
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:936
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:262
virtual AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
Alias Queries...
Module.h This file contains the declarations for the Module class.
MDNode * getMetadata(unsigned KindID) const
getMetadata - Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:167
ModRefResult getModRefInfo(const Instruction *I)
getModRefInfo - Return information about whether or not an instruction may read or write memory (with...
MDNode * NoAlias
The tag specifying the noalias scope.
Definition: Metadata.h:568
void setPreservesAll()
Set by analyses that do not transform their input at all.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:418
#define N
ModRefResult
Simple mod/ref information...
virtual void getAnalysisUsage(AnalysisUsage &AU) const
getAnalysisUsage - All alias analysis implementations should invoke this directly (using AliasAnalysi...