BasicAliasAnalysis.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- BasicAliasAnalysis.h - Stateless, local Alias Analysis ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. /// This is the interface for LLVM's primary stateless and local alias analysis.
  15. ///
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_BASICALIASANALYSIS_H
  18. #define LLVM_ANALYSIS_BASICALIASANALYSIS_H
  19. #include "llvm/ADT/Optional.h"
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/Analysis/AliasAnalysis.h"
  22. #include "llvm/IR/PassManager.h"
  23. #include "llvm/Pass.h"
  24. #include <algorithm>
  25. #include <cstdint>
  26. #include <memory>
  27. #include <utility>
  28. namespace llvm {
  29. class AssumptionCache;
  30. class BasicBlock;
  31. class DataLayout;
  32. class DominatorTree;
  33. class Function;
  34. class GEPOperator;
  35. class PHINode;
  36. class SelectInst;
  37. class TargetLibraryInfo;
  38. class PhiValues;
  39. class Value;
  40. /// This is the AA result object for the basic, local, and stateless alias
  41. /// analysis. It implements the AA query interface in an entirely stateless
  42. /// manner. As one consequence, it is never invalidated due to IR changes.
  43. /// While it does retain some storage, that is used as an optimization and not
  44. /// to preserve information from query to query. However it does retain handles
  45. /// to various other analyses and must be recomputed when those analyses are.
  46. class BasicAAResult : public AAResultBase<BasicAAResult> {
  47. friend AAResultBase<BasicAAResult>;
  48. const DataLayout &DL;
  49. const Function &F;
  50. const TargetLibraryInfo &TLI;
  51. AssumptionCache &AC;
  52. DominatorTree *DT;
  53. PhiValues *PV;
  54. public:
  55. BasicAAResult(const DataLayout &DL, const Function &F,
  56. const TargetLibraryInfo &TLI, AssumptionCache &AC,
  57. DominatorTree *DT = nullptr, PhiValues *PV = nullptr)
  58. : DL(DL), F(F), TLI(TLI), AC(AC), DT(DT), PV(PV) {}
  59. BasicAAResult(const BasicAAResult &Arg)
  60. : AAResultBase(Arg), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI), AC(Arg.AC),
  61. DT(Arg.DT), PV(Arg.PV) {}
  62. BasicAAResult(BasicAAResult &&Arg)
  63. : AAResultBase(std::move(Arg)), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
  64. AC(Arg.AC), DT(Arg.DT), PV(Arg.PV) {}
  65. /// Handle invalidation events in the new pass manager.
  66. bool invalidate(Function &Fn, const PreservedAnalyses &PA,
  67. FunctionAnalysisManager::Invalidator &Inv);
  68. AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
  69. AAQueryInfo &AAQI);
  70. ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
  71. AAQueryInfo &AAQI);
  72. ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
  73. AAQueryInfo &AAQI);
  74. /// Chases pointers until we find a (constant global) or not.
  75. bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
  76. bool OrLocal);
  77. /// Get the location associated with a pointer argument of a callsite.
  78. ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
  79. /// Returns the behavior when calling the given call site.
  80. FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
  81. /// Returns the behavior when calling the given function. For use when the
  82. /// call site is not known.
  83. FunctionModRefBehavior getModRefBehavior(const Function *Fn);
  84. private:
  85. struct DecomposedGEP;
  86. /// Tracks phi nodes we have visited.
  87. ///
  88. /// When interpret "Value" pointer equality as value equality we need to make
  89. /// sure that the "Value" is not part of a cycle. Otherwise, two uses could
  90. /// come from different "iterations" of a cycle and see different values for
  91. /// the same "Value" pointer.
  92. ///
  93. /// The following example shows the problem:
  94. /// %p = phi(%alloca1, %addr2)
  95. /// %l = load %ptr
  96. /// %addr1 = gep, %alloca2, 0, %l
  97. /// %addr2 = gep %alloca2, 0, (%l + 1)
  98. /// alias(%p, %addr1) -> MayAlias !
  99. /// store %l, ...
  100. SmallPtrSet<const BasicBlock *, 8> VisitedPhiBBs;
  101. /// Tracks instructions visited by pointsToConstantMemory.
  102. SmallPtrSet<const Value *, 16> Visited;
  103. static DecomposedGEP
  104. DecomposeGEPExpression(const Value *V, const DataLayout &DL,
  105. AssumptionCache *AC, DominatorTree *DT);
  106. /// A Heuristic for aliasGEP that searches for a constant offset
  107. /// between the variables.
  108. ///
  109. /// GetLinearExpression has some limitations, as generally zext(%x + 1)
  110. /// != zext(%x) + zext(1) if the arithmetic overflows. GetLinearExpression
  111. /// will therefore conservatively refuse to decompose these expressions.
  112. /// However, we know that, for all %x, zext(%x) != zext(%x + 1), even if
  113. /// the addition overflows.
  114. bool
  115. constantOffsetHeuristic(const DecomposedGEP &GEP, LocationSize V1Size,
  116. LocationSize V2Size, AssumptionCache *AC,
  117. DominatorTree *DT);
  118. bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
  119. void subtractDecomposedGEPs(DecomposedGEP &DestGEP,
  120. const DecomposedGEP &SrcGEP);
  121. AliasResult aliasGEP(const GEPOperator *V1, LocationSize V1Size,
  122. const Value *V2, LocationSize V2Size,
  123. const Value *UnderlyingV1, const Value *UnderlyingV2,
  124. AAQueryInfo &AAQI);
  125. AliasResult aliasPHI(const PHINode *PN, LocationSize PNSize,
  126. const Value *V2, LocationSize V2Size, AAQueryInfo &AAQI);
  127. AliasResult aliasSelect(const SelectInst *SI, LocationSize SISize,
  128. const Value *V2, LocationSize V2Size,
  129. AAQueryInfo &AAQI);
  130. AliasResult aliasCheck(const Value *V1, LocationSize V1Size,
  131. const Value *V2, LocationSize V2Size,
  132. AAQueryInfo &AAQI);
  133. AliasResult aliasCheckRecursive(const Value *V1, LocationSize V1Size,
  134. const Value *V2, LocationSize V2Size,
  135. AAQueryInfo &AAQI, const Value *O1,
  136. const Value *O2);
  137. };
  138. /// Analysis pass providing a never-invalidated alias analysis result.
  139. class BasicAA : public AnalysisInfoMixin<BasicAA> {
  140. friend AnalysisInfoMixin<BasicAA>;
  141. static AnalysisKey Key;
  142. public:
  143. using Result = BasicAAResult;
  144. BasicAAResult run(Function &F, FunctionAnalysisManager &AM);
  145. };
  146. /// Legacy wrapper pass to provide the BasicAAResult object.
  147. class BasicAAWrapperPass : public FunctionPass {
  148. std::unique_ptr<BasicAAResult> Result;
  149. virtual void anchor();
  150. public:
  151. static char ID;
  152. BasicAAWrapperPass();
  153. BasicAAResult &getResult() { return *Result; }
  154. const BasicAAResult &getResult() const { return *Result; }
  155. bool runOnFunction(Function &F) override;
  156. void getAnalysisUsage(AnalysisUsage &AU) const override;
  157. };
  158. FunctionPass *createBasicAAWrapperPass();
  159. /// A helper for the legacy pass manager to create a \c BasicAAResult object
  160. /// populated to the best of our ability for a particular function when inside
  161. /// of a \c ModulePass or a \c CallGraphSCCPass.
  162. BasicAAResult createLegacyPMBasicAAResult(Pass &P, Function &F);
  163. /// This class is a functor to be used in legacy module or SCC passes for
  164. /// computing AA results for a function. We store the results in fields so that
  165. /// they live long enough to be queried, but we re-use them each time.
  166. class LegacyAARGetter {
  167. Pass &P;
  168. Optional<BasicAAResult> BAR;
  169. Optional<AAResults> AAR;
  170. public:
  171. LegacyAARGetter(Pass &P) : P(P) {}
  172. AAResults &operator()(Function &F) {
  173. BAR.emplace(createLegacyPMBasicAAResult(P, F));
  174. AAR.emplace(createLegacyPMAAResults(P, F, *BAR));
  175. return *AAR;
  176. }
  177. };
  178. } // end namespace llvm
  179. #endif // LLVM_ANALYSIS_BASICALIASANALYSIS_H
  180. #ifdef __GNUC__
  181. #pragma GCC diagnostic pop
  182. #endif