IVUsers.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Analysis/IVUsers.h - Induction Variable Users -------*- 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. //
  14. // This file implements bookkeeping for "interesting" users of expressions
  15. // computed from induction variables.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_IVUSERS_H
  19. #define LLVM_ANALYSIS_IVUSERS_H
  20. #include "llvm/Analysis/LoopAnalysisManager.h"
  21. #include "llvm/Analysis/LoopPass.h"
  22. #include "llvm/Analysis/ScalarEvolutionNormalization.h"
  23. #include "llvm/IR/ValueHandle.h"
  24. namespace llvm {
  25. class AssumptionCache;
  26. class DominatorTree;
  27. class Instruction;
  28. class Value;
  29. class ScalarEvolution;
  30. class SCEV;
  31. class IVUsers;
  32. class DataLayout;
  33. /// IVStrideUse - Keep track of one use of a strided induction variable.
  34. /// The Expr member keeps track of the expression, User is the actual user
  35. /// instruction of the operand, and 'OperandValToReplace' is the operand of
  36. /// the User that is the use.
  37. class IVStrideUse final : public CallbackVH, public ilist_node<IVStrideUse> {
  38. friend class IVUsers;
  39. public:
  40. IVStrideUse(IVUsers *P, Instruction* U, Value *O)
  41. : CallbackVH(U), Parent(P), OperandValToReplace(O) {
  42. }
  43. /// getUser - Return the user instruction for this use.
  44. Instruction *getUser() const {
  45. return cast<Instruction>(getValPtr());
  46. }
  47. /// setUser - Assign a new user instruction for this use.
  48. void setUser(Instruction *NewUser) {
  49. setValPtr(NewUser);
  50. }
  51. /// getOperandValToReplace - Return the Value of the operand in the user
  52. /// instruction that this IVStrideUse is representing.
  53. Value *getOperandValToReplace() const {
  54. return OperandValToReplace;
  55. }
  56. /// setOperandValToReplace - Assign a new Value as the operand value
  57. /// to replace.
  58. void setOperandValToReplace(Value *Op) {
  59. OperandValToReplace = Op;
  60. }
  61. /// getPostIncLoops - Return the set of loops for which the expression has
  62. /// been adjusted to use post-inc mode.
  63. const PostIncLoopSet &getPostIncLoops() const {
  64. return PostIncLoops;
  65. }
  66. /// transformToPostInc - Transform the expression to post-inc form for the
  67. /// given loop.
  68. void transformToPostInc(const Loop *L);
  69. private:
  70. /// Parent - a pointer to the IVUsers that owns this IVStrideUse.
  71. IVUsers *Parent;
  72. /// OperandValToReplace - The Value of the operand in the user instruction
  73. /// that this IVStrideUse is representing.
  74. WeakTrackingVH OperandValToReplace;
  75. /// PostIncLoops - The set of loops for which Expr has been adjusted to
  76. /// use post-inc mode. This corresponds with SCEVExpander's post-inc concept.
  77. PostIncLoopSet PostIncLoops;
  78. /// Deleted - Implementation of CallbackVH virtual function to
  79. /// receive notification when the User is deleted.
  80. void deleted() override;
  81. };
  82. class IVUsers {
  83. friend class IVStrideUse;
  84. Loop *L;
  85. AssumptionCache *AC;
  86. LoopInfo *LI;
  87. DominatorTree *DT;
  88. ScalarEvolution *SE;
  89. SmallPtrSet<Instruction*, 16> Processed;
  90. /// IVUses - A list of all tracked IV uses of induction variable expressions
  91. /// we are interested in.
  92. ilist<IVStrideUse> IVUses;
  93. // Ephemeral values used by @llvm.assume in this function.
  94. SmallPtrSet<const Value *, 32> EphValues;
  95. public:
  96. IVUsers(Loop *L, AssumptionCache *AC, LoopInfo *LI, DominatorTree *DT,
  97. ScalarEvolution *SE);
  98. IVUsers(IVUsers &&X)
  99. : L(std::move(X.L)), AC(std::move(X.AC)), DT(std::move(X.DT)),
  100. SE(std::move(X.SE)), Processed(std::move(X.Processed)),
  101. IVUses(std::move(X.IVUses)), EphValues(std::move(X.EphValues)) {
  102. for (IVStrideUse &U : IVUses)
  103. U.Parent = this;
  104. }
  105. IVUsers(const IVUsers &) = delete;
  106. IVUsers &operator=(IVUsers &&) = delete;
  107. IVUsers &operator=(const IVUsers &) = delete;
  108. Loop *getLoop() const { return L; }
  109. /// AddUsersIfInteresting - Inspect the specified Instruction. If it is a
  110. /// reducible SCEV, recursively add its users to the IVUsesByStride set and
  111. /// return true. Otherwise, return false.
  112. bool AddUsersIfInteresting(Instruction *I);
  113. IVStrideUse &AddUser(Instruction *User, Value *Operand);
  114. /// getReplacementExpr - Return a SCEV expression which computes the
  115. /// value of the OperandValToReplace of the given IVStrideUse.
  116. const SCEV *getReplacementExpr(const IVStrideUse &IU) const;
  117. /// getExpr - Return the expression for the use.
  118. const SCEV *getExpr(const IVStrideUse &IU) const;
  119. const SCEV *getStride(const IVStrideUse &IU, const Loop *L) const;
  120. typedef ilist<IVStrideUse>::iterator iterator;
  121. typedef ilist<IVStrideUse>::const_iterator const_iterator;
  122. iterator begin() { return IVUses.begin(); }
  123. iterator end() { return IVUses.end(); }
  124. const_iterator begin() const { return IVUses.begin(); }
  125. const_iterator end() const { return IVUses.end(); }
  126. bool empty() const { return IVUses.empty(); }
  127. bool isIVUserOrOperand(Instruction *Inst) const {
  128. return Processed.count(Inst);
  129. }
  130. void releaseMemory();
  131. void print(raw_ostream &OS, const Module * = nullptr) const;
  132. /// dump - This method is used for debugging.
  133. void dump() const;
  134. protected:
  135. bool AddUsersImpl(Instruction *I, SmallPtrSetImpl<Loop*> &SimpleLoopNests);
  136. };
  137. Pass *createIVUsersPass();
  138. class IVUsersWrapperPass : public LoopPass {
  139. std::unique_ptr<IVUsers> IU;
  140. public:
  141. static char ID;
  142. IVUsersWrapperPass();
  143. IVUsers &getIU() { return *IU; }
  144. const IVUsers &getIU() const { return *IU; }
  145. void getAnalysisUsage(AnalysisUsage &AU) const override;
  146. bool runOnLoop(Loop *L, LPPassManager &LPM) override;
  147. void releaseMemory() override;
  148. void print(raw_ostream &OS, const Module * = nullptr) const override;
  149. };
  150. /// Analysis pass that exposes the \c IVUsers for a loop.
  151. class IVUsersAnalysis : public AnalysisInfoMixin<IVUsersAnalysis> {
  152. friend AnalysisInfoMixin<IVUsersAnalysis>;
  153. static AnalysisKey Key;
  154. public:
  155. typedef IVUsers Result;
  156. IVUsers run(Loop &L, LoopAnalysisManager &AM,
  157. LoopStandardAnalysisResults &AR);
  158. };
  159. }
  160. #endif
  161. #ifdef __GNUC__
  162. #pragma GCC diagnostic pop
  163. #endif