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 return Insert(ReturnInst::Create(retVals, N));
108 GetResultInst *CreateGetResult(Value *V, unsigned Index,
109 const char *Name = "") {
110 return Insert(new GetResultInst(V, Index), Name);
113 /// CreateBr - Create an unconditional 'br label X' instruction.
114 BranchInst *CreateBr(BasicBlock *Dest) {
115 return Insert(BranchInst::Create(Dest));
118 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
120 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
121 return Insert(BranchInst::Create(True, False, Cond));
124 /// CreateSwitch - Create a switch instruction with the specified value,
125 /// default dest, and with a hint for the number of cases that will be added
126 /// (for efficient allocation).
127 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
128 return Insert(SwitchInst::Create(V, Dest, NumCases));
131 /// CreateInvoke - Create an invoke instruction.
132 template<typename InputIterator>
133 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
134 BasicBlock *UnwindDest, InputIterator ArgBegin,
135 InputIterator ArgEnd, const char *Name = "") {
136 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
137 ArgBegin, ArgEnd), Name);
140 UnwindInst *CreateUnwind() {
141 return Insert(new UnwindInst());
144 UnreachableInst *CreateUnreachable() {
145 return Insert(new UnreachableInst());
148 //===--------------------------------------------------------------------===//
149 // Instruction creation methods: Binary Operators
150 //===--------------------------------------------------------------------===//
152 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
153 if (Constant *LC = dyn_cast<Constant>(LHS))
154 if (Constant *RC = dyn_cast<Constant>(RHS))
155 return ConstantExpr::getAdd(LC, RC);
156 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
158 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
159 if (Constant *LC = dyn_cast<Constant>(LHS))
160 if (Constant *RC = dyn_cast<Constant>(RHS))
161 return ConstantExpr::getSub(LC, RC);
162 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
164 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
165 if (Constant *LC = dyn_cast<Constant>(LHS))
166 if (Constant *RC = dyn_cast<Constant>(RHS))
167 return ConstantExpr::getMul(LC, RC);
168 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
170 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
171 if (Constant *LC = dyn_cast<Constant>(LHS))
172 if (Constant *RC = dyn_cast<Constant>(RHS))
173 return ConstantExpr::getUDiv(LC, RC);
174 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
176 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
177 if (Constant *LC = dyn_cast<Constant>(LHS))
178 if (Constant *RC = dyn_cast<Constant>(RHS))
179 return ConstantExpr::getSDiv(LC, RC);
180 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
182 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
183 if (Constant *LC = dyn_cast<Constant>(LHS))
184 if (Constant *RC = dyn_cast<Constant>(RHS))
185 return ConstantExpr::getFDiv(LC, RC);
186 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
188 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
189 if (Constant *LC = dyn_cast<Constant>(LHS))
190 if (Constant *RC = dyn_cast<Constant>(RHS))
191 return ConstantExpr::getURem(LC, RC);
192 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
194 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
195 if (Constant *LC = dyn_cast<Constant>(LHS))
196 if (Constant *RC = dyn_cast<Constant>(RHS))
197 return ConstantExpr::getSRem(LC, RC);
198 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
200 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
201 if (Constant *LC = dyn_cast<Constant>(LHS))
202 if (Constant *RC = dyn_cast<Constant>(RHS))
203 return ConstantExpr::getFRem(LC, RC);
204 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
206 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
207 if (Constant *LC = dyn_cast<Constant>(LHS))
208 if (Constant *RC = dyn_cast<Constant>(RHS))
209 return ConstantExpr::getShl(LC, RC);
210 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
212 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
213 if (Constant *LC = dyn_cast<Constant>(LHS))
214 if (Constant *RC = dyn_cast<Constant>(RHS))
215 return ConstantExpr::getLShr(LC, RC);
216 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
218 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
219 if (Constant *LC = dyn_cast<Constant>(LHS))
220 if (Constant *RC = dyn_cast<Constant>(RHS))
221 return ConstantExpr::getAShr(LC, RC);
222 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
224 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
225 if (Constant *LC = dyn_cast<Constant>(LHS))
226 if (Constant *RC = dyn_cast<Constant>(RHS))
227 return ConstantExpr::getAnd(LC, RC);
228 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
230 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
231 if (Constant *LC = dyn_cast<Constant>(LHS))
232 if (Constant *RC = dyn_cast<Constant>(RHS))
233 return ConstantExpr::getOr(LC, RC);
234 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
236 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
237 if (Constant *LC = dyn_cast<Constant>(LHS))
238 if (Constant *RC = dyn_cast<Constant>(RHS))
239 return ConstantExpr::getXor(LC, RC);
240 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
243 BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
244 Value *LHS, Value *RHS, const char *Name = "") {
245 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
248 BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
249 return Insert(BinaryOperator::CreateNeg(V), Name);
251 BinaryOperator *CreateNot(Value *V, const char *Name = "") {
252 return Insert(BinaryOperator::CreateNot(V), Name);
255 //===--------------------------------------------------------------------===//
256 // Instruction creation methods: Memory Instructions
257 //===--------------------------------------------------------------------===//
259 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
260 const char *Name = "") {
261 return Insert(new MallocInst(Ty, ArraySize), Name);
263 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
264 const char *Name = "") {
265 return Insert(new AllocaInst(Ty, ArraySize), Name);
267 FreeInst *CreateFree(Value *Ptr) {
268 return Insert(new FreeInst(Ptr));
270 LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
271 return Insert(new LoadInst(Ptr, Name));
273 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
274 return Insert(new LoadInst(Ptr, Name, isVolatile));
276 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
277 return Insert(new StoreInst(Val, Ptr, isVolatile));
279 template<typename InputIterator>
280 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin,
281 InputIterator IdxEnd, const char *Name = "") {
283 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
284 // Every index must be constant.
286 for (i = IdxBegin; i < IdxEnd; ++i) {
287 if (!dyn_cast<Constant>(*i))
291 return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0],
294 return(Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name));
296 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
297 if (Constant *PC = dyn_cast<Constant>(Ptr))
298 if (Constant *IC = dyn_cast<Constant>(Idx))
299 return ConstantExpr::getGetElementPtr(PC, &IC, 1);
300 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
302 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
303 llvm::Value *Idxs[] = {
304 ConstantInt::get(llvm::Type::Int32Ty, 0),
305 ConstantInt::get(llvm::Type::Int32Ty, Idx)
308 if (Constant *PC = dyn_cast<Constant>(Ptr))
309 return ConstantExpr::getGetElementPtr(PC, Idxs, 2);
311 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
313 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
314 Constant *StrConstant = ConstantArray::get(Str, true);
315 GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(),
317 GlobalValue::InternalLinkage,
320 BB->getParent()->getParent(),
325 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
326 Value *gv = CreateGlobalString(Str, Name);
327 Value *zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
328 Value *Args[] = { zero, zero };
329 return CreateGEP(gv, Args, Args+2, Name);
331 //===--------------------------------------------------------------------===//
332 // Instruction creation methods: Cast/Conversion Operators
333 //===--------------------------------------------------------------------===//
335 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
336 return CreateCast(Instruction::Trunc, V, DestTy, Name);
338 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
339 return CreateCast(Instruction::ZExt, V, DestTy, Name);
341 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
342 return CreateCast(Instruction::SExt, V, DestTy, Name);
344 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
345 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
347 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
348 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
350 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
351 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
353 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
354 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
356 Value *CreateFPTrunc(Value *V, const Type *DestTy,
357 const char *Name = "") {
358 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
360 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
361 return CreateCast(Instruction::FPExt, V, DestTy, Name);
363 Value *CreatePtrToInt(Value *V, const Type *DestTy,
364 const char *Name = "") {
365 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
367 Value *CreateIntToPtr(Value *V, const Type *DestTy,
368 const char *Name = "") {
369 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
371 Value *CreateBitCast(Value *V, const Type *DestTy,
372 const char *Name = "") {
373 return CreateCast(Instruction::BitCast, V, DestTy, Name);
376 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
377 const char *Name = "") {
378 if (V->getType() == DestTy)
380 if (Constant *VC = dyn_cast<Constant>(V))
381 return ConstantExpr::getCast(Op, VC, DestTy);
382 return Insert(CastInst::Create(Op, V, DestTy), Name);
384 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
385 const char *Name = "") {
386 if (V->getType() == DestTy)
388 if (Constant *VC = dyn_cast<Constant>(V))
389 return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
390 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
393 //===--------------------------------------------------------------------===//
394 // Instruction creation methods: Compare Instructions
395 //===--------------------------------------------------------------------===//
397 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
398 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
400 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
401 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
403 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
404 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
406 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
407 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
409 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
410 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
412 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
413 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
415 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
416 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
418 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
419 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
421 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
422 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
424 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
425 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
428 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
429 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
431 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
432 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
434 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
435 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
437 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
438 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
440 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
441 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
443 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
444 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
446 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
447 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
449 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
450 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
452 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
453 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
455 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
456 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
458 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
459 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
461 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
462 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
464 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
465 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
467 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
468 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
471 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
472 const char *Name = "") {
473 if (Constant *LC = dyn_cast<Constant>(LHS))
474 if (Constant *RC = dyn_cast<Constant>(RHS))
475 return ConstantExpr::getCompare(P, LC, RC);
476 return Insert(new ICmpInst(P, LHS, RHS), Name);
478 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
479 const char *Name = "") {
480 if (Constant *LC = dyn_cast<Constant>(LHS))
481 if (Constant *RC = dyn_cast<Constant>(RHS))
482 return ConstantExpr::getCompare(P, LC, RC);
483 return Insert(new FCmpInst(P, LHS, RHS), Name);
486 Value *CreateVICmp(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 VICmpInst(P, LHS, RHS), Name);
493 Value *CreateVFCmp(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 VFCmpInst(P, LHS, RHS), Name);
501 //===--------------------------------------------------------------------===//
502 // Instruction creation methods: Other Instructions
503 //===--------------------------------------------------------------------===//
505 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
506 return Insert(PHINode::Create(Ty), Name);
509 CallInst *CreateCall(Value *Callee, const char *Name = "") {
510 return Insert(CallInst::Create(Callee), Name);
512 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
513 return Insert(CallInst::Create(Callee, Arg), Name);
515 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
516 const char *Name = "") {
517 Value *Args[] = { Arg1, Arg2 };
518 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
520 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
521 const char *Name = "") {
522 Value *Args[] = { Arg1, Arg2, Arg3 };
523 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
525 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
526 Value *Arg4, const char *Name = "") {
527 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
528 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
531 template<typename InputIterator>
532 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
533 InputIterator ArgEnd, const char *Name = "") {
534 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
537 Value *CreateSelect(Value *C, Value *True, Value *False,
538 const char *Name = "") {
539 if (Constant *CC = dyn_cast<Constant>(C))
540 if (Constant *TC = dyn_cast<Constant>(True))
541 if (Constant *FC = dyn_cast<Constant>(False))
542 return ConstantExpr::getSelect(CC, TC, FC);
543 return Insert(SelectInst::Create(C, True, False), Name);
546 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
547 return Insert(new VAArgInst(List, Ty), Name);
550 Value *CreateExtractElement(Value *Vec, Value *Idx,
551 const char *Name = "") {
552 if (Constant *VC = dyn_cast<Constant>(Vec))
553 if (Constant *IC = dyn_cast<Constant>(Idx))
554 return ConstantExpr::getExtractElement(VC, IC);
555 return Insert(new ExtractElementInst(Vec, Idx), Name);
558 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
559 const char *Name = "") {
560 if (Constant *VC = dyn_cast<Constant>(Vec))
561 if (Constant *NC = dyn_cast<Constant>(NewElt))
562 if (Constant *IC = dyn_cast<Constant>(Idx))
563 return ConstantExpr::getInsertElement(VC, NC, IC);
564 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
567 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
568 const char *Name = "") {
569 if (Constant *V1C = dyn_cast<Constant>(V1))
570 if (Constant *V2C = dyn_cast<Constant>(V2))
571 if (Constant *MC = dyn_cast<Constant>(Mask))
572 return ConstantExpr::getShuffleVector(V1C, V2C, MC);
573 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);