1//===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ScopedNoAlias alias-analysis pass, which implements
10// metadata-based scoped no-alias support.
11//
12// Alias-analysis scopes are defined by an id (which can be a string or some
13// other metadata node), a domain node, and an optional descriptive string.
14// A domain is defined by an id (which can be a string or some other metadata
15// node), and an optional descriptive string.
16//
17// !dom0 = metadata !{ metadata !"domain of foo()" }
18// !scope1 = metadata !{ metadata !scope1, metadata !dom0, metadata !"scope 1" }
19// !scope2 = metadata !{ metadata !scope2, metadata !dom0, metadata !"scope 2" }
20//
21// Loads and stores can be tagged with an alias-analysis scope, and also, with
22// a noalias tag for a specific scope:
23//
24// ... = load %ptr1, !alias.scope !{ !scope1 }
25// ... = load %ptr2, !alias.scope !{ !scope1, !scope2 }, !noalias !{ !scope1 }
26//
27// When evaluating an aliasing query, if one of the instructions is associated
28// has a set of noalias scopes in some domain that is a superset of the alias
29// scopes in that domain of some other instruction, then the two memory
30// accesses are assumed not to alias.
31//
32//===----------------------------------------------------------------------===//
33
34#include "llvm/Analysis/ScopedNoAliasAA.h"
35#include "llvm/ADT/SetOperations.h"
36#include "llvm/ADT/SmallPtrSet.h"
37#include "llvm/Analysis/MemoryLocation.h"
38#include "llvm/IR/InstrTypes.h"
39#include "llvm/IR/Instructions.h"
40#include "llvm/IR/LLVMContext.h"
41#include "llvm/IR/Metadata.h"
42#include "llvm/InitializePasses.h"
43#include "llvm/Pass.h"
44#include "llvm/Support/Casting.h"
45#include "llvm/Support/CommandLine.h"
46
47using namespace llvm;
48
49// A handy option for disabling scoped no-alias functionality. The same effect
50// can also be achieved by stripping the associated metadata tags from IR, but
51// this option is sometimes more convenient.
52static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
53 cl::init(Val: true), cl::Hidden);
54
55AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
56 const MemoryLocation &LocB) {
57 if (!EnableScopedNoAlias)
58 return AliasResult::MayAlias;
59
60 // Get the attached MDNodes.
61 const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
62
63 const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
64
65 if (!mayAliasInScopes(Scopes: AScopes, NoAlias: BNoAlias))
66 return AliasResult::NoAlias;
67
68 if (!mayAliasInScopes(Scopes: BScopes, NoAlias: ANoAlias))
69 return AliasResult::NoAlias;
70
71 return AliasResult::MayAlias;
72}
73
74AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
75 const MemoryLocation &LocB,
76 AAQueryInfo &, const Instruction *) {
77 return alias(LocA, LocB);
78}
79
80ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call,
81 const MemoryLocation &Loc,
82 AAQueryInfo &AAQI) {
83 if (!EnableScopedNoAlias)
84 return ModRefInfo::ModRef;
85
86 if (!mayAliasInScopes(Scopes: Loc.AATags.Scope,
87 NoAlias: Call->getMetadata(KindID: LLVMContext::MD_noalias)))
88 return ModRefInfo::NoModRef;
89
90 if (!mayAliasInScopes(Scopes: Call->getMetadata(KindID: LLVMContext::MD_alias_scope),
91 NoAlias: Loc.AATags.NoAlias))
92 return ModRefInfo::NoModRef;
93
94 return ModRefInfo::ModRef;
95}
96
97ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const FenceInst *F,
98 const MemoryLocation &Loc,
99 AAQueryInfo &AAQI) {
100 if (!EnableScopedNoAlias)
101 return ModRefInfo::ModRef;
102
103 if (!mayAliasInScopes(Scopes: Loc.AATags.Scope,
104 NoAlias: F->getMetadata(KindID: LLVMContext::MD_noalias)))
105 return ModRefInfo::NoModRef;
106
107 if (!mayAliasInScopes(Scopes: F->getMetadata(KindID: LLVMContext::MD_alias_scope),
108 NoAlias: Loc.AATags.NoAlias))
109 return ModRefInfo::NoModRef;
110
111 return ModRefInfo::ModRef;
112}
113
114ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1,
115 const CallBase *Call2,
116 AAQueryInfo &AAQI) {
117 if (!EnableScopedNoAlias)
118 return ModRefInfo::ModRef;
119
120 if (!mayAliasInScopes(Scopes: Call1->getMetadata(KindID: LLVMContext::MD_alias_scope),
121 NoAlias: Call2->getMetadata(KindID: LLVMContext::MD_noalias)))
122 return ModRefInfo::NoModRef;
123
124 if (!mayAliasInScopes(Scopes: Call2->getMetadata(KindID: LLVMContext::MD_alias_scope),
125 NoAlias: Call1->getMetadata(KindID: LLVMContext::MD_noalias)))
126 return ModRefInfo::NoModRef;
127
128 return ModRefInfo::ModRef;
129}
130
131static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
132 SmallPtrSetImpl<const MDNode *> &Nodes) {
133 for (const MDOperand &MDOp : List->operands())
134 if (const MDNode *MD = dyn_cast<MDNode>(Val: MDOp))
135 if (AliasScopeNode(MD).getDomain() == Domain)
136 Nodes.insert(Ptr: MD);
137}
138
139/// Collect the set of scoped domains relevant to the noalias scopes.
140void ScopedNoAliasAAResult::collectScopedDomains(
141 const MDNode *NoAlias, SmallPtrSetImpl<const MDNode *> &Domains) {
142 if (!NoAlias)
143 return;
144 assert(Domains.empty() && "Domains should be empty");
145 for (const MDOperand &MDOp : NoAlias->operands())
146 if (const MDNode *NAMD = dyn_cast<MDNode>(Val: MDOp))
147 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
148 Domains.insert(Ptr: Domain);
149}
150
151bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
152 const MDNode *NoAlias) {
153 if (!Scopes || !NoAlias)
154 return true;
155
156 // Collect the set of scope domains relevant to the noalias scopes.
157 SmallPtrSet<const MDNode *, 16> Domains;
158 collectScopedDomains(NoAlias, Domains);
159
160 // We alias unless, for some domain, the set of noalias scopes in that domain
161 // is a superset of the set of alias scopes in that domain.
162 for (const MDNode *Domain : Domains) {
163 SmallPtrSet<const MDNode *, 16> ScopeNodes;
164 collectMDInDomain(List: Scopes, Domain, Nodes&: ScopeNodes);
165 if (ScopeNodes.empty())
166 continue;
167
168 SmallPtrSet<const MDNode *, 16> NANodes;
169 collectMDInDomain(List: NoAlias, Domain, Nodes&: NANodes);
170
171 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
172 if (llvm::set_is_subset(S1: ScopeNodes, S2: NANodes))
173 return false;
174 }
175
176 return true;
177}
178
179AnalysisKey ScopedNoAliasAA::Key;
180
181ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
182 FunctionAnalysisManager &AM) {
183 return ScopedNoAliasAAResult();
184}
185
186char ScopedNoAliasAAWrapperPass::ID = 0;
187
188INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias-aa",
189 "Scoped NoAlias Alias Analysis", false, true)
190
191ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
192 return new ScopedNoAliasAAWrapperPass();
193}
194
195ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {}
196
197bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
198 Result.reset(p: new ScopedNoAliasAAResult());
199 return false;
200}
201
202bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
203 Result.reset();
204 return false;
205}
206
207void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
208 AU.setPreservesAll();
209}
210