ConstantFolder.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //===- 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_IR_CONSTANTFOLDER_H
  17. #define LLVM_IR_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(Type *Ty, Constant *C,
  111. ArrayRef<Constant *> IdxList) const {
  112. return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
  113. }
  114. Constant *CreateGetElementPtr(Type *Ty, 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(Ty, C, Idx);
  119. }
  120. Constant *CreateGetElementPtr(Type *Ty, Constant *C,
  121. ArrayRef<Value *> IdxList) const {
  122. return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
  123. }
  124. Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
  125. ArrayRef<Constant *> IdxList) const {
  126. return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
  127. }
  128. Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
  129. Constant *Idx) const {
  130. // This form of the function only exists to avoid ambiguous overload
  131. // warnings about whether to convert Idx to ArrayRef<Constant *> or
  132. // ArrayRef<Value *>.
  133. return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
  134. }
  135. Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
  136. ArrayRef<Value *> IdxList) const {
  137. return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
  138. }
  139. //===--------------------------------------------------------------------===//
  140. // Cast/Conversion Operators
  141. //===--------------------------------------------------------------------===//
  142. Constant *CreateCast(Instruction::CastOps Op, Constant *C,
  143. Type *DestTy) const {
  144. return ConstantExpr::getCast(Op, C, DestTy);
  145. }
  146. Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
  147. return ConstantExpr::getPointerCast(C, DestTy);
  148. }
  149. Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
  150. Type *DestTy) const {
  151. return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
  152. }
  153. Constant *CreateIntCast(Constant *C, Type *DestTy,
  154. bool isSigned) const {
  155. return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
  156. }
  157. Constant *CreateFPCast(Constant *C, Type *DestTy) const {
  158. return ConstantExpr::getFPCast(C, DestTy);
  159. }
  160. Constant *CreateBitCast(Constant *C, Type *DestTy) const {
  161. return CreateCast(Instruction::BitCast, C, DestTy);
  162. }
  163. Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
  164. return CreateCast(Instruction::IntToPtr, C, DestTy);
  165. }
  166. Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
  167. return CreateCast(Instruction::PtrToInt, C, DestTy);
  168. }
  169. Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
  170. return ConstantExpr::getZExtOrBitCast(C, DestTy);
  171. }
  172. Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
  173. return ConstantExpr::getSExtOrBitCast(C, DestTy);
  174. }
  175. Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
  176. return ConstantExpr::getTruncOrBitCast(C, DestTy);
  177. }
  178. //===--------------------------------------------------------------------===//
  179. // Compare Instructions
  180. //===--------------------------------------------------------------------===//
  181. Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
  182. Constant *RHS) const {
  183. return ConstantExpr::getCompare(P, LHS, RHS);
  184. }
  185. Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
  186. Constant *RHS) const {
  187. return ConstantExpr::getCompare(P, LHS, RHS);
  188. }
  189. //===--------------------------------------------------------------------===//
  190. // Other Instructions
  191. //===--------------------------------------------------------------------===//
  192. Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
  193. return ConstantExpr::getSelect(C, True, False);
  194. }
  195. Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
  196. return ConstantExpr::getExtractElement(Vec, Idx);
  197. }
  198. Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
  199. Constant *Idx) const {
  200. return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
  201. }
  202. Constant *CreateShuffleVector(Constant *V1, Constant *V2,
  203. Constant *Mask) const {
  204. return ConstantExpr::getShuffleVector(V1, V2, Mask);
  205. }
  206. Constant *CreateExtractValue(Constant *Agg,
  207. ArrayRef<unsigned> IdxList) const {
  208. return ConstantExpr::getExtractValue(Agg, IdxList);
  209. }
  210. Constant *CreateInsertValue(Constant *Agg, Constant *Val,
  211. ArrayRef<unsigned> IdxList) const {
  212. return ConstantExpr::getInsertValue(Agg, Val, IdxList);
  213. }
  214. };
  215. }
  216. #endif