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