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_IRBUILDER_H
16 #define LLVM_IRBUILDER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/BasicBlock.h"
22 #include "llvm/DataLayout.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/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 /// ClearInsertionPoint - Clear the insertion point: created instructions will
65 /// not be 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 /// SetInsertPoint - This specifies that created instructions should be
75 /// appended to the end of the specified block.
76 void SetInsertPoint(BasicBlock *TheBB) {
81 /// SetInsertPoint - This specifies that created instructions should be
82 /// inserted before the specified instruction.
83 void SetInsertPoint(Instruction *I) {
86 SetCurrentDebugLocation(I->getDebugLoc());
89 /// SetInsertPoint - This specifies that created instructions should be
90 /// inserted at the specified point.
91 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
96 /// SetInsertPoint(Use) - Find the nearest point that dominates this use, and
97 /// specify that 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 /// SetCurrentDebugLocation - Set location information used by debugging
111 void SetCurrentDebugLocation(const DebugLoc &L) {
115 /// getCurrentDebugLocation - Get location information used by debugging
117 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
119 /// SetInstDebugLocation - If this builder has a current debug location, set
120 /// it on the specified instruction.
121 void SetInstDebugLocation(Instruction *I) const {
122 if (!CurDbgLocation.isUnknown())
123 I->setDebugLoc(CurDbgLocation);
126 /// getCurrentFunctionReturnType - Get the return type of the current function
127 /// that we're emitting into.
128 Type *getCurrentFunctionReturnType() const;
130 /// InsertPoint - A saved insertion point.
133 BasicBlock::iterator Point;
136 /// Creates a new insertion point which doesn't point to anything.
137 InsertPoint() : Block(0) {}
139 /// Creates a new insertion point at the given location.
140 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
141 : Block(InsertBlock), Point(InsertPoint) {}
143 /// isSet - Returns true if this insert point is set.
144 bool isSet() const { return (Block != 0); }
146 llvm::BasicBlock *getBlock() const { return Block; }
147 llvm::BasicBlock::iterator getPoint() const { return Point; }
150 /// saveIP - Returns the current insert point.
151 InsertPoint saveIP() const {
152 return InsertPoint(GetInsertBlock(), GetInsertPoint());
155 /// saveAndClearIP - Returns the current insert point, clearing it
157 InsertPoint saveAndClearIP() {
158 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
159 ClearInsertionPoint();
163 /// restoreIP - Sets the current insert point to a previously-saved
165 void restoreIP(InsertPoint IP) {
167 SetInsertPoint(IP.getBlock(), IP.getPoint());
169 ClearInsertionPoint();
172 //===--------------------------------------------------------------------===//
173 // Miscellaneous creation methods.
174 //===--------------------------------------------------------------------===//
176 /// CreateGlobalString - Make a new global variable with an initializer that
177 /// has array of i8 type filled in with the nul terminated string value
178 /// specified. The new global variable will be marked mergable with any
179 /// others of the same contents. If Name is specified, it is the name of the
180 /// global variable created.
181 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
183 /// getInt1 - Get a constant value representing either true or false.
184 ConstantInt *getInt1(bool V) {
185 return ConstantInt::get(getInt1Ty(), V);
188 /// getTrue - Get the constant value for i1 true.
189 ConstantInt *getTrue() {
190 return ConstantInt::getTrue(Context);
193 /// getFalse - Get the constant value for i1 false.
194 ConstantInt *getFalse() {
195 return ConstantInt::getFalse(Context);
198 /// getInt8 - Get a constant 8-bit value.
199 ConstantInt *getInt8(uint8_t C) {
200 return ConstantInt::get(getInt8Ty(), C);
203 /// getInt16 - Get a constant 16-bit value.
204 ConstantInt *getInt16(uint16_t C) {
205 return ConstantInt::get(getInt16Ty(), C);
208 /// getInt32 - Get a constant 32-bit value.
209 ConstantInt *getInt32(uint32_t C) {
210 return ConstantInt::get(getInt32Ty(), C);
213 /// getInt64 - Get a constant 64-bit value.
214 ConstantInt *getInt64(uint64_t C) {
215 return ConstantInt::get(getInt64Ty(), C);
218 /// getInt - Get a constant integer value.
219 ConstantInt *getInt(const APInt &AI) {
220 return ConstantInt::get(Context, AI);
223 //===--------------------------------------------------------------------===//
224 // Type creation methods
225 //===--------------------------------------------------------------------===//
227 /// getInt1Ty - Fetch the type representing a single bit
228 IntegerType *getInt1Ty() {
229 return Type::getInt1Ty(Context);
232 /// getInt8Ty - Fetch the type representing an 8-bit integer.
233 IntegerType *getInt8Ty() {
234 return Type::getInt8Ty(Context);
237 /// getInt16Ty - Fetch the type representing a 16-bit integer.
238 IntegerType *getInt16Ty() {
239 return Type::getInt16Ty(Context);
242 /// getInt32Ty - Fetch the type representing a 32-bit integer.
243 IntegerType *getInt32Ty() {
244 return Type::getInt32Ty(Context);
247 /// getInt64Ty - Fetch the type representing a 64-bit integer.
248 IntegerType *getInt64Ty() {
249 return Type::getInt64Ty(Context);
252 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
254 return Type::getFloatTy(Context);
257 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
258 Type *getDoubleTy() {
259 return Type::getDoubleTy(Context);
262 /// getVoidTy - Fetch the type representing void.
264 return Type::getVoidTy(Context);
267 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
268 return Type::getInt8PtrTy(Context, AddrSpace);
271 IntegerType* getIntPtrTy(DataLayout *DL, unsigned AddrSpace = 0) {
272 return DL->getIntPtrType(Context, AddrSpace);
275 //===--------------------------------------------------------------------===//
276 // Intrinsic creation methods
277 //===--------------------------------------------------------------------===//
279 /// CreateMemSet - Create and insert a memset to the specified pointer and the
280 /// specified value. If the pointer isn't an i8*, it will be converted. If a
281 /// TBAA tag is specified, it will be added to the instruction.
282 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
283 bool isVolatile = false, MDNode *TBAATag = 0) {
284 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
287 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
288 bool isVolatile = false, MDNode *TBAATag = 0);
290 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
291 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
292 /// specified, it will be added to the instruction.
293 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
294 bool isVolatile = false, MDNode *TBAATag = 0,
295 MDNode *TBAAStructTag = 0) {
296 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
300 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
301 bool isVolatile = false, MDNode *TBAATag = 0,
302 MDNode *TBAAStructTag = 0);
304 /// CreateMemMove - Create and insert a memmove between the specified
305 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
306 /// tag is specified, it will be added to the instruction.
307 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
308 bool isVolatile = false, MDNode *TBAATag = 0) {
309 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
312 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
313 bool isVolatile = false, MDNode *TBAATag = 0);
315 /// CreateLifetimeStart - Create a lifetime.start intrinsic. If the pointer
316 /// isn't i8* it will be converted.
317 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
319 /// CreateLifetimeEnd - Create a lifetime.end intrinsic. If the pointer isn't
320 /// i8* it will be converted.
321 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
324 Value *getCastedInt8PtrValue(Value *Ptr);
327 /// IRBuilder - This provides a uniform API for creating instructions and
328 /// inserting them into a basic block: either at the end of a BasicBlock, or
329 /// at a specific iterator location in a block.
331 /// Note that the builder does not expose the full generality of LLVM
332 /// instructions. For access to extra instruction properties, use the mutators
333 /// (e.g. setVolatile) on the instructions after they have been
334 /// created. Convenience state exists to specify fast-math flags and fp-math
337 /// The first template argument handles whether or not to preserve names in the
338 /// final instruction output. This defaults to on. The second template argument
339 /// specifies a class to use for creating constants. This defaults to creating
340 /// minimally folded constants. The fourth template argument allows clients to
341 /// specify custom insertion hooks that are called on every newly created
343 template<bool preserveNames = true, typename T = ConstantFolder,
344 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
345 class IRBuilder : public IRBuilderBase, public Inserter {
347 MDNode *DefaultFPMathTag;
350 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
351 MDNode *FPMathTag = 0)
352 : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag),
356 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0)
357 : IRBuilderBase(C), Folder(), DefaultFPMathTag(FPMathTag), FMF() {
360 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
361 : IRBuilderBase(TheBB->getContext()), Folder(F),
362 DefaultFPMathTag(FPMathTag), FMF() {
363 SetInsertPoint(TheBB);
366 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
367 : IRBuilderBase(TheBB->getContext()), Folder(),
368 DefaultFPMathTag(FPMathTag), FMF() {
369 SetInsertPoint(TheBB);
372 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
373 : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
376 SetCurrentDebugLocation(IP->getDebugLoc());
379 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
380 : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
383 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
386 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
387 MDNode *FPMathTag = 0)
388 : IRBuilderBase(TheBB->getContext()), Folder(F),
389 DefaultFPMathTag(FPMathTag), FMF() {
390 SetInsertPoint(TheBB, IP);
393 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
394 : IRBuilderBase(TheBB->getContext()), Folder(),
395 DefaultFPMathTag(FPMathTag), FMF() {
396 SetInsertPoint(TheBB, IP);
399 /// getFolder - Get the constant folder being used.
400 const T &getFolder() { return Folder; }
402 /// getDefaultFPMathTag - Get the floating point math metadata being used.
403 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
405 /// Get the flags to be applied to created floating point ops
406 FastMathFlags getFastMathFlags() const { return FMF; }
408 /// Clear the fast-math flags.
409 void clearFastMathFlags() { FMF.clear(); }
411 /// SetDefaultFPMathTag - Set the floating point math metadata to be used.
412 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
414 /// Set the fast-math flags to be used with generated fp-math operators
415 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
417 /// isNamePreserving - Return true if this builder is configured to actually
418 /// add the requested names to IR created through it.
419 bool isNamePreserving() const { return preserveNames; }
421 /// Insert - Insert and return the specified instruction.
422 template<typename InstTy>
423 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
424 this->InsertHelper(I, Name, BB, InsertPt);
425 if (!getCurrentDebugLocation().isUnknown())
426 this->SetInstDebugLocation(I);
430 /// Insert - No-op overload to handle constants.
431 Constant *Insert(Constant *C, const Twine& = "") const {
435 //===--------------------------------------------------------------------===//
436 // Instruction creation methods: Terminators
437 //===--------------------------------------------------------------------===//
440 /// \brief Helper to add branch weight metadata onto an instruction.
441 /// \returns The annotated instruction.
442 template <typename InstTy>
443 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
445 I->setMetadata(LLVMContext::MD_prof, Weights);
450 /// CreateRetVoid - Create a 'ret void' instruction.
451 ReturnInst *CreateRetVoid() {
452 return Insert(ReturnInst::Create(Context));
456 /// CreateRet - Create a 'ret <val>' instruction.
458 ReturnInst *CreateRet(Value *V) {
459 return Insert(ReturnInst::Create(Context, V));
462 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
463 /// with one Value from the retVals array each, that build a aggregate
464 /// return value one value at a time, and a ret instruction to return
465 /// the resulting aggregate value. This is a convenience function for
466 /// code that uses aggregate return values as a vehicle for having
467 /// multiple return values.
469 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
470 Value *V = UndefValue::get(getCurrentFunctionReturnType());
471 for (unsigned i = 0; i != N; ++i)
472 V = CreateInsertValue(V, retVals[i], i, "mrv");
473 return Insert(ReturnInst::Create(Context, V));
476 /// CreateBr - Create an unconditional 'br label X' instruction.
477 BranchInst *CreateBr(BasicBlock *Dest) {
478 return Insert(BranchInst::Create(Dest));
481 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
483 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
484 MDNode *BranchWeights = 0) {
485 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
489 /// CreateSwitch - Create a switch instruction with the specified value,
490 /// default dest, and with a hint for the number of cases that will be added
491 /// (for efficient allocation).
492 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
493 MDNode *BranchWeights = 0) {
494 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
498 /// CreateIndirectBr - Create an indirect branch instruction with the
499 /// specified address operand, with an optional hint for the number of
500 /// destinations that will be added (for efficient allocation).
501 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
502 return Insert(IndirectBrInst::Create(Addr, NumDests));
505 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
506 BasicBlock *UnwindDest, const Twine &Name = "") {
507 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
508 ArrayRef<Value *>()),
511 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
512 BasicBlock *UnwindDest, Value *Arg1,
513 const Twine &Name = "") {
514 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
517 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
518 BasicBlock *UnwindDest, Value *Arg1,
519 Value *Arg2, Value *Arg3,
520 const Twine &Name = "") {
521 Value *Args[] = { Arg1, Arg2, Arg3 };
522 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
525 /// CreateInvoke - Create an invoke instruction.
526 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
527 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
528 const Twine &Name = "") {
529 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
533 ResumeInst *CreateResume(Value *Exn) {
534 return Insert(ResumeInst::Create(Exn));
537 UnreachableInst *CreateUnreachable() {
538 return Insert(new UnreachableInst(Context));
541 //===--------------------------------------------------------------------===//
542 // Instruction creation methods: Binary Operators
543 //===--------------------------------------------------------------------===//
545 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
546 Value *LHS, Value *RHS,
548 bool HasNUW, bool HasNSW) {
549 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
550 if (HasNUW) BO->setHasNoUnsignedWrap();
551 if (HasNSW) BO->setHasNoSignedWrap();
555 Instruction *AddFPMathAttributes(Instruction *I,
557 FastMathFlags FMF) const {
559 FPMathTag = DefaultFPMathTag;
561 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
562 I->setFastMathFlags(FMF);
566 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
567 bool HasNUW = false, bool HasNSW = false) {
568 if (Constant *LC = dyn_cast<Constant>(LHS))
569 if (Constant *RC = dyn_cast<Constant>(RHS))
570 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
571 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
574 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
575 return CreateAdd(LHS, RHS, Name, false, true);
577 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
578 return CreateAdd(LHS, RHS, Name, true, false);
580 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
581 MDNode *FPMathTag = 0) {
582 if (Constant *LC = dyn_cast<Constant>(LHS))
583 if (Constant *RC = dyn_cast<Constant>(RHS))
584 return Insert(Folder.CreateFAdd(LC, RC), Name);
585 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
586 FPMathTag, FMF), Name);
588 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
589 bool HasNUW = false, bool HasNSW = false) {
590 if (Constant *LC = dyn_cast<Constant>(LHS))
591 if (Constant *RC = dyn_cast<Constant>(RHS))
592 return Insert(Folder.CreateSub(LC, RC), Name);
593 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
596 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
597 return CreateSub(LHS, RHS, Name, false, true);
599 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
600 return CreateSub(LHS, RHS, Name, true, false);
602 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
603 MDNode *FPMathTag = 0) {
604 if (Constant *LC = dyn_cast<Constant>(LHS))
605 if (Constant *RC = dyn_cast<Constant>(RHS))
606 return Insert(Folder.CreateFSub(LC, RC), Name);
607 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
608 FPMathTag, FMF), Name);
610 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
611 bool HasNUW = false, bool HasNSW = false) {
612 if (Constant *LC = dyn_cast<Constant>(LHS))
613 if (Constant *RC = dyn_cast<Constant>(RHS))
614 return Insert(Folder.CreateMul(LC, RC), Name);
615 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
618 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
619 return CreateMul(LHS, RHS, Name, false, true);
621 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
622 return CreateMul(LHS, RHS, Name, true, false);
624 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
625 MDNode *FPMathTag = 0) {
626 if (Constant *LC = dyn_cast<Constant>(LHS))
627 if (Constant *RC = dyn_cast<Constant>(RHS))
628 return Insert(Folder.CreateFMul(LC, RC), Name);
629 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
630 FPMathTag, FMF), Name);
632 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
633 bool isExact = false) {
634 if (Constant *LC = dyn_cast<Constant>(LHS))
635 if (Constant *RC = dyn_cast<Constant>(RHS))
636 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
638 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
639 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
641 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
642 return CreateUDiv(LHS, RHS, Name, true);
644 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
645 bool isExact = false) {
646 if (Constant *LC = dyn_cast<Constant>(LHS))
647 if (Constant *RC = dyn_cast<Constant>(RHS))
648 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
650 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
651 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
653 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
654 return CreateSDiv(LHS, RHS, Name, true);
656 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
657 MDNode *FPMathTag = 0) {
658 if (Constant *LC = dyn_cast<Constant>(LHS))
659 if (Constant *RC = dyn_cast<Constant>(RHS))
660 return Insert(Folder.CreateFDiv(LC, RC), Name);
661 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
662 FPMathTag, FMF), Name);
664 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
665 if (Constant *LC = dyn_cast<Constant>(LHS))
666 if (Constant *RC = dyn_cast<Constant>(RHS))
667 return Insert(Folder.CreateURem(LC, RC), Name);
668 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
670 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
671 if (Constant *LC = dyn_cast<Constant>(LHS))
672 if (Constant *RC = dyn_cast<Constant>(RHS))
673 return Insert(Folder.CreateSRem(LC, RC), Name);
674 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
676 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
677 MDNode *FPMathTag = 0) {
678 if (Constant *LC = dyn_cast<Constant>(LHS))
679 if (Constant *RC = dyn_cast<Constant>(RHS))
680 return Insert(Folder.CreateFRem(LC, RC), Name);
681 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
682 FPMathTag, FMF), Name);
685 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
686 bool HasNUW = false, bool HasNSW = false) {
687 if (Constant *LC = dyn_cast<Constant>(LHS))
688 if (Constant *RC = dyn_cast<Constant>(RHS))
689 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
690 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
693 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
694 bool HasNUW = false, bool HasNSW = false) {
695 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
698 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
699 bool HasNUW = false, bool HasNSW = false) {
700 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
704 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
705 bool isExact = false) {
706 if (Constant *LC = dyn_cast<Constant>(LHS))
707 if (Constant *RC = dyn_cast<Constant>(RHS))
708 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
710 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
711 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
713 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
714 bool isExact = false) {
715 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
717 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
718 bool isExact = false) {
719 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
722 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
723 bool isExact = false) {
724 if (Constant *LC = dyn_cast<Constant>(LHS))
725 if (Constant *RC = dyn_cast<Constant>(RHS))
726 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
728 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
729 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
731 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
732 bool isExact = false) {
733 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
735 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
736 bool isExact = false) {
737 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
740 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
741 if (Constant *RC = dyn_cast<Constant>(RHS)) {
742 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
743 return LHS; // LHS & -1 -> LHS
744 if (Constant *LC = dyn_cast<Constant>(LHS))
745 return Insert(Folder.CreateAnd(LC, RC), Name);
747 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
749 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
750 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
752 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
753 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
756 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
757 if (Constant *RC = dyn_cast<Constant>(RHS)) {
758 if (RC->isNullValue())
759 return LHS; // LHS | 0 -> LHS
760 if (Constant *LC = dyn_cast<Constant>(LHS))
761 return Insert(Folder.CreateOr(LC, RC), Name);
763 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
765 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
766 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
768 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
769 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
772 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
773 if (Constant *LC = dyn_cast<Constant>(LHS))
774 if (Constant *RC = dyn_cast<Constant>(RHS))
775 return Insert(Folder.CreateXor(LC, RC), Name);
776 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
778 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
779 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
781 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
782 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
785 Value *CreateBinOp(Instruction::BinaryOps Opc,
786 Value *LHS, Value *RHS, const Twine &Name = "") {
787 if (Constant *LC = dyn_cast<Constant>(LHS))
788 if (Constant *RC = dyn_cast<Constant>(RHS))
789 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
790 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
793 Value *CreateNeg(Value *V, const Twine &Name = "",
794 bool HasNUW = false, bool HasNSW = false) {
795 if (Constant *VC = dyn_cast<Constant>(V))
796 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
797 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
798 if (HasNUW) BO->setHasNoUnsignedWrap();
799 if (HasNSW) BO->setHasNoSignedWrap();
802 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
803 return CreateNeg(V, Name, false, true);
805 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
806 return CreateNeg(V, Name, true, false);
808 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
809 if (Constant *VC = dyn_cast<Constant>(V))
810 return Insert(Folder.CreateFNeg(VC), Name);
811 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
812 FPMathTag, FMF), Name);
814 Value *CreateNot(Value *V, const Twine &Name = "") {
815 if (Constant *VC = dyn_cast<Constant>(V))
816 return Insert(Folder.CreateNot(VC), Name);
817 return Insert(BinaryOperator::CreateNot(V), Name);
820 //===--------------------------------------------------------------------===//
821 // Instruction creation methods: Memory Instructions
822 //===--------------------------------------------------------------------===//
824 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
825 const Twine &Name = "") {
826 return Insert(new AllocaInst(Ty, ArraySize), Name);
828 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
829 // converting the string to 'bool' for the isVolatile parameter.
830 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
831 return Insert(new LoadInst(Ptr), Name);
833 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
834 return Insert(new LoadInst(Ptr), Name);
836 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
837 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
839 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
840 return Insert(new StoreInst(Val, Ptr, isVolatile));
842 // Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")' correctly,
843 // instead of converting the string to 'bool' for the isVolatile parameter.
844 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
845 LoadInst *LI = CreateLoad(Ptr, Name);
846 LI->setAlignment(Align);
849 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
850 const Twine &Name = "") {
851 LoadInst *LI = CreateLoad(Ptr, Name);
852 LI->setAlignment(Align);
855 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
856 const Twine &Name = "") {
857 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
858 LI->setAlignment(Align);
861 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
862 bool isVolatile = false) {
863 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
864 SI->setAlignment(Align);
867 FenceInst *CreateFence(AtomicOrdering Ordering,
868 SynchronizationScope SynchScope = CrossThread) {
869 return Insert(new FenceInst(Context, Ordering, SynchScope));
871 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
872 AtomicOrdering Ordering,
873 SynchronizationScope SynchScope = CrossThread) {
874 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
876 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
877 AtomicOrdering Ordering,
878 SynchronizationScope SynchScope = CrossThread) {
879 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
881 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
882 const Twine &Name = "") {
883 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
884 // Every index must be constant.
886 for (i = 0, e = IdxList.size(); i != e; ++i)
887 if (!isa<Constant>(IdxList[i]))
890 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
892 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
894 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
895 const Twine &Name = "") {
896 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
897 // Every index must be constant.
899 for (i = 0, e = IdxList.size(); i != e; ++i)
900 if (!isa<Constant>(IdxList[i]))
903 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
905 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
907 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
908 if (Constant *PC = dyn_cast<Constant>(Ptr))
909 if (Constant *IC = dyn_cast<Constant>(Idx))
910 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
911 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
913 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
914 if (Constant *PC = dyn_cast<Constant>(Ptr))
915 if (Constant *IC = dyn_cast<Constant>(Idx))
916 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
917 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
919 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
920 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
922 if (Constant *PC = dyn_cast<Constant>(Ptr))
923 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
925 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
927 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
928 const Twine &Name = "") {
929 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
931 if (Constant *PC = dyn_cast<Constant>(Ptr))
932 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
934 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
936 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
937 const Twine &Name = "") {
939 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
940 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
943 if (Constant *PC = dyn_cast<Constant>(Ptr))
944 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
946 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
948 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
949 const Twine &Name = "") {
951 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
952 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
955 if (Constant *PC = dyn_cast<Constant>(Ptr))
956 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
958 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
960 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
961 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
963 if (Constant *PC = dyn_cast<Constant>(Ptr))
964 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
966 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
968 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
969 const Twine &Name = "") {
970 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
972 if (Constant *PC = dyn_cast<Constant>(Ptr))
973 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
975 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
977 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
978 const Twine &Name = "") {
980 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
981 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
984 if (Constant *PC = dyn_cast<Constant>(Ptr))
985 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
987 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
989 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
990 const Twine &Name = "") {
992 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
993 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
996 if (Constant *PC = dyn_cast<Constant>(Ptr))
997 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
999 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1001 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1002 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
1005 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
1006 /// with "i8*" type instead of a pointer to array of i8.
1007 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
1008 Value *gv = CreateGlobalString(Str, Name);
1009 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1010 Value *Args[] = { zero, zero };
1011 return CreateInBoundsGEP(gv, Args, Name);
1014 //===--------------------------------------------------------------------===//
1015 // Instruction creation methods: Cast/Conversion Operators
1016 //===--------------------------------------------------------------------===//
1018 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1019 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1021 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1022 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1024 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1025 return CreateCast(Instruction::SExt, V, DestTy, Name);
1027 /// CreateZExtOrTrunc - Create a ZExt or Trunc from the integer value V to
1028 /// DestTy. Return the value untouched if the type of V is already DestTy.
1029 Value *CreateZExtOrTrunc(Value *V, IntegerType *DestTy,
1030 const Twine &Name = "") {
1031 assert(isa<IntegerType>(V->getType()) && "Can only zero extend integers!");
1032 IntegerType *IntTy = cast<IntegerType>(V->getType());
1033 if (IntTy->getBitWidth() < DestTy->getBitWidth())
1034 return CreateZExt(V, DestTy, Name);
1035 if (IntTy->getBitWidth() > DestTy->getBitWidth())
1036 return CreateTrunc(V, DestTy, Name);
1039 /// CreateSExtOrTrunc - Create a SExt or Trunc from the integer value V to
1040 /// DestTy. Return the value untouched if the type of V is already DestTy.
1041 Value *CreateSExtOrTrunc(Value *V, IntegerType *DestTy,
1042 const Twine &Name = "") {
1043 assert(isa<IntegerType>(V->getType()) && "Can only sign extend integers!");
1044 IntegerType *IntTy = cast<IntegerType>(V->getType());
1045 if (IntTy->getBitWidth() < DestTy->getBitWidth())
1046 return CreateSExt(V, DestTy, Name);
1047 if (IntTy->getBitWidth() > DestTy->getBitWidth())
1048 return CreateTrunc(V, DestTy, Name);
1051 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1052 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1054 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1055 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1057 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1058 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1060 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1061 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1063 Value *CreateFPTrunc(Value *V, Type *DestTy,
1064 const Twine &Name = "") {
1065 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1067 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1068 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1070 Value *CreatePtrToInt(Value *V, Type *DestTy,
1071 const Twine &Name = "") {
1072 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1074 Value *CreateIntToPtr(Value *V, Type *DestTy,
1075 const Twine &Name = "") {
1076 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1078 Value *CreateBitCast(Value *V, Type *DestTy,
1079 const Twine &Name = "") {
1080 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1082 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1083 const Twine &Name = "") {
1084 if (V->getType() == DestTy)
1086 if (Constant *VC = dyn_cast<Constant>(V))
1087 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1088 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1090 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1091 const Twine &Name = "") {
1092 if (V->getType() == DestTy)
1094 if (Constant *VC = dyn_cast<Constant>(V))
1095 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1096 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1098 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1099 const Twine &Name = "") {
1100 if (V->getType() == DestTy)
1102 if (Constant *VC = dyn_cast<Constant>(V))
1103 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1104 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1106 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1107 const Twine &Name = "") {
1108 if (V->getType() == DestTy)
1110 if (Constant *VC = dyn_cast<Constant>(V))
1111 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1112 return Insert(CastInst::Create(Op, V, DestTy), Name);
1114 Value *CreatePointerCast(Value *V, Type *DestTy,
1115 const Twine &Name = "") {
1116 if (V->getType() == DestTy)
1118 if (Constant *VC = dyn_cast<Constant>(V))
1119 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1120 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1122 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1123 const Twine &Name = "") {
1124 if (V->getType() == DestTy)
1126 if (Constant *VC = dyn_cast<Constant>(V))
1127 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1128 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1131 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
1132 // error, instead of converting the string to bool for the isSigned parameter.
1133 Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
1135 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1136 if (V->getType() == DestTy)
1138 if (Constant *VC = dyn_cast<Constant>(V))
1139 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1140 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1143 //===--------------------------------------------------------------------===//
1144 // Instruction creation methods: Compare Instructions
1145 //===--------------------------------------------------------------------===//
1147 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1148 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1150 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1151 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1153 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1154 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1156 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1157 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1159 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1160 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1162 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1163 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1165 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1166 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1168 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1169 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1171 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1172 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1174 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1175 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1178 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1179 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1181 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1182 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1184 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1185 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1187 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1188 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1190 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1191 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1193 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1194 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1196 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1197 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1199 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1200 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1202 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1203 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1205 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1206 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1208 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1209 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1211 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1212 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1214 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1215 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1217 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1218 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1221 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1222 const Twine &Name = "") {
1223 if (Constant *LC = dyn_cast<Constant>(LHS))
1224 if (Constant *RC = dyn_cast<Constant>(RHS))
1225 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1226 return Insert(new ICmpInst(P, LHS, RHS), Name);
1228 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1229 const Twine &Name = "") {
1230 if (Constant *LC = dyn_cast<Constant>(LHS))
1231 if (Constant *RC = dyn_cast<Constant>(RHS))
1232 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1233 return Insert(new FCmpInst(P, LHS, RHS), Name);
1236 //===--------------------------------------------------------------------===//
1237 // Instruction creation methods: Other Instructions
1238 //===--------------------------------------------------------------------===//
1240 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1241 const Twine &Name = "") {
1242 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1245 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1246 return Insert(CallInst::Create(Callee), Name);
1248 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1249 return Insert(CallInst::Create(Callee, Arg), Name);
1251 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1252 const Twine &Name = "") {
1253 Value *Args[] = { Arg1, Arg2 };
1254 return Insert(CallInst::Create(Callee, Args), Name);
1256 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1257 const Twine &Name = "") {
1258 Value *Args[] = { Arg1, Arg2, Arg3 };
1259 return Insert(CallInst::Create(Callee, Args), Name);
1261 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1262 Value *Arg4, const Twine &Name = "") {
1263 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1264 return Insert(CallInst::Create(Callee, Args), Name);
1266 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1267 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1268 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1269 return Insert(CallInst::Create(Callee, Args), Name);
1272 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1273 const Twine &Name = "") {
1274 return Insert(CallInst::Create(Callee, Args), Name);
1277 Value *CreateSelect(Value *C, Value *True, Value *False,
1278 const Twine &Name = "") {
1279 if (Constant *CC = dyn_cast<Constant>(C))
1280 if (Constant *TC = dyn_cast<Constant>(True))
1281 if (Constant *FC = dyn_cast<Constant>(False))
1282 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1283 return Insert(SelectInst::Create(C, True, False), Name);
1286 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1287 return Insert(new VAArgInst(List, Ty), Name);
1290 Value *CreateExtractElement(Value *Vec, Value *Idx,
1291 const Twine &Name = "") {
1292 if (Constant *VC = dyn_cast<Constant>(Vec))
1293 if (Constant *IC = dyn_cast<Constant>(Idx))
1294 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1295 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1298 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1299 const Twine &Name = "") {
1300 if (Constant *VC = dyn_cast<Constant>(Vec))
1301 if (Constant *NC = dyn_cast<Constant>(NewElt))
1302 if (Constant *IC = dyn_cast<Constant>(Idx))
1303 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1304 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1307 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1308 const Twine &Name = "") {
1309 if (Constant *V1C = dyn_cast<Constant>(V1))
1310 if (Constant *V2C = dyn_cast<Constant>(V2))
1311 if (Constant *MC = dyn_cast<Constant>(Mask))
1312 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1313 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1316 Value *CreateExtractValue(Value *Agg,
1317 ArrayRef<unsigned> Idxs,
1318 const Twine &Name = "") {
1319 if (Constant *AggC = dyn_cast<Constant>(Agg))
1320 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1321 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1324 Value *CreateInsertValue(Value *Agg, Value *Val,
1325 ArrayRef<unsigned> Idxs,
1326 const Twine &Name = "") {
1327 if (Constant *AggC = dyn_cast<Constant>(Agg))
1328 if (Constant *ValC = dyn_cast<Constant>(Val))
1329 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1330 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1333 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1334 const Twine &Name = "") {
1335 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses, Name));
1338 //===--------------------------------------------------------------------===//
1339 // Utility creation methods
1340 //===--------------------------------------------------------------------===//
1342 /// CreateIsNull - Return an i1 value testing if \p Arg is null.
1343 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1344 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1348 /// CreateIsNotNull - Return an i1 value testing if \p Arg is not null.
1349 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1350 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1354 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1355 /// dividing out the size of the pointed-to objects. This is intended to
1356 /// implement C-style pointer subtraction. As such, the pointers must be
1357 /// appropriately aligned for their element types and pointing into the
1359 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1360 assert(LHS->getType() == RHS->getType() &&
1361 "Pointer subtraction operand types must match!");
1362 PointerType *ArgType = cast<PointerType>(LHS->getType());
1363 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1364 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1365 Value *Difference = CreateSub(LHS_int, RHS_int);
1366 return CreateExactSDiv(Difference,
1367 ConstantExpr::getSizeOf(ArgType->getElementType()),