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