BasicAliasAnalysis.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/SmallPtrSet.h"
  20. #include "llvm/Analysis/AliasAnalysis.h"
  21. #include "llvm/IR/PassManager.h"
  22. #include "llvm/Pass.h"
  23. #include <memory>
  24. #include <optional>
  25. #include <utility>
  26. namespace llvm {
  27. class AssumptionCache;
  28. class DataLayout;
  29. class DominatorTree;
  30. class Function;
  31. class GEPOperator;
  32. class PHINode;
  33. class SelectInst;
  34. class TargetLibraryInfo;
  35. class PhiValues;
  36. class Value;
  37. /// This is the AA result object for the basic, local, and stateless alias
  38. /// analysis. It implements the AA query interface in an entirely stateless
  39. /// manner. As one consequence, it is never invalidated due to IR changes.
  40. /// While it does retain some storage, that is used as an optimization and not
  41. /// to preserve information from query to query. However it does retain handles
  42. /// to various other analyses and must be recomputed when those analyses are.
  43. class BasicAAResult : public AAResultBase {
  44. const DataLayout &DL;
  45. const Function &F;
  46. const TargetLibraryInfo &TLI;
  47. AssumptionCache &AC;
  48. DominatorTree *DT;
  49. public:
  50. BasicAAResult(const DataLayout &DL, const Function &F,
  51. const TargetLibraryInfo &TLI, AssumptionCache &AC,
  52. DominatorTree *DT = nullptr)
  53. : DL(DL), F(F), TLI(TLI), AC(AC), DT(DT) {}
  54. BasicAAResult(const BasicAAResult &Arg)
  55. : AAResultBase(Arg), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI), AC(Arg.AC),
  56. DT(Arg.DT) {}
  57. BasicAAResult(BasicAAResult &&Arg)
  58. : AAResultBase(std::move(Arg)), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
  59. AC(Arg.AC), DT(Arg.DT) {}
  60. /// Handle invalidation events in the new pass manager.
  61. bool invalidate(Function &Fn, const PreservedAnalyses &PA,
  62. FunctionAnalysisManager::Invalidator &Inv);
  63. AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
  64. AAQueryInfo &AAQI, const Instruction *CtxI);
  65. ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
  66. AAQueryInfo &AAQI);
  67. ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
  68. AAQueryInfo &AAQI);
  69. /// Returns a bitmask that should be unconditionally applied to the ModRef
  70. /// info of a memory location. This allows us to eliminate Mod and/or Ref
  71. /// from the ModRef info based on the knowledge that the memory location
  72. /// points to constant and/or locally-invariant memory.
  73. ///
  74. /// If IgnoreLocals is true, then this method returns NoModRef for memory
  75. /// that points to a local alloca.
  76. ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI,
  77. bool IgnoreLocals = false);
  78. /// Get the location associated with a pointer argument of a callsite.
  79. ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
  80. /// Returns the behavior when calling the given call site.
  81. MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI);
  82. /// Returns the behavior when calling the given function. For use when the
  83. /// call site is not known.
  84. MemoryEffects getMemoryEffects(const Function *Fn);
  85. private:
  86. struct DecomposedGEP;
  87. /// Tracks instructions visited by pointsToConstantMemory.
  88. SmallPtrSet<const Value *, 16> Visited;
  89. static DecomposedGEP
  90. DecomposeGEPExpression(const Value *V, const DataLayout &DL,
  91. AssumptionCache *AC, DominatorTree *DT);
  92. /// A Heuristic for aliasGEP that searches for a constant offset
  93. /// between the variables.
  94. ///
  95. /// GetLinearExpression has some limitations, as generally zext(%x + 1)
  96. /// != zext(%x) + zext(1) if the arithmetic overflows. GetLinearExpression
  97. /// will therefore conservatively refuse to decompose these expressions.
  98. /// However, we know that, for all %x, zext(%x) != zext(%x + 1), even if
  99. /// the addition overflows.
  100. bool constantOffsetHeuristic(const DecomposedGEP &GEP, LocationSize V1Size,
  101. LocationSize V2Size, AssumptionCache *AC,
  102. DominatorTree *DT, const AAQueryInfo &AAQI);
  103. bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2,
  104. const AAQueryInfo &AAQI);
  105. void subtractDecomposedGEPs(DecomposedGEP &DestGEP,
  106. const DecomposedGEP &SrcGEP,
  107. const AAQueryInfo &AAQI);
  108. AliasResult aliasGEP(const GEPOperator *V1, LocationSize V1Size,
  109. const Value *V2, LocationSize V2Size,
  110. const Value *UnderlyingV1, const Value *UnderlyingV2,
  111. AAQueryInfo &AAQI);
  112. AliasResult aliasPHI(const PHINode *PN, LocationSize PNSize,
  113. const Value *V2, LocationSize V2Size, AAQueryInfo &AAQI);
  114. AliasResult aliasSelect(const SelectInst *SI, LocationSize SISize,
  115. const Value *V2, LocationSize V2Size,
  116. AAQueryInfo &AAQI);
  117. AliasResult aliasCheck(const Value *V1, LocationSize V1Size, const Value *V2,
  118. LocationSize V2Size, AAQueryInfo &AAQI,
  119. const Instruction *CtxI);
  120. AliasResult aliasCheckRecursive(const Value *V1, LocationSize V1Size,
  121. const Value *V2, LocationSize V2Size,
  122. AAQueryInfo &AAQI, const Value *O1,
  123. const Value *O2);
  124. };
  125. /// Analysis pass providing a never-invalidated alias analysis result.
  126. class BasicAA : public AnalysisInfoMixin<BasicAA> {
  127. friend AnalysisInfoMixin<BasicAA>;
  128. static AnalysisKey Key;
  129. public:
  130. using Result = BasicAAResult;
  131. BasicAAResult run(Function &F, FunctionAnalysisManager &AM);
  132. };
  133. /// Legacy wrapper pass to provide the BasicAAResult object.
  134. class BasicAAWrapperPass : public FunctionPass {
  135. std::unique_ptr<BasicAAResult> Result;
  136. virtual void anchor();
  137. public:
  138. static char ID;
  139. BasicAAWrapperPass();
  140. BasicAAResult &getResult() { return *Result; }
  141. const BasicAAResult &getResult() const { return *Result; }
  142. bool runOnFunction(Function &F) override;
  143. void getAnalysisUsage(AnalysisUsage &AU) const override;
  144. };
  145. FunctionPass *createBasicAAWrapperPass();
  146. /// A helper for the legacy pass manager to create a \c BasicAAResult object
  147. /// populated to the best of our ability for a particular function when inside
  148. /// of a \c ModulePass or a \c CallGraphSCCPass.
  149. BasicAAResult createLegacyPMBasicAAResult(Pass &P, Function &F);
  150. /// This class is a functor to be used in legacy module or SCC passes for
  151. /// computing AA results for a function. We store the results in fields so that
  152. /// they live long enough to be queried, but we re-use them each time.
  153. class LegacyAARGetter {
  154. Pass &P;
  155. std::optional<BasicAAResult> BAR;
  156. std::optional<AAResults> AAR;
  157. public:
  158. LegacyAARGetter(Pass &P) : P(P) {}
  159. AAResults &operator()(Function &F) {
  160. BAR.emplace(createLegacyPMBasicAAResult(P, F));
  161. AAR.emplace(createLegacyPMAAResults(P, F, *BAR));
  162. return *AAR;
  163. }
  164. };
  165. } // end namespace llvm
  166. #endif // LLVM_ANALYSIS_BASICALIASANALYSIS_H
  167. #ifdef __GNUC__
  168. #pragma GCC diagnostic pop
  169. #endif