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