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/ConstantFolder.h"
31 /// \brief This provides the default implementation of the IRBuilder
32 /// 'InsertHelper' method that is called whenever an instruction is created by
33 /// IRBuilder and needs to be inserted.
35 /// By default, this inserts the instruction at the insertion point.
36 template <bool preserveNames = true>
37 class IRBuilderDefaultInserter {
39 void InsertHelper(Instruction *I, const Twine &Name,
40 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
41 if (BB) BB->getInstList().insert(InsertPt, I);
47 /// \brief Common base class shared among various IRBuilders.
49 DebugLoc CurDbgLocation;
52 BasicBlock::iterator InsertPt;
56 IRBuilderBase(LLVMContext &context)
58 ClearInsertionPoint();
61 //===--------------------------------------------------------------------===//
62 // Builder configuration methods
63 //===--------------------------------------------------------------------===//
65 /// \brief Clear the insertion point: created instructions will not be
66 /// inserted into a block.
67 void ClearInsertionPoint() {
71 BasicBlock *GetInsertBlock() const { return BB; }
72 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
73 LLVMContext &getContext() const { return Context; }
75 /// \brief This specifies that created instructions should be appended to the
76 /// end of the specified block.
77 void SetInsertPoint(BasicBlock *TheBB) {
82 /// \brief This specifies that created instructions should be inserted before
83 /// the specified instruction.
84 void SetInsertPoint(Instruction *I) {
87 SetCurrentDebugLocation(I->getDebugLoc());
90 /// \brief This specifies that created instructions should be inserted at the
92 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
97 /// \brief Find the nearest point that dominates this use, and specify that
98 /// created instructions should be inserted at this point.
99 void SetInsertPoint(Use &U) {
100 Instruction *UseInst = cast<Instruction>(U.getUser());
101 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
102 BasicBlock *PredBB = Phi->getIncomingBlock(U);
103 assert(U != PredBB->getTerminator() && "critical edge not split");
104 SetInsertPoint(PredBB, PredBB->getTerminator());
107 SetInsertPoint(UseInst);
110 /// \brief Set location information used by debugging information.
111 void SetCurrentDebugLocation(const DebugLoc &L) {
115 /// \brief Get location information used by debugging information.
116 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
118 /// \brief If this builder has a current debug location, set it on the
119 /// specified instruction.
120 void SetInstDebugLocation(Instruction *I) const {
121 if (!CurDbgLocation.isUnknown())
122 I->setDebugLoc(CurDbgLocation);
125 /// \brief Get the return type of the current function that we're emitting
127 Type *getCurrentFunctionReturnType() const;
129 /// InsertPoint - A saved insertion point.
132 BasicBlock::iterator Point;
135 /// \brief Creates a new insertion point which doesn't point to anything.
136 InsertPoint() : Block(0) {}
138 /// \brief Creates a new insertion point at the given location.
139 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
140 : Block(InsertBlock), Point(InsertPoint) {}
142 /// \brief Returns true if this insert point is set.
143 bool isSet() const { return (Block != 0); }
145 llvm::BasicBlock *getBlock() const { return Block; }
146 llvm::BasicBlock::iterator getPoint() const { return Point; }
149 /// \brief Returns the current insert point.
150 InsertPoint saveIP() const {
151 return InsertPoint(GetInsertBlock(), GetInsertPoint());
154 /// \brief Returns the current insert point, clearing it in the process.
155 InsertPoint saveAndClearIP() {
156 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
157 ClearInsertionPoint();
161 /// \brief Sets the current insert point to a previously-saved location.
162 void restoreIP(InsertPoint IP) {
164 SetInsertPoint(IP.getBlock(), IP.getPoint());
166 ClearInsertionPoint();
169 //===--------------------------------------------------------------------===//
170 // Miscellaneous creation methods.
171 //===--------------------------------------------------------------------===//
173 /// \brief Make a new global variable with initializer type i8*
175 /// Make a new global variable with an initializer that has array of i8 type
176 /// filled in with the null terminated string value specified. The new global
177 /// variable will be marked mergable with any others of the same contents. If
178 /// Name is specified, it is the name of the global variable created.
179 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
181 /// \brief Get a constant value representing either true or false.
182 ConstantInt *getInt1(bool V) {
183 return ConstantInt::get(getInt1Ty(), V);
186 /// \brief Get the constant value for i1 true.
187 ConstantInt *getTrue() {
188 return ConstantInt::getTrue(Context);
191 /// \brief Get the constant value for i1 false.
192 ConstantInt *getFalse() {
193 return ConstantInt::getFalse(Context);
196 /// \brief Get a constant 8-bit value.
197 ConstantInt *getInt8(uint8_t C) {
198 return ConstantInt::get(getInt8Ty(), C);
201 /// \brief Get a constant 16-bit value.
202 ConstantInt *getInt16(uint16_t C) {
203 return ConstantInt::get(getInt16Ty(), C);
206 /// \brief Get a constant 32-bit value.
207 ConstantInt *getInt32(uint32_t C) {
208 return ConstantInt::get(getInt32Ty(), C);
211 /// \brief Get a constant 64-bit value.
212 ConstantInt *getInt64(uint64_t C) {
213 return ConstantInt::get(getInt64Ty(), C);
216 /// \brief Get a constant integer value.
217 ConstantInt *getInt(const APInt &AI) {
218 return ConstantInt::get(Context, AI);
221 //===--------------------------------------------------------------------===//
222 // Type creation methods
223 //===--------------------------------------------------------------------===//
225 /// \brief Fetch the type representing a single bit
226 IntegerType *getInt1Ty() {
227 return Type::getInt1Ty(Context);
230 /// \brief Fetch the type representing an 8-bit integer.
231 IntegerType *getInt8Ty() {
232 return Type::getInt8Ty(Context);
235 /// \brief Fetch the type representing a 16-bit integer.
236 IntegerType *getInt16Ty() {
237 return Type::getInt16Ty(Context);
240 /// \brief Fetch the type representing a 32-bit integer.
241 IntegerType *getInt32Ty() {
242 return Type::getInt32Ty(Context);
245 /// \brief Fetch the type representing a 64-bit integer.
246 IntegerType *getInt64Ty() {
247 return Type::getInt64Ty(Context);
250 /// \brief Fetch the type representing a 32-bit floating point value.
252 return Type::getFloatTy(Context);
255 /// \brief Fetch the type representing a 64-bit floating point value.
256 Type *getDoubleTy() {
257 return Type::getDoubleTy(Context);
260 /// \brief Fetch the type representing void.
262 return Type::getVoidTy(Context);
265 /// \brief Fetch the type representing a pointer to an 8-bit integer value.
266 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
267 return Type::getInt8PtrTy(Context, AddrSpace);
270 /// \brief Fetch the type representing a pointer to an integer value.
271 IntegerType* getIntPtrTy(DataLayout *DL, unsigned AddrSpace = 0) {
272 return DL->getIntPtrType(Context, AddrSpace);
275 //===--------------------------------------------------------------------===//
276 // Intrinsic creation methods
277 //===--------------------------------------------------------------------===//
279 /// \brief Create and insert a memset to the specified pointer and the
282 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
283 /// specified, it will be added to the instruction.
284 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
285 bool isVolatile = false, MDNode *TBAATag = 0) {
286 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
289 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
290 bool isVolatile = false, MDNode *TBAATag = 0);
292 /// \brief Create and insert a memcpy between the specified pointers.
294 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
295 /// specified, it will be added to the instruction.
296 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
297 bool isVolatile = false, MDNode *TBAATag = 0,
298 MDNode *TBAAStructTag = 0) {
299 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
303 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
304 bool isVolatile = false, MDNode *TBAATag = 0,
305 MDNode *TBAAStructTag = 0);
307 /// \brief Create and insert a memmove between the specified
310 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
311 /// specified, it will be added to the instruction.
312 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
313 bool isVolatile = false, MDNode *TBAATag = 0) {
314 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
317 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
318 bool isVolatile = false, MDNode *TBAATag = 0);
320 /// \brief Create a lifetime.start intrinsic.
322 /// If the pointer isn't i8* it will be converted.
323 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
325 /// \brief Create a lifetime.end intrinsic.
327 /// If the pointer isn't i8* it will be converted.
328 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
331 Value *getCastedInt8PtrValue(Value *Ptr);
334 /// \brief This provides a uniform API for creating instructions and inserting
335 /// them into a basic block: either at the end of a BasicBlock, or at a specific
336 /// iterator location in a block.
338 /// Note that the builder does not expose the full generality of LLVM
339 /// instructions. For access to extra instruction properties, use the mutators
340 /// (e.g. setVolatile) on the instructions after they have been
341 /// created. Convenience state exists to specify fast-math flags and fp-math
344 /// The first template argument handles whether or not to preserve names in the
345 /// final instruction output. This defaults to on. The second template argument
346 /// specifies a class to use for creating constants. This defaults to creating
347 /// minimally folded constants. The fourth template argument allows clients to
348 /// specify custom insertion hooks that are called on every newly created
350 template<bool preserveNames = true, typename T = ConstantFolder,
351 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
352 class IRBuilder : public IRBuilderBase, public Inserter {
354 MDNode *DefaultFPMathTag;
357 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
358 MDNode *FPMathTag = 0)
359 : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag),
363 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0)
364 : IRBuilderBase(C), Folder(), DefaultFPMathTag(FPMathTag), FMF() {
367 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
368 : IRBuilderBase(TheBB->getContext()), Folder(F),
369 DefaultFPMathTag(FPMathTag), FMF() {
370 SetInsertPoint(TheBB);
373 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
374 : IRBuilderBase(TheBB->getContext()), Folder(),
375 DefaultFPMathTag(FPMathTag), FMF() {
376 SetInsertPoint(TheBB);
379 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
380 : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
383 SetCurrentDebugLocation(IP->getDebugLoc());
386 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
387 : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
390 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
393 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
394 MDNode *FPMathTag = 0)
395 : IRBuilderBase(TheBB->getContext()), Folder(F),
396 DefaultFPMathTag(FPMathTag), FMF() {
397 SetInsertPoint(TheBB, IP);
400 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
401 : IRBuilderBase(TheBB->getContext()), Folder(),
402 DefaultFPMathTag(FPMathTag), FMF() {
403 SetInsertPoint(TheBB, IP);
406 /// \brief Get the constant folder being used.
407 const T &getFolder() { return Folder; }
409 /// \brief Get the floating point math metadata being used.
410 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
412 /// \brief Get the flags to be applied to created floating point ops
413 FastMathFlags getFastMathFlags() const { return FMF; }
415 /// \brief Clear the fast-math flags.
416 void clearFastMathFlags() { FMF.clear(); }
418 /// \brief SetDefaultFPMathTag - Set the floating point math metadata to be used.
419 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
421 /// \brief Set the fast-math flags to be used with generated fp-math operators
422 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
424 /// \brief Return true if this builder is configured to actually add the
425 /// requested names to IR created through it.
426 bool isNamePreserving() const { return preserveNames; }
428 /// \brief Insert and return the specified instruction.
429 template<typename InstTy>
430 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
431 this->InsertHelper(I, Name, BB, InsertPt);
432 this->SetInstDebugLocation(I);
436 /// \brief No-op overload to handle constants.
437 Constant *Insert(Constant *C, const Twine& = "") const {
441 //===--------------------------------------------------------------------===//
442 // Instruction creation methods: Terminators
443 //===--------------------------------------------------------------------===//
446 /// \brief Helper to add branch weight metadata onto an instruction.
447 /// \returns The annotated instruction.
448 template <typename InstTy>
449 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
451 I->setMetadata(LLVMContext::MD_prof, Weights);
456 /// \brief Create a 'ret void' instruction.
457 ReturnInst *CreateRetVoid() {
458 return Insert(ReturnInst::Create(Context));
461 /// \brief Create a 'ret <val>' instruction.
462 ReturnInst *CreateRet(Value *V) {
463 return Insert(ReturnInst::Create(Context, V));
466 /// \brief Create a sequence of N insertvalue instructions,
467 /// with one Value from the retVals array each, that build a aggregate
468 /// return value one value at a time, and a ret instruction to return
469 /// the resulting aggregate value.
471 /// This is a convenience function for code that uses aggregate return values
472 /// as a vehicle for having multiple return values.
473 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
474 Value *V = UndefValue::get(getCurrentFunctionReturnType());
475 for (unsigned i = 0; i != N; ++i)
476 V = CreateInsertValue(V, retVals[i], i, "mrv");
477 return Insert(ReturnInst::Create(Context, V));
480 /// \brief Create an unconditional 'br label X' instruction.
481 BranchInst *CreateBr(BasicBlock *Dest) {
482 return Insert(BranchInst::Create(Dest));
485 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
487 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
488 MDNode *BranchWeights = 0) {
489 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
493 /// \brief Create a switch instruction with the specified value, default dest,
494 /// and with a hint for the number of cases that will be added (for efficient
496 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
497 MDNode *BranchWeights = 0) {
498 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
502 /// \brief Create an indirect branch instruction with the specified address
503 /// operand, with an optional hint for the number of destinations that will be
504 /// added (for efficient allocation).
505 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
506 return Insert(IndirectBrInst::Create(Addr, NumDests));
509 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
510 BasicBlock *UnwindDest, const Twine &Name = "") {
511 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
512 ArrayRef<Value *>()),
515 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
516 BasicBlock *UnwindDest, Value *Arg1,
517 const Twine &Name = "") {
518 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
521 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
522 BasicBlock *UnwindDest, Value *Arg1,
523 Value *Arg2, Value *Arg3,
524 const Twine &Name = "") {
525 Value *Args[] = { Arg1, Arg2, Arg3 };
526 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
529 /// \brief Create an invoke instruction.
530 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
531 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
532 const Twine &Name = "") {
533 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
537 ResumeInst *CreateResume(Value *Exn) {
538 return Insert(ResumeInst::Create(Exn));
541 UnreachableInst *CreateUnreachable() {
542 return Insert(new UnreachableInst(Context));
545 //===--------------------------------------------------------------------===//
546 // Instruction creation methods: Binary Operators
547 //===--------------------------------------------------------------------===//
549 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
550 Value *LHS, Value *RHS,
552 bool HasNUW, bool HasNSW) {
553 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
554 if (HasNUW) BO->setHasNoUnsignedWrap();
555 if (HasNSW) BO->setHasNoSignedWrap();
559 Instruction *AddFPMathAttributes(Instruction *I,
561 FastMathFlags FMF) const {
563 FPMathTag = DefaultFPMathTag;
565 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
566 I->setFastMathFlags(FMF);
570 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
571 bool HasNUW = false, bool HasNSW = false) {
572 if (Constant *LC = dyn_cast<Constant>(LHS))
573 if (Constant *RC = dyn_cast<Constant>(RHS))
574 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
575 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
578 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
579 return CreateAdd(LHS, RHS, Name, false, true);
581 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
582 return CreateAdd(LHS, RHS, Name, true, false);
584 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
585 MDNode *FPMathTag = 0) {
586 if (Constant *LC = dyn_cast<Constant>(LHS))
587 if (Constant *RC = dyn_cast<Constant>(RHS))
588 return Insert(Folder.CreateFAdd(LC, RC), Name);
589 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
590 FPMathTag, FMF), Name);
592 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
593 bool HasNUW = false, bool HasNSW = false) {
594 if (Constant *LC = dyn_cast<Constant>(LHS))
595 if (Constant *RC = dyn_cast<Constant>(RHS))
596 return Insert(Folder.CreateSub(LC, RC), Name);
597 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
600 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
601 return CreateSub(LHS, RHS, Name, false, true);
603 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
604 return CreateSub(LHS, RHS, Name, true, false);
606 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
607 MDNode *FPMathTag = 0) {
608 if (Constant *LC = dyn_cast<Constant>(LHS))
609 if (Constant *RC = dyn_cast<Constant>(RHS))
610 return Insert(Folder.CreateFSub(LC, RC), Name);
611 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
612 FPMathTag, FMF), Name);
614 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
615 bool HasNUW = false, bool HasNSW = false) {
616 if (Constant *LC = dyn_cast<Constant>(LHS))
617 if (Constant *RC = dyn_cast<Constant>(RHS))
618 return Insert(Folder.CreateMul(LC, RC), Name);
619 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
622 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
623 return CreateMul(LHS, RHS, Name, false, true);
625 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
626 return CreateMul(LHS, RHS, Name, true, false);
628 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
629 MDNode *FPMathTag = 0) {
630 if (Constant *LC = dyn_cast<Constant>(LHS))
631 if (Constant *RC = dyn_cast<Constant>(RHS))
632 return Insert(Folder.CreateFMul(LC, RC), Name);
633 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
634 FPMathTag, FMF), Name);
636 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
637 bool isExact = false) {
638 if (Constant *LC = dyn_cast<Constant>(LHS))
639 if (Constant *RC = dyn_cast<Constant>(RHS))
640 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
642 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
643 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
645 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
646 return CreateUDiv(LHS, RHS, Name, true);
648 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
649 bool isExact = false) {
650 if (Constant *LC = dyn_cast<Constant>(LHS))
651 if (Constant *RC = dyn_cast<Constant>(RHS))
652 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
654 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
655 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
657 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
658 return CreateSDiv(LHS, RHS, Name, true);
660 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
661 MDNode *FPMathTag = 0) {
662 if (Constant *LC = dyn_cast<Constant>(LHS))
663 if (Constant *RC = dyn_cast<Constant>(RHS))
664 return Insert(Folder.CreateFDiv(LC, RC), Name);
665 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
666 FPMathTag, FMF), Name);
668 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
669 if (Constant *LC = dyn_cast<Constant>(LHS))
670 if (Constant *RC = dyn_cast<Constant>(RHS))
671 return Insert(Folder.CreateURem(LC, RC), Name);
672 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
674 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
675 if (Constant *LC = dyn_cast<Constant>(LHS))
676 if (Constant *RC = dyn_cast<Constant>(RHS))
677 return Insert(Folder.CreateSRem(LC, RC), Name);
678 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
680 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
681 MDNode *FPMathTag = 0) {
682 if (Constant *LC = dyn_cast<Constant>(LHS))
683 if (Constant *RC = dyn_cast<Constant>(RHS))
684 return Insert(Folder.CreateFRem(LC, RC), Name);
685 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
686 FPMathTag, FMF), Name);
689 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
690 bool HasNUW = false, bool HasNSW = false) {
691 if (Constant *LC = dyn_cast<Constant>(LHS))
692 if (Constant *RC = dyn_cast<Constant>(RHS))
693 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
694 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
697 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
698 bool HasNUW = false, bool HasNSW = false) {
699 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
702 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
703 bool HasNUW = false, bool HasNSW = false) {
704 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
708 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
709 bool isExact = false) {
710 if (Constant *LC = dyn_cast<Constant>(LHS))
711 if (Constant *RC = dyn_cast<Constant>(RHS))
712 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
714 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
715 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
717 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
718 bool isExact = false) {
719 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
721 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
722 bool isExact = false) {
723 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
726 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
727 bool isExact = false) {
728 if (Constant *LC = dyn_cast<Constant>(LHS))
729 if (Constant *RC = dyn_cast<Constant>(RHS))
730 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
732 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
733 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
735 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
736 bool isExact = false) {
737 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
739 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
740 bool isExact = false) {
741 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
744 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
745 if (Constant *RC = dyn_cast<Constant>(RHS)) {
746 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
747 return LHS; // LHS & -1 -> LHS
748 if (Constant *LC = dyn_cast<Constant>(LHS))
749 return Insert(Folder.CreateAnd(LC, RC), Name);
751 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
753 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
754 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
756 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
757 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
760 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
761 if (Constant *RC = dyn_cast<Constant>(RHS)) {
762 if (RC->isNullValue())
763 return LHS; // LHS | 0 -> LHS
764 if (Constant *LC = dyn_cast<Constant>(LHS))
765 return Insert(Folder.CreateOr(LC, RC), Name);
767 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
769 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
770 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
772 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
773 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
776 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
777 if (Constant *LC = dyn_cast<Constant>(LHS))
778 if (Constant *RC = dyn_cast<Constant>(RHS))
779 return Insert(Folder.CreateXor(LC, RC), Name);
780 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
782 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
783 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
785 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
786 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
789 Value *CreateBinOp(Instruction::BinaryOps Opc,
790 Value *LHS, Value *RHS, const Twine &Name = "") {
791 if (Constant *LC = dyn_cast<Constant>(LHS))
792 if (Constant *RC = dyn_cast<Constant>(RHS))
793 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
794 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
797 Value *CreateNeg(Value *V, const Twine &Name = "",
798 bool HasNUW = false, bool HasNSW = false) {
799 if (Constant *VC = dyn_cast<Constant>(V))
800 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
801 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
802 if (HasNUW) BO->setHasNoUnsignedWrap();
803 if (HasNSW) BO->setHasNoSignedWrap();
806 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
807 return CreateNeg(V, Name, false, true);
809 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
810 return CreateNeg(V, Name, true, false);
812 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
813 if (Constant *VC = dyn_cast<Constant>(V))
814 return Insert(Folder.CreateFNeg(VC), Name);
815 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
816 FPMathTag, FMF), Name);
818 Value *CreateNot(Value *V, const Twine &Name = "") {
819 if (Constant *VC = dyn_cast<Constant>(V))
820 return Insert(Folder.CreateNot(VC), Name);
821 return Insert(BinaryOperator::CreateNot(V), Name);
824 //===--------------------------------------------------------------------===//
825 // Instruction creation methods: Memory Instructions
826 //===--------------------------------------------------------------------===//
828 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
829 const Twine &Name = "") {
830 return Insert(new AllocaInst(Ty, ArraySize), Name);
832 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
833 // converting the string to 'bool' for the isVolatile parameter.
834 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
835 return Insert(new LoadInst(Ptr), Name);
837 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
838 return Insert(new LoadInst(Ptr), Name);
840 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
841 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
843 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
844 return Insert(new StoreInst(Val, Ptr, isVolatile));
846 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
847 // correctly, instead of converting the string to 'bool' for the isVolatile
849 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
850 LoadInst *LI = CreateLoad(Ptr, Name);
851 LI->setAlignment(Align);
854 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
855 const Twine &Name = "") {
856 LoadInst *LI = CreateLoad(Ptr, Name);
857 LI->setAlignment(Align);
860 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
861 const Twine &Name = "") {
862 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
863 LI->setAlignment(Align);
866 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
867 bool isVolatile = false) {
868 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
869 SI->setAlignment(Align);
872 FenceInst *CreateFence(AtomicOrdering Ordering,
873 SynchronizationScope SynchScope = CrossThread) {
874 return Insert(new FenceInst(Context, Ordering, SynchScope));
876 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
877 AtomicOrdering Ordering,
878 SynchronizationScope SynchScope = CrossThread) {
879 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
881 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
882 AtomicOrdering Ordering,
883 SynchronizationScope SynchScope = CrossThread) {
884 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
886 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
887 const Twine &Name = "") {
888 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
889 // Every index must be constant.
891 for (i = 0, e = IdxList.size(); i != e; ++i)
892 if (!isa<Constant>(IdxList[i]))
895 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
897 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
899 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
900 const Twine &Name = "") {
901 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
902 // Every index must be constant.
904 for (i = 0, e = IdxList.size(); i != e; ++i)
905 if (!isa<Constant>(IdxList[i]))
908 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
910 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
912 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
913 if (Constant *PC = dyn_cast<Constant>(Ptr))
914 if (Constant *IC = dyn_cast<Constant>(Idx))
915 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
916 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
918 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
919 if (Constant *PC = dyn_cast<Constant>(Ptr))
920 if (Constant *IC = dyn_cast<Constant>(Idx))
921 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
922 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
924 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
925 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
927 if (Constant *PC = dyn_cast<Constant>(Ptr))
928 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
930 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
932 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
933 const Twine &Name = "") {
934 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
936 if (Constant *PC = dyn_cast<Constant>(Ptr))
937 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
939 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
941 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
942 const Twine &Name = "") {
944 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
945 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
948 if (Constant *PC = dyn_cast<Constant>(Ptr))
949 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
951 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
953 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
954 const Twine &Name = "") {
956 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
957 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
960 if (Constant *PC = dyn_cast<Constant>(Ptr))
961 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
963 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
965 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
966 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
968 if (Constant *PC = dyn_cast<Constant>(Ptr))
969 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
971 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
973 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
974 const Twine &Name = "") {
975 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
977 if (Constant *PC = dyn_cast<Constant>(Ptr))
978 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
980 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
982 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
983 const Twine &Name = "") {
985 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
986 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
989 if (Constant *PC = dyn_cast<Constant>(Ptr))
990 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
992 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
994 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
995 const Twine &Name = "") {
997 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
998 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1001 if (Constant *PC = dyn_cast<Constant>(Ptr))
1002 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1004 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1006 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1007 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
1010 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1011 /// instead of a pointer to array of i8.
1012 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
1013 Value *gv = CreateGlobalString(Str, Name);
1014 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1015 Value *Args[] = { zero, zero };
1016 return CreateInBoundsGEP(gv, Args, Name);
1019 //===--------------------------------------------------------------------===//
1020 // Instruction creation methods: Cast/Conversion Operators
1021 //===--------------------------------------------------------------------===//
1023 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1024 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1026 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1027 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1029 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1030 return CreateCast(Instruction::SExt, V, DestTy, Name);
1032 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1033 /// the value untouched if the type of V is already DestTy.
1034 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1035 const Twine &Name = "") {
1036 assert(V->getType()->isIntOrIntVectorTy() &&
1037 DestTy->isIntOrIntVectorTy() &&
1038 "Can only zero extend/truncate integers!");
1039 Type *VTy = V->getType();
1040 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1041 return CreateZExt(V, DestTy, Name);
1042 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1043 return CreateTrunc(V, DestTy, Name);
1046 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1047 /// the value untouched if the type of V is already DestTy.
1048 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1049 const Twine &Name = "") {
1050 assert(V->getType()->isIntOrIntVectorTy() &&
1051 DestTy->isIntOrIntVectorTy() &&
1052 "Can only sign extend/truncate integers!");
1053 Type *VTy = V->getType();
1054 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1055 return CreateSExt(V, DestTy, Name);
1056 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1057 return CreateTrunc(V, DestTy, Name);
1060 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1061 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1063 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1064 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1066 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1067 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1069 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1070 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1072 Value *CreateFPTrunc(Value *V, Type *DestTy,
1073 const Twine &Name = "") {
1074 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1076 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1077 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1079 Value *CreatePtrToInt(Value *V, Type *DestTy,
1080 const Twine &Name = "") {
1081 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1083 Value *CreateIntToPtr(Value *V, Type *DestTy,
1084 const Twine &Name = "") {
1085 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1087 Value *CreateBitCast(Value *V, Type *DestTy,
1088 const Twine &Name = "") {
1089 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1091 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1092 const Twine &Name = "") {
1093 if (V->getType() == DestTy)
1095 if (Constant *VC = dyn_cast<Constant>(V))
1096 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1097 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1099 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1100 const Twine &Name = "") {
1101 if (V->getType() == DestTy)
1103 if (Constant *VC = dyn_cast<Constant>(V))
1104 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1105 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1107 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1108 const Twine &Name = "") {
1109 if (V->getType() == DestTy)
1111 if (Constant *VC = dyn_cast<Constant>(V))
1112 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1113 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1115 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1116 const Twine &Name = "") {
1117 if (V->getType() == DestTy)
1119 if (Constant *VC = dyn_cast<Constant>(V))
1120 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1121 return Insert(CastInst::Create(Op, V, DestTy), Name);
1123 Value *CreatePointerCast(Value *V, Type *DestTy,
1124 const Twine &Name = "") {
1125 if (V->getType() == DestTy)
1127 if (Constant *VC = dyn_cast<Constant>(V))
1128 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1129 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1131 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1132 const Twine &Name = "") {
1133 if (V->getType() == DestTy)
1135 if (Constant *VC = dyn_cast<Constant>(V))
1136 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1137 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1140 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1141 // compile time error, instead of converting the string to bool for the
1142 // isSigned parameter.
1143 Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
1145 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1146 if (V->getType() == DestTy)
1148 if (Constant *VC = dyn_cast<Constant>(V))
1149 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1150 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1153 //===--------------------------------------------------------------------===//
1154 // Instruction creation methods: Compare Instructions
1155 //===--------------------------------------------------------------------===//
1157 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1158 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1160 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1161 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1163 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1164 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1166 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1167 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1169 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1170 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1172 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1173 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1175 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1176 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1178 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1179 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1181 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1182 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1184 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1185 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1188 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1189 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1191 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1192 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1194 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1195 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1197 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1198 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1200 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1201 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1203 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1204 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1206 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1207 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1209 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1210 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1212 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1213 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1215 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1216 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1218 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1219 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1221 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1222 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1224 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1225 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1227 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1228 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1231 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1232 const Twine &Name = "") {
1233 if (Constant *LC = dyn_cast<Constant>(LHS))
1234 if (Constant *RC = dyn_cast<Constant>(RHS))
1235 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1236 return Insert(new ICmpInst(P, LHS, RHS), Name);
1238 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1239 const Twine &Name = "") {
1240 if (Constant *LC = dyn_cast<Constant>(LHS))
1241 if (Constant *RC = dyn_cast<Constant>(RHS))
1242 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1243 return Insert(new FCmpInst(P, LHS, RHS), Name);
1246 //===--------------------------------------------------------------------===//
1247 // Instruction creation methods: Other Instructions
1248 //===--------------------------------------------------------------------===//
1250 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1251 const Twine &Name = "") {
1252 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1255 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1256 return Insert(CallInst::Create(Callee), Name);
1258 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1259 return Insert(CallInst::Create(Callee, Arg), Name);
1261 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1262 const Twine &Name = "") {
1263 Value *Args[] = { Arg1, Arg2 };
1264 return Insert(CallInst::Create(Callee, Args), Name);
1266 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1267 const Twine &Name = "") {
1268 Value *Args[] = { Arg1, Arg2, Arg3 };
1269 return Insert(CallInst::Create(Callee, Args), Name);
1271 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1272 Value *Arg4, const Twine &Name = "") {
1273 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1274 return Insert(CallInst::Create(Callee, Args), Name);
1276 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1277 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1278 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1279 return Insert(CallInst::Create(Callee, Args), Name);
1282 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1283 const Twine &Name = "") {
1284 return Insert(CallInst::Create(Callee, Args), Name);
1287 Value *CreateSelect(Value *C, Value *True, Value *False,
1288 const Twine &Name = "") {
1289 if (Constant *CC = dyn_cast<Constant>(C))
1290 if (Constant *TC = dyn_cast<Constant>(True))
1291 if (Constant *FC = dyn_cast<Constant>(False))
1292 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1293 return Insert(SelectInst::Create(C, True, False), Name);
1296 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1297 return Insert(new VAArgInst(List, Ty), Name);
1300 Value *CreateExtractElement(Value *Vec, Value *Idx,
1301 const Twine &Name = "") {
1302 if (Constant *VC = dyn_cast<Constant>(Vec))
1303 if (Constant *IC = dyn_cast<Constant>(Idx))
1304 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1305 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1308 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1309 const Twine &Name = "") {
1310 if (Constant *VC = dyn_cast<Constant>(Vec))
1311 if (Constant *NC = dyn_cast<Constant>(NewElt))
1312 if (Constant *IC = dyn_cast<Constant>(Idx))
1313 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1314 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1317 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1318 const Twine &Name = "") {
1319 if (Constant *V1C = dyn_cast<Constant>(V1))
1320 if (Constant *V2C = dyn_cast<Constant>(V2))
1321 if (Constant *MC = dyn_cast<Constant>(Mask))
1322 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1323 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1326 Value *CreateExtractValue(Value *Agg,
1327 ArrayRef<unsigned> Idxs,
1328 const Twine &Name = "") {
1329 if (Constant *AggC = dyn_cast<Constant>(Agg))
1330 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1331 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1334 Value *CreateInsertValue(Value *Agg, Value *Val,
1335 ArrayRef<unsigned> Idxs,
1336 const Twine &Name = "") {
1337 if (Constant *AggC = dyn_cast<Constant>(Agg))
1338 if (Constant *ValC = dyn_cast<Constant>(Val))
1339 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1340 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1343 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1344 const Twine &Name = "") {
1345 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
1348 //===--------------------------------------------------------------------===//
1349 // Utility creation methods
1350 //===--------------------------------------------------------------------===//
1352 /// \brief Return an i1 value testing if \p Arg is null.
1353 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1354 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1358 /// \brief Return an i1 value testing if \p Arg is not null.
1359 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1360 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1364 /// \brief Return the i64 difference between two pointer values, dividing out
1365 /// the size of the pointed-to objects.
1367 /// This is intended to implement C-style pointer subtraction. As such, the
1368 /// pointers must be appropriately aligned for their element types and
1369 /// pointing into the same object.
1370 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1371 assert(LHS->getType() == RHS->getType() &&
1372 "Pointer subtraction operand types must match!");
1373 PointerType *ArgType = cast<PointerType>(LHS->getType());
1374 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1375 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1376 Value *Difference = CreateSub(LHS_int, RHS_int);
1377 return CreateExactSDiv(Difference,
1378 ConstantExpr::getSizeOf(ArgType->getElementType()),
1382 /// \brief Return a vector value that contains \arg V broadcasted to \p
1383 /// NumElts elements.
1384 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1385 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1387 // First insert it into an undef vector so we can shuffle it.
1388 Type *I32Ty = getInt32Ty();
1389 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1390 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1391 Name + ".splatinsert");
1393 // Shuffle the value across the desired number of elements.
1394 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1395 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");