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