39aaebbedd8c4a23ea87384bc096a1faa87e6543
[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(TransferAttributes(InvokeInst::Create(Callee,
158                                                         NormalDest, UnwindDest,
159                                                         ArgBegin, ArgEnd),
160                                      Callee), Name);
161   }
162
163   UnwindInst *CreateUnwind() {
164     return Insert(new UnwindInst());
165   }
166
167   UnreachableInst *CreateUnreachable() {
168     return Insert(new UnreachableInst());
169   }
170
171   //===--------------------------------------------------------------------===//
172   // Instruction creation methods: Binary Operators
173   //===--------------------------------------------------------------------===//
174
175   Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
176     if (Constant *LC = dyn_cast<Constant>(LHS))
177       if (Constant *RC = dyn_cast<Constant>(RHS))
178         return Folder.CreateAdd(LC, RC);
179     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
180   }
181   Value *CreateFAdd(Value *LHS, Value *RHS, const char *Name = "") {
182     if (Constant *LC = dyn_cast<Constant>(LHS))
183       if (Constant *RC = dyn_cast<Constant>(RHS))
184         return Folder.CreateFAdd(LC, RC);
185     return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
186   }
187   Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
188     if (Constant *LC = dyn_cast<Constant>(LHS))
189       if (Constant *RC = dyn_cast<Constant>(RHS))
190         return Folder.CreateSub(LC, RC);
191     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
192   }
193   Value *CreateFSub(Value *LHS, Value *RHS, const char *Name = "") {
194     if (Constant *LC = dyn_cast<Constant>(LHS))
195       if (Constant *RC = dyn_cast<Constant>(RHS))
196         return Folder.CreateFSub(LC, RC);
197     return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
198   }
199   Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
200     if (Constant *LC = dyn_cast<Constant>(LHS))
201       if (Constant *RC = dyn_cast<Constant>(RHS))
202         return Folder.CreateMul(LC, RC);
203     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
204   }
205   Value *CreateFMul(Value *LHS, Value *RHS, const char *Name = "") {
206     if (Constant *LC = dyn_cast<Constant>(LHS))
207       if (Constant *RC = dyn_cast<Constant>(RHS))
208         return Folder.CreateFMul(LC, RC);
209     return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
210   }
211   Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
212     if (Constant *LC = dyn_cast<Constant>(LHS))
213       if (Constant *RC = dyn_cast<Constant>(RHS))
214         return Folder.CreateUDiv(LC, RC);
215     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
216   }
217   Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
218     if (Constant *LC = dyn_cast<Constant>(LHS))
219       if (Constant *RC = dyn_cast<Constant>(RHS))
220         return Folder.CreateSDiv(LC, RC);
221     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
222   }
223   Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
224     if (Constant *LC = dyn_cast<Constant>(LHS))
225       if (Constant *RC = dyn_cast<Constant>(RHS))
226         return Folder.CreateFDiv(LC, RC);
227     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
228   }
229   Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
230     if (Constant *LC = dyn_cast<Constant>(LHS))
231       if (Constant *RC = dyn_cast<Constant>(RHS))
232         return Folder.CreateURem(LC, RC);
233     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
234   }
235   Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
236     if (Constant *LC = dyn_cast<Constant>(LHS))
237       if (Constant *RC = dyn_cast<Constant>(RHS))
238         return Folder.CreateSRem(LC, RC);
239     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
240   }
241   Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
242     if (Constant *LC = dyn_cast<Constant>(LHS))
243       if (Constant *RC = dyn_cast<Constant>(RHS))
244         return Folder.CreateFRem(LC, RC);
245     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
246   }
247   Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
248     if (Constant *LC = dyn_cast<Constant>(LHS))
249       if (Constant *RC = dyn_cast<Constant>(RHS))
250         return Folder.CreateShl(LC, RC);
251     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
252   }
253   Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
254     if (Constant *LC = dyn_cast<Constant>(LHS))
255       if (Constant *RC = dyn_cast<Constant>(RHS))
256         return Folder.CreateLShr(LC, RC);
257     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
258   }
259   Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
260     if (Constant *LC = dyn_cast<Constant>(LHS))
261       if (Constant *RC = dyn_cast<Constant>(RHS))
262         return Folder.CreateAShr(LC, RC);
263     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
264   }
265   Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
266     if (Constant *LC = dyn_cast<Constant>(LHS))
267       if (Constant *RC = dyn_cast<Constant>(RHS))
268         return Folder.CreateAnd(LC, RC);
269     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
270   }
271   Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
272     if (Constant *LC = dyn_cast<Constant>(LHS))
273       if (Constant *RC = dyn_cast<Constant>(RHS))
274         return Folder.CreateOr(LC, RC);
275     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
276   }
277   Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
278     if (Constant *LC = dyn_cast<Constant>(LHS))
279       if (Constant *RC = dyn_cast<Constant>(RHS))
280         return Folder.CreateXor(LC, RC);
281     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
282   }
283
284   Value *CreateBinOp(Instruction::BinaryOps Opc,
285                      Value *LHS, Value *RHS, const char *Name = "") {
286     if (Constant *LC = dyn_cast<Constant>(LHS))
287       if (Constant *RC = dyn_cast<Constant>(RHS))
288         return Folder.CreateBinOp(Opc, LC, RC);
289     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
290   }
291
292   Value *CreateNeg(Value *V, const char *Name = "") {
293     if (Constant *VC = dyn_cast<Constant>(V))
294       return Folder.CreateNeg(VC);
295     return Insert(BinaryOperator::CreateNeg(V), Name);
296   }
297   Value *CreateFNeg(Value *V, const char *Name = "") {
298     if (Constant *VC = dyn_cast<Constant>(V))
299       return Folder.CreateFNeg(VC);
300     return Insert(BinaryOperator::CreateFNeg(V), Name);
301   }
302   Value *CreateNot(Value *V, const char *Name = "") {
303     if (Constant *VC = dyn_cast<Constant>(V))
304       return Folder.CreateNot(VC);
305     return Insert(BinaryOperator::CreateNot(V), Name);
306   }
307
308   //===--------------------------------------------------------------------===//
309   // Instruction creation methods: Memory Instructions
310   //===--------------------------------------------------------------------===//
311
312   MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
313                            const char *Name = "") {
314     return Insert(new MallocInst(Ty, ArraySize), Name);
315   }
316   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
317                            const char *Name = "") {
318     return Insert(new AllocaInst(Ty, ArraySize), Name);
319   }
320   FreeInst *CreateFree(Value *Ptr) {
321     return Insert(new FreeInst(Ptr));
322   }
323   LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
324     return Insert(new LoadInst(Ptr), Name);
325   }
326   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
327     return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
328   }
329   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
330     return Insert(new StoreInst(Val, Ptr, isVolatile));
331   }
332   template<typename InputIterator>
333   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
334                    const char *Name = "") {
335     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
336       // Every index must be constant.
337       InputIterator i;
338       for (i = IdxBegin; i < IdxEnd; ++i) {
339         if (!dyn_cast<Constant>(*i))
340           break;
341       }
342       if (i == IdxEnd)
343         return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
344     }
345     return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
346   }
347   Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
348     if (Constant *PC = dyn_cast<Constant>(Ptr))
349       if (Constant *IC = dyn_cast<Constant>(Idx))
350         return Folder.CreateGetElementPtr(PC, &IC, 1);
351     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
352   }
353   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
354     Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
355
356     if (Constant *PC = dyn_cast<Constant>(Ptr))
357       return Folder.CreateGetElementPtr(PC, &Idx, 1);
358
359     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
360   }
361   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, 
362                     const char *Name = "") {
363     Value *Idxs[] = {
364       ConstantInt::get(Type::Int32Ty, Idx0),
365       ConstantInt::get(Type::Int32Ty, Idx1)
366     };
367
368     if (Constant *PC = dyn_cast<Constant>(Ptr))
369       return Folder.CreateGetElementPtr(PC, Idxs, 2);
370
371     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
372   }
373   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
374     Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
375
376     if (Constant *PC = dyn_cast<Constant>(Ptr))
377       return Folder.CreateGetElementPtr(PC, &Idx, 1);
378
379     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
380   }
381   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, 
382                     const char *Name = "") {
383     Value *Idxs[] = {
384       ConstantInt::get(Type::Int64Ty, Idx0),
385       ConstantInt::get(Type::Int64Ty, Idx1)
386     };
387
388     if (Constant *PC = dyn_cast<Constant>(Ptr))
389       return Folder.CreateGetElementPtr(PC, Idxs, 2);
390
391     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
392   }
393   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
394     return CreateConstGEP2_32(Ptr, 0, Idx, Name);
395   }
396   Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
397     Constant *StrConstant = ConstantArray::get(Str, true);
398     GlobalVariable *gv = new GlobalVariable(StrConstant->getType(),
399                                             true,
400                                             GlobalValue::InternalLinkage,
401                                             StrConstant,
402                                             "",
403                                             BB->getParent()->getParent(),
404                                             false);
405     gv->setName(Name);
406     return gv;
407   }
408   Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
409     Value *gv = CreateGlobalString(Str, Name);
410     Value *zero = ConstantInt::get(Type::Int32Ty, 0);
411     Value *Args[] = { zero, zero };
412     return CreateGEP(gv, Args, Args+2, Name);
413   }
414   //===--------------------------------------------------------------------===//
415   // Instruction creation methods: Cast/Conversion Operators
416   //===--------------------------------------------------------------------===//
417
418   Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
419     return CreateCast(Instruction::Trunc, V, DestTy, Name);
420   }
421   Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
422     return CreateCast(Instruction::ZExt, V, DestTy, Name);
423   }
424   Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
425     return CreateCast(Instruction::SExt, V, DestTy, Name);
426   }
427   Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
428     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
429   }
430   Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
431     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
432   }
433   Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
434     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
435   }
436   Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
437     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
438   }
439   Value *CreateFPTrunc(Value *V, const Type *DestTy,
440                        const char *Name = "") {
441     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
442   }
443   Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
444     return CreateCast(Instruction::FPExt, V, DestTy, Name);
445   }
446   Value *CreatePtrToInt(Value *V, const Type *DestTy,
447                         const char *Name = "") {
448     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
449   }
450   Value *CreateIntToPtr(Value *V, const Type *DestTy,
451                         const char *Name = "") {
452     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
453   }
454   Value *CreateBitCast(Value *V, const Type *DestTy,
455                        const char *Name = "") {
456     return CreateCast(Instruction::BitCast, V, DestTy, Name);
457   }
458
459   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
460                     const char *Name = "") {
461     if (V->getType() == DestTy)
462       return V;
463     if (Constant *VC = dyn_cast<Constant>(V))
464       return Folder.CreateCast(Op, VC, DestTy);
465     return Insert(CastInst::Create(Op, V, DestTy), Name);
466   }
467   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
468                        const char *Name = "") {
469     if (V->getType() == DestTy)
470       return V;
471     if (Constant *VC = dyn_cast<Constant>(V))
472       return Folder.CreateIntCast(VC, DestTy, isSigned);
473     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
474   }
475
476   //===--------------------------------------------------------------------===//
477   // Instruction creation methods: Compare Instructions
478   //===--------------------------------------------------------------------===//
479
480   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
481     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
482   }
483   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
484     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
485   }
486   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
487     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
488   }
489   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
490     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
491   }
492   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
493     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
494   }
495   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
496     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
497   }
498   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
499     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
500   }
501   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
502     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
503   }
504   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
505     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
506   }
507   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
508     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
509   }
510
511   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
512     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
513   }
514   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
515     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
516   }
517   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
518     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
519   }
520   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
521     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
522   }
523   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
524     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
525   }
526   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
527     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
528   }
529   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
530     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
531   }
532   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
533     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
534   }
535   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
536     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
537   }
538   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
539     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
540   }
541   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
542     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
543   }
544   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
545     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
546   }
547   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
548     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
549   }
550   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
551     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
552   }
553
554   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
555                     const char *Name = "") {
556     if (Constant *LC = dyn_cast<Constant>(LHS))
557       if (Constant *RC = dyn_cast<Constant>(RHS))
558         return Folder.CreateICmp(P, LC, RC);
559     return Insert(new ICmpInst(P, LHS, RHS), Name);
560   }
561   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
562                     const char *Name = "") {
563     if (Constant *LC = dyn_cast<Constant>(LHS))
564       if (Constant *RC = dyn_cast<Constant>(RHS))
565         return Folder.CreateFCmp(P, LC, RC);
566     return Insert(new FCmpInst(P, LHS, RHS), Name);
567   }
568
569   Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
570                      const char *Name = "") {
571     if (Constant *LC = dyn_cast<Constant>(LHS))
572       if (Constant *RC = dyn_cast<Constant>(RHS))
573         return Folder.CreateVICmp(P, LC, RC);
574     return Insert(new VICmpInst(P, LHS, RHS), Name);
575   }
576   Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
577                      const char *Name = "") {
578     if (Constant *LC = dyn_cast<Constant>(LHS))
579       if (Constant *RC = dyn_cast<Constant>(RHS))
580         return Folder.CreateVFCmp(P, LC, RC);
581     return Insert(new VFCmpInst(P, LHS, RHS), Name);
582   }
583
584   //===--------------------------------------------------------------------===//
585   // Instruction creation methods: Other Instructions
586   //===--------------------------------------------------------------------===//
587
588   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
589     return Insert(PHINode::Create(Ty), Name);
590   }
591
592   CallInst *TransferAttributes(CallInst *CI, const Value* Callee) const {
593     if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Callee))
594       Callee = GA->getAliasedGlobal();
595
596     if (const Function *F = dyn_cast<Function>(Callee)) {
597       CI->setCallingConv(F->getCallingConv());
598       CI->setAttributes(F->getAttributes());
599     }
600
601     return CI;
602   }
603
604   CallInst *CreateCall(Value *Callee, const char *Name = "") {
605     return Insert(TransferAttributes(CallInst::Create(Callee), Callee), Name);
606   }
607   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
608     return Insert(TransferAttributes(CallInst::Create(Callee, Arg),
609                                      Callee), Name);
610   }
611   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
612                         const char *Name = "") {
613     Value *Args[] = { Arg1, Arg2 };
614     return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+2),
615                                      Callee), Name);
616   }
617   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
618                         const char *Name = "") {
619     Value *Args[] = { Arg1, Arg2, Arg3 };
620     return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+3),
621                                      Callee), Name);
622   }
623   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
624                         Value *Arg4, const char *Name = "") {
625     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
626     return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+4),
627                                      Callee), Name);
628   }
629
630   template<typename InputIterator>
631   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
632                        InputIterator ArgEnd, const char *Name = "") {
633     return Insert(TransferAttributes(CallInst::Create(Callee, ArgBegin, ArgEnd),
634                                      Callee), Name);
635   }
636
637   Value *CreateSelect(Value *C, Value *True, Value *False,
638                       const char *Name = "") {
639     if (Constant *CC = dyn_cast<Constant>(C))
640       if (Constant *TC = dyn_cast<Constant>(True))
641         if (Constant *FC = dyn_cast<Constant>(False))
642           return Folder.CreateSelect(CC, TC, FC);
643     return Insert(SelectInst::Create(C, True, False), Name);
644   }
645
646   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
647     return Insert(new VAArgInst(List, Ty), Name);
648   }
649
650   Value *CreateExtractElement(Value *Vec, Value *Idx,
651                               const char *Name = "") {
652     if (Constant *VC = dyn_cast<Constant>(Vec))
653       if (Constant *IC = dyn_cast<Constant>(Idx))
654         return Folder.CreateExtractElement(VC, IC);
655     return Insert(new ExtractElementInst(Vec, Idx), Name);
656   }
657
658   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
659                              const char *Name = "") {
660     if (Constant *VC = dyn_cast<Constant>(Vec))
661       if (Constant *NC = dyn_cast<Constant>(NewElt))
662         if (Constant *IC = dyn_cast<Constant>(Idx))
663           return Folder.CreateInsertElement(VC, NC, IC);
664     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
665   }
666
667   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
668                              const char *Name = "") {
669     if (Constant *V1C = dyn_cast<Constant>(V1))
670       if (Constant *V2C = dyn_cast<Constant>(V2))
671         if (Constant *MC = dyn_cast<Constant>(Mask))
672           return Folder.CreateShuffleVector(V1C, V2C, MC);
673     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
674   }
675
676   Value *CreateExtractValue(Value *Agg, unsigned Idx,
677                             const char *Name = "") {
678     if (Constant *AggC = dyn_cast<Constant>(Agg))
679       return Folder.CreateExtractValue(AggC, &Idx, 1);
680     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
681   }
682
683   template<typename InputIterator>
684   Value *CreateExtractValue(Value *Agg,
685                             InputIterator IdxBegin,
686                             InputIterator IdxEnd,
687                             const char *Name = "") {
688     if (Constant *AggC = dyn_cast<Constant>(Agg))
689       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
690     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
691   }
692
693   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
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, &Idx, 1);
698     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
699   }
700
701   template<typename InputIterator>
702   Value *CreateInsertValue(Value *Agg, Value *Val,
703                            InputIterator IdxBegin,
704                            InputIterator IdxEnd,
705                            const char *Name = "") {
706     if (Constant *AggC = dyn_cast<Constant>(Agg))
707       if (Constant *ValC = dyn_cast<Constant>(Val))
708         return Folder.CreateInsertValue(AggC, ValC,
709                                             IdxBegin, IdxEnd - IdxBegin);
710     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
711   }
712
713   //===--------------------------------------------------------------------===//
714   // Utility creation methods
715   //===--------------------------------------------------------------------===//
716
717   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
718   Value *CreateIsNull(Value *Arg, const char *Name = "") {
719     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
720                         Name);
721   }
722
723   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
724   Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
725     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
726                         Name);
727   }
728
729   /// CreatePtrDiff - Return the i64 difference between two pointer values,
730   /// dividing out the size of the pointed-to objects.  This is intended to
731   /// implement C-style pointer subtraction.
732   Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
733     assert(LHS->getType() == RHS->getType() &&
734            "Pointer subtraction operand types must match!");
735     const PointerType *ArgType = cast<PointerType>(LHS->getType());
736     Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
737     Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
738     Value *Difference = CreateSub(LHS_int, RHS_int);
739     return CreateSDiv(Difference,
740                       ConstantExpr::getSizeOf(ArgType->getElementType()),
741                       Name);
742   }
743 };
744
745 }
746
747 #endif