ConstantFolder.h 10 KB

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