LoopUtils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //===- llvm/Transforms/Utils/LoopUtils.h - Loop utilities -*- C++ -*-=========//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines some loop transformation utilities.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
  14. #define LLVM_TRANSFORMS_UTILS_LOOPUTILS_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/IR/Dominators.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. namespace llvm {
  19. class AliasAnalysis;
  20. class AliasSet;
  21. class AliasSetTracker;
  22. class AssumptionCache;
  23. class BasicBlock;
  24. class DataLayout;
  25. class DominatorTree;
  26. class Loop;
  27. class LoopInfo;
  28. class Pass;
  29. class PredIteratorCache;
  30. class ScalarEvolution;
  31. class TargetLibraryInfo;
  32. /// \brief Captures loop safety information.
  33. /// It keep information for loop & its header may throw exception.
  34. struct LICMSafetyInfo {
  35. bool MayThrow; // The current loop contains an instruction which
  36. // may throw.
  37. bool HeaderMayThrow; // Same as previous, but specific to loop header
  38. LICMSafetyInfo() : MayThrow(false), HeaderMayThrow(false)
  39. {}
  40. };
  41. /// The RecurrenceDescriptor is used to identify recurrences variables in a
  42. /// loop. Reduction is a special case of recurrence that has uses of the
  43. /// recurrence variable outside the loop. The method isReductionPHI identifies
  44. /// reductions that are basic recurrences.
  45. ///
  46. /// Basic recurrences are defined as the summation, product, OR, AND, XOR, min,
  47. /// or max of a set of terms. For example: for(i=0; i<n; i++) { total +=
  48. /// array[i]; } is a summation of array elements. Basic recurrences are a
  49. /// special case of chains of recurrences (CR). See ScalarEvolution for CR
  50. /// references.
  51. /// This struct holds information about recurrence variables.
  52. class RecurrenceDescriptor {
  53. public:
  54. /// This enum represents the kinds of recurrences that we support.
  55. enum RecurrenceKind {
  56. RK_NoRecurrence, ///< Not a recurrence.
  57. RK_IntegerAdd, ///< Sum of integers.
  58. RK_IntegerMult, ///< Product of integers.
  59. RK_IntegerOr, ///< Bitwise or logical OR of numbers.
  60. RK_IntegerAnd, ///< Bitwise or logical AND of numbers.
  61. RK_IntegerXor, ///< Bitwise or logical XOR of numbers.
  62. RK_IntegerMinMax, ///< Min/max implemented in terms of select(cmp()).
  63. RK_FloatAdd, ///< Sum of floats.
  64. RK_FloatMult, ///< Product of floats.
  65. RK_FloatMinMax ///< Min/max implemented in terms of select(cmp()).
  66. };
  67. // This enum represents the kind of minmax recurrence.
  68. enum MinMaxRecurrenceKind {
  69. MRK_Invalid,
  70. MRK_UIntMin,
  71. MRK_UIntMax,
  72. MRK_SIntMin,
  73. MRK_SIntMax,
  74. MRK_FloatMin,
  75. MRK_FloatMax
  76. };
  77. RecurrenceDescriptor()
  78. : StartValue(nullptr), LoopExitInstr(nullptr), Kind(RK_NoRecurrence),
  79. MinMaxKind(MRK_Invalid) {}
  80. RecurrenceDescriptor(Value *Start, Instruction *Exit, RecurrenceKind K,
  81. MinMaxRecurrenceKind MK)
  82. : StartValue(Start), LoopExitInstr(Exit), Kind(K), MinMaxKind(MK) {}
  83. /// This POD struct holds information about a potential recurrence operation.
  84. class InstDesc {
  85. public:
  86. InstDesc(bool IsRecur, Instruction *I)
  87. : IsRecurrence(IsRecur), PatternLastInst(I), MinMaxKind(MRK_Invalid) {}
  88. InstDesc(Instruction *I, MinMaxRecurrenceKind K)
  89. : IsRecurrence(true), PatternLastInst(I), MinMaxKind(K) {}
  90. bool isRecurrence() { return IsRecurrence; }
  91. MinMaxRecurrenceKind getMinMaxKind() { return MinMaxKind; }
  92. Instruction *getPatternInst() { return PatternLastInst; }
  93. private:
  94. // Is this instruction a recurrence candidate.
  95. bool IsRecurrence;
  96. // The last instruction in a min/max pattern (select of the select(icmp())
  97. // pattern), or the current recurrence instruction otherwise.
  98. Instruction *PatternLastInst;
  99. // If this is a min/max pattern the comparison predicate.
  100. MinMaxRecurrenceKind MinMaxKind;
  101. };
  102. /// Returns a struct describing if the instruction 'I' can be a recurrence
  103. /// variable of type 'Kind'. If the recurrence is a min/max pattern of
  104. /// select(icmp()) this function advances the instruction pointer 'I' from the
  105. /// compare instruction to the select instruction and stores this pointer in
  106. /// 'PatternLastInst' member of the returned struct.
  107. static InstDesc isRecurrenceInstr(Instruction *I, RecurrenceKind Kind,
  108. InstDesc &Prev, bool HasFunNoNaNAttr);
  109. /// Returns true if instuction I has multiple uses in Insts
  110. static bool hasMultipleUsesOf(Instruction *I,
  111. SmallPtrSetImpl<Instruction *> &Insts);
  112. /// Returns true if all uses of the instruction I is within the Set.
  113. static bool areAllUsesIn(Instruction *I, SmallPtrSetImpl<Instruction *> &Set);
  114. /// Returns a struct describing if the instruction if the instruction is a
  115. /// Select(ICmp(X, Y), X, Y) instruction pattern corresponding to a min(X, Y)
  116. /// or max(X, Y).
  117. static InstDesc isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev);
  118. /// Returns identity corresponding to the RecurrenceKind.
  119. static Constant *getRecurrenceIdentity(RecurrenceKind K, Type *Tp);
  120. /// Returns the opcode of binary operation corresponding to the
  121. /// RecurrenceKind.
  122. static unsigned getRecurrenceBinOp(RecurrenceKind Kind);
  123. /// Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
  124. static Value *createMinMaxOp(IRBuilder<> &Builder, MinMaxRecurrenceKind RK,
  125. Value *Left, Value *Right);
  126. /// Returns true if Phi is a reduction of type Kind and adds it to the
  127. /// RecurrenceDescriptor.
  128. static bool AddReductionVar(PHINode *Phi, RecurrenceKind Kind, Loop *TheLoop,
  129. bool HasFunNoNaNAttr,
  130. RecurrenceDescriptor &RedDes);
  131. /// Returns true if Phi is a reduction in TheLoop. The RecurrenceDescriptor is
  132. /// returned in RedDes.
  133. static bool isReductionPHI(PHINode *Phi, Loop *TheLoop,
  134. RecurrenceDescriptor &RedDes);
  135. RecurrenceKind getRecurrenceKind() { return Kind; }
  136. MinMaxRecurrenceKind getMinMaxRecurrenceKind() { return MinMaxKind; }
  137. TrackingVH<Value> getRecurrenceStartValue() { return StartValue; }
  138. Instruction *getLoopExitInstr() { return LoopExitInstr; }
  139. private:
  140. // The starting value of the recurrence.
  141. // It does not have to be zero!
  142. TrackingVH<Value> StartValue;
  143. // The instruction who's value is used outside the loop.
  144. Instruction *LoopExitInstr;
  145. // The kind of the recurrence.
  146. RecurrenceKind Kind;
  147. // If this a min/max recurrence the kind of recurrence.
  148. MinMaxRecurrenceKind MinMaxKind;
  149. };
  150. BasicBlock *InsertPreheaderForLoop(Loop *L, Pass *P);
  151. /// \brief Simplify each loop in a loop nest recursively.
  152. ///
  153. /// This takes a potentially un-simplified loop L (and its children) and turns
  154. /// it into a simplified loop nest with preheaders and single backedges. It
  155. /// will optionally update \c AliasAnalysis and \c ScalarEvolution analyses if
  156. /// passed into it.
  157. bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, Pass *PP,
  158. AliasAnalysis *AA = nullptr, ScalarEvolution *SE = nullptr,
  159. AssumptionCache *AC = nullptr);
  160. /// \brief Put loop into LCSSA form.
  161. ///
  162. /// Looks at all instructions in the loop which have uses outside of the
  163. /// current loop. For each, an LCSSA PHI node is inserted and the uses outside
  164. /// the loop are rewritten to use this node.
  165. ///
  166. /// LoopInfo and DominatorTree are required and preserved.
  167. ///
  168. /// If ScalarEvolution is passed in, it will be preserved.
  169. ///
  170. /// Returns true if any modifications are made to the loop.
  171. bool formLCSSA(Loop &L, DominatorTree &DT, LoopInfo *LI,
  172. ScalarEvolution *SE = nullptr);
  173. /// \brief Put a loop nest into LCSSA form.
  174. ///
  175. /// This recursively forms LCSSA for a loop nest.
  176. ///
  177. /// LoopInfo and DominatorTree are required and preserved.
  178. ///
  179. /// If ScalarEvolution is passed in, it will be preserved.
  180. ///
  181. /// Returns true if any modifications are made to the loop.
  182. bool formLCSSARecursively(Loop &L, DominatorTree &DT, LoopInfo *LI,
  183. ScalarEvolution *SE = nullptr);
  184. /// \brief Walk the specified region of the CFG (defined by all blocks
  185. /// dominated by the specified block, and that are in the current loop) in
  186. /// reverse depth first order w.r.t the DominatorTree. This allows us to visit
  187. /// uses before definitions, allowing us to sink a loop body in one pass without
  188. /// iteration. Takes DomTreeNode, AliasAnalysis, LoopInfo, DominatorTree,
  189. /// DataLayout, TargetLibraryInfo, Loop, AliasSet information for all
  190. /// instructions of the loop and loop safety information as arguments.
  191. /// It returns changed status.
  192. bool sinkRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *,
  193. TargetLibraryInfo *, Loop *, AliasSetTracker *,
  194. LICMSafetyInfo *);
  195. /// \brief Walk the specified region of the CFG (defined by all blocks
  196. /// dominated by the specified block, and that are in the current loop) in depth
  197. /// first order w.r.t the DominatorTree. This allows us to visit definitions
  198. /// before uses, allowing us to hoist a loop body in one pass without iteration.
  199. /// Takes DomTreeNode, AliasAnalysis, LoopInfo, DominatorTree, DataLayout,
  200. /// TargetLibraryInfo, Loop, AliasSet information for all instructions of the
  201. /// loop and loop safety information as arguments. It returns changed status.
  202. bool hoistRegion(DomTreeNode *, AliasAnalysis *, LoopInfo *, DominatorTree *,
  203. TargetLibraryInfo *, Loop *, AliasSetTracker *,
  204. LICMSafetyInfo *);
  205. /// \brief Try to promote memory values to scalars by sinking stores out of
  206. /// the loop and moving loads to before the loop. We do this by looping over
  207. /// the stores in the loop, looking for stores to Must pointers which are
  208. /// loop invariant. It takes AliasSet, Loop exit blocks vector, loop exit blocks
  209. /// insertion point vector, PredIteratorCache, LoopInfo, DominatorTree, Loop,
  210. /// AliasSet information for all instructions of the loop and loop safety
  211. /// information as arguments. It returns changed status.
  212. bool promoteLoopAccessesToScalars(AliasSet &, SmallVectorImpl<BasicBlock*> &,
  213. SmallVectorImpl<Instruction*> &,
  214. PredIteratorCache &, LoopInfo *,
  215. DominatorTree *, Loop *, AliasSetTracker *,
  216. LICMSafetyInfo *);
  217. /// \brief Computes safety information for a loop
  218. /// checks loop body & header for the possiblity of may throw
  219. /// exception, it takes LICMSafetyInfo and loop as argument.
  220. /// Updates safety information in LICMSafetyInfo argument.
  221. void computeLICMSafetyInfo(LICMSafetyInfo *, Loop *);
  222. /// \brief Checks if the given PHINode in a loop header is an induction
  223. /// variable. Returns true if this is an induction PHI along with the step
  224. /// value.
  225. bool isInductionPHI(PHINode *, ScalarEvolution *, ConstantInt *&);
  226. }
  227. #endif