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