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