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