ScopedNoAliasAA.cpp revision 302408
1180744Sdes//===- ScopedNoAliasAA.cpp - Scoped No-Alias Alias Analysis ---------------===// 2180744Sdes// 3180744Sdes// The LLVM Compiler Infrastructure 4180744Sdes// 5180744Sdes// This file is distributed under the University of Illinois Open Source 6180744Sdes// License. See LICENSE.TXT for details. 7180744Sdes// 8180744Sdes//===----------------------------------------------------------------------===// 9180744Sdes// 10180744Sdes// This file defines the ScopedNoAlias alias-analysis pass, which implements 11180744Sdes// metadata-based scoped no-alias support. 12180744Sdes// 13180744Sdes// Alias-analysis scopes are defined by an id (which can be a string or some 14180744Sdes// other metadata node), a domain node, and an optional descriptive string. 15180744Sdes// A domain is defined by an id (which can be a string or some other metadata 16180744Sdes// node), and an optional descriptive string. 17180744Sdes// 18180744Sdes// !dom0 = metadata !{ metadata !"domain of foo()" } 19180744Sdes// !scope1 = metadata !{ metadata !scope1, metadata !dom0, metadata !"scope 1" } 20180744Sdes// !scope2 = metadata !{ metadata !scope2, metadata !dom0, metadata !"scope 2" } 21180744Sdes// 22180744Sdes// Loads and stores can be tagged with an alias-analysis scope, and also, with 23180744Sdes// a noalias tag for a specific scope: 24180744Sdes// 25180744Sdes// ... = load %ptr1, !alias.scope !{ !scope1 } 26180744Sdes// ... = load %ptr2, !alias.scope !{ !scope1, !scope2 }, !noalias !{ !scope1 } 27180744Sdes// 28180744Sdes// When evaluating an aliasing query, if one of the instructions is associated 29180744Sdes// has a set of noalias scopes in some domain that is a superset of the alias 30180744Sdes// scopes in that domain of some other instruction, then the two memory 31180744Sdes// accesses are assumed not to alias. 32180744Sdes// 33180744Sdes//===----------------------------------------------------------------------===// 34180744Sdes 35180744Sdes#include "llvm/Analysis/ScopedNoAliasAA.h" 36180744Sdes#include "llvm/ADT/SmallPtrSet.h" 37180744Sdes#include "llvm/Analysis/TargetLibraryInfo.h" 38180744Sdes#include "llvm/IR/Constants.h" 39180744Sdes#include "llvm/IR/LLVMContext.h" 40180744Sdes#include "llvm/IR/Metadata.h" 41180744Sdes#include "llvm/IR/Module.h" 42180744Sdes#include "llvm/Pass.h" 43180744Sdes#include "llvm/Support/CommandLine.h" 44180744Sdes 45180744Sdesusing namespace llvm; 46180744Sdes 47180744Sdes// A handy option for disabling scoped no-alias functionality. The same effect 48180744Sdes// can also be achieved by stripping the associated metadata tags from IR, but 49180744Sdes// this option is sometimes more convenient. 50180744Sdesstatic cl::opt<bool> EnableScopedNoAlias("enable-scoped-noalias", 51180744Sdes cl::init(true)); 52180744Sdes 53180744Sdesnamespace { 54180744Sdes/// AliasScopeNode - This is a simple wrapper around an MDNode which provides 55180744Sdes/// a higher-level interface by hiding the details of how alias analysis 56180744Sdes/// information is encoded in its operands. 57180744Sdesclass AliasScopeNode { 58180744Sdes const MDNode *Node; 59180744Sdes 60180744Sdespublic: 61180744Sdes AliasScopeNode() : Node(nullptr) {} 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 67 /// getDomain - Get the MDNode for this AliasScopeNode's domain. 68 const MDNode *getDomain() const { 69 if (Node->getNumOperands() < 2) 70 return nullptr; 71 return dyn_cast_or_null<MDNode>(Node->getOperand(1)); 72 } 73}; 74} // end of anonymous namespace 75 76AliasResult ScopedNoAliasAAResult::alias(const MemoryLocation &LocA, 77 const MemoryLocation &LocB) { 78 if (!EnableScopedNoAlias) 79 return AAResultBase::alias(LocA, LocB); 80 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); 94} 95 96ModRefInfo 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); 111} 112 113ModRefInfo 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); 129} 130 131void ScopedNoAliasAAResult::collectMDInDomain( 132 const MDNode *List, const MDNode *Domain, 133 SmallPtrSetImpl<const MDNode *> &Nodes) const { 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); 138} 139 140bool ScopedNoAliasAAResult::mayAliasInScopes(const MDNode *Scopes, 141 const MDNode *NoAlias) const { 142 if (!Scopes || !NoAlias) 143 return true; 144 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); 151 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; 174} 175 176ScopedNoAliasAAResult ScopedNoAliasAA::run(Function &F, 177 AnalysisManager<Function> *AM) { 178 return ScopedNoAliasAAResult(AM->getResult<TargetLibraryAnalysis>(F)); 179} 180 181char ScopedNoAliasAA::PassID; 182 183char ScopedNoAliasAAWrapperPass::ID = 0; 184INITIALIZE_PASS_BEGIN(ScopedNoAliasAAWrapperPass, "scoped-noalias", 185 "Scoped NoAlias Alias Analysis", false, true) 186INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 187INITIALIZE_PASS_END(ScopedNoAliasAAWrapperPass, "scoped-noalias", 188 "Scoped NoAlias Alias Analysis", false, true) 189 190ImmutablePass *llvm::createScopedNoAliasAAWrapperPass() { 191 return new ScopedNoAliasAAWrapperPass(); 192} 193 194ScopedNoAliasAAWrapperPass::ScopedNoAliasAAWrapperPass() : ImmutablePass(ID) { 195 initializeScopedNoAliasAAWrapperPassPass(*PassRegistry::getPassRegistry()); 196} 197 198bool ScopedNoAliasAAWrapperPass::doInitialization(Module &M) { 199 Result.reset(new ScopedNoAliasAAResult( 200 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI())); 201 return false; 202} 203 204bool ScopedNoAliasAAWrapperPass::doFinalization(Module &M) { 205 Result.reset(); 206 return false; 207} 208 209void ScopedNoAliasAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 210 AU.setPreservesAll(); 211 AU.addRequired<TargetLibraryInfoWrapperPass>(); 212} 213