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