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) {
83 SetCurrentDebugLocation(I->getDebugLoc());
86 /// SetInsertPoint - This specifies that created instructions should be
87 /// inserted at the specified point.
88 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
93 /// SetCurrentDebugLocation - Set location information used by debugging
95 void SetCurrentDebugLocation(const DebugLoc &L) {
99 /// getCurrentDebugLocation - Get location information used by debugging
101 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
103 /// SetInstDebugLocation - If this builder has a current debug location, set
104 /// it on the specified instruction.
105 void SetInstDebugLocation(Instruction *I) const {
106 if (!CurDbgLocation.isUnknown())
107 I->setDebugLoc(CurDbgLocation);
110 /// InsertPoint - A saved insertion point.
113 BasicBlock::iterator Point;
116 /// Creates a new insertion point which doesn't point to anything.
117 InsertPoint() : Block(0) {}
119 /// Creates a new insertion point at the given location.
120 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
121 : Block(InsertBlock), Point(InsertPoint) {}
123 /// isSet - Returns true if this insert point is set.
124 bool isSet() const { return (Block != 0); }
126 llvm::BasicBlock *getBlock() const { return Block; }
127 llvm::BasicBlock::iterator getPoint() const { return Point; }
130 /// saveIP - Returns the current insert point.
131 InsertPoint saveIP() const {
132 return InsertPoint(GetInsertBlock(), GetInsertPoint());
135 /// saveAndClearIP - Returns the current insert point, clearing it
137 InsertPoint saveAndClearIP() {
138 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
139 ClearInsertionPoint();
143 /// restoreIP - Sets the current insert point to a previously-saved
145 void restoreIP(InsertPoint IP) {
147 SetInsertPoint(IP.getBlock(), IP.getPoint());
149 ClearInsertionPoint();
152 //===--------------------------------------------------------------------===//
153 // Miscellaneous creation methods.
154 //===--------------------------------------------------------------------===//
156 /// CreateGlobalString - Make a new global variable with an initializer that
157 /// has array of i8 type filled in with the nul terminated string value
158 /// specified. The new global variable will be marked mergable with any
159 /// others of the same contents. If Name is specified, it is the name of the
160 /// global variable created.
161 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
163 /// getInt1 - Get a constant value representing either true or false.
164 ConstantInt *getInt1(bool V) {
165 return ConstantInt::get(getInt1Ty(), V);
168 /// getTrue - Get the constant value for i1 true.
169 ConstantInt *getTrue() {
170 return ConstantInt::getTrue(Context);
173 /// getFalse - Get the constant value for i1 false.
174 ConstantInt *getFalse() {
175 return ConstantInt::getFalse(Context);
178 /// getInt8 - Get a constant 8-bit value.
179 ConstantInt *getInt8(uint8_t C) {
180 return ConstantInt::get(getInt8Ty(), C);
183 /// getInt16 - Get a constant 16-bit value.
184 ConstantInt *getInt16(uint16_t C) {
185 return ConstantInt::get(getInt16Ty(), C);
188 /// getInt32 - Get a constant 32-bit value.
189 ConstantInt *getInt32(uint32_t C) {
190 return ConstantInt::get(getInt32Ty(), C);
193 /// getInt64 - Get a constant 64-bit value.
194 ConstantInt *getInt64(uint64_t C) {
195 return ConstantInt::get(getInt64Ty(), C);
198 ConstantInt *getInt(const APInt &AI) {
199 return ConstantInt::get(Context, AI);
202 //===--------------------------------------------------------------------===//
203 // Type creation methods
204 //===--------------------------------------------------------------------===//
206 /// getInt1Ty - Fetch the type representing a single bit
207 const IntegerType *getInt1Ty() {
208 return Type::getInt1Ty(Context);
211 /// getInt8Ty - Fetch the type representing an 8-bit integer.
212 const IntegerType *getInt8Ty() {
213 return Type::getInt8Ty(Context);
216 /// getInt16Ty - Fetch the type representing a 16-bit integer.
217 const IntegerType *getInt16Ty() {
218 return Type::getInt16Ty(Context);
221 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
222 const IntegerType *getInt32Ty() {
223 return Type::getInt32Ty(Context);
226 /// getInt64Ty - Fetch the type representing a 64-bit integer.
227 const IntegerType *getInt64Ty() {
228 return Type::getInt64Ty(Context);
231 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
232 const Type *getFloatTy() {
233 return Type::getFloatTy(Context);
236 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
237 const Type *getDoubleTy() {
238 return Type::getDoubleTy(Context);
241 /// getVoidTy - Fetch the type representing void.
242 const Type *getVoidTy() {
243 return Type::getVoidTy(Context);
246 const PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
247 return Type::getInt8PtrTy(Context, AddrSpace);
250 /// getCurrentFunctionReturnType - Get the return type of the current function
251 /// that we're emitting into.
252 const Type *getCurrentFunctionReturnType() const;
254 /// CreateMemSet - Create and insert a memset to the specified pointer and the
255 /// specified value. If the pointer isn't an i8*, it will be converted. If a
256 /// TBAA tag is specified, it will be added to the instruction.
257 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
258 bool isVolatile = false, MDNode *TBAATag = 0) {
259 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
262 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
263 bool isVolatile = false, MDNode *TBAATag = 0);
265 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
266 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
267 /// specified, it will be added to the instruction.
268 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
269 bool isVolatile = false, MDNode *TBAATag = 0) {
270 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
273 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
274 bool isVolatile = false, MDNode *TBAATag = 0);
276 /// CreateMemMove - Create and insert a memmove between the specified
277 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
278 /// tag is specified, it will be added to the instruction.
279 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
280 bool isVolatile = false, MDNode *TBAATag = 0) {
281 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
284 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
285 bool isVolatile = false, MDNode *TBAATag = 0);
287 Value *getCastedInt8PtrValue(Value *Ptr);
290 /// IRBuilder - This provides a uniform API for creating instructions and
291 /// inserting them into a basic block: either at the end of a BasicBlock, or
292 /// at a specific iterator location in a block.
294 /// Note that the builder does not expose the full generality of LLVM
295 /// instructions. For access to extra instruction properties, use the mutators
296 /// (e.g. setVolatile) on the instructions after they have been created.
297 /// The first template argument handles whether or not to preserve names in the
298 /// final instruction output. This defaults to on. The second template argument
299 /// specifies a class to use for creating constants. This defaults to creating
300 /// minimally folded constants. The fourth template argument allows clients to
301 /// specify custom insertion hooks that are called on every newly created
303 template<bool preserveNames = true, typename T = ConstantFolder,
304 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
305 class IRBuilder : public IRBuilderBase, public Inserter {
308 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
309 : IRBuilderBase(C), Inserter(I), Folder(F) {
312 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder() {
315 explicit IRBuilder(BasicBlock *TheBB, const T &F)
316 : IRBuilderBase(TheBB->getContext()), Folder(F) {
317 SetInsertPoint(TheBB);
320 explicit IRBuilder(BasicBlock *TheBB)
321 : IRBuilderBase(TheBB->getContext()), Folder() {
322 SetInsertPoint(TheBB);
325 explicit IRBuilder(Instruction *IP)
326 : IRBuilderBase(IP->getContext()), Folder() {
328 SetCurrentDebugLocation(IP->getDebugLoc());
331 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
332 : IRBuilderBase(TheBB->getContext()), Folder(F) {
333 SetInsertPoint(TheBB, IP);
336 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
337 : IRBuilderBase(TheBB->getContext()), Folder() {
338 SetInsertPoint(TheBB, IP);
341 /// getFolder - Get the constant folder being used.
342 const T &getFolder() { return Folder; }
344 /// isNamePreserving - Return true if this builder is configured to actually
345 /// add the requested names to IR created through it.
346 bool isNamePreserving() const { return preserveNames; }
348 /// Insert - Insert and return the specified instruction.
349 template<typename InstTy>
350 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
351 this->InsertHelper(I, Name, BB, InsertPt);
352 if (!getCurrentDebugLocation().isUnknown())
353 this->SetInstDebugLocation(I);
357 /// Insert - No-op overload to handle constants.
358 Constant *Insert(Constant *C, const Twine& = "") const {
362 //===--------------------------------------------------------------------===//
363 // Instruction creation methods: Terminators
364 //===--------------------------------------------------------------------===//
366 /// CreateRetVoid - Create a 'ret void' instruction.
367 ReturnInst *CreateRetVoid() {
368 return Insert(ReturnInst::Create(Context));
372 /// CreateRet - Create a 'ret <val>' instruction.
374 ReturnInst *CreateRet(Value *V) {
375 return Insert(ReturnInst::Create(Context, V));
378 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
379 /// with one Value from the retVals array each, that build a aggregate
380 /// return value one value at a time, and a ret instruction to return
381 /// the resulting aggregate value. This is a convenience function for
382 /// code that uses aggregate return values as a vehicle for having
383 /// multiple return values.
385 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
386 Value *V = UndefValue::get(getCurrentFunctionReturnType());
387 for (unsigned i = 0; i != N; ++i)
388 V = CreateInsertValue(V, retVals[i], i, "mrv");
389 return Insert(ReturnInst::Create(Context, V));
392 /// CreateBr - Create an unconditional 'br label X' instruction.
393 BranchInst *CreateBr(BasicBlock *Dest) {
394 return Insert(BranchInst::Create(Dest));
397 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
399 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
400 return Insert(BranchInst::Create(True, False, Cond));
403 /// CreateSwitch - Create a switch instruction with the specified value,
404 /// default dest, and with a hint for the number of cases that will be added
405 /// (for efficient allocation).
406 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
407 return Insert(SwitchInst::Create(V, Dest, NumCases));
410 /// CreateIndirectBr - Create an indirect branch instruction with the
411 /// specified address operand, with an optional hint for the number of
412 /// destinations that will be added (for efficient allocation).
413 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
414 return Insert(IndirectBrInst::Create(Addr, NumDests));
417 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
418 BasicBlock *UnwindDest, const Twine &Name = "") {
419 Value *Args[] = { 0 };
420 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
423 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
424 BasicBlock *UnwindDest, Value *Arg1,
425 const Twine &Name = "") {
426 Value *Args[] = { Arg1 };
427 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
430 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
431 BasicBlock *UnwindDest, Value *Arg1,
432 Value *Arg2, Value *Arg3,
433 const Twine &Name = "") {
434 Value *Args[] = { Arg1, Arg2, Arg3 };
435 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
438 /// CreateInvoke - Create an invoke instruction.
439 template<typename RandomAccessIterator>
440 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
441 BasicBlock *UnwindDest,
442 RandomAccessIterator ArgBegin,
443 RandomAccessIterator ArgEnd,
444 const Twine &Name = "") {
445 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
446 ArgBegin, ArgEnd), Name);
449 UnwindInst *CreateUnwind() {
450 return Insert(new UnwindInst(Context));
453 UnreachableInst *CreateUnreachable() {
454 return Insert(new UnreachableInst(Context));
457 //===--------------------------------------------------------------------===//
458 // Instruction creation methods: Binary Operators
459 //===--------------------------------------------------------------------===//
461 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
462 Value *LHS, Value *RHS,
464 bool HasNUW, bool HasNSW) {
465 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
466 if (HasNUW) BO->setHasNoUnsignedWrap();
467 if (HasNSW) BO->setHasNoSignedWrap();
471 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
472 bool HasNUW = false, bool HasNSW = false) {
473 if (Constant *LC = dyn_cast<Constant>(LHS))
474 if (Constant *RC = dyn_cast<Constant>(RHS))
475 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
476 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
479 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
480 return CreateAdd(LHS, RHS, Name, false, true);
482 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
483 return CreateAdd(LHS, RHS, Name, true, false);
485 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
486 if (Constant *LC = dyn_cast<Constant>(LHS))
487 if (Constant *RC = dyn_cast<Constant>(RHS))
488 return Insert(Folder.CreateFAdd(LC, RC), Name);
489 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
491 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
492 bool HasNUW = false, bool HasNSW = false) {
493 if (Constant *LC = dyn_cast<Constant>(LHS))
494 if (Constant *RC = dyn_cast<Constant>(RHS))
495 return Insert(Folder.CreateSub(LC, RC), Name);
496 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
499 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
500 return CreateSub(LHS, RHS, Name, false, true);
502 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
503 return CreateSub(LHS, RHS, Name, true, false);
505 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
506 if (Constant *LC = dyn_cast<Constant>(LHS))
507 if (Constant *RC = dyn_cast<Constant>(RHS))
508 return Insert(Folder.CreateFSub(LC, RC), Name);
509 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
511 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
512 bool HasNUW = false, bool HasNSW = false) {
513 if (Constant *LC = dyn_cast<Constant>(LHS))
514 if (Constant *RC = dyn_cast<Constant>(RHS))
515 return Insert(Folder.CreateMul(LC, RC), Name);
516 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
519 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
520 return CreateMul(LHS, RHS, Name, false, true);
522 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
523 return CreateMul(LHS, RHS, Name, true, false);
525 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
526 if (Constant *LC = dyn_cast<Constant>(LHS))
527 if (Constant *RC = dyn_cast<Constant>(RHS))
528 return Insert(Folder.CreateFMul(LC, RC), Name);
529 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
531 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
532 bool isExact = false) {
533 if (Constant *LC = dyn_cast<Constant>(LHS))
534 if (Constant *RC = dyn_cast<Constant>(RHS))
535 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
537 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
538 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
540 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
541 return CreateUDiv(LHS, RHS, Name, true);
543 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
544 bool isExact = false) {
545 if (Constant *LC = dyn_cast<Constant>(LHS))
546 if (Constant *RC = dyn_cast<Constant>(RHS))
547 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
549 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
550 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
552 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
553 return CreateSDiv(LHS, RHS, Name, true);
555 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
556 if (Constant *LC = dyn_cast<Constant>(LHS))
557 if (Constant *RC = dyn_cast<Constant>(RHS))
558 return Insert(Folder.CreateFDiv(LC, RC), Name);
559 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
561 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
562 if (Constant *LC = dyn_cast<Constant>(LHS))
563 if (Constant *RC = dyn_cast<Constant>(RHS))
564 return Insert(Folder.CreateURem(LC, RC), Name);
565 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
567 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
568 if (Constant *LC = dyn_cast<Constant>(LHS))
569 if (Constant *RC = dyn_cast<Constant>(RHS))
570 return Insert(Folder.CreateSRem(LC, RC), Name);
571 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
573 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
574 if (Constant *LC = dyn_cast<Constant>(LHS))
575 if (Constant *RC = dyn_cast<Constant>(RHS))
576 return Insert(Folder.CreateFRem(LC, RC), Name);
577 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
580 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
581 bool HasNUW = false, bool HasNSW = false) {
582 if (Constant *LC = dyn_cast<Constant>(LHS))
583 if (Constant *RC = dyn_cast<Constant>(RHS))
584 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
585 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
588 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
589 bool HasNUW = false, bool HasNSW = false) {
590 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
593 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
594 bool HasNUW = false, bool HasNSW = false) {
595 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
599 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
600 bool isExact = false) {
601 if (Constant *LC = dyn_cast<Constant>(LHS))
602 if (Constant *RC = dyn_cast<Constant>(RHS))
603 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
605 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
606 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
608 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
609 bool isExact = false) {
610 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
612 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
613 bool isExact = false) {
614 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
617 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
618 bool isExact = false) {
619 if (Constant *LC = dyn_cast<Constant>(LHS))
620 if (Constant *RC = dyn_cast<Constant>(RHS))
621 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
623 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
624 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
626 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
627 bool isExact = false) {
628 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
630 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
631 bool isExact = false) {
632 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
635 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
636 if (Constant *RC = dyn_cast<Constant>(RHS)) {
637 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
638 return LHS; // LHS & -1 -> LHS
639 if (Constant *LC = dyn_cast<Constant>(LHS))
640 return Insert(Folder.CreateAnd(LC, RC), Name);
642 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
644 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
645 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
647 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
648 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
651 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
652 if (Constant *RC = dyn_cast<Constant>(RHS)) {
653 if (RC->isNullValue())
654 return LHS; // LHS | 0 -> LHS
655 if (Constant *LC = dyn_cast<Constant>(LHS))
656 return Insert(Folder.CreateOr(LC, RC), Name);
658 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
660 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
661 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
663 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
664 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
667 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
668 if (Constant *LC = dyn_cast<Constant>(LHS))
669 if (Constant *RC = dyn_cast<Constant>(RHS))
670 return Insert(Folder.CreateXor(LC, RC), Name);
671 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
673 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
674 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
676 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
677 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
680 Value *CreateBinOp(Instruction::BinaryOps Opc,
681 Value *LHS, Value *RHS, const Twine &Name = "") {
682 if (Constant *LC = dyn_cast<Constant>(LHS))
683 if (Constant *RC = dyn_cast<Constant>(RHS))
684 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
685 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
688 Value *CreateNeg(Value *V, const Twine &Name = "",
689 bool HasNUW = false, bool HasNSW = false) {
690 if (Constant *VC = dyn_cast<Constant>(V))
691 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
692 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
693 if (HasNUW) BO->setHasNoUnsignedWrap();
694 if (HasNSW) BO->setHasNoSignedWrap();
697 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
698 return CreateNeg(V, Name, false, true);
700 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
701 return CreateNeg(V, Name, true, false);
703 Value *CreateFNeg(Value *V, const Twine &Name = "") {
704 if (Constant *VC = dyn_cast<Constant>(V))
705 return Insert(Folder.CreateFNeg(VC), Name);
706 return Insert(BinaryOperator::CreateFNeg(V), Name);
708 Value *CreateNot(Value *V, const Twine &Name = "") {
709 if (Constant *VC = dyn_cast<Constant>(V))
710 return Insert(Folder.CreateNot(VC), Name);
711 return Insert(BinaryOperator::CreateNot(V), Name);
714 //===--------------------------------------------------------------------===//
715 // Instruction creation methods: Memory Instructions
716 //===--------------------------------------------------------------------===//
718 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
719 const Twine &Name = "") {
720 return Insert(new AllocaInst(Ty, ArraySize), Name);
722 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
723 // converting the string to 'bool' for the isVolatile parameter.
724 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
725 return Insert(new LoadInst(Ptr), Name);
727 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
728 return Insert(new LoadInst(Ptr), Name);
730 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
731 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
733 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
734 return Insert(new StoreInst(Val, Ptr, isVolatile));
736 template<typename RandomAccessIterator>
737 Value *CreateGEP(Value *Ptr,
738 RandomAccessIterator IdxBegin,
739 RandomAccessIterator IdxEnd,
740 const Twine &Name = "") {
741 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
742 // Every index must be constant.
743 RandomAccessIterator i;
744 for (i = IdxBegin; i < IdxEnd; ++i)
745 if (!isa<Constant>(*i))
748 return Insert(Folder.CreateGetElementPtr(PC, &IdxBegin[0],
752 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
754 template<typename RandomAccessIterator>
755 Value *CreateInBoundsGEP(Value *Ptr, RandomAccessIterator IdxBegin,
756 RandomAccessIterator IdxEnd,
757 const Twine &Name = "") {
758 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
759 // Every index must be constant.
760 RandomAccessIterator i;
761 for (i = IdxBegin; i < IdxEnd; ++i)
762 if (!isa<Constant>(*i))
765 return Insert(Folder.CreateInBoundsGetElementPtr(PC,
770 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
773 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
774 if (Constant *PC = dyn_cast<Constant>(Ptr))
775 if (Constant *IC = dyn_cast<Constant>(Idx))
776 return Insert(Folder.CreateGetElementPtr(PC, &IC, 1), Name);
777 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
779 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
780 if (Constant *PC = dyn_cast<Constant>(Ptr))
781 if (Constant *IC = dyn_cast<Constant>(Idx))
782 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &IC, 1), Name);
783 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
785 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
786 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
788 if (Constant *PC = dyn_cast<Constant>(Ptr))
789 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
791 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
793 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
794 const Twine &Name = "") {
795 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
797 if (Constant *PC = dyn_cast<Constant>(Ptr))
798 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
800 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
802 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
803 const Twine &Name = "") {
805 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
806 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
809 if (Constant *PC = dyn_cast<Constant>(Ptr))
810 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
812 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
814 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
815 const Twine &Name = "") {
817 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
818 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
821 if (Constant *PC = dyn_cast<Constant>(Ptr))
822 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
824 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
826 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
827 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
829 if (Constant *PC = dyn_cast<Constant>(Ptr))
830 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
832 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
834 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
835 const Twine &Name = "") {
836 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
838 if (Constant *PC = dyn_cast<Constant>(Ptr))
839 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
841 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
843 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
844 const Twine &Name = "") {
846 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
847 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
850 if (Constant *PC = dyn_cast<Constant>(Ptr))
851 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
853 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
855 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
856 const Twine &Name = "") {
858 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
859 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
862 if (Constant *PC = dyn_cast<Constant>(Ptr))
863 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
865 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
867 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
868 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
871 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
872 /// with "i8*" type instead of a pointer to array of i8.
873 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
874 Value *gv = CreateGlobalString(Str, Name);
875 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
876 Value *Args[] = { zero, zero };
877 return CreateInBoundsGEP(gv, Args, Args+2, Name);
880 //===--------------------------------------------------------------------===//
881 // Instruction creation methods: Cast/Conversion Operators
882 //===--------------------------------------------------------------------===//
884 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
885 return CreateCast(Instruction::Trunc, V, DestTy, Name);
887 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
888 return CreateCast(Instruction::ZExt, V, DestTy, Name);
890 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
891 return CreateCast(Instruction::SExt, V, DestTy, Name);
893 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
894 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
896 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
897 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
899 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
900 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
902 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
903 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
905 Value *CreateFPTrunc(Value *V, const Type *DestTy,
906 const Twine &Name = "") {
907 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
909 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
910 return CreateCast(Instruction::FPExt, V, DestTy, Name);
912 Value *CreatePtrToInt(Value *V, const Type *DestTy,
913 const Twine &Name = "") {
914 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
916 Value *CreateIntToPtr(Value *V, const Type *DestTy,
917 const Twine &Name = "") {
918 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
920 Value *CreateBitCast(Value *V, const Type *DestTy,
921 const Twine &Name = "") {
922 return CreateCast(Instruction::BitCast, V, DestTy, Name);
924 Value *CreateZExtOrBitCast(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.CreateZExtOrBitCast(VC, DestTy), Name);
930 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
932 Value *CreateSExtOrBitCast(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.CreateSExtOrBitCast(VC, DestTy), Name);
938 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
940 Value *CreateTruncOrBitCast(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.CreateTruncOrBitCast(VC, DestTy), Name);
946 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
948 Value *CreateCast(Instruction::CastOps Op, 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.CreateCast(Op, VC, DestTy), Name);
954 return Insert(CastInst::Create(Op, V, DestTy), Name);
956 Value *CreatePointerCast(Value *V, const Type *DestTy,
957 const Twine &Name = "") {
958 if (V->getType() == DestTy)
960 if (Constant *VC = dyn_cast<Constant>(V))
961 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
962 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
964 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
965 const Twine &Name = "") {
966 if (V->getType() == DestTy)
968 if (Constant *VC = dyn_cast<Constant>(V))
969 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
970 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
973 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
974 // error, instead of converting the string to bool for the isSigned parameter.
975 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
977 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
978 if (V->getType() == DestTy)
980 if (Constant *VC = dyn_cast<Constant>(V))
981 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
982 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
985 //===--------------------------------------------------------------------===//
986 // Instruction creation methods: Compare Instructions
987 //===--------------------------------------------------------------------===//
989 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
990 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
992 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
993 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
995 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
996 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
998 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
999 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1001 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1002 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1004 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1005 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1007 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1008 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1010 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1011 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1013 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1014 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1016 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1017 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1020 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1021 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1023 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1024 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1026 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1027 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1029 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1030 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1032 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1033 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1035 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1036 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1038 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1039 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1041 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1042 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1044 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1045 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1047 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1048 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1050 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1051 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1053 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1054 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1056 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1057 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1059 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1060 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1063 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1064 const Twine &Name = "") {
1065 if (Constant *LC = dyn_cast<Constant>(LHS))
1066 if (Constant *RC = dyn_cast<Constant>(RHS))
1067 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1068 return Insert(new ICmpInst(P, LHS, RHS), Name);
1070 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1071 const Twine &Name = "") {
1072 if (Constant *LC = dyn_cast<Constant>(LHS))
1073 if (Constant *RC = dyn_cast<Constant>(RHS))
1074 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1075 return Insert(new FCmpInst(P, LHS, RHS), Name);
1078 //===--------------------------------------------------------------------===//
1079 // Instruction creation methods: Other Instructions
1080 //===--------------------------------------------------------------------===//
1082 PHINode *CreatePHI(const Type *Ty, unsigned NumReservedValues,
1083 const Twine &Name = "") {
1084 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1087 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1088 return Insert(CallInst::Create(Callee), Name);
1090 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1091 return Insert(CallInst::Create(Callee, Arg), Name);
1093 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1094 const Twine &Name = "") {
1095 Value *Args[] = { Arg1, Arg2 };
1096 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
1098 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1099 const Twine &Name = "") {
1100 Value *Args[] = { Arg1, Arg2, Arg3 };
1101 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
1103 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1104 Value *Arg4, const Twine &Name = "") {
1105 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1106 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
1108 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1109 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1110 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1111 return Insert(CallInst::Create(Callee, Args, Args+5), Name);
1114 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Arg,
1115 const Twine &Name = "") {
1116 return Insert(CallInst::Create(Callee, Arg.begin(), Arg.end(), Name));
1119 template<typename RandomAccessIterator>
1120 CallInst *CreateCall(Value *Callee, RandomAccessIterator ArgBegin,
1121 RandomAccessIterator ArgEnd, const Twine &Name = "") {
1122 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
1125 Value *CreateSelect(Value *C, Value *True, Value *False,
1126 const Twine &Name = "") {
1127 if (Constant *CC = dyn_cast<Constant>(C))
1128 if (Constant *TC = dyn_cast<Constant>(True))
1129 if (Constant *FC = dyn_cast<Constant>(False))
1130 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1131 return Insert(SelectInst::Create(C, True, False), Name);
1134 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
1135 return Insert(new VAArgInst(List, Ty), Name);
1138 Value *CreateExtractElement(Value *Vec, Value *Idx,
1139 const Twine &Name = "") {
1140 if (Constant *VC = dyn_cast<Constant>(Vec))
1141 if (Constant *IC = dyn_cast<Constant>(Idx))
1142 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1143 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1146 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1147 const Twine &Name = "") {
1148 if (Constant *VC = dyn_cast<Constant>(Vec))
1149 if (Constant *NC = dyn_cast<Constant>(NewElt))
1150 if (Constant *IC = dyn_cast<Constant>(Idx))
1151 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1152 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1155 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1156 const Twine &Name = "") {
1157 if (Constant *V1C = dyn_cast<Constant>(V1))
1158 if (Constant *V2C = dyn_cast<Constant>(V2))
1159 if (Constant *MC = dyn_cast<Constant>(Mask))
1160 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1161 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1164 Value *CreateExtractValue(Value *Agg, unsigned Idx,
1165 const Twine &Name = "") {
1166 if (Constant *AggC = dyn_cast<Constant>(Agg))
1167 return Insert(Folder.CreateExtractValue(AggC, &Idx, 1), Name);
1168 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1171 template<typename RandomAccessIterator>
1172 Value *CreateExtractValue(Value *Agg,
1173 RandomAccessIterator IdxBegin,
1174 RandomAccessIterator IdxEnd,
1175 const Twine &Name = "") {
1176 if (Constant *AggC = dyn_cast<Constant>(Agg))
1177 return Insert(Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd-IdxBegin),
1179 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1182 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1183 const Twine &Name = "") {
1184 if (Constant *AggC = dyn_cast<Constant>(Agg))
1185 if (Constant *ValC = dyn_cast<Constant>(Val))
1186 return Insert(Folder.CreateInsertValue(AggC, ValC, &Idx, 1), Name);
1187 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1190 template<typename RandomAccessIterator>
1191 Value *CreateInsertValue(Value *Agg, Value *Val,
1192 RandomAccessIterator IdxBegin,
1193 RandomAccessIterator IdxEnd,
1194 const Twine &Name = "") {
1195 if (Constant *AggC = dyn_cast<Constant>(Agg))
1196 if (Constant *ValC = dyn_cast<Constant>(Val))
1197 return Insert(Folder.CreateInsertValue(AggC, ValC, IdxBegin,
1200 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1203 //===--------------------------------------------------------------------===//
1204 // Utility creation methods
1205 //===--------------------------------------------------------------------===//
1207 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1208 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1209 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1213 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1214 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1215 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1219 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1220 /// dividing out the size of the pointed-to objects. This is intended to
1221 /// implement C-style pointer subtraction. As such, the pointers must be
1222 /// appropriately aligned for their element types and pointing into the
1224 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1225 assert(LHS->getType() == RHS->getType() &&
1226 "Pointer subtraction operand types must match!");
1227 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1228 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1229 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1230 Value *Difference = CreateSub(LHS_int, RHS_int);
1231 return CreateExactSDiv(Difference,
1232 ConstantExpr::getSizeOf(ArgType->getElementType()),