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/Twine.h"
21 #include "llvm/Support/ConstantFolder.h"
26 /// IRBuilderDefaultInserter - This provides the default implementation of the
27 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
28 /// created by IRBuilder and needs to be inserted. By default, this inserts the
29 /// instruction at the insertion point.
30 template <bool preserveNames = true>
31 class IRBuilderDefaultInserter {
33 void InsertHelper(Instruction *I, const Twine &Name,
34 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
35 if (BB) BB->getInstList().insert(InsertPt, I);
41 /// IRBuilderBase - Common base class shared among various IRBuilders.
43 DebugLoc CurDbgLocation;
46 BasicBlock::iterator InsertPt;
50 IRBuilderBase(LLVMContext &context)
52 ClearInsertionPoint();
55 //===--------------------------------------------------------------------===//
56 // Builder configuration methods
57 //===--------------------------------------------------------------------===//
59 /// ClearInsertionPoint - Clear the insertion point: created instructions will
60 /// not be inserted into a block.
61 void ClearInsertionPoint() {
65 BasicBlock *GetInsertBlock() const { return BB; }
66 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
67 LLVMContext &getContext() const { return Context; }
69 /// SetInsertPoint - This specifies that created instructions should be
70 /// appended to the end of the specified block.
71 void SetInsertPoint(BasicBlock *TheBB) {
76 /// SetInsertPoint - This specifies that created instructions should be
77 /// inserted before the specified instruction.
78 void SetInsertPoint(Instruction *I) {
83 /// SetInsertPoint - This specifies that created instructions should be
84 /// inserted at the specified point.
85 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
90 /// SetCurrentDebugLocation - Set location information used by debugging
92 void SetCurrentDebugLocation(const DebugLoc &L) {
96 /// getCurrentDebugLocation - Get location information used by debugging
98 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
100 /// SetInstDebugLocation - If this builder has a current debug location, set
101 /// it on the specified instruction.
102 void SetInstDebugLocation(Instruction *I) const {
103 if (!CurDbgLocation.isUnknown())
104 I->setDebugLoc(CurDbgLocation);
107 /// InsertPoint - A saved insertion point.
110 BasicBlock::iterator Point;
113 /// Creates a new insertion point which doesn't point to anything.
114 InsertPoint() : Block(0) {}
116 /// Creates a new insertion point at the given location.
117 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
118 : Block(InsertBlock), Point(InsertPoint) {}
120 /// isSet - Returns true if this insert point is set.
121 bool isSet() const { return (Block != 0); }
123 llvm::BasicBlock *getBlock() const { return Block; }
124 llvm::BasicBlock::iterator getPoint() const { return Point; }
127 /// saveIP - Returns the current insert point.
128 InsertPoint saveIP() const {
129 return InsertPoint(GetInsertBlock(), GetInsertPoint());
132 /// saveAndClearIP - Returns the current insert point, clearing it
134 InsertPoint saveAndClearIP() {
135 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
136 ClearInsertionPoint();
140 /// restoreIP - Sets the current insert point to a previously-saved
142 void restoreIP(InsertPoint IP) {
144 SetInsertPoint(IP.getBlock(), IP.getPoint());
146 ClearInsertionPoint();
149 //===--------------------------------------------------------------------===//
150 // Miscellaneous creation methods.
151 //===--------------------------------------------------------------------===//
153 /// CreateGlobalString - Make a new global variable with an initializer that
154 /// has array of i8 type filled in with the nul terminated string value
155 /// specified. If Name is specified, it is the name of the global variable
157 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
159 /// getInt1 - Get a constant value representing either true or false.
160 ConstantInt *getInt1(bool V) {
161 return ConstantInt::get(getInt1Ty(), V);
164 /// getTrue - Get the constant value for i1 true.
165 ConstantInt *getTrue() {
166 return ConstantInt::getTrue(Context);
169 /// getFalse - Get the constant value for i1 false.
170 ConstantInt *getFalse() {
171 return ConstantInt::getFalse(Context);
174 /// getInt8 - Get a constant 8-bit value.
175 ConstantInt *getInt8(uint8_t C) {
176 return ConstantInt::get(getInt8Ty(), C);
179 /// getInt16 - Get a constant 16-bit value.
180 ConstantInt *getInt16(uint16_t C) {
181 return ConstantInt::get(getInt16Ty(), C);
184 /// getInt32 - Get a constant 32-bit value.
185 ConstantInt *getInt32(uint32_t C) {
186 return ConstantInt::get(getInt32Ty(), C);
189 /// getInt64 - Get a constant 64-bit value.
190 ConstantInt *getInt64(uint64_t C) {
191 return ConstantInt::get(getInt64Ty(), C);
194 //===--------------------------------------------------------------------===//
195 // Type creation methods
196 //===--------------------------------------------------------------------===//
198 /// getInt1Ty - Fetch the type representing a single bit
199 const IntegerType *getInt1Ty() {
200 return Type::getInt1Ty(Context);
203 /// getInt8Ty - Fetch the type representing an 8-bit integer.
204 const IntegerType *getInt8Ty() {
205 return Type::getInt8Ty(Context);
208 /// getInt16Ty - Fetch the type representing a 16-bit integer.
209 const IntegerType *getInt16Ty() {
210 return Type::getInt16Ty(Context);
213 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
214 const IntegerType *getInt32Ty() {
215 return Type::getInt32Ty(Context);
218 /// getInt64Ty - Fetch the type representing a 64-bit integer.
219 const IntegerType *getInt64Ty() {
220 return Type::getInt64Ty(Context);
223 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
224 const Type *getFloatTy() {
225 return Type::getFloatTy(Context);
228 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
229 const Type *getDoubleTy() {
230 return Type::getDoubleTy(Context);
233 /// getVoidTy - Fetch the type representing void.
234 const Type *getVoidTy() {
235 return Type::getVoidTy(Context);
238 const PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
239 return Type::getInt8PtrTy(Context, AddrSpace);
242 /// getCurrentFunctionReturnType - Get the return type of the current function
243 /// that we're emitting into.
244 const Type *getCurrentFunctionReturnType() const;
246 /// CreateMemSet - Create and insert a memset to the specified pointer and the
247 /// specified value. If the pointer isn't an i8*, it will be converted. If a
248 /// TBAA tag is specified, it will be added to the instruction.
249 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
250 bool isVolatile = false, MDNode *TBAATag = 0) {
251 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
254 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
255 bool isVolatile = false, MDNode *TBAATag = 0);
257 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
258 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
259 /// specified, it will be added to the instruction.
260 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
261 bool isVolatile = false, MDNode *TBAATag = 0) {
262 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
265 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
266 bool isVolatile = false, MDNode *TBAATag = 0);
268 /// CreateMemMove - Create and insert a memmove between the specified
269 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
270 /// tag is specified, it will be added to the instruction.
271 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
272 bool isVolatile = false, MDNode *TBAATag = 0) {
273 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
276 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
277 bool isVolatile = false, MDNode *TBAATag = 0);
279 Value *getCastedInt8PtrValue(Value *Ptr);
282 /// IRBuilder - This provides a uniform API for creating instructions and
283 /// inserting them into a basic block: either at the end of a BasicBlock, or
284 /// at a specific iterator location in a block.
286 /// Note that the builder does not expose the full generality of LLVM
287 /// instructions. For access to extra instruction properties, use the mutators
288 /// (e.g. setVolatile) on the instructions after they have been created.
289 /// The first template argument handles whether or not to preserve names in the
290 /// final instruction output. This defaults to on. The second template argument
291 /// specifies a class to use for creating constants. This defaults to creating
292 /// minimally folded constants. The fourth template argument allows clients to
293 /// specify custom insertion hooks that are called on every newly created
295 template<bool preserveNames = true, typename T = ConstantFolder,
296 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
297 class IRBuilder : public IRBuilderBase, public Inserter {
300 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
301 : IRBuilderBase(C), Inserter(I), Folder(F) {
304 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder() {
307 explicit IRBuilder(BasicBlock *TheBB, const T &F)
308 : IRBuilderBase(TheBB->getContext()), Folder(F) {
309 SetInsertPoint(TheBB);
312 explicit IRBuilder(BasicBlock *TheBB)
313 : IRBuilderBase(TheBB->getContext()), Folder() {
314 SetInsertPoint(TheBB);
317 explicit IRBuilder(Instruction *IP)
318 : IRBuilderBase(IP->getContext()), Folder() {
322 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
323 : IRBuilderBase(TheBB->getContext()), Folder(F) {
324 SetInsertPoint(TheBB, IP);
327 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
328 : IRBuilderBase(TheBB->getContext()), Folder() {
329 SetInsertPoint(TheBB, IP);
332 /// getFolder - Get the constant folder being used.
333 const T &getFolder() { return Folder; }
335 /// isNamePreserving - Return true if this builder is configured to actually
336 /// add the requested names to IR created through it.
337 bool isNamePreserving() const { return preserveNames; }
339 /// Insert - Insert and return the specified instruction.
340 template<typename InstTy>
341 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
342 this->InsertHelper(I, Name, BB, InsertPt);
343 if (!getCurrentDebugLocation().isUnknown())
344 this->SetInstDebugLocation(I);
348 /// Insert - No-op overload to handle constants.
349 Constant *Insert(Constant *C, const Twine& = "") const {
353 //===--------------------------------------------------------------------===//
354 // Instruction creation methods: Terminators
355 //===--------------------------------------------------------------------===//
357 /// CreateRetVoid - Create a 'ret void' instruction.
358 ReturnInst *CreateRetVoid() {
359 return Insert(ReturnInst::Create(Context));
363 /// CreateRet - Create a 'ret <val>' instruction.
365 ReturnInst *CreateRet(Value *V) {
366 return Insert(ReturnInst::Create(Context, V));
369 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
370 /// with one Value from the retVals array each, that build a aggregate
371 /// return value one value at a time, and a ret instruction to return
372 /// the resulting aggregate value. This is a convenience function for
373 /// code that uses aggregate return values as a vehicle for having
374 /// multiple return values.
376 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
377 Value *V = UndefValue::get(getCurrentFunctionReturnType());
378 for (unsigned i = 0; i != N; ++i)
379 V = CreateInsertValue(V, retVals[i], i, "mrv");
380 return Insert(ReturnInst::Create(Context, V));
383 /// CreateBr - Create an unconditional 'br label X' instruction.
384 BranchInst *CreateBr(BasicBlock *Dest) {
385 return Insert(BranchInst::Create(Dest));
388 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
390 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
391 return Insert(BranchInst::Create(True, False, Cond));
394 /// CreateSwitch - Create a switch instruction with the specified value,
395 /// default dest, and with a hint for the number of cases that will be added
396 /// (for efficient allocation).
397 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
398 return Insert(SwitchInst::Create(V, Dest, NumCases));
401 /// CreateIndirectBr - Create an indirect branch instruction with the
402 /// specified address operand, with an optional hint for the number of
403 /// destinations that will be added (for efficient allocation).
404 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
405 return Insert(IndirectBrInst::Create(Addr, NumDests));
408 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
409 BasicBlock *UnwindDest, const Twine &Name = "") {
410 Value *Args[] = { 0 };
411 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
414 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
415 BasicBlock *UnwindDest, Value *Arg1,
416 const Twine &Name = "") {
417 Value *Args[] = { Arg1 };
418 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
421 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
422 BasicBlock *UnwindDest, Value *Arg1,
423 Value *Arg2, Value *Arg3,
424 const Twine &Name = "") {
425 Value *Args[] = { Arg1, Arg2, Arg3 };
426 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
429 /// CreateInvoke - Create an invoke instruction.
430 template<typename RandomAccessIterator>
431 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
432 BasicBlock *UnwindDest,
433 RandomAccessIterator ArgBegin,
434 RandomAccessIterator ArgEnd,
435 const Twine &Name = "") {
436 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
437 ArgBegin, ArgEnd), Name);
440 UnwindInst *CreateUnwind() {
441 return Insert(new UnwindInst(Context));
444 UnreachableInst *CreateUnreachable() {
445 return Insert(new UnreachableInst(Context));
448 //===--------------------------------------------------------------------===//
449 // Instruction creation methods: Binary Operators
450 //===--------------------------------------------------------------------===//
452 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
453 Value *LHS, Value *RHS,
455 bool HasNUW, bool HasNSW) {
456 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
457 if (HasNUW) BO->setHasNoUnsignedWrap();
458 if (HasNSW) BO->setHasNoSignedWrap();
462 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
463 bool HasNUW = false, bool HasNSW = false) {
464 if (Constant *LC = dyn_cast<Constant>(LHS))
465 if (Constant *RC = dyn_cast<Constant>(RHS))
466 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
467 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
470 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
471 return CreateAdd(LHS, RHS, Name, false, true);
473 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
474 return CreateAdd(LHS, RHS, Name, true, false);
476 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
477 if (Constant *LC = dyn_cast<Constant>(LHS))
478 if (Constant *RC = dyn_cast<Constant>(RHS))
479 return Insert(Folder.CreateFAdd(LC, RC), Name);
480 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
482 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
483 bool HasNUW = false, bool HasNSW = false) {
484 if (Constant *LC = dyn_cast<Constant>(LHS))
485 if (Constant *RC = dyn_cast<Constant>(RHS))
486 return Insert(Folder.CreateSub(LC, RC), Name);
487 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
490 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
491 return CreateSub(LHS, RHS, Name, false, true);
493 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
494 return CreateSub(LHS, RHS, Name, true, false);
496 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
497 if (Constant *LC = dyn_cast<Constant>(LHS))
498 if (Constant *RC = dyn_cast<Constant>(RHS))
499 return Insert(Folder.CreateFSub(LC, RC), Name);
500 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
502 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
503 bool HasNUW = false, bool HasNSW = false) {
504 if (Constant *LC = dyn_cast<Constant>(LHS))
505 if (Constant *RC = dyn_cast<Constant>(RHS))
506 return Insert(Folder.CreateMul(LC, RC), Name);
507 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
510 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
511 return CreateMul(LHS, RHS, Name, false, true);
513 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
514 return CreateMul(LHS, RHS, Name, true, false);
516 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
517 if (Constant *LC = dyn_cast<Constant>(LHS))
518 if (Constant *RC = dyn_cast<Constant>(RHS))
519 return Insert(Folder.CreateFMul(LC, RC), Name);
520 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
522 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
523 bool isExact = false) {
524 if (Constant *LC = dyn_cast<Constant>(LHS))
525 if (Constant *RC = dyn_cast<Constant>(RHS))
526 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
528 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
529 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
531 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
532 return CreateUDiv(LHS, RHS, Name, true);
534 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
535 bool isExact = false) {
536 if (Constant *LC = dyn_cast<Constant>(LHS))
537 if (Constant *RC = dyn_cast<Constant>(RHS))
538 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
540 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
541 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
543 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
544 return CreateSDiv(LHS, RHS, Name, true);
546 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
547 if (Constant *LC = dyn_cast<Constant>(LHS))
548 if (Constant *RC = dyn_cast<Constant>(RHS))
549 return Insert(Folder.CreateFDiv(LC, RC), Name);
550 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
552 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
553 if (Constant *LC = dyn_cast<Constant>(LHS))
554 if (Constant *RC = dyn_cast<Constant>(RHS))
555 return Insert(Folder.CreateURem(LC, RC), Name);
556 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
558 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
559 if (Constant *LC = dyn_cast<Constant>(LHS))
560 if (Constant *RC = dyn_cast<Constant>(RHS))
561 return Insert(Folder.CreateSRem(LC, RC), Name);
562 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
564 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
565 if (Constant *LC = dyn_cast<Constant>(LHS))
566 if (Constant *RC = dyn_cast<Constant>(RHS))
567 return Insert(Folder.CreateFRem(LC, RC), Name);
568 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
571 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
572 bool HasNUW = false, bool HasNSW = false) {
573 if (Constant *LC = dyn_cast<Constant>(LHS))
574 if (Constant *RC = dyn_cast<Constant>(RHS))
575 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
576 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
579 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
580 bool HasNUW = false, bool HasNSW = false) {
581 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
584 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
585 bool HasNUW = false, bool HasNSW = false) {
586 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
590 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
591 bool isExact = false) {
592 if (Constant *LC = dyn_cast<Constant>(LHS))
593 if (Constant *RC = dyn_cast<Constant>(RHS))
594 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
596 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
597 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
599 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
600 bool isExact = false) {
601 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
603 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
604 bool isExact = false) {
605 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
608 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
609 bool isExact = false) {
610 if (Constant *LC = dyn_cast<Constant>(LHS))
611 if (Constant *RC = dyn_cast<Constant>(RHS))
612 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
614 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
615 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
617 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
618 bool isExact = false) {
619 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
621 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
622 bool isExact = false) {
623 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
626 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
627 if (Constant *RC = dyn_cast<Constant>(RHS)) {
628 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
629 return LHS; // LHS & -1 -> LHS
630 if (Constant *LC = dyn_cast<Constant>(LHS))
631 return Insert(Folder.CreateAnd(LC, RC), Name);
633 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
635 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
636 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
638 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
639 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
642 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
643 if (Constant *RC = dyn_cast<Constant>(RHS)) {
644 if (RC->isNullValue())
645 return LHS; // LHS | 0 -> LHS
646 if (Constant *LC = dyn_cast<Constant>(LHS))
647 return Insert(Folder.CreateOr(LC, RC), Name);
649 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
651 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
652 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
654 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
655 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
658 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
659 if (Constant *LC = dyn_cast<Constant>(LHS))
660 if (Constant *RC = dyn_cast<Constant>(RHS))
661 return Insert(Folder.CreateXor(LC, RC), Name);
662 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
664 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
665 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
667 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
668 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
671 Value *CreateBinOp(Instruction::BinaryOps Opc,
672 Value *LHS, Value *RHS, const Twine &Name = "") {
673 if (Constant *LC = dyn_cast<Constant>(LHS))
674 if (Constant *RC = dyn_cast<Constant>(RHS))
675 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
676 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
679 Value *CreateNeg(Value *V, const Twine &Name = "",
680 bool HasNUW = false, bool HasNSW = false) {
681 if (Constant *VC = dyn_cast<Constant>(V))
682 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
683 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
684 if (HasNUW) BO->setHasNoUnsignedWrap();
685 if (HasNSW) BO->setHasNoSignedWrap();
688 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
689 return CreateNeg(V, Name, false, true);
691 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
692 return CreateNeg(V, Name, true, false);
694 Value *CreateFNeg(Value *V, const Twine &Name = "") {
695 if (Constant *VC = dyn_cast<Constant>(V))
696 return Insert(Folder.CreateFNeg(VC), Name);
697 return Insert(BinaryOperator::CreateFNeg(V), Name);
699 Value *CreateNot(Value *V, const Twine &Name = "") {
700 if (Constant *VC = dyn_cast<Constant>(V))
701 return Insert(Folder.CreateNot(VC), Name);
702 return Insert(BinaryOperator::CreateNot(V), Name);
705 //===--------------------------------------------------------------------===//
706 // Instruction creation methods: Memory Instructions
707 //===--------------------------------------------------------------------===//
709 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
710 const Twine &Name = "") {
711 return Insert(new AllocaInst(Ty, ArraySize), Name);
713 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
714 // converting the string to 'bool' for the isVolatile parameter.
715 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
716 return Insert(new LoadInst(Ptr), Name);
718 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
719 return Insert(new LoadInst(Ptr), Name);
721 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
722 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
724 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
725 return Insert(new StoreInst(Val, Ptr, isVolatile));
727 template<typename RandomAccessIterator>
728 Value *CreateGEP(Value *Ptr,
729 RandomAccessIterator IdxBegin,
730 RandomAccessIterator IdxEnd,
731 const Twine &Name = "") {
732 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
733 // Every index must be constant.
734 RandomAccessIterator i;
735 for (i = IdxBegin; i < IdxEnd; ++i)
736 if (!isa<Constant>(*i))
739 return Insert(Folder.CreateGetElementPtr(PC, &IdxBegin[0],
743 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
745 template<typename RandomAccessIterator>
746 Value *CreateInBoundsGEP(Value *Ptr, RandomAccessIterator IdxBegin,
747 RandomAccessIterator IdxEnd,
748 const Twine &Name = "") {
749 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
750 // Every index must be constant.
751 RandomAccessIterator i;
752 for (i = IdxBegin; i < IdxEnd; ++i)
753 if (!isa<Constant>(*i))
756 return Insert(Folder.CreateInBoundsGetElementPtr(PC,
761 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
764 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
765 if (Constant *PC = dyn_cast<Constant>(Ptr))
766 if (Constant *IC = dyn_cast<Constant>(Idx))
767 return Insert(Folder.CreateGetElementPtr(PC, &IC, 1), Name);
768 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
770 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
771 if (Constant *PC = dyn_cast<Constant>(Ptr))
772 if (Constant *IC = dyn_cast<Constant>(Idx))
773 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &IC, 1), Name);
774 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
776 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
777 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
779 if (Constant *PC = dyn_cast<Constant>(Ptr))
780 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
782 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
784 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
785 const Twine &Name = "") {
786 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
788 if (Constant *PC = dyn_cast<Constant>(Ptr))
789 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
791 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
793 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
794 const Twine &Name = "") {
796 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
797 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
800 if (Constant *PC = dyn_cast<Constant>(Ptr))
801 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
803 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
805 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
806 const Twine &Name = "") {
808 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
809 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
812 if (Constant *PC = dyn_cast<Constant>(Ptr))
813 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
815 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
817 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
818 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
820 if (Constant *PC = dyn_cast<Constant>(Ptr))
821 return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
823 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
825 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
826 const Twine &Name = "") {
827 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
829 if (Constant *PC = dyn_cast<Constant>(Ptr))
830 return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
832 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
834 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
835 const Twine &Name = "") {
837 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
838 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
841 if (Constant *PC = dyn_cast<Constant>(Ptr))
842 return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
844 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
846 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
847 const Twine &Name = "") {
849 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
850 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
853 if (Constant *PC = dyn_cast<Constant>(Ptr))
854 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
856 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
858 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
859 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
862 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
863 /// with "i8*" type instead of a pointer to array of i8.
864 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
865 Value *gv = CreateGlobalString(Str, Name);
866 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
867 Value *Args[] = { zero, zero };
868 return CreateInBoundsGEP(gv, Args, Args+2, Name);
871 //===--------------------------------------------------------------------===//
872 // Instruction creation methods: Cast/Conversion Operators
873 //===--------------------------------------------------------------------===//
875 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
876 return CreateCast(Instruction::Trunc, V, DestTy, Name);
878 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
879 return CreateCast(Instruction::ZExt, V, DestTy, Name);
881 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
882 return CreateCast(Instruction::SExt, V, DestTy, Name);
884 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
885 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
887 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
888 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
890 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
891 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
893 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
894 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
896 Value *CreateFPTrunc(Value *V, const Type *DestTy,
897 const Twine &Name = "") {
898 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
900 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
901 return CreateCast(Instruction::FPExt, V, DestTy, Name);
903 Value *CreatePtrToInt(Value *V, const Type *DestTy,
904 const Twine &Name = "") {
905 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
907 Value *CreateIntToPtr(Value *V, const Type *DestTy,
908 const Twine &Name = "") {
909 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
911 Value *CreateBitCast(Value *V, const Type *DestTy,
912 const Twine &Name = "") {
913 return CreateCast(Instruction::BitCast, V, DestTy, Name);
915 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
916 const Twine &Name = "") {
917 if (V->getType() == DestTy)
919 if (Constant *VC = dyn_cast<Constant>(V))
920 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
921 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
923 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
924 const Twine &Name = "") {
925 if (V->getType() == DestTy)
927 if (Constant *VC = dyn_cast<Constant>(V))
928 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
929 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
931 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
932 const Twine &Name = "") {
933 if (V->getType() == DestTy)
935 if (Constant *VC = dyn_cast<Constant>(V))
936 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
937 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
939 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
940 const Twine &Name = "") {
941 if (V->getType() == DestTy)
943 if (Constant *VC = dyn_cast<Constant>(V))
944 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
945 return Insert(CastInst::Create(Op, V, DestTy), Name);
947 Value *CreatePointerCast(Value *V, const Type *DestTy,
948 const Twine &Name = "") {
949 if (V->getType() == DestTy)
951 if (Constant *VC = dyn_cast<Constant>(V))
952 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
953 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
955 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
956 const Twine &Name = "") {
957 if (V->getType() == DestTy)
959 if (Constant *VC = dyn_cast<Constant>(V))
960 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
961 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
964 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
965 // error, instead of converting the string to bool for the isSigned parameter.
966 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
968 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
969 if (V->getType() == DestTy)
971 if (Constant *VC = dyn_cast<Constant>(V))
972 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
973 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
976 //===--------------------------------------------------------------------===//
977 // Instruction creation methods: Compare Instructions
978 //===--------------------------------------------------------------------===//
980 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
981 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
983 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
984 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
986 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
987 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
989 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
990 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
992 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
993 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
995 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
996 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
998 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
999 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1001 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1002 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1004 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1005 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1007 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1008 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1011 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1012 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1014 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1015 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1017 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1018 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1020 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1021 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1023 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1024 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1026 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1027 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1029 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1030 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1032 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1033 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1035 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1036 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1038 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1039 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1041 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1042 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1044 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1045 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1047 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1048 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1050 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1051 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1054 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1055 const Twine &Name = "") {
1056 if (Constant *LC = dyn_cast<Constant>(LHS))
1057 if (Constant *RC = dyn_cast<Constant>(RHS))
1058 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1059 return Insert(new ICmpInst(P, LHS, RHS), Name);
1061 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1062 const Twine &Name = "") {
1063 if (Constant *LC = dyn_cast<Constant>(LHS))
1064 if (Constant *RC = dyn_cast<Constant>(RHS))
1065 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1066 return Insert(new FCmpInst(P, LHS, RHS), Name);
1069 //===--------------------------------------------------------------------===//
1070 // Instruction creation methods: Other Instructions
1071 //===--------------------------------------------------------------------===//
1073 PHINode *CreatePHI(const Type *Ty, unsigned NumReservedValues,
1074 const Twine &Name = "") {
1075 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1078 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1079 return Insert(CallInst::Create(Callee), Name);
1081 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1082 return Insert(CallInst::Create(Callee, Arg), Name);
1084 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1085 const Twine &Name = "") {
1086 Value *Args[] = { Arg1, Arg2 };
1087 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
1089 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1090 const Twine &Name = "") {
1091 Value *Args[] = { Arg1, Arg2, Arg3 };
1092 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
1094 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1095 Value *Arg4, const Twine &Name = "") {
1096 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1097 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
1099 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1100 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1101 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1102 return Insert(CallInst::Create(Callee, Args, Args+5), Name);
1105 template<typename RandomAccessIterator>
1106 CallInst *CreateCall(Value *Callee, RandomAccessIterator ArgBegin,
1107 RandomAccessIterator ArgEnd, const Twine &Name = "") {
1108 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
1111 Value *CreateSelect(Value *C, Value *True, Value *False,
1112 const Twine &Name = "") {
1113 if (Constant *CC = dyn_cast<Constant>(C))
1114 if (Constant *TC = dyn_cast<Constant>(True))
1115 if (Constant *FC = dyn_cast<Constant>(False))
1116 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1117 return Insert(SelectInst::Create(C, True, False), Name);
1120 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
1121 return Insert(new VAArgInst(List, Ty), Name);
1124 Value *CreateExtractElement(Value *Vec, Value *Idx,
1125 const Twine &Name = "") {
1126 if (Constant *VC = dyn_cast<Constant>(Vec))
1127 if (Constant *IC = dyn_cast<Constant>(Idx))
1128 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1129 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1132 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1133 const Twine &Name = "") {
1134 if (Constant *VC = dyn_cast<Constant>(Vec))
1135 if (Constant *NC = dyn_cast<Constant>(NewElt))
1136 if (Constant *IC = dyn_cast<Constant>(Idx))
1137 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1138 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1141 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1142 const Twine &Name = "") {
1143 if (Constant *V1C = dyn_cast<Constant>(V1))
1144 if (Constant *V2C = dyn_cast<Constant>(V2))
1145 if (Constant *MC = dyn_cast<Constant>(Mask))
1146 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1147 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1150 Value *CreateExtractValue(Value *Agg, unsigned Idx,
1151 const Twine &Name = "") {
1152 if (Constant *AggC = dyn_cast<Constant>(Agg))
1153 return Insert(Folder.CreateExtractValue(AggC, &Idx, 1), Name);
1154 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1157 template<typename RandomAccessIterator>
1158 Value *CreateExtractValue(Value *Agg,
1159 RandomAccessIterator IdxBegin,
1160 RandomAccessIterator IdxEnd,
1161 const Twine &Name = "") {
1162 if (Constant *AggC = dyn_cast<Constant>(Agg))
1163 return Insert(Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd-IdxBegin),
1165 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1168 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1169 const Twine &Name = "") {
1170 if (Constant *AggC = dyn_cast<Constant>(Agg))
1171 if (Constant *ValC = dyn_cast<Constant>(Val))
1172 return Insert(Folder.CreateInsertValue(AggC, ValC, &Idx, 1), Name);
1173 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1176 template<typename RandomAccessIterator>
1177 Value *CreateInsertValue(Value *Agg, Value *Val,
1178 RandomAccessIterator IdxBegin,
1179 RandomAccessIterator IdxEnd,
1180 const Twine &Name = "") {
1181 if (Constant *AggC = dyn_cast<Constant>(Agg))
1182 if (Constant *ValC = dyn_cast<Constant>(Val))
1183 return Insert(Folder.CreateInsertValue(AggC, ValC, IdxBegin,
1186 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1189 //===--------------------------------------------------------------------===//
1190 // Utility creation methods
1191 //===--------------------------------------------------------------------===//
1193 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1194 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1195 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1199 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1200 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1201 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1205 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1206 /// dividing out the size of the pointed-to objects. This is intended to
1207 /// implement C-style pointer subtraction. As such, the pointers must be
1208 /// appropriately aligned for their element types and pointing into the
1210 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1211 assert(LHS->getType() == RHS->getType() &&
1212 "Pointer subtraction operand types must match!");
1213 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1214 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1215 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1216 Value *Difference = CreateSub(LHS_int, RHS_int);
1217 return CreateExactSDiv(Difference,
1218 ConstantExpr::getSizeOf(ArgType->getElementType()),