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.

274 lines
13 KiB

  1. //===-- InstructionSimplify.h - Fold instructions into simpler forms ------===//
  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 declares routines for folding instructions into simpler forms
  11. // that do not require creating new instructions. This does constant folding
  12. // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
  13. // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
  14. // ("and i32 %x, %x" -> "%x"). If the simplification is also an instruction
  15. // then it dominates the original instruction.
  16. //
  17. // These routines implicitly resolve undef uses. The easiest way to be safe when
  18. // using these routines to obtain simplified values for existing instructions is
  19. // to always replace all uses of the instructions with the resulting simplified
  20. // values. This will prevent other code from seeing the same undef uses and
  21. // resolving them to different values.
  22. //
  23. // These routines are designed to tolerate moderately incomplete IR, such as
  24. // instructions that are not connected to basic blocks yet. However, they do
  25. // require that all the IR that they encounter be valid. In particular, they
  26. // require that all non-constant values be defined in the same function, and the
  27. // same call context of that function (and not split between caller and callee
  28. // contexts of a directly recursive call, for example).
  29. //
  30. //===----------------------------------------------------------------------===//
  31. #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
  32. #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
  33. #include "llvm/IR/User.h"
  34. namespace llvm {
  35. template<typename T>
  36. class ArrayRef;
  37. class DominatorTree;
  38. class Instruction;
  39. class DataLayout;
  40. class FastMathFlags;
  41. class TargetLibraryInfo;
  42. class Type;
  43. class Value;
  44. /// SimplifyAddInst - Given operands for an Add, see if we can
  45. /// fold the result. If not, this returns null.
  46. Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
  47. const DataLayout *TD = 0,
  48. const TargetLibraryInfo *TLI = 0,
  49. const DominatorTree *DT = 0);
  50. /// SimplifySubInst - Given operands for a Sub, see if we can
  51. /// fold the result. If not, this returns null.
  52. Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
  53. const DataLayout *TD = 0,
  54. const TargetLibraryInfo *TLI = 0,
  55. const DominatorTree *DT = 0);
  56. /// Given operands for an FAdd, see if we can fold the result. If not, this
  57. /// returns null.
  58. Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  59. const DataLayout *TD = 0,
  60. const TargetLibraryInfo *TLI = 0,
  61. const DominatorTree *DT = 0);
  62. /// Given operands for an FSub, see if we can fold the result. If not, this
  63. /// returns null.
  64. Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  65. const DataLayout *TD = 0,
  66. const TargetLibraryInfo *TLI = 0,
  67. const DominatorTree *DT = 0);
  68. /// Given operands for an FMul, see if we can fold the result. If not, this
  69. /// returns null.
  70. Value *SimplifyFMulInst(Value *LHS, Value *RHS,
  71. FastMathFlags FMF,
  72. const DataLayout *TD = 0,
  73. const TargetLibraryInfo *TLI = 0,
  74. const DominatorTree *DT = 0);
  75. /// SimplifyMulInst - Given operands for a Mul, see if we can
  76. /// fold the result. If not, this returns null.
  77. Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  78. const TargetLibraryInfo *TLI = 0,
  79. const DominatorTree *DT = 0);
  80. /// SimplifySDivInst - Given operands for an SDiv, see if we can
  81. /// fold the result. If not, this returns null.
  82. Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  83. const TargetLibraryInfo *TLI = 0,
  84. const DominatorTree *DT = 0);
  85. /// SimplifyUDivInst - Given operands for a UDiv, see if we can
  86. /// fold the result. If not, this returns null.
  87. Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  88. const TargetLibraryInfo *TLI = 0,
  89. const DominatorTree *DT = 0);
  90. /// SimplifyFDivInst - Given operands for an FDiv, see if we can
  91. /// fold the result. If not, this returns null.
  92. Value *SimplifyFDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  93. const TargetLibraryInfo *TLI = 0,
  94. const DominatorTree *DT = 0);
  95. /// SimplifySRemInst - Given operands for an SRem, see if we can
  96. /// fold the result. If not, this returns null.
  97. Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  98. const TargetLibraryInfo *TLI = 0,
  99. const DominatorTree *DT = 0);
  100. /// SimplifyURemInst - Given operands for a URem, see if we can
  101. /// fold the result. If not, this returns null.
  102. Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  103. const TargetLibraryInfo *TLI = 0,
  104. const DominatorTree *DT = 0);
  105. /// SimplifyFRemInst - Given operands for an FRem, see if we can
  106. /// fold the result. If not, this returns null.
  107. Value *SimplifyFRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  108. const TargetLibraryInfo *TLI = 0,
  109. const DominatorTree *DT = 0);
  110. /// SimplifyShlInst - Given operands for a Shl, see if we can
  111. /// fold the result. If not, this returns null.
  112. Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
  113. const DataLayout *TD = 0,
  114. const TargetLibraryInfo *TLI = 0,
  115. const DominatorTree *DT = 0);
  116. /// SimplifyLShrInst - Given operands for a LShr, see if we can
  117. /// fold the result. If not, this returns null.
  118. Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
  119. const DataLayout *TD = 0,
  120. const TargetLibraryInfo *TLI = 0,
  121. const DominatorTree *DT = 0);
  122. /// SimplifyAShrInst - Given operands for a AShr, see if we can
  123. /// fold the result. If not, this returns null.
  124. Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
  125. const DataLayout *TD = 0,
  126. const TargetLibraryInfo *TLI = 0,
  127. const DominatorTree *DT = 0);
  128. /// SimplifyAndInst - Given operands for an And, see if we can
  129. /// fold the result. If not, this returns null.
  130. Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  131. const TargetLibraryInfo *TLI = 0,
  132. const DominatorTree *DT = 0);
  133. /// SimplifyOrInst - Given operands for an Or, see if we can
  134. /// fold the result. If not, this returns null.
  135. Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  136. const TargetLibraryInfo *TLI = 0,
  137. const DominatorTree *DT = 0);
  138. /// SimplifyXorInst - Given operands for a Xor, see if we can
  139. /// fold the result. If not, this returns null.
  140. Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD = 0,
  141. const TargetLibraryInfo *TLI = 0,
  142. const DominatorTree *DT = 0);
  143. /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
  144. /// fold the result. If not, this returns null.
  145. Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  146. const DataLayout *TD = 0,
  147. const TargetLibraryInfo *TLI = 0,
  148. const DominatorTree *DT = 0);
  149. /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
  150. /// fold the result. If not, this returns null.
  151. Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  152. const DataLayout *TD = 0,
  153. const TargetLibraryInfo *TLI = 0,
  154. const DominatorTree *DT = 0);
  155. /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
  156. /// the result. If not, this returns null.
  157. Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
  158. const DataLayout *TD = 0,
  159. const TargetLibraryInfo *TLI = 0,
  160. const DominatorTree *DT = 0);
  161. /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
  162. /// fold the result. If not, this returns null.
  163. Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD = 0,
  164. const TargetLibraryInfo *TLI = 0,
  165. const DominatorTree *DT = 0);
  166. /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
  167. /// can fold the result. If not, this returns null.
  168. Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
  169. ArrayRef<unsigned> Idxs,
  170. const DataLayout *TD = 0,
  171. const TargetLibraryInfo *TLI = 0,
  172. const DominatorTree *DT = 0);
  173. /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
  174. /// the result. If not, this returns null.
  175. Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD = 0,
  176. const TargetLibraryInfo *TLI = 0,
  177. const DominatorTree *DT = 0);
  178. //=== Helper functions for higher up the class hierarchy.
  179. /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
  180. /// fold the result. If not, this returns null.
  181. Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  182. const DataLayout *TD = 0,
  183. const TargetLibraryInfo *TLI = 0,
  184. const DominatorTree *DT = 0);
  185. /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
  186. /// fold the result. If not, this returns null.
  187. Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
  188. const DataLayout *TD = 0,
  189. const TargetLibraryInfo *TLI = 0,
  190. const DominatorTree *DT = 0);
  191. /// \brief Given a function and iterators over arguments, see if we can fold
  192. /// the result.
  193. ///
  194. /// If this call could not be simplified returns null.
  195. Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
  196. User::op_iterator ArgEnd, const DataLayout *TD = 0,
  197. const TargetLibraryInfo *TLI = 0,
  198. const DominatorTree *DT = 0);
  199. /// \brief Given a function and set of arguments, see if we can fold the
  200. /// result.
  201. ///
  202. /// If this call could not be simplified returns null.
  203. Value *SimplifyCall(Value *V, ArrayRef<Value *> Args,
  204. const DataLayout *TD = 0,
  205. const TargetLibraryInfo *TLI = 0,
  206. const DominatorTree *DT = 0);
  207. /// SimplifyInstruction - See if we can compute a simplified version of this
  208. /// instruction. If not, this returns null.
  209. Value *SimplifyInstruction(Instruction *I, const DataLayout *TD = 0,
  210. const TargetLibraryInfo *TLI = 0,
  211. const DominatorTree *DT = 0);
  212. /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses
  213. /// recursively.
  214. ///
  215. /// This first performs a normal RAUW of I with SimpleV. It then recursively
  216. /// attempts to simplify those users updated by the operation. The 'I'
  217. /// instruction must not be equal to the simplified value 'SimpleV'.
  218. ///
  219. /// The function returns true if any simplifications were performed.
  220. bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
  221. const DataLayout *TD = 0,
  222. const TargetLibraryInfo *TLI = 0,
  223. const DominatorTree *DT = 0);
  224. /// \brief Recursively attempt to simplify an instruction.
  225. ///
  226. /// This routine uses SimplifyInstruction to simplify 'I', and if successful
  227. /// replaces uses of 'I' with the simplified value. It then recurses on each
  228. /// of the users impacted. It returns true if any simplifications were
  229. /// performed.
  230. bool recursivelySimplifyInstruction(Instruction *I,
  231. const DataLayout *TD = 0,
  232. const TargetLibraryInfo *TLI = 0,
  233. const DominatorTree *DT = 0);
  234. } // end namespace llvm
  235. #endif