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/Twine.h"
22 #include "llvm/Support/ConstantFolder.h"
27 /// IRBuilderDefaultInserter - This provides the default implementation of the
28 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
29 /// created by IRBuilder and needs to be inserted. By default, this inserts the
30 /// instruction at the insertion point.
31 template <bool preserveNames = true>
32 class IRBuilderDefaultInserter {
34 void InsertHelper(Instruction *I, const Twine &Name,
35 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
36 if (BB) BB->getInstList().insert(InsertPt, I);
42 /// IRBuilderBase - Common base class shared among various IRBuilders.
44 DebugLoc CurDbgLocation;
47 BasicBlock::iterator InsertPt;
51 IRBuilderBase(LLVMContext &context)
53 ClearInsertionPoint();
56 //===--------------------------------------------------------------------===//
57 // Builder configuration methods
58 //===--------------------------------------------------------------------===//
60 /// ClearInsertionPoint - Clear the insertion point: created instructions will
61 /// not be inserted into a block.
62 void ClearInsertionPoint() {
66 BasicBlock *GetInsertBlock() const { return BB; }
67 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
68 LLVMContext &getContext() const { return Context; }
70 /// SetInsertPoint - This specifies that created instructions should be
71 /// appended to the end of the specified block.
72 void SetInsertPoint(BasicBlock *TheBB) {
77 /// SetInsertPoint - This specifies that created instructions should be
78 /// inserted before the specified instruction.
79 void SetInsertPoint(Instruction *I) {
84 /// SetInsertPoint - This specifies that created instructions should be
85 /// inserted at the specified point.
86 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
91 /// SetCurrentDebugLocation - Set location information used by debugging
93 void SetCurrentDebugLocation(const DebugLoc &L) {
97 /// getCurrentDebugLocation - Get location information used by debugging
99 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
101 /// SetInstDebugLocation - If this builder has a current debug location, set
102 /// it on the specified instruction.
103 void SetInstDebugLocation(Instruction *I) const {
104 if (!CurDbgLocation.isUnknown())
105 I->setDebugLoc(CurDbgLocation);
108 /// InsertPoint - A saved insertion point.
111 BasicBlock::iterator Point;
114 /// Creates a new insertion point which doesn't point to anything.
115 InsertPoint() : Block(0) {}
117 /// Creates a new insertion point at the given location.
118 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
119 : Block(InsertBlock), Point(InsertPoint) {}
121 /// isSet - Returns true if this insert point is set.
122 bool isSet() const { return (Block != 0); }
124 llvm::BasicBlock *getBlock() const { return Block; }
125 llvm::BasicBlock::iterator getPoint() const { return Point; }
128 /// saveIP - Returns the current insert point.
129 InsertPoint saveIP() const {
130 return InsertPoint(GetInsertBlock(), GetInsertPoint());
133 /// saveAndClearIP - Returns the current insert point, clearing it
135 InsertPoint saveAndClearIP() {
136 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
137 ClearInsertionPoint();
141 /// restoreIP - Sets the current insert point to a previously-saved
143 void restoreIP(InsertPoint IP) {
145 SetInsertPoint(IP.getBlock(), IP.getPoint());
147 ClearInsertionPoint();
150 //===--------------------------------------------------------------------===//
151 // Miscellaneous creation methods.
152 //===--------------------------------------------------------------------===//
154 /// CreateGlobalString - Make a new global variable with an initializer that
155 /// has array of i8 type filled in with the nul terminated string value
156 /// specified. If Name is specified, it is the name of the global variable
158 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
160 /// getInt1 - Get a constant value representing either true or false.
161 ConstantInt *getInt1(bool V) {
162 return ConstantInt::get(getInt1Ty(), V);
165 /// getTrue - Get the constant value for i1 true.
166 ConstantInt *getTrue() {
167 return ConstantInt::getTrue(Context);
170 /// getFalse - Get the constant value for i1 false.
171 ConstantInt *getFalse() {
172 return ConstantInt::getFalse(Context);
175 /// getInt8 - Get a constant 8-bit value.
176 ConstantInt *getInt8(uint8_t C) {
177 return ConstantInt::get(getInt8Ty(), C);
180 /// getInt16 - Get a constant 16-bit value.
181 ConstantInt *getInt16(uint16_t C) {
182 return ConstantInt::get(getInt16Ty(), C);
185 /// getInt32 - Get a constant 32-bit value.
186 ConstantInt *getInt32(uint32_t C) {
187 return ConstantInt::get(getInt32Ty(), C);
190 /// getInt64 - Get a constant 64-bit value.
191 ConstantInt *getInt64(uint64_t C) {
192 return ConstantInt::get(getInt64Ty(), C);
195 //===--------------------------------------------------------------------===//
196 // Type creation methods
197 //===--------------------------------------------------------------------===//
199 /// getInt1Ty - Fetch the type representing a single bit
200 const IntegerType *getInt1Ty() {
201 return Type::getInt1Ty(Context);
204 /// getInt8Ty - Fetch the type representing an 8-bit integer.
205 const IntegerType *getInt8Ty() {
206 return Type::getInt8Ty(Context);
209 /// getInt16Ty - Fetch the type representing a 16-bit integer.
210 const IntegerType *getInt16Ty() {
211 return Type::getInt16Ty(Context);
214 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
215 const IntegerType *getInt32Ty() {
216 return Type::getInt32Ty(Context);
219 /// getInt64Ty - Fetch the type representing a 64-bit integer.
220 const IntegerType *getInt64Ty() {
221 return Type::getInt64Ty(Context);
224 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
225 const Type *getFloatTy() {
226 return Type::getFloatTy(Context);
229 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
230 const Type *getDoubleTy() {
231 return Type::getDoubleTy(Context);
234 /// getVoidTy - Fetch the type representing void.
235 const Type *getVoidTy() {
236 return Type::getVoidTy(Context);
239 const PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
240 return Type::getInt8PtrTy(Context, AddrSpace);
243 /// getCurrentFunctionReturnType - Get the return type of the current function
244 /// that we're emitting into.
245 const Type *getCurrentFunctionReturnType() const;
247 /// CreateMemSet - Create and insert a memset to the specified pointer and the
248 /// specified value. If the pointer isn't an i8*, it will be converted. If a
249 /// TBAA tag is specified, it will be added to the instruction.
250 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
251 bool isVolatile = false, MDNode *TBAATag = 0) {
252 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
255 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
256 bool isVolatile = false, MDNode *TBAATag = 0);
258 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
259 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
260 /// specified, it will be added to the instruction.
261 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
262 bool isVolatile = false, MDNode *TBAATag = 0) {
263 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
266 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
267 bool isVolatile = false, MDNode *TBAATag = 0);
269 /// CreateMemMove - Create and insert a memmove between the specified
270 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
271 /// tag is specified, it will be added to the instruction.
272 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
273 bool isVolatile = false, MDNode *TBAATag = 0) {
274 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
277 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
278 bool isVolatile = false, MDNode *TBAATag = 0);
280 Value *getCastedInt8PtrValue(Value *Ptr);
283 /// IRBuilder - This provides a uniform API for creating instructions and
284 /// inserting them into a basic block: either at the end of a BasicBlock, or
285 /// at a specific iterator location in a block.
287 /// Note that the builder does not expose the full generality of LLVM
288 /// instructions. For access to extra instruction properties, use the mutators
289 /// (e.g. setVolatile) on the instructions after they have been created.
290 /// The first template argument handles whether or not to preserve names in the
291 /// final instruction output. This defaults to on. The second template argument
292 /// specifies a class to use for creating constants. This defaults to creating
293 /// minimally folded constants. The fourth template argument allows clients to
294 /// specify custom insertion hooks that are called on every newly created
296 template<bool preserveNames = true, typename T = ConstantFolder,
297 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
298 class IRBuilder : public IRBuilderBase, public Inserter {
301 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
302 : IRBuilderBase(C), Inserter(I), Folder(F) {
305 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder() {
308 explicit IRBuilder(BasicBlock *TheBB, const T &F)
309 : IRBuilderBase(TheBB->getContext()), Folder(F) {
310 SetInsertPoint(TheBB);
313 explicit IRBuilder(BasicBlock *TheBB)
314 : IRBuilderBase(TheBB->getContext()), Folder() {
315 SetInsertPoint(TheBB);
318 explicit IRBuilder(Instruction *IP)
319 : IRBuilderBase(IP->getContext()), Folder() {
323 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
324 : IRBuilderBase(TheBB->getContext()), Folder(F) {
325 SetInsertPoint(TheBB, IP);
328 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
329 : IRBuilderBase(TheBB->getContext()), Folder() {
330 SetInsertPoint(TheBB, IP);
333 /// getFolder - Get the constant folder being used.
334 const T &getFolder() { return Folder; }
336 /// isNamePreserving - Return true if this builder is configured to actually
337 /// add the requested names to IR created through it.
338 bool isNamePreserving() const { return preserveNames; }
340 /// Insert - Insert and return the specified instruction.
341 template<typename InstTy>
342 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
343 this->InsertHelper(I, Name, BB, InsertPt);
344 if (!getCurrentDebugLocation().isUnknown())
345 this->SetInstDebugLocation(I);
349 /// Insert - No-op overload to handle constants.
350 Constant *Insert(Constant *C, const Twine& = "") const {
354 //===--------------------------------------------------------------------===//
355 // Instruction creation methods: Terminators
356 //===--------------------------------------------------------------------===//
358 /// CreateRetVoid - Create a 'ret void' instruction.
359 ReturnInst *CreateRetVoid() {
360 return Insert(ReturnInst::Create(Context));
364 /// CreateRet - Create a 'ret <val>' instruction.
366 ReturnInst *CreateRet(Value *V) {
367 return Insert(ReturnInst::Create(Context, V));
370 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
371 /// with one Value from the retVals array each, that build a aggregate
372 /// return value one value at a time, and a ret instruction to return
373 /// the resulting aggregate value. This is a convenience function for
374 /// code that uses aggregate return values as a vehicle for having
375 /// multiple return values.
377 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
378 Value *V = UndefValue::get(getCurrentFunctionReturnType());
379 for (unsigned i = 0; i != N; ++i)
380 V = CreateInsertValue(V, retVals[i], i, "mrv");
381 return Insert(ReturnInst::Create(Context, V));
384 /// CreateBr - Create an unconditional 'br label X' instruction.
385 BranchInst *CreateBr(BasicBlock *Dest) {
386 return Insert(BranchInst::Create(Dest));
389 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
391 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
392 return Insert(BranchInst::Create(True, False, Cond));
395 /// CreateSwitch - Create a switch instruction with the specified value,
396 /// default dest, and with a hint for the number of cases that will be added
397 /// (for efficient allocation).
398 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
399 return Insert(SwitchInst::Create(V, Dest, NumCases));
402 /// CreateIndirectBr - Create an indirect branch instruction with the
403 /// specified address operand, with an optional hint for the number of
404 /// destinations that will be added (for efficient allocation).
405 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
406 return Insert(IndirectBrInst::Create(Addr, NumDests));
409 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
410 BasicBlock *UnwindDest, const Twine &Name = "") {
411 Value *Args[] = { 0 };
412 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
415 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
416 BasicBlock *UnwindDest, Value *Arg1,
417 const Twine &Name = "") {
418 Value *Args[] = { Arg1 };
419 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
422 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
423 BasicBlock *UnwindDest, Value *Arg1,
424 Value *Arg2, Value *Arg3,
425 const Twine &Name = "") {
426 Value *Args[] = { Arg1, Arg2, Arg3 };
427 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
430 /// CreateInvoke - Create an invoke instruction.
431 template<typename RandomAccessIterator>
432 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
433 BasicBlock *UnwindDest,
434 RandomAccessIterator ArgBegin,
435 RandomAccessIterator ArgEnd,
436 const Twine &Name = "") {
437 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
438 ArgBegin, ArgEnd), Name);
441 UnwindInst *CreateUnwind() {
442 return Insert(new UnwindInst(Context));
445 UnreachableInst *CreateUnreachable() {
446 return Insert(new UnreachableInst(Context));
449 //===--------------------------------------------------------------------===//
450 // Instruction creation methods: Binary Operators
451 //===--------------------------------------------------------------------===//
453 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
454 Value *LHS, Value *RHS,
456 bool HasNUW, bool HasNSW) {
457 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
458 if (HasNUW) BO->setHasNoUnsignedWrap();
459 if (HasNSW) BO->setHasNoSignedWrap();
463 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
464 bool HasNUW = false, bool HasNSW = false) {
465 if (Constant *LC = dyn_cast<Constant>(LHS))
466 if (Constant *RC = dyn_cast<Constant>(RHS))
467 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
468 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
471 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
472 return CreateAdd(LHS, RHS, Name, false, true);
474 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
475 return CreateAdd(LHS, RHS, Name, true, false);
477 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
478 if (Constant *LC = dyn_cast<Constant>(LHS))
479 if (Constant *RC = dyn_cast<Constant>(RHS))
480 return Insert(Folder.CreateFAdd(LC, RC), Name);
481 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
483 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
484 bool HasNUW = false, bool HasNSW = false) {
485 if (Constant *LC = dyn_cast<Constant>(LHS))
486 if (Constant *RC = dyn_cast<Constant>(RHS))
487 return Insert(Folder.CreateSub(LC, RC), Name);
488 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
491 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
492 return CreateSub(LHS, RHS, Name, false, true);
494 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
495 return CreateSub(LHS, RHS, Name, true, false);
497 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
498 if (Constant *LC = dyn_cast<Constant>(LHS))
499 if (Constant *RC = dyn_cast<Constant>(RHS))
500 return Insert(Folder.CreateFSub(LC, RC), Name);
501 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
503 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
504 bool HasNUW = false, bool HasNSW = false) {
505 if (Constant *LC = dyn_cast<Constant>(LHS))
506 if (Constant *RC = dyn_cast<Constant>(RHS))
507 return Insert(Folder.CreateMul(LC, RC), Name);
508 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
511 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
512 return CreateMul(LHS, RHS, Name, false, true);
514 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
515 return CreateMul(LHS, RHS, Name, true, false);
517 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
518 if (Constant *LC = dyn_cast<Constant>(LHS))
519 if (Constant *RC = dyn_cast<Constant>(RHS))
520 return Insert(Folder.CreateFMul(LC, RC), Name);
521 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
523 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
524 bool isExact = false) {
525 if (Constant *LC = dyn_cast<Constant>(LHS))
526 if (Constant *RC = dyn_cast<Constant>(RHS))
527 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
529 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
530 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
532 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
533 return CreateUDiv(LHS, RHS, Name, true);
535 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
536 bool isExact = false) {
537 if (Constant *LC = dyn_cast<Constant>(LHS))
538 if (Constant *RC = dyn_cast<Constant>(RHS))
539 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
541 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
542 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
544 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
545 return CreateSDiv(LHS, RHS, Name, true);
547 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
548 if (Constant *LC = dyn_cast<Constant>(LHS))
549 if (Constant *RC = dyn_cast<Constant>(RHS))
550 return Insert(Folder.CreateFDiv(LC, RC), Name);
551 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
553 Value *CreateURem(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.CreateURem(LC, RC), Name);
557 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
559 Value *CreateSRem(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.CreateSRem(LC, RC), Name);
563 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
565 Value *CreateFRem(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.CreateFRem(LC, RC), Name);
569 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
572 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
573 bool HasNUW = false, bool HasNSW = false) {
574 if (Constant *LC = dyn_cast<Constant>(LHS))
575 if (Constant *RC = dyn_cast<Constant>(RHS))
576 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
577 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
580 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
581 bool HasNUW = false, bool HasNSW = false) {
582 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
585 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
586 bool HasNUW = false, bool HasNSW = false) {
587 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
591 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
592 bool isExact = false) {
593 if (Constant *LC = dyn_cast<Constant>(LHS))
594 if (Constant *RC = dyn_cast<Constant>(RHS))
595 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
597 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
598 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
600 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
601 bool isExact = false) {
602 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
604 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
605 bool isExact = false) {
606 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
609 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
610 bool isExact = false) {
611 if (Constant *LC = dyn_cast<Constant>(LHS))
612 if (Constant *RC = dyn_cast<Constant>(RHS))
613 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
615 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
616 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
618 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
619 bool isExact = false) {
620 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
622 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
623 bool isExact = false) {
624 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
627 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
628 if (Constant *RC = dyn_cast<Constant>(RHS)) {
629 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
630 return LHS; // LHS & -1 -> LHS
631 if (Constant *LC = dyn_cast<Constant>(LHS))
632 return Insert(Folder.CreateAnd(LC, RC), Name);
634 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
636 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
637 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
639 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
640 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
643 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
644 if (Constant *RC = dyn_cast<Constant>(RHS)) {
645 if (RC->isNullValue())
646 return LHS; // LHS | 0 -> LHS
647 if (Constant *LC = dyn_cast<Constant>(LHS))
648 return Insert(Folder.CreateOr(LC, RC), Name);
650 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
652 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
653 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
655 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
656 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
659 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
660 if (Constant *LC = dyn_cast<Constant>(LHS))
661 if (Constant *RC = dyn_cast<Constant>(RHS))
662 return Insert(Folder.CreateXor(LC, RC), Name);
663 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
665 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
666 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
668 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
669 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
672 Value *CreateBinOp(Instruction::BinaryOps Opc,
673 Value *LHS, Value *RHS, const Twine &Name = "") {
674 if (Constant *LC = dyn_cast<Constant>(LHS))
675 if (Constant *RC = dyn_cast<Constant>(RHS))
676 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
677 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
680 Value *CreateNeg(Value *V, const Twine &Name = "",
681 bool HasNUW = false, bool HasNSW = false) {
682 if (Constant *VC = dyn_cast<Constant>(V))
683 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
684 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
685 if (HasNUW) BO->setHasNoUnsignedWrap();
686 if (HasNSW) BO->setHasNoSignedWrap();
689 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
690 return CreateNeg(V, Name, false, true);
692 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
693 return CreateNeg(V, Name, true, false);
695 Value *CreateFNeg(Value *V, const Twine &Name = "") {
696 if (Constant *VC = dyn_cast<Constant>(V))
697 return Insert(Folder.CreateFNeg(VC), Name);
698 return Insert(BinaryOperator::CreateFNeg(V), Name);
700 Value *CreateNot(Value *V, const Twine &Name = "") {
701 if (Constant *VC = dyn_cast<Constant>(V))
702 return Insert(Folder.CreateNot(VC), Name);
703 return Insert(BinaryOperator::CreateNot(V), Name);
706 //===--------------------------------------------------------------------===//
707 // Instruction creation methods: Memory Instructions
708 //===--------------------------------------------------------------------===//
710 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
711 const Twine &Name = "") {
712 return Insert(new AllocaInst(Ty, ArraySize), Name);
714 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
715 // converting the string to 'bool' for the isVolatile parameter.
716 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
717 return Insert(new LoadInst(Ptr), Name);
719 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
720 return Insert(new LoadInst(Ptr), Name);
722 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
723 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
725 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
726 return Insert(new StoreInst(Val, Ptr, isVolatile));
728 template<typename RandomAccessIterator>
729 Value *CreateGEP(Value *Ptr,
730 RandomAccessIterator IdxBegin,
731 RandomAccessIterator IdxEnd,
732 const Twine &Name = "") {
733 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
734 // Every index must be constant.
735 RandomAccessIterator i;
736 for (i = IdxBegin; i < IdxEnd; ++i)
737 if (!isa<Constant>(*i))
740 return Insert(Folder.CreateGetElementPtr(PC, &IdxBegin[0],
744 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
746 template<typename RandomAccessIterator>
747 Value *CreateInBoundsGEP(Value *Ptr, RandomAccessIterator IdxBegin,
748 RandomAccessIterator IdxEnd,
749 const Twine &Name = "") {
750 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
751 // Every index must be constant.
752 RandomAccessIterator i;
753 for (i = IdxBegin; i < IdxEnd; ++i)
754 if (!isa<Constant>(*i))
757 return Insert(Folder.CreateInBoundsGetElementPtr(PC,
762 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
765 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
766 if (Constant *PC = dyn_cast<Constant>(Ptr))
767 if (Constant *IC = dyn_cast<Constant>(Idx))
768 return Insert(Folder.CreateGetElementPtr(PC, &IC, 1), Name);
769 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
771 Value *CreateInBoundsGEP(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.CreateInBoundsGetElementPtr(PC, &IC, 1), Name);
775 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
777 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
778 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
780 if (Constant *PC = dyn_cast<Constant>(Ptr))
781 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
783 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
785 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
786 const Twine &Name = "") {
787 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
789 if (Constant *PC = dyn_cast<Constant>(Ptr))
790 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
792 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
794 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
795 const Twine &Name = "") {
797 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
798 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
801 if (Constant *PC = dyn_cast<Constant>(Ptr))
802 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
804 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
806 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
807 const Twine &Name = "") {
809 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
810 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
813 if (Constant *PC = dyn_cast<Constant>(Ptr))
814 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
816 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
818 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
819 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
821 if (Constant *PC = dyn_cast<Constant>(Ptr))
822 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
824 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
826 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
827 const Twine &Name = "") {
828 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
830 if (Constant *PC = dyn_cast<Constant>(Ptr))
831 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
833 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
835 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
836 const Twine &Name = "") {
838 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
839 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
842 if (Constant *PC = dyn_cast<Constant>(Ptr))
843 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
845 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
847 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
848 const Twine &Name = "") {
850 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
851 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
854 if (Constant *PC = dyn_cast<Constant>(Ptr))
855 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
857 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
859 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
860 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
863 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
864 /// with "i8*" type instead of a pointer to array of i8.
865 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
866 Value *gv = CreateGlobalString(Str, Name);
867 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
868 Value *Args[] = { zero, zero };
869 return CreateInBoundsGEP(gv, Args, Args+2, Name);
872 //===--------------------------------------------------------------------===//
873 // Instruction creation methods: Cast/Conversion Operators
874 //===--------------------------------------------------------------------===//
876 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
877 return CreateCast(Instruction::Trunc, V, DestTy, Name);
879 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
880 return CreateCast(Instruction::ZExt, V, DestTy, Name);
882 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
883 return CreateCast(Instruction::SExt, V, DestTy, Name);
885 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
886 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
888 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
889 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
891 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
892 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
894 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
895 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
897 Value *CreateFPTrunc(Value *V, const Type *DestTy,
898 const Twine &Name = "") {
899 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
901 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
902 return CreateCast(Instruction::FPExt, V, DestTy, Name);
904 Value *CreatePtrToInt(Value *V, const Type *DestTy,
905 const Twine &Name = "") {
906 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
908 Value *CreateIntToPtr(Value *V, const Type *DestTy,
909 const Twine &Name = "") {
910 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
912 Value *CreateBitCast(Value *V, const Type *DestTy,
913 const Twine &Name = "") {
914 return CreateCast(Instruction::BitCast, V, DestTy, Name);
916 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
917 const Twine &Name = "") {
918 if (V->getType() == DestTy)
920 if (Constant *VC = dyn_cast<Constant>(V))
921 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
922 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
924 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
925 const Twine &Name = "") {
926 if (V->getType() == DestTy)
928 if (Constant *VC = dyn_cast<Constant>(V))
929 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
930 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
932 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
933 const Twine &Name = "") {
934 if (V->getType() == DestTy)
936 if (Constant *VC = dyn_cast<Constant>(V))
937 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
938 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
940 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
941 const Twine &Name = "") {
942 if (V->getType() == DestTy)
944 if (Constant *VC = dyn_cast<Constant>(V))
945 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
946 return Insert(CastInst::Create(Op, V, DestTy), Name);
948 Value *CreatePointerCast(Value *V, const Type *DestTy,
949 const Twine &Name = "") {
950 if (V->getType() == DestTy)
952 if (Constant *VC = dyn_cast<Constant>(V))
953 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
954 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
956 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
957 const Twine &Name = "") {
958 if (V->getType() == DestTy)
960 if (Constant *VC = dyn_cast<Constant>(V))
961 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
962 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
965 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
966 // error, instead of converting the string to bool for the isSigned parameter.
967 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
969 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
970 if (V->getType() == DestTy)
972 if (Constant *VC = dyn_cast<Constant>(V))
973 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
974 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
977 //===--------------------------------------------------------------------===//
978 // Instruction creation methods: Compare Instructions
979 //===--------------------------------------------------------------------===//
981 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
982 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
984 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
985 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
987 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
988 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
990 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
991 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
993 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
994 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
996 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
997 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
999 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1000 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1002 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1003 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1005 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1006 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1008 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1009 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1012 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1013 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1015 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1016 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1018 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1019 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1021 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1022 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1024 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1025 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1027 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1028 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1030 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1031 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1033 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1034 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1036 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1037 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1039 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1040 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1042 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1043 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1045 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1046 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1048 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1049 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1051 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1052 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1055 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1056 const Twine &Name = "") {
1057 if (Constant *LC = dyn_cast<Constant>(LHS))
1058 if (Constant *RC = dyn_cast<Constant>(RHS))
1059 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1060 return Insert(new ICmpInst(P, LHS, RHS), Name);
1062 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1063 const Twine &Name = "") {
1064 if (Constant *LC = dyn_cast<Constant>(LHS))
1065 if (Constant *RC = dyn_cast<Constant>(RHS))
1066 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1067 return Insert(new FCmpInst(P, LHS, RHS), Name);
1070 //===--------------------------------------------------------------------===//
1071 // Instruction creation methods: Other Instructions
1072 //===--------------------------------------------------------------------===//
1074 PHINode *CreatePHI(const Type *Ty, unsigned NumReservedValues,
1075 const Twine &Name = "") {
1076 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1079 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1080 return Insert(CallInst::Create(Callee), Name);
1082 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1083 return Insert(CallInst::Create(Callee, Arg), Name);
1085 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1086 const Twine &Name = "") {
1087 Value *Args[] = { Arg1, Arg2 };
1088 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
1090 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1091 const Twine &Name = "") {
1092 Value *Args[] = { Arg1, Arg2, Arg3 };
1093 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
1095 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1096 Value *Arg4, const Twine &Name = "") {
1097 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1098 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
1100 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1101 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1102 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1103 return Insert(CallInst::Create(Callee, Args, Args+5), Name);
1106 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Arg,
1107 const Twine &Name = "") {
1108 return Insert(CallInst::Create(Callee, Arg.begin(), Arg.end(), Name));
1111 template<typename RandomAccessIterator>
1112 CallInst *CreateCall(Value *Callee, RandomAccessIterator ArgBegin,
1113 RandomAccessIterator ArgEnd, const Twine &Name = "") {
1114 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
1117 Value *CreateSelect(Value *C, Value *True, Value *False,
1118 const Twine &Name = "") {
1119 if (Constant *CC = dyn_cast<Constant>(C))
1120 if (Constant *TC = dyn_cast<Constant>(True))
1121 if (Constant *FC = dyn_cast<Constant>(False))
1122 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1123 return Insert(SelectInst::Create(C, True, False), Name);
1126 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
1127 return Insert(new VAArgInst(List, Ty), Name);
1130 Value *CreateExtractElement(Value *Vec, Value *Idx,
1131 const Twine &Name = "") {
1132 if (Constant *VC = dyn_cast<Constant>(Vec))
1133 if (Constant *IC = dyn_cast<Constant>(Idx))
1134 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1135 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1138 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1139 const Twine &Name = "") {
1140 if (Constant *VC = dyn_cast<Constant>(Vec))
1141 if (Constant *NC = dyn_cast<Constant>(NewElt))
1142 if (Constant *IC = dyn_cast<Constant>(Idx))
1143 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1144 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1147 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1148 const Twine &Name = "") {
1149 if (Constant *V1C = dyn_cast<Constant>(V1))
1150 if (Constant *V2C = dyn_cast<Constant>(V2))
1151 if (Constant *MC = dyn_cast<Constant>(Mask))
1152 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1153 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1156 Value *CreateExtractValue(Value *Agg, unsigned Idx,
1157 const Twine &Name = "") {
1158 if (Constant *AggC = dyn_cast<Constant>(Agg))
1159 return Insert(Folder.CreateExtractValue(AggC, &Idx, 1), Name);
1160 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1163 template<typename RandomAccessIterator>
1164 Value *CreateExtractValue(Value *Agg,
1165 RandomAccessIterator IdxBegin,
1166 RandomAccessIterator IdxEnd,
1167 const Twine &Name = "") {
1168 if (Constant *AggC = dyn_cast<Constant>(Agg))
1169 return Insert(Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd-IdxBegin),
1171 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1174 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1175 const Twine &Name = "") {
1176 if (Constant *AggC = dyn_cast<Constant>(Agg))
1177 if (Constant *ValC = dyn_cast<Constant>(Val))
1178 return Insert(Folder.CreateInsertValue(AggC, ValC, &Idx, 1), Name);
1179 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1182 template<typename RandomAccessIterator>
1183 Value *CreateInsertValue(Value *Agg, Value *Val,
1184 RandomAccessIterator IdxBegin,
1185 RandomAccessIterator IdxEnd,
1186 const Twine &Name = "") {
1187 if (Constant *AggC = dyn_cast<Constant>(Agg))
1188 if (Constant *ValC = dyn_cast<Constant>(Val))
1189 return Insert(Folder.CreateInsertValue(AggC, ValC, IdxBegin,
1192 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1195 //===--------------------------------------------------------------------===//
1196 // Utility creation methods
1197 //===--------------------------------------------------------------------===//
1199 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1200 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1201 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1205 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1206 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1207 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1211 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1212 /// dividing out the size of the pointed-to objects. This is intended to
1213 /// implement C-style pointer subtraction. As such, the pointers must be
1214 /// appropriately aligned for their element types and pointing into the
1216 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1217 assert(LHS->getType() == RHS->getType() &&
1218 "Pointer subtraction operand types must match!");
1219 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1220 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1221 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1222 Value *Difference = CreateSub(LHS_int, RHS_int);
1223 return CreateExactSDiv(Difference,
1224 ConstantExpr::getSizeOf(ArgType->getElementType()),