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.
36 /// The template argument handles whether or not to preserve names in the final
37 /// instruction output. This defaults to on.
38 template <bool preserveNames=true> class IRBuilder {
40 BasicBlock::iterator InsertPt;
42 IRBuilder() { ClearInsertionPoint(); }
43 explicit IRBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
44 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
45 SetInsertPoint(TheBB, IP);
48 //===--------------------------------------------------------------------===//
49 // Builder configuration methods
50 //===--------------------------------------------------------------------===//
52 /// ClearInsertionPoint - Clear the insertion point: created instructions will
53 /// not be inserted into a block.
54 void ClearInsertionPoint() {
58 BasicBlock *GetInsertBlock() const { return BB; }
60 /// SetInsertPoint - This specifies that created instructions should be
61 /// appended to the end of the specified block.
62 void SetInsertPoint(BasicBlock *TheBB) {
67 /// SetInsertPoint - This specifies that created instructions should be
68 /// inserted at the specified point.
69 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
74 /// Insert - Insert and return the specified instruction.
75 template<typename InstTy>
76 InstTy *Insert(InstTy *I, const char *Name = "") const {
77 InsertHelper(I, Name);
81 /// InsertHelper - Insert the specified instruction at the specified insertion
82 /// point. This is split out of Insert so that it isn't duplicated for every
83 /// template instantiation.
84 void InsertHelper(Instruction *I, const char *Name) const {
85 if (BB) BB->getInstList().insert(InsertPt, I);
86 if (preserveNames && Name[0])
90 //===--------------------------------------------------------------------===//
91 // Instruction creation methods: Terminators
92 //===--------------------------------------------------------------------===//
94 /// CreateRetVoid - Create a 'ret void' instruction.
95 ReturnInst *CreateRetVoid() {
96 return Insert(ReturnInst::Create());
100 /// CreateRet - Create a 'ret <val>' instruction.
102 ReturnInst *CreateRet(Value *V) {
103 return Insert(ReturnInst::Create(V));
106 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
107 /// with one Value from the retVals array each, that build a aggregate
108 /// return value one value at a time, and a ret instruction to return
109 /// the resulting aggregate value. This is a convenience function for
110 /// code that uses aggregate return values as a vehicle for having
111 /// multiple return values.
113 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
114 const Type *RetType = BB->getParent()->getReturnType();
115 Value *V = UndefValue::get(RetType);
116 for (unsigned i = 0; i != N; ++i)
117 V = CreateInsertValue(V, retVals[i], i, "mrv");
118 return Insert(ReturnInst::Create(V));
121 /// CreateBr - Create an unconditional 'br label X' instruction.
122 BranchInst *CreateBr(BasicBlock *Dest) {
123 return Insert(BranchInst::Create(Dest));
126 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
128 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
129 return Insert(BranchInst::Create(True, False, Cond));
132 /// CreateSwitch - Create a switch instruction with the specified value,
133 /// default dest, and with a hint for the number of cases that will be added
134 /// (for efficient allocation).
135 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
136 return Insert(SwitchInst::Create(V, Dest, NumCases));
139 /// CreateInvoke - Create an invoke instruction.
140 template<typename InputIterator>
141 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
142 BasicBlock *UnwindDest, InputIterator ArgBegin,
143 InputIterator ArgEnd, const char *Name = "") {
144 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
145 ArgBegin, ArgEnd), Name);
148 UnwindInst *CreateUnwind() {
149 return Insert(new UnwindInst());
152 UnreachableInst *CreateUnreachable() {
153 return Insert(new UnreachableInst());
156 //===--------------------------------------------------------------------===//
157 // Instruction creation methods: Binary Operators
158 //===--------------------------------------------------------------------===//
160 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
161 if (Constant *LC = dyn_cast<Constant>(LHS))
162 if (Constant *RC = dyn_cast<Constant>(RHS))
163 return ConstantExpr::getAdd(LC, RC);
164 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
166 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
167 if (Constant *LC = dyn_cast<Constant>(LHS))
168 if (Constant *RC = dyn_cast<Constant>(RHS))
169 return ConstantExpr::getSub(LC, RC);
170 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
172 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
173 if (Constant *LC = dyn_cast<Constant>(LHS))
174 if (Constant *RC = dyn_cast<Constant>(RHS))
175 return ConstantExpr::getMul(LC, RC);
176 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
178 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
179 if (Constant *LC = dyn_cast<Constant>(LHS))
180 if (Constant *RC = dyn_cast<Constant>(RHS))
181 return ConstantExpr::getUDiv(LC, RC);
182 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
184 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
185 if (Constant *LC = dyn_cast<Constant>(LHS))
186 if (Constant *RC = dyn_cast<Constant>(RHS))
187 return ConstantExpr::getSDiv(LC, RC);
188 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
190 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
191 if (Constant *LC = dyn_cast<Constant>(LHS))
192 if (Constant *RC = dyn_cast<Constant>(RHS))
193 return ConstantExpr::getFDiv(LC, RC);
194 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
196 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
197 if (Constant *LC = dyn_cast<Constant>(LHS))
198 if (Constant *RC = dyn_cast<Constant>(RHS))
199 return ConstantExpr::getURem(LC, RC);
200 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
202 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
203 if (Constant *LC = dyn_cast<Constant>(LHS))
204 if (Constant *RC = dyn_cast<Constant>(RHS))
205 return ConstantExpr::getSRem(LC, RC);
206 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
208 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
209 if (Constant *LC = dyn_cast<Constant>(LHS))
210 if (Constant *RC = dyn_cast<Constant>(RHS))
211 return ConstantExpr::getFRem(LC, RC);
212 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
214 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
215 if (Constant *LC = dyn_cast<Constant>(LHS))
216 if (Constant *RC = dyn_cast<Constant>(RHS))
217 return ConstantExpr::getShl(LC, RC);
218 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
220 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
221 if (Constant *LC = dyn_cast<Constant>(LHS))
222 if (Constant *RC = dyn_cast<Constant>(RHS))
223 return ConstantExpr::getLShr(LC, RC);
224 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
226 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
227 if (Constant *LC = dyn_cast<Constant>(LHS))
228 if (Constant *RC = dyn_cast<Constant>(RHS))
229 return ConstantExpr::getAShr(LC, RC);
230 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
232 Value *CreateAnd(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 ConstantExpr::getAnd(LC, RC);
236 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
238 Value *CreateOr(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 ConstantExpr::getOr(LC, RC);
242 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
244 Value *CreateXor(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 ConstantExpr::getXor(LC, RC);
248 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
251 Value *CreateBinOp(Instruction::BinaryOps Opc,
252 Value *LHS, Value *RHS, const char *Name = "") {
253 if (Constant *LC = dyn_cast<Constant>(LHS))
254 if (Constant *RC = dyn_cast<Constant>(RHS))
255 return ConstantExpr::get(Opc, LC, RC);
256 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
259 Value *CreateNeg(Value *V, const char *Name = "") {
260 if (Constant *VC = dyn_cast<Constant>(V))
261 return ConstantExpr::getNeg(VC);
262 return Insert(BinaryOperator::CreateNeg(V), Name);
264 Value *CreateNot(Value *V, const char *Name = "") {
265 if (Constant *VC = dyn_cast<Constant>(V))
266 return ConstantExpr::getNot(VC);
267 return Insert(BinaryOperator::CreateNot(V), Name);
270 //===--------------------------------------------------------------------===//
271 // Instruction creation methods: Memory Instructions
272 //===--------------------------------------------------------------------===//
274 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
275 const char *Name = "") {
276 return Insert(new MallocInst(Ty, ArraySize), Name);
278 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
279 const char *Name = "") {
280 return Insert(new AllocaInst(Ty, ArraySize), Name);
282 FreeInst *CreateFree(Value *Ptr) {
283 return Insert(new FreeInst(Ptr));
285 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
286 return Insert(new LoadInst(Ptr), Name);
288 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
289 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
291 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
292 return Insert(new StoreInst(Val, Ptr, isVolatile));
294 template<typename InputIterator>
295 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin,
296 InputIterator IdxEnd, const char *Name = "") {
298 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
299 // Every index must be constant.
301 for (i = IdxBegin; i < IdxEnd; ++i) {
302 if (!dyn_cast<Constant>(*i))
306 return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0],
309 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
311 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
312 if (Constant *PC = dyn_cast<Constant>(Ptr))
313 if (Constant *IC = dyn_cast<Constant>(Idx))
314 return ConstantExpr::getGetElementPtr(PC, &IC, 1);
315 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
317 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
318 llvm::Value *Idxs[] = {
319 ConstantInt::get(llvm::Type::Int32Ty, 0),
320 ConstantInt::get(llvm::Type::Int32Ty, Idx)
323 if (Constant *PC = dyn_cast<Constant>(Ptr))
324 return ConstantExpr::getGetElementPtr(PC, Idxs, 2);
326 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
328 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
329 Constant *StrConstant = ConstantArray::get(Str, true);
330 GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(),
332 GlobalValue::InternalLinkage,
335 BB->getParent()->getParent(),
340 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
341 Value *gv = CreateGlobalString(Str, Name);
342 Value *zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
343 Value *Args[] = { zero, zero };
344 return CreateGEP(gv, Args, Args+2, Name);
346 //===--------------------------------------------------------------------===//
347 // Instruction creation methods: Cast/Conversion Operators
348 //===--------------------------------------------------------------------===//
350 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
351 return CreateCast(Instruction::Trunc, V, DestTy, Name);
353 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
354 return CreateCast(Instruction::ZExt, V, DestTy, Name);
356 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
357 return CreateCast(Instruction::SExt, V, DestTy, Name);
359 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
360 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
362 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
363 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
365 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
366 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
368 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
369 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
371 Value *CreateFPTrunc(Value *V, const Type *DestTy,
372 const char *Name = "") {
373 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
375 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
376 return CreateCast(Instruction::FPExt, V, DestTy, Name);
378 Value *CreatePtrToInt(Value *V, const Type *DestTy,
379 const char *Name = "") {
380 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
382 Value *CreateIntToPtr(Value *V, const Type *DestTy,
383 const char *Name = "") {
384 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
386 Value *CreateBitCast(Value *V, const Type *DestTy,
387 const char *Name = "") {
388 return CreateCast(Instruction::BitCast, V, DestTy, Name);
391 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
392 const char *Name = "") {
393 if (V->getType() == DestTy)
395 if (Constant *VC = dyn_cast<Constant>(V))
396 return ConstantExpr::getCast(Op, VC, DestTy);
397 return Insert(CastInst::Create(Op, V, DestTy), Name);
399 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
400 const char *Name = "") {
401 if (V->getType() == DestTy)
403 if (Constant *VC = dyn_cast<Constant>(V))
404 return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
405 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
408 //===--------------------------------------------------------------------===//
409 // Instruction creation methods: Compare Instructions
410 //===--------------------------------------------------------------------===//
412 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
413 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
415 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
416 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
418 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
419 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
421 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
422 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
424 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
425 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
427 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
428 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
430 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
431 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
433 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
434 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
436 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
437 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
439 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
440 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
443 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
444 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
446 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
447 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
449 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
450 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
452 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
453 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
455 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
456 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
458 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
459 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
461 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
462 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
464 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
465 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
467 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
468 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
470 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
471 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
473 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
474 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
476 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
477 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
479 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
480 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
482 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
483 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
486 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
487 const char *Name = "") {
488 if (Constant *LC = dyn_cast<Constant>(LHS))
489 if (Constant *RC = dyn_cast<Constant>(RHS))
490 return ConstantExpr::getCompare(P, LC, RC);
491 return Insert(new ICmpInst(P, LHS, RHS), Name);
493 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
494 const char *Name = "") {
495 if (Constant *LC = dyn_cast<Constant>(LHS))
496 if (Constant *RC = dyn_cast<Constant>(RHS))
497 return ConstantExpr::getCompare(P, LC, RC);
498 return Insert(new FCmpInst(P, LHS, RHS), Name);
501 Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
502 const char *Name = "") {
503 if (Constant *LC = dyn_cast<Constant>(LHS))
504 if (Constant *RC = dyn_cast<Constant>(RHS))
505 return ConstantExpr::getCompare(P, LC, RC);
506 return Insert(new VICmpInst(P, LHS, RHS), Name);
508 Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
509 const char *Name = "") {
510 if (Constant *LC = dyn_cast<Constant>(LHS))
511 if (Constant *RC = dyn_cast<Constant>(RHS))
512 return ConstantExpr::getCompare(P, LC, RC);
513 return Insert(new VFCmpInst(P, LHS, RHS), Name);
516 //===--------------------------------------------------------------------===//
517 // Instruction creation methods: Other Instructions
518 //===--------------------------------------------------------------------===//
520 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
521 return Insert(PHINode::Create(Ty), Name);
524 CallInst *CreateCall(Value *Callee, const char *Name = "") {
525 return Insert(CallInst::Create(Callee), Name);
527 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
528 return Insert(CallInst::Create(Callee, Arg), Name);
530 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
531 const char *Name = "") {
532 Value *Args[] = { Arg1, Arg2 };
533 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
535 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
536 const char *Name = "") {
537 Value *Args[] = { Arg1, Arg2, Arg3 };
538 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
540 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
541 Value *Arg4, const char *Name = "") {
542 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
543 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
546 template<typename InputIterator>
547 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
548 InputIterator ArgEnd, const char *Name = "") {
549 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
552 Value *CreateSelect(Value *C, Value *True, Value *False,
553 const char *Name = "") {
554 if (Constant *CC = dyn_cast<Constant>(C))
555 if (Constant *TC = dyn_cast<Constant>(True))
556 if (Constant *FC = dyn_cast<Constant>(False))
557 return ConstantExpr::getSelect(CC, TC, FC);
558 return Insert(SelectInst::Create(C, True, False), Name);
561 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
562 return Insert(new VAArgInst(List, Ty), Name);
565 Value *CreateExtractElement(Value *Vec, Value *Idx,
566 const char *Name = "") {
567 if (Constant *VC = dyn_cast<Constant>(Vec))
568 if (Constant *IC = dyn_cast<Constant>(Idx))
569 return ConstantExpr::getExtractElement(VC, IC);
570 return Insert(new ExtractElementInst(Vec, Idx), Name);
573 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
574 const char *Name = "") {
575 if (Constant *VC = dyn_cast<Constant>(Vec))
576 if (Constant *NC = dyn_cast<Constant>(NewElt))
577 if (Constant *IC = dyn_cast<Constant>(Idx))
578 return ConstantExpr::getInsertElement(VC, NC, IC);
579 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
582 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
583 const char *Name = "") {
584 if (Constant *V1C = dyn_cast<Constant>(V1))
585 if (Constant *V2C = dyn_cast<Constant>(V2))
586 if (Constant *MC = dyn_cast<Constant>(Mask))
587 return ConstantExpr::getShuffleVector(V1C, V2C, MC);
588 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
591 Value *CreateExtractValue(Value *Agg, unsigned Idx,
592 const char *Name = "") {
593 if (Constant *AggC = dyn_cast<Constant>(Agg))
594 return ConstantExpr::getExtractValue(AggC, &Idx, 1);
595 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
598 template<typename InputIterator>
599 Value *CreateExtractValue(Value *Agg,
600 InputIterator IdxBegin,
601 InputIterator IdxEnd,
602 const char *Name = "") {
603 if (Constant *AggC = dyn_cast<Constant>(Agg))
604 return ConstantExpr::getExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
605 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
608 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
609 const char *Name = "") {
610 if (Constant *AggC = dyn_cast<Constant>(Agg))
611 if (Constant *ValC = dyn_cast<Constant>(Val))
612 return ConstantExpr::getInsertValue(AggC, ValC, &Idx, 1);
613 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
616 template<typename InputIterator>
617 Value *CreateInsertValue(Value *Agg, Value *Val,
618 InputIterator IdxBegin,
619 InputIterator IdxEnd,
620 const char *Name = "") {
621 if (Constant *AggC = dyn_cast<Constant>(Agg))
622 if (Constant *ValC = dyn_cast<Constant>(Val))
623 return ConstantExpr::getInsertValue(AggC, ValC,
624 IdxBegin, IdxEnd - IdxBegin);
625 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);