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