Loads.h 9.3 KB

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