IVUsers.h 6.1 KB

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