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/BasicBlock.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Constants.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Function.h"
26 /// IRBuilder - This provides a uniform API for creating instructions and
27 /// inserting them into a basic block: either at the end of a BasicBlock, or
28 /// at a specific iterator location in a block.
30 /// Note that the builder does not expose the full generality of LLVM
31 /// instructions. For example, it cannot be used to create instructions with
32 /// arbitrary names (specifically, names with nul characters in them) - It only
33 /// supports nul-terminated C strings. For fully generic names, use
34 /// I->setName(). For access to extra instruction properties, use the mutators
35 /// (e.g. setVolatile) on the instructions after they have been created.
38 BasicBlock::iterator InsertPt;
40 IRBuilder() { ClearInsertionPoint(); }
41 explicit IRBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
42 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
43 SetInsertPoint(TheBB, IP);
46 //===--------------------------------------------------------------------===//
47 // Builder configuration methods
48 //===--------------------------------------------------------------------===//
50 /// ClearInsertionPoint - Clear the insertion point: created instructions will
51 /// not be inserted into a block.
52 void ClearInsertionPoint() {
56 BasicBlock *GetInsertBlock() const { return BB; }
58 /// SetInsertPoint - This specifies that created instructions should be
59 /// appended to the end of the specified block.
60 void SetInsertPoint(BasicBlock *TheBB) {
65 /// SetInsertPoint - This specifies that created instructions should be
66 /// inserted at the specified point.
67 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
72 /// Insert - Insert and return the specified instruction.
73 template<typename InstTy>
74 InstTy *Insert(InstTy *I, const char *Name = "") const {
75 InsertHelper(I, Name);
79 /// InsertHelper - Insert the specified instruction at the specified insertion
80 /// point. This is split out of Insert so that it isn't duplicated for every
81 /// template instantiation.
82 void InsertHelper(Instruction *I, const char *Name) const {
83 if (BB) BB->getInstList().insert(InsertPt, I);
88 //===--------------------------------------------------------------------===//
89 // Instruction creation methods: Terminators
90 //===--------------------------------------------------------------------===//
92 /// CreateRetVoid - Create a 'ret void' instruction.
93 ReturnInst *CreateRetVoid() {
94 return Insert(ReturnInst::Create());
98 /// CreateRet - Create a 'ret <val>' instruction.
100 ReturnInst *CreateRet(Value *V) {
101 return Insert(ReturnInst::Create(V));
104 ReturnInst *CreateRet(Value * const* retVals, unsigned N) {
105 const Type *RetType = BB->getParent()->getReturnType();
106 if (N == 0 && RetType == Type::VoidTy)
107 return CreateRetVoid();
108 if (N == 1 && retVals[0]->getType() == RetType)
109 return Insert(ReturnInst::Create(retVals[0]));
110 Value *V = UndefValue::get(RetType);
111 for (unsigned i = 0; i != N; ++i)
112 V = CreateInsertValue(V, retVals[i], i, "mrv");
113 return Insert(ReturnInst::Create(V));
116 /// CreateBr - Create an unconditional 'br label X' instruction.
117 BranchInst *CreateBr(BasicBlock *Dest) {
118 return Insert(BranchInst::Create(Dest));
121 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
123 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
124 return Insert(BranchInst::Create(True, False, Cond));
127 /// CreateSwitch - Create a switch instruction with the specified value,
128 /// default dest, and with a hint for the number of cases that will be added
129 /// (for efficient allocation).
130 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
131 return Insert(SwitchInst::Create(V, Dest, NumCases));
134 /// CreateInvoke - Create an invoke instruction.
135 template<typename InputIterator>
136 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
137 BasicBlock *UnwindDest, InputIterator ArgBegin,
138 InputIterator ArgEnd, const char *Name = "") {
139 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
140 ArgBegin, ArgEnd), Name);
143 UnwindInst *CreateUnwind() {
144 return Insert(new UnwindInst());
147 UnreachableInst *CreateUnreachable() {
148 return Insert(new UnreachableInst());
151 //===--------------------------------------------------------------------===//
152 // Instruction creation methods: Binary Operators
153 //===--------------------------------------------------------------------===//
155 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
156 if (Constant *LC = dyn_cast<Constant>(LHS))
157 if (Constant *RC = dyn_cast<Constant>(RHS))
158 return ConstantExpr::getAdd(LC, RC);
159 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
161 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
162 if (Constant *LC = dyn_cast<Constant>(LHS))
163 if (Constant *RC = dyn_cast<Constant>(RHS))
164 return ConstantExpr::getSub(LC, RC);
165 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
167 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
168 if (Constant *LC = dyn_cast<Constant>(LHS))
169 if (Constant *RC = dyn_cast<Constant>(RHS))
170 return ConstantExpr::getMul(LC, RC);
171 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
173 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
174 if (Constant *LC = dyn_cast<Constant>(LHS))
175 if (Constant *RC = dyn_cast<Constant>(RHS))
176 return ConstantExpr::getUDiv(LC, RC);
177 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
179 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
180 if (Constant *LC = dyn_cast<Constant>(LHS))
181 if (Constant *RC = dyn_cast<Constant>(RHS))
182 return ConstantExpr::getSDiv(LC, RC);
183 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
185 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
186 if (Constant *LC = dyn_cast<Constant>(LHS))
187 if (Constant *RC = dyn_cast<Constant>(RHS))
188 return ConstantExpr::getFDiv(LC, RC);
189 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
191 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
192 if (Constant *LC = dyn_cast<Constant>(LHS))
193 if (Constant *RC = dyn_cast<Constant>(RHS))
194 return ConstantExpr::getURem(LC, RC);
195 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
197 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
198 if (Constant *LC = dyn_cast<Constant>(LHS))
199 if (Constant *RC = dyn_cast<Constant>(RHS))
200 return ConstantExpr::getSRem(LC, RC);
201 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
203 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
204 if (Constant *LC = dyn_cast<Constant>(LHS))
205 if (Constant *RC = dyn_cast<Constant>(RHS))
206 return ConstantExpr::getFRem(LC, RC);
207 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
209 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
210 if (Constant *LC = dyn_cast<Constant>(LHS))
211 if (Constant *RC = dyn_cast<Constant>(RHS))
212 return ConstantExpr::getShl(LC, RC);
213 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
215 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
216 if (Constant *LC = dyn_cast<Constant>(LHS))
217 if (Constant *RC = dyn_cast<Constant>(RHS))
218 return ConstantExpr::getLShr(LC, RC);
219 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
221 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
222 if (Constant *LC = dyn_cast<Constant>(LHS))
223 if (Constant *RC = dyn_cast<Constant>(RHS))
224 return ConstantExpr::getAShr(LC, RC);
225 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
227 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
228 if (Constant *LC = dyn_cast<Constant>(LHS))
229 if (Constant *RC = dyn_cast<Constant>(RHS))
230 return ConstantExpr::getAnd(LC, RC);
231 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
233 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
234 if (Constant *LC = dyn_cast<Constant>(LHS))
235 if (Constant *RC = dyn_cast<Constant>(RHS))
236 return ConstantExpr::getOr(LC, RC);
237 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
239 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
240 if (Constant *LC = dyn_cast<Constant>(LHS))
241 if (Constant *RC = dyn_cast<Constant>(RHS))
242 return ConstantExpr::getXor(LC, RC);
243 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
246 BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
247 Value *LHS, Value *RHS, const char *Name = "") {
248 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
251 BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
252 return Insert(BinaryOperator::CreateNeg(V), Name);
254 BinaryOperator *CreateNot(Value *V, const char *Name = "") {
255 return Insert(BinaryOperator::CreateNot(V), Name);
258 //===--------------------------------------------------------------------===//
259 // Instruction creation methods: Memory Instructions
260 //===--------------------------------------------------------------------===//
262 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
263 const char *Name = "") {
264 return Insert(new MallocInst(Ty, ArraySize), Name);
266 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
267 const char *Name = "") {
268 return Insert(new AllocaInst(Ty, ArraySize), Name);
270 FreeInst *CreateFree(Value *Ptr) {
271 return Insert(new FreeInst(Ptr));
273 LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
274 return Insert(new LoadInst(Ptr, Name));
276 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
277 return Insert(new LoadInst(Ptr, Name, isVolatile));
279 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
280 return Insert(new StoreInst(Val, Ptr, isVolatile));
282 template<typename InputIterator>
283 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin,
284 InputIterator IdxEnd, const char *Name = "") {
286 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
287 // Every index must be constant.
289 for (i = IdxBegin; i < IdxEnd; ++i) {
290 if (!dyn_cast<Constant>(*i))
294 return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0],
297 return(Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name));
299 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
300 if (Constant *PC = dyn_cast<Constant>(Ptr))
301 if (Constant *IC = dyn_cast<Constant>(Idx))
302 return ConstantExpr::getGetElementPtr(PC, &IC, 1);
303 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
305 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
306 llvm::Value *Idxs[] = {
307 ConstantInt::get(llvm::Type::Int32Ty, 0),
308 ConstantInt::get(llvm::Type::Int32Ty, Idx)
311 if (Constant *PC = dyn_cast<Constant>(Ptr))
312 return ConstantExpr::getGetElementPtr(PC, Idxs, 2);
314 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
316 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
317 Constant *StrConstant = ConstantArray::get(Str, true);
318 GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(),
320 GlobalValue::InternalLinkage,
323 BB->getParent()->getParent(),
328 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
329 Value *gv = CreateGlobalString(Str, Name);
330 Value *zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
331 Value *Args[] = { zero, zero };
332 return CreateGEP(gv, Args, Args+2, Name);
334 //===--------------------------------------------------------------------===//
335 // Instruction creation methods: Cast/Conversion Operators
336 //===--------------------------------------------------------------------===//
338 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
339 return CreateCast(Instruction::Trunc, V, DestTy, Name);
341 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
342 return CreateCast(Instruction::ZExt, V, DestTy, Name);
344 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
345 return CreateCast(Instruction::SExt, V, DestTy, Name);
347 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
348 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
350 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
351 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
353 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
354 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
356 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
357 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
359 Value *CreateFPTrunc(Value *V, const Type *DestTy,
360 const char *Name = "") {
361 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
363 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
364 return CreateCast(Instruction::FPExt, V, DestTy, Name);
366 Value *CreatePtrToInt(Value *V, const Type *DestTy,
367 const char *Name = "") {
368 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
370 Value *CreateIntToPtr(Value *V, const Type *DestTy,
371 const char *Name = "") {
372 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
374 Value *CreateBitCast(Value *V, const Type *DestTy,
375 const char *Name = "") {
376 return CreateCast(Instruction::BitCast, V, DestTy, Name);
379 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
380 const char *Name = "") {
381 if (V->getType() == DestTy)
383 if (Constant *VC = dyn_cast<Constant>(V))
384 return ConstantExpr::getCast(Op, VC, DestTy);
385 return Insert(CastInst::Create(Op, V, DestTy), Name);
387 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
388 const char *Name = "") {
389 if (V->getType() == DestTy)
391 if (Constant *VC = dyn_cast<Constant>(V))
392 return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
393 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
396 //===--------------------------------------------------------------------===//
397 // Instruction creation methods: Compare Instructions
398 //===--------------------------------------------------------------------===//
400 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
401 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
403 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
404 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
406 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
407 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
409 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
410 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
412 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
413 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
415 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
416 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
418 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
419 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
421 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
422 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
424 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
425 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
427 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
428 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
431 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
432 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
434 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
435 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
437 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
438 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
440 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
441 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
443 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
444 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
446 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
447 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
449 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
450 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
452 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
453 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
455 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
456 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
458 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
459 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
461 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
462 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
464 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
465 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
467 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
468 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
470 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
471 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
474 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
475 const char *Name = "") {
476 if (Constant *LC = dyn_cast<Constant>(LHS))
477 if (Constant *RC = dyn_cast<Constant>(RHS))
478 return ConstantExpr::getCompare(P, LC, RC);
479 return Insert(new ICmpInst(P, LHS, RHS), Name);
481 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
482 const char *Name = "") {
483 if (Constant *LC = dyn_cast<Constant>(LHS))
484 if (Constant *RC = dyn_cast<Constant>(RHS))
485 return ConstantExpr::getCompare(P, LC, RC);
486 return Insert(new FCmpInst(P, LHS, RHS), Name);
489 Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
490 const char *Name = "") {
491 if (Constant *LC = dyn_cast<Constant>(LHS))
492 if (Constant *RC = dyn_cast<Constant>(RHS))
493 return ConstantExpr::getCompare(P, LC, RC);
494 return Insert(new VICmpInst(P, LHS, RHS), Name);
496 Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
497 const char *Name = "") {
498 if (Constant *LC = dyn_cast<Constant>(LHS))
499 if (Constant *RC = dyn_cast<Constant>(RHS))
500 return ConstantExpr::getCompare(P, LC, RC);
501 return Insert(new VFCmpInst(P, LHS, RHS), Name);
504 //===--------------------------------------------------------------------===//
505 // Instruction creation methods: Other Instructions
506 //===--------------------------------------------------------------------===//
508 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
509 return Insert(PHINode::Create(Ty), Name);
512 CallInst *CreateCall(Value *Callee, const char *Name = "") {
513 return Insert(CallInst::Create(Callee), Name);
515 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
516 return Insert(CallInst::Create(Callee, Arg), Name);
518 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
519 const char *Name = "") {
520 Value *Args[] = { Arg1, Arg2 };
521 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
523 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
524 const char *Name = "") {
525 Value *Args[] = { Arg1, Arg2, Arg3 };
526 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
528 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
529 Value *Arg4, const char *Name = "") {
530 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
531 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
534 template<typename InputIterator>
535 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
536 InputIterator ArgEnd, const char *Name = "") {
537 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
540 Value *CreateSelect(Value *C, Value *True, Value *False,
541 const char *Name = "") {
542 if (Constant *CC = dyn_cast<Constant>(C))
543 if (Constant *TC = dyn_cast<Constant>(True))
544 if (Constant *FC = dyn_cast<Constant>(False))
545 return ConstantExpr::getSelect(CC, TC, FC);
546 return Insert(SelectInst::Create(C, True, False), Name);
549 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
550 return Insert(new VAArgInst(List, Ty), Name);
553 Value *CreateExtractElement(Value *Vec, Value *Idx,
554 const char *Name = "") {
555 if (Constant *VC = dyn_cast<Constant>(Vec))
556 if (Constant *IC = dyn_cast<Constant>(Idx))
557 return ConstantExpr::getExtractElement(VC, IC);
558 return Insert(new ExtractElementInst(Vec, Idx), Name);
561 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
562 const char *Name = "") {
563 if (Constant *VC = dyn_cast<Constant>(Vec))
564 if (Constant *NC = dyn_cast<Constant>(NewElt))
565 if (Constant *IC = dyn_cast<Constant>(Idx))
566 return ConstantExpr::getInsertElement(VC, NC, IC);
567 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
570 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
571 const char *Name = "") {
572 if (Constant *V1C = dyn_cast<Constant>(V1))
573 if (Constant *V2C = dyn_cast<Constant>(V2))
574 if (Constant *MC = dyn_cast<Constant>(Mask))
575 return ConstantExpr::getShuffleVector(V1C, V2C, MC);
576 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
579 Value *CreateExtractValue(Value *Agg, unsigned Idx,
580 const char *Name = "") {
581 if (Constant *AggC = dyn_cast<Constant>(Agg))
582 return ConstantExpr::getExtractValue(AggC, &Idx, 1);
583 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
586 template<typename InputIterator>
587 Value *CreateExtractValue(Value *Agg,
588 InputIterator IdxBegin,
589 InputIterator IdxEnd,
590 const char *Name = "") {
591 if (Constant *AggC = dyn_cast<Constant>(Agg))
592 return ConstantExpr::getExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
593 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
596 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
597 const char *Name = "") {
598 if (Constant *AggC = dyn_cast<Constant>(Agg))
599 if (Constant *ValC = dyn_cast<Constant>(Val))
600 return ConstantExpr::getInsertValue(AggC, ValC, &Idx, 1);
601 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
604 template<typename InputIterator>
605 Value *CreateInsertValue(Value *Agg, Value *Val,
606 InputIterator IdxBegin,
607 InputIterator IdxEnd,
608 const char *Name = "") {
609 if (Constant *AggC = dyn_cast<Constant>(Agg))
610 if (Constant *ValC = dyn_cast<Constant>(Val))
611 return ConstantExpr::getInsertValue(AggC, ValC,
612 IdxBegin, IdxEnd - IdxBegin);
613 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);