InstructionSimplify.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //===-- InstructionSimplify.h - Fold instrs into simpler forms --*- 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 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 AssumptionCache;
  38. class DominatorTree;
  39. class Instruction;
  40. class DataLayout;
  41. class FastMathFlags;
  42. class TargetLibraryInfo;
  43. class Type;
  44. class Value;
  45. /// SimplifyAddInst - Given operands for an Add, see if we can
  46. /// fold the result. If not, this returns null.
  47. Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
  48. const DataLayout &DL,
  49. const TargetLibraryInfo *TLI = nullptr,
  50. const DominatorTree *DT = nullptr,
  51. AssumptionCache *AC = nullptr,
  52. const Instruction *CxtI = nullptr);
  53. /// SimplifySubInst - Given operands for a Sub, see if we can
  54. /// fold the result. If not, this returns null.
  55. Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
  56. const DataLayout &DL,
  57. const TargetLibraryInfo *TLI = nullptr,
  58. const DominatorTree *DT = nullptr,
  59. AssumptionCache *AC = nullptr,
  60. const Instruction *CxtI = nullptr);
  61. /// Given operands for an FAdd, see if we can fold the result. If not, this
  62. /// returns null.
  63. Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  64. const DataLayout &DL,
  65. const TargetLibraryInfo *TLI = nullptr,
  66. const DominatorTree *DT = nullptr,
  67. AssumptionCache *AC = nullptr,
  68. const Instruction *CxtI = nullptr);
  69. /// Given operands for an FSub, see if we can fold the result. If not, this
  70. /// returns null.
  71. Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  72. const DataLayout &DL,
  73. const TargetLibraryInfo *TLI = nullptr,
  74. const DominatorTree *DT = nullptr,
  75. AssumptionCache *AC = nullptr,
  76. const Instruction *CxtI = nullptr);
  77. /// Given operands for an FMul, see if we can fold the result. If not, this
  78. /// returns null.
  79. Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  80. const DataLayout &DL,
  81. const TargetLibraryInfo *TLI = nullptr,
  82. const DominatorTree *DT = nullptr,
  83. AssumptionCache *AC = nullptr,
  84. const Instruction *CxtI = nullptr);
  85. /// SimplifyMulInst - Given operands for a Mul, see if we can
  86. /// fold the result. If not, this returns null.
  87. Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout &DL,
  88. const TargetLibraryInfo *TLI = nullptr,
  89. const DominatorTree *DT = nullptr,
  90. AssumptionCache *AC = nullptr,
  91. const Instruction *CxtI = nullptr);
  92. /// SimplifySDivInst - Given operands for an SDiv, see if we can
  93. /// fold the result. If not, this returns null.
  94. Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
  95. const TargetLibraryInfo *TLI = nullptr,
  96. const DominatorTree *DT = nullptr,
  97. AssumptionCache *AC = nullptr,
  98. const Instruction *CxtI = nullptr);
  99. /// SimplifyUDivInst - Given operands for a UDiv, see if we can
  100. /// fold the result. If not, this returns null.
  101. Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
  102. const TargetLibraryInfo *TLI = nullptr,
  103. const DominatorTree *DT = nullptr,
  104. AssumptionCache *AC = nullptr,
  105. const Instruction *CxtI = nullptr);
  106. /// SimplifyFDivInst - Given operands for an FDiv, see if we can
  107. /// fold the result. If not, this returns null.
  108. Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  109. const DataLayout &DL,
  110. const TargetLibraryInfo *TLI = nullptr,
  111. const DominatorTree *DT = nullptr,
  112. AssumptionCache *AC = nullptr,
  113. const Instruction *CxtI = nullptr);
  114. /// SimplifySRemInst - Given operands for an SRem, see if we can
  115. /// fold the result. If not, this returns null.
  116. Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout &DL,
  117. const TargetLibraryInfo *TLI = nullptr,
  118. const DominatorTree *DT = nullptr,
  119. AssumptionCache *AC = nullptr,
  120. const Instruction *CxtI = nullptr);
  121. /// SimplifyURemInst - Given operands for a URem, see if we can
  122. /// fold the result. If not, this returns null.
  123. Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout &DL,
  124. const TargetLibraryInfo *TLI = nullptr,
  125. const DominatorTree *DT = nullptr,
  126. AssumptionCache *AC = nullptr,
  127. const Instruction *CxtI = nullptr);
  128. /// SimplifyFRemInst - Given operands for an FRem, see if we can
  129. /// fold the result. If not, this returns null.
  130. Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  131. const DataLayout &DL,
  132. const TargetLibraryInfo *TLI = nullptr,
  133. const DominatorTree *DT = nullptr,
  134. AssumptionCache *AC = nullptr,
  135. const Instruction *CxtI = nullptr);
  136. /// SimplifyShlInst - Given operands for a Shl, see if we can
  137. /// fold the result. If not, this returns null.
  138. Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
  139. const DataLayout &DL,
  140. const TargetLibraryInfo *TLI = nullptr,
  141. const DominatorTree *DT = nullptr,
  142. AssumptionCache *AC = nullptr,
  143. const Instruction *CxtI = nullptr);
  144. /// SimplifyLShrInst - Given operands for a LShr, see if we can
  145. /// fold the result. If not, this returns null.
  146. Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
  147. const DataLayout &DL,
  148. const TargetLibraryInfo *TLI = nullptr,
  149. const DominatorTree *DT = nullptr,
  150. AssumptionCache *AC = nullptr,
  151. const Instruction *CxtI = nullptr);
  152. /// SimplifyAShrInst - Given operands for a AShr, see if we can
  153. /// fold the result. If not, this returns null.
  154. Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
  155. const DataLayout &DL,
  156. const TargetLibraryInfo *TLI = nullptr,
  157. const DominatorTree *DT = nullptr,
  158. AssumptionCache *AC = nullptr,
  159. const Instruction *CxtI = nullptr);
  160. /// SimplifyAndInst - Given operands for an And, see if we can
  161. /// fold the result. If not, this returns null.
  162. Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout &DL,
  163. const TargetLibraryInfo *TLI = nullptr,
  164. const DominatorTree *DT = nullptr,
  165. AssumptionCache *AC = nullptr,
  166. const Instruction *CxtI = nullptr);
  167. /// SimplifyOrInst - Given operands for an Or, see if we can
  168. /// fold the result. If not, this returns null.
  169. Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout &DL,
  170. const TargetLibraryInfo *TLI = nullptr,
  171. const DominatorTree *DT = nullptr,
  172. AssumptionCache *AC = nullptr,
  173. const Instruction *CxtI = nullptr);
  174. /// SimplifyXorInst - Given operands for a Xor, see if we can
  175. /// fold the result. If not, this returns null.
  176. Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout &DL,
  177. const TargetLibraryInfo *TLI = nullptr,
  178. const DominatorTree *DT = nullptr,
  179. AssumptionCache *AC = nullptr,
  180. const Instruction *CxtI = nullptr);
  181. /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
  182. /// fold the result. If not, this returns null.
  183. Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  184. const DataLayout &DL,
  185. const TargetLibraryInfo *TLI = nullptr,
  186. const DominatorTree *DT = nullptr,
  187. AssumptionCache *AC = nullptr,
  188. Instruction *CxtI = nullptr);
  189. /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
  190. /// fold the result. If not, this returns null.
  191. Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  192. FastMathFlags FMF, const DataLayout &DL,
  193. const TargetLibraryInfo *TLI = nullptr,
  194. const DominatorTree *DT = nullptr,
  195. AssumptionCache *AC = nullptr,
  196. const Instruction *CxtI = nullptr);
  197. /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
  198. /// the result. If not, this returns null.
  199. Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
  200. const DataLayout &DL,
  201. const TargetLibraryInfo *TLI = nullptr,
  202. const DominatorTree *DT = nullptr,
  203. AssumptionCache *AC = nullptr,
  204. const Instruction *CxtI = nullptr);
  205. /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
  206. /// fold the result. If not, this returns null.
  207. Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout &DL,
  208. const TargetLibraryInfo *TLI = nullptr,
  209. const DominatorTree *DT = nullptr,
  210. AssumptionCache *AC = nullptr,
  211. const Instruction *CxtI = nullptr);
  212. /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
  213. /// can fold the result. If not, this returns null.
  214. Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
  215. ArrayRef<unsigned> Idxs, const DataLayout &DL,
  216. const TargetLibraryInfo *TLI = nullptr,
  217. const DominatorTree *DT = nullptr,
  218. AssumptionCache *AC = nullptr,
  219. const Instruction *CxtI = nullptr);
  220. /// \brief Given operands for an ExtractValueInst, see if we can fold the
  221. /// result. If not, this returns null.
  222. Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
  223. const DataLayout &DL,
  224. const TargetLibraryInfo *TLI = nullptr,
  225. const DominatorTree *DT = nullptr,
  226. AssumptionCache *AC = nullptr,
  227. const Instruction *CxtI = nullptr);
  228. /// \brief Given operands for an ExtractElementInst, see if we can fold the
  229. /// result. If not, this returns null.
  230. Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
  231. const DataLayout &DL,
  232. const TargetLibraryInfo *TLI = nullptr,
  233. const DominatorTree *DT = nullptr,
  234. AssumptionCache *AC = nullptr,
  235. const Instruction *CxtI = nullptr);
  236. /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
  237. /// the result. If not, this returns null.
  238. Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout &DL,
  239. const TargetLibraryInfo *TLI = nullptr,
  240. const DominatorTree *DT = nullptr,
  241. AssumptionCache *AC = nullptr,
  242. const Instruction *CxtI = nullptr);
  243. //=== Helper functions for higher up the class hierarchy.
  244. /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
  245. /// fold the result. If not, this returns null.
  246. Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  247. const DataLayout &DL,
  248. const TargetLibraryInfo *TLI = nullptr,
  249. const DominatorTree *DT = nullptr,
  250. AssumptionCache *AC = nullptr,
  251. const Instruction *CxtI = nullptr);
  252. /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
  253. /// fold the result. If not, this returns null.
  254. Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
  255. const DataLayout &DL,
  256. const TargetLibraryInfo *TLI = nullptr,
  257. const DominatorTree *DT = nullptr,
  258. AssumptionCache *AC = nullptr,
  259. const Instruction *CxtI = nullptr);
  260. /// SimplifyFPBinOp - Given operands for a BinaryOperator, see if we can
  261. /// fold the result. If not, this returns null.
  262. /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
  263. /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
  264. Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
  265. const FastMathFlags &FMF, const DataLayout &DL,
  266. const TargetLibraryInfo *TLI = nullptr,
  267. const DominatorTree *DT = nullptr,
  268. AssumptionCache *AC = nullptr,
  269. const Instruction *CxtI = nullptr);
  270. /// \brief Given a function and iterators over arguments, see if we can fold
  271. /// the result.
  272. ///
  273. /// If this call could not be simplified returns null.
  274. Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
  275. User::op_iterator ArgEnd, const DataLayout &DL,
  276. const TargetLibraryInfo *TLI = nullptr,
  277. const DominatorTree *DT = nullptr,
  278. AssumptionCache *AC = nullptr,
  279. const Instruction *CxtI = nullptr);
  280. /// \brief Given a function and set of arguments, see if we can fold the
  281. /// result.
  282. ///
  283. /// If this call could not be simplified returns null.
  284. Value *SimplifyCall(Value *V, ArrayRef<Value *> Args, const DataLayout &DL,
  285. const TargetLibraryInfo *TLI = nullptr,
  286. const DominatorTree *DT = nullptr,
  287. AssumptionCache *AC = nullptr,
  288. const Instruction *CxtI = nullptr);
  289. /// SimplifyInstruction - See if we can compute a simplified version of this
  290. /// instruction. If not, this returns null.
  291. Value *SimplifyInstruction(Instruction *I, const DataLayout &DL,
  292. const TargetLibraryInfo *TLI = nullptr,
  293. const DominatorTree *DT = nullptr,
  294. AssumptionCache *AC = nullptr);
  295. /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses
  296. /// recursively.
  297. ///
  298. /// This first performs a normal RAUW of I with SimpleV. It then recursively
  299. /// attempts to simplify those users updated by the operation. The 'I'
  300. /// instruction must not be equal to the simplified value 'SimpleV'.
  301. ///
  302. /// The function returns true if any simplifications were performed.
  303. bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
  304. const TargetLibraryInfo *TLI = nullptr,
  305. const DominatorTree *DT = nullptr,
  306. AssumptionCache *AC = nullptr);
  307. /// \brief Recursively attempt to simplify an instruction.
  308. ///
  309. /// This routine uses SimplifyInstruction to simplify 'I', and if successful
  310. /// replaces uses of 'I' with the simplified value. It then recurses on each
  311. /// of the users impacted. It returns true if any simplifications were
  312. /// performed.
  313. bool recursivelySimplifyInstruction(Instruction *I,
  314. const TargetLibraryInfo *TLI = nullptr,
  315. const DominatorTree *DT = nullptr,
  316. AssumptionCache *AC = nullptr);
  317. } // end namespace llvm
  318. #endif