add zextOrTrunc and sextOrTrunc methods, that are similar to the ones in APInt
[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   /// CreateIndirectBr - Create an indirect branch instruction with the
257   /// specified address operand, with an optional hint for the number of
258   /// destinations that will be added (for efficient allocation).
259   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
260     return Insert(IndirectBrInst::Create(Addr, NumDests));
261   }
262
263   /// CreateInvoke - Create an invoke instruction.
264   template<typename InputIterator>
265   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
266                            BasicBlock *UnwindDest, InputIterator ArgBegin,
267                            InputIterator ArgEnd, const Twine &Name = "") {
268     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
269                                      ArgBegin, ArgEnd), Name);
270   }
271
272   UnwindInst *CreateUnwind() {
273     return Insert(new UnwindInst(Context));
274   }
275
276   UnreachableInst *CreateUnreachable() {
277     return Insert(new UnreachableInst(Context));
278   }
279
280   //===--------------------------------------------------------------------===//
281   // Instruction creation methods: Binary Operators
282   //===--------------------------------------------------------------------===//
283
284   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
285     if (Constant *LC = dyn_cast<Constant>(LHS))
286       if (Constant *RC = dyn_cast<Constant>(RHS))
287         return Folder.CreateAdd(LC, RC);
288     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
289   }
290   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
291     if (Constant *LC = dyn_cast<Constant>(LHS))
292       if (Constant *RC = dyn_cast<Constant>(RHS))
293         return Folder.CreateNSWAdd(LC, RC);
294     return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
295   }
296   Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
297     if (Constant *LC = dyn_cast<Constant>(LHS))
298       if (Constant *RC = dyn_cast<Constant>(RHS))
299         return Folder.CreateFAdd(LC, RC);
300     return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
301   }
302   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
303     if (Constant *LC = dyn_cast<Constant>(LHS))
304       if (Constant *RC = dyn_cast<Constant>(RHS))
305         return Folder.CreateSub(LC, RC);
306     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
307   }
308   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
309     if (Constant *LC = dyn_cast<Constant>(LHS))
310       if (Constant *RC = dyn_cast<Constant>(RHS))
311         return Folder.CreateNSWSub(LC, RC);
312     return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
313   }
314   Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
315     if (Constant *LC = dyn_cast<Constant>(LHS))
316       if (Constant *RC = dyn_cast<Constant>(RHS))
317         return Folder.CreateFSub(LC, RC);
318     return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
319   }
320   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
321     if (Constant *LC = dyn_cast<Constant>(LHS))
322       if (Constant *RC = dyn_cast<Constant>(RHS))
323         return Folder.CreateMul(LC, RC);
324     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
325   }
326   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
327     if (Constant *LC = dyn_cast<Constant>(LHS))
328       if (Constant *RC = dyn_cast<Constant>(RHS))
329         return Folder.CreateFMul(LC, RC);
330     return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
331   }
332   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
333     if (Constant *LC = dyn_cast<Constant>(LHS))
334       if (Constant *RC = dyn_cast<Constant>(RHS))
335         return Folder.CreateUDiv(LC, RC);
336     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
337   }
338   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
339     if (Constant *LC = dyn_cast<Constant>(LHS))
340       if (Constant *RC = dyn_cast<Constant>(RHS))
341         return Folder.CreateSDiv(LC, RC);
342     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
343   }
344   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
345     if (Constant *LC = dyn_cast<Constant>(LHS))
346       if (Constant *RC = dyn_cast<Constant>(RHS))
347         return Folder.CreateExactSDiv(LC, RC);
348     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
349   }
350   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
351     if (Constant *LC = dyn_cast<Constant>(LHS))
352       if (Constant *RC = dyn_cast<Constant>(RHS))
353         return Folder.CreateFDiv(LC, RC);
354     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
355   }
356   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
357     if (Constant *LC = dyn_cast<Constant>(LHS))
358       if (Constant *RC = dyn_cast<Constant>(RHS))
359         return Folder.CreateURem(LC, RC);
360     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
361   }
362   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
363     if (Constant *LC = dyn_cast<Constant>(LHS))
364       if (Constant *RC = dyn_cast<Constant>(RHS))
365         return Folder.CreateSRem(LC, RC);
366     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
367   }
368   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
369     if (Constant *LC = dyn_cast<Constant>(LHS))
370       if (Constant *RC = dyn_cast<Constant>(RHS))
371         return Folder.CreateFRem(LC, RC);
372     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
373   }
374   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
375     if (Constant *LC = dyn_cast<Constant>(LHS))
376       if (Constant *RC = dyn_cast<Constant>(RHS))
377         return Folder.CreateShl(LC, RC);
378     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
379   }
380   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
381     if (Constant *LC = dyn_cast<Constant>(LHS))
382       if (Constant *RC = dyn_cast<Constant>(RHS))
383         return Folder.CreateLShr(LC, RC);
384     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
385   }
386   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
387     if (Constant *LC = dyn_cast<Constant>(LHS))
388       if (Constant *RC = dyn_cast<Constant>(RHS))
389         return Folder.CreateAShr(LC, RC);
390     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
391   }
392   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
393     if (Constant *RC = dyn_cast<Constant>(RHS)) {
394       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
395         return LHS;  // LHS & -1 -> LHS
396       if (Constant *LC = dyn_cast<Constant>(LHS))
397         return Folder.CreateAnd(LC, RC);
398     }
399     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
400   }
401   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
402     if (Constant *RC = dyn_cast<Constant>(RHS)) {
403       if (RC->isNullValue())
404         return LHS;  // LHS | 0 -> LHS
405       if (Constant *LC = dyn_cast<Constant>(LHS))
406         return Folder.CreateOr(LC, RC);
407     }
408     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
409   }
410   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
411     if (Constant *LC = dyn_cast<Constant>(LHS))
412       if (Constant *RC = dyn_cast<Constant>(RHS))
413         return Folder.CreateXor(LC, RC);
414     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
415   }
416
417   Value *CreateBinOp(Instruction::BinaryOps Opc,
418                      Value *LHS, Value *RHS, const Twine &Name = "") {
419     if (Constant *LC = dyn_cast<Constant>(LHS))
420       if (Constant *RC = dyn_cast<Constant>(RHS))
421         return Folder.CreateBinOp(Opc, LC, RC);
422     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
423   }
424
425   Value *CreateNeg(Value *V, const Twine &Name = "") {
426     if (Constant *VC = dyn_cast<Constant>(V))
427       return Folder.CreateNeg(VC);
428     return Insert(BinaryOperator::CreateNeg(V), Name);
429   }
430   Value *CreateFNeg(Value *V, const Twine &Name = "") {
431     if (Constant *VC = dyn_cast<Constant>(V))
432       return Folder.CreateFNeg(VC);
433     return Insert(BinaryOperator::CreateFNeg(V), Name);
434   }
435   Value *CreateNot(Value *V, const Twine &Name = "") {
436     if (Constant *VC = dyn_cast<Constant>(V))
437       return Folder.CreateNot(VC);
438     return Insert(BinaryOperator::CreateNot(V), Name);
439   }
440
441   //===--------------------------------------------------------------------===//
442   // Instruction creation methods: Memory Instructions
443   //===--------------------------------------------------------------------===//
444
445   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
446                            const Twine &Name = "") {
447     return Insert(new AllocaInst(Ty, ArraySize), Name);
448   }
449   // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
450   // converting the string to 'bool' for the isVolatile parameter.
451   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
452     return Insert(new LoadInst(Ptr), Name);
453   }
454   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
455     return Insert(new LoadInst(Ptr), Name);
456   }
457   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
458     return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
459   }
460   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
461     return Insert(new StoreInst(Val, Ptr, isVolatile));
462   }
463   template<typename InputIterator>
464   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
465                    const Twine &Name = "") {
466     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
467       // Every index must be constant.
468       InputIterator i;
469       for (i = IdxBegin; i < IdxEnd; ++i)
470         if (!isa<Constant>(*i))
471           break;
472       if (i == IdxEnd)
473         return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
474     }
475     return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
476   }
477   template<typename InputIterator>
478   Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
479                            const Twine &Name = "") {
480     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
481       // Every index must be constant.
482       InputIterator i;
483       for (i = IdxBegin; i < IdxEnd; ++i)
484         if (!isa<Constant>(*i))
485           break;
486       if (i == IdxEnd)
487         return Folder.CreateInBoundsGetElementPtr(PC,
488                                                   &IdxBegin[0],
489                                                   IdxEnd - IdxBegin);
490     }
491     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
492                   Name);
493   }
494   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
495     if (Constant *PC = dyn_cast<Constant>(Ptr))
496       if (Constant *IC = dyn_cast<Constant>(Idx))
497         return Folder.CreateGetElementPtr(PC, &IC, 1);
498     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
499   }
500   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
501     if (Constant *PC = dyn_cast<Constant>(Ptr))
502       if (Constant *IC = dyn_cast<Constant>(Idx))
503         return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
504     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
505   }
506   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
507     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
508
509     if (Constant *PC = dyn_cast<Constant>(Ptr))
510       return Folder.CreateGetElementPtr(PC, &Idx, 1);
511
512     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
513   }
514   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
515                                     const Twine &Name = "") {
516     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
517
518     if (Constant *PC = dyn_cast<Constant>(Ptr))
519       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
520
521     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
522   }
523   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, 
524                     const Twine &Name = "") {
525     Value *Idxs[] = {
526       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
527       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
528     };
529
530     if (Constant *PC = dyn_cast<Constant>(Ptr))
531       return Folder.CreateGetElementPtr(PC, Idxs, 2);
532
533     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
534   }
535   Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
536                                     const Twine &Name = "") {
537     Value *Idxs[] = {
538       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
539       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
540     };
541
542     if (Constant *PC = dyn_cast<Constant>(Ptr))
543       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
544
545     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
546   }
547   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
548     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
549
550     if (Constant *PC = dyn_cast<Constant>(Ptr))
551       return Folder.CreateGetElementPtr(PC, &Idx, 1);
552
553     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
554   }
555   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
556                                     const Twine &Name = "") {
557     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
558
559     if (Constant *PC = dyn_cast<Constant>(Ptr))
560       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
561
562     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
563   }
564   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
565                     const Twine &Name = "") {
566     Value *Idxs[] = {
567       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
568       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
569     };
570
571     if (Constant *PC = dyn_cast<Constant>(Ptr))
572       return Folder.CreateGetElementPtr(PC, Idxs, 2);
573
574     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
575   }
576   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
577                                     const Twine &Name = "") {
578     Value *Idxs[] = {
579       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
580       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
581     };
582
583     if (Constant *PC = dyn_cast<Constant>(Ptr))
584       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
585
586     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
587   }
588   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
589     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
590   }
591   Value *CreateGlobalString(const char *Str = "", const Twine &Name = "") {
592     Constant *StrConstant = ConstantArray::get(Context, Str, true);
593     Module &M = *BB->getParent()->getParent();
594     GlobalVariable *gv = new GlobalVariable(M,
595                                             StrConstant->getType(),
596                                             true,
597                                             GlobalValue::InternalLinkage,
598                                             StrConstant,
599                                             "",
600                                             0,
601                                             false);
602     gv->setName(Name);
603     return gv;
604   }
605   Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
606     Value *gv = CreateGlobalString(Str, Name);
607     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
608     Value *Args[] = { zero, zero };
609     return CreateInBoundsGEP(gv, Args, Args+2, Name);
610   }
611   //===--------------------------------------------------------------------===//
612   // Instruction creation methods: Cast/Conversion Operators
613   //===--------------------------------------------------------------------===//
614
615   Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
616     return CreateCast(Instruction::Trunc, V, DestTy, Name);
617   }
618   Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
619     return CreateCast(Instruction::ZExt, V, DestTy, Name);
620   }
621   Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
622     return CreateCast(Instruction::SExt, V, DestTy, Name);
623   }
624   Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
625     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
626   }
627   Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
628     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
629   }
630   Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
631     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
632   }
633   Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
634     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
635   }
636   Value *CreateFPTrunc(Value *V, const Type *DestTy,
637                        const Twine &Name = "") {
638     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
639   }
640   Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
641     return CreateCast(Instruction::FPExt, V, DestTy, Name);
642   }
643   Value *CreatePtrToInt(Value *V, const Type *DestTy,
644                         const Twine &Name = "") {
645     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
646   }
647   Value *CreateIntToPtr(Value *V, const Type *DestTy,
648                         const Twine &Name = "") {
649     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
650   }
651   Value *CreateBitCast(Value *V, const Type *DestTy,
652                        const Twine &Name = "") {
653     return CreateCast(Instruction::BitCast, V, DestTy, Name);
654   }
655   Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
656                              const Twine &Name = "") {
657     if (V->getType() == DestTy)
658       return V;
659     if (Constant *VC = dyn_cast<Constant>(V))
660       return Folder.CreateZExtOrBitCast(VC, DestTy);
661     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
662   }
663   Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
664                              const Twine &Name = "") {
665     if (V->getType() == DestTy)
666       return V;
667     if (Constant *VC = dyn_cast<Constant>(V))
668       return Folder.CreateSExtOrBitCast(VC, DestTy);
669     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
670   }
671   Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
672                               const Twine &Name = "") {
673     if (V->getType() == DestTy)
674       return V;
675     if (Constant *VC = dyn_cast<Constant>(V))
676       return Folder.CreateTruncOrBitCast(VC, DestTy);
677     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
678   }
679   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
680                     const Twine &Name = "") {
681     if (V->getType() == DestTy)
682       return V;
683     if (Constant *VC = dyn_cast<Constant>(V))
684       return Folder.CreateCast(Op, VC, DestTy);
685     return Insert(CastInst::Create(Op, V, DestTy), Name);
686   }
687   Value *CreatePointerCast(Value *V, const Type *DestTy,
688                            const Twine &Name = "") {
689     if (V->getType() == DestTy)
690       return V;
691     if (Constant *VC = dyn_cast<Constant>(V))
692       return Folder.CreatePointerCast(VC, DestTy);
693     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
694   }
695   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
696                        const Twine &Name = "") {
697     if (V->getType() == DestTy)
698       return V;
699     if (Constant *VC = dyn_cast<Constant>(V))
700       return Folder.CreateIntCast(VC, DestTy, isSigned);
701     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
702   }
703   Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
704     if (V->getType() == DestTy)
705       return V;
706     if (Constant *VC = dyn_cast<Constant>(V))
707       return Folder.CreateFPCast(VC, DestTy);
708     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
709   }
710
711   //===--------------------------------------------------------------------===//
712   // Instruction creation methods: Compare Instructions
713   //===--------------------------------------------------------------------===//
714
715   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
716     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
717   }
718   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
719     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
720   }
721   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
722     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
723   }
724   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
725     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
726   }
727   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
728     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
729   }
730   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
731     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
732   }
733   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
734     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
735   }
736   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
737     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
738   }
739   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
740     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
741   }
742   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
743     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
744   }
745
746   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
747     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
748   }
749   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
750     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
751   }
752   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
753     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
754   }
755   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
756     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
757   }
758   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
759     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
760   }
761   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
762     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
763   }
764   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
765     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
766   }
767   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
768     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
769   }
770   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
771     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
772   }
773   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
774     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
775   }
776   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
777     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
778   }
779   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
780     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
781   }
782   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
783     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
784   }
785   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
786     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
787   }
788
789   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
790                     const Twine &Name = "") {
791     if (Constant *LC = dyn_cast<Constant>(LHS))
792       if (Constant *RC = dyn_cast<Constant>(RHS))
793         return Folder.CreateICmp(P, LC, RC);
794     return Insert(new ICmpInst(P, LHS, RHS), Name);
795   }
796   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
797                     const Twine &Name = "") {
798     if (Constant *LC = dyn_cast<Constant>(LHS))
799       if (Constant *RC = dyn_cast<Constant>(RHS))
800         return Folder.CreateFCmp(P, LC, RC);
801     return Insert(new FCmpInst(P, LHS, RHS), Name);
802   }
803
804   //===--------------------------------------------------------------------===//
805   // Instruction creation methods: Other Instructions
806   //===--------------------------------------------------------------------===//
807
808   PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
809     return Insert(PHINode::Create(Ty), Name);
810   }
811
812   CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
813     return Insert(CallInst::Create(Callee), Name);
814   }
815   CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
816     return Insert(CallInst::Create(Callee, Arg), Name);
817   }
818   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
819                         const Twine &Name = "") {
820     Value *Args[] = { Arg1, Arg2 };
821     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
822   }
823   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
824                         const Twine &Name = "") {
825     Value *Args[] = { Arg1, Arg2, Arg3 };
826     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
827   }
828   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
829                         Value *Arg4, const Twine &Name = "") {
830     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
831     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
832   }
833
834   template<typename InputIterator>
835   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
836                        InputIterator ArgEnd, const Twine &Name = "") {
837     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
838   }
839
840   Value *CreateSelect(Value *C, Value *True, Value *False,
841                       const Twine &Name = "") {
842     if (Constant *CC = dyn_cast<Constant>(C))
843       if (Constant *TC = dyn_cast<Constant>(True))
844         if (Constant *FC = dyn_cast<Constant>(False))
845           return Folder.CreateSelect(CC, TC, FC);
846     return Insert(SelectInst::Create(C, True, False), Name);
847   }
848
849   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
850     return Insert(new VAArgInst(List, Ty), Name);
851   }
852
853   Value *CreateExtractElement(Value *Vec, Value *Idx,
854                               const Twine &Name = "") {
855     if (Constant *VC = dyn_cast<Constant>(Vec))
856       if (Constant *IC = dyn_cast<Constant>(Idx))
857         return Folder.CreateExtractElement(VC, IC);
858     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
859   }
860
861   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
862                              const Twine &Name = "") {
863     if (Constant *VC = dyn_cast<Constant>(Vec))
864       if (Constant *NC = dyn_cast<Constant>(NewElt))
865         if (Constant *IC = dyn_cast<Constant>(Idx))
866           return Folder.CreateInsertElement(VC, NC, IC);
867     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
868   }
869
870   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
871                              const Twine &Name = "") {
872     if (Constant *V1C = dyn_cast<Constant>(V1))
873       if (Constant *V2C = dyn_cast<Constant>(V2))
874         if (Constant *MC = dyn_cast<Constant>(Mask))
875           return Folder.CreateShuffleVector(V1C, V2C, MC);
876     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
877   }
878
879   Value *CreateExtractValue(Value *Agg, unsigned Idx,
880                             const Twine &Name = "") {
881     if (Constant *AggC = dyn_cast<Constant>(Agg))
882       return Folder.CreateExtractValue(AggC, &Idx, 1);
883     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
884   }
885
886   template<typename InputIterator>
887   Value *CreateExtractValue(Value *Agg,
888                             InputIterator IdxBegin,
889                             InputIterator IdxEnd,
890                             const Twine &Name = "") {
891     if (Constant *AggC = dyn_cast<Constant>(Agg))
892       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
893     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
894   }
895
896   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
897                            const Twine &Name = "") {
898     if (Constant *AggC = dyn_cast<Constant>(Agg))
899       if (Constant *ValC = dyn_cast<Constant>(Val))
900         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
901     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
902   }
903
904   template<typename InputIterator>
905   Value *CreateInsertValue(Value *Agg, Value *Val,
906                            InputIterator IdxBegin,
907                            InputIterator IdxEnd,
908                            const Twine &Name = "") {
909     if (Constant *AggC = dyn_cast<Constant>(Agg))
910       if (Constant *ValC = dyn_cast<Constant>(Val))
911         return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
912     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
913   }
914
915   //===--------------------------------------------------------------------===//
916   // Utility creation methods
917   //===--------------------------------------------------------------------===//
918
919   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
920   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
921     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
922                         Name);
923   }
924
925   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
926   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
927     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
928                         Name);
929   }
930
931   /// CreatePtrDiff - Return the i64 difference between two pointer values,
932   /// dividing out the size of the pointed-to objects.  This is intended to
933   /// implement C-style pointer subtraction. As such, the pointers must be
934   /// appropriately aligned for their element types and pointing into the
935   /// same object.
936   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
937     assert(LHS->getType() == RHS->getType() &&
938            "Pointer subtraction operand types must match!");
939     const PointerType *ArgType = cast<PointerType>(LHS->getType());
940     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
941     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
942     Value *Difference = CreateSub(LHS_int, RHS_int);
943     return CreateExactSDiv(Difference,
944                            ConstantExpr::getSizeOf(ArgType->getElementType()),
945                            Name);
946   }
947 };
948
949 }
950
951 #endif