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.

262 lines
10 KiB

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