1 //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_IRBUILDER_H
16 #define LLVM_IR_IRBUILDER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Operator.h"
26 #include "llvm/Support/CBindingWrapping.h"
27 #include "llvm/Support/ConstantFolder.h"
28 #include "llvm/Support/ValueHandle.h"
33 /// \brief This provides the default implementation of the IRBuilder
34 /// 'InsertHelper' method that is called whenever an instruction is created by
35 /// IRBuilder and needs to be inserted.
37 /// By default, this inserts the instruction at the insertion point.
38 template <bool preserveNames = true>
39 class IRBuilderDefaultInserter {
41 void InsertHelper(Instruction *I, const Twine &Name,
42 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
43 if (BB) BB->getInstList().insert(InsertPt, I);
49 /// \brief Common base class shared among various IRBuilders.
51 DebugLoc CurDbgLocation;
54 BasicBlock::iterator InsertPt;
57 MDNode *DefaultFPMathTag;
61 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = 0)
62 : Context(context), DefaultFPMathTag(FPMathTag), FMF() {
63 ClearInsertionPoint();
66 //===--------------------------------------------------------------------===//
67 // Builder configuration methods
68 //===--------------------------------------------------------------------===//
70 /// \brief Clear the insertion point: created instructions will not be
71 /// inserted into a block.
72 void ClearInsertionPoint() {
77 BasicBlock *GetInsertBlock() const { return BB; }
78 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
79 LLVMContext &getContext() const { return Context; }
81 /// \brief This specifies that created instructions should be appended to the
82 /// end of the specified block.
83 void SetInsertPoint(BasicBlock *TheBB) {
88 /// \brief This specifies that created instructions should be inserted before
89 /// the specified instruction.
90 void SetInsertPoint(Instruction *I) {
93 assert(I != BB->end() && "Can't read debug loc from end()");
94 SetCurrentDebugLocation(I->getDebugLoc());
97 /// \brief This specifies that created instructions should be inserted at the
99 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
104 /// \brief Find the nearest point that dominates this use, and specify that
105 /// created instructions should be inserted at this point.
106 void SetInsertPoint(Use &U) {
107 Instruction *UseInst = cast<Instruction>(U.getUser());
108 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
109 BasicBlock *PredBB = Phi->getIncomingBlock(U);
110 assert(U != PredBB->getTerminator() && "critical edge not split");
111 SetInsertPoint(PredBB, PredBB->getTerminator());
114 SetInsertPoint(UseInst);
117 /// \brief Set location information used by debugging information.
118 void SetCurrentDebugLocation(const DebugLoc &L) {
122 /// \brief Get location information used by debugging information.
123 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
125 /// \brief If this builder has a current debug location, set it on the
126 /// specified instruction.
127 void SetInstDebugLocation(Instruction *I) const {
128 if (!CurDbgLocation.isUnknown())
129 I->setDebugLoc(CurDbgLocation);
132 /// \brief Get the return type of the current function that we're emitting
134 Type *getCurrentFunctionReturnType() const;
136 /// InsertPoint - A saved insertion point.
139 BasicBlock::iterator Point;
142 /// \brief Creates a new insertion point which doesn't point to anything.
143 InsertPoint() : Block(0) {}
145 /// \brief Creates a new insertion point at the given location.
146 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
147 : Block(InsertBlock), Point(InsertPoint) {}
149 /// \brief Returns true if this insert point is set.
150 bool isSet() const { return (Block != 0); }
152 llvm::BasicBlock *getBlock() const { return Block; }
153 llvm::BasicBlock::iterator getPoint() const { return Point; }
156 /// \brief Returns the current insert point.
157 InsertPoint saveIP() const {
158 return InsertPoint(GetInsertBlock(), GetInsertPoint());
161 /// \brief Returns the current insert point, clearing it in the process.
162 InsertPoint saveAndClearIP() {
163 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
164 ClearInsertionPoint();
168 /// \brief Sets the current insert point to a previously-saved location.
169 void restoreIP(InsertPoint IP) {
171 SetInsertPoint(IP.getBlock(), IP.getPoint());
173 ClearInsertionPoint();
176 /// \brief Get the floating point math metadata being used.
177 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
179 /// \brief Get the flags to be applied to created floating point ops
180 FastMathFlags getFastMathFlags() const { return FMF; }
182 /// \brief Clear the fast-math flags.
183 void clearFastMathFlags() { FMF.clear(); }
185 /// \brief Set the floating point math metadata to be used.
186 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
188 /// \brief Set the fast-math flags to be used with generated fp-math operators
189 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
191 //===--------------------------------------------------------------------===//
193 //===--------------------------------------------------------------------===//
195 // \brief RAII object that stores the current insertion point and restores it
196 // when the object is destroyed. This includes the debug location.
197 class InsertPointGuard {
198 IRBuilderBase &Builder;
199 AssertingVH<BasicBlock> Block;
200 BasicBlock::iterator Point;
203 InsertPointGuard(const InsertPointGuard &) LLVM_DELETED_FUNCTION;
204 InsertPointGuard &operator=(const InsertPointGuard &) LLVM_DELETED_FUNCTION;
207 InsertPointGuard(IRBuilderBase &B)
208 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
209 DbgLoc(B.getCurrentDebugLocation()) {}
211 ~InsertPointGuard() {
212 Builder.restoreIP(InsertPoint(Block, Point));
213 Builder.SetCurrentDebugLocation(DbgLoc);
217 // \brief RAII object that stores the current fast math settings and restores
218 // them when the object is destroyed.
219 class FastMathFlagGuard {
220 IRBuilderBase &Builder;
224 FastMathFlagGuard(const FastMathFlagGuard &) LLVM_DELETED_FUNCTION;
225 FastMathFlagGuard &operator=(
226 const FastMathFlagGuard &) LLVM_DELETED_FUNCTION;
229 FastMathFlagGuard(IRBuilderBase &B)
230 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
232 ~FastMathFlagGuard() {
234 Builder.DefaultFPMathTag = FPMathTag;
238 //===--------------------------------------------------------------------===//
239 // Miscellaneous creation methods.
240 //===--------------------------------------------------------------------===//
242 /// \brief Make a new global variable with initializer type i8*
244 /// Make a new global variable with an initializer that has array of i8 type
245 /// filled in with the null terminated string value specified. The new global
246 /// variable will be marked mergable with any others of the same contents. If
247 /// Name is specified, it is the name of the global variable created.
248 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
250 /// \brief Get a constant value representing either true or false.
251 ConstantInt *getInt1(bool V) {
252 return ConstantInt::get(getInt1Ty(), V);
255 /// \brief Get the constant value for i1 true.
256 ConstantInt *getTrue() {
257 return ConstantInt::getTrue(Context);
260 /// \brief Get the constant value for i1 false.
261 ConstantInt *getFalse() {
262 return ConstantInt::getFalse(Context);
265 /// \brief Get a constant 8-bit value.
266 ConstantInt *getInt8(uint8_t C) {
267 return ConstantInt::get(getInt8Ty(), C);
270 /// \brief Get a constant 16-bit value.
271 ConstantInt *getInt16(uint16_t C) {
272 return ConstantInt::get(getInt16Ty(), C);
275 /// \brief Get a constant 32-bit value.
276 ConstantInt *getInt32(uint32_t C) {
277 return ConstantInt::get(getInt32Ty(), C);
280 /// \brief Get a constant 64-bit value.
281 ConstantInt *getInt64(uint64_t C) {
282 return ConstantInt::get(getInt64Ty(), C);
285 /// \brief Get a constant integer value.
286 ConstantInt *getInt(const APInt &AI) {
287 return ConstantInt::get(Context, AI);
290 //===--------------------------------------------------------------------===//
291 // Type creation methods
292 //===--------------------------------------------------------------------===//
294 /// \brief Fetch the type representing a single bit
295 IntegerType *getInt1Ty() {
296 return Type::getInt1Ty(Context);
299 /// \brief Fetch the type representing an 8-bit integer.
300 IntegerType *getInt8Ty() {
301 return Type::getInt8Ty(Context);
304 /// \brief Fetch the type representing a 16-bit integer.
305 IntegerType *getInt16Ty() {
306 return Type::getInt16Ty(Context);
309 /// \brief Fetch the type representing a 32-bit integer.
310 IntegerType *getInt32Ty() {
311 return Type::getInt32Ty(Context);
314 /// \brief Fetch the type representing a 64-bit integer.
315 IntegerType *getInt64Ty() {
316 return Type::getInt64Ty(Context);
319 /// \brief Fetch the type representing a 32-bit floating point value.
321 return Type::getFloatTy(Context);
324 /// \brief Fetch the type representing a 64-bit floating point value.
325 Type *getDoubleTy() {
326 return Type::getDoubleTy(Context);
329 /// \brief Fetch the type representing void.
331 return Type::getVoidTy(Context);
334 /// \brief Fetch the type representing a pointer to an 8-bit integer value.
335 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
336 return Type::getInt8PtrTy(Context, AddrSpace);
339 /// \brief Fetch the type representing a pointer to an integer value.
340 IntegerType* getIntPtrTy(const DataLayout *DL, unsigned AddrSpace = 0) {
341 return DL->getIntPtrType(Context, AddrSpace);
344 //===--------------------------------------------------------------------===//
345 // Intrinsic creation methods
346 //===--------------------------------------------------------------------===//
348 /// \brief Create and insert a memset to the specified pointer and the
351 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
352 /// specified, it will be added to the instruction.
353 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
354 bool isVolatile = false, MDNode *TBAATag = 0) {
355 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
358 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
359 bool isVolatile = false, MDNode *TBAATag = 0);
361 /// \brief Create and insert a memcpy between the specified pointers.
363 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
364 /// specified, it will be added to the instruction.
365 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
366 bool isVolatile = false, MDNode *TBAATag = 0,
367 MDNode *TBAAStructTag = 0) {
368 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
372 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
373 bool isVolatile = false, MDNode *TBAATag = 0,
374 MDNode *TBAAStructTag = 0);
376 /// \brief Create and insert a memmove between the specified
379 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
380 /// specified, it will be added to the instruction.
381 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
382 bool isVolatile = false, MDNode *TBAATag = 0) {
383 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
386 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
387 bool isVolatile = false, MDNode *TBAATag = 0);
389 /// \brief Create a lifetime.start intrinsic.
391 /// If the pointer isn't i8* it will be converted.
392 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
394 /// \brief Create a lifetime.end intrinsic.
396 /// If the pointer isn't i8* it will be converted.
397 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
400 Value *getCastedInt8PtrValue(Value *Ptr);
403 /// \brief This provides a uniform API for creating instructions and inserting
404 /// them into a basic block: either at the end of a BasicBlock, or at a specific
405 /// iterator location in a block.
407 /// Note that the builder does not expose the full generality of LLVM
408 /// instructions. For access to extra instruction properties, use the mutators
409 /// (e.g. setVolatile) on the instructions after they have been
410 /// created. Convenience state exists to specify fast-math flags and fp-math
413 /// The first template argument handles whether or not to preserve names in the
414 /// final instruction output. This defaults to on. The second template argument
415 /// specifies a class to use for creating constants. This defaults to creating
416 /// minimally folded constants. The fourth template argument allows clients to
417 /// specify custom insertion hooks that are called on every newly created
419 template<bool preserveNames = true, typename T = ConstantFolder,
420 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
421 class IRBuilder : public IRBuilderBase, public Inserter {
424 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
425 MDNode *FPMathTag = 0)
426 : IRBuilderBase(C, FPMathTag), Inserter(I), Folder(F) {
429 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0)
430 : IRBuilderBase(C, FPMathTag), Folder() {
433 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
434 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
435 SetInsertPoint(TheBB);
438 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
439 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
440 SetInsertPoint(TheBB);
443 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
444 : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
446 SetCurrentDebugLocation(IP->getDebugLoc());
449 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
450 : IRBuilderBase(U->getContext(), FPMathTag), Folder() {
452 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
455 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
456 MDNode *FPMathTag = 0)
457 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
458 SetInsertPoint(TheBB, IP);
461 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
462 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
463 SetInsertPoint(TheBB, IP);
466 /// \brief Get the constant folder being used.
467 const T &getFolder() { return Folder; }
469 /// \brief Return true if this builder is configured to actually add the
470 /// requested names to IR created through it.
471 bool isNamePreserving() const { return preserveNames; }
473 /// \brief Insert and return the specified instruction.
474 template<typename InstTy>
475 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
476 this->InsertHelper(I, Name, BB, InsertPt);
477 this->SetInstDebugLocation(I);
481 /// \brief No-op overload to handle constants.
482 Constant *Insert(Constant *C, const Twine& = "") const {
486 //===--------------------------------------------------------------------===//
487 // Instruction creation methods: Terminators
488 //===--------------------------------------------------------------------===//
491 /// \brief Helper to add branch weight metadata onto an instruction.
492 /// \returns The annotated instruction.
493 template <typename InstTy>
494 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
496 I->setMetadata(LLVMContext::MD_prof, Weights);
501 /// \brief Create a 'ret void' instruction.
502 ReturnInst *CreateRetVoid() {
503 return Insert(ReturnInst::Create(Context));
506 /// \brief Create a 'ret <val>' instruction.
507 ReturnInst *CreateRet(Value *V) {
508 return Insert(ReturnInst::Create(Context, V));
511 /// \brief Create a sequence of N insertvalue instructions,
512 /// with one Value from the retVals array each, that build a aggregate
513 /// return value one value at a time, and a ret instruction to return
514 /// the resulting aggregate value.
516 /// This is a convenience function for code that uses aggregate return values
517 /// as a vehicle for having multiple return values.
518 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
519 Value *V = UndefValue::get(getCurrentFunctionReturnType());
520 for (unsigned i = 0; i != N; ++i)
521 V = CreateInsertValue(V, retVals[i], i, "mrv");
522 return Insert(ReturnInst::Create(Context, V));
525 /// \brief Create an unconditional 'br label X' instruction.
526 BranchInst *CreateBr(BasicBlock *Dest) {
527 return Insert(BranchInst::Create(Dest));
530 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
532 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
533 MDNode *BranchWeights = 0) {
534 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
538 /// \brief Create a switch instruction with the specified value, default dest,
539 /// and with a hint for the number of cases that will be added (for efficient
541 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
542 MDNode *BranchWeights = 0) {
543 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
547 /// \brief Create an indirect branch instruction with the specified address
548 /// operand, with an optional hint for the number of destinations that will be
549 /// added (for efficient allocation).
550 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
551 return Insert(IndirectBrInst::Create(Addr, NumDests));
554 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
555 BasicBlock *UnwindDest, const Twine &Name = "") {
556 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
557 ArrayRef<Value *>()),
560 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
561 BasicBlock *UnwindDest, Value *Arg1,
562 const Twine &Name = "") {
563 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
566 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
567 BasicBlock *UnwindDest, Value *Arg1,
568 Value *Arg2, Value *Arg3,
569 const Twine &Name = "") {
570 Value *Args[] = { Arg1, Arg2, Arg3 };
571 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
574 /// \brief Create an invoke instruction.
575 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
576 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
577 const Twine &Name = "") {
578 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
582 ResumeInst *CreateResume(Value *Exn) {
583 return Insert(ResumeInst::Create(Exn));
586 UnreachableInst *CreateUnreachable() {
587 return Insert(new UnreachableInst(Context));
590 //===--------------------------------------------------------------------===//
591 // Instruction creation methods: Binary Operators
592 //===--------------------------------------------------------------------===//
594 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
595 Value *LHS, Value *RHS,
597 bool HasNUW, bool HasNSW) {
598 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
599 if (HasNUW) BO->setHasNoUnsignedWrap();
600 if (HasNSW) BO->setHasNoSignedWrap();
604 Instruction *AddFPMathAttributes(Instruction *I,
606 FastMathFlags FMF) const {
608 FPMathTag = DefaultFPMathTag;
610 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
611 I->setFastMathFlags(FMF);
615 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
616 bool HasNUW = false, bool HasNSW = false) {
617 if (Constant *LC = dyn_cast<Constant>(LHS))
618 if (Constant *RC = dyn_cast<Constant>(RHS))
619 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
620 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
623 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
624 return CreateAdd(LHS, RHS, Name, false, true);
626 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
627 return CreateAdd(LHS, RHS, Name, true, false);
629 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
630 MDNode *FPMathTag = 0) {
631 if (Constant *LC = dyn_cast<Constant>(LHS))
632 if (Constant *RC = dyn_cast<Constant>(RHS))
633 return Insert(Folder.CreateFAdd(LC, RC), Name);
634 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
635 FPMathTag, FMF), Name);
637 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
638 bool HasNUW = false, bool HasNSW = false) {
639 if (Constant *LC = dyn_cast<Constant>(LHS))
640 if (Constant *RC = dyn_cast<Constant>(RHS))
641 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
642 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
645 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
646 return CreateSub(LHS, RHS, Name, false, true);
648 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
649 return CreateSub(LHS, RHS, Name, true, false);
651 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
652 MDNode *FPMathTag = 0) {
653 if (Constant *LC = dyn_cast<Constant>(LHS))
654 if (Constant *RC = dyn_cast<Constant>(RHS))
655 return Insert(Folder.CreateFSub(LC, RC), Name);
656 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
657 FPMathTag, FMF), Name);
659 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
660 bool HasNUW = false, bool HasNSW = false) {
661 if (Constant *LC = dyn_cast<Constant>(LHS))
662 if (Constant *RC = dyn_cast<Constant>(RHS))
663 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
664 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
667 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
668 return CreateMul(LHS, RHS, Name, false, true);
670 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
671 return CreateMul(LHS, RHS, Name, true, false);
673 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
674 MDNode *FPMathTag = 0) {
675 if (Constant *LC = dyn_cast<Constant>(LHS))
676 if (Constant *RC = dyn_cast<Constant>(RHS))
677 return Insert(Folder.CreateFMul(LC, RC), Name);
678 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
679 FPMathTag, FMF), Name);
681 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
682 bool isExact = false) {
683 if (Constant *LC = dyn_cast<Constant>(LHS))
684 if (Constant *RC = dyn_cast<Constant>(RHS))
685 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
687 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
688 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
690 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
691 return CreateUDiv(LHS, RHS, Name, true);
693 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
694 bool isExact = false) {
695 if (Constant *LC = dyn_cast<Constant>(LHS))
696 if (Constant *RC = dyn_cast<Constant>(RHS))
697 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
699 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
700 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
702 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
703 return CreateSDiv(LHS, RHS, Name, true);
705 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
706 MDNode *FPMathTag = 0) {
707 if (Constant *LC = dyn_cast<Constant>(LHS))
708 if (Constant *RC = dyn_cast<Constant>(RHS))
709 return Insert(Folder.CreateFDiv(LC, RC), Name);
710 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
711 FPMathTag, FMF), Name);
713 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
714 if (Constant *LC = dyn_cast<Constant>(LHS))
715 if (Constant *RC = dyn_cast<Constant>(RHS))
716 return Insert(Folder.CreateURem(LC, RC), Name);
717 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
719 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
720 if (Constant *LC = dyn_cast<Constant>(LHS))
721 if (Constant *RC = dyn_cast<Constant>(RHS))
722 return Insert(Folder.CreateSRem(LC, RC), Name);
723 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
725 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
726 MDNode *FPMathTag = 0) {
727 if (Constant *LC = dyn_cast<Constant>(LHS))
728 if (Constant *RC = dyn_cast<Constant>(RHS))
729 return Insert(Folder.CreateFRem(LC, RC), Name);
730 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
731 FPMathTag, FMF), Name);
734 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
735 bool HasNUW = false, bool HasNSW = false) {
736 if (Constant *LC = dyn_cast<Constant>(LHS))
737 if (Constant *RC = dyn_cast<Constant>(RHS))
738 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
739 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
742 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
743 bool HasNUW = false, bool HasNSW = false) {
744 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
747 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
748 bool HasNUW = false, bool HasNSW = false) {
749 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
753 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
754 bool isExact = false) {
755 if (Constant *LC = dyn_cast<Constant>(LHS))
756 if (Constant *RC = dyn_cast<Constant>(RHS))
757 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
759 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
760 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
762 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
763 bool isExact = false) {
764 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
766 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
767 bool isExact = false) {
768 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
771 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
772 bool isExact = false) {
773 if (Constant *LC = dyn_cast<Constant>(LHS))
774 if (Constant *RC = dyn_cast<Constant>(RHS))
775 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
777 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
778 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
780 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
781 bool isExact = false) {
782 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
784 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
785 bool isExact = false) {
786 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
789 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
790 if (Constant *RC = dyn_cast<Constant>(RHS)) {
791 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
792 return LHS; // LHS & -1 -> LHS
793 if (Constant *LC = dyn_cast<Constant>(LHS))
794 return Insert(Folder.CreateAnd(LC, RC), Name);
796 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
798 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
799 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
801 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
802 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
805 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
806 if (Constant *RC = dyn_cast<Constant>(RHS)) {
807 if (RC->isNullValue())
808 return LHS; // LHS | 0 -> LHS
809 if (Constant *LC = dyn_cast<Constant>(LHS))
810 return Insert(Folder.CreateOr(LC, RC), Name);
812 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
814 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
815 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
817 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
818 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
821 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
822 if (Constant *LC = dyn_cast<Constant>(LHS))
823 if (Constant *RC = dyn_cast<Constant>(RHS))
824 return Insert(Folder.CreateXor(LC, RC), Name);
825 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
827 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
828 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
830 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
831 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
834 Value *CreateBinOp(Instruction::BinaryOps Opc,
835 Value *LHS, Value *RHS, const Twine &Name = "",
836 MDNode *FPMathTag = 0) {
837 if (Constant *LC = dyn_cast<Constant>(LHS))
838 if (Constant *RC = dyn_cast<Constant>(RHS))
839 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
840 llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
841 if (isa<FPMathOperator>(BinOp))
842 BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
843 return Insert(BinOp, Name);
846 Value *CreateNeg(Value *V, const Twine &Name = "",
847 bool HasNUW = false, bool HasNSW = false) {
848 if (Constant *VC = dyn_cast<Constant>(V))
849 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
850 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
851 if (HasNUW) BO->setHasNoUnsignedWrap();
852 if (HasNSW) BO->setHasNoSignedWrap();
855 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
856 return CreateNeg(V, Name, false, true);
858 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
859 return CreateNeg(V, Name, true, false);
861 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
862 if (Constant *VC = dyn_cast<Constant>(V))
863 return Insert(Folder.CreateFNeg(VC), Name);
864 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
865 FPMathTag, FMF), Name);
867 Value *CreateNot(Value *V, const Twine &Name = "") {
868 if (Constant *VC = dyn_cast<Constant>(V))
869 return Insert(Folder.CreateNot(VC), Name);
870 return Insert(BinaryOperator::CreateNot(V), Name);
873 //===--------------------------------------------------------------------===//
874 // Instruction creation methods: Memory Instructions
875 //===--------------------------------------------------------------------===//
877 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
878 const Twine &Name = "") {
879 return Insert(new AllocaInst(Ty, ArraySize), Name);
881 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
882 // converting the string to 'bool' for the isVolatile parameter.
883 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
884 return Insert(new LoadInst(Ptr), Name);
886 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
887 return Insert(new LoadInst(Ptr), Name);
889 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
890 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
892 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
893 return Insert(new StoreInst(Val, Ptr, isVolatile));
895 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
896 // correctly, instead of converting the string to 'bool' for the isVolatile
898 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
899 LoadInst *LI = CreateLoad(Ptr, Name);
900 LI->setAlignment(Align);
903 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
904 const Twine &Name = "") {
905 LoadInst *LI = CreateLoad(Ptr, Name);
906 LI->setAlignment(Align);
909 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
910 const Twine &Name = "") {
911 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
912 LI->setAlignment(Align);
915 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
916 bool isVolatile = false) {
917 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
918 SI->setAlignment(Align);
921 FenceInst *CreateFence(AtomicOrdering Ordering,
922 SynchronizationScope SynchScope = CrossThread,
923 const Twine &Name = "") {
924 return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
926 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
927 AtomicOrdering Ordering,
928 SynchronizationScope SynchScope = CrossThread) {
929 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
931 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
932 AtomicOrdering Ordering,
933 SynchronizationScope SynchScope = CrossThread) {
934 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
936 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
937 const Twine &Name = "") {
938 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
939 // Every index must be constant.
941 for (i = 0, e = IdxList.size(); i != e; ++i)
942 if (!isa<Constant>(IdxList[i]))
945 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
947 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
949 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
950 const Twine &Name = "") {
951 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
952 // Every index must be constant.
954 for (i = 0, e = IdxList.size(); i != e; ++i)
955 if (!isa<Constant>(IdxList[i]))
958 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
960 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
962 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
963 if (Constant *PC = dyn_cast<Constant>(Ptr))
964 if (Constant *IC = dyn_cast<Constant>(Idx))
965 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
966 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
968 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
969 if (Constant *PC = dyn_cast<Constant>(Ptr))
970 if (Constant *IC = dyn_cast<Constant>(Idx))
971 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
972 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
974 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
975 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
977 if (Constant *PC = dyn_cast<Constant>(Ptr))
978 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
980 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
982 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
983 const Twine &Name = "") {
984 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
986 if (Constant *PC = dyn_cast<Constant>(Ptr))
987 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
989 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
991 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
992 const Twine &Name = "") {
994 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
995 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
998 if (Constant *PC = dyn_cast<Constant>(Ptr))
999 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
1001 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
1003 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1004 const Twine &Name = "") {
1006 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1007 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1010 if (Constant *PC = dyn_cast<Constant>(Ptr))
1011 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1013 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1015 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1016 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1018 if (Constant *PC = dyn_cast<Constant>(Ptr))
1019 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
1021 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
1023 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1024 const Twine &Name = "") {
1025 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1027 if (Constant *PC = dyn_cast<Constant>(Ptr))
1028 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
1030 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1032 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1033 const Twine &Name = "") {
1035 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1036 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1039 if (Constant *PC = dyn_cast<Constant>(Ptr))
1040 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
1042 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
1044 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1045 const Twine &Name = "") {
1047 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1048 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1051 if (Constant *PC = dyn_cast<Constant>(Ptr))
1052 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1054 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1056 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1057 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
1060 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1061 /// instead of a pointer to array of i8.
1062 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
1063 Value *gv = CreateGlobalString(Str, Name);
1064 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1065 Value *Args[] = { zero, zero };
1066 return CreateInBoundsGEP(gv, Args, Name);
1069 //===--------------------------------------------------------------------===//
1070 // Instruction creation methods: Cast/Conversion Operators
1071 //===--------------------------------------------------------------------===//
1073 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1074 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1076 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1077 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1079 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1080 return CreateCast(Instruction::SExt, V, DestTy, Name);
1082 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1083 /// the value untouched if the type of V is already DestTy.
1084 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1085 const Twine &Name = "") {
1086 assert(V->getType()->isIntOrIntVectorTy() &&
1087 DestTy->isIntOrIntVectorTy() &&
1088 "Can only zero extend/truncate integers!");
1089 Type *VTy = V->getType();
1090 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1091 return CreateZExt(V, DestTy, Name);
1092 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1093 return CreateTrunc(V, DestTy, Name);
1096 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1097 /// the value untouched if the type of V is already DestTy.
1098 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1099 const Twine &Name = "") {
1100 assert(V->getType()->isIntOrIntVectorTy() &&
1101 DestTy->isIntOrIntVectorTy() &&
1102 "Can only sign extend/truncate integers!");
1103 Type *VTy = V->getType();
1104 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1105 return CreateSExt(V, DestTy, Name);
1106 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1107 return CreateTrunc(V, DestTy, Name);
1110 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1111 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1113 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1114 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1116 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1117 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1119 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1120 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1122 Value *CreateFPTrunc(Value *V, Type *DestTy,
1123 const Twine &Name = "") {
1124 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1126 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1127 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1129 Value *CreatePtrToInt(Value *V, Type *DestTy,
1130 const Twine &Name = "") {
1131 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1133 Value *CreateIntToPtr(Value *V, Type *DestTy,
1134 const Twine &Name = "") {
1135 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1137 Value *CreateBitCast(Value *V, Type *DestTy,
1138 const Twine &Name = "") {
1139 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1141 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1142 const Twine &Name = "") {
1143 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1145 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1146 const Twine &Name = "") {
1147 if (V->getType() == DestTy)
1149 if (Constant *VC = dyn_cast<Constant>(V))
1150 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1151 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1153 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1154 const Twine &Name = "") {
1155 if (V->getType() == DestTy)
1157 if (Constant *VC = dyn_cast<Constant>(V))
1158 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1159 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1161 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1162 const Twine &Name = "") {
1163 if (V->getType() == DestTy)
1165 if (Constant *VC = dyn_cast<Constant>(V))
1166 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1167 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1169 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1170 const Twine &Name = "") {
1171 if (V->getType() == DestTy)
1173 if (Constant *VC = dyn_cast<Constant>(V))
1174 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1175 return Insert(CastInst::Create(Op, V, DestTy), Name);
1177 Value *CreatePointerCast(Value *V, Type *DestTy,
1178 const Twine &Name = "") {
1179 if (V->getType() == DestTy)
1181 if (Constant *VC = dyn_cast<Constant>(V))
1182 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1183 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1185 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1186 const Twine &Name = "") {
1187 if (V->getType() == DestTy)
1189 if (Constant *VC = dyn_cast<Constant>(V))
1190 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1191 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1194 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1195 // compile time error, instead of converting the string to bool for the
1196 // isSigned parameter.
1197 Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
1199 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1200 if (V->getType() == DestTy)
1202 if (Constant *VC = dyn_cast<Constant>(V))
1203 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1204 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1207 //===--------------------------------------------------------------------===//
1208 // Instruction creation methods: Compare Instructions
1209 //===--------------------------------------------------------------------===//
1211 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1212 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1214 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1215 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1217 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1218 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1220 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1221 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1223 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1224 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1226 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1227 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1229 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1230 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1232 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1233 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1235 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1236 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1238 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1239 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1242 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1243 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1245 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1246 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1248 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1249 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1251 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1252 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1254 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1255 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1257 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1258 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1260 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1261 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1263 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1264 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1266 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1267 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1269 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1270 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1272 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1273 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1275 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1276 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1278 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1279 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1281 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1282 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1285 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1286 const Twine &Name = "") {
1287 if (Constant *LC = dyn_cast<Constant>(LHS))
1288 if (Constant *RC = dyn_cast<Constant>(RHS))
1289 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1290 return Insert(new ICmpInst(P, LHS, RHS), Name);
1292 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1293 const Twine &Name = "") {
1294 if (Constant *LC = dyn_cast<Constant>(LHS))
1295 if (Constant *RC = dyn_cast<Constant>(RHS))
1296 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1297 return Insert(new FCmpInst(P, LHS, RHS), Name);
1300 //===--------------------------------------------------------------------===//
1301 // Instruction creation methods: Other Instructions
1302 //===--------------------------------------------------------------------===//
1304 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1305 const Twine &Name = "") {
1306 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1309 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1310 return Insert(CallInst::Create(Callee), Name);
1312 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1313 return Insert(CallInst::Create(Callee, Arg), Name);
1315 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1316 const Twine &Name = "") {
1317 Value *Args[] = { Arg1, Arg2 };
1318 return Insert(CallInst::Create(Callee, Args), Name);
1320 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1321 const Twine &Name = "") {
1322 Value *Args[] = { Arg1, Arg2, Arg3 };
1323 return Insert(CallInst::Create(Callee, Args), Name);
1325 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1326 Value *Arg4, const Twine &Name = "") {
1327 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1328 return Insert(CallInst::Create(Callee, Args), Name);
1330 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1331 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1332 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1333 return Insert(CallInst::Create(Callee, Args), Name);
1336 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1337 const Twine &Name = "") {
1338 return Insert(CallInst::Create(Callee, Args), Name);
1341 Value *CreateSelect(Value *C, Value *True, Value *False,
1342 const Twine &Name = "") {
1343 if (Constant *CC = dyn_cast<Constant>(C))
1344 if (Constant *TC = dyn_cast<Constant>(True))
1345 if (Constant *FC = dyn_cast<Constant>(False))
1346 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1347 return Insert(SelectInst::Create(C, True, False), Name);
1350 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1351 return Insert(new VAArgInst(List, Ty), Name);
1354 Value *CreateExtractElement(Value *Vec, Value *Idx,
1355 const Twine &Name = "") {
1356 if (Constant *VC = dyn_cast<Constant>(Vec))
1357 if (Constant *IC = dyn_cast<Constant>(Idx))
1358 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1359 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1362 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1363 const Twine &Name = "") {
1364 if (Constant *VC = dyn_cast<Constant>(Vec))
1365 if (Constant *NC = dyn_cast<Constant>(NewElt))
1366 if (Constant *IC = dyn_cast<Constant>(Idx))
1367 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1368 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1371 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1372 const Twine &Name = "") {
1373 if (Constant *V1C = dyn_cast<Constant>(V1))
1374 if (Constant *V2C = dyn_cast<Constant>(V2))
1375 if (Constant *MC = dyn_cast<Constant>(Mask))
1376 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1377 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1380 Value *CreateExtractValue(Value *Agg,
1381 ArrayRef<unsigned> Idxs,
1382 const Twine &Name = "") {
1383 if (Constant *AggC = dyn_cast<Constant>(Agg))
1384 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1385 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1388 Value *CreateInsertValue(Value *Agg, Value *Val,
1389 ArrayRef<unsigned> Idxs,
1390 const Twine &Name = "") {
1391 if (Constant *AggC = dyn_cast<Constant>(Agg))
1392 if (Constant *ValC = dyn_cast<Constant>(Val))
1393 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1394 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1397 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1398 const Twine &Name = "") {
1399 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
1402 //===--------------------------------------------------------------------===//
1403 // Utility creation methods
1404 //===--------------------------------------------------------------------===//
1406 /// \brief Return an i1 value testing if \p Arg is null.
1407 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1408 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1412 /// \brief Return an i1 value testing if \p Arg is not null.
1413 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1414 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1418 /// \brief Return the i64 difference between two pointer values, dividing out
1419 /// the size of the pointed-to objects.
1421 /// This is intended to implement C-style pointer subtraction. As such, the
1422 /// pointers must be appropriately aligned for their element types and
1423 /// pointing into the same object.
1424 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1425 assert(LHS->getType() == RHS->getType() &&
1426 "Pointer subtraction operand types must match!");
1427 PointerType *ArgType = cast<PointerType>(LHS->getType());
1428 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1429 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1430 Value *Difference = CreateSub(LHS_int, RHS_int);
1431 return CreateExactSDiv(Difference,
1432 ConstantExpr::getSizeOf(ArgType->getElementType()),
1436 /// \brief Return a vector value that contains \arg V broadcasted to \p
1437 /// NumElts elements.
1438 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1439 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1441 // First insert it into an undef vector so we can shuffle it.
1442 Type *I32Ty = getInt32Ty();
1443 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1444 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1445 Name + ".splatinsert");
1447 // Shuffle the value across the desired number of elements.
1448 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1449 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1453 // Create wrappers for C Binding types (see CBindingWrapping.h).
1454 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)