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 Value *CreateBinOp(Instruction::BinaryOps Opc,
247 Value *LHS, Value *RHS, const char *Name = "") {
248 if (Constant *LC = dyn_cast<Constant>(LHS))
249 if (Constant *RC = dyn_cast<Constant>(RHS))
250 return ConstantExpr::get(Opc, LC, RC);
251 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
254 Value *CreateNeg(Value *V, const char *Name = "") {
255 if (Constant *VC = dyn_cast<Constant>(V))
256 return ConstantExpr::getNeg(VC);
257 return Insert(BinaryOperator::CreateNeg(V), Name);
259 Value *CreateNot(Value *V, const char *Name = "") {
260 if (Constant *VC = dyn_cast<Constant>(V))
261 return ConstantExpr::getNot(VC);
262 return Insert(BinaryOperator::CreateNot(V), Name);
265 //===--------------------------------------------------------------------===//
266 // Instruction creation methods: Memory Instructions
267 //===--------------------------------------------------------------------===//
269 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
270 const char *Name = "") {
271 return Insert(new MallocInst(Ty, ArraySize), Name);
273 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
274 const char *Name = "") {
275 return Insert(new AllocaInst(Ty, ArraySize), Name);
277 FreeInst *CreateFree(Value *Ptr) {
278 return Insert(new FreeInst(Ptr));
280 LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
281 return Insert(new LoadInst(Ptr, Name));
283 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
284 return Insert(new LoadInst(Ptr, Name, isVolatile));
286 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
287 return Insert(new StoreInst(Val, Ptr, isVolatile));
289 template<typename InputIterator>
290 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin,
291 InputIterator IdxEnd, const char *Name = "") {
293 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
294 // Every index must be constant.
296 for (i = IdxBegin; i < IdxEnd; ++i) {
297 if (!dyn_cast<Constant>(*i))
301 return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0],
304 return(Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name));
306 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
307 if (Constant *PC = dyn_cast<Constant>(Ptr))
308 if (Constant *IC = dyn_cast<Constant>(Idx))
309 return ConstantExpr::getGetElementPtr(PC, &IC, 1);
310 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
312 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
313 llvm::Value *Idxs[] = {
314 ConstantInt::get(llvm::Type::Int32Ty, 0),
315 ConstantInt::get(llvm::Type::Int32Ty, Idx)
318 if (Constant *PC = dyn_cast<Constant>(Ptr))
319 return ConstantExpr::getGetElementPtr(PC, Idxs, 2);
321 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
323 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
324 Constant *StrConstant = ConstantArray::get(Str, true);
325 GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(),
327 GlobalValue::InternalLinkage,
330 BB->getParent()->getParent(),
335 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
336 Value *gv = CreateGlobalString(Str, Name);
337 Value *zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
338 Value *Args[] = { zero, zero };
339 return CreateGEP(gv, Args, Args+2, Name);
341 //===--------------------------------------------------------------------===//
342 // Instruction creation methods: Cast/Conversion Operators
343 //===--------------------------------------------------------------------===//
345 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
346 return CreateCast(Instruction::Trunc, V, DestTy, Name);
348 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
349 return CreateCast(Instruction::ZExt, V, DestTy, Name);
351 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
352 return CreateCast(Instruction::SExt, V, DestTy, Name);
354 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
355 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
357 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
358 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
360 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
361 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
363 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
364 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
366 Value *CreateFPTrunc(Value *V, const Type *DestTy,
367 const char *Name = "") {
368 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
370 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
371 return CreateCast(Instruction::FPExt, V, DestTy, Name);
373 Value *CreatePtrToInt(Value *V, const Type *DestTy,
374 const char *Name = "") {
375 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
377 Value *CreateIntToPtr(Value *V, const Type *DestTy,
378 const char *Name = "") {
379 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
381 Value *CreateBitCast(Value *V, const Type *DestTy,
382 const char *Name = "") {
383 return CreateCast(Instruction::BitCast, V, DestTy, Name);
386 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
387 const char *Name = "") {
388 if (V->getType() == DestTy)
390 if (Constant *VC = dyn_cast<Constant>(V))
391 return ConstantExpr::getCast(Op, VC, DestTy);
392 return Insert(CastInst::Create(Op, V, DestTy), Name);
394 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
395 const char *Name = "") {
396 if (V->getType() == DestTy)
398 if (Constant *VC = dyn_cast<Constant>(V))
399 return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
400 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
403 //===--------------------------------------------------------------------===//
404 // Instruction creation methods: Compare Instructions
405 //===--------------------------------------------------------------------===//
407 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
408 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
410 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
411 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
413 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
414 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
416 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
417 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
419 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
420 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
422 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
423 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
425 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
426 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
428 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
429 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
431 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
432 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
434 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
435 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
438 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
439 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
441 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
442 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
444 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
445 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
447 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
448 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
450 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
451 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
453 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
454 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
456 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
457 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
459 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
460 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
462 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
463 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
465 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
466 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
468 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
469 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
471 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
472 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
474 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
475 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
477 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
478 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
481 Value *CreateICmp(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 ICmpInst(P, LHS, RHS), Name);
488 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
489 const char *Name = "") {
490 if (Constant *LC = dyn_cast<Constant>(LHS))
491 if (Constant *RC = dyn_cast<Constant>(RHS))
492 return ConstantExpr::getCompare(P, LC, RC);
493 return Insert(new FCmpInst(P, LHS, RHS), Name);
496 Value *CreateVICmp(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 VICmpInst(P, LHS, RHS), Name);
503 Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
504 const char *Name = "") {
505 if (Constant *LC = dyn_cast<Constant>(LHS))
506 if (Constant *RC = dyn_cast<Constant>(RHS))
507 return ConstantExpr::getCompare(P, LC, RC);
508 return Insert(new VFCmpInst(P, LHS, RHS), Name);
511 //===--------------------------------------------------------------------===//
512 // Instruction creation methods: Other Instructions
513 //===--------------------------------------------------------------------===//
515 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
516 return Insert(PHINode::Create(Ty), Name);
519 CallInst *CreateCall(Value *Callee, const char *Name = "") {
520 return Insert(CallInst::Create(Callee), Name);
522 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
523 return Insert(CallInst::Create(Callee, Arg), Name);
525 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
526 const char *Name = "") {
527 Value *Args[] = { Arg1, Arg2 };
528 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
530 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
531 const char *Name = "") {
532 Value *Args[] = { Arg1, Arg2, Arg3 };
533 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
535 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
536 Value *Arg4, const char *Name = "") {
537 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
538 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
541 template<typename InputIterator>
542 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
543 InputIterator ArgEnd, const char *Name = "") {
544 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
547 Value *CreateSelect(Value *C, Value *True, Value *False,
548 const char *Name = "") {
549 if (Constant *CC = dyn_cast<Constant>(C))
550 if (Constant *TC = dyn_cast<Constant>(True))
551 if (Constant *FC = dyn_cast<Constant>(False))
552 return ConstantExpr::getSelect(CC, TC, FC);
553 return Insert(SelectInst::Create(C, True, False), Name);
556 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
557 return Insert(new VAArgInst(List, Ty), Name);
560 Value *CreateExtractElement(Value *Vec, Value *Idx,
561 const char *Name = "") {
562 if (Constant *VC = dyn_cast<Constant>(Vec))
563 if (Constant *IC = dyn_cast<Constant>(Idx))
564 return ConstantExpr::getExtractElement(VC, IC);
565 return Insert(new ExtractElementInst(Vec, Idx), Name);
568 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
569 const char *Name = "") {
570 if (Constant *VC = dyn_cast<Constant>(Vec))
571 if (Constant *NC = dyn_cast<Constant>(NewElt))
572 if (Constant *IC = dyn_cast<Constant>(Idx))
573 return ConstantExpr::getInsertElement(VC, NC, IC);
574 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
577 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
578 const char *Name = "") {
579 if (Constant *V1C = dyn_cast<Constant>(V1))
580 if (Constant *V2C = dyn_cast<Constant>(V2))
581 if (Constant *MC = dyn_cast<Constant>(Mask))
582 return ConstantExpr::getShuffleVector(V1C, V2C, MC);
583 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
586 Value *CreateExtractValue(Value *Agg, unsigned Idx,
587 const char *Name = "") {
588 if (Constant *AggC = dyn_cast<Constant>(Agg))
589 return ConstantExpr::getExtractValue(AggC, &Idx, 1);
590 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
593 template<typename InputIterator>
594 Value *CreateExtractValue(Value *Agg,
595 InputIterator IdxBegin,
596 InputIterator IdxEnd,
597 const char *Name = "") {
598 if (Constant *AggC = dyn_cast<Constant>(Agg))
599 return ConstantExpr::getExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
600 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
603 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
604 const char *Name = "") {
605 if (Constant *AggC = dyn_cast<Constant>(Agg))
606 if (Constant *ValC = dyn_cast<Constant>(Val))
607 return ConstantExpr::getInsertValue(AggC, ValC, &Idx, 1);
608 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
611 template<typename InputIterator>
612 Value *CreateInsertValue(Value *Agg, Value *Val,
613 InputIterator IdxBegin,
614 InputIterator IdxEnd,
615 const char *Name = "") {
616 if (Constant *AggC = dyn_cast<Constant>(Agg))
617 if (Constant *ValC = dyn_cast<Constant>(Val))
618 return ConstantExpr::getInsertValue(AggC, ValC,
619 IdxBegin, IdxEnd - IdxBegin);
620 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);