Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
11 KiB

  1. //======-- llvm/Support/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_SUPPORT_NOFOLDER_H
  22. #define LLVM_SUPPORT_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(Constant *C,
  170. ArrayRef<Constant *> IdxList) const {
  171. return ConstantExpr::getGetElementPtr(C, IdxList);
  172. }
  173. Constant *CreateGetElementPtr(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(C, Idx);
  178. }
  179. Instruction *CreateGetElementPtr(Constant *C,
  180. ArrayRef<Value *> IdxList) const {
  181. return GetElementPtrInst::Create(C, IdxList);
  182. }
  183. Constant *CreateInBoundsGetElementPtr(Constant *C,
  184. ArrayRef<Constant *> IdxList) const {
  185. return ConstantExpr::getInBoundsGetElementPtr(C, IdxList);
  186. }
  187. Constant *CreateInBoundsGetElementPtr(Constant *C, Constant *Idx) const {
  188. // This form of the function only exists to avoid ambiguous overload
  189. // warnings about whether to convert Idx to ArrayRef<Constant *> or
  190. // ArrayRef<Value *>.
  191. return ConstantExpr::getInBoundsGetElementPtr(C, Idx);
  192. }
  193. Instruction *CreateInBoundsGetElementPtr(Constant *C,
  194. ArrayRef<Value *> IdxList) const {
  195. return GetElementPtrInst::CreateInBounds(C, IdxList);
  196. }
  197. //===--------------------------------------------------------------------===//
  198. // Cast/Conversion Operators
  199. //===--------------------------------------------------------------------===//
  200. Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
  201. Type *DestTy) const {
  202. return CastInst::Create(Op, C, DestTy);
  203. }
  204. Instruction *CreatePointerCast(Constant *C, Type *DestTy) const {
  205. return CastInst::CreatePointerCast(C, DestTy);
  206. }
  207. Instruction *CreateIntCast(Constant *C, Type *DestTy,
  208. bool isSigned) const {
  209. return CastInst::CreateIntegerCast(C, DestTy, isSigned);
  210. }
  211. Instruction *CreateFPCast(Constant *C, Type *DestTy) const {
  212. return CastInst::CreateFPCast(C, DestTy);
  213. }
  214. Instruction *CreateBitCast(Constant *C, Type *DestTy) const {
  215. return CreateCast(Instruction::BitCast, C, DestTy);
  216. }
  217. Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const {
  218. return CreateCast(Instruction::IntToPtr, C, DestTy);
  219. }
  220. Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const {
  221. return CreateCast(Instruction::PtrToInt, C, DestTy);
  222. }
  223. Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const {
  224. return CastInst::CreateZExtOrBitCast(C, DestTy);
  225. }
  226. Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const {
  227. return CastInst::CreateSExtOrBitCast(C, DestTy);
  228. }
  229. Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const {
  230. return CastInst::CreateTruncOrBitCast(C, DestTy);
  231. }
  232. //===--------------------------------------------------------------------===//
  233. // Compare Instructions
  234. //===--------------------------------------------------------------------===//
  235. Instruction *CreateICmp(CmpInst::Predicate P,
  236. Constant *LHS, Constant *RHS) const {
  237. return new ICmpInst(P, LHS, RHS);
  238. }
  239. Instruction *CreateFCmp(CmpInst::Predicate P,
  240. Constant *LHS, Constant *RHS) const {
  241. return new FCmpInst(P, LHS, RHS);
  242. }
  243. //===--------------------------------------------------------------------===//
  244. // Other Instructions
  245. //===--------------------------------------------------------------------===//
  246. Instruction *CreateSelect(Constant *C,
  247. Constant *True, Constant *False) const {
  248. return SelectInst::Create(C, True, False);
  249. }
  250. Instruction *CreateExtractElement(Constant *Vec, Constant *Idx) const {
  251. return ExtractElementInst::Create(Vec, Idx);
  252. }
  253. Instruction *CreateInsertElement(Constant *Vec, Constant *NewElt,
  254. Constant *Idx) const {
  255. return InsertElementInst::Create(Vec, NewElt, Idx);
  256. }
  257. Instruction *CreateShuffleVector(Constant *V1, Constant *V2,
  258. Constant *Mask) const {
  259. return new ShuffleVectorInst(V1, V2, Mask);
  260. }
  261. Instruction *CreateExtractValue(Constant *Agg,
  262. ArrayRef<unsigned> IdxList) const {
  263. return ExtractValueInst::Create(Agg, IdxList);
  264. }
  265. Instruction *CreateInsertValue(Constant *Agg, Constant *Val,
  266. ArrayRef<unsigned> IdxList) const {
  267. return InsertValueInst::Create(Agg, Val, IdxList);
  268. }
  269. };
  270. }
  271. #endif