ConstantFolder.h 10 KB

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