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"
32 /// \brief This provides the default implementation of the IRBuilder
33 /// 'InsertHelper' method that is called whenever an instruction is created by
34 /// IRBuilder and needs to be inserted.
36 /// By default, this inserts the instruction at the insertion point.
37 template <bool preserveNames = true>
38 class IRBuilderDefaultInserter {
40 void InsertHelper(Instruction *I, const Twine &Name,
41 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
42 if (BB) BB->getInstList().insert(InsertPt, I);
48 /// \brief Common base class shared among various IRBuilders.
50 DebugLoc CurDbgLocation;
52 /// Save the current debug location here while we are suppressing
53 /// line table entries.
54 llvm::DebugLoc SavedDbgLocation;
57 BasicBlock::iterator InsertPt;
61 IRBuilderBase(LLVMContext &context)
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 SetCurrentDebugLocation(I->getDebugLoc());
96 /// \brief This specifies that created instructions should be inserted at the
98 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
103 /// \brief Find the nearest point that dominates this use, and specify that
104 /// created instructions should be inserted at this point.
105 void SetInsertPoint(Use &U) {
106 Instruction *UseInst = cast<Instruction>(U.getUser());
107 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
108 BasicBlock *PredBB = Phi->getIncomingBlock(U);
109 assert(U != PredBB->getTerminator() && "critical edge not split");
110 SetInsertPoint(PredBB, PredBB->getTerminator());
113 SetInsertPoint(UseInst);
116 /// \brief Set location information used by debugging information.
117 void SetCurrentDebugLocation(const DebugLoc &L) {
121 /// \brief Temporarily suppress DebugLocations from being attached
122 /// to emitted instructions, until the next call to
123 /// SetCurrentDebugLocation() or EnableDebugLocations(). Use this
124 /// if you want an instruction to be counted towards the prologue or
125 /// if there is no useful source location.
126 void DisableDebugLocations() {
127 llvm::DebugLoc Empty;
128 SavedDbgLocation = getCurrentDebugLocation();
129 SetCurrentDebugLocation(Empty);
132 /// \brief Restore the previously saved DebugLocation.
133 void EnableDebugLocations() {
134 assert(CurDbgLocation.isUnknown());
135 SetCurrentDebugLocation(SavedDbgLocation);
138 /// \brief Get location information used by debugging information.
139 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
141 /// \brief If this builder has a current debug location, set it on the
142 /// specified instruction.
143 void SetInstDebugLocation(Instruction *I) const {
144 if (!CurDbgLocation.isUnknown())
145 I->setDebugLoc(CurDbgLocation);
148 /// \brief Get the return type of the current function that we're emitting
150 Type *getCurrentFunctionReturnType() const;
152 /// InsertPoint - A saved insertion point.
155 BasicBlock::iterator Point;
158 /// \brief Creates a new insertion point which doesn't point to anything.
159 InsertPoint() : Block(0) {}
161 /// \brief Creates a new insertion point at the given location.
162 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
163 : Block(InsertBlock), Point(InsertPoint) {}
165 /// \brief Returns true if this insert point is set.
166 bool isSet() const { return (Block != 0); }
168 llvm::BasicBlock *getBlock() const { return Block; }
169 llvm::BasicBlock::iterator getPoint() const { return Point; }
172 /// \brief Returns the current insert point.
173 InsertPoint saveIP() const {
174 return InsertPoint(GetInsertBlock(), GetInsertPoint());
177 /// \brief Returns the current insert point, clearing it in the process.
178 InsertPoint saveAndClearIP() {
179 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
180 ClearInsertionPoint();
184 /// \brief Sets the current insert point to a previously-saved location.
185 void restoreIP(InsertPoint IP) {
187 SetInsertPoint(IP.getBlock(), IP.getPoint());
189 ClearInsertionPoint();
192 //===--------------------------------------------------------------------===//
193 // Miscellaneous creation methods.
194 //===--------------------------------------------------------------------===//
196 /// \brief Make a new global variable with initializer type i8*
198 /// Make a new global variable with an initializer that has array of i8 type
199 /// filled in with the null terminated string value specified. The new global
200 /// variable will be marked mergable with any others of the same contents. If
201 /// Name is specified, it is the name of the global variable created.
202 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
204 /// \brief Get a constant value representing either true or false.
205 ConstantInt *getInt1(bool V) {
206 return ConstantInt::get(getInt1Ty(), V);
209 /// \brief Get the constant value for i1 true.
210 ConstantInt *getTrue() {
211 return ConstantInt::getTrue(Context);
214 /// \brief Get the constant value for i1 false.
215 ConstantInt *getFalse() {
216 return ConstantInt::getFalse(Context);
219 /// \brief Get a constant 8-bit value.
220 ConstantInt *getInt8(uint8_t C) {
221 return ConstantInt::get(getInt8Ty(), C);
224 /// \brief Get a constant 16-bit value.
225 ConstantInt *getInt16(uint16_t C) {
226 return ConstantInt::get(getInt16Ty(), C);
229 /// \brief Get a constant 32-bit value.
230 ConstantInt *getInt32(uint32_t C) {
231 return ConstantInt::get(getInt32Ty(), C);
234 /// \brief Get a constant 64-bit value.
235 ConstantInt *getInt64(uint64_t C) {
236 return ConstantInt::get(getInt64Ty(), C);
239 /// \brief Get a constant integer value.
240 ConstantInt *getInt(const APInt &AI) {
241 return ConstantInt::get(Context, AI);
244 //===--------------------------------------------------------------------===//
245 // Type creation methods
246 //===--------------------------------------------------------------------===//
248 /// \brief Fetch the type representing a single bit
249 IntegerType *getInt1Ty() {
250 return Type::getInt1Ty(Context);
253 /// \brief Fetch the type representing an 8-bit integer.
254 IntegerType *getInt8Ty() {
255 return Type::getInt8Ty(Context);
258 /// \brief Fetch the type representing a 16-bit integer.
259 IntegerType *getInt16Ty() {
260 return Type::getInt16Ty(Context);
263 /// \brief Fetch the type representing a 32-bit integer.
264 IntegerType *getInt32Ty() {
265 return Type::getInt32Ty(Context);
268 /// \brief Fetch the type representing a 64-bit integer.
269 IntegerType *getInt64Ty() {
270 return Type::getInt64Ty(Context);
273 /// \brief Fetch the type representing a 32-bit floating point value.
275 return Type::getFloatTy(Context);
278 /// \brief Fetch the type representing a 64-bit floating point value.
279 Type *getDoubleTy() {
280 return Type::getDoubleTy(Context);
283 /// \brief Fetch the type representing void.
285 return Type::getVoidTy(Context);
288 /// \brief Fetch the type representing a pointer to an 8-bit integer value.
289 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
290 return Type::getInt8PtrTy(Context, AddrSpace);
293 /// \brief Fetch the type representing a pointer to an integer value.
294 IntegerType* getIntPtrTy(DataLayout *DL, unsigned AddrSpace = 0) {
295 return DL->getIntPtrType(Context, AddrSpace);
298 //===--------------------------------------------------------------------===//
299 // Intrinsic creation methods
300 //===--------------------------------------------------------------------===//
302 /// \brief Create and insert a memset to the specified pointer and the
305 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
306 /// specified, it will be added to the instruction.
307 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
308 bool isVolatile = false, MDNode *TBAATag = 0) {
309 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
312 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
313 bool isVolatile = false, MDNode *TBAATag = 0);
315 /// \brief Create and insert a memcpy between the specified pointers.
317 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
318 /// specified, it will be added to the instruction.
319 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
320 bool isVolatile = false, MDNode *TBAATag = 0,
321 MDNode *TBAAStructTag = 0) {
322 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
326 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
327 bool isVolatile = false, MDNode *TBAATag = 0,
328 MDNode *TBAAStructTag = 0);
330 /// \brief Create and insert a memmove between the specified
333 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
334 /// specified, it will be added to the instruction.
335 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
336 bool isVolatile = false, MDNode *TBAATag = 0) {
337 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
340 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
341 bool isVolatile = false, MDNode *TBAATag = 0);
343 /// \brief Create a lifetime.start intrinsic.
345 /// If the pointer isn't i8* it will be converted.
346 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
348 /// \brief Create a lifetime.end intrinsic.
350 /// If the pointer isn't i8* it will be converted.
351 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
354 Value *getCastedInt8PtrValue(Value *Ptr);
357 /// \brief This provides a uniform API for creating instructions and inserting
358 /// them into a basic block: either at the end of a BasicBlock, or at a specific
359 /// iterator location in a block.
361 /// Note that the builder does not expose the full generality of LLVM
362 /// instructions. For access to extra instruction properties, use the mutators
363 /// (e.g. setVolatile) on the instructions after they have been
364 /// created. Convenience state exists to specify fast-math flags and fp-math
367 /// The first template argument handles whether or not to preserve names in the
368 /// final instruction output. This defaults to on. The second template argument
369 /// specifies a class to use for creating constants. This defaults to creating
370 /// minimally folded constants. The fourth template argument allows clients to
371 /// specify custom insertion hooks that are called on every newly created
373 template<bool preserveNames = true, typename T = ConstantFolder,
374 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
375 class IRBuilder : public IRBuilderBase, public Inserter {
377 MDNode *DefaultFPMathTag;
380 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
381 MDNode *FPMathTag = 0)
382 : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag),
386 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0)
387 : IRBuilderBase(C), Folder(), DefaultFPMathTag(FPMathTag), FMF() {
390 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
391 : IRBuilderBase(TheBB->getContext()), Folder(F),
392 DefaultFPMathTag(FPMathTag), FMF() {
393 SetInsertPoint(TheBB);
396 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
397 : IRBuilderBase(TheBB->getContext()), Folder(),
398 DefaultFPMathTag(FPMathTag), FMF() {
399 SetInsertPoint(TheBB);
402 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
403 : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
406 SetCurrentDebugLocation(IP->getDebugLoc());
409 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
410 : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
413 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
416 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
417 MDNode *FPMathTag = 0)
418 : IRBuilderBase(TheBB->getContext()), Folder(F),
419 DefaultFPMathTag(FPMathTag), FMF() {
420 SetInsertPoint(TheBB, IP);
423 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
424 : IRBuilderBase(TheBB->getContext()), Folder(),
425 DefaultFPMathTag(FPMathTag), FMF() {
426 SetInsertPoint(TheBB, IP);
429 /// \brief Get the constant folder being used.
430 const T &getFolder() { return Folder; }
432 /// \brief Get the floating point math metadata being used.
433 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
435 /// \brief Get the flags to be applied to created floating point ops
436 FastMathFlags getFastMathFlags() const { return FMF; }
438 /// \brief Clear the fast-math flags.
439 void clearFastMathFlags() { FMF.clear(); }
441 /// \brief SetDefaultFPMathTag - Set the floating point math metadata to be used.
442 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
444 /// \brief Set the fast-math flags to be used with generated fp-math operators
445 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
447 /// \brief Return true if this builder is configured to actually add the
448 /// requested names to IR created through it.
449 bool isNamePreserving() const { return preserveNames; }
451 /// \brief Insert and return the specified instruction.
452 template<typename InstTy>
453 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
454 this->InsertHelper(I, Name, BB, InsertPt);
455 this->SetInstDebugLocation(I);
459 /// \brief No-op overload to handle constants.
460 Constant *Insert(Constant *C, const Twine& = "") const {
464 //===--------------------------------------------------------------------===//
465 // Instruction creation methods: Terminators
466 //===--------------------------------------------------------------------===//
469 /// \brief Helper to add branch weight metadata onto an instruction.
470 /// \returns The annotated instruction.
471 template <typename InstTy>
472 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
474 I->setMetadata(LLVMContext::MD_prof, Weights);
479 /// \brief Create a 'ret void' instruction.
480 ReturnInst *CreateRetVoid() {
481 return Insert(ReturnInst::Create(Context));
484 /// \brief Create a 'ret <val>' instruction.
485 ReturnInst *CreateRet(Value *V) {
486 return Insert(ReturnInst::Create(Context, V));
489 /// \brief Create a sequence of N insertvalue instructions,
490 /// with one Value from the retVals array each, that build a aggregate
491 /// return value one value at a time, and a ret instruction to return
492 /// the resulting aggregate value.
494 /// This is a convenience function for code that uses aggregate return values
495 /// as a vehicle for having multiple return values.
496 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
497 Value *V = UndefValue::get(getCurrentFunctionReturnType());
498 for (unsigned i = 0; i != N; ++i)
499 V = CreateInsertValue(V, retVals[i], i, "mrv");
500 return Insert(ReturnInst::Create(Context, V));
503 /// \brief Create an unconditional 'br label X' instruction.
504 BranchInst *CreateBr(BasicBlock *Dest) {
505 return Insert(BranchInst::Create(Dest));
508 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
510 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
511 MDNode *BranchWeights = 0) {
512 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
516 /// \brief Create a switch instruction with the specified value, default dest,
517 /// and with a hint for the number of cases that will be added (for efficient
519 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
520 MDNode *BranchWeights = 0) {
521 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
525 /// \brief Create an indirect branch instruction with the specified address
526 /// operand, with an optional hint for the number of destinations that will be
527 /// added (for efficient allocation).
528 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
529 return Insert(IndirectBrInst::Create(Addr, NumDests));
532 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
533 BasicBlock *UnwindDest, const Twine &Name = "") {
534 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
535 ArrayRef<Value *>()),
538 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
539 BasicBlock *UnwindDest, Value *Arg1,
540 const Twine &Name = "") {
541 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
544 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
545 BasicBlock *UnwindDest, Value *Arg1,
546 Value *Arg2, Value *Arg3,
547 const Twine &Name = "") {
548 Value *Args[] = { Arg1, Arg2, Arg3 };
549 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
552 /// \brief Create an invoke instruction.
553 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
554 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
555 const Twine &Name = "") {
556 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
560 ResumeInst *CreateResume(Value *Exn) {
561 return Insert(ResumeInst::Create(Exn));
564 UnreachableInst *CreateUnreachable() {
565 return Insert(new UnreachableInst(Context));
568 //===--------------------------------------------------------------------===//
569 // Instruction creation methods: Binary Operators
570 //===--------------------------------------------------------------------===//
572 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
573 Value *LHS, Value *RHS,
575 bool HasNUW, bool HasNSW) {
576 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
577 if (HasNUW) BO->setHasNoUnsignedWrap();
578 if (HasNSW) BO->setHasNoSignedWrap();
582 Instruction *AddFPMathAttributes(Instruction *I,
584 FastMathFlags FMF) const {
586 FPMathTag = DefaultFPMathTag;
588 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
589 I->setFastMathFlags(FMF);
593 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
594 bool HasNUW = false, bool HasNSW = false) {
595 if (Constant *LC = dyn_cast<Constant>(LHS))
596 if (Constant *RC = dyn_cast<Constant>(RHS))
597 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
598 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
601 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
602 return CreateAdd(LHS, RHS, Name, false, true);
604 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
605 return CreateAdd(LHS, RHS, Name, true, false);
607 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
608 MDNode *FPMathTag = 0) {
609 if (Constant *LC = dyn_cast<Constant>(LHS))
610 if (Constant *RC = dyn_cast<Constant>(RHS))
611 return Insert(Folder.CreateFAdd(LC, RC), Name);
612 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
613 FPMathTag, FMF), Name);
615 Value *CreateSub(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.CreateSub(LC, RC), Name);
620 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
623 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
624 return CreateSub(LHS, RHS, Name, false, true);
626 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
627 return CreateSub(LHS, RHS, Name, true, false);
629 Value *CreateFSub(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.CreateFSub(LC, RC), Name);
634 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
635 FPMathTag, FMF), Name);
637 Value *CreateMul(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.CreateMul(LC, RC), Name);
642 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
645 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
646 return CreateMul(LHS, RHS, Name, false, true);
648 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
649 return CreateMul(LHS, RHS, Name, true, false);
651 Value *CreateFMul(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.CreateFMul(LC, RC), Name);
656 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
657 FPMathTag, FMF), Name);
659 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
660 bool isExact = false) {
661 if (Constant *LC = dyn_cast<Constant>(LHS))
662 if (Constant *RC = dyn_cast<Constant>(RHS))
663 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
665 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
666 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
668 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
669 return CreateUDiv(LHS, RHS, Name, true);
671 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
672 bool isExact = false) {
673 if (Constant *LC = dyn_cast<Constant>(LHS))
674 if (Constant *RC = dyn_cast<Constant>(RHS))
675 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
677 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
678 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
680 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
681 return CreateSDiv(LHS, RHS, Name, true);
683 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
684 MDNode *FPMathTag = 0) {
685 if (Constant *LC = dyn_cast<Constant>(LHS))
686 if (Constant *RC = dyn_cast<Constant>(RHS))
687 return Insert(Folder.CreateFDiv(LC, RC), Name);
688 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
689 FPMathTag, FMF), Name);
691 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
692 if (Constant *LC = dyn_cast<Constant>(LHS))
693 if (Constant *RC = dyn_cast<Constant>(RHS))
694 return Insert(Folder.CreateURem(LC, RC), Name);
695 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
697 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
698 if (Constant *LC = dyn_cast<Constant>(LHS))
699 if (Constant *RC = dyn_cast<Constant>(RHS))
700 return Insert(Folder.CreateSRem(LC, RC), Name);
701 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
703 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
704 MDNode *FPMathTag = 0) {
705 if (Constant *LC = dyn_cast<Constant>(LHS))
706 if (Constant *RC = dyn_cast<Constant>(RHS))
707 return Insert(Folder.CreateFRem(LC, RC), Name);
708 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
709 FPMathTag, FMF), Name);
712 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
713 bool HasNUW = false, bool HasNSW = false) {
714 if (Constant *LC = dyn_cast<Constant>(LHS))
715 if (Constant *RC = dyn_cast<Constant>(RHS))
716 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
717 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
720 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
721 bool HasNUW = false, bool HasNSW = false) {
722 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
725 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
726 bool HasNUW = false, bool HasNSW = false) {
727 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
731 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
732 bool isExact = false) {
733 if (Constant *LC = dyn_cast<Constant>(LHS))
734 if (Constant *RC = dyn_cast<Constant>(RHS))
735 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
737 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
738 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
740 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
741 bool isExact = false) {
742 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
744 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
745 bool isExact = false) {
746 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
749 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
750 bool isExact = false) {
751 if (Constant *LC = dyn_cast<Constant>(LHS))
752 if (Constant *RC = dyn_cast<Constant>(RHS))
753 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
755 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
756 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
758 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
759 bool isExact = false) {
760 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
762 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
763 bool isExact = false) {
764 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
767 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
768 if (Constant *RC = dyn_cast<Constant>(RHS)) {
769 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
770 return LHS; // LHS & -1 -> LHS
771 if (Constant *LC = dyn_cast<Constant>(LHS))
772 return Insert(Folder.CreateAnd(LC, RC), Name);
774 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
776 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
777 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
779 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
780 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
783 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
784 if (Constant *RC = dyn_cast<Constant>(RHS)) {
785 if (RC->isNullValue())
786 return LHS; // LHS | 0 -> LHS
787 if (Constant *LC = dyn_cast<Constant>(LHS))
788 return Insert(Folder.CreateOr(LC, RC), Name);
790 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
792 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
793 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
795 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
796 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
799 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
800 if (Constant *LC = dyn_cast<Constant>(LHS))
801 if (Constant *RC = dyn_cast<Constant>(RHS))
802 return Insert(Folder.CreateXor(LC, RC), Name);
803 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
805 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
806 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
808 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
809 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
812 Value *CreateBinOp(Instruction::BinaryOps Opc,
813 Value *LHS, Value *RHS, const Twine &Name = "") {
814 if (Constant *LC = dyn_cast<Constant>(LHS))
815 if (Constant *RC = dyn_cast<Constant>(RHS))
816 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
817 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
820 Value *CreateNeg(Value *V, const Twine &Name = "",
821 bool HasNUW = false, bool HasNSW = false) {
822 if (Constant *VC = dyn_cast<Constant>(V))
823 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
824 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
825 if (HasNUW) BO->setHasNoUnsignedWrap();
826 if (HasNSW) BO->setHasNoSignedWrap();
829 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
830 return CreateNeg(V, Name, false, true);
832 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
833 return CreateNeg(V, Name, true, false);
835 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
836 if (Constant *VC = dyn_cast<Constant>(V))
837 return Insert(Folder.CreateFNeg(VC), Name);
838 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
839 FPMathTag, FMF), Name);
841 Value *CreateNot(Value *V, const Twine &Name = "") {
842 if (Constant *VC = dyn_cast<Constant>(V))
843 return Insert(Folder.CreateNot(VC), Name);
844 return Insert(BinaryOperator::CreateNot(V), Name);
847 //===--------------------------------------------------------------------===//
848 // Instruction creation methods: Memory Instructions
849 //===--------------------------------------------------------------------===//
851 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
852 const Twine &Name = "") {
853 return Insert(new AllocaInst(Ty, ArraySize), Name);
855 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
856 // converting the string to 'bool' for the isVolatile parameter.
857 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
858 return Insert(new LoadInst(Ptr), Name);
860 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
861 return Insert(new LoadInst(Ptr), Name);
863 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
864 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
866 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
867 return Insert(new StoreInst(Val, Ptr, isVolatile));
869 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
870 // correctly, instead of converting the string to 'bool' for the isVolatile
872 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
873 LoadInst *LI = CreateLoad(Ptr, Name);
874 LI->setAlignment(Align);
877 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
878 const Twine &Name = "") {
879 LoadInst *LI = CreateLoad(Ptr, Name);
880 LI->setAlignment(Align);
883 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
884 const Twine &Name = "") {
885 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
886 LI->setAlignment(Align);
889 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
890 bool isVolatile = false) {
891 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
892 SI->setAlignment(Align);
895 FenceInst *CreateFence(AtomicOrdering Ordering,
896 SynchronizationScope SynchScope = CrossThread) {
897 return Insert(new FenceInst(Context, Ordering, SynchScope));
899 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
900 AtomicOrdering Ordering,
901 SynchronizationScope SynchScope = CrossThread) {
902 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
904 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
905 AtomicOrdering Ordering,
906 SynchronizationScope SynchScope = CrossThread) {
907 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
909 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
910 const Twine &Name = "") {
911 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
912 // Every index must be constant.
914 for (i = 0, e = IdxList.size(); i != e; ++i)
915 if (!isa<Constant>(IdxList[i]))
918 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
920 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
922 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
923 const Twine &Name = "") {
924 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
925 // Every index must be constant.
927 for (i = 0, e = IdxList.size(); i != e; ++i)
928 if (!isa<Constant>(IdxList[i]))
931 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
933 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
935 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
936 if (Constant *PC = dyn_cast<Constant>(Ptr))
937 if (Constant *IC = dyn_cast<Constant>(Idx))
938 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
939 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
941 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
942 if (Constant *PC = dyn_cast<Constant>(Ptr))
943 if (Constant *IC = dyn_cast<Constant>(Idx))
944 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
945 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
947 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
948 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
950 if (Constant *PC = dyn_cast<Constant>(Ptr))
951 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
953 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
955 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
956 const Twine &Name = "") {
957 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
959 if (Constant *PC = dyn_cast<Constant>(Ptr))
960 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
962 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
964 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
965 const Twine &Name = "") {
967 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
968 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
971 if (Constant *PC = dyn_cast<Constant>(Ptr))
972 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
974 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
976 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
977 const Twine &Name = "") {
979 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
980 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
983 if (Constant *PC = dyn_cast<Constant>(Ptr))
984 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
986 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
988 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
989 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
991 if (Constant *PC = dyn_cast<Constant>(Ptr))
992 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
994 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
996 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
997 const Twine &Name = "") {
998 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1000 if (Constant *PC = dyn_cast<Constant>(Ptr))
1001 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
1003 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1005 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1006 const Twine &Name = "") {
1008 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1009 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1012 if (Constant *PC = dyn_cast<Constant>(Ptr))
1013 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
1015 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
1017 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1018 const Twine &Name = "") {
1020 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1021 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1024 if (Constant *PC = dyn_cast<Constant>(Ptr))
1025 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1027 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1029 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1030 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
1033 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1034 /// instead of a pointer to array of i8.
1035 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
1036 Value *gv = CreateGlobalString(Str, Name);
1037 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1038 Value *Args[] = { zero, zero };
1039 return CreateInBoundsGEP(gv, Args, Name);
1042 //===--------------------------------------------------------------------===//
1043 // Instruction creation methods: Cast/Conversion Operators
1044 //===--------------------------------------------------------------------===//
1046 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1047 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1049 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1050 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1052 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1053 return CreateCast(Instruction::SExt, V, DestTy, Name);
1055 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1056 /// the value untouched if the type of V is already DestTy.
1057 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1058 const Twine &Name = "") {
1059 assert(V->getType()->isIntOrIntVectorTy() &&
1060 DestTy->isIntOrIntVectorTy() &&
1061 "Can only zero extend/truncate integers!");
1062 Type *VTy = V->getType();
1063 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1064 return CreateZExt(V, DestTy, Name);
1065 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1066 return CreateTrunc(V, DestTy, Name);
1069 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1070 /// the value untouched if the type of V is already DestTy.
1071 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1072 const Twine &Name = "") {
1073 assert(V->getType()->isIntOrIntVectorTy() &&
1074 DestTy->isIntOrIntVectorTy() &&
1075 "Can only sign extend/truncate integers!");
1076 Type *VTy = V->getType();
1077 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1078 return CreateSExt(V, DestTy, Name);
1079 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1080 return CreateTrunc(V, DestTy, Name);
1083 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1084 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1086 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1087 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1089 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1090 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1092 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1093 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1095 Value *CreateFPTrunc(Value *V, Type *DestTy,
1096 const Twine &Name = "") {
1097 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1099 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1100 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1102 Value *CreatePtrToInt(Value *V, Type *DestTy,
1103 const Twine &Name = "") {
1104 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1106 Value *CreateIntToPtr(Value *V, Type *DestTy,
1107 const Twine &Name = "") {
1108 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1110 Value *CreateBitCast(Value *V, Type *DestTy,
1111 const Twine &Name = "") {
1112 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1114 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1115 const Twine &Name = "") {
1116 if (V->getType() == DestTy)
1118 if (Constant *VC = dyn_cast<Constant>(V))
1119 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1120 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1122 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1123 const Twine &Name = "") {
1124 if (V->getType() == DestTy)
1126 if (Constant *VC = dyn_cast<Constant>(V))
1127 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1128 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1130 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1131 const Twine &Name = "") {
1132 if (V->getType() == DestTy)
1134 if (Constant *VC = dyn_cast<Constant>(V))
1135 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1136 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1138 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1139 const Twine &Name = "") {
1140 if (V->getType() == DestTy)
1142 if (Constant *VC = dyn_cast<Constant>(V))
1143 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1144 return Insert(CastInst::Create(Op, V, DestTy), Name);
1146 Value *CreatePointerCast(Value *V, Type *DestTy,
1147 const Twine &Name = "") {
1148 if (V->getType() == DestTy)
1150 if (Constant *VC = dyn_cast<Constant>(V))
1151 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1152 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1154 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1155 const Twine &Name = "") {
1156 if (V->getType() == DestTy)
1158 if (Constant *VC = dyn_cast<Constant>(V))
1159 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1160 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1163 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1164 // compile time error, instead of converting the string to bool for the
1165 // isSigned parameter.
1166 Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
1168 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1169 if (V->getType() == DestTy)
1171 if (Constant *VC = dyn_cast<Constant>(V))
1172 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1173 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1176 //===--------------------------------------------------------------------===//
1177 // Instruction creation methods: Compare Instructions
1178 //===--------------------------------------------------------------------===//
1180 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1181 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1183 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1184 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1186 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1187 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1189 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1190 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1192 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1193 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1195 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1196 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1198 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1199 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1201 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1202 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1204 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1205 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1207 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1208 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1211 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1212 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1214 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1215 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1217 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1218 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1220 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1221 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1223 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1224 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1226 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1227 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1229 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1230 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1232 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1233 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1235 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1236 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1238 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1239 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1241 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1242 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1244 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1245 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1247 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1248 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1250 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1251 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1254 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1255 const Twine &Name = "") {
1256 if (Constant *LC = dyn_cast<Constant>(LHS))
1257 if (Constant *RC = dyn_cast<Constant>(RHS))
1258 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1259 return Insert(new ICmpInst(P, LHS, RHS), Name);
1261 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1262 const Twine &Name = "") {
1263 if (Constant *LC = dyn_cast<Constant>(LHS))
1264 if (Constant *RC = dyn_cast<Constant>(RHS))
1265 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1266 return Insert(new FCmpInst(P, LHS, RHS), Name);
1269 //===--------------------------------------------------------------------===//
1270 // Instruction creation methods: Other Instructions
1271 //===--------------------------------------------------------------------===//
1273 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1274 const Twine &Name = "") {
1275 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1278 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1279 return Insert(CallInst::Create(Callee), Name);
1281 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1282 return Insert(CallInst::Create(Callee, Arg), Name);
1284 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1285 const Twine &Name = "") {
1286 Value *Args[] = { Arg1, Arg2 };
1287 return Insert(CallInst::Create(Callee, Args), Name);
1289 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1290 const Twine &Name = "") {
1291 Value *Args[] = { Arg1, Arg2, Arg3 };
1292 return Insert(CallInst::Create(Callee, Args), Name);
1294 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1295 Value *Arg4, const Twine &Name = "") {
1296 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1297 return Insert(CallInst::Create(Callee, Args), Name);
1299 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1300 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1301 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1302 return Insert(CallInst::Create(Callee, Args), Name);
1305 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1306 const Twine &Name = "") {
1307 return Insert(CallInst::Create(Callee, Args), Name);
1310 Value *CreateSelect(Value *C, Value *True, Value *False,
1311 const Twine &Name = "") {
1312 if (Constant *CC = dyn_cast<Constant>(C))
1313 if (Constant *TC = dyn_cast<Constant>(True))
1314 if (Constant *FC = dyn_cast<Constant>(False))
1315 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1316 return Insert(SelectInst::Create(C, True, False), Name);
1319 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1320 return Insert(new VAArgInst(List, Ty), Name);
1323 Value *CreateExtractElement(Value *Vec, Value *Idx,
1324 const Twine &Name = "") {
1325 if (Constant *VC = dyn_cast<Constant>(Vec))
1326 if (Constant *IC = dyn_cast<Constant>(Idx))
1327 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1328 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1331 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1332 const Twine &Name = "") {
1333 if (Constant *VC = dyn_cast<Constant>(Vec))
1334 if (Constant *NC = dyn_cast<Constant>(NewElt))
1335 if (Constant *IC = dyn_cast<Constant>(Idx))
1336 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1337 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1340 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1341 const Twine &Name = "") {
1342 if (Constant *V1C = dyn_cast<Constant>(V1))
1343 if (Constant *V2C = dyn_cast<Constant>(V2))
1344 if (Constant *MC = dyn_cast<Constant>(Mask))
1345 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1346 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1349 Value *CreateExtractValue(Value *Agg,
1350 ArrayRef<unsigned> Idxs,
1351 const Twine &Name = "") {
1352 if (Constant *AggC = dyn_cast<Constant>(Agg))
1353 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1354 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1357 Value *CreateInsertValue(Value *Agg, Value *Val,
1358 ArrayRef<unsigned> Idxs,
1359 const Twine &Name = "") {
1360 if (Constant *AggC = dyn_cast<Constant>(Agg))
1361 if (Constant *ValC = dyn_cast<Constant>(Val))
1362 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1363 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1366 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1367 const Twine &Name = "") {
1368 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
1371 //===--------------------------------------------------------------------===//
1372 // Utility creation methods
1373 //===--------------------------------------------------------------------===//
1375 /// \brief Return an i1 value testing if \p Arg is null.
1376 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1377 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1381 /// \brief Return an i1 value testing if \p Arg is not null.
1382 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1383 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1387 /// \brief Return the i64 difference between two pointer values, dividing out
1388 /// the size of the pointed-to objects.
1390 /// This is intended to implement C-style pointer subtraction. As such, the
1391 /// pointers must be appropriately aligned for their element types and
1392 /// pointing into the same object.
1393 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1394 assert(LHS->getType() == RHS->getType() &&
1395 "Pointer subtraction operand types must match!");
1396 PointerType *ArgType = cast<PointerType>(LHS->getType());
1397 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1398 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1399 Value *Difference = CreateSub(LHS_int, RHS_int);
1400 return CreateExactSDiv(Difference,
1401 ConstantExpr::getSizeOf(ArgType->getElementType()),
1405 /// \brief Return a vector value that contains \arg V broadcasted to \p
1406 /// NumElts elements.
1407 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1408 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1410 // First insert it into an undef vector so we can shuffle it.
1411 Type *I32Ty = getInt32Ty();
1412 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1413 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1414 Name + ".splatinsert");
1416 // Shuffle the value across the desired number of elements.
1417 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1418 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1422 // Create wrappers for C Binding types (see CBindingWrapping.h).
1423 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)