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/Constants.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/GlobalAlias.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Function.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Support/ConstantFolder.h"
28 /// IRBuilder - This provides a uniform API for creating instructions and
29 /// inserting them into a basic block: either at the end of a BasicBlock, or
30 /// at a specific iterator location in a block.
32 /// Note that the builder does not expose the full generality of LLVM
33 /// instructions. For example, it cannot be used to create instructions with
34 /// arbitrary names (specifically, names with nul characters in them) - It only
35 /// supports nul-terminated C strings. For fully generic names, use
36 /// I->setName(). For access to extra instruction properties, use the mutators
37 /// (e.g. setVolatile) on the instructions after they have been created.
38 /// The first template argument handles whether or not to preserve names in the
39 /// final instruction output. This defaults to on. The second template argument
40 /// specifies a class to use for creating constants. This defaults to creating
41 /// minimally folded constants.
42 template <bool preserveNames=true, typename T = ConstantFolder> class IRBuilder{
44 BasicBlock::iterator InsertPt;
48 IRBuilder(LLVMContext &C, const T& F) :
49 Context(C), Folder(F) { ClearInsertionPoint(); }
51 explicit IRBuilder(LLVMContext &C) : Context(C), Folder(C) {
52 ClearInsertionPoint();
55 explicit IRBuilder(BasicBlock *TheBB, const T& F)
56 : Context(TheBB->getContext()), Folder(F) {
57 SetInsertPoint(TheBB);
60 explicit IRBuilder(BasicBlock *TheBB)
61 : Context(TheBB->getContext()), Folder(Context) {
62 SetInsertPoint(TheBB);
65 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
66 : Context(TheBB->getContext()), Folder(F) {
67 SetInsertPoint(TheBB, IP);
70 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
71 : Context(TheBB->getContext()), Folder(Context) {
72 SetInsertPoint(TheBB, IP);
75 /// getFolder - Get the constant folder being used.
76 const T& getFolder() { return Folder; }
78 /// isNamePreserving - Return true if this builder is configured to actually
79 /// add the requested names to IR created through it.
80 bool isNamePreserving() const { return preserveNames; }
82 //===--------------------------------------------------------------------===//
83 // Builder configuration methods
84 //===--------------------------------------------------------------------===//
86 /// ClearInsertionPoint - Clear the insertion point: created instructions will
87 /// not be inserted into a block.
88 void ClearInsertionPoint() {
92 BasicBlock *GetInsertBlock() const { return BB; }
94 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
96 /// SetInsertPoint - This specifies that created instructions should be
97 /// appended to the end of the specified block.
98 void SetInsertPoint(BasicBlock *TheBB) {
100 InsertPt = BB->end();
103 /// SetInsertPoint - This specifies that created instructions should be
104 /// inserted at the specified point.
105 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
110 /// Insert - Insert and return the specified instruction.
111 template<typename InstTy>
112 InstTy *Insert(InstTy *I, const char *Name = "") const {
113 InsertHelper(I, Name);
117 /// InsertHelper - Insert the specified instruction at the specified insertion
118 /// point. This is split out of Insert so that it isn't duplicated for every
119 /// template instantiation.
120 void InsertHelper(Instruction *I, const char *Name) const {
121 if (BB) BB->getInstList().insert(InsertPt, I);
122 if (preserveNames && Name[0])
126 //===--------------------------------------------------------------------===//
127 // Type creation methods
128 //===--------------------------------------------------------------------===//
130 const Type *getInt1Ty() {
131 return Type::getInt1Ty(Context);
134 const Type *getInt8Ty() {
135 return Type::getInt8Ty(Context);
138 const Type *getInt16Ty() {
139 return Type::getInt16Ty(Context);
142 const Type *getInt32Ty() {
143 return Type::getInt32Ty(Context);
146 const Type *getInt64Ty() {
147 return Type::getInt64Ty(Context);
150 const Type *getFloatTy() {
151 return Type::getFloatTy(Context);
154 const Type *getDoubleTy() {
155 return Type::getDoubleTy(Context);
158 const Type *getVoidTy() {
159 return Type::getVoidTy(Context);
162 //===--------------------------------------------------------------------===//
163 // Instruction creation methods: Terminators
164 //===--------------------------------------------------------------------===//
166 /// CreateRetVoid - Create a 'ret void' instruction.
167 ReturnInst *CreateRetVoid() {
168 return Insert(ReturnInst::Create(Context));
172 /// CreateRet - Create a 'ret <val>' instruction.
174 ReturnInst *CreateRet(Value *V) {
175 return Insert(ReturnInst::Create(Context, V));
178 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
179 /// with one Value from the retVals array each, that build a aggregate
180 /// return value one value at a time, and a ret instruction to return
181 /// the resulting aggregate value. This is a convenience function for
182 /// code that uses aggregate return values as a vehicle for having
183 /// multiple return values.
185 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
186 const Type *RetType = BB->getParent()->getReturnType();
187 Value *V = UndefValue::get(RetType);
188 for (unsigned i = 0; i != N; ++i)
189 V = CreateInsertValue(V, retVals[i], i, "mrv");
190 return Insert(ReturnInst::Create(Context, V));
193 /// CreateBr - Create an unconditional 'br label X' instruction.
194 BranchInst *CreateBr(BasicBlock *Dest) {
195 return Insert(BranchInst::Create(Dest));
198 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
200 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
201 return Insert(BranchInst::Create(True, False, Cond));
204 /// CreateSwitch - Create a switch instruction with the specified value,
205 /// default dest, and with a hint for the number of cases that will be added
206 /// (for efficient allocation).
207 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
208 return Insert(SwitchInst::Create(V, Dest, NumCases));
211 /// CreateInvoke - Create an invoke instruction.
212 template<typename InputIterator>
213 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
214 BasicBlock *UnwindDest, InputIterator ArgBegin,
215 InputIterator ArgEnd, const char *Name = "") {
216 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
217 ArgBegin, ArgEnd), Name);
220 UnwindInst *CreateUnwind() {
221 return Insert(new UnwindInst(Context));
224 UnreachableInst *CreateUnreachable() {
225 return Insert(new UnreachableInst(Context));
228 //===--------------------------------------------------------------------===//
229 // Instruction creation methods: Binary Operators
230 //===--------------------------------------------------------------------===//
232 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
233 if (Constant *LC = dyn_cast<Constant>(LHS))
234 if (Constant *RC = dyn_cast<Constant>(RHS))
235 return Folder.CreateAdd(LC, RC);
236 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
238 Value *CreateNSWAdd(Value *LHS, Value *RHS, const char *Name = "") {
239 if (Constant *LC = dyn_cast<Constant>(LHS))
240 if (Constant *RC = dyn_cast<Constant>(RHS))
241 return Folder.CreateNSWAdd(LC, RC);
242 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
244 Value *CreateFAdd(Value *LHS, Value *RHS, const char *Name = "") {
245 if (Constant *LC = dyn_cast<Constant>(LHS))
246 if (Constant *RC = dyn_cast<Constant>(RHS))
247 return Folder.CreateFAdd(LC, RC);
248 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
250 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
251 if (Constant *LC = dyn_cast<Constant>(LHS))
252 if (Constant *RC = dyn_cast<Constant>(RHS))
253 return Folder.CreateSub(LC, RC);
254 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
256 Value *CreateFSub(Value *LHS, Value *RHS, const char *Name = "") {
257 if (Constant *LC = dyn_cast<Constant>(LHS))
258 if (Constant *RC = dyn_cast<Constant>(RHS))
259 return Folder.CreateFSub(LC, RC);
260 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
262 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
263 if (Constant *LC = dyn_cast<Constant>(LHS))
264 if (Constant *RC = dyn_cast<Constant>(RHS))
265 return Folder.CreateMul(LC, RC);
266 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
268 Value *CreateFMul(Value *LHS, Value *RHS, const char *Name = "") {
269 if (Constant *LC = dyn_cast<Constant>(LHS))
270 if (Constant *RC = dyn_cast<Constant>(RHS))
271 return Folder.CreateFMul(LC, RC);
272 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
274 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
275 if (Constant *LC = dyn_cast<Constant>(LHS))
276 if (Constant *RC = dyn_cast<Constant>(RHS))
277 return Folder.CreateUDiv(LC, RC);
278 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
280 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
281 if (Constant *LC = dyn_cast<Constant>(LHS))
282 if (Constant *RC = dyn_cast<Constant>(RHS))
283 return Folder.CreateSDiv(LC, RC);
284 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
286 Value *CreateExactSDiv(Value *LHS, Value *RHS, const char *Name = "") {
287 if (Constant *LC = dyn_cast<Constant>(LHS))
288 if (Constant *RC = dyn_cast<Constant>(RHS))
289 return Folder.CreateExactSDiv(LC, RC);
290 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
292 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
293 if (Constant *LC = dyn_cast<Constant>(LHS))
294 if (Constant *RC = dyn_cast<Constant>(RHS))
295 return Folder.CreateFDiv(LC, RC);
296 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
298 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
299 if (Constant *LC = dyn_cast<Constant>(LHS))
300 if (Constant *RC = dyn_cast<Constant>(RHS))
301 return Folder.CreateURem(LC, RC);
302 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
304 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
305 if (Constant *LC = dyn_cast<Constant>(LHS))
306 if (Constant *RC = dyn_cast<Constant>(RHS))
307 return Folder.CreateSRem(LC, RC);
308 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
310 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
311 if (Constant *LC = dyn_cast<Constant>(LHS))
312 if (Constant *RC = dyn_cast<Constant>(RHS))
313 return Folder.CreateFRem(LC, RC);
314 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
316 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
317 if (Constant *LC = dyn_cast<Constant>(LHS))
318 if (Constant *RC = dyn_cast<Constant>(RHS))
319 return Folder.CreateShl(LC, RC);
320 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
322 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
323 if (Constant *LC = dyn_cast<Constant>(LHS))
324 if (Constant *RC = dyn_cast<Constant>(RHS))
325 return Folder.CreateLShr(LC, RC);
326 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
328 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
329 if (Constant *LC = dyn_cast<Constant>(LHS))
330 if (Constant *RC = dyn_cast<Constant>(RHS))
331 return Folder.CreateAShr(LC, RC);
332 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
334 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
335 if (Constant *LC = dyn_cast<Constant>(LHS))
336 if (Constant *RC = dyn_cast<Constant>(RHS))
337 return Folder.CreateAnd(LC, RC);
338 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
340 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
341 if (Constant *LC = dyn_cast<Constant>(LHS))
342 if (Constant *RC = dyn_cast<Constant>(RHS))
343 return Folder.CreateOr(LC, RC);
344 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
346 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
347 if (Constant *LC = dyn_cast<Constant>(LHS))
348 if (Constant *RC = dyn_cast<Constant>(RHS))
349 return Folder.CreateXor(LC, RC);
350 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
353 Value *CreateBinOp(Instruction::BinaryOps Opc,
354 Value *LHS, Value *RHS, const char *Name = "") {
355 if (Constant *LC = dyn_cast<Constant>(LHS))
356 if (Constant *RC = dyn_cast<Constant>(RHS))
357 return Folder.CreateBinOp(Opc, LC, RC);
358 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
361 Value *CreateNeg(Value *V, const char *Name = "") {
362 if (Constant *VC = dyn_cast<Constant>(V))
363 return Folder.CreateNeg(VC);
364 return Insert(BinaryOperator::CreateNeg(V), Name);
366 Value *CreateFNeg(Value *V, const char *Name = "") {
367 if (Constant *VC = dyn_cast<Constant>(V))
368 return Folder.CreateFNeg(VC);
369 return Insert(BinaryOperator::CreateFNeg(V), Name);
371 Value *CreateNot(Value *V, const char *Name = "") {
372 if (Constant *VC = dyn_cast<Constant>(V))
373 return Folder.CreateNot(VC);
374 return Insert(BinaryOperator::CreateNot(V), Name);
377 //===--------------------------------------------------------------------===//
378 // Instruction creation methods: Memory Instructions
379 //===--------------------------------------------------------------------===//
381 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
382 const char *Name = "") {
383 return Insert(new MallocInst(Ty, ArraySize), Name);
385 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
386 const char *Name = "") {
387 return Insert(new AllocaInst(Ty, ArraySize), Name);
389 FreeInst *CreateFree(Value *Ptr) {
390 return Insert(new FreeInst(Ptr));
392 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
393 return Insert(new LoadInst(Ptr), Name);
395 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
396 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
398 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
399 return Insert(new StoreInst(Val, Ptr, isVolatile));
401 template<typename InputIterator>
402 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
403 const char *Name = "") {
404 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
405 // Every index must be constant.
407 for (i = IdxBegin; i < IdxEnd; ++i)
408 if (!isa<Constant>(*i))
411 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
413 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
415 template<typename InputIterator>
416 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
417 const char *Name = "") {
418 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
419 // Every index must be constant.
421 for (i = IdxBegin; i < IdxEnd; ++i)
422 if (!isa<Constant>(*i))
425 return Folder.CreateInBoundsGetElementPtr(PC,
429 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
432 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
433 if (Constant *PC = dyn_cast<Constant>(Ptr))
434 if (Constant *IC = dyn_cast<Constant>(Idx))
435 return Folder.CreateGetElementPtr(PC, &IC, 1);
436 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
438 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const char *Name = "") {
439 if (Constant *PC = dyn_cast<Constant>(Ptr))
440 if (Constant *IC = dyn_cast<Constant>(Idx))
441 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
442 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
444 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
445 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
447 if (Constant *PC = dyn_cast<Constant>(Ptr))
448 return Folder.CreateGetElementPtr(PC, &Idx, 1);
450 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
452 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
453 const char *Name = "") {
454 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
456 if (Constant *PC = dyn_cast<Constant>(Ptr))
457 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
459 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
461 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
462 const char *Name = "") {
464 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
465 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
468 if (Constant *PC = dyn_cast<Constant>(Ptr))
469 return Folder.CreateGetElementPtr(PC, Idxs, 2);
471 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
473 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
474 const char *Name = "") {
476 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
477 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
480 if (Constant *PC = dyn_cast<Constant>(Ptr))
481 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
483 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
485 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
486 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
488 if (Constant *PC = dyn_cast<Constant>(Ptr))
489 return Folder.CreateGetElementPtr(PC, &Idx, 1);
491 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
493 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
494 const char *Name = "") {
495 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
497 if (Constant *PC = dyn_cast<Constant>(Ptr))
498 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
500 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
502 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
503 const char *Name = "") {
505 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
506 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
509 if (Constant *PC = dyn_cast<Constant>(Ptr))
510 return Folder.CreateGetElementPtr(PC, Idxs, 2);
512 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
514 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
515 const char *Name = "") {
517 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
518 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
521 if (Constant *PC = dyn_cast<Constant>(Ptr))
522 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
524 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
526 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
527 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
529 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
530 Constant *StrConstant = ConstantArray::get(Context, Str, true);
531 Module &M = *BB->getParent()->getParent();
532 GlobalVariable *gv = new GlobalVariable(M,
533 StrConstant->getType(),
535 GlobalValue::InternalLinkage,
543 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
544 Value *gv = CreateGlobalString(Str, Name);
545 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
546 Value *Args[] = { zero, zero };
547 return CreateInBoundsGEP(gv, Args, Args+2, Name);
549 //===--------------------------------------------------------------------===//
550 // Instruction creation methods: Cast/Conversion Operators
551 //===--------------------------------------------------------------------===//
553 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
554 return CreateCast(Instruction::Trunc, V, DestTy, Name);
556 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
557 return CreateCast(Instruction::ZExt, V, DestTy, Name);
559 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
560 return CreateCast(Instruction::SExt, V, DestTy, Name);
562 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
563 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
565 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
566 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
568 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
569 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
571 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
572 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
574 Value *CreateFPTrunc(Value *V, const Type *DestTy,
575 const char *Name = "") {
576 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
578 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
579 return CreateCast(Instruction::FPExt, V, DestTy, Name);
581 Value *CreatePtrToInt(Value *V, const Type *DestTy,
582 const char *Name = "") {
583 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
585 Value *CreateIntToPtr(Value *V, const Type *DestTy,
586 const char *Name = "") {
587 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
589 Value *CreateBitCast(Value *V, const Type *DestTy,
590 const char *Name = "") {
591 return CreateCast(Instruction::BitCast, V, DestTy, Name);
594 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
595 const char *Name = "") {
596 if (V->getType() == DestTy)
598 if (Constant *VC = dyn_cast<Constant>(V))
599 return Folder.CreateCast(Op, VC, DestTy);
600 return Insert(CastInst::Create(Op, V, DestTy), Name);
602 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
603 const char *Name = "") {
604 if (V->getType() == DestTy)
606 if (Constant *VC = dyn_cast<Constant>(V))
607 return Folder.CreateIntCast(VC, DestTy, isSigned);
608 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
611 //===--------------------------------------------------------------------===//
612 // Instruction creation methods: Compare Instructions
613 //===--------------------------------------------------------------------===//
615 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
616 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
618 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
619 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
621 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
622 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
624 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
625 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
627 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
628 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
630 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
631 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
633 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
634 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
636 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
637 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
639 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
640 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
642 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
643 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
646 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
647 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
649 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
650 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
652 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
653 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
655 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
656 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
658 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
659 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
661 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
662 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
664 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
665 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
667 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
668 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
670 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
671 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
673 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
674 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
676 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
677 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
679 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
680 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
682 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
683 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
685 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
686 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
689 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
690 const char *Name = "") {
691 if (Constant *LC = dyn_cast<Constant>(LHS))
692 if (Constant *RC = dyn_cast<Constant>(RHS))
693 return Folder.CreateICmp(P, LC, RC);
694 return Insert(new ICmpInst(Context, P, LHS, RHS), Name);
696 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
697 const char *Name = "") {
698 if (Constant *LC = dyn_cast<Constant>(LHS))
699 if (Constant *RC = dyn_cast<Constant>(RHS))
700 return Folder.CreateFCmp(P, LC, RC);
701 return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
704 //===--------------------------------------------------------------------===//
705 // Instruction creation methods: Other Instructions
706 //===--------------------------------------------------------------------===//
708 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
709 return Insert(PHINode::Create(Ty), Name);
712 CallInst *CreateCall(Value *Callee, const char *Name = "") {
713 return Insert(CallInst::Create(Callee), Name);
715 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
716 return Insert(CallInst::Create(Callee, Arg), Name);
718 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
719 const char *Name = "") {
720 Value *Args[] = { Arg1, Arg2 };
721 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
723 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
724 const char *Name = "") {
725 Value *Args[] = { Arg1, Arg2, Arg3 };
726 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
728 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
729 Value *Arg4, const char *Name = "") {
730 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
731 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
734 template<typename InputIterator>
735 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
736 InputIterator ArgEnd, const char *Name = "") {
737 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
740 Value *CreateSelect(Value *C, Value *True, Value *False,
741 const char *Name = "") {
742 if (Constant *CC = dyn_cast<Constant>(C))
743 if (Constant *TC = dyn_cast<Constant>(True))
744 if (Constant *FC = dyn_cast<Constant>(False))
745 return Folder.CreateSelect(CC, TC, FC);
746 return Insert(SelectInst::Create(C, True, False), Name);
749 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
750 return Insert(new VAArgInst(List, Ty), Name);
753 Value *CreateExtractElement(Value *Vec, Value *Idx,
754 const char *Name = "") {
755 if (Constant *VC = dyn_cast<Constant>(Vec))
756 if (Constant *IC = dyn_cast<Constant>(Idx))
757 return Folder.CreateExtractElement(VC, IC);
758 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
761 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
762 const char *Name = "") {
763 if (Constant *VC = dyn_cast<Constant>(Vec))
764 if (Constant *NC = dyn_cast<Constant>(NewElt))
765 if (Constant *IC = dyn_cast<Constant>(Idx))
766 return Folder.CreateInsertElement(VC, NC, IC);
767 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
770 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
771 const char *Name = "") {
772 if (Constant *V1C = dyn_cast<Constant>(V1))
773 if (Constant *V2C = dyn_cast<Constant>(V2))
774 if (Constant *MC = dyn_cast<Constant>(Mask))
775 return Folder.CreateShuffleVector(V1C, V2C, MC);
776 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
779 Value *CreateExtractValue(Value *Agg, unsigned Idx,
780 const char *Name = "") {
781 if (Constant *AggC = dyn_cast<Constant>(Agg))
782 return Folder.CreateExtractValue(AggC, &Idx, 1);
783 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
786 template<typename InputIterator>
787 Value *CreateExtractValue(Value *Agg,
788 InputIterator IdxBegin,
789 InputIterator IdxEnd,
790 const char *Name = "") {
791 if (Constant *AggC = dyn_cast<Constant>(Agg))
792 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
793 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
796 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
797 const char *Name = "") {
798 if (Constant *AggC = dyn_cast<Constant>(Agg))
799 if (Constant *ValC = dyn_cast<Constant>(Val))
800 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
801 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
804 template<typename InputIterator>
805 Value *CreateInsertValue(Value *Agg, Value *Val,
806 InputIterator IdxBegin,
807 InputIterator IdxEnd,
808 const char *Name = "") {
809 if (Constant *AggC = dyn_cast<Constant>(Agg))
810 if (Constant *ValC = dyn_cast<Constant>(Val))
811 return Folder.CreateInsertValue(AggC, ValC,
812 IdxBegin, IdxEnd - IdxBegin);
813 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
816 //===--------------------------------------------------------------------===//
817 // Utility creation methods
818 //===--------------------------------------------------------------------===//
820 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
821 Value *CreateIsNull(Value *Arg, const char *Name = "") {
822 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
826 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
827 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
828 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
832 /// CreatePtrDiff - Return the i64 difference between two pointer values,
833 /// dividing out the size of the pointed-to objects. This is intended to
834 /// implement C-style pointer subtraction. As such, the pointers must be
835 /// appropriately aligned for their element types and pointing into the
837 Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
838 assert(LHS->getType() == RHS->getType() &&
839 "Pointer subtraction operand types must match!");
840 const PointerType *ArgType = cast<PointerType>(LHS->getType());
841 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
842 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
843 Value *Difference = CreateSub(LHS_int, RHS_int);
844 return CreateExactSDiv(Difference,
845 ConstantExpr::getSizeOf(ArgType->getElementType()),