Loads.h 8.5 KB

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