blob: f12275aff387007c683efbffce9dc97e61db4f20 [file] [log] [blame]
Hal Finkel94146652014-07-24 14:25:39 +00001//===- 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//
Hal Finkel029cde62014-07-25 15:50:02 +000013// 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.
Hal Finkel94146652014-07-24 14:25:39 +000017//
Hal Finkel029cde62014-07-25 15:50:02 +000018// !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" }
Hal Finkel94146652014-07-24 14:25:39 +000021//
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
Sanjay Patelf2341682016-01-13 17:23:52 +000029// has a set of noalias scopes in some domain that is a superset of the alias
Hal Finkel029cde62014-07-25 15:50:02 +000030// scopes in that domain of some other instruction, then the two memory
Hal Finkel94146652014-07-24 14:25:39 +000031// accesses are assumed not to alias.
32//
Hal Finkel94146652014-07-24 14:25:39 +000033//===----------------------------------------------------------------------===//
34
Chandler Carruth42ff4482015-08-14 02:55:50 +000035#include "llvm/Analysis/ScopedNoAliasAA.h"
Hal Finkel029cde62014-07-25 15:50:02 +000036#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000037#include "llvm/Analysis/MemoryLocation.h"
38#include "llvm/IR/Instruction.h"
Hal Finkel94146652014-07-24 14:25:39 +000039#include "llvm/IR/LLVMContext.h"
40#include "llvm/IR/Metadata.h"
Hal Finkel94146652014-07-24 14:25:39 +000041#include "llvm/Pass.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000042#include "llvm/Support/Casting.h"
Hal Finkel94146652014-07-24 14:25:39 +000043#include "llvm/Support/CommandLine.h"
Hans Wennborg083ca9b2015-10-06 23:24:35 +000044
Hal Finkel94146652014-07-24 14:25:39 +000045using namespace llvm;
46
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.
Chandler Carruth496b7fb2015-08-14 02:46:07 +000050static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
Zachary Turner8065f0b2017-12-01 00:53:10 +000051 cl::init(true), cl::Hidden);
Hal Finkel94146652014-07-24 14:25:39 +000052
53namespace {
Eugene Zelenkobe709f22017-08-18 23:51:26 +000054
Sanjay Patelc5d29aa2016-01-13 17:43:35 +000055/// 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
57/// in its operands.
Hal Finkel94146652014-07-24 14:25:39 +000058class AliasScopeNode {
Eugene Zelenkobe709f22017-08-18 23:51:26 +000059 const MDNode *Node = nullptr;
Hal Finkel94146652014-07-24 14:25:39 +000060
61public:
Eugene Zelenkobe709f22017-08-18 23:51:26 +000062 AliasScopeNode() = default;
Hal Finkel94146652014-07-24 14:25:39 +000063 explicit AliasScopeNode(const MDNode *N) : Node(N) {}
64
Sanjay Patelc5d29aa2016-01-13 17:43:35 +000065 /// Get the MDNode for this AliasScopeNode.
Hal Finkel94146652014-07-24 14:25:39 +000066 const MDNode *getNode() const { return Node; }
67
Sanjay Patelc5d29aa2016-01-13 17:43:35 +000068 /// Get the MDNode for this AliasScopeNode's domain.
Hal Finkel029cde62014-07-25 15:50:02 +000069 const MDNode *getDomain() const {
Hal Finkel94146652014-07-24 14:25:39 +000070 if (Node->getNumOperands() < 2)
Hal Finkel029cde62014-07-25 15:50:02 +000071 return nullptr;
72 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
Hal Finkel94146652014-07-24 14:25:39 +000073 }
74};
Eugene Zelenkobe709f22017-08-18 23:51:26 +000075
76} // end anonymous namespace
Hal Finkel94146652014-07-24 14:25:39 +000077
Chandler Carruth7b560d42015-09-09 17:55:00 +000078AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
79 const MemoryLocation &LocB) {
80 if (!EnableScopedNoAlias)
81 return AAResultBase::alias(LocA, LocB);
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.
95 return AAResultBase::alias(LocA, LocB);
Hal Finkel94146652014-07-24 14:25:39 +000096}
97
Chandler Carruth7b560d42015-09-09 17:55:00 +000098ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS,
99 const MemoryLocation &Loc) {
100 if (!EnableScopedNoAlias)
101 return AAResultBase::getModRefInfo(CS, Loc);
102
103 if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata(
104 LLVMContext::MD_noalias)))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000105 return ModRefInfo::NoModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000106
107 if (!mayAliasInScopes(
108 CS.getInstruction()->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
112 return AAResultBase::getModRefInfo(CS, Loc);
Mehdi Amini46a43552015-03-04 18:43:29 +0000113}
114
Chandler Carruth7b560d42015-09-09 17:55:00 +0000115ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS1,
116 ImmutableCallSite CS2) {
117 if (!EnableScopedNoAlias)
118 return AAResultBase::getModRefInfo(CS1, CS2);
119
120 if (!mayAliasInScopes(
121 CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
122 CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000123 return ModRefInfo::NoModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000124
125 if (!mayAliasInScopes(
126 CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope),
127 CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias)))
Alina Sbirlea193429f2017-12-07 22:41:34 +0000128 return ModRefInfo::NoModRef;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000129
130 return AAResultBase::getModRefInfo(CS1, CS2);
Hal Finkel94146652014-07-24 14:25:39 +0000131}
132
David Majnemer3b47a5a2016-08-15 03:56:06 +0000133static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
134 SmallPtrSetImpl<const MDNode *> &Nodes) {
David Majnemerc77a1392016-08-15 02:23:46 +0000135 for (const MDOperand &MDOp : List->operands())
136 if (const MDNode *MD = dyn_cast<MDNode>(MDOp))
137 if (AliasScopeNode(MD).getDomain() == Domain)
138 Nodes.insert(MD);
139}
140
Chandler Carruth7b560d42015-09-09 17:55:00 +0000141bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
142 const MDNode *NoAlias) const {
Hal Finkel94146652014-07-24 14:25:39 +0000143 if (!Scopes || !NoAlias)
144 return true;
145
Hal Finkel029cde62014-07-25 15:50:02 +0000146 // Collect the set of scope domains relevant to the noalias scopes.
147 SmallPtrSet<const MDNode *, 16> Domains;
Sanjay Patelda080822016-01-13 18:37:28 +0000148 for (const MDOperand &MDOp : NoAlias->operands())
149 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
Hal Finkel029cde62014-07-25 15:50:02 +0000150 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
151 Domains.insert(Domain);
Hal Finkel94146652014-07-24 14:25:39 +0000152
Hal Finkel029cde62014-07-25 15:50:02 +0000153 // We alias unless, for some domain, the set of noalias scopes in that domain
154 // is a superset of the set of alias scopes in that domain.
155 for (const MDNode *Domain : Domains) {
David Majnemer8b8869f2016-08-15 02:23:50 +0000156 SmallPtrSet<const MDNode *, 16> ScopeNodes;
David Majnemerc77a1392016-08-15 02:23:46 +0000157 collectMDInDomain(Scopes, Domain, ScopeNodes);
David Majnemerddc7ab22016-08-15 02:23:48 +0000158 if (ScopeNodes.empty())
David Majnemerc77a1392016-08-15 02:23:46 +0000159 continue;
Hal Finkel029cde62014-07-25 15:50:02 +0000160
David Majnemer8b8869f2016-08-15 02:23:50 +0000161 SmallPtrSet<const MDNode *, 16> NANodes;
162 collectMDInDomain(NoAlias, Domain, NANodes);
163
David Majnemerc77a1392016-08-15 02:23:46 +0000164 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
Hal Finkel029cde62014-07-25 15:50:02 +0000165 bool FoundAll = true;
David Majnemerc77a1392016-08-15 02:23:46 +0000166 for (const MDNode *SMD : ScopeNodes)
167 if (!NANodes.count(SMD)) {
168 FoundAll = false;
169 break;
170 }
Hal Finkel029cde62014-07-25 15:50:02 +0000171
172 if (FoundAll)
173 return false;
174 }
175
176 return true;
Hal Finkel94146652014-07-24 14:25:39 +0000177}
178
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000179AnalysisKey ScopedNoAliasAA::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000180
Chandler Carruth7b560d42015-09-09 17:55:00 +0000181ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +0000182 FunctionAnalysisManager &AM) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000183 return ScopedNoAliasAAResult();
Hal Finkel94146652014-07-24 14:25:39 +0000184}
185
Chandler Carruth7b560d42015-09-09 17:55:00 +0000186char ScopedNoAliasAAWrapperPass::ID = 0;
Eugene Zelenkobe709f22017-08-18 23:51:26 +0000187
Chandler Carruth12884f72016-03-02 15:56:53 +0000188INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias",
189 "Scoped NoAlias Alias Analysis", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000190
191ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
192 return new ScopedNoAliasAAWrapperPass();
Hal Finkel94146652014-07-24 14:25:39 +0000193}
194
Chandler Carruth7b560d42015-09-09 17:55:00 +0000195ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {
196 initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry());
Hal Finkel94146652014-07-24 14:25:39 +0000197}
198
Chandler Carruth7b560d42015-09-09 17:55:00 +0000199bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
Chandler Carruth12884f72016-03-02 15:56:53 +0000200 Result.reset(new ScopedNoAliasAAResult());
Chandler Carruth7b560d42015-09-09 17:55:00 +0000201 return false;
Hal Finkel94146652014-07-24 14:25:39 +0000202}
203
Chandler Carruth7b560d42015-09-09 17:55:00 +0000204bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
205 Result.reset();
206 return false;
Hal Finkel94146652014-07-24 14:25:39 +0000207}
208
Chandler Carruth7b560d42015-09-09 17:55:00 +0000209void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
210 AU.setPreservesAll();
Hal Finkel94146652014-07-24 14:25:39 +0000211}