TargetFolder.h 11 KB

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