ConstantFolder.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/ADT/STLExtras.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/IRBuilderFolder.h"
  26. #include "llvm/IR/InstrTypes.h"
  27. #include "llvm/IR/Instruction.h"
  28. namespace llvm {
  29. /// ConstantFolder - Create constants with minimum, target independent, folding.
  30. class ConstantFolder final : public IRBuilderFolder {
  31. virtual void anchor();
  32. public:
  33. explicit ConstantFolder() = default;
  34. //===--------------------------------------------------------------------===//
  35. // Value-based folders.
  36. //
  37. // Return an existing value or a constant if the operation can be simplified.
  38. // Otherwise return nullptr.
  39. //===--------------------------------------------------------------------===//
  40. Value *FoldAdd(Value *LHS, Value *RHS, bool HasNUW = false,
  41. bool HasNSW = false) const override {
  42. auto *LC = dyn_cast<Constant>(LHS);
  43. auto *RC = dyn_cast<Constant>(RHS);
  44. if (LC && RC)
  45. return ConstantExpr::getAdd(LC, RC, HasNUW, HasNSW);
  46. return nullptr;
  47. }
  48. Value *FoldAnd(Value *LHS, Value *RHS) const override {
  49. auto *LC = dyn_cast<Constant>(LHS);
  50. auto *RC = dyn_cast<Constant>(RHS);
  51. if (LC && RC)
  52. return ConstantExpr::getAnd(LC, RC);
  53. return nullptr;
  54. }
  55. Value *FoldOr(Value *LHS, Value *RHS) const override {
  56. auto *LC = dyn_cast<Constant>(LHS);
  57. auto *RC = dyn_cast<Constant>(RHS);
  58. if (LC && RC)
  59. return ConstantExpr::getOr(LC, RC);
  60. return nullptr;
  61. }
  62. Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
  63. auto *LC = dyn_cast<Constant>(LHS);
  64. auto *RC = dyn_cast<Constant>(RHS);
  65. if (LC && RC)
  66. return ConstantExpr::getCompare(P, LC, RC);
  67. return nullptr;
  68. }
  69. Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  70. bool IsInBounds = false) const override {
  71. if (auto *PC = dyn_cast<Constant>(Ptr)) {
  72. // Every index must be constant.
  73. if (any_of(IdxList, [](Value *V) { return !isa<Constant>(V); }))
  74. return nullptr;
  75. if (IsInBounds)
  76. return ConstantExpr::getInBoundsGetElementPtr(Ty, PC, IdxList);
  77. else
  78. return ConstantExpr::getGetElementPtr(Ty, PC, IdxList);
  79. }
  80. return nullptr;
  81. }
  82. Value *FoldSelect(Value *C, Value *True, Value *False) const override {
  83. auto *CC = dyn_cast<Constant>(C);
  84. auto *TC = dyn_cast<Constant>(True);
  85. auto *FC = dyn_cast<Constant>(False);
  86. if (CC && TC && FC)
  87. return ConstantExpr::getSelect(CC, TC, FC);
  88. return nullptr;
  89. }
  90. //===--------------------------------------------------------------------===//
  91. // Binary Operators
  92. //===--------------------------------------------------------------------===//
  93. Constant *CreateFAdd(Constant *LHS, Constant *RHS) const override {
  94. return ConstantExpr::getFAdd(LHS, RHS);
  95. }
  96. Constant *CreateSub(Constant *LHS, Constant *RHS,
  97. bool HasNUW = false, bool HasNSW = false) const override {
  98. return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
  99. }
  100. Constant *CreateFSub(Constant *LHS, Constant *RHS) const override {
  101. return ConstantExpr::getFSub(LHS, RHS);
  102. }
  103. Constant *CreateMul(Constant *LHS, Constant *RHS,
  104. bool HasNUW = false, bool HasNSW = false) const override {
  105. return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
  106. }
  107. Constant *CreateFMul(Constant *LHS, Constant *RHS) const override {
  108. return ConstantExpr::getFMul(LHS, RHS);
  109. }
  110. Constant *CreateUDiv(Constant *LHS, Constant *RHS,
  111. bool isExact = false) const override {
  112. return ConstantExpr::getUDiv(LHS, RHS, isExact);
  113. }
  114. Constant *CreateSDiv(Constant *LHS, Constant *RHS,
  115. bool isExact = false) const override {
  116. return ConstantExpr::getSDiv(LHS, RHS, isExact);
  117. }
  118. Constant *CreateFDiv(Constant *LHS, Constant *RHS) const override {
  119. return ConstantExpr::getFDiv(LHS, RHS);
  120. }
  121. Constant *CreateURem(Constant *LHS, Constant *RHS) const override {
  122. return ConstantExpr::getURem(LHS, RHS);
  123. }
  124. Constant *CreateSRem(Constant *LHS, Constant *RHS) const override {
  125. return ConstantExpr::getSRem(LHS, RHS);
  126. }
  127. Constant *CreateFRem(Constant *LHS, Constant *RHS) const override {
  128. return ConstantExpr::getFRem(LHS, RHS);
  129. }
  130. Constant *CreateShl(Constant *LHS, Constant *RHS,
  131. bool HasNUW = false, bool HasNSW = false) const override {
  132. return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
  133. }
  134. Constant *CreateLShr(Constant *LHS, Constant *RHS,
  135. bool isExact = false) const override {
  136. return ConstantExpr::getLShr(LHS, RHS, isExact);
  137. }
  138. Constant *CreateAShr(Constant *LHS, Constant *RHS,
  139. bool isExact = false) const override {
  140. return ConstantExpr::getAShr(LHS, RHS, isExact);
  141. }
  142. Constant *CreateOr(Constant *LHS, Constant *RHS) const {
  143. return ConstantExpr::getOr(LHS, RHS);
  144. }
  145. Constant *CreateXor(Constant *LHS, Constant *RHS) const override {
  146. return ConstantExpr::getXor(LHS, RHS);
  147. }
  148. Constant *CreateBinOp(Instruction::BinaryOps Opc,
  149. Constant *LHS, Constant *RHS) const override {
  150. return ConstantExpr::get(Opc, LHS, RHS);
  151. }
  152. //===--------------------------------------------------------------------===//
  153. // Unary Operators
  154. //===--------------------------------------------------------------------===//
  155. Constant *CreateNeg(Constant *C,
  156. bool HasNUW = false, bool HasNSW = false) const override {
  157. return ConstantExpr::getNeg(C, HasNUW, HasNSW);
  158. }
  159. Constant *CreateFNeg(Constant *C) const override {
  160. return ConstantExpr::getFNeg(C);
  161. }
  162. Constant *CreateNot(Constant *C) const override {
  163. return ConstantExpr::getNot(C);
  164. }
  165. Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override {
  166. return ConstantExpr::get(Opc, C);
  167. }
  168. //===--------------------------------------------------------------------===//
  169. // Cast/Conversion Operators
  170. //===--------------------------------------------------------------------===//
  171. Constant *CreateCast(Instruction::CastOps Op, Constant *C,
  172. Type *DestTy) const override {
  173. return ConstantExpr::getCast(Op, C, DestTy);
  174. }
  175. Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
  176. return ConstantExpr::getPointerCast(C, DestTy);
  177. }
  178. Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
  179. Type *DestTy) const override {
  180. return ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy);
  181. }
  182. Constant *CreateIntCast(Constant *C, Type *DestTy,
  183. bool isSigned) const override {
  184. return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
  185. }
  186. Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
  187. return ConstantExpr::getFPCast(C, DestTy);
  188. }
  189. Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
  190. return CreateCast(Instruction::BitCast, C, DestTy);
  191. }
  192. Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
  193. return CreateCast(Instruction::IntToPtr, C, DestTy);
  194. }
  195. Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
  196. return CreateCast(Instruction::PtrToInt, C, DestTy);
  197. }
  198. Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
  199. return ConstantExpr::getZExtOrBitCast(C, DestTy);
  200. }
  201. Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
  202. return ConstantExpr::getSExtOrBitCast(C, DestTy);
  203. }
  204. Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
  205. return ConstantExpr::getTruncOrBitCast(C, DestTy);
  206. }
  207. //===--------------------------------------------------------------------===//
  208. // Compare Instructions
  209. //===--------------------------------------------------------------------===//
  210. Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
  211. Constant *RHS) const override {
  212. return ConstantExpr::getCompare(P, LHS, RHS);
  213. }
  214. //===--------------------------------------------------------------------===//
  215. // Other Instructions
  216. //===--------------------------------------------------------------------===//
  217. Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const override {
  218. return ConstantExpr::getExtractElement(Vec, Idx);
  219. }
  220. Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
  221. Constant *Idx) const override {
  222. return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
  223. }
  224. Constant *CreateShuffleVector(Constant *V1, Constant *V2,
  225. ArrayRef<int> Mask) const override {
  226. return ConstantExpr::getShuffleVector(V1, V2, Mask);
  227. }
  228. Constant *CreateExtractValue(Constant *Agg,
  229. ArrayRef<unsigned> IdxList) const override {
  230. return ConstantExpr::getExtractValue(Agg, IdxList);
  231. }
  232. Constant *CreateInsertValue(Constant *Agg, Constant *Val,
  233. ArrayRef<unsigned> IdxList) const override {
  234. return ConstantExpr::getInsertValue(Agg, Val, IdxList);
  235. }
  236. };
  237. } // end namespace llvm
  238. #endif // LLVM_IR_CONSTANTFOLDER_H
  239. #ifdef __GNUC__
  240. #pragma GCC diagnostic pop
  241. #endif