eliminate the old InstFormatName which is always "AsmString",
[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 RandomAccessIterator>
379   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
380                            BasicBlock *UnwindDest,
381                            RandomAccessIterator ArgBegin,
382                            RandomAccessIterator ArgEnd,
383                            const Twine &Name = "") {
384     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
385                                      ArgBegin, ArgEnd), Name);
386   }
387
388   UnwindInst *CreateUnwind() {
389     return Insert(new UnwindInst(Context));
390   }
391
392   UnreachableInst *CreateUnreachable() {
393     return Insert(new UnreachableInst(Context));
394   }
395
396   //===--------------------------------------------------------------------===//
397   // Instruction creation methods: Binary Operators
398   //===--------------------------------------------------------------------===//
399
400   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
401     if (Constant *LC = dyn_cast<Constant>(LHS))
402       if (Constant *RC = dyn_cast<Constant>(RHS))
403         return Folder.CreateAdd(LC, RC);
404     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
405   }
406   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
407     if (Constant *LC = dyn_cast<Constant>(LHS))
408       if (Constant *RC = dyn_cast<Constant>(RHS))
409         return Folder.CreateNSWAdd(LC, RC);
410     return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
411   }
412   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
413     if (Constant *LC = dyn_cast<Constant>(LHS))
414       if (Constant *RC = dyn_cast<Constant>(RHS))
415         return Folder.CreateNUWAdd(LC, RC);
416     return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
417   }
418   Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
419     if (Constant *LC = dyn_cast<Constant>(LHS))
420       if (Constant *RC = dyn_cast<Constant>(RHS))
421         return Folder.CreateFAdd(LC, RC);
422     return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
423   }
424   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
425     if (Constant *LC = dyn_cast<Constant>(LHS))
426       if (Constant *RC = dyn_cast<Constant>(RHS))
427         return Folder.CreateSub(LC, RC);
428     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
429   }
430   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
431     if (Constant *LC = dyn_cast<Constant>(LHS))
432       if (Constant *RC = dyn_cast<Constant>(RHS))
433         return Folder.CreateNSWSub(LC, RC);
434     return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
435   }
436   Value *CreateNUWSub(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.CreateNUWSub(LC, RC);
440     return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
441   }
442   Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
443     if (Constant *LC = dyn_cast<Constant>(LHS))
444       if (Constant *RC = dyn_cast<Constant>(RHS))
445         return Folder.CreateFSub(LC, RC);
446     return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
447   }
448   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
449     if (Constant *LC = dyn_cast<Constant>(LHS))
450       if (Constant *RC = dyn_cast<Constant>(RHS))
451         return Folder.CreateMul(LC, RC);
452     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
453   }
454   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
455     if (Constant *LC = dyn_cast<Constant>(LHS))
456       if (Constant *RC = dyn_cast<Constant>(RHS))
457         return Folder.CreateNSWMul(LC, RC);
458     return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
459   }
460   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
461     if (Constant *LC = dyn_cast<Constant>(LHS))
462       if (Constant *RC = dyn_cast<Constant>(RHS))
463         return Folder.CreateNUWMul(LC, RC);
464     return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
465   }
466   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
467     if (Constant *LC = dyn_cast<Constant>(LHS))
468       if (Constant *RC = dyn_cast<Constant>(RHS))
469         return Folder.CreateFMul(LC, RC);
470     return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
471   }
472   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
473     if (Constant *LC = dyn_cast<Constant>(LHS))
474       if (Constant *RC = dyn_cast<Constant>(RHS))
475         return Folder.CreateUDiv(LC, RC);
476     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
477   }
478   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
479     if (Constant *LC = dyn_cast<Constant>(LHS))
480       if (Constant *RC = dyn_cast<Constant>(RHS))
481         return Folder.CreateSDiv(LC, RC);
482     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
483   }
484   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
485     if (Constant *LC = dyn_cast<Constant>(LHS))
486       if (Constant *RC = dyn_cast<Constant>(RHS))
487         return Folder.CreateExactSDiv(LC, RC);
488     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
489   }
490   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
491     if (Constant *LC = dyn_cast<Constant>(LHS))
492       if (Constant *RC = dyn_cast<Constant>(RHS))
493         return Folder.CreateFDiv(LC, RC);
494     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
495   }
496   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
497     if (Constant *LC = dyn_cast<Constant>(LHS))
498       if (Constant *RC = dyn_cast<Constant>(RHS))
499         return Folder.CreateURem(LC, RC);
500     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
501   }
502   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
503     if (Constant *LC = dyn_cast<Constant>(LHS))
504       if (Constant *RC = dyn_cast<Constant>(RHS))
505         return Folder.CreateSRem(LC, RC);
506     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
507   }
508   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
509     if (Constant *LC = dyn_cast<Constant>(LHS))
510       if (Constant *RC = dyn_cast<Constant>(RHS))
511         return Folder.CreateFRem(LC, RC);
512     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
513   }
514
515   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
516     if (Constant *LC = dyn_cast<Constant>(LHS))
517       if (Constant *RC = dyn_cast<Constant>(RHS))
518         return Folder.CreateShl(LC, RC);
519     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
520   }
521   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "") {
522     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
523     if (Constant *LC = dyn_cast<Constant>(LHS))
524       return Folder.CreateShl(LC, RHSC);
525     return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
526   }
527   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
528     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
529     if (Constant *LC = dyn_cast<Constant>(LHS))
530       return Folder.CreateShl(LC, RHSC);
531     return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
532   }
533
534   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
535     if (Constant *LC = dyn_cast<Constant>(LHS))
536       if (Constant *RC = dyn_cast<Constant>(RHS))
537         return Folder.CreateLShr(LC, RC);
538     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
539   }
540   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
541     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
542     if (Constant *LC = dyn_cast<Constant>(LHS))
543       return Folder.CreateLShr(LC, RHSC);
544     return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
545   }
546   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
547     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
548     if (Constant *LC = dyn_cast<Constant>(LHS))
549       return Folder.CreateLShr(LC, RHSC);
550     return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
551   }
552
553   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
554     if (Constant *LC = dyn_cast<Constant>(LHS))
555       if (Constant *RC = dyn_cast<Constant>(RHS))
556         return Folder.CreateAShr(LC, RC);
557     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
558   }
559   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
560     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
561     if (Constant *LC = dyn_cast<Constant>(LHS))
562       return Folder.CreateAShr(LC, RHSC);
563     return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
564   }
565   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
566     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
567     if (Constant *LC = dyn_cast<Constant>(LHS))
568       return Folder.CreateAShr(LC, RHSC);
569     return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
570   }
571
572   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
573     if (Constant *RC = dyn_cast<Constant>(RHS)) {
574       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
575         return LHS;  // LHS & -1 -> LHS
576       if (Constant *LC = dyn_cast<Constant>(LHS))
577         return Folder.CreateAnd(LC, RC);
578     }
579     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
580   }
581   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
582     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
583     if (Constant *LC = dyn_cast<Constant>(LHS))
584       return Folder.CreateAnd(LC, RHSC);
585     return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
586   }
587   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
588     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
589     if (Constant *LC = dyn_cast<Constant>(LHS))
590       return Folder.CreateAnd(LC, RHSC);
591     return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
592   }
593
594   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
595     if (Constant *RC = dyn_cast<Constant>(RHS)) {
596       if (RC->isNullValue())
597         return LHS;  // LHS | 0 -> LHS
598       if (Constant *LC = dyn_cast<Constant>(LHS))
599         return Folder.CreateOr(LC, RC);
600     }
601     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
602   }
603   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
604     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
605     if (Constant *LC = dyn_cast<Constant>(LHS))
606       return Folder.CreateOr(LC, RHSC);
607     return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
608   }
609   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
610     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
611     if (Constant *LC = dyn_cast<Constant>(LHS))
612       return Folder.CreateOr(LC, RHSC);
613     return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
614   }
615
616   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
617     if (Constant *LC = dyn_cast<Constant>(LHS))
618       if (Constant *RC = dyn_cast<Constant>(RHS))
619         return Folder.CreateXor(LC, RC);
620     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
621   }
622   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
623     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
624     if (Constant *LC = dyn_cast<Constant>(LHS))
625       return Folder.CreateXor(LC, RHSC);
626     return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
627   }
628   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
629     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
630     if (Constant *LC = dyn_cast<Constant>(LHS))
631       return Folder.CreateXor(LC, RHSC);
632     return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
633   }
634
635   Value *CreateBinOp(Instruction::BinaryOps Opc,
636                      Value *LHS, Value *RHS, const Twine &Name = "") {
637     if (Constant *LC = dyn_cast<Constant>(LHS))
638       if (Constant *RC = dyn_cast<Constant>(RHS))
639         return Folder.CreateBinOp(Opc, LC, RC);
640     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
641   }
642
643   Value *CreateNeg(Value *V, const Twine &Name = "") {
644     if (Constant *VC = dyn_cast<Constant>(V))
645       return Folder.CreateNeg(VC);
646     return Insert(BinaryOperator::CreateNeg(V), Name);
647   }
648   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
649     if (Constant *VC = dyn_cast<Constant>(V))
650       return Folder.CreateNSWNeg(VC);
651     return Insert(BinaryOperator::CreateNSWNeg(V), Name);
652   }
653   Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
654     if (Constant *VC = dyn_cast<Constant>(V))
655       return Folder.CreateNUWNeg(VC);
656     return Insert(BinaryOperator::CreateNUWNeg(V), Name);
657   }
658   Value *CreateFNeg(Value *V, const Twine &Name = "") {
659     if (Constant *VC = dyn_cast<Constant>(V))
660       return Folder.CreateFNeg(VC);
661     return Insert(BinaryOperator::CreateFNeg(V), Name);
662   }
663   Value *CreateNot(Value *V, const Twine &Name = "") {
664     if (Constant *VC = dyn_cast<Constant>(V))
665       return Folder.CreateNot(VC);
666     return Insert(BinaryOperator::CreateNot(V), Name);
667   }
668
669   //===--------------------------------------------------------------------===//
670   // Instruction creation methods: Memory Instructions
671   //===--------------------------------------------------------------------===//
672
673   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
674                            const Twine &Name = "") {
675     return Insert(new AllocaInst(Ty, ArraySize), Name);
676   }
677   // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
678   // converting the string to 'bool' for the isVolatile parameter.
679   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
680     return Insert(new LoadInst(Ptr), Name);
681   }
682   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
683     return Insert(new LoadInst(Ptr), Name);
684   }
685   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
686     return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
687   }
688   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
689     return Insert(new StoreInst(Val, Ptr, isVolatile));
690   }
691   template<typename RandomAccessIterator>
692   Value *CreateGEP(Value *Ptr,
693                    RandomAccessIterator IdxBegin,
694                    RandomAccessIterator IdxEnd,
695                    const Twine &Name = "") {
696     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
697       // Every index must be constant.
698       RandomAccessIterator i;
699       for (i = IdxBegin; i < IdxEnd; ++i)
700         if (!isa<Constant>(*i))
701           break;
702       if (i == IdxEnd)
703         return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
704     }
705     return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
706   }
707   template<typename RandomAccessIterator>
708   Value *CreateInBoundsGEP(Value *Ptr, RandomAccessIterator IdxBegin,
709                            RandomAccessIterator IdxEnd,
710                            const Twine &Name = "") {
711     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
712       // Every index must be constant.
713       RandomAccessIterator i;
714       for (i = IdxBegin; i < IdxEnd; ++i)
715         if (!isa<Constant>(*i))
716           break;
717       if (i == IdxEnd)
718         return Folder.CreateInBoundsGetElementPtr(PC,
719                                                   &IdxBegin[0],
720                                                   IdxEnd - IdxBegin);
721     }
722     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
723                   Name);
724   }
725   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
726     if (Constant *PC = dyn_cast<Constant>(Ptr))
727       if (Constant *IC = dyn_cast<Constant>(Idx))
728         return Folder.CreateGetElementPtr(PC, &IC, 1);
729     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
730   }
731   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
732     if (Constant *PC = dyn_cast<Constant>(Ptr))
733       if (Constant *IC = dyn_cast<Constant>(Idx))
734         return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
735     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
736   }
737   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
738     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
739
740     if (Constant *PC = dyn_cast<Constant>(Ptr))
741       return Folder.CreateGetElementPtr(PC, &Idx, 1);
742
743     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
744   }
745   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
746                                     const Twine &Name = "") {
747     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
748
749     if (Constant *PC = dyn_cast<Constant>(Ptr))
750       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
751
752     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
753   }
754   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
755                     const Twine &Name = "") {
756     Value *Idxs[] = {
757       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
758       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
759     };
760
761     if (Constant *PC = dyn_cast<Constant>(Ptr))
762       return Folder.CreateGetElementPtr(PC, Idxs, 2);
763
764     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
765   }
766   Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
767                                     const Twine &Name = "") {
768     Value *Idxs[] = {
769       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
770       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
771     };
772
773     if (Constant *PC = dyn_cast<Constant>(Ptr))
774       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
775
776     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
777   }
778   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
779     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
780
781     if (Constant *PC = dyn_cast<Constant>(Ptr))
782       return Folder.CreateGetElementPtr(PC, &Idx, 1);
783
784     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
785   }
786   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
787                                     const Twine &Name = "") {
788     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
789
790     if (Constant *PC = dyn_cast<Constant>(Ptr))
791       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
792
793     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
794   }
795   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
796                     const Twine &Name = "") {
797     Value *Idxs[] = {
798       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
799       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
800     };
801
802     if (Constant *PC = dyn_cast<Constant>(Ptr))
803       return Folder.CreateGetElementPtr(PC, Idxs, 2);
804
805     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
806   }
807   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
808                                     const Twine &Name = "") {
809     Value *Idxs[] = {
810       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
811       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
812     };
813
814     if (Constant *PC = dyn_cast<Constant>(Ptr))
815       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
816
817     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
818   }
819   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
820     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
821   }
822
823   /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
824   /// with "i8*" type instead of a pointer to array of i8.
825   Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
826     Value *gv = CreateGlobalString(Str, Name);
827     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
828     Value *Args[] = { zero, zero };
829     return CreateInBoundsGEP(gv, Args, Args+2, Name);
830   }
831
832   //===--------------------------------------------------------------------===//
833   // Instruction creation methods: Cast/Conversion Operators
834   //===--------------------------------------------------------------------===//
835
836   Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
837     return CreateCast(Instruction::Trunc, V, DestTy, Name);
838   }
839   Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
840     return CreateCast(Instruction::ZExt, V, DestTy, Name);
841   }
842   Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
843     return CreateCast(Instruction::SExt, V, DestTy, Name);
844   }
845   Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
846     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
847   }
848   Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
849     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
850   }
851   Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
852     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
853   }
854   Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
855     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
856   }
857   Value *CreateFPTrunc(Value *V, const Type *DestTy,
858                        const Twine &Name = "") {
859     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
860   }
861   Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
862     return CreateCast(Instruction::FPExt, V, DestTy, Name);
863   }
864   Value *CreatePtrToInt(Value *V, const Type *DestTy,
865                         const Twine &Name = "") {
866     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
867   }
868   Value *CreateIntToPtr(Value *V, const Type *DestTy,
869                         const Twine &Name = "") {
870     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
871   }
872   Value *CreateBitCast(Value *V, const Type *DestTy,
873                        const Twine &Name = "") {
874     return CreateCast(Instruction::BitCast, V, DestTy, Name);
875   }
876   Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
877                              const Twine &Name = "") {
878     if (V->getType() == DestTy)
879       return V;
880     if (Constant *VC = dyn_cast<Constant>(V))
881       return Folder.CreateZExtOrBitCast(VC, DestTy);
882     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
883   }
884   Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
885                              const Twine &Name = "") {
886     if (V->getType() == DestTy)
887       return V;
888     if (Constant *VC = dyn_cast<Constant>(V))
889       return Folder.CreateSExtOrBitCast(VC, DestTy);
890     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
891   }
892   Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
893                               const Twine &Name = "") {
894     if (V->getType() == DestTy)
895       return V;
896     if (Constant *VC = dyn_cast<Constant>(V))
897       return Folder.CreateTruncOrBitCast(VC, DestTy);
898     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
899   }
900   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
901                     const Twine &Name = "") {
902     if (V->getType() == DestTy)
903       return V;
904     if (Constant *VC = dyn_cast<Constant>(V))
905       return Folder.CreateCast(Op, VC, DestTy);
906     return Insert(CastInst::Create(Op, V, DestTy), Name);
907   }
908   Value *CreatePointerCast(Value *V, const Type *DestTy,
909                            const Twine &Name = "") {
910     if (V->getType() == DestTy)
911       return V;
912     if (Constant *VC = dyn_cast<Constant>(V))
913       return Folder.CreatePointerCast(VC, DestTy);
914     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
915   }
916   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
917                        const Twine &Name = "") {
918     if (V->getType() == DestTy)
919       return V;
920     if (Constant *VC = dyn_cast<Constant>(V))
921       return Folder.CreateIntCast(VC, DestTy, isSigned);
922     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
923   }
924 private:
925   // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
926   // error, instead of converting the string to bool for the isSigned parameter.
927   Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
928 public:
929   Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
930     if (V->getType() == DestTy)
931       return V;
932     if (Constant *VC = dyn_cast<Constant>(V))
933       return Folder.CreateFPCast(VC, DestTy);
934     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
935   }
936
937   //===--------------------------------------------------------------------===//
938   // Instruction creation methods: Compare Instructions
939   //===--------------------------------------------------------------------===//
940
941   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
942     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
943   }
944   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
945     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
946   }
947   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
948     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
949   }
950   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
951     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
952   }
953   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
954     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
955   }
956   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
957     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
958   }
959   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
960     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
961   }
962   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
963     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
964   }
965   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
966     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
967   }
968   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
969     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
970   }
971
972   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
973     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
974   }
975   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
976     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
977   }
978   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
979     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
980   }
981   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
982     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
983   }
984   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
985     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
986   }
987   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
988     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
989   }
990   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
991     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
992   }
993   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
994     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
995   }
996   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
997     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
998   }
999   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1000     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1001   }
1002   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1003     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1004   }
1005   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1006     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1007   }
1008   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1009     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1010   }
1011   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1012     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1013   }
1014
1015   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1016                     const Twine &Name = "") {
1017     if (Constant *LC = dyn_cast<Constant>(LHS))
1018       if (Constant *RC = dyn_cast<Constant>(RHS))
1019         return Folder.CreateICmp(P, LC, RC);
1020     return Insert(new ICmpInst(P, LHS, RHS), Name);
1021   }
1022   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1023                     const Twine &Name = "") {
1024     if (Constant *LC = dyn_cast<Constant>(LHS))
1025       if (Constant *RC = dyn_cast<Constant>(RHS))
1026         return Folder.CreateFCmp(P, LC, RC);
1027     return Insert(new FCmpInst(P, LHS, RHS), Name);
1028   }
1029
1030   //===--------------------------------------------------------------------===//
1031   // Instruction creation methods: Other Instructions
1032   //===--------------------------------------------------------------------===//
1033
1034   PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
1035     return Insert(PHINode::Create(Ty), Name);
1036   }
1037
1038   CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1039     return Insert(CallInst::Create(Callee), Name);
1040   }
1041   CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1042     return Insert(CallInst::Create(Callee, Arg), Name);
1043   }
1044   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1045                         const Twine &Name = "") {
1046     Value *Args[] = { Arg1, Arg2 };
1047     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
1048   }
1049   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1050                         const Twine &Name = "") {
1051     Value *Args[] = { Arg1, Arg2, Arg3 };
1052     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
1053   }
1054   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1055                         Value *Arg4, const Twine &Name = "") {
1056     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1057     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
1058   }
1059   CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1060                         Value *Arg4, Value *Arg5, const Twine &Name = "") {
1061     Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1062     return Insert(CallInst::Create(Callee, Args, Args+5), Name);
1063   }
1064
1065   template<typename RandomAccessIterator>
1066   CallInst *CreateCall(Value *Callee, RandomAccessIterator ArgBegin,
1067                        RandomAccessIterator ArgEnd, const Twine &Name = "") {
1068     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
1069   }
1070
1071   Value *CreateSelect(Value *C, Value *True, Value *False,
1072                       const Twine &Name = "") {
1073     if (Constant *CC = dyn_cast<Constant>(C))
1074       if (Constant *TC = dyn_cast<Constant>(True))
1075         if (Constant *FC = dyn_cast<Constant>(False))
1076           return Folder.CreateSelect(CC, TC, FC);
1077     return Insert(SelectInst::Create(C, True, False), Name);
1078   }
1079
1080   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
1081     return Insert(new VAArgInst(List, Ty), Name);
1082   }
1083
1084   Value *CreateExtractElement(Value *Vec, Value *Idx,
1085                               const Twine &Name = "") {
1086     if (Constant *VC = dyn_cast<Constant>(Vec))
1087       if (Constant *IC = dyn_cast<Constant>(Idx))
1088         return Folder.CreateExtractElement(VC, IC);
1089     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1090   }
1091
1092   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1093                              const Twine &Name = "") {
1094     if (Constant *VC = dyn_cast<Constant>(Vec))
1095       if (Constant *NC = dyn_cast<Constant>(NewElt))
1096         if (Constant *IC = dyn_cast<Constant>(Idx))
1097           return Folder.CreateInsertElement(VC, NC, IC);
1098     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1099   }
1100
1101   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1102                              const Twine &Name = "") {
1103     if (Constant *V1C = dyn_cast<Constant>(V1))
1104       if (Constant *V2C = dyn_cast<Constant>(V2))
1105         if (Constant *MC = dyn_cast<Constant>(Mask))
1106           return Folder.CreateShuffleVector(V1C, V2C, MC);
1107     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1108   }
1109
1110   Value *CreateExtractValue(Value *Agg, unsigned Idx,
1111                             const Twine &Name = "") {
1112     if (Constant *AggC = dyn_cast<Constant>(Agg))
1113       return Folder.CreateExtractValue(AggC, &Idx, 1);
1114     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1115   }
1116
1117   template<typename RandomAccessIterator>
1118   Value *CreateExtractValue(Value *Agg,
1119                             RandomAccessIterator IdxBegin,
1120                             RandomAccessIterator IdxEnd,
1121                             const Twine &Name = "") {
1122     if (Constant *AggC = dyn_cast<Constant>(Agg))
1123       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
1124     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1125   }
1126
1127   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1128                            const Twine &Name = "") {
1129     if (Constant *AggC = dyn_cast<Constant>(Agg))
1130       if (Constant *ValC = dyn_cast<Constant>(Val))
1131         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
1132     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1133   }
1134
1135   template<typename RandomAccessIterator>
1136   Value *CreateInsertValue(Value *Agg, Value *Val,
1137                            RandomAccessIterator IdxBegin,
1138                            RandomAccessIterator IdxEnd,
1139                            const Twine &Name = "") {
1140     if (Constant *AggC = dyn_cast<Constant>(Agg))
1141       if (Constant *ValC = dyn_cast<Constant>(Val))
1142         return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
1143     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1144   }
1145
1146   //===--------------------------------------------------------------------===//
1147   // Utility creation methods
1148   //===--------------------------------------------------------------------===//
1149
1150   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1151   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1152     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1153                         Name);
1154   }
1155
1156   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1157   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1158     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1159                         Name);
1160   }
1161
1162   /// CreatePtrDiff - Return the i64 difference between two pointer values,
1163   /// dividing out the size of the pointed-to objects.  This is intended to
1164   /// implement C-style pointer subtraction. As such, the pointers must be
1165   /// appropriately aligned for their element types and pointing into the
1166   /// same object.
1167   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1168     assert(LHS->getType() == RHS->getType() &&
1169            "Pointer subtraction operand types must match!");
1170     const PointerType *ArgType = cast<PointerType>(LHS->getType());
1171     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1172     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1173     Value *Difference = CreateSub(LHS_int, RHS_int);
1174     return CreateExactSDiv(Difference,
1175                            ConstantExpr::getSizeOf(ArgType->getElementType()),
1176                            Name);
1177   }
1178 };
1179
1180 }
1181
1182 #endif