IVUsers.h 6.1 KB

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