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