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