[NFC] Update memcpy tests
[llvm-complete.git] / lib / Analysis / ScopedNoAliasAA.cpp
blob094e4a3d5dc8db6125effc8926136b1ea596fcb0
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.
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.
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" }
21 // Loads and stores can be tagged with an alias-analysis scope, and also, with
22 // a noalias tag for a specific scope:
24 // ... = load %ptr1, !alias.scope !{ !scope1 }
25 // ... = load %ptr2, !alias.scope !{ !scope1, !scope2 }, !noalias !{ !scope1 }
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.
32 //===----------------------------------------------------------------------===//
34 #include "llvm/Analysis/ScopedNoAliasAA.h"
35 #include "llvm/ADT/SmallPtrSet.h"
36 #include "llvm/Analysis/MemoryLocation.h"
37 #include "llvm/IR/Instruction.h"
38 #include "llvm/IR/LLVMContext.h"
39 #include "llvm/IR/Metadata.h"
40 #include "llvm/Pass.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CommandLine.h"
44 using namespace llvm;
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.
49 static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias",
50 cl::init(true), cl::Hidden);
52 namespace {
54 /// 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.
57 class AliasScopeNode {
58 const MDNode *Node = nullptr;
60 public:
61 AliasScopeNode() = default;
62 explicit AliasScopeNode(const MDNode *N) : Node(N) {}
64 /// Get the MDNode for this AliasScopeNode.
65 const MDNode *getNode() const { return Node; }
67 /// Get the MDNode for this AliasScopeNode's domain.
68 const MDNode *getDomain() const {
69 if (Node->getNumOperands() < 2)
70 return nullptr;
71 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
75 } // end anonymous namespace
77 AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA,
78 const MemoryLocation &LocB,
79 AAQueryInfo &AAQI) {
80 if (!EnableScopedNoAlias)
81 return AAResultBase::alias(LocA, LocB, AAQI);
83 // Get the attached MDNodes.
84 const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
86 const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
88 if (!mayAliasInScopes(AScopes, BNoAlias))
89 return NoAlias;
91 if (!mayAliasInScopes(BScopes, ANoAlias))
92 return NoAlias;
94 // If they may alias, chain to the next AliasAnalysis.
95 return AAResultBase::alias(LocA, LocB, AAQI);
98 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call,
99 const MemoryLocation &Loc,
100 AAQueryInfo &AAQI) {
101 if (!EnableScopedNoAlias)
102 return AAResultBase::getModRefInfo(Call, Loc, AAQI);
104 if (!mayAliasInScopes(Loc.AATags.Scope,
105 Call->getMetadata(LLVMContext::MD_noalias)))
106 return ModRefInfo::NoModRef;
108 if (!mayAliasInScopes(Call->getMetadata(LLVMContext::MD_alias_scope),
109 Loc.AATags.NoAlias))
110 return ModRefInfo::NoModRef;
112 return AAResultBase::getModRefInfo(Call, Loc, AAQI);
115 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1,
116 const CallBase *Call2,
117 AAQueryInfo &AAQI) {
118 if (!EnableScopedNoAlias)
119 return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
121 if (!mayAliasInScopes(Call1->getMetadata(LLVMContext::MD_alias_scope),
122 Call2->getMetadata(LLVMContext::MD_noalias)))
123 return ModRefInfo::NoModRef;
125 if (!mayAliasInScopes(Call2->getMetadata(LLVMContext::MD_alias_scope),
126 Call1->getMetadata(LLVMContext::MD_noalias)))
127 return ModRefInfo::NoModRef;
129 return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
132 static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
133 SmallPtrSetImpl<const MDNode *> &Nodes) {
134 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);
140 bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
141 const MDNode *NoAlias) const {
142 if (!Scopes || !NoAlias)
143 return true;
145 // Collect the set of scope domains relevant to the noalias scopes.
146 SmallPtrSet<const MDNode *, 16> Domains;
147 for (const MDOperand &MDOp : NoAlias->operands())
148 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
149 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
150 Domains.insert(Domain);
152 // 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) {
155 SmallPtrSet<const MDNode *, 16> ScopeNodes;
156 collectMDInDomain(Scopes, Domain, ScopeNodes);
157 if (ScopeNodes.empty())
158 continue;
160 SmallPtrSet<const MDNode *, 16> NANodes;
161 collectMDInDomain(NoAlias, Domain, NANodes);
163 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
164 bool FoundAll = true;
165 for (const MDNode *SMD : ScopeNodes)
166 if (!NANodes.count(SMD)) {
167 FoundAll = false;
168 break;
171 if (FoundAll)
172 return false;
175 return true;
178 AnalysisKey ScopedNoAliasAA::Key;
180 ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
181 FunctionAnalysisManager &AM) {
182 return ScopedNoAliasAAResult();
185 char ScopedNoAliasAAWrapperPass::ID = 0;
187 INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias",
188 "Scoped NoAlias Alias Analysis", false, true)
190 ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
191 return new ScopedNoAliasAAWrapperPass();
194 ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {
195 initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry());
198 bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
199 Result.reset(new ScopedNoAliasAAResult());
200 return false;
203 bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
204 Result.reset();
205 return false;
208 void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
209 AU.setPreservesAll();