Loads.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Loads.h - Local load analysis --------------------------------------===//
  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. //
  14. // This file declares simple local analyses for load instructions.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_LOADS_H
  18. #define LLVM_ANALYSIS_LOADS_H
  19. #include "llvm/IR/BasicBlock.h"
  20. #include "llvm/Support/CommandLine.h"
  21. namespace llvm {
  22. class AAResults;
  23. class AssumptionCache;
  24. class DataLayout;
  25. class DominatorTree;
  26. class Instruction;
  27. class LoadInst;
  28. class Loop;
  29. class MemoryLocation;
  30. class ScalarEvolution;
  31. class TargetLibraryInfo;
  32. /// Return true if this is always a dereferenceable pointer. If the context
  33. /// instruction is specified perform context-sensitive analysis and return true
  34. /// if the pointer is dereferenceable at the specified instruction.
  35. bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL,
  36. const Instruction *CtxI = nullptr,
  37. AssumptionCache *AC = nullptr,
  38. const DominatorTree *DT = nullptr,
  39. const TargetLibraryInfo *TLI = nullptr);
  40. /// Returns true if V is always a dereferenceable pointer with alignment
  41. /// greater or equal than requested. If the context instruction is specified
  42. /// performs context-sensitive analysis and returns true if the pointer is
  43. /// dereferenceable at the specified instruction.
  44. bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty,
  45. Align Alignment, const DataLayout &DL,
  46. const Instruction *CtxI = nullptr,
  47. AssumptionCache *AC = nullptr,
  48. const DominatorTree *DT = nullptr,
  49. const TargetLibraryInfo *TLI = nullptr);
  50. /// Returns true if V is always dereferenceable for Size byte with alignment
  51. /// greater or equal than requested. If the context instruction is specified
  52. /// performs context-sensitive analysis and returns true if the pointer is
  53. /// dereferenceable at the specified instruction.
  54. bool isDereferenceableAndAlignedPointer(const Value *V, Align Alignment,
  55. const APInt &Size, const DataLayout &DL,
  56. const Instruction *CtxI = nullptr,
  57. AssumptionCache *AC = nullptr,
  58. const DominatorTree *DT = nullptr,
  59. const TargetLibraryInfo *TLI = nullptr);
  60. /// Return true if we know that executing a load from this value cannot trap.
  61. ///
  62. /// If DT and ScanFrom are specified this method performs context-sensitive
  63. /// analysis and returns true if it is safe to load immediately before ScanFrom.
  64. ///
  65. /// If it is not obviously safe to load from the specified pointer, we do a
  66. /// quick local scan of the basic block containing ScanFrom, to determine if
  67. /// the address is already accessed.
  68. bool isSafeToLoadUnconditionally(Value *V, Align Alignment, APInt &Size,
  69. const DataLayout &DL,
  70. Instruction *ScanFrom = nullptr,
  71. AssumptionCache *AC = nullptr,
  72. const DominatorTree *DT = nullptr,
  73. const TargetLibraryInfo *TLI = nullptr);
  74. /// Return true if we can prove that the given load (which is assumed to be
  75. /// within the specified loop) would access only dereferenceable memory, and
  76. /// be properly aligned on every iteration of the specified loop regardless of
  77. /// its placement within the loop. (i.e. does not require predication beyond
  78. /// that required by the header itself and could be hoisted into the header
  79. /// if desired.) This is more powerful than the variants above when the
  80. /// address loaded from is analyzeable by SCEV.
  81. bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
  82. ScalarEvolution &SE, DominatorTree &DT,
  83. AssumptionCache *AC = nullptr);
  84. /// Return true if we know that executing a load from this value cannot trap.
  85. ///
  86. /// If DT and ScanFrom are specified this method performs context-sensitive
  87. /// analysis and returns true if it is safe to load immediately before ScanFrom.
  88. ///
  89. /// If it is not obviously safe to load from the specified pointer, we do a
  90. /// quick local scan of the basic block containing ScanFrom, to determine if
  91. /// the address is already accessed.
  92. bool isSafeToLoadUnconditionally(Value *V, Type *Ty, Align Alignment,
  93. const DataLayout &DL,
  94. Instruction *ScanFrom = nullptr,
  95. AssumptionCache *AC = nullptr,
  96. const DominatorTree *DT = nullptr,
  97. const TargetLibraryInfo *TLI = nullptr);
  98. /// The default number of maximum instructions to scan in the block, used by
  99. /// FindAvailableLoadedValue().
  100. extern cl::opt<unsigned> DefMaxInstsToScan;
  101. /// Scan backwards to see if we have the value of the given load available
  102. /// locally within a small number of instructions.
  103. ///
  104. /// You can use this function to scan across multiple blocks: after you call
  105. /// this function, if ScanFrom points at the beginning of the block, it's safe
  106. /// to continue scanning the predecessors.
  107. ///
  108. /// Note that performing load CSE requires special care to make sure the
  109. /// metadata is set appropriately. In particular, aliasing metadata needs
  110. /// to be merged. (This doesn't matter for store-to-load forwarding because
  111. /// the only relevant load gets deleted.)
  112. ///
  113. /// \param Load The load we want to replace.
  114. /// \param ScanBB The basic block to scan.
  115. /// \param [in,out] ScanFrom The location to start scanning from. When this
  116. /// function returns, it points at the last instruction scanned.
  117. /// \param MaxInstsToScan The maximum number of instructions to scan. If this
  118. /// is zero, the whole block will be scanned.
  119. /// \param AA Optional pointer to alias analysis, to make the scan more
  120. /// precise.
  121. /// \param [out] IsLoadCSE Whether the returned value is a load from the same
  122. /// location in memory, as opposed to the value operand of a store.
  123. ///
  124. /// \returns The found value, or nullptr if no value is found.
  125. Value *FindAvailableLoadedValue(LoadInst *Load,
  126. BasicBlock *ScanBB,
  127. BasicBlock::iterator &ScanFrom,
  128. unsigned MaxInstsToScan = DefMaxInstsToScan,
  129. AAResults *AA = nullptr,
  130. bool *IsLoadCSE = nullptr,
  131. unsigned *NumScanedInst = nullptr);
  132. /// This overload provides a more efficient implementation of
  133. /// FindAvailableLoadedValue() for the case where we are not interested in
  134. /// finding the closest clobbering instruction if no available load is found.
  135. /// This overload cannot be used to scan across multiple blocks.
  136. Value *FindAvailableLoadedValue(LoadInst *Load, AAResults &AA, bool *IsLoadCSE,
  137. unsigned MaxInstsToScan = DefMaxInstsToScan);
  138. /// Scan backwards to see if we have the value of the given pointer available
  139. /// locally within a small number of instructions.
  140. ///
  141. /// You can use this function to scan across multiple blocks: after you call
  142. /// this function, if ScanFrom points at the beginning of the block, it's safe
  143. /// to continue scanning the predecessors.
  144. ///
  145. /// \param Loc The location we want the load and store to originate from.
  146. /// \param AccessTy The access type of the pointer.
  147. /// \param AtLeastAtomic Are we looking for at-least an atomic load/store ? In
  148. /// case it is false, we can return an atomic or non-atomic load or store. In
  149. /// case it is true, we need to return an atomic load or store.
  150. /// \param ScanBB The basic block to scan.
  151. /// \param [in,out] ScanFrom The location to start scanning from. When this
  152. /// function returns, it points at the last instruction scanned.
  153. /// \param MaxInstsToScan The maximum number of instructions to scan. If this
  154. /// is zero, the whole block will be scanned.
  155. /// \param AA Optional pointer to alias analysis, to make the scan more
  156. /// precise.
  157. /// \param [out] IsLoadCSE Whether the returned value is a load from the same
  158. /// location in memory, as opposed to the value operand of a store.
  159. ///
  160. /// \returns The found value, or nullptr if no value is found.
  161. Value *findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy,
  162. bool AtLeastAtomic, BasicBlock *ScanBB,
  163. BasicBlock::iterator &ScanFrom,
  164. unsigned MaxInstsToScan, AAResults *AA,
  165. bool *IsLoadCSE, unsigned *NumScanedInst);
  166. /// Returns true if a pointer value \p A can be replace with another pointer
  167. /// value \B if they are deemed equal through some means (e.g. information from
  168. /// conditions).
  169. /// NOTE: the current implementations is incomplete and unsound. It does not
  170. /// reject all invalid cases yet, but will be made stricter in the future. In
  171. /// particular this means returning true means unknown if replacement is safe.
  172. bool canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL,
  173. Instruction *CtxI);
  174. }
  175. #endif
  176. #ifdef __GNUC__
  177. #pragma GCC diagnostic pop
  178. #endif