TargetFolder.h 10 KB

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