Add a GetElementPtrInst::getIndexedType that accepts uint64_t's instead of just Value*'s.
[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   /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
105   /// with one Value from the retVals array each, that build a aggregate
106   /// return value one value at a time, and a ret instruction to return
107   /// the resulting aggregate value. This is a convenience function for
108   /// code that uses aggregate return values as a vehicle for having
109   /// multiple return values.
110   ///
111   ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
112     const Type *RetType = BB->getParent()->getReturnType();
113     Value *V = UndefValue::get(RetType);
114     for (unsigned i = 0; i != N; ++i)
115       V = CreateInsertValue(V, retVals[i], i, "mrv");
116     return Insert(ReturnInst::Create(V));
117   }
118   
119   /// CreateBr - Create an unconditional 'br label X' instruction.
120   BranchInst *CreateBr(BasicBlock *Dest) {
121     return Insert(BranchInst::Create(Dest));
122   }
123
124   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
125   /// instruction.
126   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
127     return Insert(BranchInst::Create(True, False, Cond));
128   }
129   
130   /// CreateSwitch - Create a switch instruction with the specified value,
131   /// default dest, and with a hint for the number of cases that will be added
132   /// (for efficient allocation).
133   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
134     return Insert(SwitchInst::Create(V, Dest, NumCases));
135   }
136   
137   /// CreateInvoke - Create an invoke instruction.
138   template<typename InputIterator>
139   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 
140                            BasicBlock *UnwindDest, InputIterator ArgBegin, 
141                            InputIterator ArgEnd, const char *Name = "") {
142     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
143                                      ArgBegin, ArgEnd), Name);
144   }
145   
146   UnwindInst *CreateUnwind() {
147     return Insert(new UnwindInst());
148   }
149
150   UnreachableInst *CreateUnreachable() {
151     return Insert(new UnreachableInst());
152   }
153   
154   //===--------------------------------------------------------------------===//
155   // Instruction creation methods: Binary Operators
156   //===--------------------------------------------------------------------===//
157
158   Value *CreateAdd(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::getAdd(LC, RC);      
162     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
163   }
164   Value *CreateSub(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::getSub(LC, RC);
168     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
169   }
170   Value *CreateMul(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::getMul(LC, RC);
174     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
175   }
176   Value *CreateUDiv(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::getUDiv(LC, RC);
180     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
181   }
182   Value *CreateSDiv(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::getSDiv(LC, RC);      
186     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
187   }
188   Value *CreateFDiv(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::getFDiv(LC, RC);      
192     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
193   }
194   Value *CreateURem(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::getURem(LC, RC);
198     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
199   }
200   Value *CreateSRem(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::getSRem(LC, RC);
204     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
205   }
206   Value *CreateFRem(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::getFRem(LC, RC);
210     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
211   }
212   Value *CreateShl(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::getShl(LC, RC);
216     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
217   }
218   Value *CreateLShr(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::getLShr(LC, RC);
222     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
223   }
224   Value *CreateAShr(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::getAShr(LC, RC);
228     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
229   }
230   Value *CreateAnd(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::getAnd(LC, RC);
234     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
235   }
236   Value *CreateOr(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::getOr(LC, RC);
240     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
241   }
242   Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
243     if (Constant *LC = dyn_cast<Constant>(LHS))
244       if (Constant *RC = dyn_cast<Constant>(RHS))
245         return ConstantExpr::getXor(LC, RC);
246     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
247   }
248
249   Value *CreateBinOp(Instruction::BinaryOps Opc,
250                      Value *LHS, Value *RHS, const char *Name = "") {
251     if (Constant *LC = dyn_cast<Constant>(LHS))
252       if (Constant *RC = dyn_cast<Constant>(RHS))
253         return ConstantExpr::get(Opc, LC, RC);
254     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
255   }
256   
257   Value *CreateNeg(Value *V, const char *Name = "") {
258     if (Constant *VC = dyn_cast<Constant>(V))
259       return ConstantExpr::getNeg(VC);
260     return Insert(BinaryOperator::CreateNeg(V), Name);
261   }
262   Value *CreateNot(Value *V, const char *Name = "") {
263     if (Constant *VC = dyn_cast<Constant>(V))
264       return ConstantExpr::getNot(VC);
265     return Insert(BinaryOperator::CreateNot(V), Name);
266   }
267   
268   //===--------------------------------------------------------------------===//
269   // Instruction creation methods: Memory Instructions
270   //===--------------------------------------------------------------------===//
271   
272   MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
273                            const char *Name = "") {
274     return Insert(new MallocInst(Ty, ArraySize), Name);
275   }
276   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
277                            const char *Name = "") {
278     return Insert(new AllocaInst(Ty, ArraySize), Name);
279   }
280   FreeInst *CreateFree(Value *Ptr) {
281     return Insert(new FreeInst(Ptr));
282   }
283   LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
284     return Insert(new LoadInst(Ptr, Name));
285   }
286   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
287     return Insert(new LoadInst(Ptr, Name, isVolatile));
288   }
289   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
290     return Insert(new StoreInst(Val, Ptr, isVolatile));
291   }
292   template<typename InputIterator>
293   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, 
294                                InputIterator IdxEnd, const char *Name = "") {
295       
296     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
297       // Every index must be constant.
298       InputIterator i;
299       for (i = IdxBegin; i < IdxEnd; ++i) {
300         if (!dyn_cast<Constant>(*i))
301           break;
302       }
303       if (i == IdxEnd)
304         return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0], 
305                                               IdxEnd - IdxBegin);
306     }      
307     return(Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name));
308   }
309   Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
310     if (Constant *PC = dyn_cast<Constant>(Ptr))
311       if (Constant *IC = dyn_cast<Constant>(Idx))
312         return ConstantExpr::getGetElementPtr(PC, &IC, 1);
313     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
314   }
315   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
316     llvm::Value *Idxs[] = {
317       ConstantInt::get(llvm::Type::Int32Ty, 0),
318       ConstantInt::get(llvm::Type::Int32Ty, Idx)
319     };
320     
321     if (Constant *PC = dyn_cast<Constant>(Ptr))
322       return ConstantExpr::getGetElementPtr(PC, Idxs, 2);
323     
324     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
325   }
326   Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
327     Constant *StrConstant = ConstantArray::get(Str, true);
328     GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(),
329                                                   true, 
330                                                   GlobalValue::InternalLinkage,
331                                                   StrConstant,
332                                                   "",
333                                                   BB->getParent()->getParent(),
334                                                   false);
335     gv->setName(Name);
336     return gv;
337   }
338   Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
339     Value *gv = CreateGlobalString(Str, Name);
340     Value *zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
341     Value *Args[] = { zero, zero };
342     return CreateGEP(gv, Args, Args+2, Name);    
343   }
344   //===--------------------------------------------------------------------===//
345   // Instruction creation methods: Cast/Conversion Operators
346   //===--------------------------------------------------------------------===//
347     
348   Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
349     return CreateCast(Instruction::Trunc, V, DestTy, Name);
350   }
351   Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
352     return CreateCast(Instruction::ZExt, V, DestTy, Name);
353   }
354   Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
355     return CreateCast(Instruction::SExt, V, DestTy, Name);
356   }
357   Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
358     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
359   }
360   Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
361     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
362   }
363   Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
364     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
365   }
366   Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
367     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
368   }
369   Value *CreateFPTrunc(Value *V, const Type *DestTy,
370                        const char *Name = "") {
371     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
372   }
373   Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
374     return CreateCast(Instruction::FPExt, V, DestTy, Name);
375   }
376   Value *CreatePtrToInt(Value *V, const Type *DestTy,
377                         const char *Name = "") {
378     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
379   }
380   Value *CreateIntToPtr(Value *V, const Type *DestTy,
381                         const char *Name = "") {
382     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
383   }
384   Value *CreateBitCast(Value *V, const Type *DestTy,
385                        const char *Name = "") {
386     return CreateCast(Instruction::BitCast, V, DestTy, Name);
387   }
388
389   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
390                      const char *Name = "") {
391     if (V->getType() == DestTy)
392       return V;
393     if (Constant *VC = dyn_cast<Constant>(V))
394       return ConstantExpr::getCast(Op, VC, DestTy);      
395     return Insert(CastInst::Create(Op, V, DestTy), Name);
396   }
397   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
398                         const char *Name = "") {
399     if (V->getType() == DestTy)
400       return V;
401     if (Constant *VC = dyn_cast<Constant>(V))
402       return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
403     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
404   }
405
406   //===--------------------------------------------------------------------===//
407   // Instruction creation methods: Compare Instructions
408   //===--------------------------------------------------------------------===//
409   
410   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
411     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
412   }
413   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
414     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
415   }
416   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
417     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
418   }
419   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
420     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
421   }
422   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
423     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
424   }
425   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
426     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
427   }
428   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
429     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
430   }
431   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
432     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
433   }
434   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
435     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
436   }
437   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
438     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
439   }
440   
441   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
442     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
443   }
444   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
445     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
446   }
447   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
448     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
449   }
450   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
451     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
452   }
453   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
454     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
455   }
456   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
457     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
458   }
459   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
460     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
461   }
462   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
463     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
464   }
465   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
466     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
467   }
468   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
469     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
470   }
471   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
472     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
473   }
474   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
475     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
476   }
477   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
478     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
479   }
480   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
481     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
482   }
483
484   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 
485                     const char *Name = "") {
486     if (Constant *LC = dyn_cast<Constant>(LHS))
487       if (Constant *RC = dyn_cast<Constant>(RHS))
488         return ConstantExpr::getCompare(P, LC, RC);      
489     return Insert(new ICmpInst(P, LHS, RHS), Name);
490   }
491   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 
492                     const char *Name = "") {
493     if (Constant *LC = dyn_cast<Constant>(LHS))
494       if (Constant *RC = dyn_cast<Constant>(RHS))
495         return ConstantExpr::getCompare(P, LC, RC);
496     return Insert(new FCmpInst(P, LHS, RHS), Name);
497   }
498
499   Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 
500                      const char *Name = "") {
501     if (Constant *LC = dyn_cast<Constant>(LHS))
502       if (Constant *RC = dyn_cast<Constant>(RHS))
503         return ConstantExpr::getCompare(P, LC, RC);      
504     return Insert(new VICmpInst(P, LHS, RHS), Name);
505   }
506   Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, 
507                      const char *Name = "") {
508     if (Constant *LC = dyn_cast<Constant>(LHS))
509       if (Constant *RC = dyn_cast<Constant>(RHS))
510         return ConstantExpr::getCompare(P, LC, RC);
511     return Insert(new VFCmpInst(P, LHS, RHS), Name);
512   }
513
514   //===--------------------------------------------------------------------===//
515   // Instruction creation methods: Other Instructions
516   //===--------------------------------------------------------------------===//
517
518   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
519     return Insert(PHINode::Create(Ty), Name);
520   }
521
522   CallInst *CreateCall(Value *Callee, const char *Name = "") {
523     return Insert(CallInst::Create(Callee), Name);
524   }
525   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
526     return Insert(CallInst::Create(Callee, Arg), Name);
527   }
528   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
529                         const char *Name = "") {
530     Value *Args[] = { Arg1, Arg2 };
531     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
532   }
533   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
534                         const char *Name = "") {
535     Value *Args[] = { Arg1, Arg2, Arg3 };
536     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
537   }
538   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
539                         Value *Arg4, const char *Name = "") {
540     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
541     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
542   }
543   
544   template<typename InputIterator>
545   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin, 
546                        InputIterator ArgEnd, const char *Name = "") {
547     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
548   }
549
550   Value *CreateSelect(Value *C, Value *True, Value *False,
551                       const char *Name = "") {
552     if (Constant *CC = dyn_cast<Constant>(C))
553       if (Constant *TC = dyn_cast<Constant>(True))
554         if (Constant *FC = dyn_cast<Constant>(False))
555           return ConstantExpr::getSelect(CC, TC, FC);      
556     return Insert(SelectInst::Create(C, True, False), Name);
557   }
558
559   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
560     return Insert(new VAArgInst(List, Ty), Name);
561   }
562
563   Value *CreateExtractElement(Value *Vec, Value *Idx,
564                                          const char *Name = "") {
565     if (Constant *VC = dyn_cast<Constant>(Vec))
566       if (Constant *IC = dyn_cast<Constant>(Idx))
567         return ConstantExpr::getExtractElement(VC, IC);
568     return Insert(new ExtractElementInst(Vec, Idx), Name);
569   }
570
571   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
572                              const char *Name = "") {
573     if (Constant *VC = dyn_cast<Constant>(Vec))
574       if (Constant *NC = dyn_cast<Constant>(NewElt))
575         if (Constant *IC = dyn_cast<Constant>(Idx))
576           return ConstantExpr::getInsertElement(VC, NC, IC);
577     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
578   }
579
580   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
581                                        const char *Name = "") {
582     if (Constant *V1C = dyn_cast<Constant>(V1))
583       if (Constant *V2C = dyn_cast<Constant>(V2))
584         if (Constant *MC = dyn_cast<Constant>(Mask))
585           return ConstantExpr::getShuffleVector(V1C, V2C, MC);      
586     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
587   }
588
589   Value *CreateExtractValue(Value *Agg, unsigned Idx,
590                             const char *Name = "") {
591     if (Constant *AggC = dyn_cast<Constant>(Agg))
592       return ConstantExpr::getExtractValue(AggC, &Idx, 1);
593     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
594   }
595
596   template<typename InputIterator>
597   Value *CreateExtractValue(Value *Agg,
598                             InputIterator IdxBegin,
599                             InputIterator IdxEnd,
600                             const char *Name = "") {
601     if (Constant *AggC = dyn_cast<Constant>(Agg))
602       return ConstantExpr::getExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
603     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
604   }
605
606   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
607                            const char *Name = "") {
608     if (Constant *AggC = dyn_cast<Constant>(Agg))
609       if (Constant *ValC = dyn_cast<Constant>(Val))
610         return ConstantExpr::getInsertValue(AggC, ValC, &Idx, 1);
611     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
612   }
613
614   template<typename InputIterator>
615   Value *CreateInsertValue(Value *Agg, Value *Val,
616                            InputIterator IdxBegin,
617                            InputIterator IdxEnd,
618                            const char *Name = "") {
619     if (Constant *AggC = dyn_cast<Constant>(Agg))
620       if (Constant *ValC = dyn_cast<Constant>(Val))
621         return ConstantExpr::getInsertValue(AggC, ValC,
622                                             IdxBegin, IdxEnd - IdxBegin);
623     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
624   }
625 };
626   
627 }
628
629 #endif