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