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/LLVMContext.h"
40#include "llvm/IR/Metadata.h"
41#include "llvm/InitializePasses.h"
42#include "llvm/Pass.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/CommandLine.h"
45
46using namespace llvm;
47
48// A handy option for disabling scoped no-alias functionality. The same effect
49// can also be achieved by stripping the associated metadata tags from IR, but
50// this option is sometimes more convenient.
51static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
52 cl::init(Val: true), cl::Hidden);
53
54AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
55 const MemoryLocation &LocB) {
56 if (!EnableScopedNoAlias)
57 return AliasResult::MayAlias;
58
59 // Get the attached MDNodes.
60 const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
61
62 const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
63
64 if (!mayAliasInScopes(Scopes: AScopes, NoAlias: BNoAlias))
65 return AliasResult::NoAlias;
66
67 if (!mayAliasInScopes(Scopes: BScopes, NoAlias: ANoAlias))
68 return AliasResult::NoAlias;
69
70 return AliasResult::MayAlias;
71}
72
73AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
74 const MemoryLocation &LocB,
75 AAQueryInfo &, const Instruction *) {
76 return alias(LocA, LocB);
77}
78
79ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call,
80 const MemoryLocation &Loc,
81 AAQueryInfo &AAQI) {
82 if (!EnableScopedNoAlias)
83 return ModRefInfo::ModRef;
84
85 if (!mayAliasInScopes(Scopes: Loc.AATags.Scope,
86 NoAlias: Call->getMetadata(KindID: LLVMContext::MD_noalias)))
87 return ModRefInfo::NoModRef;
88
89 if (!mayAliasInScopes(Scopes: Call->getMetadata(KindID: LLVMContext::MD_alias_scope),
90 NoAlias: Loc.AATags.NoAlias))
91 return ModRefInfo::NoModRef;
92
93 return ModRefInfo::ModRef;
94}
95
96ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1,
97 const CallBase *Call2,
98 AAQueryInfo &AAQI) {
99 if (!EnableScopedNoAlias)
100 return ModRefInfo::ModRef;
101
102 if (!mayAliasInScopes(Scopes: Call1->getMetadata(KindID: LLVMContext::MD_alias_scope),
103 NoAlias: Call2->getMetadata(KindID: LLVMContext::MD_noalias)))
104 return ModRefInfo::NoModRef;
105
106 if (!mayAliasInScopes(Scopes: Call2->getMetadata(KindID: LLVMContext::MD_alias_scope),
107 NoAlias: Call1->getMetadata(KindID: LLVMContext::MD_noalias)))
108 return ModRefInfo::NoModRef;
109
110 return ModRefInfo::ModRef;
111}
112
113static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
114 SmallPtrSetImpl<const MDNode *> &Nodes) {
115 for (const MDOperand &MDOp : List->operands())
116 if (const MDNode *MD = dyn_cast<MDNode>(Val: MDOp))
117 if (AliasScopeNode(MD).getDomain() == Domain)
118 Nodes.insert(Ptr: MD);
119}
120
121/// Collect the set of scoped domains relevant to the noalias scopes.
122void ScopedNoAliasAAResult::collectScopedDomains(
123 const MDNode *NoAlias, SmallPtrSetImpl<const MDNode *> &Domains) {
124 if (!NoAlias)
125 return;
126 assert(Domains.empty() && "Domains should be empty");
127 for (const MDOperand &MDOp : NoAlias->operands())
128 if (const MDNode *NAMD = dyn_cast<MDNode>(Val: MDOp))
129 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
130 Domains.insert(Ptr: Domain);
131}
132
133bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
134 const MDNode *NoAlias) {
135 if (!Scopes || !NoAlias)
136 return true;
137
138 // Collect the set of scope domains relevant to the noalias scopes.
139 SmallPtrSet<const MDNode *, 16> Domains;
140 collectScopedDomains(NoAlias, Domains);
141
142 // We alias unless, for some domain, the set of noalias scopes in that domain
143 // is a superset of the set of alias scopes in that domain.
144 for (const MDNode *Domain : Domains) {
145 SmallPtrSet<const MDNode *, 16> ScopeNodes;
146 collectMDInDomain(List: Scopes, Domain, Nodes&: ScopeNodes);
147 if (ScopeNodes.empty())
148 continue;
149
150 SmallPtrSet<const MDNode *, 16> NANodes;
151 collectMDInDomain(List: NoAlias, Domain, Nodes&: NANodes);
152
153 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
154 if (llvm::set_is_subset(S1: ScopeNodes, S2: NANodes))
155 return false;
156 }
157
158 return true;
159}
160
161AnalysisKey ScopedNoAliasAA::Key;
162
163ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
164 FunctionAnalysisManager &AM) {
165 return ScopedNoAliasAAResult();
166}
167
168char ScopedNoAliasAAWrapperPass::ID = 0;
169
170INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias-aa",
171 "Scoped NoAlias Alias Analysis", false, true)
172
173ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
174 return new ScopedNoAliasAAWrapperPass();
175}
176
177ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {}
178
179bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
180 Result.reset(p: new ScopedNoAliasAAResult());
181 return false;
182}
183
184bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
185 Result.reset();
186 return false;
187}
188
189void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
190 AU.setPreservesAll();
191}
192