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