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