TargetFolder.h 11 KB

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