/include/llvm/IR/NoFolder.h

https://gitlab.com/Birhetia/platform_external_llvm · C Header · 299 lines · 227 code · 27 blank · 45 comment · 14 complexity · 32ed56f29e721d0e3ea24ebd83aa4c6a MD5 · raw file

  1. //===- NoFolder.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 NoFolder class, a helper for IRBuilder. It provides
  11. // IRBuilder with a set of methods for creating unfolded constants. This is
  12. // useful for learners trying to understand how LLVM IR works, and who don't
  13. // want details to be hidden by the constant folder. For general constant
  14. // creation and folding, use ConstantExpr and the routines in
  15. // llvm/Analysis/ConstantFolding.h.
  16. //
  17. // Note: since it is not actually possible to create unfolded constants, this
  18. // class returns instructions rather than constants.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_IR_NOFOLDER_H
  22. #define LLVM_IR_NOFOLDER_H
  23. #include "llvm/ADT/ArrayRef.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/Instructions.h"
  26. namespace llvm {
  27. /// NoFolder - Create "constants" (actually, instructions) with no folding.
  28. class NoFolder {
  29. public:
  30. explicit NoFolder() {}
  31. //===--------------------------------------------------------------------===//
  32. // Binary Operators
  33. //===--------------------------------------------------------------------===//
  34. Instruction *CreateAdd(Constant *LHS, Constant *RHS,
  35. bool HasNUW = false, bool HasNSW = false) const {
  36. BinaryOperator *BO = BinaryOperator::CreateAdd(LHS, RHS);
  37. if (HasNUW) BO->setHasNoUnsignedWrap();
  38. if (HasNSW) BO->setHasNoSignedWrap();
  39. return BO;
  40. }
  41. Instruction *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
  42. return BinaryOperator::CreateNSWAdd(LHS, RHS);
  43. }
  44. Instruction *CreateNUWAdd(Constant *LHS, Constant *RHS) const {
  45. return BinaryOperator::CreateNUWAdd(LHS, RHS);
  46. }
  47. Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
  48. return BinaryOperator::CreateFAdd(LHS, RHS);
  49. }
  50. Instruction *CreateSub(Constant *LHS, Constant *RHS,
  51. bool HasNUW = false, bool HasNSW = false) const {
  52. BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
  53. if (HasNUW) BO->setHasNoUnsignedWrap();
  54. if (HasNSW) BO->setHasNoSignedWrap();
  55. return BO;
  56. }
  57. Instruction *CreateNSWSub(Constant *LHS, Constant *RHS) const {
  58. return BinaryOperator::CreateNSWSub(LHS, RHS);
  59. }
  60. Instruction *CreateNUWSub(Constant *LHS, Constant *RHS) const {
  61. return BinaryOperator::CreateNUWSub(LHS, RHS);
  62. }
  63. Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
  64. return BinaryOperator::CreateFSub(LHS, RHS);
  65. }
  66. Instruction *CreateMul(Constant *LHS, Constant *RHS,
  67. bool HasNUW = false, bool HasNSW = false) const {
  68. BinaryOperator *BO = BinaryOperator::CreateMul(LHS, RHS);
  69. if (HasNUW) BO->setHasNoUnsignedWrap();
  70. if (HasNSW) BO->setHasNoSignedWrap();
  71. return BO;
  72. }
  73. Instruction *CreateNSWMul(Constant *LHS, Constant *RHS) const {
  74. return BinaryOperator::CreateNSWMul(LHS, RHS);
  75. }
  76. Instruction *CreateNUWMul(Constant *LHS, Constant *RHS) const {
  77. return BinaryOperator::CreateNUWMul(LHS, RHS);
  78. }
  79. Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
  80. return BinaryOperator::CreateFMul(LHS, RHS);
  81. }
  82. Instruction *CreateUDiv(Constant *LHS, Constant *RHS,
  83. bool isExact = false) const {
  84. if (!isExact)
  85. return BinaryOperator::CreateUDiv(LHS, RHS);
  86. return BinaryOperator::CreateExactUDiv(LHS, RHS);
  87. }
  88. Instruction *CreateExactUDiv(Constant *LHS, Constant *RHS) const {
  89. return BinaryOperator::CreateExactUDiv(LHS, RHS);
  90. }
  91. Instruction *CreateSDiv(Constant *LHS, Constant *RHS,
  92. bool isExact = false) const {
  93. if (!isExact)
  94. return BinaryOperator::CreateSDiv(LHS, RHS);
  95. return BinaryOperator::CreateExactSDiv(LHS, RHS);
  96. }
  97. Instruction *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
  98. return BinaryOperator::CreateExactSDiv(LHS, RHS);
  99. }
  100. Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const {
  101. return BinaryOperator::CreateFDiv(LHS, RHS);
  102. }
  103. Instruction *CreateURem(Constant *LHS, Constant *RHS) const {
  104. return BinaryOperator::CreateURem(LHS, RHS);
  105. }
  106. Instruction *CreateSRem(Constant *LHS, Constant *RHS) const {
  107. return BinaryOperator::CreateSRem(LHS, RHS);
  108. }
  109. Instruction *CreateFRem(Constant *LHS, Constant *RHS) const {
  110. return BinaryOperator::CreateFRem(LHS, RHS);
  111. }
  112. Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
  113. bool HasNSW = false) const {
  114. BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
  115. if (HasNUW) BO->setHasNoUnsignedWrap();
  116. if (HasNSW) BO->setHasNoSignedWrap();
  117. return BO;
  118. }
  119. Instruction *CreateLShr(Constant *LHS, Constant *RHS,
  120. bool isExact = false) const {
  121. if (!isExact)
  122. return BinaryOperator::CreateLShr(LHS, RHS);
  123. return BinaryOperator::CreateExactLShr(LHS, RHS);
  124. }
  125. Instruction *CreateAShr(Constant *LHS, Constant *RHS,
  126. bool isExact = false) const {
  127. if (!isExact)
  128. return BinaryOperator::CreateAShr(LHS, RHS);
  129. return BinaryOperator::CreateExactAShr(LHS, RHS);
  130. }
  131. Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
  132. return BinaryOperator::CreateAnd(LHS, RHS);
  133. }
  134. Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
  135. return BinaryOperator::CreateOr(LHS, RHS);
  136. }
  137. Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
  138. return BinaryOperator::CreateXor(LHS, RHS);
  139. }
  140. Instruction *CreateBinOp(Instruction::BinaryOps Opc,
  141. Constant *LHS, Constant *RHS) const {
  142. return BinaryOperator::Create(Opc, LHS, RHS);
  143. }
  144. //===--------------------------------------------------------------------===//
  145. // Unary Operators
  146. //===--------------------------------------------------------------------===//
  147. Instruction *CreateNeg(Constant *C,
  148. bool HasNUW = false, bool HasNSW = false) const {
  149. BinaryOperator *BO = BinaryOperator::CreateNeg(C);
  150. if (HasNUW) BO->setHasNoUnsignedWrap();
  151. if (HasNSW) BO->setHasNoSignedWrap();
  152. return BO;
  153. }
  154. Instruction *CreateNSWNeg(Constant *C) const {
  155. return BinaryOperator::CreateNSWNeg(C);
  156. }
  157. Instruction *CreateNUWNeg(Constant *C) const {
  158. return BinaryOperator::CreateNUWNeg(C);
  159. }
  160. Instruction *CreateFNeg(Constant *C) const {
  161. return BinaryOperator::CreateFNeg(C);
  162. }
  163. Instruction *CreateNot(Constant *C) const {
  164. return BinaryOperator::CreateNot(C);
  165. }
  166. //===--------------------------------------------------------------------===//
  167. // Memory Instructions
  168. //===--------------------------------------------------------------------===//
  169. Constant *CreateGetElementPtr(Type *Ty, Constant *C,
  170. ArrayRef<Constant *> IdxList) const {
  171. return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
  172. }
  173. Constant *CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const {
  174. // This form of the function only exists to avoid ambiguous overload
  175. // warnings about whether to convert Idx to ArrayRef<Constant *> or
  176. // ArrayRef<Value *>.
  177. return ConstantExpr::getGetElementPtr(Ty, C, Idx);
  178. }
  179. Instruction *CreateGetElementPtr(Type *Ty, Constant *C,
  180. ArrayRef<Value *> IdxList) const {
  181. return GetElementPtrInst::Create(Ty, C, IdxList);
  182. }
  183. Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
  184. ArrayRef<Constant *> IdxList) const {
  185. return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
  186. }
  187. Constant *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
  188. Constant *Idx) const {
  189. // This form of the function only exists to avoid ambiguous overload
  190. // warnings about whether to convert Idx to ArrayRef<Constant *> or
  191. // ArrayRef<Value *>.
  192. return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
  193. }
  194. Instruction *CreateInBoundsGetElementPtr(Type *Ty, Constant *C,
  195. ArrayRef<Value *> IdxList) const {
  196. return GetElementPtrInst::CreateInBounds(Ty, C, IdxList);
  197. }
  198. //===--------------------------------------------------------------------===//
  199. // Cast/Conversion Operators
  200. //===--------------------------------------------------------------------===//
  201. Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
  202. Type *DestTy) const {
  203. return CastInst::Create(Op, C, DestTy);
  204. }
  205. Instruction *CreatePointerCast(Constant *C, Type *DestTy) const {
  206. return CastInst::CreatePointerCast(C, DestTy);
  207. }
  208. Instruction *CreateIntCast(Constant *C, Type *DestTy,
  209. bool isSigned) const {
  210. return CastInst::CreateIntegerCast(C, DestTy, isSigned);
  211. }
  212. Instruction *CreateFPCast(Constant *C, Type *DestTy) const {
  213. return CastInst::CreateFPCast(C, DestTy);
  214. }
  215. Instruction *CreateBitCast(Constant *C, Type *DestTy) const {
  216. return CreateCast(Instruction::BitCast, C, DestTy);
  217. }
  218. Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const {
  219. return CreateCast(Instruction::IntToPtr, C, DestTy);
  220. }
  221. Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const {
  222. return CreateCast(Instruction::PtrToInt, C, DestTy);
  223. }
  224. Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
  225. return CastInst::CreateZExtOrBitCast(C, DestTy);
  226. }
  227. Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
  228. return CastInst::CreateSExtOrBitCast(C, DestTy);
  229. }
  230. Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
  231. return CastInst::CreateTruncOrBitCast(C, DestTy);
  232. }
  233. //===--------------------------------------------------------------------===//
  234. // Compare Instructions
  235. //===--------------------------------------------------------------------===//
  236. Instruction *CreateICmp(CmpInst::Predicate P,
  237. Constant *LHS, Constant *RHS) const {
  238. return new ICmpInst(P, LHS, RHS);
  239. }
  240. Instruction *CreateFCmp(CmpInst::Predicate P,
  241. Constant *LHS, Constant *RHS) const {
  242. return new FCmpInst(P, LHS, RHS);
  243. }
  244. //===--------------------------------------------------------------------===//
  245. // Other Instructions
  246. //===--------------------------------------------------------------------===//
  247. Instruction *CreateSelect(Constant *C,
  248. Constant *True, Constant *False) const {
  249. return SelectInst::Create(C, True, False);
  250. }
  251. Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
  252. return ExtractElementInst::Create(Vec, Idx);
  253. }
  254. Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
  255. Constant *Idx) const {
  256. return InsertElementInst::Create(Vec, NewElt, Idx);
  257. }
  258. Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
  259. Constant *Mask) const {
  260. return new ShuffleVectorInst(V1, V2, Mask);
  261. }
  262. Instruction *CreateExtractValue(Constant *Agg,
  263. ArrayRef<unsigned> IdxList) const {
  264. return ExtractValueInst::Create(Agg, IdxList);
  265. }
  266. Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
  267. ArrayRef<unsigned> IdxList) const {
  268. return InsertValueInst::Create(Agg, Val, IdxList);
  269. }
  270. };
  271. }
  272. #endif