Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / Analysis / ScopedNoAliasAA.cpp
blobdcf30c2f0a7fc9afa5c08b14c8283f042ec62c04
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 if (!EnableScopedNoAlias)
80 return AAResultBase::alias(LocA, LocB);
82 // Get the attached MDNodes.
83 const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope;
85 const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias;
87 if (!mayAliasInScopes(AScopes, BNoAlias))
88 return NoAlias;
90 if (!mayAliasInScopes(BScopes, ANoAlias))
91 return NoAlias;
93 // If they may alias, chain to the next AliasAnalysis.
94 return AAResultBase::alias(LocA, LocB);
97 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call,
98 const MemoryLocation &Loc) {
99 if (!EnableScopedNoAlias)
100 return AAResultBase::getModRefInfo(Call, Loc);
102 if (!mayAliasInScopes(Loc.AATags.Scope,
103 Call->getMetadata(LLVMContext::MD_noalias)))
104 return ModRefInfo::NoModRef;
106 if (!mayAliasInScopes(Call->getMetadata(LLVMContext::MD_alias_scope),
107 Loc.AATags.NoAlias))
108 return ModRefInfo::NoModRef;
110 return AAResultBase::getModRefInfo(Call, Loc);
113 ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1,
114 const CallBase *Call2) {
115 if (!EnableScopedNoAlias)
116 return AAResultBase::getModRefInfo(Call1, Call2);
118 if (!mayAliasInScopes(Call1->getMetadata(LLVMContext::MD_alias_scope),
119 Call2->getMetadata(LLVMContext::MD_noalias)))
120 return ModRefInfo::NoModRef;
122 if (!mayAliasInScopes(Call2->getMetadata(LLVMContext::MD_alias_scope),
123 Call1->getMetadata(LLVMContext::MD_noalias)))
124 return ModRefInfo::NoModRef;
126 return AAResultBase::getModRefInfo(Call1, Call2);
129 static void collectMDInDomain(const MDNode *List, const MDNode *Domain,
130 SmallPtrSetImpl<const MDNode *> &Nodes) {
131 for (const MDOperand &MDOp : List->operands())
132 if (const MDNode *MD = dyn_cast<MDNode>(MDOp))
133 if (AliasScopeNode(MD).getDomain() == Domain)
134 Nodes.insert(MD);
137 bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes,
138 const MDNode *NoAlias) const {
139 if (!Scopes || !NoAlias)
140 return true;
142 // Collect the set of scope domains relevant to the noalias scopes.
143 SmallPtrSet<const MDNode *, 16> Domains;
144 for (const MDOperand &MDOp : NoAlias->operands())
145 if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
146 if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
147 Domains.insert(Domain);
149 // We alias unless, for some domain, the set of noalias scopes in that domain
150 // is a superset of the set of alias scopes in that domain.
151 for (const MDNode *Domain : Domains) {
152 SmallPtrSet<const MDNode *, 16> ScopeNodes;
153 collectMDInDomain(Scopes, Domain, ScopeNodes);
154 if (ScopeNodes.empty())
155 continue;
157 SmallPtrSet<const MDNode *, 16> NANodes;
158 collectMDInDomain(NoAlias, Domain, NANodes);
160 // To not alias, all of the nodes in ScopeNodes must be in NANodes.
161 bool FoundAll = true;
162 for (const MDNode *SMD : ScopeNodes)
163 if (!NANodes.count(SMD)) {
164 FoundAll = false;
165 break;
168 if (FoundAll)
169 return false;
172 return true;
175 AnalysisKey ScopedNoAliasAA::Key;
177 ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F,
178 FunctionAnalysisManager &AM) {
179 return ScopedNoAliasAAResult();
182 char ScopedNoAliasAAWrapperPass::ID = 0;
184 INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias",
185 "Scoped NoAlias Alias Analysis", false, true)
187 ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() {
188 return new ScopedNoAliasAAWrapperPass();
191 ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {
192 initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry());
195 bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) {
196 Result.reset(new ScopedNoAliasAAResult());
197 return false;
200 bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) {
201 Result.reset();
202 return false;
205 void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
206 AU.setPreservesAll();