Add doxygen comments.
[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
602   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
603                     const char *Name = "") {
604     if (V->getType() == DestTy)
605       return V;
606     if (Constant *VC = dyn_cast<Constant>(V))
607       return Folder.CreateCast(Op, VC, DestTy);
608     return Insert(CastInst::Create(Op, V, DestTy), Name);
609   }
610   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
611                        const char *Name = "") {
612     if (V->getType() == DestTy)
613       return V;
614     if (Constant *VC = dyn_cast<Constant>(V))
615       return Folder.CreateIntCast(VC, DestTy, isSigned);
616     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
617   }
618
619   //===--------------------------------------------------------------------===//
620   // Instruction creation methods: Compare Instructions
621   //===--------------------------------------------------------------------===//
622
623   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
624     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
625   }
626   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
627     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
628   }
629   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
630     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
631   }
632   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
633     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
634   }
635   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
636     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
637   }
638   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
639     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
640   }
641   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
642     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
643   }
644   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
645     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
646   }
647   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
648     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
649   }
650   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
651     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
652   }
653
654   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
655     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
656   }
657   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
658     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
659   }
660   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
661     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
662   }
663   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
664     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
665   }
666   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
667     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
668   }
669   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
670     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
671   }
672   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
673     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
674   }
675   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
676     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
677   }
678   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
679     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
680   }
681   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
682     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
683   }
684   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
685     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
686   }
687   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
688     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
689   }
690   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
691     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
692   }
693   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
694     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
695   }
696
697   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
698                     const char *Name = "") {
699     if (Constant *LC = dyn_cast<Constant>(LHS))
700       if (Constant *RC = dyn_cast<Constant>(RHS))
701         return Folder.CreateICmp(P, LC, RC);
702     return Insert(new ICmpInst(Context, P, LHS, RHS), Name);
703   }
704   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
705                     const char *Name = "") {
706     if (Constant *LC = dyn_cast<Constant>(LHS))
707       if (Constant *RC = dyn_cast<Constant>(RHS))
708         return Folder.CreateFCmp(P, LC, RC);
709     return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
710   }
711
712   //===--------------------------------------------------------------------===//
713   // Instruction creation methods: Other Instructions
714   //===--------------------------------------------------------------------===//
715
716   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
717     return Insert(PHINode::Create(Ty), Name);
718   }
719
720   CallInst *CreateCall(Value *Callee, const char *Name = "") {
721     return Insert(CallInst::Create(Callee), Name);
722   }
723   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
724     return Insert(CallInst::Create(Callee, Arg), Name);
725   }
726   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
727                         const char *Name = "") {
728     Value *Args[] = { Arg1, Arg2 };
729     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
730   }
731   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
732                         const char *Name = "") {
733     Value *Args[] = { Arg1, Arg2, Arg3 };
734     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
735   }
736   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
737                         Value *Arg4, const char *Name = "") {
738     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
739     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
740   }
741
742   template<typename InputIterator>
743   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
744                        InputIterator ArgEnd, const char *Name = "") {
745     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
746   }
747
748   Value *CreateSelect(Value *C, Value *True, Value *False,
749                       const char *Name = "") {
750     if (Constant *CC = dyn_cast<Constant>(C))
751       if (Constant *TC = dyn_cast<Constant>(True))
752         if (Constant *FC = dyn_cast<Constant>(False))
753           return Folder.CreateSelect(CC, TC, FC);
754     return Insert(SelectInst::Create(C, True, False), Name);
755   }
756
757   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
758     return Insert(new VAArgInst(List, Ty), Name);
759   }
760
761   Value *CreateExtractElement(Value *Vec, Value *Idx,
762                               const char *Name = "") {
763     if (Constant *VC = dyn_cast<Constant>(Vec))
764       if (Constant *IC = dyn_cast<Constant>(Idx))
765         return Folder.CreateExtractElement(VC, IC);
766     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
767   }
768
769   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
770                              const char *Name = "") {
771     if (Constant *VC = dyn_cast<Constant>(Vec))
772       if (Constant *NC = dyn_cast<Constant>(NewElt))
773         if (Constant *IC = dyn_cast<Constant>(Idx))
774           return Folder.CreateInsertElement(VC, NC, IC);
775     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
776   }
777
778   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
779                              const char *Name = "") {
780     if (Constant *V1C = dyn_cast<Constant>(V1))
781       if (Constant *V2C = dyn_cast<Constant>(V2))
782         if (Constant *MC = dyn_cast<Constant>(Mask))
783           return Folder.CreateShuffleVector(V1C, V2C, MC);
784     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
785   }
786
787   Value *CreateExtractValue(Value *Agg, unsigned Idx,
788                             const char *Name = "") {
789     if (Constant *AggC = dyn_cast<Constant>(Agg))
790       return Folder.CreateExtractValue(AggC, &Idx, 1);
791     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
792   }
793
794   template<typename InputIterator>
795   Value *CreateExtractValue(Value *Agg,
796                             InputIterator IdxBegin,
797                             InputIterator IdxEnd,
798                             const char *Name = "") {
799     if (Constant *AggC = dyn_cast<Constant>(Agg))
800       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
801     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
802   }
803
804   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
805                            const char *Name = "") {
806     if (Constant *AggC = dyn_cast<Constant>(Agg))
807       if (Constant *ValC = dyn_cast<Constant>(Val))
808         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
809     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
810   }
811
812   template<typename InputIterator>
813   Value *CreateInsertValue(Value *Agg, Value *Val,
814                            InputIterator IdxBegin,
815                            InputIterator IdxEnd,
816                            const char *Name = "") {
817     if (Constant *AggC = dyn_cast<Constant>(Agg))
818       if (Constant *ValC = dyn_cast<Constant>(Val))
819         return Folder.CreateInsertValue(AggC, ValC,
820                                             IdxBegin, IdxEnd - IdxBegin);
821     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
822   }
823
824   //===--------------------------------------------------------------------===//
825   // Utility creation methods
826   //===--------------------------------------------------------------------===//
827
828   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
829   Value *CreateIsNull(Value *Arg, const char *Name = "") {
830     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
831                         Name);
832   }
833
834   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
835   Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
836     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
837                         Name);
838   }
839
840   /// CreatePtrDiff - Return the i64 difference between two pointer values,
841   /// dividing out the size of the pointed-to objects.  This is intended to
842   /// implement C-style pointer subtraction. As such, the pointers must be
843   /// appropriately aligned for their element types and pointing into the
844   /// same object.
845   Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
846     assert(LHS->getType() == RHS->getType() &&
847            "Pointer subtraction operand types must match!");
848     const PointerType *ArgType = cast<PointerType>(LHS->getType());
849     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
850     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
851     Value *Difference = CreateSub(LHS_int, RHS_int);
852     return CreateExactSDiv(Difference,
853                            ConstantExpr::getSizeOf(ArgType->getElementType()),
854                            Name);
855   }
856 };
857
858 }
859
860 #endif