ScopedNoAliasAA.cpp 6.7 KB

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