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