153c58a3acd8d706a18f51679bab938e273d1894
[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   LLVMContext &Context;
46   T Folder;
47 public:
48   IRBuilder(LLVMContext &C, const T& F) :
49     Context(C), Folder(F) { ClearInsertionPoint(); }
50   
51   explicit IRBuilder(LLVMContext &C) : Context(C), Folder(C) {
52     ClearInsertionPoint();
53   }
54   
55   explicit IRBuilder(BasicBlock *TheBB, const T& F)
56       : Context(TheBB->getContext()), Folder(F) {
57     SetInsertPoint(TheBB);
58   }
59   
60   explicit IRBuilder(BasicBlock *TheBB)
61       : Context(TheBB->getContext()), Folder(Context) {
62     SetInsertPoint(TheBB);
63   }
64   
65   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
66       : Context(TheBB->getContext()), Folder(F) {
67     SetInsertPoint(TheBB, IP);
68   }
69   
70   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
71       : Context(TheBB->getContext()), Folder(Context) {
72     SetInsertPoint(TheBB, IP);
73   }
74
75   /// getFolder - Get the constant folder being used.
76   const T& getFolder() { return Folder; }
77
78   /// isNamePreserving - Return true if this builder is configured to actually
79   /// add the requested names to IR created through it.
80   bool isNamePreserving() const { return preserveNames; }
81   
82   //===--------------------------------------------------------------------===//
83   // Builder configuration methods
84   //===--------------------------------------------------------------------===//
85
86   /// ClearInsertionPoint - Clear the insertion point: created instructions will
87   /// not be inserted into a block.
88   void ClearInsertionPoint() {
89     BB = 0;
90   }
91
92   BasicBlock *GetInsertBlock() const { return BB; }
93
94   BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
95
96   /// SetInsertPoint - This specifies that created instructions should be
97   /// appended to the end of the specified block.
98   void SetInsertPoint(BasicBlock *TheBB) {
99     BB = TheBB;
100     InsertPt = BB->end();
101   }
102
103   /// SetInsertPoint - This specifies that created instructions should be
104   /// inserted at the specified point.
105   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
106     BB = TheBB;
107     InsertPt = IP;
108   }
109
110   /// Insert - Insert and return the specified instruction.
111   template<typename InstTy>
112   InstTy *Insert(InstTy *I, const char *Name = "") const {
113     InsertHelper(I, Name);
114     return I;
115   }
116
117   /// InsertHelper - Insert the specified instruction at the specified insertion
118   /// point.  This is split out of Insert so that it isn't duplicated for every
119   /// template instantiation.
120   void InsertHelper(Instruction *I, const char *Name) const {
121     if (BB) BB->getInstList().insert(InsertPt, I);
122     if (preserveNames && Name[0])
123       I->setName(Name);
124   }
125
126   //===--------------------------------------------------------------------===//
127   // Type creation methods
128   //===--------------------------------------------------------------------===//
129
130   /// getInt1Ty - Fetch the type representing a single bit
131   const Type *getInt1Ty() {
132     return Type::getInt1Ty(Context);
133   }
134   
135   /// getInt8Ty - Fetch the type representing an 8-bit integer.
136   const Type *getInt8Ty() {
137     return Type::getInt8Ty(Context);
138   }
139   
140   /// getInt16Ty - Fetch the type representing a 16-bit integer.
141   const Type *getInt16Ty() {
142     return Type::getInt16Ty(Context);
143   }
144   
145   /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
146   const Type *getInt32Ty() {
147     return Type::getInt32Ty(Context);
148   }
149   
150   /// getInt64Ty - Fetch the type representing a 64-bit integer.
151   const Type *getInt64Ty() {
152     return Type::getInt64Ty(Context);
153   }
154
155   /// getFloatTy - Fetch the type representing a 32-bit floating point value.
156   const Type *getFloatTy() {
157     return Type::getFloatTy(Context);
158   }
159   
160   /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
161   const Type *getDoubleTy() {
162     return Type::getDoubleTy(Context);
163   }
164   
165   /// getVoidTy - Fetch the type representing void.
166   const Type *getVoidTy() {
167     return Type::getVoidTy(Context);
168   }
169
170   //===--------------------------------------------------------------------===//
171   // Instruction creation methods: Terminators
172   //===--------------------------------------------------------------------===//
173
174   /// CreateRetVoid - Create a 'ret void' instruction.
175   ReturnInst *CreateRetVoid() {
176     return Insert(ReturnInst::Create(Context));
177   }
178
179   /// @verbatim
180   /// CreateRet - Create a 'ret <val>' instruction.
181   /// @endverbatim
182   ReturnInst *CreateRet(Value *V) {
183     return Insert(ReturnInst::Create(Context, V));
184   }
185
186   /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
187   /// with one Value from the retVals array each, that build a aggregate
188   /// return value one value at a time, and a ret instruction to return
189   /// the resulting aggregate value. This is a convenience function for
190   /// code that uses aggregate return values as a vehicle for having
191   /// multiple return values.
192   ///
193   ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
194     const Type *RetType = BB->getParent()->getReturnType();
195     Value *V = UndefValue::get(RetType);
196     for (unsigned i = 0; i != N; ++i)
197       V = CreateInsertValue(V, retVals[i], i, "mrv");
198     return Insert(ReturnInst::Create(Context, V));
199   }
200
201   /// CreateBr - Create an unconditional 'br label X' instruction.
202   BranchInst *CreateBr(BasicBlock *Dest) {
203     return Insert(BranchInst::Create(Dest));
204   }
205
206   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
207   /// instruction.
208   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
209     return Insert(BranchInst::Create(True, False, Cond));
210   }
211
212   /// CreateSwitch - Create a switch instruction with the specified value,
213   /// default dest, and with a hint for the number of cases that will be added
214   /// (for efficient allocation).
215   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
216     return Insert(SwitchInst::Create(V, Dest, NumCases));
217   }
218
219   /// CreateInvoke - Create an invoke instruction.
220   template<typename InputIterator>
221   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
222                            BasicBlock *UnwindDest, InputIterator ArgBegin,
223                            InputIterator ArgEnd, const char *Name = "") {
224     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
225                                      ArgBegin, ArgEnd), Name);
226   }
227
228   UnwindInst *CreateUnwind() {
229     return Insert(new UnwindInst(Context));
230   }
231
232   UnreachableInst *CreateUnreachable() {
233     return Insert(new UnreachableInst(Context));
234   }
235
236   //===--------------------------------------------------------------------===//
237   // Instruction creation methods: Binary Operators
238   //===--------------------------------------------------------------------===//
239
240   Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
241     if (Constant *LC = dyn_cast<Constant>(LHS))
242       if (Constant *RC = dyn_cast<Constant>(RHS))
243         return Folder.CreateAdd(LC, RC);
244     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
245   }
246   Value *CreateNSWAdd(Value *LHS, Value *RHS, const char *Name = "") {
247     if (Constant *LC = dyn_cast<Constant>(LHS))
248       if (Constant *RC = dyn_cast<Constant>(RHS))
249         return Folder.CreateNSWAdd(LC, RC);
250     return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
251   }
252   Value *CreateFAdd(Value *LHS, Value *RHS, const char *Name = "") {
253     if (Constant *LC = dyn_cast<Constant>(LHS))
254       if (Constant *RC = dyn_cast<Constant>(RHS))
255         return Folder.CreateFAdd(LC, RC);
256     return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
257   }
258   Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
259     if (Constant *LC = dyn_cast<Constant>(LHS))
260       if (Constant *RC = dyn_cast<Constant>(RHS))
261         return Folder.CreateSub(LC, RC);
262     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
263   }
264   Value *CreateFSub(Value *LHS, Value *RHS, const char *Name = "") {
265     if (Constant *LC = dyn_cast<Constant>(LHS))
266       if (Constant *RC = dyn_cast<Constant>(RHS))
267         return Folder.CreateFSub(LC, RC);
268     return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
269   }
270   Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
271     if (Constant *LC = dyn_cast<Constant>(LHS))
272       if (Constant *RC = dyn_cast<Constant>(RHS))
273         return Folder.CreateMul(LC, RC);
274     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
275   }
276   Value *CreateFMul(Value *LHS, Value *RHS, const char *Name = "") {
277     if (Constant *LC = dyn_cast<Constant>(LHS))
278       if (Constant *RC = dyn_cast<Constant>(RHS))
279         return Folder.CreateFMul(LC, RC);
280     return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
281   }
282   Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
283     if (Constant *LC = dyn_cast<Constant>(LHS))
284       if (Constant *RC = dyn_cast<Constant>(RHS))
285         return Folder.CreateUDiv(LC, RC);
286     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
287   }
288   Value *CreateSDiv(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.CreateSDiv(LC, RC);
292     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
293   }
294   Value *CreateExactSDiv(Value *LHS, Value *RHS, const char *Name = "") {
295     if (Constant *LC = dyn_cast<Constant>(LHS))
296       if (Constant *RC = dyn_cast<Constant>(RHS))
297         return Folder.CreateExactSDiv(LC, RC);
298     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
299   }
300   Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
301     if (Constant *LC = dyn_cast<Constant>(LHS))
302       if (Constant *RC = dyn_cast<Constant>(RHS))
303         return Folder.CreateFDiv(LC, RC);
304     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
305   }
306   Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
307     if (Constant *LC = dyn_cast<Constant>(LHS))
308       if (Constant *RC = dyn_cast<Constant>(RHS))
309         return Folder.CreateURem(LC, RC);
310     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
311   }
312   Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
313     if (Constant *LC = dyn_cast<Constant>(LHS))
314       if (Constant *RC = dyn_cast<Constant>(RHS))
315         return Folder.CreateSRem(LC, RC);
316     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
317   }
318   Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
319     if (Constant *LC = dyn_cast<Constant>(LHS))
320       if (Constant *RC = dyn_cast<Constant>(RHS))
321         return Folder.CreateFRem(LC, RC);
322     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
323   }
324   Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
325     if (Constant *LC = dyn_cast<Constant>(LHS))
326       if (Constant *RC = dyn_cast<Constant>(RHS))
327         return Folder.CreateShl(LC, RC);
328     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
329   }
330   Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
331     if (Constant *LC = dyn_cast<Constant>(LHS))
332       if (Constant *RC = dyn_cast<Constant>(RHS))
333         return Folder.CreateLShr(LC, RC);
334     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
335   }
336   Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
337     if (Constant *LC = dyn_cast<Constant>(LHS))
338       if (Constant *RC = dyn_cast<Constant>(RHS))
339         return Folder.CreateAShr(LC, RC);
340     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
341   }
342   Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
343     if (Constant *LC = dyn_cast<Constant>(LHS))
344       if (Constant *RC = dyn_cast<Constant>(RHS))
345         return Folder.CreateAnd(LC, RC);
346     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
347   }
348   Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
349     if (Constant *LC = dyn_cast<Constant>(LHS))
350       if (Constant *RC = dyn_cast<Constant>(RHS))
351         return Folder.CreateOr(LC, RC);
352     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
353   }
354   Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
355     if (Constant *LC = dyn_cast<Constant>(LHS))
356       if (Constant *RC = dyn_cast<Constant>(RHS))
357         return Folder.CreateXor(LC, RC);
358     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
359   }
360
361   Value *CreateBinOp(Instruction::BinaryOps Opc,
362                      Value *LHS, Value *RHS, const char *Name = "") {
363     if (Constant *LC = dyn_cast<Constant>(LHS))
364       if (Constant *RC = dyn_cast<Constant>(RHS))
365         return Folder.CreateBinOp(Opc, LC, RC);
366     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
367   }
368
369   Value *CreateNeg(Value *V, const char *Name = "") {
370     if (Constant *VC = dyn_cast<Constant>(V))
371       return Folder.CreateNeg(VC);
372     return Insert(BinaryOperator::CreateNeg(V), Name);
373   }
374   Value *CreateFNeg(Value *V, const char *Name = "") {
375     if (Constant *VC = dyn_cast<Constant>(V))
376       return Folder.CreateFNeg(VC);
377     return Insert(BinaryOperator::CreateFNeg(V), Name);
378   }
379   Value *CreateNot(Value *V, const char *Name = "") {
380     if (Constant *VC = dyn_cast<Constant>(V))
381       return Folder.CreateNot(VC);
382     return Insert(BinaryOperator::CreateNot(V), Name);
383   }
384
385   //===--------------------------------------------------------------------===//
386   // Instruction creation methods: Memory Instructions
387   //===--------------------------------------------------------------------===//
388
389   MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
390                            const char *Name = "") {
391     return Insert(new MallocInst(Ty, ArraySize), Name);
392   }
393   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
394                            const char *Name = "") {
395     return Insert(new AllocaInst(Ty, ArraySize), Name);
396   }
397   FreeInst *CreateFree(Value *Ptr) {
398     return Insert(new FreeInst(Ptr));
399   }
400   LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
401     return Insert(new LoadInst(Ptr), Name);
402   }
403   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
404     return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
405   }
406   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
407     return Insert(new StoreInst(Val, Ptr, isVolatile));
408   }
409   template<typename InputIterator>
410   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
411                    const char *Name = "") {
412     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
413       // Every index must be constant.
414       InputIterator i;
415       for (i = IdxBegin; i < IdxEnd; ++i)
416         if (!isa<Constant>(*i))
417           break;
418       if (i == IdxEnd)
419         return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
420     }
421     return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
422   }
423   template<typename InputIterator>
424   Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
425                            const char *Name = "") {
426     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
427       // Every index must be constant.
428       InputIterator i;
429       for (i = IdxBegin; i < IdxEnd; ++i)
430         if (!isa<Constant>(*i))
431           break;
432       if (i == IdxEnd)
433         return Folder.CreateInBoundsGetElementPtr(PC,
434                                                   &IdxBegin[0],
435                                                   IdxEnd - IdxBegin);
436     }
437     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
438                   Name);
439   }
440   Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
441     if (Constant *PC = dyn_cast<Constant>(Ptr))
442       if (Constant *IC = dyn_cast<Constant>(Idx))
443         return Folder.CreateGetElementPtr(PC, &IC, 1);
444     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
445   }
446   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const char *Name = "") {
447     if (Constant *PC = dyn_cast<Constant>(Ptr))
448       if (Constant *IC = dyn_cast<Constant>(Idx))
449         return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
450     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
451   }
452   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
453     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
454
455     if (Constant *PC = dyn_cast<Constant>(Ptr))
456       return Folder.CreateGetElementPtr(PC, &Idx, 1);
457
458     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
459   }
460   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
461                                     const char *Name = "") {
462     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
463
464     if (Constant *PC = dyn_cast<Constant>(Ptr))
465       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
466
467     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
468   }
469   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, 
470                     const char *Name = "") {
471     Value *Idxs[] = {
472       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
473       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
474     };
475
476     if (Constant *PC = dyn_cast<Constant>(Ptr))
477       return Folder.CreateGetElementPtr(PC, Idxs, 2);
478
479     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
480   }
481   Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
482                                     const char *Name = "") {
483     Value *Idxs[] = {
484       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
485       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
486     };
487
488     if (Constant *PC = dyn_cast<Constant>(Ptr))
489       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
490
491     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
492   }
493   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
494     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
495
496     if (Constant *PC = dyn_cast<Constant>(Ptr))
497       return Folder.CreateGetElementPtr(PC, &Idx, 1);
498
499     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
500   }
501   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
502                                     const char *Name = "") {
503     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
504
505     if (Constant *PC = dyn_cast<Constant>(Ptr))
506       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
507
508     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
509   }
510   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
511                     const char *Name = "") {
512     Value *Idxs[] = {
513       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
514       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
515     };
516
517     if (Constant *PC = dyn_cast<Constant>(Ptr))
518       return Folder.CreateGetElementPtr(PC, Idxs, 2);
519
520     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
521   }
522   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
523                                     const char *Name = "") {
524     Value *Idxs[] = {
525       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
526       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
527     };
528
529     if (Constant *PC = dyn_cast<Constant>(Ptr))
530       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
531
532     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
533   }
534   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
535     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
536   }
537   Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
538     Constant *StrConstant = ConstantArray::get(Context, Str, true);
539     Module &M = *BB->getParent()->getParent();
540     GlobalVariable *gv = new GlobalVariable(M,
541                                             StrConstant->getType(),
542                                             true,
543                                             GlobalValue::InternalLinkage,
544                                             StrConstant,
545                                             "",
546                                             0,
547                                             false);
548     gv->setName(Name);
549     return gv;
550   }
551   Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
552     Value *gv = CreateGlobalString(Str, Name);
553     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
554     Value *Args[] = { zero, zero };
555     return CreateInBoundsGEP(gv, Args, Args+2, Name);
556   }
557   //===--------------------------------------------------------------------===//
558   // Instruction creation methods: Cast/Conversion Operators
559   //===--------------------------------------------------------------------===//
560
561   Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
562     return CreateCast(Instruction::Trunc, V, DestTy, Name);
563   }
564   Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
565     return CreateCast(Instruction::ZExt, V, DestTy, Name);
566   }
567   Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
568     return CreateCast(Instruction::SExt, V, DestTy, Name);
569   }
570   Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
571     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
572   }
573   Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
574     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
575   }
576   Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
577     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
578   }
579   Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
580     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
581   }
582   Value *CreateFPTrunc(Value *V, const Type *DestTy,
583                        const char *Name = "") {
584     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
585   }
586   Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
587     return CreateCast(Instruction::FPExt, V, DestTy, Name);
588   }
589   Value *CreatePtrToInt(Value *V, const Type *DestTy,
590                         const char *Name = "") {
591     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
592   }
593   Value *CreateIntToPtr(Value *V, const Type *DestTy,
594                         const char *Name = "") {
595     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
596   }
597   Value *CreateBitCast(Value *V, const Type *DestTy,
598                        const char *Name = "") {
599     return CreateCast(Instruction::BitCast, V, DestTy, Name);
600   }
601   Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
602                              const char *Name = "") {
603     if (V->getType() == DestTy)
604       return V;
605     if (Constant *VC = dyn_cast<Constant>(V))
606       return Folder.CreateZExtOrBitCast(VC, DestTy);
607     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
608   }
609   Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
610                              const char *Name = "") {
611     if (V->getType() == DestTy)
612       return V;
613     if (Constant *VC = dyn_cast<Constant>(V))
614       return Folder.CreateSExtOrBitCast(VC, DestTy);
615     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
616   }
617   Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
618                               const char *Name = "") {
619     if (V->getType() == DestTy)
620       return V;
621     if (Constant *VC = dyn_cast<Constant>(V))
622       return Folder.CreateTruncOrBitCast(VC, DestTy);
623     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
624   }
625   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
626                     const char *Name = "") {
627     if (V->getType() == DestTy)
628       return V;
629     if (Constant *VC = dyn_cast<Constant>(V))
630       return Folder.CreateCast(Op, VC, DestTy);
631     return Insert(CastInst::Create(Op, V, DestTy), Name);
632   }
633   Value *CreatePointerCast(Value *V, const Type *DestTy,
634                            const char *Name = "") {
635     if (V->getType() == DestTy)
636       return V;
637     if (Constant *VC = dyn_cast<Constant>(V))
638       return Folder.CreatePointerCast(VC, DestTy);
639     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
640   }
641   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
642                        const char *Name = "") {
643     if (V->getType() == DestTy)
644       return V;
645     if (Constant *VC = dyn_cast<Constant>(V))
646       return Folder.CreateIntCast(VC, DestTy, isSigned);
647     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
648   }
649   Value *CreateFPCast(Value *V, const Type *DestTy, const char *Name = "") {
650     if (V->getType() == DestTy)
651       return V;
652     if (Constant *VC = dyn_cast<Constant>(V))
653       return Folder.CreateFPCast(VC, DestTy);
654     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
655   }
656
657   //===--------------------------------------------------------------------===//
658   // Instruction creation methods: Compare Instructions
659   //===--------------------------------------------------------------------===//
660
661   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
662     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
663   }
664   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
665     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
666   }
667   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
668     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
669   }
670   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
671     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
672   }
673   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
674     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
675   }
676   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
677     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
678   }
679   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
680     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
681   }
682   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
683     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
684   }
685   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
686     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
687   }
688   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
689     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
690   }
691
692   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
693     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
694   }
695   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
696     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
697   }
698   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
699     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
700   }
701   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
702     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
703   }
704   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
705     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
706   }
707   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
708     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
709   }
710   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
711     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
712   }
713   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
714     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
715   }
716   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
717     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
718   }
719   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
720     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
721   }
722   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
723     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
724   }
725   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
726     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
727   }
728   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
729     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
730   }
731   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
732     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
733   }
734
735   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
736                     const char *Name = "") {
737     if (Constant *LC = dyn_cast<Constant>(LHS))
738       if (Constant *RC = dyn_cast<Constant>(RHS))
739         return Folder.CreateICmp(P, LC, RC);
740     return Insert(new ICmpInst(Context, P, LHS, RHS), Name);
741   }
742   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
743                     const char *Name = "") {
744     if (Constant *LC = dyn_cast<Constant>(LHS))
745       if (Constant *RC = dyn_cast<Constant>(RHS))
746         return Folder.CreateFCmp(P, LC, RC);
747     return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
748   }
749
750   //===--------------------------------------------------------------------===//
751   // Instruction creation methods: Other Instructions
752   //===--------------------------------------------------------------------===//
753
754   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
755     return Insert(PHINode::Create(Ty), Name);
756   }
757
758   CallInst *CreateCall(Value *Callee, const char *Name = "") {
759     return Insert(CallInst::Create(Callee), Name);
760   }
761   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
762     return Insert(CallInst::Create(Callee, Arg), Name);
763   }
764   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
765                         const char *Name = "") {
766     Value *Args[] = { Arg1, Arg2 };
767     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
768   }
769   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
770                         const char *Name = "") {
771     Value *Args[] = { Arg1, Arg2, Arg3 };
772     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
773   }
774   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
775                         Value *Arg4, const char *Name = "") {
776     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
777     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
778   }
779
780   template<typename InputIterator>
781   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
782                        InputIterator ArgEnd, const char *Name = "") {
783     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
784   }
785
786   Value *CreateSelect(Value *C, Value *True, Value *False,
787                       const char *Name = "") {
788     if (Constant *CC = dyn_cast<Constant>(C))
789       if (Constant *TC = dyn_cast<Constant>(True))
790         if (Constant *FC = dyn_cast<Constant>(False))
791           return Folder.CreateSelect(CC, TC, FC);
792     return Insert(SelectInst::Create(C, True, False), Name);
793   }
794
795   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
796     return Insert(new VAArgInst(List, Ty), Name);
797   }
798
799   Value *CreateExtractElement(Value *Vec, Value *Idx,
800                               const char *Name = "") {
801     if (Constant *VC = dyn_cast<Constant>(Vec))
802       if (Constant *IC = dyn_cast<Constant>(Idx))
803         return Folder.CreateExtractElement(VC, IC);
804     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
805   }
806
807   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
808                              const char *Name = "") {
809     if (Constant *VC = dyn_cast<Constant>(Vec))
810       if (Constant *NC = dyn_cast<Constant>(NewElt))
811         if (Constant *IC = dyn_cast<Constant>(Idx))
812           return Folder.CreateInsertElement(VC, NC, IC);
813     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
814   }
815
816   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
817                              const char *Name = "") {
818     if (Constant *V1C = dyn_cast<Constant>(V1))
819       if (Constant *V2C = dyn_cast<Constant>(V2))
820         if (Constant *MC = dyn_cast<Constant>(Mask))
821           return Folder.CreateShuffleVector(V1C, V2C, MC);
822     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
823   }
824
825   Value *CreateExtractValue(Value *Agg, unsigned Idx,
826                             const char *Name = "") {
827     if (Constant *AggC = dyn_cast<Constant>(Agg))
828       return Folder.CreateExtractValue(AggC, &Idx, 1);
829     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
830   }
831
832   template<typename InputIterator>
833   Value *CreateExtractValue(Value *Agg,
834                             InputIterator IdxBegin,
835                             InputIterator IdxEnd,
836                             const char *Name = "") {
837     if (Constant *AggC = dyn_cast<Constant>(Agg))
838       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
839     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
840   }
841
842   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
843                            const char *Name = "") {
844     if (Constant *AggC = dyn_cast<Constant>(Agg))
845       if (Constant *ValC = dyn_cast<Constant>(Val))
846         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
847     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
848   }
849
850   template<typename InputIterator>
851   Value *CreateInsertValue(Value *Agg, Value *Val,
852                            InputIterator IdxBegin,
853                            InputIterator IdxEnd,
854                            const char *Name = "") {
855     if (Constant *AggC = dyn_cast<Constant>(Agg))
856       if (Constant *ValC = dyn_cast<Constant>(Val))
857         return Folder.CreateInsertValue(AggC, ValC,
858                                             IdxBegin, IdxEnd - IdxBegin);
859     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
860   }
861
862   //===--------------------------------------------------------------------===//
863   // Utility creation methods
864   //===--------------------------------------------------------------------===//
865
866   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
867   Value *CreateIsNull(Value *Arg, const char *Name = "") {
868     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
869                         Name);
870   }
871
872   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
873   Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
874     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
875                         Name);
876   }
877
878   /// CreatePtrDiff - Return the i64 difference between two pointer values,
879   /// dividing out the size of the pointed-to objects.  This is intended to
880   /// implement C-style pointer subtraction. As such, the pointers must be
881   /// appropriately aligned for their element types and pointing into the
882   /// same object.
883   Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
884     assert(LHS->getType() == RHS->getType() &&
885            "Pointer subtraction operand types must match!");
886     const PointerType *ArgType = cast<PointerType>(LHS->getType());
887     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
888     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
889     Value *Difference = CreateSub(LHS_int, RHS_int);
890     return CreateExactSDiv(Difference,
891                            ConstantExpr::getSizeOf(ArgType->getElementType()),
892                            Name);
893   }
894 };
895
896 }
897
898 #endif