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