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