Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
10 KiB

  1. //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- 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 the classes used to generate code from scalar expressions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
  14. #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
  15. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  16. #include "llvm/Analysis/ScalarEvolutionNormalization.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/Support/TargetFolder.h"
  19. #include "llvm/Support/ValueHandle.h"
  20. #include <set>
  21. namespace llvm {
  22. class TargetTransformInfo;
  23. /// Return true if the given expression is safe to expand in the sense that
  24. /// all materialized values are safe to speculate.
  25. bool isSafeToExpand(const SCEV *S);
  26. /// SCEVExpander - This class uses information about analyze scalars to
  27. /// rewrite expressions in canonical form.
  28. ///
  29. /// Clients should create an instance of this class when rewriting is needed,
  30. /// and destroy it when finished to allow the release of the associated
  31. /// memory.
  32. class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
  33. ScalarEvolution &SE;
  34. // New instructions receive a name to identifies them with the current pass.
  35. const char* IVName;
  36. // InsertedExpressions caches Values for reuse, so must track RAUW.
  37. std::map<std::pair<const SCEV *, Instruction *>, TrackingVH<Value> >
  38. InsertedExpressions;
  39. // InsertedValues only flags inserted instructions so needs no RAUW.
  40. std::set<AssertingVH<Value> > InsertedValues;
  41. std::set<AssertingVH<Value> > InsertedPostIncValues;
  42. /// RelevantLoops - A memoization of the "relevant" loop for a given SCEV.
  43. DenseMap<const SCEV *, const Loop *> RelevantLoops;
  44. /// PostIncLoops - Addrecs referring to any of the given loops are expanded
  45. /// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode
  46. /// returns the add instruction that adds one to the phi for {0,+,1}<L>,
  47. /// as opposed to a new phi starting at 1. This is only supported in
  48. /// non-canonical mode.
  49. PostIncLoopSet PostIncLoops;
  50. /// IVIncInsertPos - When this is non-null, addrecs expanded in the
  51. /// loop it indicates should be inserted with increments at
  52. /// IVIncInsertPos.
  53. const Loop *IVIncInsertLoop;
  54. /// IVIncInsertPos - When expanding addrecs in the IVIncInsertLoop loop,
  55. /// insert the IV increment at this position.
  56. Instruction *IVIncInsertPos;
  57. /// Phis that complete an IV chain. Reuse
  58. std::set<AssertingVH<PHINode> > ChainedPhis;
  59. /// CanonicalMode - When true, expressions are expanded in "canonical"
  60. /// form. In particular, addrecs are expanded as arithmetic based on
  61. /// a canonical induction variable. When false, expression are expanded
  62. /// in a more literal form.
  63. bool CanonicalMode;
  64. /// When invoked from LSR, the expander is in "strength reduction" mode. The
  65. /// only difference is that phi's are only reused if they are already in
  66. /// "expanded" form.
  67. bool LSRMode;
  68. typedef IRBuilder<true, TargetFolder> BuilderType;
  69. BuilderType Builder;
  70. #ifndef NDEBUG
  71. const char *DebugType;
  72. #endif
  73. friend struct SCEVVisitor<SCEVExpander, Value*>;
  74. public:
  75. /// SCEVExpander - Construct a SCEVExpander in "canonical" mode.
  76. explicit SCEVExpander(ScalarEvolution &se, const char *name)
  77. : SE(se), IVName(name), IVIncInsertLoop(0), IVIncInsertPos(0),
  78. CanonicalMode(true), LSRMode(false),
  79. Builder(se.getContext(), TargetFolder(se.TD)) {
  80. #ifndef NDEBUG
  81. DebugType = "";
  82. #endif
  83. }
  84. #ifndef NDEBUG
  85. void setDebugType(const char* s) { DebugType = s; }
  86. #endif
  87. /// clear - Erase the contents of the InsertedExpressions map so that users
  88. /// trying to expand the same expression into multiple BasicBlocks or
  89. /// different places within the same BasicBlock can do so.
  90. void clear() {
  91. InsertedExpressions.clear();
  92. InsertedValues.clear();
  93. InsertedPostIncValues.clear();
  94. ChainedPhis.clear();
  95. }
  96. /// getOrInsertCanonicalInductionVariable - This method returns the
  97. /// canonical induction variable of the specified type for the specified
  98. /// loop (inserting one if there is none). A canonical induction variable
  99. /// starts at zero and steps by one on each iteration.
  100. PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty);
  101. /// getIVIncOperand - Return the induction variable increment's IV operand.
  102. Instruction *getIVIncOperand(Instruction *IncV, Instruction *InsertPos,
  103. bool allowScale);
  104. /// hoistIVInc - Utility for hoisting an IV increment.
  105. bool hoistIVInc(Instruction *IncV, Instruction *InsertPos);
  106. /// replaceCongruentIVs - replace congruent phis with their most canonical
  107. /// representative. Return the number of phis eliminated.
  108. unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
  109. SmallVectorImpl<WeakVH> &DeadInsts,
  110. const TargetTransformInfo *TTI = NULL);
  111. /// expandCodeFor - Insert code to directly compute the specified SCEV
  112. /// expression into the program. The inserted code is inserted into the
  113. /// specified block.
  114. Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I);
  115. /// setIVIncInsertPos - Set the current IV increment loop and position.
  116. void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
  117. assert(!CanonicalMode &&
  118. "IV increment positions are not supported in CanonicalMode");
  119. IVIncInsertLoop = L;
  120. IVIncInsertPos = Pos;
  121. }
  122. /// setPostInc - Enable post-inc expansion for addrecs referring to the
  123. /// given loops. Post-inc expansion is only supported in non-canonical
  124. /// mode.
  125. void setPostInc(const PostIncLoopSet &L) {
  126. assert(!CanonicalMode &&
  127. "Post-inc expansion is not supported in CanonicalMode");
  128. PostIncLoops = L;
  129. }
  130. /// clearPostInc - Disable all post-inc expansion.
  131. void clearPostInc() {
  132. PostIncLoops.clear();
  133. // When we change the post-inc loop set, cached expansions may no
  134. // longer be valid.
  135. InsertedPostIncValues.clear();
  136. }
  137. /// disableCanonicalMode - Disable the behavior of expanding expressions in
  138. /// canonical form rather than in a more literal form. Non-canonical mode
  139. /// is useful for late optimization passes.
  140. void disableCanonicalMode() { CanonicalMode = false; }
  141. void enableLSRMode() { LSRMode = true; }
  142. /// clearInsertPoint - Clear the current insertion point. This is useful
  143. /// if the instruction that had been serving as the insertion point may
  144. /// have been deleted.
  145. void clearInsertPoint() {
  146. Builder.ClearInsertionPoint();
  147. }
  148. /// isInsertedInstruction - Return true if the specified instruction was
  149. /// inserted by the code rewriter. If so, the client should not modify the
  150. /// instruction.
  151. bool isInsertedInstruction(Instruction *I) const {
  152. return InsertedValues.count(I) || InsertedPostIncValues.count(I);
  153. }
  154. void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); }
  155. private:
  156. LLVMContext &getContext() const { return SE.getContext(); }
  157. /// InsertBinop - Insert the specified binary operator, doing a small amount
  158. /// of work to avoid inserting an obviously redundant operation.
  159. Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
  160. /// ReuseOrCreateCast - Arange for there to be a cast of V to Ty at IP,
  161. /// reusing an existing cast if a suitable one exists, moving an existing
  162. /// cast if a suitable one exists but isn't in the right place, or
  163. /// or creating a new one.
  164. Value *ReuseOrCreateCast(Value *V, Type *Ty,
  165. Instruction::CastOps Op,
  166. BasicBlock::iterator IP);
  167. /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
  168. /// which must be possible with a noop cast, doing what we can to
  169. /// share the casts.
  170. Value *InsertNoopCastOfTo(Value *V, Type *Ty);
  171. /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
  172. /// instead of using ptrtoint+arithmetic+inttoptr.
  173. Value *expandAddToGEP(const SCEV *const *op_begin,
  174. const SCEV *const *op_end,
  175. PointerType *PTy, Type *Ty, Value *V);
  176. Value *expand(const SCEV *S);
  177. /// expandCodeFor - Insert code to directly compute the specified SCEV
  178. /// expression into the program. The inserted code is inserted into the
  179. /// SCEVExpander's current insertion point. If a type is specified, the
  180. /// result will be expanded to have that type, with a cast if necessary.
  181. Value *expandCodeFor(const SCEV *SH, Type *Ty = 0);
  182. /// getRelevantLoop - Determine the most "relevant" loop for the given SCEV.
  183. const Loop *getRelevantLoop(const SCEV *);
  184. Value *visitConstant(const SCEVConstant *S) {
  185. return S->getValue();
  186. }
  187. Value *visitTruncateExpr(const SCEVTruncateExpr *S);
  188. Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
  189. Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
  190. Value *visitAddExpr(const SCEVAddExpr *S);
  191. Value *visitMulExpr(const SCEVMulExpr *S);
  192. Value *visitUDivExpr(const SCEVUDivExpr *S);
  193. Value *visitAddRecExpr(const SCEVAddRecExpr *S);
  194. Value *visitSMaxExpr(const SCEVSMaxExpr *S);
  195. Value *visitUMaxExpr(const SCEVUMaxExpr *S);
  196. Value *visitUnknown(const SCEVUnknown *S) {
  197. return S->getValue();
  198. }
  199. void rememberInstruction(Value *I);
  200. void restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I);
  201. bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
  202. bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
  203. Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
  204. PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
  205. const Loop *L,
  206. Type *ExpandTy,
  207. Type *IntTy);
  208. Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
  209. Type *ExpandTy, Type *IntTy, bool useSubtract);
  210. };
  211. }
  212. #endif