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 /// IRBuilderDefaultInserter - This provides the default implementation of the
32 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
33 /// created by IRBuilder and needs to be inserted. By default, this inserts the
34 /// instruction at the insertion point.
35 template <bool preserveNames = true>
36 class IRBuilderDefaultInserter {
38 void InsertHelper(Instruction *I, const Twine &Name,
39 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
40 if (BB) BB->getInstList().insert(InsertPt, I);
46 /// IRBuilderBase - Common base class shared among various IRBuilders.
48 DebugLoc CurDbgLocation;
51 BasicBlock::iterator InsertPt;
55 IRBuilderBase(LLVMContext &context)
57 ClearInsertionPoint();
60 //===--------------------------------------------------------------------===//
61 // Builder configuration methods
62 //===--------------------------------------------------------------------===//
64 /// \brief Clear the insertion point: created instructions will not be
65 /// inserted into a block.
66 void ClearInsertionPoint() {
70 BasicBlock *GetInsertBlock() const { return BB; }
71 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
72 LLVMContext &getContext() const { return Context; }
74 /// \brief This specifies that created instructions should be appended to the
75 /// end of the specified block.
76 void SetInsertPoint(BasicBlock *TheBB) {
81 /// \brief This specifies that created instructions should be inserted before
82 /// the specified instruction.
83 void SetInsertPoint(Instruction *I) {
86 SetCurrentDebugLocation(I->getDebugLoc());
89 /// \brief This specifies that created instructions should be inserted at the
91 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
96 /// \brief Find the nearest point that dominates this use, and specify that
97 /// created instructions should be inserted at this point.
98 void SetInsertPoint(Use &U) {
99 Instruction *UseInst = cast<Instruction>(U.getUser());
100 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
101 BasicBlock *PredBB = Phi->getIncomingBlock(U);
102 assert(U != PredBB->getTerminator() && "critical edge not split");
103 SetInsertPoint(PredBB, PredBB->getTerminator());
106 SetInsertPoint(UseInst);
109 /// \brief Set location information used by debugging information.
110 void SetCurrentDebugLocation(const DebugLoc &L) {
114 /// \brief Get location information used by debugging information.
115 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
117 /// \brief If this builder has a current debug location, set it on the
118 /// specified instruction.
119 void SetInstDebugLocation(Instruction *I) const {
120 if (!CurDbgLocation.isUnknown())
121 I->setDebugLoc(CurDbgLocation);
124 /// \brief Get the return type of the current function that we're emitting
126 Type *getCurrentFunctionReturnType() const;
128 /// InsertPoint - A saved insertion point.
131 BasicBlock::iterator Point;
134 /// \brief Creates a new insertion point which doesn't point to anything.
135 InsertPoint() : Block(0) {}
137 /// \brief Creates a new insertion point at the given location.
138 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
139 : Block(InsertBlock), Point(InsertPoint) {}
141 /// \brief Returns true if this insert point is set.
142 bool isSet() const { return (Block != 0); }
144 llvm::BasicBlock *getBlock() const { return Block; }
145 llvm::BasicBlock::iterator getPoint() const { return Point; }
148 /// \brief Returns the current insert point.
149 InsertPoint saveIP() const {
150 return InsertPoint(GetInsertBlock(), GetInsertPoint());
153 /// \brief Returns the current insert point, clearing it in the process.
154 InsertPoint saveAndClearIP() {
155 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
156 ClearInsertionPoint();
160 /// \brief Sets the current insert point to a previously-saved location.
161 void restoreIP(InsertPoint IP) {
163 SetInsertPoint(IP.getBlock(), IP.getPoint());
165 ClearInsertionPoint();
168 //===--------------------------------------------------------------------===//
169 // Miscellaneous creation methods.
170 //===--------------------------------------------------------------------===//
172 /// \brief Make a new global variable with initializer type i8*
174 /// Make a new global variable with an initializer that has array of i8 type
175 /// filled in with the null terminated string value specified. The new global
176 /// variable will be marked mergable with any others of the same contents. If
177 /// Name is specified, it is the name of the global variable created.
178 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
180 /// \brief Get a constant value representing either true or false.
181 ConstantInt *getInt1(bool V) {
182 return ConstantInt::get(getInt1Ty(), V);
185 /// \brief Get the constant value for i1 true.
186 ConstantInt *getTrue() {
187 return ConstantInt::getTrue(Context);
190 /// \brief Get the constant value for i1 false.
191 ConstantInt *getFalse() {
192 return ConstantInt::getFalse(Context);
195 /// \brief Get a constant 8-bit value.
196 ConstantInt *getInt8(uint8_t C) {
197 return ConstantInt::get(getInt8Ty(), C);
200 /// \brief Get a constant 16-bit value.
201 ConstantInt *getInt16(uint16_t C) {
202 return ConstantInt::get(getInt16Ty(), C);
205 /// \brief Get a constant 32-bit value.
206 ConstantInt *getInt32(uint32_t C) {
207 return ConstantInt::get(getInt32Ty(), C);
210 /// \brief Get a constant 64-bit value.
211 ConstantInt *getInt64(uint64_t C) {
212 return ConstantInt::get(getInt64Ty(), C);
215 /// \brief Get a constant integer value.
216 ConstantInt *getInt(const APInt &AI) {
217 return ConstantInt::get(Context, AI);
220 //===--------------------------------------------------------------------===//
221 // Type creation methods
222 //===--------------------------------------------------------------------===//
224 /// \brief Fetch the type representing a single bit
225 IntegerType *getInt1Ty() {
226 return Type::getInt1Ty(Context);
229 /// \brief Fetch the type representing an 8-bit integer.
230 IntegerType *getInt8Ty() {
231 return Type::getInt8Ty(Context);
234 /// \brief Fetch the type representing a 16-bit integer.
235 IntegerType *getInt16Ty() {
236 return Type::getInt16Ty(Context);
239 /// \brief Fetch the type representing a 32-bit integer.
240 IntegerType *getInt32Ty() {
241 return Type::getInt32Ty(Context);
244 /// \brief Fetch the type representing a 64-bit integer.
245 IntegerType *getInt64Ty() {
246 return Type::getInt64Ty(Context);
249 /// \brief Fetch the type representing a 32-bit floating point value.
251 return Type::getFloatTy(Context);
254 /// \brief Fetch the type representing a 64-bit floating point value.
255 Type *getDoubleTy() {
256 return Type::getDoubleTy(Context);
259 /// \brief Fetch the type representing void.
261 return Type::getVoidTy(Context);
264 /// \brief Fetch the type representing a pointer to an 8-bit integer value.
265 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
266 return Type::getInt8PtrTy(Context, AddrSpace);
269 /// \brief Fetch the type representing a pointer to an integer value.
270 IntegerType* getIntPtrTy(DataLayout *DL, unsigned AddrSpace = 0) {
271 return DL->getIntPtrType(Context, AddrSpace);
274 //===--------------------------------------------------------------------===//
275 // Intrinsic creation methods
276 //===--------------------------------------------------------------------===//
278 /// \brief Create and insert a memset to the specified pointer and the
281 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
282 /// specified, it will be added to the instruction.
283 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
284 bool isVolatile = false, MDNode *TBAATag = 0) {
285 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
288 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
289 bool isVolatile = false, MDNode *TBAATag = 0);
291 /// \brief Create and insert a memcpy between the specified pointers.
293 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
294 /// specified, it will be added to the instruction.
295 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
296 bool isVolatile = false, MDNode *TBAATag = 0,
297 MDNode *TBAAStructTag = 0) {
298 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
302 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
303 bool isVolatile = false, MDNode *TBAATag = 0,
304 MDNode *TBAAStructTag = 0);
306 /// \brief Create and insert a memmove between the specified
309 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
310 /// specified, it will be added to the instruction.
311 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
312 bool isVolatile = false, MDNode *TBAATag = 0) {
313 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
316 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
317 bool isVolatile = false, MDNode *TBAATag = 0);
319 /// \brief Create a lifetime.start intrinsic.
321 /// If the pointer isn't i8* it will be converted.
322 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
324 /// \brief Create a lifetime.end intrinsic.
326 /// If the pointer isn't i8* it will be converted.
327 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
330 Value *getCastedInt8PtrValue(Value *Ptr);
333 /// IRBuilder - This provides a uniform API for creating instructions and
334 /// inserting them into a basic block: either at the end of a BasicBlock, or
335 /// at a specific iterator location in a block.
337 /// Note that the builder does not expose the full generality of LLVM
338 /// instructions. For access to extra instruction properties, use the mutators
339 /// (e.g. setVolatile) on the instructions after they have been
340 /// created. Convenience state exists to specify fast-math flags and fp-math
343 /// The first template argument handles whether or not to preserve names in the
344 /// final instruction output. This defaults to on. The second template argument
345 /// specifies a class to use for creating constants. This defaults to creating
346 /// minimally folded constants. The fourth template argument allows clients to
347 /// specify custom insertion hooks that are called on every newly created
349 template<bool preserveNames = true, typename T = ConstantFolder,
350 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
351 class IRBuilder : public IRBuilderBase, public Inserter {
353 MDNode *DefaultFPMathTag;
356 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
357 MDNode *FPMathTag = 0)
358 : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag),
362 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0)
363 : IRBuilderBase(C), Folder(), DefaultFPMathTag(FPMathTag), FMF() {
366 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
367 : IRBuilderBase(TheBB->getContext()), Folder(F),
368 DefaultFPMathTag(FPMathTag), FMF() {
369 SetInsertPoint(TheBB);
372 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
373 : IRBuilderBase(TheBB->getContext()), Folder(),
374 DefaultFPMathTag(FPMathTag), FMF() {
375 SetInsertPoint(TheBB);
378 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
379 : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
382 SetCurrentDebugLocation(IP->getDebugLoc());
385 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
386 : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
389 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
392 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
393 MDNode *FPMathTag = 0)
394 : IRBuilderBase(TheBB->getContext()), Folder(F),
395 DefaultFPMathTag(FPMathTag), FMF() {
396 SetInsertPoint(TheBB, IP);
399 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
400 : IRBuilderBase(TheBB->getContext()), Folder(),
401 DefaultFPMathTag(FPMathTag), FMF() {
402 SetInsertPoint(TheBB, IP);
405 /// \brief Get the constant folder being used.
406 const T &getFolder() { return Folder; }
408 /// \brief Get the floating point math metadata being used.
409 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
411 /// \brief Get the flags to be applied to created floating point ops
412 FastMathFlags getFastMathFlags() const { return FMF; }
414 /// \brief Clear the fast-math flags.
415 void clearFastMathFlags() { FMF.clear(); }
417 /// \brief SetDefaultFPMathTag - Set the floating point math metadata to be used.
418 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
420 /// \brief Set the fast-math flags to be used with generated fp-math operators
421 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
423 /// \brief Return true if this builder is configured to actually add the
424 /// requested names to IR created through it.
425 bool isNamePreserving() const { return preserveNames; }
427 /// \brief Insert and return the specified instruction.
428 template<typename InstTy>
429 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
430 this->InsertHelper(I, Name, BB, InsertPt);
431 if (!getCurrentDebugLocation().isUnknown())
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, IntegerType *DestTy,
1035 const Twine &Name = "") {
1036 assert(isa<IntegerType>(V->getType()) &&
1037 "Can only zero extend/truncate integers!");
1038 IntegerType *IntTy = cast<IntegerType>(V->getType());
1039 if (IntTy->getBitWidth() < DestTy->getBitWidth())
1040 return CreateZExt(V, DestTy, Name);
1041 if (IntTy->getBitWidth() > DestTy->getBitWidth())
1042 return CreateTrunc(V, DestTy, Name);
1045 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1046 /// the value untouched if the type of V is already DestTy.
1047 Value *CreateSExtOrTrunc(Value *V, IntegerType *DestTy,
1048 const Twine &Name = "") {
1049 assert(isa<IntegerType>(V->getType()) &&
1050 "Can only sign extend/truncate integers!");
1051 IntegerType *IntTy = cast<IntegerType>(V->getType());
1052 if (IntTy->getBitWidth() < DestTy->getBitWidth())
1053 return CreateSExt(V, DestTy, Name);
1054 if (IntTy->getBitWidth() > DestTy->getBitWidth())
1055 return CreateTrunc(V, DestTy, Name);
1058 /// \brief Create a FPExt or FPTrunc from the float value V to DestTy. Return
1059 /// the value untouched if the type of V is already DestTy.
1060 Value *CreateFPExtOrFPTrunc(Value *V, Type *DestTy,
1061 const Twine &Name = "") {
1062 assert(V->getType()->isFPOrFPVectorTy() &&
1063 DestTy->isFPOrFPVectorTy() &&
1064 "Can only FPExt/FPTrunc floating point types!");
1065 Type *Ty = V->getType();
1066 if (Ty->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1067 return CreateFPExt(V, DestTy, Name);
1068 if (Ty->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1069 return CreateFPTrunc(V, DestTy, Name);
1072 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1073 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1075 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1076 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1078 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1079 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1081 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1082 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1084 Value *CreateFPTrunc(Value *V, Type *DestTy,
1085 const Twine &Name = "") {
1086 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1088 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1089 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1091 Value *CreatePtrToInt(Value *V, Type *DestTy,
1092 const Twine &Name = "") {
1093 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1095 Value *CreateIntToPtr(Value *V, Type *DestTy,
1096 const Twine &Name = "") {
1097 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1099 Value *CreateBitCast(Value *V, Type *DestTy,
1100 const Twine &Name = "") {
1101 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1103 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1104 const Twine &Name = "") {
1105 if (V->getType() == DestTy)
1107 if (Constant *VC = dyn_cast<Constant>(V))
1108 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1109 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1111 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1112 const Twine &Name = "") {
1113 if (V->getType() == DestTy)
1115 if (Constant *VC = dyn_cast<Constant>(V))
1116 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1117 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1119 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1120 const Twine &Name = "") {
1121 if (V->getType() == DestTy)
1123 if (Constant *VC = dyn_cast<Constant>(V))
1124 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1125 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1127 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1128 const Twine &Name = "") {
1129 if (V->getType() == DestTy)
1131 if (Constant *VC = dyn_cast<Constant>(V))
1132 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1133 return Insert(CastInst::Create(Op, V, DestTy), Name);
1135 Value *CreatePointerCast(Value *V, Type *DestTy,
1136 const Twine &Name = "") {
1137 if (V->getType() == DestTy)
1139 if (Constant *VC = dyn_cast<Constant>(V))
1140 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1141 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1143 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1144 const Twine &Name = "") {
1145 if (V->getType() == DestTy)
1147 if (Constant *VC = dyn_cast<Constant>(V))
1148 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1149 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1152 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1153 // compile time error, instead of converting the string to bool for the
1154 // isSigned parameter.
1155 Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
1157 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1158 if (V->getType() == DestTy)
1160 if (Constant *VC = dyn_cast<Constant>(V))
1161 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1162 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1165 //===--------------------------------------------------------------------===//
1166 // Instruction creation methods: Compare Instructions
1167 //===--------------------------------------------------------------------===//
1169 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1170 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1172 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1173 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1175 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1176 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1178 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1179 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1181 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1182 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1184 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1185 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1187 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1188 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1190 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1191 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1193 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1194 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1196 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1197 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1200 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1201 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1203 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1204 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1206 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1207 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1209 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1210 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1212 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1213 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1215 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1216 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1218 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1219 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1221 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1222 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1224 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1225 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1227 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1228 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1230 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1231 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1233 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1234 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1236 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1237 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1239 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1240 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1243 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1244 const Twine &Name = "") {
1245 if (Constant *LC = dyn_cast<Constant>(LHS))
1246 if (Constant *RC = dyn_cast<Constant>(RHS))
1247 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1248 return Insert(new ICmpInst(P, LHS, RHS), Name);
1250 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1251 const Twine &Name = "") {
1252 if (Constant *LC = dyn_cast<Constant>(LHS))
1253 if (Constant *RC = dyn_cast<Constant>(RHS))
1254 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1255 return Insert(new FCmpInst(P, LHS, RHS), Name);
1258 //===--------------------------------------------------------------------===//
1259 // Instruction creation methods: Other Instructions
1260 //===--------------------------------------------------------------------===//
1262 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1263 const Twine &Name = "") {
1264 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1267 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1268 return Insert(CallInst::Create(Callee), Name);
1270 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1271 return Insert(CallInst::Create(Callee, Arg), Name);
1273 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1274 const Twine &Name = "") {
1275 Value *Args[] = { Arg1, Arg2 };
1276 return Insert(CallInst::Create(Callee, Args), Name);
1278 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1279 const Twine &Name = "") {
1280 Value *Args[] = { Arg1, Arg2, Arg3 };
1281 return Insert(CallInst::Create(Callee, Args), Name);
1283 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1284 Value *Arg4, const Twine &Name = "") {
1285 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1286 return Insert(CallInst::Create(Callee, Args), Name);
1288 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1289 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1290 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1291 return Insert(CallInst::Create(Callee, Args), Name);
1294 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1295 const Twine &Name = "") {
1296 return Insert(CallInst::Create(Callee, Args), Name);
1299 Value *CreateSelect(Value *C, Value *True, Value *False,
1300 const Twine &Name = "") {
1301 if (Constant *CC = dyn_cast<Constant>(C))
1302 if (Constant *TC = dyn_cast<Constant>(True))
1303 if (Constant *FC = dyn_cast<Constant>(False))
1304 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1305 return Insert(SelectInst::Create(C, True, False), Name);
1308 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1309 return Insert(new VAArgInst(List, Ty), Name);
1312 Value *CreateExtractElement(Value *Vec, Value *Idx,
1313 const Twine &Name = "") {
1314 if (Constant *VC = dyn_cast<Constant>(Vec))
1315 if (Constant *IC = dyn_cast<Constant>(Idx))
1316 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1317 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1320 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1321 const Twine &Name = "") {
1322 if (Constant *VC = dyn_cast<Constant>(Vec))
1323 if (Constant *NC = dyn_cast<Constant>(NewElt))
1324 if (Constant *IC = dyn_cast<Constant>(Idx))
1325 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1326 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1329 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1330 const Twine &Name = "") {
1331 if (Constant *V1C = dyn_cast<Constant>(V1))
1332 if (Constant *V2C = dyn_cast<Constant>(V2))
1333 if (Constant *MC = dyn_cast<Constant>(Mask))
1334 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1335 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1338 Value *CreateExtractValue(Value *Agg,
1339 ArrayRef<unsigned> Idxs,
1340 const Twine &Name = "") {
1341 if (Constant *AggC = dyn_cast<Constant>(Agg))
1342 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1343 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1346 Value *CreateInsertValue(Value *Agg, Value *Val,
1347 ArrayRef<unsigned> Idxs,
1348 const Twine &Name = "") {
1349 if (Constant *AggC = dyn_cast<Constant>(Agg))
1350 if (Constant *ValC = dyn_cast<Constant>(Val))
1351 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1352 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1355 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1356 const Twine &Name = "") {
1357 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
1360 //===--------------------------------------------------------------------===//
1361 // Utility creation methods
1362 //===--------------------------------------------------------------------===//
1364 /// \brief Return an i1 value testing if \p Arg is null.
1365 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1366 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1370 /// \brief Return an i1 value testing if \p Arg is not null.
1371 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1372 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1376 /// \brief Return the i64 difference between two pointer values, dividing out
1377 /// the size of the pointed-to objects.
1379 /// This is intended to implement C-style pointer subtraction. As such, the
1380 /// pointers must be appropriately aligned for their element types and
1381 /// pointing into the same object.
1382 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1383 assert(LHS->getType() == RHS->getType() &&
1384 "Pointer subtraction operand types must match!");
1385 PointerType *ArgType = cast<PointerType>(LHS->getType());
1386 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1387 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1388 Value *Difference = CreateSub(LHS_int, RHS_int);
1389 return CreateExactSDiv(Difference,
1390 ConstantExpr::getSizeOf(ArgType->getElementType()),
1394 /// \brief Return a vector value that contains \arg V broadcasted to \p
1395 /// NumElts elements.
1396 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1397 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1399 // First insert it into an undef vector so we can shuffle it.
1400 Type *I32Ty = getInt32Ty();
1401 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1402 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1403 Name + ".splatinsert");
1405 // Shuffle the value across the desired number of elements.
1406 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1407 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");