Add a couple more helper functions to deal with
[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 #include "llvm/GlobalVariable.h"
22 #include "llvm/Function.h"
23
24 namespace llvm {
25
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.
29 ///
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 class IRBuilder {
37   BasicBlock *BB;
38   BasicBlock::iterator InsertPt;
39 public:
40   IRBuilder() { ClearInsertionPoint(); }
41   explicit IRBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
42   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
43     SetInsertPoint(TheBB, IP);
44   }
45
46   //===--------------------------------------------------------------------===//
47   // Builder configuration methods
48   //===--------------------------------------------------------------------===//
49
50   /// ClearInsertionPoint - Clear the insertion point: created instructions will
51   /// not be inserted into a block.
52   void ClearInsertionPoint() {
53     BB = 0;
54   }
55   
56   BasicBlock *GetInsertBlock() const { return BB; }
57   
58   /// SetInsertPoint - This specifies that created instructions should be
59   /// appended to the end of the specified block.
60   void SetInsertPoint(BasicBlock *TheBB) {
61     BB = TheBB;
62     InsertPt = BB->end();
63   }
64   
65   /// SetInsertPoint - This specifies that created instructions should be
66   /// inserted at the specified point.
67   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
68     BB = TheBB;
69     InsertPt = IP;
70   }
71   
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);
76     return I;
77   }
78   
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);
84     if (Name[0])
85       I->setName(Name);
86   }
87   
88   //===--------------------------------------------------------------------===//
89   // Instruction creation methods: Terminators
90   //===--------------------------------------------------------------------===//
91
92   /// CreateRetVoid - Create a 'ret void' instruction.
93   ReturnInst *CreateRetVoid() {
94     return Insert(ReturnInst::Create());
95   }
96
97   /// @verbatim 
98   /// CreateRet - Create a 'ret <val>' instruction. 
99   /// @endverbatim
100   ReturnInst *CreateRet(Value *V) {
101     return Insert(ReturnInst::Create(V));
102   }
103
104   ReturnInst *CreateRet(Value * const* retVals, unsigned N) {
105     return Insert(ReturnInst::Create(retVals, N));
106   }
107   
108   GetResultInst *CreateGetResult(Value *V, unsigned Index, 
109                                  const char *Name = "") {
110     return Insert(new GetResultInst(V, Index), Name);
111   }
112     
113   /// CreateBr - Create an unconditional 'br label X' instruction.
114   BranchInst *CreateBr(BasicBlock *Dest) {
115     return Insert(BranchInst::Create(Dest));
116   }
117
118   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
119   /// instruction.
120   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
121     return Insert(BranchInst::Create(True, False, Cond));
122   }
123   
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));
129   }
130   
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);
138   }
139   
140   UnwindInst *CreateUnwind() {
141     return Insert(new UnwindInst());
142   }
143
144   UnreachableInst *CreateUnreachable() {
145     return Insert(new UnreachableInst());
146   }
147   
148   //===--------------------------------------------------------------------===//
149   // Instruction creation methods: Binary Operators
150   //===--------------------------------------------------------------------===//
151
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);
157   }
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);
163   }
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);
169   }
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);
175   }
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);
181   }
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);
187   }
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);
193   }
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);
199   }
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);
205   }
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);
211   }
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);
217   }
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);
223   }
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);
229   }
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);
235   }
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);
241   }
242
243   BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
244                               Value *LHS, Value *RHS, const char *Name = "") {
245     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
246   }
247   
248   BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
249     return Insert(BinaryOperator::CreateNeg(V), Name);
250   }
251   BinaryOperator *CreateNot(Value *V, const char *Name = "") {
252     return Insert(BinaryOperator::CreateNot(V), Name);
253   }
254   
255   //===--------------------------------------------------------------------===//
256   // Instruction creation methods: Memory Instructions
257   //===--------------------------------------------------------------------===//
258   
259   MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
260                            const char *Name = "") {
261     return Insert(new MallocInst(Ty, ArraySize), Name);
262   }
263   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
264                            const char *Name = "") {
265     return Insert(new AllocaInst(Ty, ArraySize), Name);
266   }
267   FreeInst *CreateFree(Value *Ptr) {
268     return Insert(new FreeInst(Ptr));
269   }
270   LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
271     return Insert(new LoadInst(Ptr, Name));
272   }
273   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
274     return Insert(new LoadInst(Ptr, Name, isVolatile));
275   }
276   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
277     return Insert(new StoreInst(Val, Ptr, isVolatile));
278   }
279   template<typename InputIterator>
280   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, 
281                                InputIterator IdxEnd, const char *Name = "") {
282       
283     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
284       // Every index must be constant.
285       InputIterator i;
286       for (i = IdxBegin; i < IdxEnd; ++i) {
287         if (!dyn_cast<Constant>(*i))
288           break;
289       }
290       if (i == IdxEnd)
291         return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0], 
292                                               IdxEnd - IdxBegin);
293     }      
294     return(Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name));
295   }
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);
301   }
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)
306     };
307     
308     if (Constant *PC = dyn_cast<Constant>(Ptr))
309       return ConstantExpr::getGetElementPtr(PC, Idxs, 2);
310     
311     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
312   }
313   Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
314     Constant *StrConstant = ConstantArray::get(Str, true);
315     GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(),
316                                                   true, 
317                                                   GlobalValue::InternalLinkage,
318                                                   StrConstant,
319                                                   "",
320                                                   BB->getParent()->getParent(),
321                                                   false);
322     gv->setName(Name);
323     return gv;
324   }
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);    
330   }
331   //===--------------------------------------------------------------------===//
332   // Instruction creation methods: Cast/Conversion Operators
333   //===--------------------------------------------------------------------===//
334     
335   Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
336     return CreateCast(Instruction::Trunc, V, DestTy, Name);
337   }
338   Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
339     return CreateCast(Instruction::ZExt, V, DestTy, Name);
340   }
341   Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
342     return CreateCast(Instruction::SExt, V, DestTy, Name);
343   }
344   Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
345     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
346   }
347   Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
348     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
349   }
350   Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
351     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
352   }
353   Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
354     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
355   }
356   Value *CreateFPTrunc(Value *V, const Type *DestTy,
357                        const char *Name = "") {
358     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
359   }
360   Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
361     return CreateCast(Instruction::FPExt, V, DestTy, Name);
362   }
363   Value *CreatePtrToInt(Value *V, const Type *DestTy,
364                         const char *Name = "") {
365     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
366   }
367   Value *CreateIntToPtr(Value *V, const Type *DestTy,
368                         const char *Name = "") {
369     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
370   }
371   Value *CreateBitCast(Value *V, const Type *DestTy,
372                        const char *Name = "") {
373     return CreateCast(Instruction::BitCast, V, DestTy, Name);
374   }
375
376   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
377                      const char *Name = "") {
378     if (V->getType() == DestTy)
379       return V;
380     if (Constant *VC = dyn_cast<Constant>(V))
381       return ConstantExpr::getCast(Op, VC, DestTy);      
382     return Insert(CastInst::Create(Op, V, DestTy), Name);
383   }
384   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
385                         const char *Name = "") {
386     if (V->getType() == DestTy)
387       return V;
388     if (Constant *VC = dyn_cast<Constant>(V))
389       return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
390     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
391   }
392
393   //===--------------------------------------------------------------------===//
394   // Instruction creation methods: Compare Instructions
395   //===--------------------------------------------------------------------===//
396   
397   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
398     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
399   }
400   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
401     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
402   }
403   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
404     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
405   }
406   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
407     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
408   }
409   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
410     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
411   }
412   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
413     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
414   }
415   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
416     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
417   }
418   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
419     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
420   }
421   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
422     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
423   }
424   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
425     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
426   }
427   
428   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
429     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
430   }
431   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
432     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
433   }
434   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
435     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
436   }
437   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
438     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
439   }
440   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
441     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
442   }
443   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
444     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
445   }
446   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
447     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
448   }
449   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
450     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
451   }
452   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
453     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
454   }
455   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
456     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
457   }
458   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
459     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
460   }
461   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
462     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
463   }
464   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
465     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
466   }
467   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
468     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
469   }
470
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);
477   }
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);
484   }
485
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);
492   }
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);
499   }
500
501   //===--------------------------------------------------------------------===//
502   // Instruction creation methods: Other Instructions
503   //===--------------------------------------------------------------------===//
504
505   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
506     return Insert(PHINode::Create(Ty), Name);
507   }
508
509   CallInst *CreateCall(Value *Callee, const char *Name = "") {
510     return Insert(CallInst::Create(Callee), Name);
511   }
512   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
513     return Insert(CallInst::Create(Callee, Arg), Name);
514   }
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);
519   }
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);
524   }
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);
529   }
530   
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);
535   }
536
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);
544   }
545
546   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
547     return Insert(new VAArgInst(List, Ty), Name);
548   }
549
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);
556   }
557
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);
565   }
566
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);
574   }
575 };
576   
577 }
578
579 #endif