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.

238 lines
8.9 KiB

  1. //===-- llvm/Support/ConstantFolder.h - Constant folding helper -*- 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 ConstantFolder class, a helper for IRBuilder.
  11. // It provides IRBuilder with a set of methods for creating constants
  12. // with minimal folding. For general constant creation and folding,
  13. // use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_SUPPORT_CONSTANTFOLDER_H
  17. #define LLVM_SUPPORT_CONSTANTFOLDER_H
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/InstrTypes.h"
  20. namespace llvm {
  21. /// ConstantFolder - Create constants with minimum, target independent, folding.
  22. class ConstantFolder {
  23. public:
  24. explicit ConstantFolder() {}
  25. //===--------------------------------------------------------------------===//
  26. // Binary Operators
  27. //===--------------------------------------------------------------------===//
  28. Constant *CreateAdd(Constant *LHS, Constant *RHS,
  29. bool HasNUW = false, bool HasNSW = false) const {
  30. return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
  31. }
  32. Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
  33. return ConstantExpr::getFAdd(LHS, RHS);
  34. }
  35. Constant *CreateSub(Constant *LHS, Constant *RHS,
  36. bool HasNUW = false, bool HasNSW = false) const {
  37. return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
  38. }
  39. Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
  40. return ConstantExpr::getFSub(LHS, RHS);
  41. }
  42. Constant *CreateMul(Constant *LHS, Constant *RHS,
  43. bool HasNUW = false, bool HasNSW = false) const {
  44. return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
  45. }
  46. Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
  47. return ConstantExpr::getFMul(LHS, RHS);
  48. }
  49. Constant *CreateUDiv(Constant *LHS, Constant *RHS,
  50. bool isExact = false) const {
  51. return ConstantExpr::getUDiv(LHS, RHS, isExact);
  52. }
  53. Constant *CreateSDiv(Constant *LHS, Constant *RHS,
  54. bool isExact = false) const {
  55. return ConstantExpr::getSDiv(LHS, RHS, isExact);
  56. }
  57. Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
  58. return ConstantExpr::getFDiv(LHS, RHS);
  59. }
  60. Constant *CreateURem(Constant *LHS, Constant *RHS) const {
  61. return ConstantExpr::getURem(LHS, RHS);
  62. }
  63. Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
  64. return ConstantExpr::getSRem(LHS, RHS);
  65. }
  66. Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
  67. return ConstantExpr::getFRem(LHS, RHS);
  68. }
  69. Constant *CreateShl(Constant *LHS, Constant *RHS,
  70. bool HasNUW = false, bool HasNSW = false) const {
  71. return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
  72. }
  73. Constant *CreateLShr(Constant *LHS, Constant *RHS,
  74. bool isExact = false) const {
  75. return ConstantExpr::getLShr(LHS, RHS, isExact);
  76. }
  77. Constant *CreateAShr(Constant *LHS, Constant *RHS,
  78. bool isExact = false) const {
  79. return ConstantExpr::getAShr(LHS, RHS, isExact);
  80. }
  81. Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
  82. return ConstantExpr::getAnd(LHS, RHS);
  83. }
  84. Constant *CreateOr(Constant *LHS, Constant *RHS) const {
  85. return ConstantExpr::getOr(LHS, RHS);
  86. }
  87. Constant *CreateXor(Constant *LHS, Constant *RHS) const {
  88. return ConstantExpr::getXor(LHS, RHS);
  89. }
  90. Constant *CreateBinOp(Instruction::BinaryOps Opc,
  91. Constant *LHS, Constant *RHS) const {
  92. return ConstantExpr::get(Opc, LHS, RHS);
  93. }
  94. //===--------------------------------------------------------------------===//
  95. // Unary Operators
  96. //===--------------------------------------------------------------------===//
  97. Constant *CreateNeg(Constant *C,
  98. bool HasNUW = false, bool HasNSW = false) const {
  99. return ConstantExpr::getNeg(C, HasNUW, HasNSW);
  100. }
  101. Constant *CreateFNeg(Constant *C) const {
  102. return ConstantExpr::getFNeg(C);
  103. }
  104. Constant *CreateNot(Constant *C) const {
  105. return ConstantExpr::getNot(C);
  106. }
  107. //===--------------------------------------------------------------------===//
  108. // Memory Instructions
  109. //===--------------------------------------------------------------------===//
  110. Constant *CreateGetElementPtr(Constant *C,
  111. ArrayRef<Constant *> IdxList) const {
  112. return ConstantExpr::getGetElementPtr(C, IdxList);
  113. }
  114. Constant *CreateGetElementPtr(Constant *C, Constant *Idx) const {
  115. // This form of the function only exists to avoid ambiguous overload
  116. // warnings about whether to convert Idx to ArrayRef<Constant *> or
  117. // ArrayRef<Value *>.
  118. return ConstantExpr::getGetElementPtr(C, Idx);
  119. }
  120. Constant *CreateGetElementPtr(Constant *C,
  121. ArrayRef<Value *> IdxList) const {
  122. return ConstantExpr::getGetElementPtr(C, IdxList);
  123. }
  124. Constant *CreateInBoundsGetElementPtr(Constant *C,
  125. ArrayRef<Constant *> IdxList) const {
  126. return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
  127. }
  128. Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const {
  129. // This form of the function only exists to avoid ambiguous overload
  130. // warnings about whether to convert Idx to ArrayRef<Constant *> or
  131. // ArrayRef<Value *>.
  132. return ConstantExpr::getInBoundsGetElementPtr(C, Idx);
  133. }
  134. Constant *CreateInBoundsGetElementPtr(Constant *C,
  135. ArrayRef<Value *> IdxList) const {
  136. return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
  137. }
  138. //===--------------------------------------------------------------------===//
  139. // Cast/Conversion Operators
  140. //===--------------------------------------------------------------------===//
  141. Constant *CreateCast(Instruction::CastOps Op, Constant *C,
  142. Type *DestTy) const {
  143. return ConstantExpr::getCast(Op, C, DestTy);
  144. }
  145. Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
  146. return ConstantExpr::getPointerCast(C, DestTy);
  147. }
  148. Constant *CreateIntCast(Constant *C, Type *DestTy,
  149. bool isSigned) const {
  150. return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
  151. }
  152. Constant *CreateFPCast(Constant *C, Type *DestTy) const {
  153. return ConstantExpr::getFPCast(C, DestTy);
  154. }
  155. Constant *CreateBitCast(Constant *C, Type *DestTy) const {
  156. return CreateCast(Instruction::BitCast, C, DestTy);
  157. }
  158. Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
  159. return CreateCast(Instruction::IntToPtr, C, DestTy);
  160. }
  161. Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
  162. return CreateCast(Instruction::PtrToInt, C, DestTy);
  163. }
  164. Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
  165. return ConstantExpr::getZExtOrBitCast(C, DestTy);
  166. }
  167. Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
  168. return ConstantExpr::getSExtOrBitCast(C, DestTy);
  169. }
  170. Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
  171. return ConstantExpr::getTruncOrBitCast(C, DestTy);
  172. }
  173. //===--------------------------------------------------------------------===//
  174. // Compare Instructions
  175. //===--------------------------------------------------------------------===//
  176. Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
  177. Constant *RHS) const {
  178. return ConstantExpr::getCompare(P, LHS, RHS);
  179. }
  180. Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
  181. Constant *RHS) const {
  182. return ConstantExpr::getCompare(P, LHS, RHS);
  183. }
  184. //===--------------------------------------------------------------------===//
  185. // Other Instructions
  186. //===--------------------------------------------------------------------===//
  187. Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
  188. return ConstantExpr::getSelect(C, True, False);
  189. }
  190. Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
  191. return ConstantExpr::getExtractElement(Vec, Idx);
  192. }
  193. Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
  194. Constant *Idx) const {
  195. return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
  196. }
  197. Constant *CreateShuffleVector(Constant *V1, Constant *V2,
  198. Constant *Mask) const {
  199. return ConstantExpr::getShuffleVector(V1, V2, Mask);
  200. }
  201. Constant *CreateExtractValue(Constant *Agg,
  202. ArrayRef<unsigned> IdxList) const {
  203. return ConstantExpr::getExtractValue(Agg, IdxList);
  204. }
  205. Constant *CreateInsertValue(Constant *Agg, Constant *Val,
  206. ArrayRef<unsigned> IdxList) const {
  207. return ConstantExpr::getInsertValue(Agg, Val, IdxList);
  208. }
  209. };
  210. }
  211. #endif