e2610ea08cf7a192d7434498fc30ccba3447ff7c
[oota-llvm.git] / include / llvm / Support / IRBuilder.h
1 //===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_IRBUILDER_H
16 #define LLVM_SUPPORT_IRBUILDER_H
17
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Constants.h"
21
22 namespace llvm {
23
24 /// IRBuilder - This provides a uniform API for creating instructions and
25 /// inserting them into a basic block: either at the end of a BasicBlock, or 
26 /// at a specific iterator location in a block.
27 ///
28 /// Note that the builder does not expose the full generality of LLVM
29 /// instructions.  For example, it cannot be used to create instructions with
30 /// arbitrary names (specifically, names with nul characters in them) - It only
31 /// supports nul-terminated C strings.  For fully generic names, use
32 /// I->setName().  For access to extra instruction properties, use the mutators
33 /// (e.g. setVolatile) on the instructions after they have been created.
34 class IRBuilder {
35   BasicBlock *BB;
36   BasicBlock::iterator InsertPt;
37 public:
38   IRBuilder() { ClearInsertionPoint(); }
39   explicit IRBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
40   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
41     SetInsertPoint(TheBB, IP);
42   }
43
44   //===--------------------------------------------------------------------===//
45   // Builder configuration methods
46   //===--------------------------------------------------------------------===//
47
48   /// ClearInsertionPoint - Clear the insertion point: created instructions will
49   /// not be inserted into a block.
50   void ClearInsertionPoint() {
51     BB = 0;
52   }
53   
54   BasicBlock *GetInsertBlock() const { return BB; }
55   
56   /// SetInsertPoint - This specifies that created instructions should be
57   /// appended to the end of the specified block.
58   void SetInsertPoint(BasicBlock *TheBB) {
59     BB = TheBB;
60     InsertPt = BB->end();
61   }
62   
63   /// SetInsertPoint - This specifies that created instructions should be
64   /// inserted at the specified point.
65   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
66     BB = TheBB;
67     InsertPt = IP;
68   }
69   
70   /// Insert - Insert and return the specified instruction.
71   template<typename InstTy>
72   InstTy *Insert(InstTy *I) const {
73     InsertHelper(I);
74     return I;
75   }
76   
77   /// InsertHelper - Insert the specified instruction at the specified insertion
78   /// point.  This is split out of Insert so that it isn't duplicated for every
79   /// template instantiation.
80   void InsertHelper(Instruction *I) const {
81     if (BB) BB->getInstList().insert(InsertPt, I);
82   }
83   
84   //===--------------------------------------------------------------------===//
85   // Instruction creation methods: Terminators
86   //===--------------------------------------------------------------------===//
87
88   /// CreateRetVoid - Create a 'ret void' instruction.
89   ReturnInst *CreateRetVoid() {
90     return Insert(ReturnInst::Create());
91   }
92
93   /// @verbatim 
94   /// CreateRet - Create a 'ret <val>' instruction. 
95   /// @endverbatim
96   ReturnInst *CreateRet(Value *V) {
97     return Insert(ReturnInst::Create(V));
98   }
99
100   ReturnInst *CreateRet(Value * const* retVals, unsigned N) {
101     return Insert(ReturnInst::Create(retVals,  N));
102   }
103   
104   GetResultInst *CreateGetResult(Value *V, unsigned Index, 
105                                  const char *Name = "") {
106     return Insert(new GetResultInst(V, Index, Name));
107   }
108     
109   /// CreateBr - Create an unconditional 'br label X' instruction.
110   BranchInst *CreateBr(BasicBlock *Dest) {
111     return Insert(BranchInst::Create(Dest));
112   }
113
114   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
115   /// instruction.
116   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
117     return Insert(BranchInst::Create(True, False, Cond));
118   }
119   
120   /// CreateSwitch - Create a switch instruction with the specified value,
121   /// default dest, and with a hint for the number of cases that will be added
122   /// (for efficient allocation).
123   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
124     return Insert(SwitchInst::Create(V, Dest, NumCases));
125   }
126   
127   /// CreateInvoke - Create an invoke instruction.
128   template<typename InputIterator>
129   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 
130                            BasicBlock *UnwindDest, InputIterator ArgBegin, 
131                            InputIterator ArgEnd, const char *Name = "") {
132     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
133                                      ArgBegin, ArgEnd, Name));
134   }
135   
136   UnwindInst *CreateUnwind() {
137     return Insert(new UnwindInst());
138   }
139
140   UnreachableInst *CreateUnreachable() {
141     return Insert(new UnreachableInst());
142   }
143   
144   //===--------------------------------------------------------------------===//
145   // Instruction creation methods: Binary Operators
146   //===--------------------------------------------------------------------===//
147
148   Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
149     if (Constant *LC = dyn_cast<Constant>(LHS))
150       if (Constant *RC = dyn_cast<Constant>(RHS))
151         return ConstantExpr::getAdd(LC, RC);      
152     return Insert(BinaryOperator::createAdd(LHS, RHS, Name));
153   }
154   Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
155     if (Constant *LC = dyn_cast<Constant>(LHS))
156       if (Constant *RC = dyn_cast<Constant>(RHS))
157         return ConstantExpr::getSub(LC, RC);
158     return Insert(BinaryOperator::createSub(LHS, RHS, Name));
159   }
160   Value *CreateMul(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::getMul(LC, RC);
164     return Insert(BinaryOperator::createMul(LHS, RHS, Name));
165   }
166   Value *CreateUDiv(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::getUDiv(LC, RC);
170     return Insert(BinaryOperator::createUDiv(LHS, RHS, Name));
171   }
172   Value *CreateSDiv(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::getSDiv(LC, RC);      
176     return Insert(BinaryOperator::createSDiv(LHS, RHS, Name));
177   }
178   Value *CreateFDiv(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::getFDiv(LC, RC);      
182     return Insert(BinaryOperator::createFDiv(LHS, RHS, Name));
183   }
184   Value *CreateURem(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::getURem(LC, RC);
188     return Insert(BinaryOperator::createURem(LHS, RHS, Name));
189   }
190   Value *CreateSRem(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::getSRem(LC, RC);
194     return Insert(BinaryOperator::createSRem(LHS, RHS, Name));
195   }
196   Value *CreateFRem(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::getFRem(LC, RC);
200     return Insert(BinaryOperator::createFRem(LHS, RHS, Name));
201   }
202   Value *CreateShl(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::getShl(LC, RC);
206     return Insert(BinaryOperator::createShl(LHS, RHS, Name));
207   }
208   Value *CreateLShr(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::getLShr(LC, RC);
212     return Insert(BinaryOperator::createLShr(LHS, RHS, Name));
213   }
214   Value *CreateAShr(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::getAShr(LC, RC);
218     return Insert(BinaryOperator::createAShr(LHS, RHS, Name));
219   }
220   Value *CreateAnd(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::getAnd(LC, RC);
224     return Insert(BinaryOperator::createAnd(LHS, RHS, Name));
225   }
226   Value *CreateOr(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::getOr(LC, RC);
230     return Insert(BinaryOperator::createOr(LHS, RHS, Name));
231   }
232   Value *CreateXor(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::getXor(LC, RC);
236     return Insert(BinaryOperator::createXor(LHS, RHS, Name));
237   }
238
239   BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
240                               Value *LHS, Value *RHS, const char *Name = "") {
241     return Insert(BinaryOperator::create(Opc, LHS, RHS, Name));
242   }
243   
244   BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
245     return Insert(BinaryOperator::createNeg(V, Name));
246   }
247   BinaryOperator *CreateNot(Value *V, const char *Name = "") {
248     return Insert(BinaryOperator::createNot(V, Name));
249   }
250   
251   //===--------------------------------------------------------------------===//
252   // Instruction creation methods: Memory Instructions
253   //===--------------------------------------------------------------------===//
254   
255   MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
256                            const char *Name = "") {
257     return Insert(new MallocInst(Ty, ArraySize, Name));
258   }
259   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
260                            const char *Name = "") {
261     return Insert(new AllocaInst(Ty, ArraySize, Name));
262   }
263   FreeInst *CreateFree(Value *Ptr) {
264     return Insert(new FreeInst(Ptr));
265   }
266   LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
267     return Insert(new LoadInst(Ptr, Name));
268   }
269   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
270     return Insert(new LoadInst(Ptr, Name, isVolatile));
271   }
272   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
273     return Insert(new StoreInst(Val, Ptr, isVolatile));
274   }
275   template<typename InputIterator>
276   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, 
277                                InputIterator IdxEnd, const char *Name = "") {
278       
279     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
280       // Every index must be constant.
281       InputIterator i;
282       for (i = IdxBegin; i < IdxEnd; ++i) {
283         if (!dyn_cast<Constant>(*i))
284           break;
285       }
286       if (i == IdxEnd)
287         return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
288     }      
289     return(Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd, Name)));
290   }
291   Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
292     if (Constant *PC = dyn_cast<Constant>(Ptr))
293       if (Constant *IC = dyn_cast<Constant>(Idx))
294         return ConstantExpr::getGetElementPtr(PC, &IC, 1);
295     return Insert(GetElementPtrInst::Create(Ptr, Idx, Name));
296   }
297   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
298     llvm::Value *Idxs[] = {
299       ConstantInt::get(llvm::Type::Int32Ty, 0),
300       ConstantInt::get(llvm::Type::Int32Ty, Idx)
301     };
302     
303     if (Constant *PC = dyn_cast<Constant>(Ptr))
304       return ConstantExpr::getGetElementPtr(PC, Idxs, 2);
305     
306     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2, Name));
307   }
308   
309   //===--------------------------------------------------------------------===//
310   // Instruction creation methods: Cast/Conversion Operators
311   //===--------------------------------------------------------------------===//
312     
313   Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
314     return CreateCast(Instruction::Trunc, V, DestTy, Name);
315   }
316   Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
317     return CreateCast(Instruction::ZExt, V, DestTy, Name);
318   }
319   Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
320     return CreateCast(Instruction::SExt, V, DestTy, Name);
321   }
322   Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
323     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
324   }
325   Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
326     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
327   }
328   Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
329     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
330   }
331   Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
332     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
333   }
334   Value *CreateFPTrunc(Value *V, const Type *DestTy,
335                        const char *Name = "") {
336     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
337   }
338   Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
339     return CreateCast(Instruction::FPExt, V, DestTy, Name);
340   }
341   Value *CreatePtrToInt(Value *V, const Type *DestTy,
342                         const char *Name = "") {
343     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
344   }
345   Value *CreateIntToPtr(Value *V, const Type *DestTy,
346                         const char *Name = "") {
347     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
348   }
349   Value *CreateBitCast(Value *V, const Type *DestTy,
350                        const char *Name = "") {
351     return CreateCast(Instruction::BitCast, V, DestTy, Name);
352   }
353
354   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
355                      const char *Name = "") {
356     if (V->getType() == DestTy)
357       return V;
358     if (Constant *VC = dyn_cast<Constant>(V))
359       return ConstantExpr::getCast(Op, VC, DestTy);      
360     return Insert(CastInst::create(Op, V, DestTy, Name));
361   }
362   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
363                         const char *Name = "") {
364     if (V->getType() == DestTy)
365       return V;
366     if (Constant *VC = dyn_cast<Constant>(V))
367       return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
368     return Insert(CastInst::createIntegerCast(V, DestTy, isSigned, Name));
369   }
370
371   //===--------------------------------------------------------------------===//
372   // Instruction creation methods: Compare Instructions
373   //===--------------------------------------------------------------------===//
374   
375   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
376     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
377   }
378   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
379     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
380   }
381   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
382     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
383   }
384   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
385     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
386   }
387   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
388     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
389   }
390   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
391     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
392   }
393   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
394     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
395   }
396   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
397     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
398   }
399   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
400     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
401   }
402   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
403     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
404   }
405   
406   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
407     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
408   }
409   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
410     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
411   }
412   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
413     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
414   }
415   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
416     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
417   }
418   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
419     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
420   }
421   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
422     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
423   }
424   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
425     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
426   }
427   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
428     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
429   }
430   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
431     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
432   }
433   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
434     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
435   }
436   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
437     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
438   }
439   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
440     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
441   }
442   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
443     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
444   }
445   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
446     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
447   }
448
449   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 
450                     const char *Name = "") {
451     if (Constant *LC = dyn_cast<Constant>(LHS))
452       if (Constant *RC = dyn_cast<Constant>(RHS))
453         return ConstantExpr::getCompare(P, LC, RC);      
454     return Insert(new ICmpInst(P, LHS, RHS, Name));
455   }
456   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 
457                     const char *Name = "") {
458     if (Constant *LC = dyn_cast<Constant>(LHS))
459       if (Constant *RC = dyn_cast<Constant>(RHS))
460         return ConstantExpr::getCompare(P, LC, RC);
461     return Insert(new FCmpInst(P, LHS, RHS, Name));
462   }
463
464   Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 
465                      const char *Name = "") {
466     if (Constant *LC = dyn_cast<Constant>(LHS))
467       if (Constant *RC = dyn_cast<Constant>(RHS))
468         return ConstantExpr::getCompare(P, LC, RC);      
469     return Insert(new VICmpInst(P, LHS, RHS, Name));
470   }
471   Value *CreateVFCmp(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 VFCmpInst(P, LHS, RHS, Name));
477   }
478
479   //===--------------------------------------------------------------------===//
480   // Instruction creation methods: Other Instructions
481   //===--------------------------------------------------------------------===//
482
483   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
484     return Insert(PHINode::Create(Ty, Name));
485   }
486
487   CallInst *CreateCall(Value *Callee, const char *Name = "") {
488     return Insert(CallInst::Create(Callee, Name));
489   }
490   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
491     return Insert(CallInst::Create(Callee, Arg, Name));
492   }
493   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
494                         const char *Name = "") {
495     Value *Args[] = { Arg1, Arg2 };
496     return Insert(CallInst::Create(Callee, Args, Args+2, Name));
497   }
498   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
499                         const char *Name = "") {
500     Value *Args[] = { Arg1, Arg2, Arg3 };
501     return Insert(CallInst::Create(Callee, Args, Args+3, Name));
502   }
503   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
504                         Value *Arg4, const char *Name = "") {
505     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
506     return Insert(CallInst::Create(Callee, Args, Args+4, Name));
507   }
508   
509   template<typename InputIterator>
510   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin, 
511                        InputIterator ArgEnd, const char *Name = "") {
512     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd, Name));
513   }
514
515   Value *CreateSelect(Value *C, Value *True, Value *False,
516                       const char *Name = "") {
517     if (Constant *CC = dyn_cast<Constant>(C))
518       if (Constant *TC = dyn_cast<Constant>(True))
519         if (Constant *FC = dyn_cast<Constant>(False))
520           return ConstantExpr::getSelect(CC, TC, FC);      
521     return Insert(SelectInst::Create(C, True, False, Name));
522   }
523
524   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
525     return Insert(new VAArgInst(List, Ty, Name));
526   }
527
528   Value *CreateExtractElement(Value *Vec, Value *Idx,
529                                          const char *Name = "") {
530     if (Constant *VC = dyn_cast<Constant>(Vec))
531       if (Constant *IC = dyn_cast<Constant>(Idx))
532         return ConstantExpr::getExtractElement(VC, IC);
533     return Insert(new ExtractElementInst(Vec, Idx, Name));
534   }
535
536   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
537                              const char *Name = "") {
538     if (Constant *VC = dyn_cast<Constant>(Vec))
539       if (Constant *NC = dyn_cast<Constant>(NewElt))
540         if (Constant *IC = dyn_cast<Constant>(Idx))
541           return ConstantExpr::getInsertElement(VC, NC, IC);
542     return Insert(InsertElementInst::Create(Vec, NewElt, Idx, Name));
543   }
544
545   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
546                                        const char *Name = "") {
547     if (Constant *V1C = dyn_cast<Constant>(V1))
548       if (Constant *V2C = dyn_cast<Constant>(V2))
549         if (Constant *MC = dyn_cast<Constant>(Mask))
550           return ConstantExpr::getShuffleVector(V1C, V2C, MC);      
551     return Insert(new ShuffleVectorInst(V1, V2, Mask, Name));
552   }
553 };
554   
555 }
556
557 #endif