LLVM  14.0.0git
ScopedNoAliasAA.cpp
Go to the documentation of this file.
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 
35 #include "llvm/ADT/SetOperations.h"
36 #include "llvm/ADT/SmallPtrSet.h"
38 #include "llvm/IR/InstrTypes.h"
39 #include "llvm/IR/Instruction.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"
46 
47 using 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.
52 static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
53  cl::init(true), cl::Hidden);
54 
56  const MemoryLocation &LocB,
57  AAQueryInfo &AAQI) {
59  return AAResultBase::alias(LocA, LocB, AAQI);
60 
61  // Get the attached MDNodes.
62  const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
63 
64  const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
65 
66  if (!mayAliasInScopes(AScopes, BNoAlias))
67  return AliasResult::NoAlias;
68 
69  if (!mayAliasInScopes(BScopes, ANoAlias))
70  return AliasResult::NoAlias;
71 
72  // If they may alias, chain to the next AliasAnalysis.
73  return AAResultBase::alias(LocA, LocB, AAQI);
74 }
75 
77  const MemoryLocation &Loc,
78  AAQueryInfo &AAQI) {
80  return AAResultBase::getModRefInfo(Call, Loc, AAQI);
81 
82  if (!mayAliasInScopes(Loc.AATags.Scope,
83  Call->getMetadata(LLVMContext::MD_noalias)))
84  return ModRefInfo::NoModRef;
85 
86  if (!mayAliasInScopes(Call->getMetadata(LLVMContext::MD_alias_scope),
87  Loc.AATags.NoAlias))
88  return ModRefInfo::NoModRef;
89 
90  return AAResultBase::getModRefInfo(Call, Loc, AAQI);
91 }
92 
94  const CallBase *Call2,
95  AAQueryInfo &AAQI) {
97  return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
98 
99  if (!mayAliasInScopes(Call1->getMetadata(LLVMContext::MD_alias_scope),
100  Call2->getMetadata(LLVMContext::MD_noalias)))
101  return ModRefInfo::NoModRef;
102 
103  if (!mayAliasInScopes(Call2->getMetadata(LLVMContext::MD_alias_scope),
104  Call1->getMetadata(LLVMContext::MD_noalias)))
105  return ModRefInfo::NoModRef;
106 
107  return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
108 }
109 
110 static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
112  for (const MDOperand &MDOp : List->operands())
113  if (const MDNode *MD = dyn_cast<MDNode>(MDOp))
114  if (AliasScopeNode(MD).getDomain() == Domain)
115  Nodes.insert(MD);
116 }
117 
118 bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
119  const MDNode *NoAlias) const {
120  if (!Scopes || !NoAlias)
121  return true;
122 
123  // Collect the set of scope domains relevant to the noalias scopes.
125  for (const MDOperand &MDOp : NoAlias->operands())
126  if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
127  if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
128  Domains.insert(Domain);
129 
130  // We alias unless, for some domain, the set of noalias scopes in that domain
131  // is a superset of the set of alias scopes in that domain.
132  for (const MDNode *Domain : Domains) {
134  collectMDInDomain(Scopes, Domain, ScopeNodes);
135  if (ScopeNodes.empty())
136  continue;
137 
139  collectMDInDomain(NoAlias, Domain, NANodes);
140 
141  // To not alias, all of the nodes in ScopeNodes must be in NANodes.
142  if (llvm::set_is_subset(ScopeNodes, NANodes))
143  return false;
144  }
145 
146  return true;
147 }
148 
149 AnalysisKey ScopedNoAliasAA::Key;
150 
153  return ScopedNoAliasAAResult();
154 }
155 
157 
159  "Scoped NoAlias Alias Analysis", false, true)
160 
162  return new ScopedNoAliasAAWrapperPass();
163 }
164 
167 }
168 
170  Result.reset(new ScopedNoAliasAAResult());
171  return false;
172 }
173 
175  Result.reset();
176  return false;
177 }
178 
180  AU.setPreservesAll();
181 }
llvm::ModRefInfo::NoModRef
@ NoModRef
The access neither references nor modifies the value stored in memory.
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::initializeScopedNoAliasAAWrapperPassPass
void initializeScopedNoAliasAAWrapperPassPass(PassRegistry &)
llvm::set_is_subset
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
Definition: SetOperations.h:71
llvm::ScopedNoAliasAAWrapperPass::doInitialization
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: ScopedNoAliasAA.cpp:169
Metadata.h
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:269
SetOperations.h
llvm::Function
Definition: Function.h:62
Pass.h
EnableScopedNoAlias
static cl::opt< bool > EnableScopedNoAlias("enable-scoped-noalias", cl::init(true), cl::Hidden)
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:82
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
llvm::AAMDNodes::Scope
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition: Metadata.h:674
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::MDNode::operands
op_range operands() const
Definition: Metadata.h:1135
llvm::AAQueryInfo
This class stores info we want to provide to or retain within an alias query.
Definition: AliasAnalysis.h:459
Instruction.h
CommandLine.h
llvm::MemoryLocation::AATags
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
Definition: MemoryLocation.h:230
collectMDInDomain
static void collectMDInDomain(const MDNode *List, const MDNode *Domain, SmallPtrSetImpl< const MDNode * > &Nodes)
Definition: ScopedNoAliasAA.cpp:110
llvm::ScopedNoAliasAAResult::getModRefInfo
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Definition: ScopedNoAliasAA.cpp:76
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::ScopedNoAliasAAWrapperPass::doFinalization
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: ScopedNoAliasAA.cpp:174
InstrTypes.h
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
Domain
Domain
Definition: CorrelatedValuePropagation.cpp:704
llvm::ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass
ScopedNoAliasAAWrapperPass()
Definition: ScopedNoAliasAA.cpp:165
SmallPtrSet.h
ScopedNoAliasAA.h
llvm::Instruction::getMetadata
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:282
getDomain
Domain getDomain(Value *V, LazyValueInfo *LVI, Instruction *CxtI)
Definition: CorrelatedValuePropagation.cpp:706
llvm::AAResultBase::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:1219
llvm::cl::opt< bool >
llvm::ScopedNoAliasAAResult::alias
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI)
Definition: ScopedNoAliasAA.cpp:55
MemoryLocation.h
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:441
llvm::ModRefInfo
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: AliasAnalysis.h:149
INITIALIZE_PASS
INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias-aa", "Scoped NoAlias Alias Analysis", false, true) ImmutablePass *llvm
Definition: ScopedNoAliasAA.cpp:158
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::MDNode
Metadata node.
Definition: Metadata.h:906
llvm::AliasResult::NoAlias
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:100
llvm::AAResultBase::getModRefInfo
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Definition: AliasAnalysis.h:1241
llvm::ScopedNoAliasAAResult
A simple AA result which uses scoped-noalias metadata to answer queries.
Definition: ScopedNoAliasAA.h:29
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::AAMDNodes::NoAlias
MDNode * NoAlias
The tag specifying the noalias scope.
Definition: Metadata.h:677
llvm::createScopedNoAliasAAWrapperPass
ImmutablePass * createScopedNoAliasAAWrapperPass()
Casting.h
llvm::ScopedNoAliasAA::run
ScopedNoAliasAAResult run(Function &F, FunctionAnalysisManager &AM)
Definition: ScopedNoAliasAA.cpp:151
llvm::ScopedNoAliasAAWrapperPass
Legacy wrapper pass to provide the ScopedNoAliasAAResult object.
Definition: ScopedNoAliasAA.h:65
llvm::AliasScopeNode
This is a simple wrapper around an MDNode which provides a higher-level interface by hiding the detai...
Definition: Metadata.h:1257
List
const NodeList & List
Definition: RDFGraph.cpp:201
llvm::SmallPtrSetImplBase::empty
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1176
llvm::SmallPtrSetImpl< const MDNode * >
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
LLVMContext.h
InitializePasses.h
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:753
llvm::ScopedNoAliasAAWrapperPass::ID
static char ID
Definition: ScopedNoAliasAA.h:69
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:209
llvm::ScopedNoAliasAAWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: ScopedNoAliasAA.cpp:179
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:364
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38