rename getMDKind -> getMDKindID, make it autoinsert if an MD Kind
[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().getMDKindID("dbg");
141     CurDbgLocation = L;
142   }
143
144   MDNode *getCurrentDebugLocation() const { return CurDbgLocation; }
145
146   /// SetDebugLocation -  Set location information for the given instruction.
147   void SetDebugLocation(Instruction *I) {
148     if (CurDbgLocation)
149       Context.getMetadata().addMD(MDKind, CurDbgLocation, I);
150   }
151
152   /// SetDebugLocation -  Set location information for the given instruction.
153   void SetDebugLocation(Instruction *I, MDNode *Loc) {
154     if (MDKind == 0) 
155       MDKind = Context.getMetadata().getMDKindID("dbg");
156     Context.getMetadata().addMD(MDKind, Loc, I);
157   }
158
159   /// Insert - Insert and return the specified instruction.
160   template<typename InstTy>
161   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
162     this->InsertHelper(I, Name, BB, InsertPt);
163     if (CurDbgLocation)
164       Context.getMetadata().addMD(MDKind, CurDbgLocation, I);
165     return I;
166   }
167
168   //===--------------------------------------------------------------------===//
169   // Type creation methods
170   //===--------------------------------------------------------------------===//
171
172   /// getInt1Ty - Fetch the type representing a single bit
173   const Type *getInt1Ty() {
174     return Type::getInt1Ty(Context);
175   }
176   
177   /// getInt8Ty - Fetch the type representing an 8-bit integer.
178   const Type *getInt8Ty() {
179     return Type::getInt8Ty(Context);
180   }
181   
182   /// getInt16Ty - Fetch the type representing a 16-bit integer.
183   const Type *getInt16Ty() {
184     return Type::getInt16Ty(Context);
185   }
186   
187   /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
188   const Type *getInt32Ty() {
189     return Type::getInt32Ty(Context);
190   }
191   
192   /// getInt64Ty - Fetch the type representing a 64-bit integer.
193   const Type *getInt64Ty() {
194     return Type::getInt64Ty(Context);
195   }
196
197   /// getFloatTy - Fetch the type representing a 32-bit floating point value.
198   const Type *getFloatTy() {
199     return Type::getFloatTy(Context);
200   }
201   
202   /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
203   const Type *getDoubleTy() {
204     return Type::getDoubleTy(Context);
205   }
206   
207   /// getVoidTy - Fetch the type representing void.
208   const Type *getVoidTy() {
209     return Type::getVoidTy(Context);
210   }
211
212   //===--------------------------------------------------------------------===//
213   // Instruction creation methods: Terminators
214   //===--------------------------------------------------------------------===//
215
216   /// CreateRetVoid - Create a 'ret void' instruction.
217   ReturnInst *CreateRetVoid() {
218     return Insert(ReturnInst::Create(Context));
219   }
220
221   /// @verbatim
222   /// CreateRet - Create a 'ret <val>' instruction.
223   /// @endverbatim
224   ReturnInst *CreateRet(Value *V) {
225     return Insert(ReturnInst::Create(Context, V));
226   }
227
228   /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
229   /// with one Value from the retVals array each, that build a aggregate
230   /// return value one value at a time, and a ret instruction to return
231   /// the resulting aggregate value. This is a convenience function for
232   /// code that uses aggregate return values as a vehicle for having
233   /// multiple return values.
234   ///
235   ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
236     const Type *RetType = BB->getParent()->getReturnType();
237     Value *V = UndefValue::get(RetType);
238     for (unsigned i = 0; i != N; ++i)
239       V = CreateInsertValue(V, retVals[i], i, "mrv");
240     return Insert(ReturnInst::Create(Context, V));
241   }
242
243   /// CreateBr - Create an unconditional 'br label X' instruction.
244   BranchInst *CreateBr(BasicBlock *Dest) {
245     return Insert(BranchInst::Create(Dest));
246   }
247
248   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
249   /// instruction.
250   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
251     return Insert(BranchInst::Create(True, False, Cond));
252   }
253
254   /// CreateSwitch - Create a switch instruction with the specified value,
255   /// default dest, and with a hint for the number of cases that will be added
256   /// (for efficient allocation).
257   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
258     return Insert(SwitchInst::Create(V, Dest, NumCases));
259   }
260
261   /// CreateIndirectBr - Create an indirect branch instruction with the
262   /// specified address operand, with an optional hint for the number of
263   /// destinations that will be added (for efficient allocation).
264   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
265     return Insert(IndirectBrInst::Create(Addr, NumDests));
266   }
267
268   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
269                            BasicBlock *UnwindDest, const Twine &Name = "") {
270     Value *Args[] = { 0 };
271     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
272                                      Args), Name);
273   }
274   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
275                            BasicBlock *UnwindDest, Value *Arg1,
276                            const Twine &Name = "") {
277     Value *Args[] = { Arg1 };
278     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
279                                      Args+1), Name);
280   }
281   InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
282                             BasicBlock *UnwindDest, Value *Arg1,
283                             Value *Arg2, Value *Arg3,
284                             const Twine &Name = "") {
285     Value *Args[] = { Arg1, Arg2, Arg3 };
286     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
287                                      Args+3), Name);
288   }
289   /// CreateInvoke - Create an invoke instruction.
290   template<typename InputIterator>
291   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
292                            BasicBlock *UnwindDest, InputIterator ArgBegin,
293                            InputIterator ArgEnd, const Twine &Name = "") {
294     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
295                                      ArgBegin, ArgEnd), Name);
296   }
297
298   UnwindInst *CreateUnwind() {
299     return Insert(new UnwindInst(Context));
300   }
301
302   UnreachableInst *CreateUnreachable() {
303     return Insert(new UnreachableInst(Context));
304   }
305
306   //===--------------------------------------------------------------------===//
307   // Instruction creation methods: Binary Operators
308   //===--------------------------------------------------------------------===//
309
310   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
311     if (Constant *LC = dyn_cast<Constant>(LHS))
312       if (Constant *RC = dyn_cast<Constant>(RHS))
313         return Folder.CreateAdd(LC, RC);
314     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
315   }
316   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
317     if (Constant *LC = dyn_cast<Constant>(LHS))
318       if (Constant *RC = dyn_cast<Constant>(RHS))
319         return Folder.CreateNSWAdd(LC, RC);
320     return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
321   }
322   Value *CreateFAdd(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.CreateFAdd(LC, RC);
326     return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
327   }
328   Value *CreateSub(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.CreateSub(LC, RC);
332     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
333   }
334   Value *CreateNSWSub(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.CreateNSWSub(LC, RC);
338     return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
339   }
340   Value *CreateFSub(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.CreateFSub(LC, RC);
344     return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
345   }
346   Value *CreateMul(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.CreateMul(LC, RC);
350     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
351   }
352   Value *CreateNSWMul(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.CreateNSWMul(LC, RC);
356     return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
357   }
358   Value *CreateFMul(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.CreateFMul(LC, RC);
362     return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
363   }
364   Value *CreateUDiv(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.CreateUDiv(LC, RC);
368     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
369   }
370   Value *CreateSDiv(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.CreateSDiv(LC, RC);
374     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
375   }
376   Value *CreateExactSDiv(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.CreateExactSDiv(LC, RC);
380     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
381   }
382   Value *CreateFDiv(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.CreateFDiv(LC, RC);
386     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
387   }
388   Value *CreateURem(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.CreateURem(LC, RC);
392     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
393   }
394   Value *CreateSRem(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.CreateSRem(LC, RC);
398     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
399   }
400   Value *CreateFRem(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.CreateFRem(LC, RC);
404     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
405   }
406   Value *CreateShl(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.CreateShl(LC, RC);
410     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
411   }
412   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
413     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
414     if (Constant *LC = dyn_cast<Constant>(LHS))
415       return Folder.CreateShl(LC, RHSC);
416     return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
417   }
418
419   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
420     if (Constant *LC = dyn_cast<Constant>(LHS))
421       if (Constant *RC = dyn_cast<Constant>(RHS))
422         return Folder.CreateLShr(LC, RC);
423     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
424   }
425   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
426     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
427     if (Constant *LC = dyn_cast<Constant>(LHS))
428       return Folder.CreateLShr(LC, RHSC);
429     return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
430   }
431   
432   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
433     if (Constant *LC = dyn_cast<Constant>(LHS))
434       if (Constant *RC = dyn_cast<Constant>(RHS))
435         return Folder.CreateAShr(LC, RC);
436     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
437   }
438   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
439     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
440     if (Constant *LC = dyn_cast<Constant>(LHS))
441       return Folder.CreateSShr(LC, RHSC);
442     return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
443   }
444
445   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
446     if (Constant *RC = dyn_cast<Constant>(RHS)) {
447       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
448         return LHS;  // LHS & -1 -> LHS
449       if (Constant *LC = dyn_cast<Constant>(LHS))
450         return Folder.CreateAnd(LC, RC);
451     }
452     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
453   }
454   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
455     if (Constant *RC = dyn_cast<Constant>(RHS)) {
456       if (RC->isNullValue())
457         return LHS;  // LHS | 0 -> LHS
458       if (Constant *LC = dyn_cast<Constant>(LHS))
459         return Folder.CreateOr(LC, RC);
460     }
461     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
462   }
463   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
464     if (Constant *LC = dyn_cast<Constant>(LHS))
465       if (Constant *RC = dyn_cast<Constant>(RHS))
466         return Folder.CreateXor(LC, RC);
467     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
468   }
469
470   Value *CreateBinOp(Instruction::BinaryOps Opc,
471                      Value *LHS, Value *RHS, const Twine &Name = "") {
472     if (Constant *LC = dyn_cast<Constant>(LHS))
473       if (Constant *RC = dyn_cast<Constant>(RHS))
474         return Folder.CreateBinOp(Opc, LC, RC);
475     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
476   }
477
478   Value *CreateNeg(Value *V, const Twine &Name = "") {
479     if (Constant *VC = dyn_cast<Constant>(V))
480       return Folder.CreateNeg(VC);
481     return Insert(BinaryOperator::CreateNeg(V), Name);
482   }
483   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
484     if (Constant *VC = dyn_cast<Constant>(V))
485       return Folder.CreateNSWNeg(VC);
486     return Insert(BinaryOperator::CreateNSWNeg(V), Name);
487   }
488   Value *CreateFNeg(Value *V, const Twine &Name = "") {
489     if (Constant *VC = dyn_cast<Constant>(V))
490       return Folder.CreateFNeg(VC);
491     return Insert(BinaryOperator::CreateFNeg(V), Name);
492   }
493   Value *CreateNot(Value *V, const Twine &Name = "") {
494     if (Constant *VC = dyn_cast<Constant>(V))
495       return Folder.CreateNot(VC);
496     return Insert(BinaryOperator::CreateNot(V), Name);
497   }
498
499   //===--------------------------------------------------------------------===//
500   // Instruction creation methods: Memory Instructions
501   //===--------------------------------------------------------------------===//
502
503   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
504                            const Twine &Name = "") {
505     return Insert(new AllocaInst(Ty, ArraySize), Name);
506   }
507   // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
508   // converting the string to 'bool' for the isVolatile parameter.
509   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
510     return Insert(new LoadInst(Ptr), Name);
511   }
512   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
513     return Insert(new LoadInst(Ptr), Name);
514   }
515   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
516     return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
517   }
518   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
519     return Insert(new StoreInst(Val, Ptr, isVolatile));
520   }
521   template<typename InputIterator>
522   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
523                    const Twine &Name = "") {
524     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
525       // Every index must be constant.
526       InputIterator i;
527       for (i = IdxBegin; i < IdxEnd; ++i)
528         if (!isa<Constant>(*i))
529           break;
530       if (i == IdxEnd)
531         return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
532     }
533     return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
534   }
535   template<typename InputIterator>
536   Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
537                            const Twine &Name = "") {
538     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
539       // Every index must be constant.
540       InputIterator i;
541       for (i = IdxBegin; i < IdxEnd; ++i)
542         if (!isa<Constant>(*i))
543           break;
544       if (i == IdxEnd)
545         return Folder.CreateInBoundsGetElementPtr(PC,
546                                                   &IdxBegin[0],
547                                                   IdxEnd - IdxBegin);
548     }
549     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
550                   Name);
551   }
552   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
553     if (Constant *PC = dyn_cast<Constant>(Ptr))
554       if (Constant *IC = dyn_cast<Constant>(Idx))
555         return Folder.CreateGetElementPtr(PC, &IC, 1);
556     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
557   }
558   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
559     if (Constant *PC = dyn_cast<Constant>(Ptr))
560       if (Constant *IC = dyn_cast<Constant>(Idx))
561         return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
562     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
563   }
564   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
565     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
566
567     if (Constant *PC = dyn_cast<Constant>(Ptr))
568       return Folder.CreateGetElementPtr(PC, &Idx, 1);
569
570     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
571   }
572   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
573                                     const Twine &Name = "") {
574     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
575
576     if (Constant *PC = dyn_cast<Constant>(Ptr))
577       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
578
579     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
580   }
581   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, 
582                     const Twine &Name = "") {
583     Value *Idxs[] = {
584       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
585       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
586     };
587
588     if (Constant *PC = dyn_cast<Constant>(Ptr))
589       return Folder.CreateGetElementPtr(PC, Idxs, 2);
590
591     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
592   }
593   Value *CreateConstInBoundsGEP2_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.CreateInBoundsGetElementPtr(PC, Idxs, 2);
602
603     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
604   }
605   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
606     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
607
608     if (Constant *PC = dyn_cast<Constant>(Ptr))
609       return Folder.CreateGetElementPtr(PC, &Idx, 1);
610
611     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
612   }
613   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
614                                     const Twine &Name = "") {
615     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
616
617     if (Constant *PC = dyn_cast<Constant>(Ptr))
618       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
619
620     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
621   }
622   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
623                     const Twine &Name = "") {
624     Value *Idxs[] = {
625       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
626       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
627     };
628
629     if (Constant *PC = dyn_cast<Constant>(Ptr))
630       return Folder.CreateGetElementPtr(PC, Idxs, 2);
631
632     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
633   }
634   Value *CreateConstInBoundsGEP2_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.CreateInBoundsGetElementPtr(PC, Idxs, 2);
643
644     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
645   }
646   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
647     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
648   }
649   Value *CreateGlobalString(const char *Str = "", const Twine &Name = "") {
650     Constant *StrConstant = ConstantArray::get(Context, Str, true);
651     Module &M = *BB->getParent()->getParent();
652     GlobalVariable *gv = new GlobalVariable(M,
653                                             StrConstant->getType(),
654                                             true,
655                                             GlobalValue::InternalLinkage,
656                                             StrConstant,
657                                             "",
658                                             0,
659                                             false);
660     gv->setName(Name);
661     return gv;
662   }
663   Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
664     Value *gv = CreateGlobalString(Str, Name);
665     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
666     Value *Args[] = { zero, zero };
667     return CreateInBoundsGEP(gv, Args, Args+2, Name);
668   }
669   //===--------------------------------------------------------------------===//
670   // Instruction creation methods: Cast/Conversion Operators
671   //===--------------------------------------------------------------------===//
672
673   Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
674     return CreateCast(Instruction::Trunc, V, DestTy, Name);
675   }
676   Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
677     return CreateCast(Instruction::ZExt, V, DestTy, Name);
678   }
679   Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
680     return CreateCast(Instruction::SExt, V, DestTy, Name);
681   }
682   Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
683     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
684   }
685   Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
686     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
687   }
688   Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
689     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
690   }
691   Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
692     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
693   }
694   Value *CreateFPTrunc(Value *V, const Type *DestTy,
695                        const Twine &Name = "") {
696     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
697   }
698   Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
699     return CreateCast(Instruction::FPExt, V, DestTy, Name);
700   }
701   Value *CreatePtrToInt(Value *V, const Type *DestTy,
702                         const Twine &Name = "") {
703     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
704   }
705   Value *CreateIntToPtr(Value *V, const Type *DestTy,
706                         const Twine &Name = "") {
707     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
708   }
709   Value *CreateBitCast(Value *V, const Type *DestTy,
710                        const Twine &Name = "") {
711     return CreateCast(Instruction::BitCast, V, DestTy, Name);
712   }
713   Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
714                              const Twine &Name = "") {
715     if (V->getType() == DestTy)
716       return V;
717     if (Constant *VC = dyn_cast<Constant>(V))
718       return Folder.CreateZExtOrBitCast(VC, DestTy);
719     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
720   }
721   Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
722                              const Twine &Name = "") {
723     if (V->getType() == DestTy)
724       return V;
725     if (Constant *VC = dyn_cast<Constant>(V))
726       return Folder.CreateSExtOrBitCast(VC, DestTy);
727     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
728   }
729   Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
730                               const Twine &Name = "") {
731     if (V->getType() == DestTy)
732       return V;
733     if (Constant *VC = dyn_cast<Constant>(V))
734       return Folder.CreateTruncOrBitCast(VC, DestTy);
735     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
736   }
737   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
738                     const Twine &Name = "") {
739     if (V->getType() == DestTy)
740       return V;
741     if (Constant *VC = dyn_cast<Constant>(V))
742       return Folder.CreateCast(Op, VC, DestTy);
743     return Insert(CastInst::Create(Op, V, DestTy), Name);
744   }
745   Value *CreatePointerCast(Value *V, const Type *DestTy,
746                            const Twine &Name = "") {
747     if (V->getType() == DestTy)
748       return V;
749     if (Constant *VC = dyn_cast<Constant>(V))
750       return Folder.CreatePointerCast(VC, DestTy);
751     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
752   }
753   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
754                        const Twine &Name = "") {
755     if (V->getType() == DestTy)
756       return V;
757     if (Constant *VC = dyn_cast<Constant>(V))
758       return Folder.CreateIntCast(VC, DestTy, isSigned);
759     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
760   }
761 private:
762   // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
763   // error, instead of converting the string to bool for the isSigned parameter.
764   Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
765 public:
766   Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
767     if (V->getType() == DestTy)
768       return V;
769     if (Constant *VC = dyn_cast<Constant>(V))
770       return Folder.CreateFPCast(VC, DestTy);
771     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
772   }
773
774   //===--------------------------------------------------------------------===//
775   // Instruction creation methods: Compare Instructions
776   //===--------------------------------------------------------------------===//
777
778   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
779     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
780   }
781   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
782     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
783   }
784   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
785     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
786   }
787   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
788     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
789   }
790   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
791     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
792   }
793   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
794     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
795   }
796   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
797     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
798   }
799   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
800     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
801   }
802   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
803     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
804   }
805   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
806     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
807   }
808
809   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
810     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
811   }
812   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
813     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
814   }
815   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
816     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
817   }
818   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
819     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
820   }
821   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
822     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
823   }
824   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
825     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
826   }
827   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
828     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
829   }
830   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
831     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
832   }
833   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
834     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
835   }
836   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
837     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
838   }
839   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
840     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
841   }
842   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
843     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
844   }
845   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
846     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
847   }
848   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
849     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
850   }
851
852   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
853                     const Twine &Name = "") {
854     if (Constant *LC = dyn_cast<Constant>(LHS))
855       if (Constant *RC = dyn_cast<Constant>(RHS))
856         return Folder.CreateICmp(P, LC, RC);
857     return Insert(new ICmpInst(P, LHS, RHS), Name);
858   }
859   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
860                     const Twine &Name = "") {
861     if (Constant *LC = dyn_cast<Constant>(LHS))
862       if (Constant *RC = dyn_cast<Constant>(RHS))
863         return Folder.CreateFCmp(P, LC, RC);
864     return Insert(new FCmpInst(P, LHS, RHS), Name);
865   }
866
867   //===--------------------------------------------------------------------===//
868   // Instruction creation methods: Other Instructions
869   //===--------------------------------------------------------------------===//
870
871   PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
872     return Insert(PHINode::Create(Ty), Name);
873   }
874
875   CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
876     return Insert(CallInst::Create(Callee), Name);
877   }
878   CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
879     return Insert(CallInst::Create(Callee, Arg), Name);
880   }
881   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
882                         const Twine &Name = "") {
883     Value *Args[] = { Arg1, Arg2 };
884     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
885   }
886   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
887                         const Twine &Name = "") {
888     Value *Args[] = { Arg1, Arg2, Arg3 };
889     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
890   }
891   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
892                         Value *Arg4, const Twine &Name = "") {
893     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
894     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
895   }
896
897   template<typename InputIterator>
898   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
899                        InputIterator ArgEnd, const Twine &Name = "") {
900     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
901   }
902
903   Value *CreateSelect(Value *C, Value *True, Value *False,
904                       const Twine &Name = "") {
905     if (Constant *CC = dyn_cast<Constant>(C))
906       if (Constant *TC = dyn_cast<Constant>(True))
907         if (Constant *FC = dyn_cast<Constant>(False))
908           return Folder.CreateSelect(CC, TC, FC);
909     return Insert(SelectInst::Create(C, True, False), Name);
910   }
911
912   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
913     return Insert(new VAArgInst(List, Ty), Name);
914   }
915
916   Value *CreateExtractElement(Value *Vec, Value *Idx,
917                               const Twine &Name = "") {
918     if (Constant *VC = dyn_cast<Constant>(Vec))
919       if (Constant *IC = dyn_cast<Constant>(Idx))
920         return Folder.CreateExtractElement(VC, IC);
921     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
922   }
923
924   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
925                              const Twine &Name = "") {
926     if (Constant *VC = dyn_cast<Constant>(Vec))
927       if (Constant *NC = dyn_cast<Constant>(NewElt))
928         if (Constant *IC = dyn_cast<Constant>(Idx))
929           return Folder.CreateInsertElement(VC, NC, IC);
930     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
931   }
932
933   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
934                              const Twine &Name = "") {
935     if (Constant *V1C = dyn_cast<Constant>(V1))
936       if (Constant *V2C = dyn_cast<Constant>(V2))
937         if (Constant *MC = dyn_cast<Constant>(Mask))
938           return Folder.CreateShuffleVector(V1C, V2C, MC);
939     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
940   }
941
942   Value *CreateExtractValue(Value *Agg, unsigned Idx,
943                             const Twine &Name = "") {
944     if (Constant *AggC = dyn_cast<Constant>(Agg))
945       return Folder.CreateExtractValue(AggC, &Idx, 1);
946     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
947   }
948
949   template<typename InputIterator>
950   Value *CreateExtractValue(Value *Agg,
951                             InputIterator IdxBegin,
952                             InputIterator IdxEnd,
953                             const Twine &Name = "") {
954     if (Constant *AggC = dyn_cast<Constant>(Agg))
955       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
956     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
957   }
958
959   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
960                            const Twine &Name = "") {
961     if (Constant *AggC = dyn_cast<Constant>(Agg))
962       if (Constant *ValC = dyn_cast<Constant>(Val))
963         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
964     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
965   }
966
967   template<typename InputIterator>
968   Value *CreateInsertValue(Value *Agg, Value *Val,
969                            InputIterator IdxBegin,
970                            InputIterator IdxEnd,
971                            const Twine &Name = "") {
972     if (Constant *AggC = dyn_cast<Constant>(Agg))
973       if (Constant *ValC = dyn_cast<Constant>(Val))
974         return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
975     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
976   }
977
978   //===--------------------------------------------------------------------===//
979   // Utility creation methods
980   //===--------------------------------------------------------------------===//
981
982   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
983   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
984     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
985                         Name);
986   }
987
988   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
989   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
990     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
991                         Name);
992   }
993
994   /// CreatePtrDiff - Return the i64 difference between two pointer values,
995   /// dividing out the size of the pointed-to objects.  This is intended to
996   /// implement C-style pointer subtraction. As such, the pointers must be
997   /// appropriately aligned for their element types and pointing into the
998   /// same object.
999   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1000     assert(LHS->getType() == RHS->getType() &&
1001            "Pointer subtraction operand types must match!");
1002     const PointerType *ArgType = cast<PointerType>(LHS->getType());
1003     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1004     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1005     Value *Difference = CreateSub(LHS_int, RHS_int);
1006     return CreateExactSDiv(Difference,
1007                            ConstantExpr::getSizeOf(ArgType->getElementType()),
1008                            Name);
1009   }
1010 };
1011
1012 }
1013
1014 #endif