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