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 |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 29 | // has a set of noalias scopes in some domain that is superset of the alias |
| 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" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Constants.h" |
| 39 | #include "llvm/IR/LLVMContext.h" |
| 40 | #include "llvm/IR/Metadata.h" |
| 41 | #include "llvm/IR/Module.h" |
| 42 | #include "llvm/Pass.h" |
| 43 | #include "llvm/Support/CommandLine.h" |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame^] | 44 | |
Hal Finkel | 9414665 | 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 | 496b7fb | 2015-08-14 02:46:07 +0000 | [diff] [blame] | 50 | static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias", |
| 51 | cl::init(true)); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 52 | |
| 53 | namespace { |
| 54 | /// AliasScopeNode - This is a simple wrapper around an MDNode which provides |
| 55 | /// a higher-level interface by hiding the details of how alias analysis |
| 56 | /// information is encoded in its operands. |
| 57 | class AliasScopeNode { |
| 58 | const MDNode *Node; |
| 59 | |
| 60 | public: |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame^] | 61 | AliasScopeNode() : Node(nullptr) {} |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 62 | explicit AliasScopeNode(const MDNode *N) : Node(N) {} |
| 63 | |
| 64 | /// getNode - Get the MDNode for this AliasScopeNode. |
| 65 | const MDNode *getNode() const { return Node; } |
| 66 | |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 67 | /// getDomain - Get the MDNode for this AliasScopeNode's domain. |
| 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 | }; |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame^] | 74 | } // end of anonymous namespace |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 75 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 76 | AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA, |
| 77 | const MemoryLocation &LocB) { |
| 78 | if (!EnableScopedNoAlias) |
| 79 | return AAResultBase::alias(LocA, LocB); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 80 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 81 | // Get the attached MDNodes. |
| 82 | const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope; |
| 83 | |
| 84 | const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias; |
| 85 | |
| 86 | if (!mayAliasInScopes(AScopes, BNoAlias)) |
| 87 | return NoAlias; |
| 88 | |
| 89 | if (!mayAliasInScopes(BScopes, ANoAlias)) |
| 90 | return NoAlias; |
| 91 | |
| 92 | // If they may alias, chain to the next AliasAnalysis. |
| 93 | return AAResultBase::alias(LocA, LocB); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 96 | ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS, |
| 97 | const MemoryLocation &Loc) { |
| 98 | if (!EnableScopedNoAlias) |
| 99 | return AAResultBase::getModRefInfo(CS, Loc); |
| 100 | |
| 101 | if (!mayAliasInScopes(Loc.AATags.Scope, CS.getInstruction()->getMetadata( |
| 102 | LLVMContext::MD_noalias))) |
| 103 | return MRI_NoModRef; |
| 104 | |
| 105 | if (!mayAliasInScopes( |
| 106 | CS.getInstruction()->getMetadata(LLVMContext::MD_alias_scope), |
| 107 | Loc.AATags.NoAlias)) |
| 108 | return MRI_NoModRef; |
| 109 | |
| 110 | return AAResultBase::getModRefInfo(CS, Loc); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 113 | ModRefInfo ScopedNoAliasAAResult::getModRefInfo(ImmutableCallSite CS1, |
| 114 | ImmutableCallSite CS2) { |
| 115 | if (!EnableScopedNoAlias) |
| 116 | return AAResultBase::getModRefInfo(CS1, CS2); |
| 117 | |
| 118 | if (!mayAliasInScopes( |
| 119 | CS1.getInstruction()->getMetadata(LLVMContext::MD_alias_scope), |
| 120 | CS2.getInstruction()->getMetadata(LLVMContext::MD_noalias))) |
| 121 | return MRI_NoModRef; |
| 122 | |
| 123 | if (!mayAliasInScopes( |
| 124 | CS2.getInstruction()->getMetadata(LLVMContext::MD_alias_scope), |
| 125 | CS1.getInstruction()->getMetadata(LLVMContext::MD_noalias))) |
| 126 | return MRI_NoModRef; |
| 127 | |
| 128 | return AAResultBase::getModRefInfo(CS1, CS2); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 131 | void ScopedNoAliasAAResult::collectMDInDomain( |
Chandler Carruth | 496b7fb | 2015-08-14 02:46:07 +0000 | [diff] [blame] | 132 | const MDNode *List, const MDNode *Domain, |
| 133 | SmallPtrSetImpl<const MDNode *> &Nodes) const { |
Hal Finkel | 029cde6 | 2014-07-25 15:50:02 +0000 | [diff] [blame] | 134 | for (unsigned i = 0, ie = List->getNumOperands(); i != ie; ++i) |
| 135 | if (const MDNode *MD = dyn_cast<MDNode>(List->getOperand(i))) |
| 136 | if (AliasScopeNode(MD).getDomain() == Domain) |
| 137 | Nodes.insert(MD); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 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; |
| 147 | for (unsigned i = 0, ie = NoAlias->getNumOperands(); i != ie; ++i) |
| 148 | if (const MDNode *NAMD = dyn_cast<MDNode>(NoAlias->getOperand(i))) |
| 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) { |
| 155 | SmallPtrSet<const MDNode *, 16> NANodes, ScopeNodes; |
| 156 | collectMDInDomain(NoAlias, Domain, NANodes); |
| 157 | collectMDInDomain(Scopes, Domain, ScopeNodes); |
| 158 | if (!ScopeNodes.size()) |
| 159 | continue; |
| 160 | |
| 161 | // To not alias, all of the nodes in ScopeNodes must be in NANodes. |
| 162 | bool FoundAll = true; |
| 163 | for (const MDNode *SMD : ScopeNodes) |
| 164 | if (!NANodes.count(SMD)) { |
| 165 | FoundAll = false; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | if (FoundAll) |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | return true; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 176 | ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F, |
| 177 | AnalysisManager<Function> *AM) { |
| 178 | return ScopedNoAliasAAResult(AM->getResult<TargetLibraryAnalysis>(F)); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 181 | char ScopedNoAliasAA::PassID; |
| 182 | |
| 183 | char ScopedNoAliasAAWrapperPass::ID = 0; |
| 184 | INITIALIZE_PASS_BEGIN(ScopedNoAliasAAWrapperPass, "scoped-noalias", |
| 185 | "Scoped NoAlias Alias Analysis", false, true) |
| 186 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 187 | INITIALIZE_PASS_END(ScopedNoAliasAAWrapperPass, "scoped-noalias", |
| 188 | "Scoped NoAlias Alias Analysis", false, true) |
| 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) { |
| 199 | Result.reset(new ScopedNoAliasAAResult( |
| 200 | getAnalysis<TargetLibraryInfoWrapperPass>().getTLI())); |
| 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 | bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) { |
| 205 | Result.reset(); |
| 206 | return false; |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 209 | void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 210 | AU.setPreservesAll(); |
| 211 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 212 | } |