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