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