Hal Finkel | 9414665 | 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 | 029cde6 | 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 | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 17 | // |
Hal Finkel | 029cde6 | 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 | 9414665 | 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 | f234168 | 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 | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 30 | // scopes in that domain of some other instruction, then the two memory |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 31 | // accesses are assumed not to alias. |
| 32 | // |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chandler Carruth | 42ff448 | 2015-08-14 02:55:50 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/ScopedNoAliasAA.h" |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallPtrSet.h" |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Constants.h" |
| 38 | #include "llvm/IR/LLVMContext.h" |
| 39 | #include "llvm/IR/Metadata.h" |
| 40 | #include "llvm/IR/Module.h" |
| 41 | #include "llvm/Pass.h" |
| 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", |
| 50 | cl::init(true)); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 51 | |
| 52 | namespace { |
Sanjay Patel | c5d29aa | 2016-01-13 17:43:35 +0000 | [diff] [blame] | 53 | /// This is a simple wrapper around an MDNode which provides a higher-level |
| 54 | /// interface by hiding the details of how alias analysis information is encoded |
| 55 | /// in its operands. |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 56 | class AliasScopeNode { |
| 57 | const MDNode *Node; |
| 58 | |
| 59 | public: |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 60 | AliasScopeNode() : Node(nullptr) {} |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 61 | explicit AliasScopeNode(const MDNode *N) : Node(N) {} |
| 62 | |
Sanjay Patel | c5d29aa | 2016-01-13 17:43:35 +0000 | [diff] [blame] | 63 | /// Get the MDNode for this AliasScopeNode. |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 64 | const MDNode *getNode() const { return Node; } |
| 65 | |
Sanjay Patel | c5d29aa | 2016-01-13 17:43:35 +0000 | [diff] [blame] | 66 | /// Get the MDNode for this AliasScopeNode's domain. |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 67 | const MDNode *getDomain() const { |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 68 | if (Node->getNumOperands() < 2) |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 69 | return nullptr; |
| 70 | return dyn_cast_or_null<MDNode>(Node->getOperand(1)); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 71 | } |
| 72 | }; |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 73 | } // end of anonymous namespace |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 74 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 75 | AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA, |
| 76 | const MemoryLocation &LocB) { |
| 77 | if (!EnableScopedNoAlias) |
| 78 | return AAResultBase::alias(LocA, LocB); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 79 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 80 | // Get the attached MDNodes. |
| 81 | const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope; |
| 82 | |
| 83 | const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias; |
| 84 | |
| 85 | if (!mayAliasInScopes(AScopes, BNoAlias)) |
| 86 | return NoAlias; |
| 87 | |
| 88 | if (!mayAliasInScopes(BScopes, ANoAlias)) |
| 89 | return NoAlias; |
| 90 | |
| 91 | // If they may alias, chain to the next AliasAnalysis. |
| 92 | return AAResultBase::alias(LocA, LocB); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 95 | ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS, |
| 96 | const MemoryLocation &Loc) { |
| 97 | if (!EnableScopedNoAlias) |
| 98 | return AAResultBase::getModRefInfo(CS, Loc); |
| 99 | |
| 100 | if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata( |
| 101 | LLVMContext::MD_noalias))) |
| 102 | return MRI_NoModRef; |
| 103 | |
| 104 | if (!mayAliasInScopes( |
| 105 | CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope), |
| 106 | Loc.AATags.NoAlias)) |
| 107 | return MRI_NoModRef; |
| 108 | |
| 109 | return AAResultBase::getModRefInfo(CS, Loc); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 112 | ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS1, |
| 113 | ImmutableCallSite CS2) { |
| 114 | if (!EnableScopedNoAlias) |
| 115 | return AAResultBase::getModRefInfo(CS1, CS2); |
| 116 | |
| 117 | if (!mayAliasInScopes( |
| 118 | CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope), |
| 119 | CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias))) |
| 120 | return MRI_NoModRef; |
| 121 | |
| 122 | if (!mayAliasInScopes( |
| 123 | CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope), |
| 124 | CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias))) |
| 125 | return MRI_NoModRef; |
| 126 | |
| 127 | return AAResultBase::getModRefInfo(CS1, CS2); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 130 | void ScopedNoAliasAAResult::collectMDInDomain( |
Chandler Carruth | 496b7fb | 2015-08-14 02:46:07 +0000 | [diff] [blame] | 131 | const MDNode *List, const MDNode *Domain, |
| 132 | SmallPtrSetImpl<const MDNode *> &Nodes) const { |
Sanjay Patel | da08082 | 2016-01-13 18:37:28 +0000 | [diff] [blame] | 133 | for (const MDOperand &MDOp : List->operands()) |
| 134 | if (const MDNode *MD = dyn_cast<MDNode>(MDOp)) |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 135 | if (AliasScopeNode(MD).getDomain() == Domain) |
| 136 | Nodes.insert(MD); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 139 | bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes, |
| 140 | const MDNode *NoAlias) const { |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 141 | if (!Scopes || !NoAlias) |
| 142 | return true; |
| 143 | |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 144 | // Collect the set of scope domains relevant to the noalias scopes. |
| 145 | SmallPtrSet<const MDNode *, 16> Domains; |
Sanjay Patel | da08082 | 2016-01-13 18:37:28 +0000 | [diff] [blame] | 146 | for (const MDOperand &MDOp : NoAlias->operands()) |
| 147 | if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp)) |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 148 | if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain()) |
| 149 | Domains.insert(Domain); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 150 | |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 151 | // We alias unless, for some domain, the set of noalias scopes in that domain |
| 152 | // is a superset of the set of alias scopes in that domain. |
| 153 | for (const MDNode *Domain : Domains) { |
| 154 | SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes; |
| 155 | collectMDInDomain(NoAlias, Domain, NANodes); |
| 156 | collectMDInDomain(Scopes, Domain, ScopeNodes); |
| 157 | if (!ScopeNodes.size()) |
| 158 | continue; |
| 159 | |
| 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; |
| 166 | } |
| 167 | |
| 168 | if (FoundAll) |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | return true; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 175 | char ScopedNoAliasAA::PassID; |
| 176 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 177 | ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F, |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame^] | 178 | AnalysisManager<Function> &AM) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 179 | return ScopedNoAliasAAResult(); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 182 | char ScopedNoAliasAAWrapperPass::ID = 0; |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 183 | INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias", |
| 184 | "Scoped NoAlias Alias Analysis", false, true) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 185 | |
| 186 | ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() { |
| 187 | return new ScopedNoAliasAAWrapperPass(); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 190 | ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) { |
| 191 | initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry()); |
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 | bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) { |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 195 | Result.reset(new ScopedNoAliasAAResult()); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 196 | return false; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 199 | bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) { |
| 200 | Result.reset(); |
| 201 | return false; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 204 | void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 205 | AU.setPreservesAll(); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 206 | } |