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