1 //===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ScopedNoAlias alias-analysis pass, which implements
11 // metadata-based scoped no-alias support.
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.
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" }
22 // Loads and stores can be tagged with an alias-analysis scope, and also, with
23 // a noalias tag for a specific scope:
25 // ... = load %ptr1, !alias.scope !{ !scope1 }
26 // ... = load %ptr2, !alias.scope !{ !scope1, !scope2 }, !noalias !{ !scope1 }
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 a superset of the alias
30 // scopes in that domain of some other instruction, then the two memory
31 // accesses are assumed not to alias.
33 //===----------------------------------------------------------------------===//
35 #include "llvm/Analysis/ScopedNoAliasAA.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/Analysis/MemoryLocation.h"
38 #include "llvm/IR/Instruction.h"
39 #include "llvm/IR/LLVMContext.h"
40 #include "llvm/IR/Metadata.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/CommandLine.h"
47 // A handy option for disabling scoped no-alias functionality. The same effect
48 // can also be achieved by stripping the associated metadata tags from IR, but
49 // this option is sometimes more convenient.
50 static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
51 cl::init(true), cl::Hidden);
55 /// This is a simple wrapper around an MDNode which provides a higher-level
56 /// interface by hiding the details of how alias analysis information is encoded
58 class AliasScopeNode {
59 const MDNode *Node = nullptr;
62 AliasScopeNode() = default;
63 explicit AliasScopeNode(const MDNode *N) : Node(N) {}
65 /// Get the MDNode for this AliasScopeNode.
66 const MDNode *getNode() const { return Node; }
68 /// Get the MDNode for this AliasScopeNode's domain.
69 const MDNode *getDomain() const {
70 if (Node->getNumOperands() < 2)
72 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
76 } // end anonymous namespace
78 AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
79 const MemoryLocation &LocB) {
80 if (!EnableScopedNoAlias)
81 return AAResultBase::alias(LocA, LocB);
83 // Get the attached MDNodes.
84 const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
86 const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
88 if (!mayAliasInScopes(AScopes, BNoAlias))
91 if (!mayAliasInScopes(BScopes, ANoAlias))
94 // If they may alias, chain to the next AliasAnalysis.
95 return AAResultBase::alias(LocA, LocB);
98 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call,
99 const MemoryLocation &Loc) {
100 if (!EnableScopedNoAlias)
101 return AAResultBase::getModRefInfo(Call, Loc);
103 if (!mayAliasInScopes(Loc.AATags.Scope,
104 Call->getMetadata(LLVMContext::MD_noalias)))
105 return ModRefInfo::NoModRef;
107 if (!mayAliasInScopes(Call->getMetadata(LLVMContext::MD_alias_scope),
109 return ModRefInfo::NoModRef;
111 return AAResultBase::getModRefInfo(Call, Loc);
114 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1,
115 const CallBase *Call2) {
116 if (!EnableScopedNoAlias)
117 return AAResultBase::getModRefInfo(Call1, Call2);
119 if (!mayAliasInScopes(Call1->getMetadata(LLVMContext::MD_alias_scope),
120 Call2->getMetadata(LLVMContext::MD_noalias)))
121 return ModRefInfo::NoModRef;
123 if (!mayAliasInScopes(Call2->getMetadata(LLVMContext::MD_alias_scope),
124 Call1->getMetadata(LLVMContext::MD_noalias)))
125 return ModRefInfo::NoModRef;
127 return AAResultBase::getModRefInfo(Call1, Call2);
130 static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
131 SmallPtrSetImpl<const MDNode *> &Nodes) {
132 for (const MDOperand &MDOp : List->operands())
133 if (const MDNode *MD = dyn_cast<MDNode>(MDOp))
134 if (AliasScopeNode(MD).getDomain() == Domain)
138 bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
139 const MDNode *NoAlias) const {
140 if (!Scopes || !NoAlias)
143 // Collect the set of scope domains relevant to the noalias scopes.
144 SmallPtrSet<const MDNode *, 16> Domains;
145 for (const MDOperand &MDOp : NoAlias->operands())
146 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
147 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
148 Domains.insert(Domain);
150 // We alias unless, for some domain, the set of noalias scopes in that domain
151 // is a superset of the set of alias scopes in that domain.
152 for (const MDNode *Domain : Domains) {
153 SmallPtrSet<const MDNode *, 16> ScopeNodes;
154 collectMDInDomain(Scopes, Domain, ScopeNodes);
155 if (ScopeNodes.empty())
158 SmallPtrSet<const MDNode *, 16> NANodes;
159 collectMDInDomain(NoAlias, Domain, NANodes);
161 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
162 bool FoundAll = true;
163 for (const MDNode *SMD : ScopeNodes)
164 if (!NANodes.count(SMD)) {
176 AnalysisKey ScopedNoAliasAA::Key;
178 ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
179 FunctionAnalysisManager &AM) {
180 return ScopedNoAliasAAResult();
183 char ScopedNoAliasAAWrapperPass::ID = 0;
185 INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias",
186 "Scoped NoAlias Alias Analysis", false, true)
188 ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
189 return new ScopedNoAliasAAWrapperPass();
192 ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {
193 initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry());
196 bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
197 Result.reset(new ScopedNoAliasAAResult());
201 bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
206 void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
207 AU.setPreservesAll();