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