| 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. |
| 11 | // |
| 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. |
| 16 | // |
| 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" } |
| 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 |
| 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. |
| 31 | // |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | #include "llvm/Analysis/ScopedNoAliasAA.h" |
| 35 | #include "llvm/ADT/SetOperations.h" |
| 36 | #include "llvm/ADT/SmallPtrSet.h" |
| 37 | #include "llvm/Analysis/MemoryLocation.h" |
| 38 | #include "llvm/IR/InstrTypes.h" |
| 39 | #include "llvm/IR/LLVMContext.h" |
| 40 | #include "llvm/IR/Metadata.h" |
| 41 | #include "llvm/InitializePasses.h" |
| 42 | #include "llvm/Pass.h" |
| 43 | #include "llvm/Support/Casting.h" |
| 44 | #include "llvm/Support/CommandLine.h" |
| 45 | |
| 46 | using namespace llvm; |
| 47 | |
| 48 | // A handy option for disabling scoped no-alias functionality. The same effect |
| 49 | // can also be achieved by stripping the associated metadata tags from IR, but |
| 50 | // this option is sometimes more convenient. |
| 51 | static cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias" , |
| 52 | cl::init(Val: true), cl::Hidden); |
| 53 | |
| 54 | AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA, |
| 55 | const MemoryLocation &LocB, |
| 56 | AAQueryInfo &AAQI, |
| 57 | const Instruction *) { |
| 58 | if (!EnableScopedNoAlias) |
| 59 | return AliasResult::MayAlias; |
| 60 | |
| 61 | // Get the attached MDNodes. |
| 62 | const MDNode *AScopes = LocA.AATags.Scope, *BScopes = LocB.AATags.Scope; |
| 63 | |
| 64 | const MDNode *ANoAlias = LocA.AATags.NoAlias, *BNoAlias = LocB.AATags.NoAlias; |
| 65 | |
| 66 | if (!mayAliasInScopes(Scopes: AScopes, NoAlias: BNoAlias)) |
| 67 | return AliasResult::NoAlias; |
| 68 | |
| 69 | if (!mayAliasInScopes(Scopes: BScopes, NoAlias: ANoAlias)) |
| 70 | return AliasResult::NoAlias; |
| 71 | |
| 72 | return AliasResult::MayAlias; |
| 73 | } |
| 74 | |
| 75 | ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call, |
| 76 | const MemoryLocation &Loc, |
| 77 | AAQueryInfo &AAQI) { |
| 78 | if (!EnableScopedNoAlias) |
| 79 | return ModRefInfo::ModRef; |
| 80 | |
| 81 | if (!mayAliasInScopes(Scopes: Loc.AATags.Scope, |
| 82 | NoAlias: Call->getMetadata(KindID: LLVMContext::MD_noalias))) |
| 83 | return ModRefInfo::NoModRef; |
| 84 | |
| 85 | if (!mayAliasInScopes(Scopes: Call->getMetadata(KindID: LLVMContext::MD_alias_scope), |
| 86 | NoAlias: Loc.AATags.NoAlias)) |
| 87 | return ModRefInfo::NoModRef; |
| 88 | |
| 89 | return ModRefInfo::ModRef; |
| 90 | } |
| 91 | |
| 92 | ModRefInfo ScopedNoAliasAAResult::getModRefInfo(const CallBase *Call1, |
| 93 | const CallBase *Call2, |
| 94 | AAQueryInfo &AAQI) { |
| 95 | if (!EnableScopedNoAlias) |
| 96 | return ModRefInfo::ModRef; |
| 97 | |
| 98 | if (!mayAliasInScopes(Scopes: Call1->getMetadata(KindID: LLVMContext::MD_alias_scope), |
| 99 | NoAlias: Call2->getMetadata(KindID: LLVMContext::MD_noalias))) |
| 100 | return ModRefInfo::NoModRef; |
| 101 | |
| 102 | if (!mayAliasInScopes(Scopes: Call2->getMetadata(KindID: LLVMContext::MD_alias_scope), |
| 103 | NoAlias: Call1->getMetadata(KindID: LLVMContext::MD_noalias))) |
| 104 | return ModRefInfo::NoModRef; |
| 105 | |
| 106 | return ModRefInfo::ModRef; |
| 107 | } |
| 108 | |
| 109 | static void collectMDInDomain(const MDNode *List, const MDNode *Domain, |
| 110 | SmallPtrSetImpl<const MDNode *> &Nodes) { |
| 111 | for (const MDOperand &MDOp : List->operands()) |
| 112 | if (const MDNode *MD = dyn_cast<MDNode>(Val: MDOp)) |
| 113 | if (AliasScopeNode(MD).getDomain() == Domain) |
| 114 | Nodes.insert(Ptr: MD); |
| 115 | } |
| 116 | |
| 117 | /// Collect the set of scoped domains relevant to the noalias scopes. |
| 118 | void ScopedNoAliasAAResult::collectScopedDomains( |
| 119 | const MDNode *NoAlias, SmallPtrSetImpl<const MDNode *> &Domains) const { |
| 120 | if (!NoAlias) |
| 121 | return; |
| 122 | assert(Domains.empty() && "Domains should be empty" ); |
| 123 | for (const MDOperand &MDOp : NoAlias->operands()) |
| 124 | if (const MDNode *NAMD = dyn_cast<MDNode>(Val: MDOp)) |
| 125 | if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain()) |
| 126 | Domains.insert(Ptr: Domain); |
| 127 | } |
| 128 | |
| 129 | bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes, |
| 130 | const MDNode *NoAlias) const { |
| 131 | if (!Scopes || !NoAlias) |
| 132 | return true; |
| 133 | |
| 134 | // Collect the set of scope domains relevant to the noalias scopes. |
| 135 | SmallPtrSet<const MDNode *, 16> Domains; |
| 136 | collectScopedDomains(NoAlias, Domains); |
| 137 | |
| 138 | // We alias unless, for some domain, the set of noalias scopes in that domain |
| 139 | // is a superset of the set of alias scopes in that domain. |
| 140 | for (const MDNode *Domain : Domains) { |
| 141 | SmallPtrSet<const MDNode *, 16> ScopeNodes; |
| 142 | collectMDInDomain(List: Scopes, Domain, Nodes&: ScopeNodes); |
| 143 | if (ScopeNodes.empty()) |
| 144 | continue; |
| 145 | |
| 146 | SmallPtrSet<const MDNode *, 16> NANodes; |
| 147 | collectMDInDomain(List: NoAlias, Domain, Nodes&: NANodes); |
| 148 | |
| 149 | // To not alias, all of the nodes in ScopeNodes must be in NANodes. |
| 150 | if (llvm::set_is_subset(S1: ScopeNodes, S2: NANodes)) |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | AnalysisKey ScopedNoAliasAA::Key; |
| 158 | |
| 159 | ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F, |
| 160 | FunctionAnalysisManager &AM) { |
| 161 | return ScopedNoAliasAAResult(); |
| 162 | } |
| 163 | |
| 164 | char ScopedNoAliasAAWrapperPass::ID = 0; |
| 165 | |
| 166 | INITIALIZE_PASS(ScopedNoAliasAAWrapperPass, "scoped-noalias-aa" , |
| 167 | "Scoped NoAlias Alias Analysis" , false, true) |
| 168 | |
| 169 | ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() { |
| 170 | return new ScopedNoAliasAAWrapperPass(); |
| 171 | } |
| 172 | |
| 173 | ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) {} |
| 174 | |
| 175 | bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) { |
| 176 | Result.reset(p: new ScopedNoAliasAAResult()); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) { |
| 181 | Result.reset(); |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 186 | AU.setPreservesAll(); |
| 187 | } |
| 188 | |