This started as a small change, I swear. Unfortunately, lots of things call the...
[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     Module &M = *BB->getParent()->getParent();
406     GlobalVariable *gv = new GlobalVariable(M,
407                                             StrConstant->getType(),
408                                             true,
409                                             GlobalValue::InternalLinkage,
410                                             StrConstant,
411                                             "",
412                                             0,
413                                             false);
414     gv->setName(Name);
415     return gv;
416   }
417   Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
418     Value *gv = CreateGlobalString(Str, Name);
419     Value *zero = Context.getConstantInt(Type::Int32Ty, 0);
420     Value *Args[] = { zero, zero };
421     return CreateGEP(gv, Args, Args+2, Name);
422   }
423   //===--------------------------------------------------------------------===//
424   // Instruction creation methods: Cast/Conversion Operators
425   //===--------------------------------------------------------------------===//
426
427   Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
428     return CreateCast(Instruction::Trunc, V, DestTy, Name);
429   }
430   Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
431     return CreateCast(Instruction::ZExt, V, DestTy, Name);
432   }
433   Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
434     return CreateCast(Instruction::SExt, V, DestTy, Name);
435   }
436   Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
437     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
438   }
439   Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
440     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
441   }
442   Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
443     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
444   }
445   Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
446     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
447   }
448   Value *CreateFPTrunc(Value *V, const Type *DestTy,
449                        const char *Name = "") {
450     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
451   }
452   Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
453     return CreateCast(Instruction::FPExt, V, DestTy, Name);
454   }
455   Value *CreatePtrToInt(Value *V, const Type *DestTy,
456                         const char *Name = "") {
457     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
458   }
459   Value *CreateIntToPtr(Value *V, const Type *DestTy,
460                         const char *Name = "") {
461     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
462   }
463   Value *CreateBitCast(Value *V, const Type *DestTy,
464                        const char *Name = "") {
465     return CreateCast(Instruction::BitCast, V, DestTy, Name);
466   }
467
468   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
469                     const char *Name = "") {
470     if (V->getType() == DestTy)
471       return V;
472     if (Constant *VC = dyn_cast<Constant>(V))
473       return Folder.CreateCast(Op, VC, DestTy);
474     return Insert(CastInst::Create(Op, V, DestTy), Name);
475   }
476   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
477                        const char *Name = "") {
478     if (V->getType() == DestTy)
479       return V;
480     if (Constant *VC = dyn_cast<Constant>(V))
481       return Folder.CreateIntCast(VC, DestTy, isSigned);
482     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
483   }
484
485   //===--------------------------------------------------------------------===//
486   // Instruction creation methods: Compare Instructions
487   //===--------------------------------------------------------------------===//
488
489   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
490     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
491   }
492   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
493     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
494   }
495   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
496     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
497   }
498   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
499     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
500   }
501   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
502     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
503   }
504   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
505     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
506   }
507   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
508     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
509   }
510   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
511     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
512   }
513   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
514     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
515   }
516   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
517     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
518   }
519
520   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
521     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
522   }
523   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
524     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
525   }
526   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
527     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
528   }
529   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
530     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
531   }
532   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
533     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
534   }
535   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
536     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
537   }
538   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
539     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
540   }
541   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
542     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
543   }
544   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
545     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
546   }
547   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
548     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
549   }
550   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
551     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
552   }
553   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
554     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
555   }
556   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
557     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
558   }
559   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
560     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
561   }
562
563   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
564                     const char *Name = "") {
565     if (Constant *LC = dyn_cast<Constant>(LHS))
566       if (Constant *RC = dyn_cast<Constant>(RHS))
567         return Folder.CreateICmp(P, LC, RC);
568     return Insert(new ICmpInst(Context, P, LHS, RHS), Name);
569   }
570   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
571                     const char *Name = "") {
572     if (Constant *LC = dyn_cast<Constant>(LHS))
573       if (Constant *RC = dyn_cast<Constant>(RHS))
574         return Folder.CreateFCmp(P, LC, RC);
575     return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
576   }
577
578   //===--------------------------------------------------------------------===//
579   // Instruction creation methods: Other Instructions
580   //===--------------------------------------------------------------------===//
581
582   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
583     return Insert(PHINode::Create(Ty), Name);
584   }
585
586   CallInst *CreateCall(Value *Callee, const char *Name = "") {
587     return Insert(CallInst::Create(Callee), Name);
588   }
589   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
590     return Insert(CallInst::Create(Callee, Arg), Name);
591   }
592   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
593                         const char *Name = "") {
594     Value *Args[] = { Arg1, Arg2 };
595     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
596   }
597   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
598                         const char *Name = "") {
599     Value *Args[] = { Arg1, Arg2, Arg3 };
600     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
601   }
602   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
603                         Value *Arg4, const char *Name = "") {
604     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
605     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
606   }
607
608   template<typename InputIterator>
609   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
610                        InputIterator ArgEnd, const char *Name = "") {
611     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
612   }
613
614   Value *CreateSelect(Value *C, Value *True, Value *False,
615                       const char *Name = "") {
616     if (Constant *CC = dyn_cast<Constant>(C))
617       if (Constant *TC = dyn_cast<Constant>(True))
618         if (Constant *FC = dyn_cast<Constant>(False))
619           return Folder.CreateSelect(CC, TC, FC);
620     return Insert(SelectInst::Create(C, True, False), Name);
621   }
622
623   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
624     return Insert(new VAArgInst(List, Ty), Name);
625   }
626
627   Value *CreateExtractElement(Value *Vec, Value *Idx,
628                               const char *Name = "") {
629     if (Constant *VC = dyn_cast<Constant>(Vec))
630       if (Constant *IC = dyn_cast<Constant>(Idx))
631         return Folder.CreateExtractElement(VC, IC);
632     return Insert(new ExtractElementInst(Vec, Idx), Name);
633   }
634
635   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
636                              const char *Name = "") {
637     if (Constant *VC = dyn_cast<Constant>(Vec))
638       if (Constant *NC = dyn_cast<Constant>(NewElt))
639         if (Constant *IC = dyn_cast<Constant>(Idx))
640           return Folder.CreateInsertElement(VC, NC, IC);
641     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
642   }
643
644   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
645                              const char *Name = "") {
646     if (Constant *V1C = dyn_cast<Constant>(V1))
647       if (Constant *V2C = dyn_cast<Constant>(V2))
648         if (Constant *MC = dyn_cast<Constant>(Mask))
649           return Folder.CreateShuffleVector(V1C, V2C, MC);
650     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
651   }
652
653   Value *CreateExtractValue(Value *Agg, unsigned Idx,
654                             const char *Name = "") {
655     if (Constant *AggC = dyn_cast<Constant>(Agg))
656       return Folder.CreateExtractValue(AggC, &Idx, 1);
657     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
658   }
659
660   template<typename InputIterator>
661   Value *CreateExtractValue(Value *Agg,
662                             InputIterator IdxBegin,
663                             InputIterator IdxEnd,
664                             const char *Name = "") {
665     if (Constant *AggC = dyn_cast<Constant>(Agg))
666       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
667     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
668   }
669
670   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
671                            const char *Name = "") {
672     if (Constant *AggC = dyn_cast<Constant>(Agg))
673       if (Constant *ValC = dyn_cast<Constant>(Val))
674         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
675     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
676   }
677
678   template<typename InputIterator>
679   Value *CreateInsertValue(Value *Agg, Value *Val,
680                            InputIterator IdxBegin,
681                            InputIterator IdxEnd,
682                            const char *Name = "") {
683     if (Constant *AggC = dyn_cast<Constant>(Agg))
684       if (Constant *ValC = dyn_cast<Constant>(Val))
685         return Folder.CreateInsertValue(AggC, ValC,
686                                             IdxBegin, IdxEnd - IdxBegin);
687     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
688   }
689
690   //===--------------------------------------------------------------------===//
691   // Utility creation methods
692   //===--------------------------------------------------------------------===//
693
694   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
695   Value *CreateIsNull(Value *Arg, const char *Name = "") {
696     return CreateICmpEQ(Arg, Context.getNullValue(Arg->getType()),
697                         Name);
698   }
699
700   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
701   Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
702     return CreateICmpNE(Arg, Context.getNullValue(Arg->getType()),
703                         Name);
704   }
705
706   /// CreatePtrDiff - Return the i64 difference between two pointer values,
707   /// dividing out the size of the pointed-to objects.  This is intended to
708   /// implement C-style pointer subtraction.
709   Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
710     assert(LHS->getType() == RHS->getType() &&
711            "Pointer subtraction operand types must match!");
712     const PointerType *ArgType = cast<PointerType>(LHS->getType());
713     Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
714     Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
715     Value *Difference = CreateSub(LHS_int, RHS_int);
716     return CreateSDiv(Difference,
717                       Context.getConstantExprSizeOf(ArgType->getElementType()),
718                       Name);
719   }
720 };
721
722 }
723
724 #endif