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