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