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.

1348 lines
55 KiB

  1. //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- 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 IRBuilder class, which is used as a convenient way
  11. // to create LLVM instructions with a consistent and simplified interface.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_IRBUILDER_H
  15. #define LLVM_IRBUILDER_H
  16. #include "llvm/Instructions.h"
  17. #include "llvm/BasicBlock.h"
  18. #include "llvm/LLVMContext.h"
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/ADT/Twine.h"
  22. #include "llvm/Support/ConstantFolder.h"
  23. namespace llvm {
  24. class MDNode;
  25. /// IRBuilderDefaultInserter - This provides the default implementation of the
  26. /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
  27. /// created by IRBuilder and needs to be inserted. By default, this inserts the
  28. /// instruction at the insertion point.
  29. template <bool preserveNames = true>
  30. class IRBuilderDefaultInserter {
  31. protected:
  32. void InsertHelper(Instruction *I, const Twine &Name,
  33. BasicBlock *BB, BasicBlock::iterator InsertPt) const {
  34. if (BB) BB->getInstList().insert(InsertPt, I);
  35. if (preserveNames)
  36. I->setName(Name);
  37. }
  38. };
  39. /// IRBuilderBase - Common base class shared among various IRBuilders.
  40. class IRBuilderBase {
  41. DebugLoc CurDbgLocation;
  42. protected:
  43. BasicBlock *BB;
  44. BasicBlock::iterator InsertPt;
  45. LLVMContext &Context;
  46. public:
  47. IRBuilderBase(LLVMContext &context)
  48. : Context(context) {
  49. ClearInsertionPoint();
  50. }
  51. //===--------------------------------------------------------------------===//
  52. // Builder configuration methods
  53. //===--------------------------------------------------------------------===//
  54. /// ClearInsertionPoint - Clear the insertion point: created instructions will
  55. /// not be inserted into a block.
  56. void ClearInsertionPoint() {
  57. BB = 0;
  58. }
  59. BasicBlock *GetInsertBlock() const { return BB; }
  60. BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
  61. LLVMContext &getContext() const { return Context; }
  62. /// SetInsertPoint - This specifies that created instructions should be
  63. /// appended to the end of the specified block.
  64. void SetInsertPoint(BasicBlock *TheBB) {
  65. BB = TheBB;
  66. InsertPt = BB->end();
  67. }
  68. /// SetInsertPoint - This specifies that created instructions should be
  69. /// inserted before the specified instruction.
  70. void SetInsertPoint(Instruction *I) {
  71. BB = I->getParent();
  72. InsertPt = I;
  73. SetCurrentDebugLocation(I->getDebugLoc());
  74. }
  75. /// SetInsertPoint - This specifies that created instructions should be
  76. /// inserted at the specified point.
  77. void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
  78. BB = TheBB;
  79. InsertPt = IP;
  80. }
  81. /// SetInsertPoint(Use) - Find the nearest point that dominates this use, and
  82. /// specify that created instructions should be inserted at this point.
  83. void SetInsertPoint(Use &U) {
  84. Instruction *UseInst = cast<Instruction>(U.getUser());
  85. if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
  86. BasicBlock *PredBB = Phi->getIncomingBlock(U);
  87. assert(U != PredBB->getTerminator() && "critical edge not split");
  88. SetInsertPoint(PredBB, PredBB->getTerminator());
  89. return;
  90. }
  91. SetInsertPoint(UseInst);
  92. }
  93. /// SetCurrentDebugLocation - Set location information used by debugging
  94. /// information.
  95. void SetCurrentDebugLocation(const DebugLoc &L) {
  96. CurDbgLocation = L;
  97. }
  98. /// getCurrentDebugLocation - Get location information used by debugging
  99. /// information.
  100. DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
  101. /// SetInstDebugLocation - If this builder has a current debug location, set
  102. /// it on the specified instruction.
  103. void SetInstDebugLocation(Instruction *I) const {
  104. if (!CurDbgLocation.isUnknown())
  105. I->setDebugLoc(CurDbgLocation);
  106. }
  107. /// getCurrentFunctionReturnType - Get the return type of the current function
  108. /// that we're emitting into.
  109. Type *getCurrentFunctionReturnType() const;
  110. /// InsertPoint - A saved insertion point.
  111. class InsertPoint {
  112. BasicBlock *Block;
  113. BasicBlock::iterator Point;
  114. public:
  115. /// Creates a new insertion point which doesn't point to anything.
  116. InsertPoint() : Block(0) {}
  117. /// Creates a new insertion point at the given location.
  118. InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
  119. : Block(InsertBlock), Point(InsertPoint) {}
  120. /// isSet - Returns true if this insert point is set.
  121. bool isSet() const { return (Block != 0); }
  122. llvm::BasicBlock *getBlock() const { return Block; }
  123. llvm::BasicBlock::iterator getPoint() const { return Point; }
  124. };
  125. /// saveIP - Returns the current insert point.
  126. InsertPoint saveIP() const {
  127. return InsertPoint(GetInsertBlock(), GetInsertPoint());
  128. }
  129. /// saveAndClearIP - Returns the current insert point, clearing it
  130. /// in the process.
  131. InsertPoint saveAndClearIP() {
  132. InsertPoint IP(GetInsertBlock(), GetInsertPoint());
  133. ClearInsertionPoint();
  134. return IP;
  135. }
  136. /// restoreIP - Sets the current insert point to a previously-saved
  137. /// location.
  138. void restoreIP(InsertPoint IP) {
  139. if (IP.isSet())
  140. SetInsertPoint(IP.getBlock(), IP.getPoint());
  141. else
  142. ClearInsertionPoint();
  143. }
  144. //===--------------------------------------------------------------------===//
  145. // Miscellaneous creation methods.
  146. //===--------------------------------------------------------------------===//
  147. /// CreateGlobalString - Make a new global variable with an initializer that
  148. /// has array of i8 type filled in with the nul terminated string value
  149. /// specified. The new global variable will be marked mergable with any
  150. /// others of the same contents. If Name is specified, it is the name of the
  151. /// global variable created.
  152. Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
  153. /// getInt1 - Get a constant value representing either true or false.
  154. ConstantInt *getInt1(bool V) {
  155. return ConstantInt::get(getInt1Ty(), V);
  156. }
  157. /// getTrue - Get the constant value for i1 true.
  158. ConstantInt *getTrue() {
  159. return ConstantInt::getTrue(Context);
  160. }
  161. /// getFalse - Get the constant value for i1 false.
  162. ConstantInt *getFalse() {
  163. return ConstantInt::getFalse(Context);
  164. }
  165. /// getInt8 - Get a constant 8-bit value.
  166. ConstantInt *getInt8(uint8_t C) {
  167. return ConstantInt::get(getInt8Ty(), C);
  168. }
  169. /// getInt16 - Get a constant 16-bit value.
  170. ConstantInt *getInt16(uint16_t C) {
  171. return ConstantInt::get(getInt16Ty(), C);
  172. }
  173. /// getInt32 - Get a constant 32-bit value.
  174. ConstantInt *getInt32(uint32_t C) {
  175. return ConstantInt::get(getInt32Ty(), C);
  176. }
  177. /// getInt64 - Get a constant 64-bit value.
  178. ConstantInt *getInt64(uint64_t C) {
  179. return ConstantInt::get(getInt64Ty(), C);
  180. }
  181. /// getInt - Get a constant integer value.
  182. ConstantInt *getInt(const APInt &AI) {
  183. return ConstantInt::get(Context, AI);
  184. }
  185. //===--------------------------------------------------------------------===//
  186. // Type creation methods
  187. //===--------------------------------------------------------------------===//
  188. /// getInt1Ty - Fetch the type representing a single bit
  189. IntegerType *getInt1Ty() {
  190. return Type::getInt1Ty(Context);
  191. }
  192. /// getInt8Ty - Fetch the type representing an 8-bit integer.
  193. IntegerType *getInt8Ty() {
  194. return Type::getInt8Ty(Context);
  195. }
  196. /// getInt16Ty - Fetch the type representing a 16-bit integer.
  197. IntegerType *getInt16Ty() {
  198. return Type::getInt16Ty(Context);
  199. }
  200. /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
  201. IntegerType *getInt32Ty() {
  202. return Type::getInt32Ty(Context);
  203. }
  204. /// getInt64Ty - Fetch the type representing a 64-bit integer.
  205. IntegerType *getInt64Ty() {
  206. return Type::getInt64Ty(Context);
  207. }
  208. /// getFloatTy - Fetch the type representing a 32-bit floating point value.
  209. Type *getFloatTy() {
  210. return Type::getFloatTy(Context);
  211. }
  212. /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
  213. Type *getDoubleTy() {
  214. return Type::getDoubleTy(Context);
  215. }
  216. /// getVoidTy - Fetch the type representing void.
  217. Type *getVoidTy() {
  218. return Type::getVoidTy(Context);
  219. }
  220. PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
  221. return Type::getInt8PtrTy(Context, AddrSpace);
  222. }
  223. //===--------------------------------------------------------------------===//
  224. // Intrinsic creation methods
  225. //===--------------------------------------------------------------------===//
  226. /// CreateMemSet - Create and insert a memset to the specified pointer and the
  227. /// specified value. If the pointer isn't an i8*, it will be converted. If a
  228. /// TBAA tag is specified, it will be added to the instruction.
  229. CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
  230. bool isVolatile = false, MDNode *TBAATag = 0) {
  231. return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
  232. }
  233. CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
  234. bool isVolatile = false, MDNode *TBAATag = 0);
  235. /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
  236. /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
  237. /// specified, it will be added to the instruction.
  238. CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
  239. bool isVolatile = false, MDNode *TBAATag = 0,
  240. MDNode *TBAAStructTag = 0) {
  241. return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
  242. TBAAStructTag);
  243. }
  244. CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
  245. bool isVolatile = false, MDNode *TBAATag = 0,
  246. MDNode *TBAAStructTag = 0);
  247. /// CreateMemMove - Create and insert a memmove between the specified
  248. /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
  249. /// tag is specified, it will be added to the instruction.
  250. CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
  251. bool isVolatile = false, MDNode *TBAATag = 0) {
  252. return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
  253. }
  254. CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
  255. bool isVolatile = false, MDNode *TBAATag = 0);
  256. /// CreateLifetimeStart - Create a lifetime.start intrinsic. If the pointer
  257. /// isn't i8* it will be converted.
  258. CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
  259. /// CreateLifetimeEnd - Create a lifetime.end intrinsic. If the pointer isn't
  260. /// i8* it will be converted.
  261. CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
  262. private:
  263. Value *getCastedInt8PtrValue(Value *Ptr);
  264. };
  265. /// IRBuilder - This provides a uniform API for creating instructions and
  266. /// inserting them into a basic block: either at the end of a BasicBlock, or
  267. /// at a specific iterator location in a block.
  268. ///
  269. /// Note that the builder does not expose the full generality of LLVM
  270. /// instructions. For access to extra instruction properties, use the mutators
  271. /// (e.g. setVolatile) on the instructions after they have been created.
  272. /// The first template argument handles whether or not to preserve names in the
  273. /// final instruction output. This defaults to on. The second template argument
  274. /// specifies a class to use for creating constants. This defaults to creating
  275. /// minimally folded constants. The fourth template argument allows clients to
  276. /// specify custom insertion hooks that are called on every newly created
  277. /// insertion.
  278. template<bool preserveNames = true, typename T = ConstantFolder,
  279. typename Inserter = IRBuilderDefaultInserter<preserveNames> >
  280. class IRBuilder : public IRBuilderBase, public Inserter {
  281. T Folder;
  282. MDNode *DefaultFPMathTag;
  283. public:
  284. IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
  285. MDNode *FPMathTag = 0)
  286. : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag) {
  287. }
  288. explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0) : IRBuilderBase(C),
  289. Folder(), DefaultFPMathTag(FPMathTag) {
  290. }
  291. explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
  292. : IRBuilderBase(TheBB->getContext()), Folder(F),
  293. DefaultFPMathTag(FPMathTag) {
  294. SetInsertPoint(TheBB);
  295. }
  296. explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
  297. : IRBuilderBase(TheBB->getContext()), Folder(),
  298. DefaultFPMathTag(FPMathTag) {
  299. SetInsertPoint(TheBB);
  300. }
  301. explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
  302. : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
  303. SetInsertPoint(IP);
  304. SetCurrentDebugLocation(IP->getDebugLoc());
  305. }
  306. explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
  307. : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
  308. SetInsertPoint(U);
  309. SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
  310. }
  311. IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
  312. MDNode *FPMathTag = 0)
  313. : IRBuilderBase(TheBB->getContext()), Folder(F),
  314. DefaultFPMathTag(FPMathTag) {
  315. SetInsertPoint(TheBB, IP);
  316. }
  317. IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
  318. : IRBuilderBase(TheBB->getContext()), Folder(),
  319. DefaultFPMathTag(FPMathTag) {
  320. SetInsertPoint(TheBB, IP);
  321. }
  322. /// getFolder - Get the constant folder being used.
  323. const T &getFolder() { return Folder; }
  324. /// getDefaultFPMathTag - Get the floating point math metadata being used.
  325. MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
  326. /// SetDefaultFPMathTag - Set the floating point math metadata to be used.
  327. void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
  328. /// isNamePreserving - Return true if this builder is configured to actually
  329. /// add the requested names to IR created through it.
  330. bool isNamePreserving() const { return preserveNames; }
  331. /// Insert - Insert and return the specified instruction.
  332. template<typename InstTy>
  333. InstTy *Insert(InstTy *I, const Twine &Name = "") const {
  334. this->InsertHelper(I, Name, BB, InsertPt);
  335. if (!getCurrentDebugLocation().isUnknown())
  336. this->SetInstDebugLocation(I);
  337. return I;
  338. }
  339. /// Insert - No-op overload to handle constants.
  340. Constant *Insert(Constant *C, const Twine& = "") const {
  341. return C;
  342. }
  343. //===--------------------------------------------------------------------===//
  344. // Instruction creation methods: Terminators
  345. //===--------------------------------------------------------------------===//
  346. private:
  347. /// \brief Helper to add branch weight metadata onto an instruction.
  348. /// \returns The annotated instruction.
  349. template <typename InstTy>
  350. InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
  351. if (Weights)
  352. I->setMetadata(LLVMContext::MD_prof, Weights);
  353. return I;
  354. }
  355. public:
  356. /// CreateRetVoid - Create a 'ret void' instruction.
  357. ReturnInst *CreateRetVoid() {
  358. return Insert(ReturnInst::Create(Context));
  359. }
  360. /// @verbatim
  361. /// CreateRet - Create a 'ret <val>' instruction.
  362. /// @endverbatim
  363. ReturnInst *CreateRet(Value *V) {
  364. return Insert(ReturnInst::Create(Context, V));
  365. }
  366. /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
  367. /// with one Value from the retVals array each, that build a aggregate
  368. /// return value one value at a time, and a ret instruction to return
  369. /// the resulting aggregate value. This is a convenience function for
  370. /// code that uses aggregate return values as a vehicle for having
  371. /// multiple return values.
  372. ///
  373. ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
  374. Value *V = UndefValue::get(getCurrentFunctionReturnType());
  375. for (unsigned i = 0; i != N; ++i)
  376. V = CreateInsertValue(V, retVals[i], i, "mrv");
  377. return Insert(ReturnInst::Create(Context, V));
  378. }
  379. /// CreateBr - Create an unconditional 'br label X' instruction.
  380. BranchInst *CreateBr(BasicBlock *Dest) {
  381. return Insert(BranchInst::Create(Dest));
  382. }
  383. /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
  384. /// instruction.
  385. BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
  386. MDNode *BranchWeights = 0) {
  387. return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
  388. BranchWeights));
  389. }
  390. /// CreateSwitch - Create a switch instruction with the specified value,
  391. /// default dest, and with a hint for the number of cases that will be added
  392. /// (for efficient allocation).
  393. SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
  394. MDNode *BranchWeights = 0) {
  395. return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
  396. BranchWeights));
  397. }
  398. /// CreateIndirectBr - Create an indirect branch instruction with the
  399. /// specified address operand, with an optional hint for the number of
  400. /// destinations that will be added (for efficient allocation).
  401. IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
  402. return Insert(IndirectBrInst::Create(Addr, NumDests));
  403. }
  404. InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
  405. BasicBlock *UnwindDest, const Twine &Name = "") {
  406. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
  407. ArrayRef<Value *>()),
  408. Name);
  409. }
  410. InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
  411. BasicBlock *UnwindDest, Value *Arg1,
  412. const Twine &Name = "") {
  413. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
  414. Name);
  415. }
  416. InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
  417. BasicBlock *UnwindDest, Value *Arg1,
  418. Value *Arg2, Value *Arg3,
  419. const Twine &Name = "") {
  420. Value *Args[] = { Arg1, Arg2, Arg3 };
  421. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
  422. Name);
  423. }
  424. /// CreateInvoke - Create an invoke instruction.
  425. InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
  426. BasicBlock *UnwindDest, ArrayRef<Value *> Args,
  427. const Twine &Name = "") {
  428. return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
  429. Name);
  430. }
  431. ResumeInst *CreateResume(Value *Exn) {
  432. return Insert(ResumeInst::Create(Exn));
  433. }
  434. UnreachableInst *CreateUnreachable() {
  435. return Insert(new UnreachableInst(Context));
  436. }
  437. //===--------------------------------------------------------------------===//
  438. // Instruction creation methods: Binary Operators
  439. //===--------------------------------------------------------------------===//
  440. private:
  441. BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
  442. Value *LHS, Value *RHS,
  443. const Twine &Name,
  444. bool HasNUW, bool HasNSW) {
  445. BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
  446. if (HasNUW) BO->setHasNoUnsignedWrap();
  447. if (HasNSW) BO->setHasNoSignedWrap();
  448. return BO;
  449. }
  450. Instruction *AddFPMathTag(Instruction *I, MDNode *FPMathTag) const {
  451. if (!FPMathTag)
  452. FPMathTag = DefaultFPMathTag;
  453. if (FPMathTag)
  454. I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
  455. return I;
  456. }
  457. public:
  458. Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
  459. bool HasNUW = false, bool HasNSW = false) {
  460. if (Constant *LC = dyn_cast<Constant>(LHS))
  461. if (Constant *RC = dyn_cast<Constant>(RHS))
  462. return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
  463. return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
  464. HasNUW, HasNSW);
  465. }
  466. Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
  467. return CreateAdd(LHS, RHS, Name, false, true);
  468. }
  469. Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
  470. return CreateAdd(LHS, RHS, Name, true, false);
  471. }
  472. Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
  473. MDNode *FPMathTag = 0) {
  474. if (Constant *LC = dyn_cast<Constant>(LHS))
  475. if (Constant *RC = dyn_cast<Constant>(RHS))
  476. return Insert(Folder.CreateFAdd(LC, RC), Name);
  477. return Insert(AddFPMathTag(BinaryOperator::CreateFAdd(LHS, RHS),
  478. FPMathTag), Name);
  479. }
  480. Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
  481. bool HasNUW = false, bool HasNSW = false) {
  482. if (Constant *LC = dyn_cast<Constant>(LHS))
  483. if (Constant *RC = dyn_cast<Constant>(RHS))
  484. return Insert(Folder.CreateSub(LC, RC), Name);
  485. return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
  486. HasNUW, HasNSW);
  487. }
  488. Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
  489. return CreateSub(LHS, RHS, Name, false, true);
  490. }
  491. Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
  492. return CreateSub(LHS, RHS, Name, true, false);
  493. }
  494. Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
  495. MDNode *FPMathTag = 0) {
  496. if (Constant *LC = dyn_cast<Constant>(LHS))
  497. if (Constant *RC = dyn_cast<Constant>(RHS))
  498. return Insert(Folder.CreateFSub(LC, RC), Name);
  499. return Insert(AddFPMathTag(BinaryOperator::CreateFSub(LHS, RHS),
  500. FPMathTag), Name);
  501. }
  502. Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
  503. bool HasNUW = false, bool HasNSW = false) {
  504. if (Constant *LC = dyn_cast<Constant>(LHS))
  505. if (Constant *RC = dyn_cast<Constant>(RHS))
  506. return Insert(Folder.CreateMul(LC, RC), Name);
  507. return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
  508. HasNUW, HasNSW);
  509. }
  510. Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
  511. return CreateMul(LHS, RHS, Name, false, true);
  512. }
  513. Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
  514. return CreateMul(LHS, RHS, Name, true, false);
  515. }
  516. Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
  517. MDNode *FPMathTag = 0) {
  518. if (Constant *LC = dyn_cast<Constant>(LHS))
  519. if (Constant *RC = dyn_cast<Constant>(RHS))
  520. return Insert(Folder.CreateFMul(LC, RC), Name);
  521. return Insert(AddFPMathTag(BinaryOperator::CreateFMul(LHS, RHS),
  522. FPMathTag), Name);
  523. }
  524. Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  525. bool isExact = false) {
  526. if (Constant *LC = dyn_cast<Constant>(LHS))
  527. if (Constant *RC = dyn_cast<Constant>(RHS))
  528. return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
  529. if (!isExact)
  530. return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
  531. return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
  532. }
  533. Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
  534. return CreateUDiv(LHS, RHS, Name, true);
  535. }
  536. Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  537. bool isExact = false) {
  538. if (Constant *LC = dyn_cast<Constant>(LHS))
  539. if (Constant *RC = dyn_cast<Constant>(RHS))
  540. return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
  541. if (!isExact)
  542. return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
  543. return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
  544. }
  545. Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
  546. return CreateSDiv(LHS, RHS, Name, true);
  547. }
  548. Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
  549. MDNode *FPMathTag = 0) {
  550. if (Constant *LC = dyn_cast<Constant>(LHS))
  551. if (Constant *RC = dyn_cast<Constant>(RHS))
  552. return Insert(Folder.CreateFDiv(LC, RC), Name);
  553. return Insert(AddFPMathTag(BinaryOperator::CreateFDiv(LHS, RHS),
  554. FPMathTag), Name);
  555. }
  556. Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
  557. if (Constant *LC = dyn_cast<Constant>(LHS))
  558. if (Constant *RC = dyn_cast<Constant>(RHS))
  559. return Insert(Folder.CreateURem(LC, RC), Name);
  560. return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
  561. }
  562. Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
  563. if (Constant *LC = dyn_cast<Constant>(LHS))
  564. if (Constant *RC = dyn_cast<Constant>(RHS))
  565. return Insert(Folder.CreateSRem(LC, RC), Name);
  566. return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
  567. }
  568. Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
  569. MDNode *FPMathTag = 0) {
  570. if (Constant *LC = dyn_cast<Constant>(LHS))
  571. if (Constant *RC = dyn_cast<Constant>(RHS))
  572. return Insert(Folder.CreateFRem(LC, RC), Name);
  573. return Insert(AddFPMathTag(BinaryOperator::CreateFRem(LHS, RHS),
  574. FPMathTag), Name);
  575. }
  576. Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
  577. bool HasNUW = false, bool HasNSW = false) {
  578. if (Constant *LC = dyn_cast<Constant>(LHS))
  579. if (Constant *RC = dyn_cast<Constant>(RHS))
  580. return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
  581. return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
  582. HasNUW, HasNSW);
  583. }
  584. Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
  585. bool HasNUW = false, bool HasNSW = false) {
  586. return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
  587. HasNUW, HasNSW);
  588. }
  589. Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
  590. bool HasNUW = false, bool HasNSW = false) {
  591. return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
  592. HasNUW, HasNSW);
  593. }
  594. Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
  595. bool isExact = false) {
  596. if (Constant *LC = dyn_cast<Constant>(LHS))
  597. if (Constant *RC = dyn_cast<Constant>(RHS))
  598. return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
  599. if (!isExact)
  600. return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
  601. return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
  602. }
  603. Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
  604. bool isExact = false) {
  605. return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  606. }
  607. Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
  608. bool isExact = false) {
  609. return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  610. }
  611. Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
  612. bool isExact = false) {
  613. if (Constant *LC = dyn_cast<Constant>(LHS))
  614. if (Constant *RC = dyn_cast<Constant>(RHS))
  615. return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
  616. if (!isExact)
  617. return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
  618. return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
  619. }
  620. Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
  621. bool isExact = false) {
  622. return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  623. }
  624. Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
  625. bool isExact = false) {
  626. return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
  627. }
  628. Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
  629. if (Constant *RC = dyn_cast<Constant>(RHS)) {
  630. if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
  631. return LHS; // LHS & -1 -> LHS
  632. if (Constant *LC = dyn_cast<Constant>(LHS))
  633. return Insert(Folder.CreateAnd(LC, RC), Name);
  634. }
  635. return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
  636. }
  637. Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  638. return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  639. }
  640. Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  641. return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  642. }
  643. Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
  644. if (Constant *RC = dyn_cast<Constant>(RHS)) {
  645. if (RC->isNullValue())
  646. return LHS; // LHS | 0 -> LHS
  647. if (Constant *LC = dyn_cast<Constant>(LHS))
  648. return Insert(Folder.CreateOr(LC, RC), Name);
  649. }
  650. return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
  651. }
  652. Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  653. return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  654. }
  655. Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  656. return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  657. }
  658. Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
  659. if (Constant *LC = dyn_cast<Constant>(LHS))
  660. if (Constant *RC = dyn_cast<Constant>(RHS))
  661. return Insert(Folder.CreateXor(LC, RC), Name);
  662. return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
  663. }
  664. Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
  665. return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  666. }
  667. Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
  668. return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
  669. }
  670. Value *CreateBinOp(Instruction::BinaryOps Opc,
  671. Value *LHS, Value *RHS, const Twine &Name = "") {
  672. if (Constant *LC = dyn_cast<Constant>(LHS))
  673. if (Constant *RC = dyn_cast<Constant>(RHS))
  674. return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
  675. return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
  676. }
  677. Value *CreateNeg(Value *V, const Twine &Name = "",
  678. bool HasNUW = false, bool HasNSW = false) {
  679. if (Constant *VC = dyn_cast<Constant>(V))
  680. return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
  681. BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
  682. if (HasNUW) BO->setHasNoUnsignedWrap();
  683. if (HasNSW) BO->setHasNoSignedWrap();
  684. return BO;
  685. }
  686. Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
  687. return CreateNeg(V, Name, false, true);
  688. }
  689. Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
  690. return CreateNeg(V, Name, true, false);
  691. }
  692. Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
  693. if (Constant *VC = dyn_cast<Constant>(V))
  694. return Insert(Folder.CreateFNeg(VC), Name);
  695. return Insert(AddFPMathTag(BinaryOperator::CreateFNeg(V), FPMathTag), Name);
  696. }
  697. Value *CreateNot(Value *V, const Twine &Name = "") {
  698. if (Constant *VC = dyn_cast<Constant>(V))
  699. return Insert(Folder.CreateNot(VC), Name);
  700. return Insert(BinaryOperator::CreateNot(V), Name);
  701. }
  702. //===--------------------------------------------------------------------===//
  703. // Instruction creation methods: Memory Instructions
  704. //===--------------------------------------------------------------------===//
  705. AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
  706. const Twine &Name = "") {
  707. return Insert(new AllocaInst(Ty, ArraySize), Name);
  708. }
  709. // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
  710. // converting the string to 'bool' for the isVolatile parameter.
  711. LoadInst *CreateLoad(Value *Ptr, const char *Name) {
  712. return Insert(new LoadInst(Ptr), Name);
  713. }
  714. LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
  715. return Insert(new LoadInst(Ptr), Name);
  716. }
  717. LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
  718. return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
  719. }
  720. StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
  721. return Insert(new StoreInst(Val, Ptr, isVolatile));
  722. }
  723. // Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")' correctly,
  724. // instead of converting the string to 'bool' for the isVolatile parameter.
  725. LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
  726. LoadInst *LI = CreateLoad(Ptr, Name);
  727. LI->setAlignment(Align);
  728. return LI;
  729. }
  730. LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
  731. const Twine &Name = "") {
  732. LoadInst *LI = CreateLoad(Ptr, Name);
  733. LI->setAlignment(Align);
  734. return LI;
  735. }
  736. LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
  737. const Twine &Name = "") {
  738. LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
  739. LI->setAlignment(Align);
  740. return LI;
  741. }
  742. StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
  743. bool isVolatile = false) {
  744. StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
  745. SI->setAlignment(Align);
  746. return SI;
  747. }
  748. FenceInst *CreateFence(AtomicOrdering Ordering,
  749. SynchronizationScope SynchScope = CrossThread) {
  750. return Insert(new FenceInst(Context, Ordering, SynchScope));
  751. }
  752. AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
  753. AtomicOrdering Ordering,
  754. SynchronizationScope SynchScope = CrossThread) {
  755. return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
  756. }
  757. AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
  758. AtomicOrdering Ordering,
  759. SynchronizationScope SynchScope = CrossThread) {
  760. return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
  761. }
  762. Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
  763. const Twine &Name = "") {
  764. if (Constant *PC = dyn_cast<Constant>(Ptr)) {
  765. // Every index must be constant.
  766. size_t i, e;
  767. for (i = 0, e = IdxList.size(); i != e; ++i)
  768. if (!isa<Constant>(IdxList[i]))
  769. break;
  770. if (i == e)
  771. return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
  772. }
  773. return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
  774. }
  775. Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
  776. const Twine &Name = "") {
  777. if (Constant *PC = dyn_cast<Constant>(Ptr)) {
  778. // Every index must be constant.
  779. size_t i, e;
  780. for (i = 0, e = IdxList.size(); i != e; ++i)
  781. if (!isa<Constant>(IdxList[i]))
  782. break;
  783. if (i == e)
  784. return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
  785. }
  786. return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
  787. }
  788. Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
  789. if (Constant *PC = dyn_cast<Constant>(Ptr))
  790. if (Constant *IC = dyn_cast<Constant>(Idx))
  791. return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
  792. return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
  793. }
  794. Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
  795. if (Constant *PC = dyn_cast<Constant>(Ptr))
  796. if (Constant *IC = dyn_cast<Constant>(Idx))
  797. return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
  798. return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
  799. }
  800. Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
  801. Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
  802. if (Constant *PC = dyn_cast<Constant>(Ptr))
  803. return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
  804. return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
  805. }
  806. Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
  807. const Twine &Name = "") {
  808. Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
  809. if (Constant *PC = dyn_cast<Constant>(Ptr))
  810. return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
  811. return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
  812. }
  813. Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
  814. const Twine &Name = "") {
  815. Value *Idxs[] = {
  816. ConstantInt::get(Type::getInt32Ty(Context), Idx0),
  817. ConstantInt::get(Type::getInt32Ty(Context), Idx1)
  818. };
  819. if (Constant *PC = dyn_cast<Constant>(Ptr))
  820. return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
  821. return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
  822. }
  823. Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
  824. const Twine &Name = "") {
  825. Value *Idxs[] = {
  826. ConstantInt::get(Type::getInt32Ty(Context), Idx0),
  827. ConstantInt::get(Type::getInt32Ty(Context), Idx1)
  828. };
  829. if (Constant *PC = dyn_cast<Constant>(Ptr))
  830. return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
  831. return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
  832. }
  833. Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
  834. Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
  835. if (Constant *PC = dyn_cast<Constant>(Ptr))
  836. return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
  837. return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
  838. }
  839. Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
  840. const Twine &Name = "") {
  841. Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
  842. if (Constant *PC = dyn_cast<Constant>(Ptr))
  843. return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
  844. return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
  845. }
  846. Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
  847. const Twine &Name = "") {
  848. Value *Idxs[] = {
  849. ConstantInt::get(Type::getInt64Ty(Context), Idx0),
  850. ConstantInt::get(Type::getInt64Ty(Context), Idx1)
  851. };
  852. if (Constant *PC = dyn_cast<Constant>(Ptr))
  853. return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
  854. return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
  855. }
  856. Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
  857. const Twine &Name = "") {
  858. Value *Idxs[] = {
  859. ConstantInt::get(Type::getInt64Ty(Context), Idx0),
  860. ConstantInt::get(Type::getInt64Ty(Context), Idx1)
  861. };
  862. if (Constant *PC = dyn_cast<Constant>(Ptr))
  863. return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
  864. return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
  865. }
  866. Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
  867. return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
  868. }
  869. /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
  870. /// with "i8*" type instead of a pointer to array of i8.
  871. Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
  872. Value *gv = CreateGlobalString(Str, Name);
  873. Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
  874. Value *Args[] = { zero, zero };
  875. return CreateInBoundsGEP(gv, Args, Name);
  876. }
  877. //===--------------------------------------------------------------------===//
  878. // Instruction creation methods: Cast/Conversion Operators
  879. //===--------------------------------------------------------------------===//
  880. Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
  881. return CreateCast(Instruction::Trunc, V, DestTy, Name);
  882. }
  883. Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
  884. return CreateCast(Instruction::ZExt, V, DestTy, Name);
  885. }
  886. Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
  887. return CreateCast(Instruction::SExt, V, DestTy, Name);
  888. }
  889. /// CreateZExtOrTrunc - Create a ZExt or Trunc from the integer value V to
  890. /// DestTy. Return the value untouched if the type of V is already DestTy.
  891. Value *CreateZExtOrTrunc(Value *V, IntegerType *DestTy,
  892. const Twine &Name = "") {
  893. assert(isa<IntegerType>(V->getType()) && "Can only zero extend integers!");
  894. IntegerType *IntTy = cast<IntegerType>(V->getType());
  895. if (IntTy->getBitWidth() < DestTy->getBitWidth())
  896. return CreateZExt(V, DestTy, Name);
  897. if (IntTy->getBitWidth() > DestTy->getBitWidth())
  898. return CreateTrunc(V, DestTy, Name);
  899. return V;
  900. }
  901. /// CreateSExtOrTrunc - Create a SExt or Trunc from the integer value V to
  902. /// DestTy. Return the value untouched if the type of V is already DestTy.
  903. Value *CreateSExtOrTrunc(Value *V, IntegerType *DestTy,
  904. const Twine &Name = "") {
  905. assert(isa<IntegerType>(V->getType()) && "Can only sign extend integers!");
  906. IntegerType *IntTy = cast<IntegerType>(V->getType());
  907. if (IntTy->getBitWidth() < DestTy->getBitWidth())
  908. return CreateSExt(V, DestTy, Name);
  909. if (IntTy->getBitWidth() > DestTy->getBitWidth())
  910. return CreateTrunc(V, DestTy, Name);
  911. return V;
  912. }
  913. Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
  914. return CreateCast(Instruction::FPToUI, V, DestTy, Name);
  915. }
  916. Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
  917. return CreateCast(Instruction::FPToSI, V, DestTy, Name);
  918. }
  919. Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
  920. return CreateCast(Instruction::UIToFP, V, DestTy, Name);
  921. }
  922. Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
  923. return CreateCast(Instruction::SIToFP, V, DestTy, Name);
  924. }
  925. Value *CreateFPTrunc(Value *V, Type *DestTy,
  926. const Twine &Name = "") {
  927. return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
  928. }
  929. Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
  930. return CreateCast(Instruction::FPExt, V, DestTy, Name);
  931. }
  932. Value *CreatePtrToInt(Value *V, Type *DestTy,
  933. const Twine &Name = "") {
  934. return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
  935. }
  936. Value *CreateIntToPtr(Value *V, Type *DestTy,
  937. const Twine &Name = "") {
  938. return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
  939. }
  940. Value *CreateBitCast(Value *V, Type *DestTy,
  941. const Twine &Name = "") {
  942. return CreateCast(Instruction::BitCast, V, DestTy, Name);
  943. }
  944. Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
  945. const Twine &Name = "") {
  946. if (V->getType() == DestTy)
  947. return V;
  948. if (Constant *VC = dyn_cast<Constant>(V))
  949. return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
  950. return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
  951. }
  952. Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
  953. const Twine &Name = "") {
  954. if (V->getType() == DestTy)
  955. return V;
  956. if (Constant *VC = dyn_cast<Constant>(V))
  957. return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
  958. return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
  959. }
  960. Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
  961. const Twine &Name = "") {
  962. if (V->getType() == DestTy)
  963. return V;
  964. if (Constant *VC = dyn_cast<Constant>(V))
  965. return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
  966. return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
  967. }
  968. Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
  969. const Twine &Name = "") {
  970. if (V->getType() == DestTy)
  971. return V;
  972. if (Constant *VC = dyn_cast<Constant>(V))
  973. return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
  974. return Insert(CastInst::Create(Op, V, DestTy), Name);
  975. }
  976. Value *CreatePointerCast(Value *V, Type *DestTy,
  977. const Twine &Name = "") {
  978. if (V->getType() == DestTy)
  979. return V;
  980. if (Constant *VC = dyn_cast<Constant>(V))
  981. return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
  982. return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
  983. }
  984. Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
  985. const Twine &Name = "") {
  986. if (V->getType() == DestTy)
  987. return V;
  988. if (Constant *VC = dyn_cast<Constant>(V))
  989. return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
  990. return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
  991. }
  992. private:
  993. // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
  994. // error, instead of converting the string to bool for the isSigned parameter.
  995. Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
  996. public:
  997. Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
  998. if (V->getType() == DestTy)
  999. return V;
  1000. if (Constant *VC = dyn_cast<Constant>(V))
  1001. return Insert(Folder.CreateFPCast(VC, DestTy), Name);
  1002. return Insert(CastInst::CreateFPCast(V, DestTy), Name);
  1003. }
  1004. //===--------------------------------------------------------------------===//
  1005. // Instruction creation methods: Compare Instructions
  1006. //===--------------------------------------------------------------------===//
  1007. Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
  1008. return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
  1009. }
  1010. Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1011. return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
  1012. }
  1013. Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1014. return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
  1015. }
  1016. Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1017. return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
  1018. }
  1019. Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1020. return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
  1021. }
  1022. Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1023. return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
  1024. }
  1025. Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1026. return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
  1027. }
  1028. Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1029. return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
  1030. }
  1031. Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1032. return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
  1033. }
  1034. Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1035. return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
  1036. }
  1037. Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
  1038. return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
  1039. }
  1040. Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1041. return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
  1042. }
  1043. Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1044. return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
  1045. }
  1046. Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1047. return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
  1048. }
  1049. Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1050. return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
  1051. }
  1052. Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1053. return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
  1054. }
  1055. Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
  1056. return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
  1057. }
  1058. Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
  1059. return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
  1060. }
  1061. Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
  1062. return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
  1063. }
  1064. Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1065. return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
  1066. }
  1067. Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1068. return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
  1069. }
  1070. Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
  1071. return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
  1072. }
  1073. Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1074. return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
  1075. }
  1076. Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
  1077. return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
  1078. }
  1079. Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1080. const Twine &Name = "") {
  1081. if (Constant *LC = dyn_cast<Constant>(LHS))
  1082. if (Constant *RC = dyn_cast<Constant>(RHS))
  1083. return Insert(Folder.CreateICmp(P, LC, RC), Name);
  1084. return Insert(new ICmpInst(P, LHS, RHS), Name);
  1085. }
  1086. Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
  1087. const Twine &Name = "") {
  1088. if (Constant *LC = dyn_cast<Constant>(LHS))
  1089. if (Constant *RC = dyn_cast<Constant>(RHS))
  1090. return Insert(Folder.CreateFCmp(P, LC, RC), Name);
  1091. return Insert(new FCmpInst(P, LHS, RHS), Name);
  1092. }
  1093. //===--------------------------------------------------------------------===//
  1094. // Instruction creation methods: Other Instructions
  1095. //===--------------------------------------------------------------------===//
  1096. PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
  1097. const Twine &Name = "") {
  1098. return Insert(PHINode::Create(Ty, NumReservedValues), Name);
  1099. }
  1100. CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
  1101. return Insert(CallInst::Create(Callee), Name);
  1102. }
  1103. CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
  1104. return Insert(CallInst::Create(Callee, Arg), Name);
  1105. }
  1106. CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
  1107. const Twine &Name = "") {
  1108. Value *Args[] = { Arg1, Arg2 };
  1109. return Insert(CallInst::Create(Callee, Args), Name);
  1110. }
  1111. CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
  1112. const Twine &Name = "") {
  1113. Value *Args[] = { Arg1, Arg2, Arg3 };
  1114. return Insert(CallInst::Create(Callee, Args), Name);
  1115. }
  1116. CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
  1117. Value *Arg4, const Twine &Name = "") {
  1118. Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
  1119. return Insert(CallInst::Create(Callee, Args), Name);
  1120. }
  1121. CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
  1122. Value *Arg4, Value *Arg5, const Twine &Name = "") {
  1123. Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
  1124. return Insert(CallInst::Create(Callee, Args), Name);
  1125. }
  1126. CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
  1127. const Twine &Name = "") {
  1128. return Insert(CallInst::Create(Callee, Args), Name);
  1129. }
  1130. Value *CreateSelect(Value *C, Value *True, Value *False,
  1131. const Twine &Name = "") {
  1132. if (Constant *CC = dyn_cast<Constant>(C))
  1133. if (Constant *TC = dyn_cast<Constant>(True))
  1134. if (Constant *FC = dyn_cast<Constant>(False))
  1135. return Insert(Folder.CreateSelect(CC, TC, FC), Name);
  1136. return Insert(SelectInst::Create(C, True, False), Name);
  1137. }
  1138. VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
  1139. return Insert(new VAArgInst(List, Ty), Name);
  1140. }
  1141. Value *CreateExtractElement(Value *Vec, Value *Idx,
  1142. const Twine &Name = "") {
  1143. if (Constant *VC = dyn_cast<Constant>(Vec))
  1144. if (Constant *IC = dyn_cast<Constant>(Idx))
  1145. return Insert(Folder.CreateExtractElement(VC, IC), Name);
  1146. return Insert(ExtractElementInst::Create(Vec, Idx), Name);
  1147. }
  1148. Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
  1149. const Twine &Name = "") {
  1150. if (Constant *VC = dyn_cast<Constant>(Vec))
  1151. if (Constant *NC = dyn_cast<Constant>(NewElt))
  1152. if (Constant *IC = dyn_cast<Constant>(Idx))
  1153. return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
  1154. return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
  1155. }
  1156. Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
  1157. const Twine &Name = "") {
  1158. if (Constant *V1C = dyn_cast<Constant>(V1))
  1159. if (Constant *V2C = dyn_cast<Constant>(V2))
  1160. if (Constant *MC = dyn_cast<Constant>(Mask))
  1161. return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
  1162. return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
  1163. }
  1164. Value *CreateExtractValue(Value *Agg,
  1165. ArrayRef<unsigned> Idxs,
  1166. const Twine &Name = "") {
  1167. if (Constant *AggC = dyn_cast<Constant>(Agg))
  1168. return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
  1169. return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
  1170. }
  1171. Value *CreateInsertValue(Value *Agg, Value *Val,
  1172. ArrayRef<unsigned> Idxs,
  1173. const Twine &Name = "") {
  1174. if (Constant *AggC = dyn_cast<Constant>(Agg))
  1175. if (Constant *ValC = dyn_cast<Constant>(Val))
  1176. return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
  1177. return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
  1178. }
  1179. LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
  1180. const Twine &Name = "") {
  1181. return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses, Name));
  1182. }
  1183. //===--------------------------------------------------------------------===//
  1184. // Utility creation methods
  1185. //===--------------------------------------------------------------------===//
  1186. /// CreateIsNull - Return an i1 value testing if \p Arg is null.
  1187. Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
  1188. return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
  1189. Name);
  1190. }
  1191. /// CreateIsNotNull - Return an i1 value testing if \p Arg is not null.
  1192. Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
  1193. return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
  1194. Name);
  1195. }
  1196. /// CreatePtrDiff - Return the i64 difference between two pointer values,
  1197. /// dividing out the size of the pointed-to objects. This is intended to
  1198. /// implement C-style pointer subtraction. As such, the pointers must be
  1199. /// appropriately aligned for their element types and pointing into the
  1200. /// same object.
  1201. Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
  1202. assert(LHS->getType() == RHS->getType() &&
  1203. "Pointer subtraction operand types must match!");
  1204. PointerType *ArgType = cast<PointerType>(LHS->getType());
  1205. Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
  1206. Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
  1207. Value *Difference = CreateSub(LHS_int, RHS_int);
  1208. return CreateExactSDiv(Difference,
  1209. ConstantExpr::getSizeOf(ArgType->getElementType()),
  1210. Name);
  1211. }
  1212. };
  1213. }
  1214. #endif