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