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