Switch GlobalVariable ctors to a sane API, where *either* a context or a module is...
[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(*BB->getParent()->getParent(),
397                                             StrConstant->getType(),
398                                             true,
399                                             GlobalValue::InternalLinkage,
400                                             StrConstant,
401                                             "",
402                                             0,
403                                             false);
404     gv->setName(Name);
405     return gv;
406   }
407   Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
408     Value *gv = CreateGlobalString(Str, Name);
409     Value *zero = ConstantInt::get(Type::Int32Ty, 0);
410     Value *Args[] = { zero, zero };
411     return CreateGEP(gv, Args, Args+2, Name);
412   }
413   //===--------------------------------------------------------------------===//
414   // Instruction creation methods: Cast/Conversion Operators
415   //===--------------------------------------------------------------------===//
416
417   Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
418     return CreateCast(Instruction::Trunc, V, DestTy, Name);
419   }
420   Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
421     return CreateCast(Instruction::ZExt, V, DestTy, Name);
422   }
423   Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
424     return CreateCast(Instruction::SExt, V, DestTy, Name);
425   }
426   Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
427     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
428   }
429   Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
430     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
431   }
432   Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
433     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
434   }
435   Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
436     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
437   }
438   Value *CreateFPTrunc(Value *V, const Type *DestTy,
439                        const char *Name = "") {
440     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
441   }
442   Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
443     return CreateCast(Instruction::FPExt, V, DestTy, Name);
444   }
445   Value *CreatePtrToInt(Value *V, const Type *DestTy,
446                         const char *Name = "") {
447     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
448   }
449   Value *CreateIntToPtr(Value *V, const Type *DestTy,
450                         const char *Name = "") {
451     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
452   }
453   Value *CreateBitCast(Value *V, const Type *DestTy,
454                        const char *Name = "") {
455     return CreateCast(Instruction::BitCast, V, DestTy, Name);
456   }
457
458   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
459                     const char *Name = "") {
460     if (V->getType() == DestTy)
461       return V;
462     if (Constant *VC = dyn_cast<Constant>(V))
463       return Folder.CreateCast(Op, VC, DestTy);
464     return Insert(CastInst::Create(Op, V, DestTy), Name);
465   }
466   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
467                        const char *Name = "") {
468     if (V->getType() == DestTy)
469       return V;
470     if (Constant *VC = dyn_cast<Constant>(V))
471       return Folder.CreateIntCast(VC, DestTy, isSigned);
472     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
473   }
474
475   //===--------------------------------------------------------------------===//
476   // Instruction creation methods: Compare Instructions
477   //===--------------------------------------------------------------------===//
478
479   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
480     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
481   }
482   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
483     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
484   }
485   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
486     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
487   }
488   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
489     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
490   }
491   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
492     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
493   }
494   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
495     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
496   }
497   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
498     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
499   }
500   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
501     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
502   }
503   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
504     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
505   }
506   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
507     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
508   }
509
510   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
511     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
512   }
513   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
514     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
515   }
516   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
517     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
518   }
519   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
520     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
521   }
522   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
523     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
524   }
525   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
526     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
527   }
528   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
529     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
530   }
531   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
532     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
533   }
534   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
535     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
536   }
537   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
538     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
539   }
540   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
541     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
542   }
543   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
544     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
545   }
546   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
547     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
548   }
549   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
550     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
551   }
552
553   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
554                     const char *Name = "") {
555     if (Constant *LC = dyn_cast<Constant>(LHS))
556       if (Constant *RC = dyn_cast<Constant>(RHS))
557         return Folder.CreateICmp(P, LC, RC);
558     return Insert(new ICmpInst(P, LHS, RHS), Name);
559   }
560   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
561                     const char *Name = "") {
562     if (Constant *LC = dyn_cast<Constant>(LHS))
563       if (Constant *RC = dyn_cast<Constant>(RHS))
564         return Folder.CreateFCmp(P, LC, RC);
565     return Insert(new FCmpInst(P, LHS, RHS), Name);
566   }
567
568   //===--------------------------------------------------------------------===//
569   // Instruction creation methods: Other Instructions
570   //===--------------------------------------------------------------------===//
571
572   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
573     return Insert(PHINode::Create(Ty), Name);
574   }
575
576   CallInst *CreateCall(Value *Callee, const char *Name = "") {
577     return Insert(CallInst::Create(Callee), Name);
578   }
579   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
580     return Insert(CallInst::Create(Callee, Arg), Name);
581   }
582   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
583                         const char *Name = "") {
584     Value *Args[] = { Arg1, Arg2 };
585     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
586   }
587   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
588                         const char *Name = "") {
589     Value *Args[] = { Arg1, Arg2, Arg3 };
590     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
591   }
592   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
593                         Value *Arg4, const char *Name = "") {
594     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
595     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
596   }
597
598   template<typename InputIterator>
599   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
600                        InputIterator ArgEnd, const char *Name = "") {
601     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
602   }
603
604   Value *CreateSelect(Value *C, Value *True, Value *False,
605                       const char *Name = "") {
606     if (Constant *CC = dyn_cast<Constant>(C))
607       if (Constant *TC = dyn_cast<Constant>(True))
608         if (Constant *FC = dyn_cast<Constant>(False))
609           return Folder.CreateSelect(CC, TC, FC);
610     return Insert(SelectInst::Create(C, True, False), Name);
611   }
612
613   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
614     return Insert(new VAArgInst(List, Ty), Name);
615   }
616
617   Value *CreateExtractElement(Value *Vec, Value *Idx,
618                               const char *Name = "") {
619     if (Constant *VC = dyn_cast<Constant>(Vec))
620       if (Constant *IC = dyn_cast<Constant>(Idx))
621         return Folder.CreateExtractElement(VC, IC);
622     return Insert(new ExtractElementInst(Vec, Idx), Name);
623   }
624
625   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
626                              const char *Name = "") {
627     if (Constant *VC = dyn_cast<Constant>(Vec))
628       if (Constant *NC = dyn_cast<Constant>(NewElt))
629         if (Constant *IC = dyn_cast<Constant>(Idx))
630           return Folder.CreateInsertElement(VC, NC, IC);
631     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
632   }
633
634   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
635                              const char *Name = "") {
636     if (Constant *V1C = dyn_cast<Constant>(V1))
637       if (Constant *V2C = dyn_cast<Constant>(V2))
638         if (Constant *MC = dyn_cast<Constant>(Mask))
639           return Folder.CreateShuffleVector(V1C, V2C, MC);
640     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
641   }
642
643   Value *CreateExtractValue(Value *Agg, unsigned Idx,
644                             const char *Name = "") {
645     if (Constant *AggC = dyn_cast<Constant>(Agg))
646       return Folder.CreateExtractValue(AggC, &Idx, 1);
647     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
648   }
649
650   template<typename InputIterator>
651   Value *CreateExtractValue(Value *Agg,
652                             InputIterator IdxBegin,
653                             InputIterator IdxEnd,
654                             const char *Name = "") {
655     if (Constant *AggC = dyn_cast<Constant>(Agg))
656       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
657     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
658   }
659
660   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
661                            const char *Name = "") {
662     if (Constant *AggC = dyn_cast<Constant>(Agg))
663       if (Constant *ValC = dyn_cast<Constant>(Val))
664         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
665     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
666   }
667
668   template<typename InputIterator>
669   Value *CreateInsertValue(Value *Agg, Value *Val,
670                            InputIterator IdxBegin,
671                            InputIterator IdxEnd,
672                            const char *Name = "") {
673     if (Constant *AggC = dyn_cast<Constant>(Agg))
674       if (Constant *ValC = dyn_cast<Constant>(Val))
675         return Folder.CreateInsertValue(AggC, ValC,
676                                             IdxBegin, IdxEnd - IdxBegin);
677     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
678   }
679
680   //===--------------------------------------------------------------------===//
681   // Utility creation methods
682   //===--------------------------------------------------------------------===//
683
684   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
685   Value *CreateIsNull(Value *Arg, const char *Name = "") {
686     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
687                         Name);
688   }
689
690   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
691   Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
692     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
693                         Name);
694   }
695
696   /// CreatePtrDiff - Return the i64 difference between two pointer values,
697   /// dividing out the size of the pointed-to objects.  This is intended to
698   /// implement C-style pointer subtraction.
699   Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
700     assert(LHS->getType() == RHS->getType() &&
701            "Pointer subtraction operand types must match!");
702     const PointerType *ArgType = cast<PointerType>(LHS->getType());
703     Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
704     Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
705     Value *Difference = CreateSub(LHS_int, RHS_int);
706     return CreateSDiv(Difference,
707                       ConstantExpr::getSizeOf(ArgType->getElementType()),
708                       Name);
709   }
710 };
711
712 }
713
714 #endif