1 //===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- 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_SUPPORT_IRBUILDER_H
16 #define LLVM_SUPPORT_IRBUILDER_H
18 #include "llvm/Instructions.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/ConstantFolder.h"
28 /// IRBuilderDefaultInserter - This provides the default implementation of the
29 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
30 /// created by IRBuilder and needs to be inserted. By default, this inserts the
31 /// instruction at the insertion point.
32 template <bool preserveNames = true>
33 class IRBuilderDefaultInserter {
35 void InsertHelper(Instruction *I, const Twine &Name,
36 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
37 if (BB) BB->getInstList().insert(InsertPt, I);
43 /// IRBuilderBase - Common base class shared among various IRBuilders.
45 DebugLoc CurDbgLocation;
48 BasicBlock::iterator InsertPt;
52 IRBuilderBase(LLVMContext &context)
54 ClearInsertionPoint();
57 //===--------------------------------------------------------------------===//
58 // Builder configuration methods
59 //===--------------------------------------------------------------------===//
61 /// ClearInsertionPoint - Clear the insertion point: created instructions will
62 /// not be inserted into a block.
63 void ClearInsertionPoint() {
67 BasicBlock *GetInsertBlock() const { return BB; }
68 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
69 LLVMContext &getContext() const { return Context; }
71 /// SetInsertPoint - This specifies that created instructions should be
72 /// appended to the end of the specified block.
73 void SetInsertPoint(BasicBlock *TheBB) {
78 /// SetInsertPoint - This specifies that created instructions should be
79 /// inserted before the specified instruction.
80 void SetInsertPoint(Instruction *I) {
85 /// SetInsertPoint - This specifies that created instructions should be
86 /// inserted at the specified point.
87 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
92 /// SetCurrentDebugLocation - Set location information used by debugging
94 void SetCurrentDebugLocation(const DebugLoc &L) {
98 /// getCurrentDebugLocation - Get location information used by debugging
100 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
102 /// SetInstDebugLocation - If this builder has a current debug location, set
103 /// it on the specified instruction.
104 void SetInstDebugLocation(Instruction *I) const {
105 if (!CurDbgLocation.isUnknown())
106 I->setDebugLoc(CurDbgLocation);
109 /// InsertPoint - A saved insertion point.
112 BasicBlock::iterator Point;
115 /// Creates a new insertion point which doesn't point to anything.
116 InsertPoint() : Block(0) {}
118 /// Creates a new insertion point at the given location.
119 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
120 : Block(InsertBlock), Point(InsertPoint) {}
122 /// isSet - Returns true if this insert point is set.
123 bool isSet() const { return (Block != 0); }
125 llvm::BasicBlock *getBlock() const { return Block; }
126 llvm::BasicBlock::iterator getPoint() const { return Point; }
129 /// saveIP - Returns the current insert point.
130 InsertPoint saveIP() const {
131 return InsertPoint(GetInsertBlock(), GetInsertPoint());
134 /// saveAndClearIP - Returns the current insert point, clearing it
136 InsertPoint saveAndClearIP() {
137 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
138 ClearInsertionPoint();
142 /// restoreIP - Sets the current insert point to a previously-saved
144 void restoreIP(InsertPoint IP) {
146 SetInsertPoint(IP.getBlock(), IP.getPoint());
148 ClearInsertionPoint();
151 //===--------------------------------------------------------------------===//
152 // Miscellaneous creation methods.
153 //===--------------------------------------------------------------------===//
155 /// CreateGlobalString - Make a new global variable with an initializer that
156 /// has array of i8 type filled in with the nul terminated string value
157 /// specified. The new global variable will be marked mergable with any
158 /// others of the same contents. If Name is specified, it is the name of the
159 /// global variable created.
160 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
162 /// getInt1 - Get a constant value representing either true or false.
163 ConstantInt *getInt1(bool V) {
164 return ConstantInt::get(getInt1Ty(), V);
167 /// getTrue - Get the constant value for i1 true.
168 ConstantInt *getTrue() {
169 return ConstantInt::getTrue(Context);
172 /// getFalse - Get the constant value for i1 false.
173 ConstantInt *getFalse() {
174 return ConstantInt::getFalse(Context);
177 /// getInt8 - Get a constant 8-bit value.
178 ConstantInt *getInt8(uint8_t C) {
179 return ConstantInt::get(getInt8Ty(), C);
182 /// getInt16 - Get a constant 16-bit value.
183 ConstantInt *getInt16(uint16_t C) {
184 return ConstantInt::get(getInt16Ty(), C);
187 /// getInt32 - Get a constant 32-bit value.
188 ConstantInt *getInt32(uint32_t C) {
189 return ConstantInt::get(getInt32Ty(), C);
192 /// getInt64 - Get a constant 64-bit value.
193 ConstantInt *getInt64(uint64_t C) {
194 return ConstantInt::get(getInt64Ty(), C);
197 ConstantInt *getInt(const APInt &AI) {
198 return ConstantInt::get(Context, AI);
201 //===--------------------------------------------------------------------===//
202 // Type creation methods
203 //===--------------------------------------------------------------------===//
205 /// getInt1Ty - Fetch the type representing a single bit
206 const IntegerType *getInt1Ty() {
207 return Type::getInt1Ty(Context);
210 /// getInt8Ty - Fetch the type representing an 8-bit integer.
211 const IntegerType *getInt8Ty() {
212 return Type::getInt8Ty(Context);
215 /// getInt16Ty - Fetch the type representing a 16-bit integer.
216 const IntegerType *getInt16Ty() {
217 return Type::getInt16Ty(Context);
220 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
221 const IntegerType *getInt32Ty() {
222 return Type::getInt32Ty(Context);
225 /// getInt64Ty - Fetch the type representing a 64-bit integer.
226 const IntegerType *getInt64Ty() {
227 return Type::getInt64Ty(Context);
230 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
231 const Type *getFloatTy() {
232 return Type::getFloatTy(Context);
235 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
236 const Type *getDoubleTy() {
237 return Type::getDoubleTy(Context);
240 /// getVoidTy - Fetch the type representing void.
241 const Type *getVoidTy() {
242 return Type::getVoidTy(Context);
245 const PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
246 return Type::getInt8PtrTy(Context, AddrSpace);
249 /// getCurrentFunctionReturnType - Get the return type of the current function
250 /// that we're emitting into.
251 const Type *getCurrentFunctionReturnType() const;
253 /// CreateMemSet - Create and insert a memset to the specified pointer and the
254 /// specified value. If the pointer isn't an i8*, it will be converted. If a
255 /// TBAA tag is specified, it will be added to the instruction.
256 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
257 bool isVolatile = false, MDNode *TBAATag = 0) {
258 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
261 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
262 bool isVolatile = false, MDNode *TBAATag = 0);
264 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
265 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
266 /// specified, it will be added to the instruction.
267 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
268 bool isVolatile = false, MDNode *TBAATag = 0) {
269 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
272 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
273 bool isVolatile = false, MDNode *TBAATag = 0);
275 /// CreateMemMove - Create and insert a memmove between the specified
276 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
277 /// tag is specified, it will be added to the instruction.
278 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
279 bool isVolatile = false, MDNode *TBAATag = 0) {
280 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
283 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
284 bool isVolatile = false, MDNode *TBAATag = 0);
286 Value *getCastedInt8PtrValue(Value *Ptr);
289 /// IRBuilder - This provides a uniform API for creating instructions and
290 /// inserting them into a basic block: either at the end of a BasicBlock, or
291 /// at a specific iterator location in a block.
293 /// Note that the builder does not expose the full generality of LLVM
294 /// instructions. For access to extra instruction properties, use the mutators
295 /// (e.g. setVolatile) on the instructions after they have been created.
296 /// The first template argument handles whether or not to preserve names in the
297 /// final instruction output. This defaults to on. The second template argument
298 /// specifies a class to use for creating constants. This defaults to creating
299 /// minimally folded constants. The fourth template argument allows clients to
300 /// specify custom insertion hooks that are called on every newly created
302 template<bool preserveNames = true, typename T = ConstantFolder,
303 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
304 class IRBuilder : public IRBuilderBase, public Inserter {
307 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
308 : IRBuilderBase(C), Inserter(I), Folder(F) {
311 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder() {
314 explicit IRBuilder(BasicBlock *TheBB, const T &F)
315 : IRBuilderBase(TheBB->getContext()), Folder(F) {
316 SetInsertPoint(TheBB);
319 explicit IRBuilder(BasicBlock *TheBB)
320 : IRBuilderBase(TheBB->getContext()), Folder() {
321 SetInsertPoint(TheBB);
324 explicit IRBuilder(Instruction *IP)
325 : IRBuilderBase(IP->getContext()), Folder() {
329 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
330 : IRBuilderBase(TheBB->getContext()), Folder(F) {
331 SetInsertPoint(TheBB, IP);
334 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
335 : IRBuilderBase(TheBB->getContext()), Folder() {
336 SetInsertPoint(TheBB, IP);
339 /// getFolder - Get the constant folder being used.
340 const T &getFolder() { return Folder; }
342 /// isNamePreserving - Return true if this builder is configured to actually
343 /// add the requested names to IR created through it.
344 bool isNamePreserving() const { return preserveNames; }
346 /// Insert - Insert and return the specified instruction.
347 template<typename InstTy>
348 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
349 this->InsertHelper(I, Name, BB, InsertPt);
350 if (!getCurrentDebugLocation().isUnknown())
351 this->SetInstDebugLocation(I);
355 /// Insert - No-op overload to handle constants.
356 Constant *Insert(Constant *C, const Twine& = "") const {
360 //===--------------------------------------------------------------------===//
361 // Instruction creation methods: Terminators
362 //===--------------------------------------------------------------------===//
364 /// CreateRetVoid - Create a 'ret void' instruction.
365 ReturnInst *CreateRetVoid() {
366 return Insert(ReturnInst::Create(Context));
370 /// CreateRet - Create a 'ret <val>' instruction.
372 ReturnInst *CreateRet(Value *V) {
373 return Insert(ReturnInst::Create(Context, V));
376 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
377 /// with one Value from the retVals array each, that build a aggregate
378 /// return value one value at a time, and a ret instruction to return
379 /// the resulting aggregate value. This is a convenience function for
380 /// code that uses aggregate return values as a vehicle for having
381 /// multiple return values.
383 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
384 Value *V = UndefValue::get(getCurrentFunctionReturnType());
385 for (unsigned i = 0; i != N; ++i)
386 V = CreateInsertValue(V, retVals[i], i, "mrv");
387 return Insert(ReturnInst::Create(Context, V));
390 /// CreateBr - Create an unconditional 'br label X' instruction.
391 BranchInst *CreateBr(BasicBlock *Dest) {
392 return Insert(BranchInst::Create(Dest));
395 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
397 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
398 return Insert(BranchInst::Create(True, False, Cond));
401 /// CreateSwitch - Create a switch instruction with the specified value,
402 /// default dest, and with a hint for the number of cases that will be added
403 /// (for efficient allocation).
404 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
405 return Insert(SwitchInst::Create(V, Dest, NumCases));
408 /// CreateIndirectBr - Create an indirect branch instruction with the
409 /// specified address operand, with an optional hint for the number of
410 /// destinations that will be added (for efficient allocation).
411 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
412 return Insert(IndirectBrInst::Create(Addr, NumDests));
415 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
416 BasicBlock *UnwindDest, const Twine &Name = "") {
417 Value *Args[] = { 0 };
418 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
421 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
422 BasicBlock *UnwindDest, Value *Arg1,
423 const Twine &Name = "") {
424 Value *Args[] = { Arg1 };
425 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
428 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
429 BasicBlock *UnwindDest, Value *Arg1,
430 Value *Arg2, Value *Arg3,
431 const Twine &Name = "") {
432 Value *Args[] = { Arg1, Arg2, Arg3 };
433 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
436 /// CreateInvoke - Create an invoke instruction.
437 template<typename RandomAccessIterator>
438 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
439 BasicBlock *UnwindDest,
440 RandomAccessIterator ArgBegin,
441 RandomAccessIterator ArgEnd,
442 const Twine &Name = "") {
443 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
444 ArgBegin, ArgEnd), Name);
447 UnwindInst *CreateUnwind() {
448 return Insert(new UnwindInst(Context));
451 UnreachableInst *CreateUnreachable() {
452 return Insert(new UnreachableInst(Context));
455 //===--------------------------------------------------------------------===//
456 // Instruction creation methods: Binary Operators
457 //===--------------------------------------------------------------------===//
459 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
460 Value *LHS, Value *RHS,
462 bool HasNUW, bool HasNSW) {
463 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
464 if (HasNUW) BO->setHasNoUnsignedWrap();
465 if (HasNSW) BO->setHasNoSignedWrap();
469 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
470 bool HasNUW = false, bool HasNSW = false) {
471 if (Constant *LC = dyn_cast<Constant>(LHS))
472 if (Constant *RC = dyn_cast<Constant>(RHS))
473 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
474 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
477 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
478 return CreateAdd(LHS, RHS, Name, false, true);
480 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
481 return CreateAdd(LHS, RHS, Name, true, false);
483 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
484 if (Constant *LC = dyn_cast<Constant>(LHS))
485 if (Constant *RC = dyn_cast<Constant>(RHS))
486 return Insert(Folder.CreateFAdd(LC, RC), Name);
487 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
489 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
490 bool HasNUW = false, bool HasNSW = false) {
491 if (Constant *LC = dyn_cast<Constant>(LHS))
492 if (Constant *RC = dyn_cast<Constant>(RHS))
493 return Insert(Folder.CreateSub(LC, RC), Name);
494 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
497 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
498 return CreateSub(LHS, RHS, Name, false, true);
500 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
501 return CreateSub(LHS, RHS, Name, true, false);
503 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
504 if (Constant *LC = dyn_cast<Constant>(LHS))
505 if (Constant *RC = dyn_cast<Constant>(RHS))
506 return Insert(Folder.CreateFSub(LC, RC), Name);
507 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
509 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
510 bool HasNUW = false, bool HasNSW = false) {
511 if (Constant *LC = dyn_cast<Constant>(LHS))
512 if (Constant *RC = dyn_cast<Constant>(RHS))
513 return Insert(Folder.CreateMul(LC, RC), Name);
514 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
517 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
518 return CreateMul(LHS, RHS, Name, false, true);
520 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
521 return CreateMul(LHS, RHS, Name, true, false);
523 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
524 if (Constant *LC = dyn_cast<Constant>(LHS))
525 if (Constant *RC = dyn_cast<Constant>(RHS))
526 return Insert(Folder.CreateFMul(LC, RC), Name);
527 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
529 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
530 bool isExact = false) {
531 if (Constant *LC = dyn_cast<Constant>(LHS))
532 if (Constant *RC = dyn_cast<Constant>(RHS))
533 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
535 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
536 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
538 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
539 return CreateUDiv(LHS, RHS, Name, true);
541 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
542 bool isExact = false) {
543 if (Constant *LC = dyn_cast<Constant>(LHS))
544 if (Constant *RC = dyn_cast<Constant>(RHS))
545 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
547 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
548 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
550 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
551 return CreateSDiv(LHS, RHS, Name, true);
553 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
554 if (Constant *LC = dyn_cast<Constant>(LHS))
555 if (Constant *RC = dyn_cast<Constant>(RHS))
556 return Insert(Folder.CreateFDiv(LC, RC), Name);
557 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
559 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
560 if (Constant *LC = dyn_cast<Constant>(LHS))
561 if (Constant *RC = dyn_cast<Constant>(RHS))
562 return Insert(Folder.CreateURem(LC, RC), Name);
563 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
565 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
566 if (Constant *LC = dyn_cast<Constant>(LHS))
567 if (Constant *RC = dyn_cast<Constant>(RHS))
568 return Insert(Folder.CreateSRem(LC, RC), Name);
569 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
571 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
572 if (Constant *LC = dyn_cast<Constant>(LHS))
573 if (Constant *RC = dyn_cast<Constant>(RHS))
574 return Insert(Folder.CreateFRem(LC, RC), Name);
575 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
578 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
579 bool HasNUW = false, bool HasNSW = false) {
580 if (Constant *LC = dyn_cast<Constant>(LHS))
581 if (Constant *RC = dyn_cast<Constant>(RHS))
582 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
583 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
586 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
587 bool HasNUW = false, bool HasNSW = false) {
588 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
591 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
592 bool HasNUW = false, bool HasNSW = false) {
593 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
597 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
598 bool isExact = false) {
599 if (Constant *LC = dyn_cast<Constant>(LHS))
600 if (Constant *RC = dyn_cast<Constant>(RHS))
601 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
603 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
604 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
606 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
607 bool isExact = false) {
608 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
610 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
611 bool isExact = false) {
612 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
615 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
616 bool isExact = false) {
617 if (Constant *LC = dyn_cast<Constant>(LHS))
618 if (Constant *RC = dyn_cast<Constant>(RHS))
619 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
621 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
622 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
624 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
625 bool isExact = false) {
626 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
628 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
629 bool isExact = false) {
630 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
633 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
634 if (Constant *RC = dyn_cast<Constant>(RHS)) {
635 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
636 return LHS; // LHS & -1 -> LHS
637 if (Constant *LC = dyn_cast<Constant>(LHS))
638 return Insert(Folder.CreateAnd(LC, RC), Name);
640 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
642 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
643 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
645 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
646 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
649 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
650 if (Constant *RC = dyn_cast<Constant>(RHS)) {
651 if (RC->isNullValue())
652 return LHS; // LHS | 0 -> LHS
653 if (Constant *LC = dyn_cast<Constant>(LHS))
654 return Insert(Folder.CreateOr(LC, RC), Name);
656 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
658 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
659 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
661 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
662 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
665 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
666 if (Constant *LC = dyn_cast<Constant>(LHS))
667 if (Constant *RC = dyn_cast<Constant>(RHS))
668 return Insert(Folder.CreateXor(LC, RC), Name);
669 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
671 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
672 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
674 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
675 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
678 Value *CreateBinOp(Instruction::BinaryOps Opc,
679 Value *LHS, Value *RHS, const Twine &Name = "") {
680 if (Constant *LC = dyn_cast<Constant>(LHS))
681 if (Constant *RC = dyn_cast<Constant>(RHS))
682 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
683 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
686 Value *CreateNeg(Value *V, const Twine &Name = "",
687 bool HasNUW = false, bool HasNSW = false) {
688 if (Constant *VC = dyn_cast<Constant>(V))
689 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
690 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
691 if (HasNUW) BO->setHasNoUnsignedWrap();
692 if (HasNSW) BO->setHasNoSignedWrap();
695 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
696 return CreateNeg(V, Name, false, true);
698 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
699 return CreateNeg(V, Name, true, false);
701 Value *CreateFNeg(Value *V, const Twine &Name = "") {
702 if (Constant *VC = dyn_cast<Constant>(V))
703 return Insert(Folder.CreateFNeg(VC), Name);
704 return Insert(BinaryOperator::CreateFNeg(V), Name);
706 Value *CreateNot(Value *V, const Twine &Name = "") {
707 if (Constant *VC = dyn_cast<Constant>(V))
708 return Insert(Folder.CreateNot(VC), Name);
709 return Insert(BinaryOperator::CreateNot(V), Name);
712 //===--------------------------------------------------------------------===//
713 // Instruction creation methods: Memory Instructions
714 //===--------------------------------------------------------------------===//
716 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
717 const Twine &Name = "") {
718 return Insert(new AllocaInst(Ty, ArraySize), Name);
720 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
721 // converting the string to 'bool' for the isVolatile parameter.
722 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
723 return Insert(new LoadInst(Ptr), Name);
725 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
726 return Insert(new LoadInst(Ptr), Name);
728 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
729 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
731 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
732 return Insert(new StoreInst(Val, Ptr, isVolatile));
734 template<typename RandomAccessIterator>
735 Value *CreateGEP(Value *Ptr,
736 RandomAccessIterator IdxBegin,
737 RandomAccessIterator IdxEnd,
738 const Twine &Name = "") {
739 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
740 // Every index must be constant.
741 RandomAccessIterator i;
742 for (i = IdxBegin; i < IdxEnd; ++i)
743 if (!isa<Constant>(*i))
746 return Insert(Folder.CreateGetElementPtr(PC, &IdxBegin[0],
750 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
752 template<typename RandomAccessIterator>
753 Value *CreateInBoundsGEP(Value *Ptr, RandomAccessIterator IdxBegin,
754 RandomAccessIterator IdxEnd,
755 const Twine &Name = "") {
756 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
757 // Every index must be constant.
758 RandomAccessIterator i;
759 for (i = IdxBegin; i < IdxEnd; ++i)
760 if (!isa<Constant>(*i))
763 return Insert(Folder.CreateInBoundsGetElementPtr(PC,
768 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
771 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
772 if (Constant *PC = dyn_cast<Constant>(Ptr))
773 if (Constant *IC = dyn_cast<Constant>(Idx))
774 return Insert(Folder.CreateGetElementPtr(PC, &IC, 1), Name);
775 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
777 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
778 if (Constant *PC = dyn_cast<Constant>(Ptr))
779 if (Constant *IC = dyn_cast<Constant>(Idx))
780 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &IC, 1), Name);
781 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
783 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
784 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
786 if (Constant *PC = dyn_cast<Constant>(Ptr))
787 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
789 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
791 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
792 const Twine &Name = "") {
793 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
795 if (Constant *PC = dyn_cast<Constant>(Ptr))
796 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
798 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
800 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
801 const Twine &Name = "") {
803 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
804 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
807 if (Constant *PC = dyn_cast<Constant>(Ptr))
808 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
810 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
812 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
813 const Twine &Name = "") {
815 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
816 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
819 if (Constant *PC = dyn_cast<Constant>(Ptr))
820 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
822 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
824 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
825 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
827 if (Constant *PC = dyn_cast<Constant>(Ptr))
828 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
830 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
832 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
833 const Twine &Name = "") {
834 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
836 if (Constant *PC = dyn_cast<Constant>(Ptr))
837 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
839 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
841 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
842 const Twine &Name = "") {
844 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
845 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
848 if (Constant *PC = dyn_cast<Constant>(Ptr))
849 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
851 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
853 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
854 const Twine &Name = "") {
856 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
857 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
860 if (Constant *PC = dyn_cast<Constant>(Ptr))
861 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
863 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
865 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
866 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
869 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
870 /// with "i8*" type instead of a pointer to array of i8.
871 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
872 Value *gv = CreateGlobalString(Str, Name);
873 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
874 Value *Args[] = { zero, zero };
875 return CreateInBoundsGEP(gv, Args, Args+2, Name);
878 //===--------------------------------------------------------------------===//
879 // Instruction creation methods: Cast/Conversion Operators
880 //===--------------------------------------------------------------------===//
882 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
883 return CreateCast(Instruction::Trunc, V, DestTy, Name);
885 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
886 return CreateCast(Instruction::ZExt, V, DestTy, Name);
888 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
889 return CreateCast(Instruction::SExt, V, DestTy, Name);
891 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
892 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
894 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
895 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
897 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
898 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
900 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
901 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
903 Value *CreateFPTrunc(Value *V, const Type *DestTy,
904 const Twine &Name = "") {
905 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
907 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
908 return CreateCast(Instruction::FPExt, V, DestTy, Name);
910 Value *CreatePtrToInt(Value *V, const Type *DestTy,
911 const Twine &Name = "") {
912 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
914 Value *CreateIntToPtr(Value *V, const Type *DestTy,
915 const Twine &Name = "") {
916 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
918 Value *CreateBitCast(Value *V, const Type *DestTy,
919 const Twine &Name = "") {
920 return CreateCast(Instruction::BitCast, V, DestTy, Name);
922 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
923 const Twine &Name = "") {
924 if (V->getType() == DestTy)
926 if (Constant *VC = dyn_cast<Constant>(V))
927 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
928 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
930 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
931 const Twine &Name = "") {
932 if (V->getType() == DestTy)
934 if (Constant *VC = dyn_cast<Constant>(V))
935 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
936 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
938 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
939 const Twine &Name = "") {
940 if (V->getType() == DestTy)
942 if (Constant *VC = dyn_cast<Constant>(V))
943 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
944 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
946 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
947 const Twine &Name = "") {
948 if (V->getType() == DestTy)
950 if (Constant *VC = dyn_cast<Constant>(V))
951 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
952 return Insert(CastInst::Create(Op, V, DestTy), Name);
954 Value *CreatePointerCast(Value *V, const Type *DestTy,
955 const Twine &Name = "") {
956 if (V->getType() == DestTy)
958 if (Constant *VC = dyn_cast<Constant>(V))
959 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
960 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
962 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
963 const Twine &Name = "") {
964 if (V->getType() == DestTy)
966 if (Constant *VC = dyn_cast<Constant>(V))
967 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
968 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
971 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
972 // error, instead of converting the string to bool for the isSigned parameter.
973 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
975 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
976 if (V->getType() == DestTy)
978 if (Constant *VC = dyn_cast<Constant>(V))
979 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
980 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
983 //===--------------------------------------------------------------------===//
984 // Instruction creation methods: Compare Instructions
985 //===--------------------------------------------------------------------===//
987 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
988 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
990 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
991 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
993 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
994 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
996 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
997 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
999 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1000 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1002 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1003 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1005 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1006 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1008 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1009 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1011 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1012 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1014 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1015 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1018 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1019 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1021 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1022 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1024 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1025 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1027 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1028 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1030 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1031 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1033 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1034 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1036 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1037 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1039 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1040 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1042 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1043 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1045 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1046 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1048 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1049 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1051 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1052 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1054 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1055 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1057 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1058 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1061 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1062 const Twine &Name = "") {
1063 if (Constant *LC = dyn_cast<Constant>(LHS))
1064 if (Constant *RC = dyn_cast<Constant>(RHS))
1065 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1066 return Insert(new ICmpInst(P, LHS, RHS), Name);
1068 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1069 const Twine &Name = "") {
1070 if (Constant *LC = dyn_cast<Constant>(LHS))
1071 if (Constant *RC = dyn_cast<Constant>(RHS))
1072 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1073 return Insert(new FCmpInst(P, LHS, RHS), Name);
1076 //===--------------------------------------------------------------------===//
1077 // Instruction creation methods: Other Instructions
1078 //===--------------------------------------------------------------------===//
1080 PHINode *CreatePHI(const Type *Ty, unsigned NumReservedValues,
1081 const Twine &Name = "") {
1082 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1085 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1086 return Insert(CallInst::Create(Callee), Name);
1088 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1089 return Insert(CallInst::Create(Callee, Arg), Name);
1091 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1092 const Twine &Name = "") {
1093 Value *Args[] = { Arg1, Arg2 };
1094 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
1096 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1097 const Twine &Name = "") {
1098 Value *Args[] = { Arg1, Arg2, Arg3 };
1099 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
1101 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1102 Value *Arg4, const Twine &Name = "") {
1103 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1104 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
1106 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1107 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1108 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1109 return Insert(CallInst::Create(Callee, Args, Args+5), Name);
1112 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Arg,
1113 const Twine &Name = "") {
1114 return Insert(CallInst::Create(Callee, Arg.begin(), Arg.end(), Name));
1117 template<typename RandomAccessIterator>
1118 CallInst *CreateCall(Value *Callee, RandomAccessIterator ArgBegin,
1119 RandomAccessIterator ArgEnd, const Twine &Name = "") {
1120 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
1123 Value *CreateSelect(Value *C, Value *True, Value *False,
1124 const Twine &Name = "") {
1125 if (Constant *CC = dyn_cast<Constant>(C))
1126 if (Constant *TC = dyn_cast<Constant>(True))
1127 if (Constant *FC = dyn_cast<Constant>(False))
1128 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1129 return Insert(SelectInst::Create(C, True, False), Name);
1132 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
1133 return Insert(new VAArgInst(List, Ty), Name);
1136 Value *CreateExtractElement(Value *Vec, Value *Idx,
1137 const Twine &Name = "") {
1138 if (Constant *VC = dyn_cast<Constant>(Vec))
1139 if (Constant *IC = dyn_cast<Constant>(Idx))
1140 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1141 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1144 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1145 const Twine &Name = "") {
1146 if (Constant *VC = dyn_cast<Constant>(Vec))
1147 if (Constant *NC = dyn_cast<Constant>(NewElt))
1148 if (Constant *IC = dyn_cast<Constant>(Idx))
1149 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1150 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1153 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1154 const Twine &Name = "") {
1155 if (Constant *V1C = dyn_cast<Constant>(V1))
1156 if (Constant *V2C = dyn_cast<Constant>(V2))
1157 if (Constant *MC = dyn_cast<Constant>(Mask))
1158 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1159 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1162 Value *CreateExtractValue(Value *Agg, unsigned Idx,
1163 const Twine &Name = "") {
1164 if (Constant *AggC = dyn_cast<Constant>(Agg))
1165 return Insert(Folder.CreateExtractValue(AggC, &Idx, 1), Name);
1166 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1169 template<typename RandomAccessIterator>
1170 Value *CreateExtractValue(Value *Agg,
1171 RandomAccessIterator IdxBegin,
1172 RandomAccessIterator IdxEnd,
1173 const Twine &Name = "") {
1174 if (Constant *AggC = dyn_cast<Constant>(Agg))
1175 return Insert(Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd-IdxBegin),
1177 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1180 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1181 const Twine &Name = "") {
1182 if (Constant *AggC = dyn_cast<Constant>(Agg))
1183 if (Constant *ValC = dyn_cast<Constant>(Val))
1184 return Insert(Folder.CreateInsertValue(AggC, ValC, &Idx, 1), Name);
1185 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1188 template<typename RandomAccessIterator>
1189 Value *CreateInsertValue(Value *Agg, Value *Val,
1190 RandomAccessIterator IdxBegin,
1191 RandomAccessIterator IdxEnd,
1192 const Twine &Name = "") {
1193 if (Constant *AggC = dyn_cast<Constant>(Agg))
1194 if (Constant *ValC = dyn_cast<Constant>(Val))
1195 return Insert(Folder.CreateInsertValue(AggC, ValC, IdxBegin,
1198 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1201 //===--------------------------------------------------------------------===//
1202 // Utility creation methods
1203 //===--------------------------------------------------------------------===//
1205 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1206 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1207 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1211 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1212 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1213 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1217 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1218 /// dividing out the size of the pointed-to objects. This is intended to
1219 /// implement C-style pointer subtraction. As such, the pointers must be
1220 /// appropriately aligned for their element types and pointing into the
1222 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1223 assert(LHS->getType() == RHS->getType() &&
1224 "Pointer subtraction operand types must match!");
1225 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1226 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1227 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1228 Value *Difference = CreateSub(LHS_int, RHS_int);
1229 return CreateExactSDiv(Difference,
1230 ConstantExpr::getSizeOf(ArgType->getElementType()),