Add IRBuilder methods for creating an exact udiv, like for exact sdiv.
[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
452   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
453     if (Constant *LC = dyn_cast<Constant>(LHS))
454       if (Constant *RC = dyn_cast<Constant>(RHS))
455         return Insert(Folder.CreateAdd(LC, RC), Name);
456     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
457   }
458   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
459     if (Constant *LC = dyn_cast<Constant>(LHS))
460       if (Constant *RC = dyn_cast<Constant>(RHS))
461         return Insert(Folder.CreateNSWAdd(LC, RC), Name);
462     return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
463   }
464   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
465     if (Constant *LC = dyn_cast<Constant>(LHS))
466       if (Constant *RC = dyn_cast<Constant>(RHS))
467         return Insert(Folder.CreateNUWAdd(LC, RC), Name);
468     return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
469   }
470   Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
471     if (Constant *LC = dyn_cast<Constant>(LHS))
472       if (Constant *RC = dyn_cast<Constant>(RHS))
473         return Insert(Folder.CreateFAdd(LC, RC), Name);
474     return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
475   }
476   Value *CreateSub(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.CreateSub(LC, RC), Name);
480     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
481   }
482   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
483     if (Constant *LC = dyn_cast<Constant>(LHS))
484       if (Constant *RC = dyn_cast<Constant>(RHS))
485         return Insert(Folder.CreateNSWSub(LC, RC), Name);
486     return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
487   }
488   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
489     if (Constant *LC = dyn_cast<Constant>(LHS))
490       if (Constant *RC = dyn_cast<Constant>(RHS))
491         return Insert(Folder.CreateNUWSub(LC, RC), Name);
492     return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
493   }
494   Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
495     if (Constant *LC = dyn_cast<Constant>(LHS))
496       if (Constant *RC = dyn_cast<Constant>(RHS))
497         return Insert(Folder.CreateFSub(LC, RC), Name);
498     return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
499   }
500   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
501     if (Constant *LC = dyn_cast<Constant>(LHS))
502       if (Constant *RC = dyn_cast<Constant>(RHS))
503         return Insert(Folder.CreateMul(LC, RC), Name);
504     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
505   }
506   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
507     if (Constant *LC = dyn_cast<Constant>(LHS))
508       if (Constant *RC = dyn_cast<Constant>(RHS))
509         return Insert(Folder.CreateNSWMul(LC, RC), Name);
510     return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
511   }
512   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
513     if (Constant *LC = dyn_cast<Constant>(LHS))
514       if (Constant *RC = dyn_cast<Constant>(RHS))
515         return Insert(Folder.CreateNUWMul(LC, RC), Name);
516     return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
517   }
518   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
519     if (Constant *LC = dyn_cast<Constant>(LHS))
520       if (Constant *RC = dyn_cast<Constant>(RHS))
521         return Insert(Folder.CreateFMul(LC, RC), Name);
522     return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
523   }
524   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
525     if (Constant *LC = dyn_cast<Constant>(LHS))
526       if (Constant *RC = dyn_cast<Constant>(RHS))
527         return Insert(Folder.CreateUDiv(LC, RC), Name);
528     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
529   }
530   Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
531     if (Constant *LC = dyn_cast<Constant>(LHS))
532       if (Constant *RC = dyn_cast<Constant>(RHS))
533         return Insert(Folder.CreateExactUDiv(LC, RC), Name);
534     return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
535   }
536   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
537     if (Constant *LC = dyn_cast<Constant>(LHS))
538       if (Constant *RC = dyn_cast<Constant>(RHS))
539         return Insert(Folder.CreateSDiv(LC, RC), Name);
540     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
541   }
542   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
543     if (Constant *LC = dyn_cast<Constant>(LHS))
544       if (Constant *RC = dyn_cast<Constant>(RHS))
545         return Insert(Folder.CreateExactSDiv(LC, RC), Name);
546     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
547   }
548   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
549     if (Constant *LC = dyn_cast<Constant>(LHS))
550       if (Constant *RC = dyn_cast<Constant>(RHS))
551         return Insert(Folder.CreateFDiv(LC, RC), Name);
552     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
553   }
554   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
555     if (Constant *LC = dyn_cast<Constant>(LHS))
556       if (Constant *RC = dyn_cast<Constant>(RHS))
557         return Insert(Folder.CreateURem(LC, RC), Name);
558     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
559   }
560   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
561     if (Constant *LC = dyn_cast<Constant>(LHS))
562       if (Constant *RC = dyn_cast<Constant>(RHS))
563         return Insert(Folder.CreateSRem(LC, RC), Name);
564     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
565   }
566   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
567     if (Constant *LC = dyn_cast<Constant>(LHS))
568       if (Constant *RC = dyn_cast<Constant>(RHS))
569         return Insert(Folder.CreateFRem(LC, RC), Name);
570     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
571   }
572
573   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
574     if (Constant *LC = dyn_cast<Constant>(LHS))
575       if (Constant *RC = dyn_cast<Constant>(RHS))
576         return Insert(Folder.CreateShl(LC, RC), Name);
577     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
578   }
579   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "") {
580     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
581     if (Constant *LC = dyn_cast<Constant>(LHS))
582       return Insert(Folder.CreateShl(LC, RHSC), Name);
583     return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
584   }
585   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
586     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
587     if (Constant *LC = dyn_cast<Constant>(LHS))
588       return Insert(Folder.CreateShl(LC, RHSC), Name);
589     return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
590   }
591
592   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
593     if (Constant *LC = dyn_cast<Constant>(LHS))
594       if (Constant *RC = dyn_cast<Constant>(RHS))
595         return Insert(Folder.CreateLShr(LC, RC), Name);
596     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
597   }
598   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
599     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
600     if (Constant *LC = dyn_cast<Constant>(LHS))
601       return Insert(Folder.CreateLShr(LC, RHSC), Name);
602     return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
603   }
604   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
605     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
606     if (Constant *LC = dyn_cast<Constant>(LHS))
607       return Insert(Folder.CreateLShr(LC, RHSC), Name);
608     return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
609   }
610
611   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
612     if (Constant *LC = dyn_cast<Constant>(LHS))
613       if (Constant *RC = dyn_cast<Constant>(RHS))
614         return Insert(Folder.CreateAShr(LC, RC), Name);
615     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
616   }
617   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
618     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
619     if (Constant *LC = dyn_cast<Constant>(LHS))
620       return Insert(Folder.CreateAShr(LC, RHSC), Name);
621     return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
622   }
623   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
624     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
625     if (Constant *LC = dyn_cast<Constant>(LHS))
626       return Insert(Folder.CreateAShr(LC, RHSC), Name);
627     return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
628   }
629
630   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
631     if (Constant *RC = dyn_cast<Constant>(RHS)) {
632       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
633         return LHS;  // LHS & -1 -> LHS
634       if (Constant *LC = dyn_cast<Constant>(LHS))
635         return Insert(Folder.CreateAnd(LC, RC), Name);
636     }
637     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
638   }
639   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
640     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
641     if (Constant *LC = dyn_cast<Constant>(LHS))
642       return Insert(Folder.CreateAnd(LC, RHSC), Name);
643     return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
644   }
645   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
646     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
647     if (Constant *LC = dyn_cast<Constant>(LHS))
648       return Insert(Folder.CreateAnd(LC, RHSC), Name);
649     return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
650   }
651
652   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
653     if (Constant *RC = dyn_cast<Constant>(RHS)) {
654       if (RC->isNullValue())
655         return LHS;  // LHS | 0 -> LHS
656       if (Constant *LC = dyn_cast<Constant>(LHS))
657         return Insert(Folder.CreateOr(LC, RC), Name);
658     }
659     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
660   }
661   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
662     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
663     if (Constant *LC = dyn_cast<Constant>(LHS))
664       return Insert(Folder.CreateOr(LC, RHSC), Name);
665     return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
666   }
667   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
668     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
669     if (Constant *LC = dyn_cast<Constant>(LHS))
670       return Insert(Folder.CreateOr(LC, RHSC), Name);
671     return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
672   }
673
674   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
675     if (Constant *LC = dyn_cast<Constant>(LHS))
676       if (Constant *RC = dyn_cast<Constant>(RHS))
677         return Insert(Folder.CreateXor(LC, RC), Name);
678     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
679   }
680   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
681     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
682     if (Constant *LC = dyn_cast<Constant>(LHS))
683       return Insert(Folder.CreateXor(LC, RHSC), Name);
684     return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
685   }
686   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
687     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
688     if (Constant *LC = dyn_cast<Constant>(LHS))
689       return Insert(Folder.CreateXor(LC, RHSC), Name);
690     return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
691   }
692
693   Value *CreateBinOp(Instruction::BinaryOps Opc,
694                      Value *LHS, Value *RHS, const Twine &Name = "") {
695     if (Constant *LC = dyn_cast<Constant>(LHS))
696       if (Constant *RC = dyn_cast<Constant>(RHS))
697         return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
698     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
699   }
700
701   Value *CreateNeg(Value *V, const Twine &Name = "") {
702     if (Constant *VC = dyn_cast<Constant>(V))
703       return Insert(Folder.CreateNeg(VC), Name);
704     return Insert(BinaryOperator::CreateNeg(V), Name);
705   }
706   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
707     if (Constant *VC = dyn_cast<Constant>(V))
708       return Insert(Folder.CreateNSWNeg(VC), Name);
709     return Insert(BinaryOperator::CreateNSWNeg(V), Name);
710   }
711   Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
712     if (Constant *VC = dyn_cast<Constant>(V))
713       return Insert(Folder.CreateNUWNeg(VC), Name);
714     return Insert(BinaryOperator::CreateNUWNeg(V), Name);
715   }
716   Value *CreateFNeg(Value *V, const Twine &Name = "") {
717     if (Constant *VC = dyn_cast<Constant>(V))
718       return Insert(Folder.CreateFNeg(VC), Name);
719     return Insert(BinaryOperator::CreateFNeg(V), Name);
720   }
721   Value *CreateNot(Value *V, const Twine &Name = "") {
722     if (Constant *VC = dyn_cast<Constant>(V))
723       return Insert(Folder.CreateNot(VC), Name);
724     return Insert(BinaryOperator::CreateNot(V), Name);
725   }
726
727   //===--------------------------------------------------------------------===//
728   // Instruction creation methods: Memory Instructions
729   //===--------------------------------------------------------------------===//
730
731   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
732                            const Twine &Name = "") {
733     return Insert(new AllocaInst(Ty, ArraySize), Name);
734   }
735   // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
736   // converting the string to 'bool' for the isVolatile parameter.
737   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
738     return Insert(new LoadInst(Ptr), Name);
739   }
740   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
741     return Insert(new LoadInst(Ptr), Name);
742   }
743   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
744     return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
745   }
746   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
747     return Insert(new StoreInst(Val, Ptr, isVolatile));
748   }
749   template<typename RandomAccessIterator>
750   Value *CreateGEP(Value *Ptr,
751                    RandomAccessIterator IdxBegin,
752                    RandomAccessIterator IdxEnd,
753                    const Twine &Name = "") {
754     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
755       // Every index must be constant.
756       RandomAccessIterator i;
757       for (i = IdxBegin; i < IdxEnd; ++i)
758         if (!isa<Constant>(*i))
759           break;
760       if (i == IdxEnd)
761         return Insert(Folder.CreateGetElementPtr(PC, &IdxBegin[0],
762                                                  IdxEnd - IdxBegin),
763                       Name);
764     }
765     return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
766   }
767   template<typename RandomAccessIterator>
768   Value *CreateInBoundsGEP(Value *Ptr, RandomAccessIterator IdxBegin,
769                            RandomAccessIterator IdxEnd,
770                            const Twine &Name = "") {
771     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
772       // Every index must be constant.
773       RandomAccessIterator i;
774       for (i = IdxBegin; i < IdxEnd; ++i)
775         if (!isa<Constant>(*i))
776           break;
777       if (i == IdxEnd)
778         return Insert(Folder.CreateInBoundsGetElementPtr(PC,
779                                                          &IdxBegin[0],
780                                                          IdxEnd - IdxBegin),
781                       Name);
782     }
783     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
784                   Name);
785   }
786   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
787     if (Constant *PC = dyn_cast<Constant>(Ptr))
788       if (Constant *IC = dyn_cast<Constant>(Idx))
789         return Insert(Folder.CreateGetElementPtr(PC, &IC, 1), Name);
790     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
791   }
792   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
793     if (Constant *PC = dyn_cast<Constant>(Ptr))
794       if (Constant *IC = dyn_cast<Constant>(Idx))
795         return Insert(Folder.CreateInBoundsGetElementPtr(PC, &IC, 1), Name);
796     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
797   }
798   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
799     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
800
801     if (Constant *PC = dyn_cast<Constant>(Ptr))
802       return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
803
804     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
805   }
806   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
807                                     const Twine &Name = "") {
808     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
809
810     if (Constant *PC = dyn_cast<Constant>(Ptr))
811       return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
812
813     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
814   }
815   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
816                     const Twine &Name = "") {
817     Value *Idxs[] = {
818       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
819       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
820     };
821
822     if (Constant *PC = dyn_cast<Constant>(Ptr))
823       return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
824
825     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
826   }
827   Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
828                                     const Twine &Name = "") {
829     Value *Idxs[] = {
830       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
831       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
832     };
833
834     if (Constant *PC = dyn_cast<Constant>(Ptr))
835       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
836
837     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
838   }
839   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
840     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
841
842     if (Constant *PC = dyn_cast<Constant>(Ptr))
843       return Insert(Folder.CreateGetElementPtr(PC, &Idx, 1), Name);
844
845     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
846   }
847   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
848                                     const Twine &Name = "") {
849     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
850
851     if (Constant *PC = dyn_cast<Constant>(Ptr))
852       return Insert(Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1), Name);
853
854     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
855   }
856   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
857                     const Twine &Name = "") {
858     Value *Idxs[] = {
859       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
860       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
861     };
862
863     if (Constant *PC = dyn_cast<Constant>(Ptr))
864       return Insert(Folder.CreateGetElementPtr(PC, Idxs, 2), Name);
865
866     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
867   }
868   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
869                                     const Twine &Name = "") {
870     Value *Idxs[] = {
871       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
872       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
873     };
874
875     if (Constant *PC = dyn_cast<Constant>(Ptr))
876       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2), Name);
877
878     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
879   }
880   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
881     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
882   }
883
884   /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
885   /// with "i8*" type instead of a pointer to array of i8.
886   Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
887     Value *gv = CreateGlobalString(Str, Name);
888     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
889     Value *Args[] = { zero, zero };
890     return CreateInBoundsGEP(gv, Args, Args+2, Name);
891   }
892
893   //===--------------------------------------------------------------------===//
894   // Instruction creation methods: Cast/Conversion Operators
895   //===--------------------------------------------------------------------===//
896
897   Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
898     return CreateCast(Instruction::Trunc, V, DestTy, Name);
899   }
900   Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
901     return CreateCast(Instruction::ZExt, V, DestTy, Name);
902   }
903   Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
904     return CreateCast(Instruction::SExt, V, DestTy, Name);
905   }
906   Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
907     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
908   }
909   Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
910     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
911   }
912   Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
913     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
914   }
915   Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
916     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
917   }
918   Value *CreateFPTrunc(Value *V, const Type *DestTy,
919                        const Twine &Name = "") {
920     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
921   }
922   Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
923     return CreateCast(Instruction::FPExt, V, DestTy, Name);
924   }
925   Value *CreatePtrToInt(Value *V, const Type *DestTy,
926                         const Twine &Name = "") {
927     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
928   }
929   Value *CreateIntToPtr(Value *V, const Type *DestTy,
930                         const Twine &Name = "") {
931     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
932   }
933   Value *CreateBitCast(Value *V, const Type *DestTy,
934                        const Twine &Name = "") {
935     return CreateCast(Instruction::BitCast, V, DestTy, Name);
936   }
937   Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
938                              const Twine &Name = "") {
939     if (V->getType() == DestTy)
940       return V;
941     if (Constant *VC = dyn_cast<Constant>(V))
942       return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
943     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
944   }
945   Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
946                              const Twine &Name = "") {
947     if (V->getType() == DestTy)
948       return V;
949     if (Constant *VC = dyn_cast<Constant>(V))
950       return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
951     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
952   }
953   Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
954                               const Twine &Name = "") {
955     if (V->getType() == DestTy)
956       return V;
957     if (Constant *VC = dyn_cast<Constant>(V))
958       return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
959     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
960   }
961   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
962                     const Twine &Name = "") {
963     if (V->getType() == DestTy)
964       return V;
965     if (Constant *VC = dyn_cast<Constant>(V))
966       return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
967     return Insert(CastInst::Create(Op, V, DestTy), Name);
968   }
969   Value *CreatePointerCast(Value *V, const Type *DestTy,
970                            const Twine &Name = "") {
971     if (V->getType() == DestTy)
972       return V;
973     if (Constant *VC = dyn_cast<Constant>(V))
974       return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
975     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
976   }
977   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
978                        const Twine &Name = "") {
979     if (V->getType() == DestTy)
980       return V;
981     if (Constant *VC = dyn_cast<Constant>(V))
982       return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
983     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
984   }
985 private:
986   // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
987   // error, instead of converting the string to bool for the isSigned parameter.
988   Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
989 public:
990   Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
991     if (V->getType() == DestTy)
992       return V;
993     if (Constant *VC = dyn_cast<Constant>(V))
994       return Insert(Folder.CreateFPCast(VC, DestTy), Name);
995     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
996   }
997
998   //===--------------------------------------------------------------------===//
999   // Instruction creation methods: Compare Instructions
1000   //===--------------------------------------------------------------------===//
1001
1002   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1003     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1004   }
1005   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1006     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1007   }
1008   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1009     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1010   }
1011   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1012     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1013   }
1014   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1015     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1016   }
1017   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1018     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1019   }
1020   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1021     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1022   }
1023   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1024     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1025   }
1026   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1027     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1028   }
1029   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1030     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1031   }
1032
1033   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1034     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1035   }
1036   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1037     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1038   }
1039   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1040     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1041   }
1042   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1043     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1044   }
1045   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1046     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1047   }
1048   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1049     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1050   }
1051   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1052     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1053   }
1054   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1055     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1056   }
1057   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1058     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1059   }
1060   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1061     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1062   }
1063   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1064     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1065   }
1066   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1067     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1068   }
1069   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1070     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1071   }
1072   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1073     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1074   }
1075
1076   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1077                     const Twine &Name = "") {
1078     if (Constant *LC = dyn_cast<Constant>(LHS))
1079       if (Constant *RC = dyn_cast<Constant>(RHS))
1080         return Insert(Folder.CreateICmp(P, LC, RC), Name);
1081     return Insert(new ICmpInst(P, LHS, RHS), Name);
1082   }
1083   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1084                     const Twine &Name = "") {
1085     if (Constant *LC = dyn_cast<Constant>(LHS))
1086       if (Constant *RC = dyn_cast<Constant>(RHS))
1087         return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1088     return Insert(new FCmpInst(P, LHS, RHS), Name);
1089   }
1090
1091   //===--------------------------------------------------------------------===//
1092   // Instruction creation methods: Other Instructions
1093   //===--------------------------------------------------------------------===//
1094
1095   PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
1096     return Insert(PHINode::Create(Ty), Name);
1097   }
1098
1099   CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1100     return Insert(CallInst::Create(Callee), Name);
1101   }
1102   CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1103     return Insert(CallInst::Create(Callee, Arg), Name);
1104   }
1105   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1106                         const Twine &Name = "") {
1107     Value *Args[] = { Arg1, Arg2 };
1108     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
1109   }
1110   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1111                         const Twine &Name = "") {
1112     Value *Args[] = { Arg1, Arg2, Arg3 };
1113     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
1114   }
1115   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1116                         Value *Arg4, const Twine &Name = "") {
1117     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1118     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
1119   }
1120   CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1121                         Value *Arg4, Value *Arg5, const Twine &Name = "") {
1122     Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1123     return Insert(CallInst::Create(Callee, Args, Args+5), Name);
1124   }
1125
1126   template<typename RandomAccessIterator>
1127   CallInst *CreateCall(Value *Callee, RandomAccessIterator ArgBegin,
1128                        RandomAccessIterator ArgEnd, const Twine &Name = "") {
1129     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
1130   }
1131
1132   Value *CreateSelect(Value *C, Value *True, Value *False,
1133                       const Twine &Name = "") {
1134     if (Constant *CC = dyn_cast<Constant>(C))
1135       if (Constant *TC = dyn_cast<Constant>(True))
1136         if (Constant *FC = dyn_cast<Constant>(False))
1137           return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1138     return Insert(SelectInst::Create(C, True, False), Name);
1139   }
1140
1141   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
1142     return Insert(new VAArgInst(List, Ty), Name);
1143   }
1144
1145   Value *CreateExtractElement(Value *Vec, Value *Idx,
1146                               const Twine &Name = "") {
1147     if (Constant *VC = dyn_cast<Constant>(Vec))
1148       if (Constant *IC = dyn_cast<Constant>(Idx))
1149         return Insert(Folder.CreateExtractElement(VC, IC), Name);
1150     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1151   }
1152
1153   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1154                              const Twine &Name = "") {
1155     if (Constant *VC = dyn_cast<Constant>(Vec))
1156       if (Constant *NC = dyn_cast<Constant>(NewElt))
1157         if (Constant *IC = dyn_cast<Constant>(Idx))
1158           return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1159     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1160   }
1161
1162   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1163                              const Twine &Name = "") {
1164     if (Constant *V1C = dyn_cast<Constant>(V1))
1165       if (Constant *V2C = dyn_cast<Constant>(V2))
1166         if (Constant *MC = dyn_cast<Constant>(Mask))
1167           return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1168     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1169   }
1170
1171   Value *CreateExtractValue(Value *Agg, unsigned Idx,
1172                             const Twine &Name = "") {
1173     if (Constant *AggC = dyn_cast<Constant>(Agg))
1174       return Insert(Folder.CreateExtractValue(AggC, &Idx, 1), Name);
1175     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1176   }
1177
1178   template<typename RandomAccessIterator>
1179   Value *CreateExtractValue(Value *Agg,
1180                             RandomAccessIterator IdxBegin,
1181                             RandomAccessIterator IdxEnd,
1182                             const Twine &Name = "") {
1183     if (Constant *AggC = dyn_cast<Constant>(Agg))
1184       return Insert(Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd-IdxBegin),
1185                     Name);
1186     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1187   }
1188
1189   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1190                            const Twine &Name = "") {
1191     if (Constant *AggC = dyn_cast<Constant>(Agg))
1192       if (Constant *ValC = dyn_cast<Constant>(Val))
1193         return Insert(Folder.CreateInsertValue(AggC, ValC, &Idx, 1), Name);
1194     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1195   }
1196
1197   template<typename RandomAccessIterator>
1198   Value *CreateInsertValue(Value *Agg, Value *Val,
1199                            RandomAccessIterator IdxBegin,
1200                            RandomAccessIterator IdxEnd,
1201                            const Twine &Name = "") {
1202     if (Constant *AggC = dyn_cast<Constant>(Agg))
1203       if (Constant *ValC = dyn_cast<Constant>(Val))
1204         return Insert(Folder.CreateInsertValue(AggC, ValC, IdxBegin,
1205                                                IdxEnd - IdxBegin),
1206                       Name);
1207     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1208   }
1209
1210   //===--------------------------------------------------------------------===//
1211   // Utility creation methods
1212   //===--------------------------------------------------------------------===//
1213
1214   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1215   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1216     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1217                         Name);
1218   }
1219
1220   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1221   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1222     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1223                         Name);
1224   }
1225
1226   /// CreatePtrDiff - Return the i64 difference between two pointer values,
1227   /// dividing out the size of the pointed-to objects.  This is intended to
1228   /// implement C-style pointer subtraction. As such, the pointers must be
1229   /// appropriately aligned for their element types and pointing into the
1230   /// same object.
1231   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1232     assert(LHS->getType() == RHS->getType() &&
1233            "Pointer subtraction operand types must match!");
1234     const PointerType *ArgType = cast<PointerType>(LHS->getType());
1235     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1236     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1237     Value *Difference = CreateSub(LHS_int, RHS_int);
1238     return CreateExactSDiv(Difference,
1239                            ConstantExpr::getSizeOf(ArgType->getElementType()),
1240                            Name);
1241   }
1242 };
1243
1244 }
1245
1246 #endif