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