blob: 094e4a3d5dc8db6125effc8926136b1ea596fcb0 [file] [log] [blame]
Hal Finkel94146652014-07-24 14:25:39 +00001//===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Hal Finkel94146652014-07-24 14:25:39 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ScopedNoAlias alias-analysis pass, which implements
10// metadata-based scoped no-alias support.
11//
Hal Finkel029cde62014-07-25 15:50:02 +000012// 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.
Hal Finkel94146652014-07-24 14:25:39 +000016//
Hal Finkel029cde62014-07-25 15:50:02 +000017// !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" }
Hal Finkel94146652014-07-24 14:25:39 +000020//
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
Sanjay Patelf2341682016-01-13 17:23:52 +000028// has a set of noalias scopes in some domain that is a superset of the alias
Hal Finkel029cde62014-07-25 15:50:02 +000029// scopes in that domain of some other instruction, then the two memory
Hal Finkel94146652014-07-24 14:25:39 +000030// accesses are assumed not to alias.
31//
Hal Finkel94146652014-07-24 14:25:39 +000032//===----------------------------------------------------------------------===//
33
Chandler Carruth42ff4482015-08-14 02:55:50 +000034#include "llvm/Analysis/ScopedNoAliasAA.h"
Hal Finkel029cde62014-07-25 15:50:02 +000035#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000036#include "llvm/Analysis/MemoryLocation.h"
37#include "llvm/IR/Instruction.h"
Hal Finkel94146652014-07-24 14:25:39 +000038#include "llvm/IR/LLVMContext.h"
39#include "llvm/IR/Metadata.h"
Hal Finkel94146652014-07-24 14:25:39 +000040#include "llvm/Pass.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000041#include "llvm/Support/Casting.h"
Hal Finkel94146652014-07-24 14:25:39 +000042#include "llvm/Support/CommandLine.h"
Hans Wennborg083ca9b2015-10-06 23:24:35 +000043
Hal Finkel94146652014-07-24 14:25:39 +000044using 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.
Chandler Carruth496b7fb2015-08-14 02:46:07 +000049static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
Zachary Turner8065f0b2017-12-01 00:53:10 +000050 cl::init(true), cl::Hidden);
Hal Finkel94146652014-07-24 14:25:39 +000051
52namespace {
Eugene Zelenkobe709f22017-08-18 23:51:26 +000053
Sanjay Patelc5d29aa2016-01-13 17:43:35 +000054/// This is a simple wrapper around an MDNode which provides a higher-level
55/// interface by hiding the details of how alias analysis information is encoded
56/// in its operands.
Hal Finkel94146652014-07-24 14:25:39 +000057class AliasScopeNode {
Eugene Zelenkobe709f22017-08-18 23:51:26 +000058 const MDNode *Node = nullptr;
Hal Finkel94146652014-07-24 14:25:39 +000059
60public:
Eugene Zelenkobe709f22017-08-18 23:51:26 +000061 AliasScopeNode() = default;
Hal Finkel94146652014-07-24 14:25:39 +000062 explicit AliasScopeNode(const MDNode *N) : Node(N) {}
63
Sanjay Patelc5d29aa2016-01-13 17:43:35 +000064 /// Get the MDNode for this AliasScopeNode.
Hal Finkel94146652014-07-24 14:25:39 +000065 const MDNode *getNode() const { return Node; }
66
Sanjay Patelc5d29aa2016-01-13 17:43:35 +000067 /// Get the MDNode for this AliasScopeNode's domain.
Hal Finkel029cde62014-07-25 15:50:02 +000068 const MDNode *getDomain() const {
Hal Finkel94146652014-07-24 14:25:39 +000069 if (Node->getNumOperands() < 2)
Hal Finkel029cde62014-07-25 15:50:02 +000070 return nullptr;
71 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
Hal Finkel94146652014-07-24 14:25:39 +000072 }
73};
Eugene Zelenkobe709f22017-08-18 23:51:26 +000074
75} // end anonymous namespace
Hal Finkel94146652014-07-24 14:25:39 +000076
Chandler Carruth7b560d42015-09-09 17:55:00 +000077AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
Alina Sbirleabfc779e2019-03-22 17:22:19 +000078 const MemoryLocation &LocB,
79 AAQueryInfo &AAQI) {
Chandler Carruth7b560d42015-09-09 17:55:00 +000080 if (!EnableScopedNoAlias)
Alina Sbirleabfc779e2019-03-22 17:22:19 +000081 return AAResultBase::alias(LocA, LocB, AAQI);
Hal Finkel94146652014-07-24 14:25:39 +000082
Chandler Carruth7b560d42015-09-09 17:55:00 +000083 // Get the attached MDNodes.
84 const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
85
86 const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
87
88 if (!mayAliasInScopes(AScopes, BNoAlias))
89 return NoAlias;
90
91 if (!mayAliasInScopes(BScopes, ANoAlias))
92 return NoAlias;
93
94 // If they may alias, chain to the next AliasAnalysis.
Alina Sbirleabfc779e2019-03-22 17:22:19 +000095 return AAResultBase::alias(LocA, LocB, AAQI);
Hal Finkel94146652014-07-24 14:25:39 +000096}
97
Chandler Carruth363ac682019-01-07 05:42:51 +000098ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call,
Alina Sbirleabfc779e2019-03-22 17:22:19 +000099 const MemoryLocation &Loc,
100 AAQueryInfo &AAQI) {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000101 if (!EnableScopedNoAlias)
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000102 return AAResultBase::getModRefInfo(Call, Loc, AAQI);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000103
Chandler Carruth363ac682019-01-07 05:42:51 +0000104 if (!mayAliasInScopes(Loc.AATags.Scope,
105 Call->getMetadata(LLVMContext::MD_noalias)))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000106 return ModRefInfo::NoModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000107
Chandler Carruth363ac682019-01-07 05:42:51 +0000108 if (!mayAliasInScopes(Call->getMetadata(LLVMContext::MD_alias_scope),
109 Loc.AATags.NoAlias))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000110 return ModRefInfo::NoModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000111
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000112 return AAResultBase::getModRefInfo(Call, Loc, AAQI);
Mehdi Amini46a43552015-03-04 18:43:29 +0000113}
114
Chandler Carruth363ac682019-01-07 05:42:51 +0000115ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1,
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000116 const CallBase *Call2,
117 AAQueryInfo &AAQI) {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000118 if (!EnableScopedNoAlias)
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000119 return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000120
Chandler Carruth363ac682019-01-07 05:42:51 +0000121 if (!mayAliasInScopes(Call1->getMetadata(LLVMContext::MD_alias_scope),
122 Call2->getMetadata(LLVMContext::MD_noalias)))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000123 return ModRefInfo::NoModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000124
Chandler Carruth363ac682019-01-07 05:42:51 +0000125 if (!mayAliasInScopes(Call2->getMetadata(LLVMContext::MD_alias_scope),
126 Call1->getMetadata(LLVMContext::MD_noalias)))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000127 return ModRefInfo::NoModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000128
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000129 return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
Hal Finkel94146652014-07-24 14:25:39 +0000130}
131
David Majnemer3b47a5a2016-08-15 03:56:06 +0000132static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
133 SmallPtrSetImpl<const MDNode *> &Nodes) {
David Majnemerc77a1392016-08-15 02:23:46 +0000134 for (const MDOperand &MDOp : List->operands())
135 if (const MDNode *MD = dyn_cast<MDNode>(MDOp))
136 if (AliasScopeNode(MD).getDomain() == Domain)
137 Nodes.insert(MD);
138}
139
Chandler Carruth7b560d42015-09-09 17:55:00 +0000140bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
141 const MDNode *NoAlias) const {
Hal Finkel94146652014-07-24 14:25:39 +0000142 if (!Scopes || !NoAlias)
143 return true;
144
Hal Finkel029cde62014-07-25 15:50:02 +0000145 // Collect the set of scope domains relevant to the noalias scopes.
146 SmallPtrSet<const MDNode *, 16> Domains;
Sanjay Patelda080822016-01-13 18:37:28 +0000147 for (const MDOperand &MDOp : NoAlias->operands())
148 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
Hal Finkel029cde62014-07-25 15:50:02 +0000149 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
150 Domains.insert(Domain);
Hal Finkel94146652014-07-24 14:25:39 +0000151
Hal Finkel029cde62014-07-25 15:50:02 +0000152 // We alias unless, for some domain, the set of noalias scopes in that domain
153 // is a superset of the set of alias scopes in that domain.
154 for (const MDNode *Domain : Domains) {
David Majnemer8b8869f2016-08-15 02:23:50 +0000155 SmallPtrSet<const MDNode *, 16> ScopeNodes;
David Majnemerc77a1392016-08-15 02:23:46 +0000156 collectMDInDomain(Scopes, Domain, ScopeNodes);
David Majnemerddc7ab22016-08-15 02:23:48 +0000157 if (ScopeNodes.empty())
David Majnemerc77a1392016-08-15 02:23:46 +0000158 continue;
Hal Finkel029cde62014-07-25 15:50:02 +0000159
David Majnemer8b8869f2016-08-15 02:23:50 +0000160 SmallPtrSet<const MDNode *, 16> NANodes;
161 collectMDInDomain(NoAlias, Domain, NANodes);
162
David Majnemerc77a1392016-08-15 02:23:46 +0000163 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
Hal Finkel029cde62014-07-25 15:50:02 +0000164 bool FoundAll = true;
David Majnemerc77a1392016-08-15 02:23:46 +0000165 for (const MDNode *SMD : ScopeNodes)
166 if (!NANodes.count(SMD)) {
167 FoundAll = false;
168 break;
169 }
Hal Finkel029cde62014-07-25 15:50:02 +0000170
171 if (FoundAll)
172 return false;
173 }
174
175 return true;
Hal Finkel94146652014-07-24 14:25:39 +0000176}
177
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000178AnalysisKey ScopedNoAliasAA::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000179
Chandler Carruth7b560d42015-09-09 17:55:00 +0000180ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000181 FunctionAnalysisManager &AM) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000182 return ScopedNoAliasAAResult();
Hal Finkel94146652014-07-24 14:25:39 +0000183}
184
Chandler Carruth7b560d42015-09-09 17:55:00 +0000185char ScopedNoAliasAAWrapperPass::ID = 0;
Eugene Zelenkobe709f22017-08-18 23:51:26 +0000186
Chandler Carruth12884f72016-03-02 15:56:53 +0000187INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias",
188 "Scoped NoAlias Alias Analysis", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000189
190ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
191 return new ScopedNoAliasAAWrapperPass();
Hal Finkel94146652014-07-24 14:25:39 +0000192}
193
Chandler Carruth7b560d42015-09-09 17:55:00 +0000194ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {
195 initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry());
Hal Finkel94146652014-07-24 14:25:39 +0000196}
197
Chandler Carruth7b560d42015-09-09 17:55:00 +0000198bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000199 Result.reset(new ScopedNoAliasAAResult());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000200 return false;
Hal Finkel94146652014-07-24 14:25:39 +0000201}
202
Chandler Carruth7b560d42015-09-09 17:55:00 +0000203bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
204 Result.reset();
205 return false;
Hal Finkel94146652014-07-24 14:25:39 +0000206}
207
Chandler Carruth7b560d42015-09-09 17:55:00 +0000208void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
209 AU.setPreservesAll();
Hal Finkel94146652014-07-24 14:25:39 +0000210}