Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 1 | //===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the ScopedNoAlias alias-analysis pass, which implements |
| 10 | // metadata-based scoped no-alias support. |
| 11 | // |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 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. |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 16 | // |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 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" } |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 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 |
Sanjay Patel | f234168 | 2016-01-13 17:23:52 +0000 | [diff] [blame] | 28 | // has a set of noalias scopes in some domain that is a superset of the alias |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 29 | // scopes in that domain of some other instruction, then the two memory |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 30 | // accesses are assumed not to alias. |
| 31 | // |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Chandler Carruth | 42ff448 | 2015-08-14 02:55:50 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/ScopedNoAliasAA.h" |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | be709f2 | 2017-08-18 23:51:26 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/MemoryLocation.h" |
| 37 | #include "llvm/IR/Instruction.h" |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 38 | #include "llvm/IR/LLVMContext.h" |
| 39 | #include "llvm/IR/Metadata.h" |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 40 | #include "llvm/Pass.h" |
Eugene Zelenko | be709f2 | 2017-08-18 23:51:26 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Casting.h" |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CommandLine.h" |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 43 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 44 | using 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 Carruth | 496b7fb | 2015-08-14 02:46:07 +0000 | [diff] [blame] | 49 | static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias", |
Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 50 | cl::init(true), cl::Hidden); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 51 | |
| 52 | namespace { |
Eugene Zelenko | be709f2 | 2017-08-18 23:51:26 +0000 | [diff] [blame] | 53 | |
Sanjay Patel | c5d29aa | 2016-01-13 17:43:35 +0000 | [diff] [blame] | 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. |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 57 | class AliasScopeNode { |
Eugene Zelenko | be709f2 | 2017-08-18 23:51:26 +0000 | [diff] [blame] | 58 | const MDNode *Node = nullptr; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 59 | |
| 60 | public: |
Eugene Zelenko | be709f2 | 2017-08-18 23:51:26 +0000 | [diff] [blame] | 61 | AliasScopeNode() = default; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 62 | explicit AliasScopeNode(const MDNode *N) : Node(N) {} |
| 63 | |
Sanjay Patel | c5d29aa | 2016-01-13 17:43:35 +0000 | [diff] [blame] | 64 | /// Get the MDNode for this AliasScopeNode. |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 65 | const MDNode *getNode() const { return Node; } |
| 66 | |
Sanjay Patel | c5d29aa | 2016-01-13 17:43:35 +0000 | [diff] [blame] | 67 | /// Get the MDNode for this AliasScopeNode's domain. |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 68 | const MDNode *getDomain() const { |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 69 | if (Node->getNumOperands() < 2) |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 70 | return nullptr; |
| 71 | return dyn_cast_or_null<MDNode>(Node->getOperand(1)); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 72 | } |
| 73 | }; |
Eugene Zelenko | be709f2 | 2017-08-18 23:51:26 +0000 | [diff] [blame] | 74 | |
| 75 | } // end anonymous namespace |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 76 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 77 | AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 78 | const MemoryLocation &LocB, |
| 79 | AAQueryInfo &AAQI) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 80 | if (!EnableScopedNoAlias) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 81 | return AAResultBase::alias(LocA, LocB, AAQI); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 82 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 83 | // 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 Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 95 | return AAResultBase::alias(LocA, LocB, AAQI); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 98 | ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 99 | const MemoryLocation &Loc, |
| 100 | AAQueryInfo &AAQI) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 101 | if (!EnableScopedNoAlias) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 102 | return AAResultBase::getModRefInfo(Call, Loc, AAQI); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 103 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 104 | if (!mayAliasInScopes(Loc.AATags.Scope, |
| 105 | Call->getMetadata(LLVMContext::MD_noalias))) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 106 | return ModRefInfo::NoModRef; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 107 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 108 | if (!mayAliasInScopes(Call->getMetadata(LLVMContext::MD_alias_scope), |
| 109 | Loc.AATags.NoAlias)) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 110 | return ModRefInfo::NoModRef; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 111 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 112 | return AAResultBase::getModRefInfo(Call, Loc, AAQI); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 115 | ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1, |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 116 | const CallBase *Call2, |
| 117 | AAQueryInfo &AAQI) { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 118 | if (!EnableScopedNoAlias) |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 119 | return AAResultBase::getModRefInfo(Call1, Call2, AAQI); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 120 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 121 | if (!mayAliasInScopes(Call1->getMetadata(LLVMContext::MD_alias_scope), |
| 122 | Call2->getMetadata(LLVMContext::MD_noalias))) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 123 | return ModRefInfo::NoModRef; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 124 | |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 125 | if (!mayAliasInScopes(Call2->getMetadata(LLVMContext::MD_alias_scope), |
| 126 | Call1->getMetadata(LLVMContext::MD_noalias))) |
Alina Sbirlea | 193429f | 2017-12-07 22:41:34 +0000 | [diff] [blame] | 127 | return ModRefInfo::NoModRef; |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 128 | |
Alina Sbirlea | bfc779e | 2019-03-22 17:22:19 +0000 | [diff] [blame] | 129 | return AAResultBase::getModRefInfo(Call1, Call2, AAQI); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 130 | } |
| 131 | |
David Majnemer | 3b47a5a | 2016-08-15 03:56:06 +0000 | [diff] [blame] | 132 | static void collectMDInDomain(const MDNode *List, const MDNode *Domain, |
| 133 | SmallPtrSetImpl<const MDNode *> &Nodes) { |
David Majnemer | c77a139 | 2016-08-15 02:23:46 +0000 | [diff] [blame] | 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); |
| 138 | } |
| 139 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 140 | bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes, |
| 141 | const MDNode *NoAlias) const { |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 142 | if (!Scopes || !NoAlias) |
| 143 | return true; |
| 144 | |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 145 | // Collect the set of scope domains relevant to the noalias scopes. |
| 146 | SmallPtrSet<const MDNode *, 16> Domains; |
Sanjay Patel | da08082 | 2016-01-13 18:37:28 +0000 | [diff] [blame] | 147 | for (const MDOperand &MDOp : NoAlias->operands()) |
| 148 | if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp)) |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 149 | if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain()) |
| 150 | Domains.insert(Domain); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 151 | |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 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) { |
David Majnemer | 8b8869f | 2016-08-15 02:23:50 +0000 | [diff] [blame] | 155 | SmallPtrSet<const MDNode *, 16> ScopeNodes; |
David Majnemer | c77a139 | 2016-08-15 02:23:46 +0000 | [diff] [blame] | 156 | collectMDInDomain(Scopes, Domain, ScopeNodes); |
David Majnemer | ddc7ab2 | 2016-08-15 02:23:48 +0000 | [diff] [blame] | 157 | if (ScopeNodes.empty()) |
David Majnemer | c77a139 | 2016-08-15 02:23:46 +0000 | [diff] [blame] | 158 | continue; |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 159 | |
David Majnemer | 8b8869f | 2016-08-15 02:23:50 +0000 | [diff] [blame] | 160 | SmallPtrSet<const MDNode *, 16> NANodes; |
| 161 | collectMDInDomain(NoAlias, Domain, NANodes); |
| 162 | |
David Majnemer | c77a139 | 2016-08-15 02:23:46 +0000 | [diff] [blame] | 163 | // To not alias, all of the nodes in ScopeNodes must be in NANodes. |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 164 | bool FoundAll = true; |
David Majnemer | c77a139 | 2016-08-15 02:23:46 +0000 | [diff] [blame] | 165 | for (const MDNode *SMD : ScopeNodes) |
| 166 | if (!NANodes.count(SMD)) { |
| 167 | FoundAll = false; |
| 168 | break; |
| 169 | } |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 170 | |
| 171 | if (FoundAll) |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | return true; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 178 | AnalysisKey ScopedNoAliasAA::Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 179 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 180 | ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 181 | FunctionAnalysisManager &AM) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 182 | return ScopedNoAliasAAResult(); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 185 | char ScopedNoAliasAAWrapperPass::ID = 0; |
Eugene Zelenko | be709f2 | 2017-08-18 23:51:26 +0000 | [diff] [blame] | 186 | |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 187 | INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias", |
| 188 | "Scoped NoAlias Alias Analysis", false, true) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 189 | |
| 190 | ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() { |
| 191 | return new ScopedNoAliasAAWrapperPass(); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 194 | ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) { |
| 195 | initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 198 | bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 199 | Result.reset(new ScopedNoAliasAAResult()); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 200 | return false; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 203 | bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) { |
| 204 | Result.reset(); |
| 205 | return false; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 208 | void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 209 | AU.setPreservesAll(); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 210 | } |