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.

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