Move EVER MORE stuff over to LLVMContext.
[oota-llvm.git] / include / llvm / Instructions.h
1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- 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 exposes the class definitions of all of the subclasses of the
11 // Instruction class.  This is meant to be an easy way to get access to all
12 // instruction subclasses.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
18
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include <iterator>
26
27 namespace llvm {
28
29 class ConstantInt;
30 class ConstantRange;
31 class APInt;
32 class LLVMContext;
33
34 //===----------------------------------------------------------------------===//
35 //                             AllocationInst Class
36 //===----------------------------------------------------------------------===//
37
38 /// AllocationInst - This class is the common base class of MallocInst and
39 /// AllocaInst.
40 ///
41 class AllocationInst : public UnaryInstruction {
42 protected:
43   LLVMContext &Context;
44
45   AllocationInst(LLVMContext &Context, const Type *Ty, Value *ArraySize, 
46                  unsigned iTy, unsigned Align, const std::string &Name = "", 
47                  Instruction *InsertBefore = 0);
48   AllocationInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
49                  unsigned iTy, unsigned Align, const std::string &Name,
50                  BasicBlock *InsertAtEnd);
51 public:
52   // Out of line virtual method, so the vtable, etc. has a home.
53   virtual ~AllocationInst();
54
55   /// isArrayAllocation - Return true if there is an allocation size parameter
56   /// to the allocation instruction that is not 1.
57   ///
58   bool isArrayAllocation() const;
59
60   /// getArraySize - Get the number of elements allocated. For a simple
61   /// allocation of a single element, this will return a constant 1 value.
62   ///
63   const Value *getArraySize() const { return getOperand(0); }
64   Value *getArraySize() { return getOperand(0); }
65
66   /// getType - Overload to return most specific pointer type
67   ///
68   const PointerType *getType() const {
69     return reinterpret_cast<const PointerType*>(Instruction::getType());
70   }
71
72   /// getAllocatedType - Return the type that is being allocated by the
73   /// instruction.
74   ///
75   const Type *getAllocatedType() const;
76
77   /// getAlignment - Return the alignment of the memory that is being allocated
78   /// by the instruction.
79   ///
80   unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
81   void setAlignment(unsigned Align);
82
83   virtual Instruction *clone(LLVMContext &Context) const = 0;
84
85   // Methods for support type inquiry through isa, cast, and dyn_cast:
86   static inline bool classof(const AllocationInst *) { return true; }
87   static inline bool classof(const Instruction *I) {
88     return I->getOpcode() == Instruction::Alloca ||
89            I->getOpcode() == Instruction::Malloc;
90   }
91   static inline bool classof(const Value *V) {
92     return isa<Instruction>(V) && classof(cast<Instruction>(V));
93   }
94 };
95
96
97 //===----------------------------------------------------------------------===//
98 //                                MallocInst Class
99 //===----------------------------------------------------------------------===//
100
101 /// MallocInst - an instruction to allocated memory on the heap
102 ///
103 class MallocInst : public AllocationInst {
104   MallocInst(const MallocInst &MI);
105 public:
106   explicit MallocInst(LLVMContext &Context, 
107                       const Type *Ty, Value *ArraySize = 0,
108                       const std::string &NameStr = "",
109                       Instruction *InsertBefore = 0)
110     : AllocationInst(Context, Ty, ArraySize, Malloc,
111                      0, NameStr, InsertBefore) {}
112   MallocInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
113              const std::string &NameStr, BasicBlock *InsertAtEnd)
114     : AllocationInst(Context, Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
115
116   MallocInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
117              Instruction *InsertBefore = 0)
118     : AllocationInst(Context, Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
119   MallocInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
120              BasicBlock *InsertAtEnd)
121     : AllocationInst(Context, Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
122
123   MallocInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
124              unsigned Align, const std::string &NameStr,
125              BasicBlock *InsertAtEnd)
126     : AllocationInst(Context, Ty, ArraySize, Malloc,
127                      Align, NameStr, InsertAtEnd) {}
128   MallocInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
129              unsigned Align, const std::string &NameStr = "", 
130              Instruction *InsertBefore = 0)
131     : AllocationInst(Context, Ty, ArraySize,
132                      Malloc, Align, NameStr, InsertBefore) {}
133
134   virtual MallocInst *clone(LLVMContext &Context) const;
135
136   // Methods for support type inquiry through isa, cast, and dyn_cast:
137   static inline bool classof(const MallocInst *) { return true; }
138   static inline bool classof(const Instruction *I) {
139     return (I->getOpcode() == Instruction::Malloc);
140   }
141   static inline bool classof(const Value *V) {
142     return isa<Instruction>(V) && classof(cast<Instruction>(V));
143   }
144 };
145
146
147 //===----------------------------------------------------------------------===//
148 //                                AllocaInst Class
149 //===----------------------------------------------------------------------===//
150
151 /// AllocaInst - an instruction to allocate memory on the stack
152 ///
153 class AllocaInst : public AllocationInst {
154   AllocaInst(const AllocaInst &);
155 public:
156   explicit AllocaInst(LLVMContext &Context, const Type *Ty,
157                       Value *ArraySize = 0,
158                       const std::string &NameStr = "",
159                       Instruction *InsertBefore = 0)
160     : AllocationInst(Context, Ty, ArraySize, Alloca,
161                      0, NameStr, InsertBefore) {}
162   AllocaInst(LLVMContext &Context, const Type *Ty,
163              Value *ArraySize, const std::string &NameStr,
164              BasicBlock *InsertAtEnd)
165     : AllocationInst(Context, Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
166
167   AllocaInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
168              Instruction *InsertBefore = 0)
169     : AllocationInst(Context, Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
170   AllocaInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
171              BasicBlock *InsertAtEnd)
172     : AllocationInst(Context, Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
173
174   AllocaInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
175              unsigned Align, const std::string &NameStr = "",
176              Instruction *InsertBefore = 0)
177     : AllocationInst(Context, Ty, ArraySize, Alloca,
178                      Align, NameStr, InsertBefore) {}
179   AllocaInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
180              unsigned Align, const std::string &NameStr,
181              BasicBlock *InsertAtEnd)
182     : AllocationInst(Context, Ty, ArraySize, Alloca,
183                      Align, NameStr, InsertAtEnd) {}
184
185   virtual AllocaInst *clone(LLVMContext &Context) const;
186
187   /// isStaticAlloca - Return true if this alloca is in the entry block of the
188   /// function and is a constant size.  If so, the code generator will fold it
189   /// into the prolog/epilog code, so it is basically free.
190   bool isStaticAlloca() const;
191
192   // Methods for support type inquiry through isa, cast, and dyn_cast:
193   static inline bool classof(const AllocaInst *) { return true; }
194   static inline bool classof(const Instruction *I) {
195     return (I->getOpcode() == Instruction::Alloca);
196   }
197   static inline bool classof(const Value *V) {
198     return isa<Instruction>(V) && classof(cast<Instruction>(V));
199   }
200 };
201
202
203 //===----------------------------------------------------------------------===//
204 //                                 FreeInst Class
205 //===----------------------------------------------------------------------===//
206
207 /// FreeInst - an instruction to deallocate memory
208 ///
209 class FreeInst : public UnaryInstruction {
210   void AssertOK();
211 public:
212   explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
213   FreeInst(Value *Ptr, BasicBlock *InsertAfter);
214
215   virtual FreeInst *clone(LLVMContext &Context) const;
216
217   // Accessor methods for consistency with other memory operations
218   Value *getPointerOperand() { return getOperand(0); }
219   const Value *getPointerOperand() const { return getOperand(0); }
220
221   // Methods for support type inquiry through isa, cast, and dyn_cast:
222   static inline bool classof(const FreeInst *) { return true; }
223   static inline bool classof(const Instruction *I) {
224     return (I->getOpcode() == Instruction::Free);
225   }
226   static inline bool classof(const Value *V) {
227     return isa<Instruction>(V) && classof(cast<Instruction>(V));
228   }
229 };
230
231
232 //===----------------------------------------------------------------------===//
233 //                                LoadInst Class
234 //===----------------------------------------------------------------------===//
235
236 /// LoadInst - an instruction for reading from memory.  This uses the
237 /// SubclassData field in Value to store whether or not the load is volatile.
238 ///
239 class LoadInst : public UnaryInstruction {
240
241   LoadInst(const LoadInst &LI)
242     : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
243     setVolatile(LI.isVolatile());
244     setAlignment(LI.getAlignment());
245
246 #ifndef NDEBUG
247     AssertOK();
248 #endif
249   }
250   void AssertOK();
251 public:
252   LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
253   LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
254   LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
255            Instruction *InsertBefore = 0);
256   LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
257            unsigned Align, Instruction *InsertBefore = 0);
258   LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
259            BasicBlock *InsertAtEnd);
260   LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
261            unsigned Align, BasicBlock *InsertAtEnd);
262
263   LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
264   LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
265   explicit LoadInst(Value *Ptr, const char *NameStr = 0,
266                     bool isVolatile = false,  Instruction *InsertBefore = 0);
267   LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
268            BasicBlock *InsertAtEnd);
269
270   /// isVolatile - Return true if this is a load from a volatile memory
271   /// location.
272   ///
273   bool isVolatile() const { return SubclassData & 1; }
274
275   /// setVolatile - Specify whether this is a volatile load or not.
276   ///
277   void setVolatile(bool V) {
278     SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
279   }
280
281   virtual LoadInst *clone(LLVMContext &Context) const;
282
283   /// getAlignment - Return the alignment of the access that is being performed
284   ///
285   unsigned getAlignment() const {
286     return (1 << (SubclassData>>1)) >> 1;
287   }
288
289   void setAlignment(unsigned Align);
290
291   Value *getPointerOperand() { return getOperand(0); }
292   const Value *getPointerOperand() const { return getOperand(0); }
293   static unsigned getPointerOperandIndex() { return 0U; }
294
295   // Methods for support type inquiry through isa, cast, and dyn_cast:
296   static inline bool classof(const LoadInst *) { return true; }
297   static inline bool classof(const Instruction *I) {
298     return I->getOpcode() == Instruction::Load;
299   }
300   static inline bool classof(const Value *V) {
301     return isa<Instruction>(V) && classof(cast<Instruction>(V));
302   }
303 };
304
305
306 //===----------------------------------------------------------------------===//
307 //                                StoreInst Class
308 //===----------------------------------------------------------------------===//
309
310 /// StoreInst - an instruction for storing to memory
311 ///
312 class StoreInst : public Instruction {
313   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
314
315   StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
316                                                &Op<0>(), 2) {
317     Op<0>() = SI.Op<0>();
318     Op<1>() = SI.Op<1>();
319     setVolatile(SI.isVolatile());
320     setAlignment(SI.getAlignment());
321
322 #ifndef NDEBUG
323     AssertOK();
324 #endif
325   }
326   void AssertOK();
327 public:
328   // allocate space for exactly two operands
329   void *operator new(size_t s) {
330     return User::operator new(s, 2);
331   }
332   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
333   StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
334   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
335             Instruction *InsertBefore = 0);
336   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
337             unsigned Align, Instruction *InsertBefore = 0);
338   StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
339   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
340             unsigned Align, BasicBlock *InsertAtEnd);
341
342
343   /// isVolatile - Return true if this is a load from a volatile memory
344   /// location.
345   ///
346   bool isVolatile() const { return SubclassData & 1; }
347
348   /// setVolatile - Specify whether this is a volatile load or not.
349   ///
350   void setVolatile(bool V) {
351     SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
352   }
353
354   /// Transparently provide more efficient getOperand methods.
355   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
356
357   /// getAlignment - Return the alignment of the access that is being performed
358   ///
359   unsigned getAlignment() const {
360     return (1 << (SubclassData>>1)) >> 1;
361   }
362
363   void setAlignment(unsigned Align);
364
365   virtual StoreInst *clone(LLVMContext &Context) const;
366
367   Value *getPointerOperand() { return getOperand(1); }
368   const Value *getPointerOperand() const { return getOperand(1); }
369   static unsigned getPointerOperandIndex() { return 1U; }
370
371   // Methods for support type inquiry through isa, cast, and dyn_cast:
372   static inline bool classof(const StoreInst *) { return true; }
373   static inline bool classof(const Instruction *I) {
374     return I->getOpcode() == Instruction::Store;
375   }
376   static inline bool classof(const Value *V) {
377     return isa<Instruction>(V) && classof(cast<Instruction>(V));
378   }
379 };
380
381 template <>
382 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
383 };
384
385 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
386
387 //===----------------------------------------------------------------------===//
388 //                             GetElementPtrInst Class
389 //===----------------------------------------------------------------------===//
390
391 // checkType - Simple wrapper function to give a better assertion failure
392 // message on bad indexes for a gep instruction.
393 //
394 static inline const Type *checkType(const Type *Ty) {
395   assert(Ty && "Invalid GetElementPtrInst indices for type!");
396   return Ty;
397 }
398
399 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
400 /// access elements of arrays and structs
401 ///
402 class GetElementPtrInst : public Instruction {
403   GetElementPtrInst(const GetElementPtrInst &GEPI);
404   void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
405             const std::string &NameStr);
406   void init(Value *Ptr, Value *Idx, const std::string &NameStr);
407
408   template<typename InputIterator>
409   void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
410             const std::string &NameStr,
411             // This argument ensures that we have an iterator we can
412             // do arithmetic on in constant time
413             std::random_access_iterator_tag) {
414     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
415
416     if (NumIdx > 0) {
417       // This requires that the iterator points to contiguous memory.
418       init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
419                                      // we have to build an array here
420     }
421     else {
422       init(Ptr, 0, NumIdx, NameStr);
423     }
424   }
425
426   /// getIndexedType - Returns the type of the element that would be loaded with
427   /// a load instruction with the specified parameters.
428   ///
429   /// Null is returned if the indices are invalid for the specified
430   /// pointer type.
431   ///
432   template<typename InputIterator>
433   static const Type *getIndexedType(const Type *Ptr,
434                                     InputIterator IdxBegin,
435                                     InputIterator IdxEnd,
436                                     // This argument ensures that we
437                                     // have an iterator we can do
438                                     // arithmetic on in constant time
439                                     std::random_access_iterator_tag) {
440     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
441
442     if (NumIdx > 0)
443       // This requires that the iterator points to contiguous memory.
444       return getIndexedType(Ptr, &*IdxBegin, NumIdx);
445     else
446       return getIndexedType(Ptr, (Value *const*)0, NumIdx);
447   }
448
449   /// Constructors - Create a getelementptr instruction with a base pointer an
450   /// list of indices.  The first ctor can optionally insert before an existing
451   /// instruction, the second appends the new instruction to the specified
452   /// BasicBlock.
453   template<typename InputIterator>
454   inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
455                            InputIterator IdxEnd,
456                            unsigned Values,
457                            const std::string &NameStr,
458                            Instruction *InsertBefore);
459   template<typename InputIterator>
460   inline GetElementPtrInst(Value *Ptr,
461                            InputIterator IdxBegin, InputIterator IdxEnd,
462                            unsigned Values,
463                            const std::string &NameStr, BasicBlock *InsertAtEnd);
464
465   /// Constructors - These two constructors are convenience methods because one
466   /// and two index getelementptr instructions are so common.
467   GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
468                     Instruction *InsertBefore = 0);
469   GetElementPtrInst(Value *Ptr, Value *Idx,
470                     const std::string &NameStr, BasicBlock *InsertAtEnd);
471 public:
472   template<typename InputIterator>
473   static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
474                                    InputIterator IdxEnd,
475                                    const std::string &NameStr = "",
476                                    Instruction *InsertBefore = 0) {
477     typename std::iterator_traits<InputIterator>::difference_type Values =
478       1 + std::distance(IdxBegin, IdxEnd);
479     return new(Values)
480       GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
481   }
482   template<typename InputIterator>
483   static GetElementPtrInst *Create(Value *Ptr,
484                                    InputIterator IdxBegin, InputIterator IdxEnd,
485                                    const std::string &NameStr,
486                                    BasicBlock *InsertAtEnd) {
487     typename std::iterator_traits<InputIterator>::difference_type Values =
488       1 + std::distance(IdxBegin, IdxEnd);
489     return new(Values)
490       GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
491   }
492
493   /// Constructors - These two creators are convenience methods because one
494   /// index getelementptr instructions are so common.
495   static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
496                                    const std::string &NameStr = "",
497                                    Instruction *InsertBefore = 0) {
498     return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
499   }
500   static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
501                                    const std::string &NameStr,
502                                    BasicBlock *InsertAtEnd) {
503     return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
504   }
505
506   virtual GetElementPtrInst *clone(LLVMContext &Context) const;
507
508   /// Transparently provide more efficient getOperand methods.
509   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
510
511   // getType - Overload to return most specific pointer type...
512   const PointerType *getType() const {
513     return reinterpret_cast<const PointerType*>(Instruction::getType());
514   }
515
516   /// getIndexedType - Returns the type of the element that would be loaded with
517   /// a load instruction with the specified parameters.
518   ///
519   /// Null is returned if the indices are invalid for the specified
520   /// pointer type.
521   ///
522   template<typename InputIterator>
523   static const Type *getIndexedType(const Type *Ptr,
524                                     InputIterator IdxBegin,
525                                     InputIterator IdxEnd) {
526     return getIndexedType(Ptr, IdxBegin, IdxEnd,
527                           typename std::iterator_traits<InputIterator>::
528                           iterator_category());
529   }
530
531   static const Type *getIndexedType(const Type *Ptr,
532                                     Value* const *Idx, unsigned NumIdx);
533
534   static const Type *getIndexedType(const Type *Ptr,
535                                     uint64_t const *Idx, unsigned NumIdx);
536
537   static const Type *getIndexedType(const Type *Ptr, Value *Idx);
538
539   inline op_iterator       idx_begin()       { return op_begin()+1; }
540   inline const_op_iterator idx_begin() const { return op_begin()+1; }
541   inline op_iterator       idx_end()         { return op_end(); }
542   inline const_op_iterator idx_end()   const { return op_end(); }
543
544   Value *getPointerOperand() {
545     return getOperand(0);
546   }
547   const Value *getPointerOperand() const {
548     return getOperand(0);
549   }
550   static unsigned getPointerOperandIndex() {
551     return 0U;                      // get index for modifying correct operand
552   }
553
554   /// getPointerOperandType - Method to return the pointer operand as a
555   /// PointerType.
556   const PointerType *getPointerOperandType() const {
557     return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
558   }
559
560
561   unsigned getNumIndices() const {  // Note: always non-negative
562     return getNumOperands() - 1;
563   }
564
565   bool hasIndices() const {
566     return getNumOperands() > 1;
567   }
568
569   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
570   /// zeros.  If so, the result pointer and the first operand have the same
571   /// value, just potentially different types.
572   bool hasAllZeroIndices() const;
573
574   /// hasAllConstantIndices - Return true if all of the indices of this GEP are
575   /// constant integers.  If so, the result pointer and the first operand have
576   /// a constant offset between them.
577   bool hasAllConstantIndices() const;
578
579
580   // Methods for support type inquiry through isa, cast, and dyn_cast:
581   static inline bool classof(const GetElementPtrInst *) { return true; }
582   static inline bool classof(const Instruction *I) {
583     return (I->getOpcode() == Instruction::GetElementPtr);
584   }
585   static inline bool classof(const Value *V) {
586     return isa<Instruction>(V) && classof(cast<Instruction>(V));
587   }
588 };
589
590 template <>
591 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
592 };
593
594 template<typename InputIterator>
595 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
596                                      InputIterator IdxBegin,
597                                      InputIterator IdxEnd,
598                                      unsigned Values,
599                                      const std::string &NameStr,
600                                      Instruction *InsertBefore)
601   : Instruction(PointerType::get(checkType(
602                                    getIndexedType(Ptr->getType(),
603                                                   IdxBegin, IdxEnd)),
604                                  cast<PointerType>(Ptr->getType())
605                                    ->getAddressSpace()),
606                 GetElementPtr,
607                 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
608                 Values, InsertBefore) {
609   init(Ptr, IdxBegin, IdxEnd, NameStr,
610        typename std::iterator_traits<InputIterator>::iterator_category());
611 }
612 template<typename InputIterator>
613 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
614                                      InputIterator IdxBegin,
615                                      InputIterator IdxEnd,
616                                      unsigned Values,
617                                      const std::string &NameStr,
618                                      BasicBlock *InsertAtEnd)
619   : Instruction(PointerType::get(checkType(
620                                    getIndexedType(Ptr->getType(),
621                                                   IdxBegin, IdxEnd)),
622                                  cast<PointerType>(Ptr->getType())
623                                    ->getAddressSpace()),
624                 GetElementPtr,
625                 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
626                 Values, InsertAtEnd) {
627   init(Ptr, IdxBegin, IdxEnd, NameStr,
628        typename std::iterator_traits<InputIterator>::iterator_category());
629 }
630
631
632 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
633
634
635 //===----------------------------------------------------------------------===//
636 //                               ICmpInst Class
637 //===----------------------------------------------------------------------===//
638
639 /// This instruction compares its operands according to the predicate given
640 /// to the constructor. It only operates on integers or pointers. The operands
641 /// must be identical types.
642 /// @brief Represent an integer comparison operator.
643 class ICmpInst: public CmpInst {
644 public:
645   /// @brief Constructor with insert-before-instruction semantics.
646   ICmpInst(
647     Instruction *InsertBefore,  ///< Where to insert
648     Predicate pred,  ///< The predicate to use for the comparison
649     Value *LHS,      ///< The left-hand-side of the expression
650     Value *RHS,      ///< The right-hand-side of the expression
651     const std::string &NameStr = ""  ///< Name of the instruction
652   ) : CmpInst(InsertBefore->getParent()->getContext()->
653                 makeCmpResultType(LHS->getType()),
654               Instruction::ICmp, pred, LHS, RHS, NameStr,
655               InsertBefore) {
656     assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
657            pred <= CmpInst::LAST_ICMP_PREDICATE &&
658            "Invalid ICmp predicate value");
659     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
660           "Both operands to ICmp instruction are not of the same type!");
661     // Check that the operands are the right type
662     assert((getOperand(0)->getType()->isIntOrIntVector() ||
663             isa<PointerType>(getOperand(0)->getType())) &&
664            "Invalid operand types for ICmp instruction");
665   }
666
667   /// @brief Constructor with insert-at-end semantics.
668   ICmpInst(
669     BasicBlock &InsertAtEnd, ///< Block to insert into.
670     Predicate pred,  ///< The predicate to use for the comparison
671     Value *LHS,      ///< The left-hand-side of the expression
672     Value *RHS,      ///< The right-hand-side of the expression
673     const std::string &NameStr = ""  ///< Name of the instruction
674   ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
675               Instruction::ICmp, pred, LHS, RHS, NameStr,
676               &InsertAtEnd) {
677     assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
678           pred <= CmpInst::LAST_ICMP_PREDICATE &&
679           "Invalid ICmp predicate value");
680     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
681           "Both operands to ICmp instruction are not of the same type!");
682     // Check that the operands are the right type
683     assert((getOperand(0)->getType()->isIntOrIntVector() ||
684             isa<PointerType>(getOperand(0)->getType())) &&
685            "Invalid operand types for ICmp instruction");
686   }
687
688   /// @brief Constructor with no-insertion semantics
689   ICmpInst(
690     LLVMContext &Context, ///< Context to construct within
691     Predicate pred, ///< The predicate to use for the comparison
692     Value *LHS,     ///< The left-hand-side of the expression
693     Value *RHS,     ///< The right-hand-side of the expression
694     const std::string &NameStr = "" ///< Name of the instruction
695   ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
696               Instruction::ICmp, pred, LHS, RHS, NameStr) {
697     assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
698            pred <= CmpInst::LAST_ICMP_PREDICATE &&
699            "Invalid ICmp predicate value");
700     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
701           "Both operands to ICmp instruction are not of the same type!");
702     // Check that the operands are the right type
703     assert((getOperand(0)->getType()->isIntOrIntVector() ||
704             isa<PointerType>(getOperand(0)->getType())) &&
705            "Invalid operand types for ICmp instruction");
706   }
707
708   /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
709   /// @returns the predicate that would be the result if the operand were
710   /// regarded as signed.
711   /// @brief Return the signed version of the predicate
712   Predicate getSignedPredicate() const {
713     return getSignedPredicate(getPredicate());
714   }
715
716   /// This is a static version that you can use without an instruction.
717   /// @brief Return the signed version of the predicate.
718   static Predicate getSignedPredicate(Predicate pred);
719
720   /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
721   /// @returns the predicate that would be the result if the operand were
722   /// regarded as unsigned.
723   /// @brief Return the unsigned version of the predicate
724   Predicate getUnsignedPredicate() const {
725     return getUnsignedPredicate(getPredicate());
726   }
727
728   /// This is a static version that you can use without an instruction.
729   /// @brief Return the unsigned version of the predicate.
730   static Predicate getUnsignedPredicate(Predicate pred);
731
732   /// isEquality - Return true if this predicate is either EQ or NE.  This also
733   /// tests for commutativity.
734   static bool isEquality(Predicate P) {
735     return P == ICMP_EQ || P == ICMP_NE;
736   }
737
738   /// isEquality - Return true if this predicate is either EQ or NE.  This also
739   /// tests for commutativity.
740   bool isEquality() const {
741     return isEquality(getPredicate());
742   }
743
744   /// @returns true if the predicate of this ICmpInst is commutative
745   /// @brief Determine if this relation is commutative.
746   bool isCommutative() const { return isEquality(); }
747
748   /// isRelational - Return true if the predicate is relational (not EQ or NE).
749   ///
750   bool isRelational() const {
751     return !isEquality();
752   }
753
754   /// isRelational - Return true if the predicate is relational (not EQ or NE).
755   ///
756   static bool isRelational(Predicate P) {
757     return !isEquality(P);
758   }
759
760   /// @returns true if the predicate of this ICmpInst is signed, false otherwise
761   /// @brief Determine if this instruction's predicate is signed.
762   bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
763
764   /// @returns true if the predicate provided is signed, false otherwise
765   /// @brief Determine if the predicate is signed.
766   static bool isSignedPredicate(Predicate pred);
767
768   /// @returns true if the specified compare predicate is
769   /// true when both operands are equal...
770   /// @brief Determine if the icmp is true when both operands are equal
771   static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
772     return pred == ICmpInst::ICMP_EQ  || pred == ICmpInst::ICMP_UGE ||
773            pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
774            pred == ICmpInst::ICMP_SLE;
775   }
776
777   /// @returns true if the specified compare instruction is
778   /// true when both operands are equal...
779   /// @brief Determine if the ICmpInst returns true when both operands are equal
780   bool isTrueWhenEqual() {
781     return isTrueWhenEqual(getPredicate());
782   }
783
784   /// Initialize a set of values that all satisfy the predicate with C.
785   /// @brief Make a ConstantRange for a relation with a constant value.
786   static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
787
788   /// Exchange the two operands to this instruction in such a way that it does
789   /// not modify the semantics of the instruction. The predicate value may be
790   /// changed to retain the same result if the predicate is order dependent
791   /// (e.g. ult).
792   /// @brief Swap operands and adjust predicate.
793   void swapOperands() {
794     SubclassData = getSwappedPredicate();
795     Op<0>().swap(Op<1>());
796   }
797
798   virtual ICmpInst *clone(LLVMContext &Context) const;
799
800   // Methods for support type inquiry through isa, cast, and dyn_cast:
801   static inline bool classof(const ICmpInst *) { return true; }
802   static inline bool classof(const Instruction *I) {
803     return I->getOpcode() == Instruction::ICmp;
804   }
805   static inline bool classof(const Value *V) {
806     return isa<Instruction>(V) && classof(cast<Instruction>(V));
807   }
808
809 };
810
811 //===----------------------------------------------------------------------===//
812 //                               FCmpInst Class
813 //===----------------------------------------------------------------------===//
814
815 /// This instruction compares its operands according to the predicate given
816 /// to the constructor. It only operates on floating point values or packed
817 /// vectors of floating point values. The operands must be identical types.
818 /// @brief Represents a floating point comparison operator.
819 class FCmpInst: public CmpInst {
820 public:
821   /// @brief Constructor with insert-before-instruction semantics.
822   FCmpInst(
823     Instruction *InsertBefore, ///< Where to insert
824     Predicate pred,  ///< The predicate to use for the comparison
825     Value *LHS,      ///< The left-hand-side of the expression
826     Value *RHS,      ///< The right-hand-side of the expression
827     const std::string &NameStr = ""  ///< Name of the instruction
828   ) : CmpInst(InsertBefore->getParent()->getContext()->
829                 makeCmpResultType(LHS->getType()),
830               Instruction::FCmp, pred, LHS, RHS, NameStr,
831               InsertBefore) {
832     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
833            "Invalid FCmp predicate value");
834     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
835            "Both operands to FCmp instruction are not of the same type!");
836     // Check that the operands are the right type
837     assert(getOperand(0)->getType()->isFPOrFPVector() &&
838            "Invalid operand types for FCmp instruction");
839   }
840   
841   /// @brief Constructor with insert-at-end semantics.
842   FCmpInst(
843     BasicBlock &InsertAtEnd, ///< Block to insert into.
844     Predicate pred,  ///< The predicate to use for the comparison
845     Value *LHS,      ///< The left-hand-side of the expression
846     Value *RHS,      ///< The right-hand-side of the expression
847     const std::string &NameStr = ""  ///< Name of the instruction
848   ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
849               Instruction::FCmp, pred, LHS, RHS, NameStr,
850               &InsertAtEnd) {
851     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
852            "Invalid FCmp predicate value");
853     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
854            "Both operands to FCmp instruction are not of the same type!");
855     // Check that the operands are the right type
856     assert(getOperand(0)->getType()->isFPOrFPVector() &&
857            "Invalid operand types for FCmp instruction");
858   }
859
860   /// @brief Constructor with no-insertion semantics
861   FCmpInst(
862     LLVMContext &Context, ///< Context to build in
863     Predicate pred, ///< The predicate to use for the comparison
864     Value *LHS,     ///< The left-hand-side of the expression
865     Value *RHS,     ///< The right-hand-side of the expression
866     const std::string &NameStr = "" ///< Name of the instruction
867   ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
868               Instruction::FCmp, pred, LHS, RHS, NameStr) {
869     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
870            "Invalid FCmp predicate value");
871     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
872            "Both operands to FCmp instruction are not of the same type!");
873     // Check that the operands are the right type
874     assert(getOperand(0)->getType()->isFPOrFPVector() &&
875            "Invalid operand types for FCmp instruction");
876   }
877
878   /// @returns true if the predicate of this instruction is EQ or NE.
879   /// @brief Determine if this is an equality predicate.
880   bool isEquality() const {
881     return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
882            SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
883   }
884
885   /// @returns true if the predicate of this instruction is commutative.
886   /// @brief Determine if this is a commutative predicate.
887   bool isCommutative() const {
888     return isEquality() ||
889            SubclassData == FCMP_FALSE ||
890            SubclassData == FCMP_TRUE ||
891            SubclassData == FCMP_ORD ||
892            SubclassData == FCMP_UNO;
893   }
894
895   /// @returns true if the predicate is relational (not EQ or NE).
896   /// @brief Determine if this a relational predicate.
897   bool isRelational() const { return !isEquality(); }
898
899   /// Exchange the two operands to this instruction in such a way that it does
900   /// not modify the semantics of the instruction. The predicate value may be
901   /// changed to retain the same result if the predicate is order dependent
902   /// (e.g. ult).
903   /// @brief Swap operands and adjust predicate.
904   void swapOperands() {
905     SubclassData = getSwappedPredicate();
906     Op<0>().swap(Op<1>());
907   }
908
909   virtual FCmpInst *clone(LLVMContext &Context) const;
910
911   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
912   static inline bool classof(const FCmpInst *) { return true; }
913   static inline bool classof(const Instruction *I) {
914     return I->getOpcode() == Instruction::FCmp;
915   }
916   static inline bool classof(const Value *V) {
917     return isa<Instruction>(V) && classof(cast<Instruction>(V));
918   }
919
920 };
921
922 //===----------------------------------------------------------------------===//
923 //                                 CallInst Class
924 //===----------------------------------------------------------------------===//
925 /// CallInst - This class represents a function call, abstracting a target
926 /// machine's calling convention.  This class uses low bit of the SubClassData
927 /// field to indicate whether or not this is a tail call.  The rest of the bits
928 /// hold the calling convention of the call.
929 ///
930
931 class CallInst : public Instruction {
932   AttrListPtr AttributeList; ///< parameter attributes for call
933   CallInst(const CallInst &CI);
934   void init(Value *Func, Value* const *Params, unsigned NumParams);
935   void init(Value *Func, Value *Actual1, Value *Actual2);
936   void init(Value *Func, Value *Actual);
937   void init(Value *Func);
938
939   template<typename InputIterator>
940   void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
941             const std::string &NameStr,
942             // This argument ensures that we have an iterator we can
943             // do arithmetic on in constant time
944             std::random_access_iterator_tag) {
945     unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
946
947     // This requires that the iterator points to contiguous memory.
948     init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
949     setName(NameStr);
950   }
951
952   /// Construct a CallInst given a range of arguments.  InputIterator
953   /// must be a random-access iterator pointing to contiguous storage
954   /// (e.g. a std::vector<>::iterator).  Checks are made for
955   /// random-accessness but not for contiguous storage as that would
956   /// incur runtime overhead.
957   /// @brief Construct a CallInst from a range of arguments
958   template<typename InputIterator>
959   CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
960            const std::string &NameStr, Instruction *InsertBefore);
961
962   /// Construct a CallInst given a range of arguments.  InputIterator
963   /// must be a random-access iterator pointing to contiguous storage
964   /// (e.g. a std::vector<>::iterator).  Checks are made for
965   /// random-accessness but not for contiguous storage as that would
966   /// incur runtime overhead.
967   /// @brief Construct a CallInst from a range of arguments
968   template<typename InputIterator>
969   inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
970                   const std::string &NameStr, BasicBlock *InsertAtEnd);
971
972   CallInst(Value *F, Value *Actual, const std::string& NameStr,
973            Instruction *InsertBefore);
974   CallInst(Value *F, Value *Actual, const std::string& NameStr,
975            BasicBlock *InsertAtEnd);
976   explicit CallInst(Value *F, const std::string &NameStr,
977                     Instruction *InsertBefore);
978   CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
979 public:
980   template<typename InputIterator>
981   static CallInst *Create(Value *Func,
982                           InputIterator ArgBegin, InputIterator ArgEnd,
983                           const std::string &NameStr = "",
984                           Instruction *InsertBefore = 0) {
985     return new((unsigned)(ArgEnd - ArgBegin + 1))
986       CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
987   }
988   template<typename InputIterator>
989   static CallInst *Create(Value *Func,
990                           InputIterator ArgBegin, InputIterator ArgEnd,
991                           const std::string &NameStr, BasicBlock *InsertAtEnd) {
992     return new((unsigned)(ArgEnd - ArgBegin + 1))
993       CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
994   }
995   static CallInst *Create(Value *F, Value *Actual,
996                           const std::string& NameStr = "",
997                           Instruction *InsertBefore = 0) {
998     return new(2) CallInst(F, Actual, NameStr, InsertBefore);
999   }
1000   static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
1001                           BasicBlock *InsertAtEnd) {
1002     return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1003   }
1004   static CallInst *Create(Value *F, const std::string &NameStr = "",
1005                           Instruction *InsertBefore = 0) {
1006     return new(1) CallInst(F, NameStr, InsertBefore);
1007   }
1008   static CallInst *Create(Value *F, const std::string &NameStr,
1009                           BasicBlock *InsertAtEnd) {
1010     return new(1) CallInst(F, NameStr, InsertAtEnd);
1011   }
1012
1013   ~CallInst();
1014
1015   bool isTailCall() const           { return SubclassData & 1; }
1016   void setTailCall(bool isTC = true) {
1017     SubclassData = (SubclassData & ~1) | unsigned(isTC);
1018   }
1019
1020   virtual CallInst *clone(LLVMContext &Context) const;
1021
1022   /// Provide fast operand accessors
1023   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1024
1025   /// getCallingConv/setCallingConv - Get or set the calling convention of this
1026   /// function call.
1027   unsigned getCallingConv() const { return SubclassData >> 1; }
1028   void setCallingConv(unsigned CC) {
1029     SubclassData = (SubclassData & 1) | (CC << 1);
1030   }
1031
1032   /// getAttributes - Return the parameter attributes for this call.
1033   ///
1034   const AttrListPtr &getAttributes() const { return AttributeList; }
1035
1036   /// setAttributes - Set the parameter attributes for this call.
1037   ///
1038   void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1039
1040   /// addAttribute - adds the attribute to the list of attributes.
1041   void addAttribute(unsigned i, Attributes attr);
1042
1043   /// removeAttribute - removes the attribute from the list of attributes.
1044   void removeAttribute(unsigned i, Attributes attr);
1045
1046   /// @brief Determine whether the call or the callee has the given attribute.
1047   bool paramHasAttr(unsigned i, Attributes attr) const;
1048
1049   /// @brief Extract the alignment for a call or parameter (0=unknown).
1050   unsigned getParamAlignment(unsigned i) const {
1051     return AttributeList.getParamAlignment(i);
1052   }
1053
1054   /// @brief Determine if the call does not access memory.
1055   bool doesNotAccessMemory() const {
1056     return paramHasAttr(~0, Attribute::ReadNone);
1057   }
1058   void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1059     if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1060     else removeAttribute(~0, Attribute::ReadNone);
1061   }
1062
1063   /// @brief Determine if the call does not access or only reads memory.
1064   bool onlyReadsMemory() const {
1065     return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1066   }
1067   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1068     if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1069     else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1070   }
1071
1072   /// @brief Determine if the call cannot return.
1073   bool doesNotReturn() const {
1074     return paramHasAttr(~0, Attribute::NoReturn);
1075   }
1076   void setDoesNotReturn(bool DoesNotReturn = true) {
1077     if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1078     else removeAttribute(~0, Attribute::NoReturn);
1079   }
1080
1081   /// @brief Determine if the call cannot unwind.
1082   bool doesNotThrow() const {
1083     return paramHasAttr(~0, Attribute::NoUnwind);
1084   }
1085   void setDoesNotThrow(bool DoesNotThrow = true) {
1086     if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1087     else removeAttribute(~0, Attribute::NoUnwind);
1088   }
1089
1090   /// @brief Determine if the call returns a structure through first
1091   /// pointer argument.
1092   bool hasStructRetAttr() const {
1093     // Be friendly and also check the callee.
1094     return paramHasAttr(1, Attribute::StructRet);
1095   }
1096
1097   /// @brief Determine if any call argument is an aggregate passed by value.
1098   bool hasByValArgument() const {
1099     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1100   }
1101
1102   /// getCalledFunction - Return the function called, or null if this is an
1103   /// indirect function invocation.
1104   ///
1105   Function *getCalledFunction() const {
1106     return dyn_cast<Function>(Op<0>());
1107   }
1108
1109   /// getCalledValue - Get a pointer to the function that is invoked by this
1110   /// instruction
1111   const Value *getCalledValue() const { return Op<0>(); }
1112         Value *getCalledValue()       { return Op<0>(); }
1113
1114   // Methods for support type inquiry through isa, cast, and dyn_cast:
1115   static inline bool classof(const CallInst *) { return true; }
1116   static inline bool classof(const Instruction *I) {
1117     return I->getOpcode() == Instruction::Call;
1118   }
1119   static inline bool classof(const Value *V) {
1120     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1121   }
1122 };
1123
1124 template <>
1125 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1126 };
1127
1128 template<typename InputIterator>
1129 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1130                    const std::string &NameStr, BasicBlock *InsertAtEnd)
1131   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1132                                    ->getElementType())->getReturnType(),
1133                 Instruction::Call,
1134                 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1135                 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1136   init(Func, ArgBegin, ArgEnd, NameStr,
1137        typename std::iterator_traits<InputIterator>::iterator_category());
1138 }
1139
1140 template<typename InputIterator>
1141 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1142                    const std::string &NameStr, Instruction *InsertBefore)
1143   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1144                                    ->getElementType())->getReturnType(),
1145                 Instruction::Call,
1146                 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1147                 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1148   init(Func, ArgBegin, ArgEnd, NameStr,
1149        typename std::iterator_traits<InputIterator>::iterator_category());
1150 }
1151
1152 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1153
1154 //===----------------------------------------------------------------------===//
1155 //                               SelectInst Class
1156 //===----------------------------------------------------------------------===//
1157
1158 /// SelectInst - This class represents the LLVM 'select' instruction.
1159 ///
1160 class SelectInst : public Instruction {
1161   void init(Value *C, Value *S1, Value *S2) {
1162     assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1163     Op<0>() = C;
1164     Op<1>() = S1;
1165     Op<2>() = S2;
1166   }
1167
1168   SelectInst(const SelectInst &SI)
1169     : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1170     init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1171   }
1172   SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1173              Instruction *InsertBefore)
1174     : Instruction(S1->getType(), Instruction::Select,
1175                   &Op<0>(), 3, InsertBefore) {
1176     init(C, S1, S2);
1177     setName(NameStr);
1178   }
1179   SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1180              BasicBlock *InsertAtEnd)
1181     : Instruction(S1->getType(), Instruction::Select,
1182                   &Op<0>(), 3, InsertAtEnd) {
1183     init(C, S1, S2);
1184     setName(NameStr);
1185   }
1186 public:
1187   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1188                             const std::string &NameStr = "",
1189                             Instruction *InsertBefore = 0) {
1190     return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1191   }
1192   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1193                             const std::string &NameStr,
1194                             BasicBlock *InsertAtEnd) {
1195     return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1196   }
1197
1198   Value *getCondition() const { return Op<0>(); }
1199   Value *getTrueValue() const { return Op<1>(); }
1200   Value *getFalseValue() const { return Op<2>(); }
1201
1202   /// areInvalidOperands - Return a string if the specified operands are invalid
1203   /// for a select operation, otherwise return null.
1204   static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1205
1206   /// Transparently provide more efficient getOperand methods.
1207   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1208
1209   OtherOps getOpcode() const {
1210     return static_cast<OtherOps>(Instruction::getOpcode());
1211   }
1212
1213   virtual SelectInst *clone(LLVMContext &Context) const;
1214
1215   // Methods for support type inquiry through isa, cast, and dyn_cast:
1216   static inline bool classof(const SelectInst *) { return true; }
1217   static inline bool classof(const Instruction *I) {
1218     return I->getOpcode() == Instruction::Select;
1219   }
1220   static inline bool classof(const Value *V) {
1221     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1222   }
1223 };
1224
1225 template <>
1226 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1227 };
1228
1229 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1230
1231 //===----------------------------------------------------------------------===//
1232 //                                VAArgInst Class
1233 //===----------------------------------------------------------------------===//
1234
1235 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1236 /// an argument of the specified type given a va_list and increments that list
1237 ///
1238 class VAArgInst : public UnaryInstruction {
1239   VAArgInst(const VAArgInst &VAA)
1240     : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1241 public:
1242   VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1243              Instruction *InsertBefore = 0)
1244     : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1245     setName(NameStr);
1246   }
1247   VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1248             BasicBlock *InsertAtEnd)
1249     : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1250     setName(NameStr);
1251   }
1252
1253   virtual VAArgInst *clone(LLVMContext &Context) const;
1254
1255   // Methods for support type inquiry through isa, cast, and dyn_cast:
1256   static inline bool classof(const VAArgInst *) { return true; }
1257   static inline bool classof(const Instruction *I) {
1258     return I->getOpcode() == VAArg;
1259   }
1260   static inline bool classof(const Value *V) {
1261     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1262   }
1263 };
1264
1265 //===----------------------------------------------------------------------===//
1266 //                                ExtractElementInst Class
1267 //===----------------------------------------------------------------------===//
1268
1269 /// ExtractElementInst - This instruction extracts a single (scalar)
1270 /// element from a VectorType value
1271 ///
1272 class ExtractElementInst : public Instruction {
1273   ExtractElementInst(const ExtractElementInst &EE) :
1274     Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1275     Op<0>() = EE.Op<0>();
1276     Op<1>() = EE.Op<1>();
1277   }
1278
1279 public:
1280   // allocate space for exactly two operands
1281   void *operator new(size_t s) {
1282     return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1283   }
1284   ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1285                      Instruction *InsertBefore = 0);
1286   ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1287                      BasicBlock *InsertAtEnd);
1288
1289   /// isValidOperands - Return true if an extractelement instruction can be
1290   /// formed with the specified operands.
1291   static bool isValidOperands(const Value *Vec, const Value *Idx);
1292
1293   virtual ExtractElementInst *clone(LLVMContext &Context) const;
1294
1295   /// Transparently provide more efficient getOperand methods.
1296   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1297
1298   // Methods for support type inquiry through isa, cast, and dyn_cast:
1299   static inline bool classof(const ExtractElementInst *) { return true; }
1300   static inline bool classof(const Instruction *I) {
1301     return I->getOpcode() == Instruction::ExtractElement;
1302   }
1303   static inline bool classof(const Value *V) {
1304     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1305   }
1306 };
1307
1308 template <>
1309 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1310 };
1311
1312 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1313
1314 //===----------------------------------------------------------------------===//
1315 //                                InsertElementInst Class
1316 //===----------------------------------------------------------------------===//
1317
1318 /// InsertElementInst - This instruction inserts a single (scalar)
1319 /// element into a VectorType value
1320 ///
1321 class InsertElementInst : public Instruction {
1322   InsertElementInst(const InsertElementInst &IE);
1323   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1324                     const std::string &NameStr = "",
1325                     Instruction *InsertBefore = 0);
1326   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1327                     const std::string &NameStr, BasicBlock *InsertAtEnd);
1328 public:
1329   static InsertElementInst *Create(const InsertElementInst &IE) {
1330     return new(IE.getNumOperands()) InsertElementInst(IE);
1331   }
1332   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1333                                    const std::string &NameStr = "",
1334                                    Instruction *InsertBefore = 0) {
1335     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1336   }
1337   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1338                                    const std::string &NameStr,
1339                                    BasicBlock *InsertAtEnd) {
1340     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1341   }
1342
1343   /// isValidOperands - Return true if an insertelement instruction can be
1344   /// formed with the specified operands.
1345   static bool isValidOperands(const Value *Vec, const Value *NewElt,
1346                               const Value *Idx);
1347
1348   virtual InsertElementInst *clone(LLVMContext &Context) const;
1349
1350   /// getType - Overload to return most specific vector type.
1351   ///
1352   const VectorType *getType() const {
1353     return reinterpret_cast<const VectorType*>(Instruction::getType());
1354   }
1355
1356   /// Transparently provide more efficient getOperand methods.
1357   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1358
1359   // Methods for support type inquiry through isa, cast, and dyn_cast:
1360   static inline bool classof(const InsertElementInst *) { return true; }
1361   static inline bool classof(const Instruction *I) {
1362     return I->getOpcode() == Instruction::InsertElement;
1363   }
1364   static inline bool classof(const Value *V) {
1365     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1366   }
1367 };
1368
1369 template <>
1370 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1371 };
1372
1373 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1374
1375 //===----------------------------------------------------------------------===//
1376 //                           ShuffleVectorInst Class
1377 //===----------------------------------------------------------------------===//
1378
1379 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1380 /// input vectors.
1381 ///
1382 class ShuffleVectorInst : public Instruction {
1383   ShuffleVectorInst(const ShuffleVectorInst &IE);
1384 public:
1385   // allocate space for exactly three operands
1386   void *operator new(size_t s) {
1387     return User::operator new(s, 3);
1388   }
1389   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1390                     const std::string &NameStr = "",
1391                     Instruction *InsertBefor = 0);
1392   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1393                     const std::string &NameStr, BasicBlock *InsertAtEnd);
1394
1395   /// isValidOperands - Return true if a shufflevector instruction can be
1396   /// formed with the specified operands.
1397   static bool isValidOperands(const Value *V1, const Value *V2,
1398                               const Value *Mask);
1399
1400   virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1401
1402   /// getType - Overload to return most specific vector type.
1403   ///
1404   const VectorType *getType() const {
1405     return reinterpret_cast<const VectorType*>(Instruction::getType());
1406   }
1407
1408   /// Transparently provide more efficient getOperand methods.
1409   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1410
1411   /// getMaskValue - Return the index from the shuffle mask for the specified
1412   /// output result.  This is either -1 if the element is undef or a number less
1413   /// than 2*numelements.
1414   int getMaskValue(unsigned i) const;
1415
1416   // Methods for support type inquiry through isa, cast, and dyn_cast:
1417   static inline bool classof(const ShuffleVectorInst *) { return true; }
1418   static inline bool classof(const Instruction *I) {
1419     return I->getOpcode() == Instruction::ShuffleVector;
1420   }
1421   static inline bool classof(const Value *V) {
1422     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1423   }
1424 };
1425
1426 template <>
1427 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1428 };
1429
1430 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1431
1432 //===----------------------------------------------------------------------===//
1433 //                                ExtractValueInst Class
1434 //===----------------------------------------------------------------------===//
1435
1436 /// ExtractValueInst - This instruction extracts a struct member or array
1437 /// element value from an aggregate value.
1438 ///
1439 class ExtractValueInst : public UnaryInstruction {
1440   SmallVector<unsigned, 4> Indices;
1441
1442   ExtractValueInst(const ExtractValueInst &EVI);
1443   void init(const unsigned *Idx, unsigned NumIdx,
1444             const std::string &NameStr);
1445   void init(unsigned Idx, const std::string &NameStr);
1446
1447   template<typename InputIterator>
1448   void init(InputIterator IdxBegin, InputIterator IdxEnd,
1449             const std::string &NameStr,
1450             // This argument ensures that we have an iterator we can
1451             // do arithmetic on in constant time
1452             std::random_access_iterator_tag) {
1453     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1454
1455     // There's no fundamental reason why we require at least one index
1456     // (other than weirdness with &*IdxBegin being invalid; see
1457     // getelementptr's init routine for example). But there's no
1458     // present need to support it.
1459     assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1460
1461     // This requires that the iterator points to contiguous memory.
1462     init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1463                                          // we have to build an array here
1464   }
1465
1466   /// getIndexedType - Returns the type of the element that would be extracted
1467   /// with an extractvalue instruction with the specified parameters.
1468   ///
1469   /// Null is returned if the indices are invalid for the specified
1470   /// pointer type.
1471   ///
1472   static const Type *getIndexedType(const Type *Agg,
1473                                     const unsigned *Idx, unsigned NumIdx);
1474
1475   template<typename InputIterator>
1476   static const Type *getIndexedType(const Type *Ptr,
1477                                     InputIterator IdxBegin,
1478                                     InputIterator IdxEnd,
1479                                     // This argument ensures that we
1480                                     // have an iterator we can do
1481                                     // arithmetic on in constant time
1482                                     std::random_access_iterator_tag) {
1483     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1484
1485     if (NumIdx > 0)
1486       // This requires that the iterator points to contiguous memory.
1487       return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1488     else
1489       return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1490   }
1491
1492   /// Constructors - Create a extractvalue instruction with a base aggregate
1493   /// value and a list of indices.  The first ctor can optionally insert before
1494   /// an existing instruction, the second appends the new instruction to the
1495   /// specified BasicBlock.
1496   template<typename InputIterator>
1497   inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1498                           InputIterator IdxEnd,
1499                           const std::string &NameStr,
1500                           Instruction *InsertBefore);
1501   template<typename InputIterator>
1502   inline ExtractValueInst(Value *Agg,
1503                           InputIterator IdxBegin, InputIterator IdxEnd,
1504                           const std::string &NameStr, BasicBlock *InsertAtEnd);
1505
1506   // allocate space for exactly one operand
1507   void *operator new(size_t s) {
1508     return User::operator new(s, 1);
1509   }
1510
1511 public:
1512   template<typename InputIterator>
1513   static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1514                                   InputIterator IdxEnd,
1515                                   const std::string &NameStr = "",
1516                                   Instruction *InsertBefore = 0) {
1517     return new
1518       ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1519   }
1520   template<typename InputIterator>
1521   static ExtractValueInst *Create(Value *Agg,
1522                                   InputIterator IdxBegin, InputIterator IdxEnd,
1523                                   const std::string &NameStr,
1524                                   BasicBlock *InsertAtEnd) {
1525     return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1526   }
1527
1528   /// Constructors - These two creators are convenience methods because one
1529   /// index extractvalue instructions are much more common than those with
1530   /// more than one.
1531   static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1532                                   const std::string &NameStr = "",
1533                                   Instruction *InsertBefore = 0) {
1534     unsigned Idxs[1] = { Idx };
1535     return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1536   }
1537   static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1538                                   const std::string &NameStr,
1539                                   BasicBlock *InsertAtEnd) {
1540     unsigned Idxs[1] = { Idx };
1541     return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1542   }
1543
1544   virtual ExtractValueInst *clone(LLVMContext &Context) const;
1545
1546   /// getIndexedType - Returns the type of the element that would be extracted
1547   /// with an extractvalue instruction with the specified parameters.
1548   ///
1549   /// Null is returned if the indices are invalid for the specified
1550   /// pointer type.
1551   ///
1552   template<typename InputIterator>
1553   static const Type *getIndexedType(const Type *Ptr,
1554                                     InputIterator IdxBegin,
1555                                     InputIterator IdxEnd) {
1556     return getIndexedType(Ptr, IdxBegin, IdxEnd,
1557                           typename std::iterator_traits<InputIterator>::
1558                           iterator_category());
1559   }
1560   static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1561
1562   typedef const unsigned* idx_iterator;
1563   inline idx_iterator idx_begin() const { return Indices.begin(); }
1564   inline idx_iterator idx_end()   const { return Indices.end(); }
1565
1566   Value *getAggregateOperand() {
1567     return getOperand(0);
1568   }
1569   const Value *getAggregateOperand() const {
1570     return getOperand(0);
1571   }
1572   static unsigned getAggregateOperandIndex() {
1573     return 0U;                      // get index for modifying correct operand
1574   }
1575
1576   unsigned getNumIndices() const {  // Note: always non-negative
1577     return (unsigned)Indices.size();
1578   }
1579
1580   bool hasIndices() const {
1581     return true;
1582   }
1583
1584   // Methods for support type inquiry through isa, cast, and dyn_cast:
1585   static inline bool classof(const ExtractValueInst *) { return true; }
1586   static inline bool classof(const Instruction *I) {
1587     return I->getOpcode() == Instruction::ExtractValue;
1588   }
1589   static inline bool classof(const Value *V) {
1590     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1591   }
1592 };
1593
1594 template<typename InputIterator>
1595 ExtractValueInst::ExtractValueInst(Value *Agg,
1596                                    InputIterator IdxBegin,
1597                                    InputIterator IdxEnd,
1598                                    const std::string &NameStr,
1599                                    Instruction *InsertBefore)
1600   : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1601                                               IdxBegin, IdxEnd)),
1602                      ExtractValue, Agg, InsertBefore) {
1603   init(IdxBegin, IdxEnd, NameStr,
1604        typename std::iterator_traits<InputIterator>::iterator_category());
1605 }
1606 template<typename InputIterator>
1607 ExtractValueInst::ExtractValueInst(Value *Agg,
1608                                    InputIterator IdxBegin,
1609                                    InputIterator IdxEnd,
1610                                    const std::string &NameStr,
1611                                    BasicBlock *InsertAtEnd)
1612   : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1613                                               IdxBegin, IdxEnd)),
1614                      ExtractValue, Agg, InsertAtEnd) {
1615   init(IdxBegin, IdxEnd, NameStr,
1616        typename std::iterator_traits<InputIterator>::iterator_category());
1617 }
1618
1619
1620 //===----------------------------------------------------------------------===//
1621 //                                InsertValueInst Class
1622 //===----------------------------------------------------------------------===//
1623
1624 /// InsertValueInst - This instruction inserts a struct field of array element
1625 /// value into an aggregate value.
1626 ///
1627 class InsertValueInst : public Instruction {
1628   SmallVector<unsigned, 4> Indices;
1629
1630   void *operator new(size_t, unsigned); // Do not implement
1631   InsertValueInst(const InsertValueInst &IVI);
1632   void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1633             const std::string &NameStr);
1634   void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1635
1636   template<typename InputIterator>
1637   void init(Value *Agg, Value *Val,
1638             InputIterator IdxBegin, InputIterator IdxEnd,
1639             const std::string &NameStr,
1640             // This argument ensures that we have an iterator we can
1641             // do arithmetic on in constant time
1642             std::random_access_iterator_tag) {
1643     unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1644
1645     // There's no fundamental reason why we require at least one index
1646     // (other than weirdness with &*IdxBegin being invalid; see
1647     // getelementptr's init routine for example). But there's no
1648     // present need to support it.
1649     assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1650
1651     // This requires that the iterator points to contiguous memory.
1652     init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1653                                               // we have to build an array here
1654   }
1655
1656   /// Constructors - Create a insertvalue instruction with a base aggregate
1657   /// value, a value to insert, and a list of indices.  The first ctor can
1658   /// optionally insert before an existing instruction, the second appends
1659   /// the new instruction to the specified BasicBlock.
1660   template<typename InputIterator>
1661   inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1662                          InputIterator IdxEnd,
1663                          const std::string &NameStr,
1664                          Instruction *InsertBefore);
1665   template<typename InputIterator>
1666   inline InsertValueInst(Value *Agg, Value *Val,
1667                          InputIterator IdxBegin, InputIterator IdxEnd,
1668                          const std::string &NameStr, BasicBlock *InsertAtEnd);
1669
1670   /// Constructors - These two constructors are convenience methods because one
1671   /// and two index insertvalue instructions are so common.
1672   InsertValueInst(Value *Agg, Value *Val,
1673                   unsigned Idx, const std::string &NameStr = "",
1674                   Instruction *InsertBefore = 0);
1675   InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1676                   const std::string &NameStr, BasicBlock *InsertAtEnd);
1677 public:
1678   // allocate space for exactly two operands
1679   void *operator new(size_t s) {
1680     return User::operator new(s, 2);
1681   }
1682
1683   template<typename InputIterator>
1684   static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1685                                  InputIterator IdxEnd,
1686                                  const std::string &NameStr = "",
1687                                  Instruction *InsertBefore = 0) {
1688     return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1689                                NameStr, InsertBefore);
1690   }
1691   template<typename InputIterator>
1692   static InsertValueInst *Create(Value *Agg, Value *Val,
1693                                  InputIterator IdxBegin, InputIterator IdxEnd,
1694                                  const std::string &NameStr,
1695                                  BasicBlock *InsertAtEnd) {
1696     return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1697                                NameStr, InsertAtEnd);
1698   }
1699
1700   /// Constructors - These two creators are convenience methods because one
1701   /// index insertvalue instructions are much more common than those with
1702   /// more than one.
1703   static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1704                                  const std::string &NameStr = "",
1705                                  Instruction *InsertBefore = 0) {
1706     return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1707   }
1708   static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1709                                  const std::string &NameStr,
1710                                  BasicBlock *InsertAtEnd) {
1711     return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1712   }
1713
1714   virtual InsertValueInst *clone(LLVMContext &Context) const;
1715
1716   /// Transparently provide more efficient getOperand methods.
1717   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1718
1719   typedef const unsigned* idx_iterator;
1720   inline idx_iterator idx_begin() const { return Indices.begin(); }
1721   inline idx_iterator idx_end()   const { return Indices.end(); }
1722
1723   Value *getAggregateOperand() {
1724     return getOperand(0);
1725   }
1726   const Value *getAggregateOperand() const {
1727     return getOperand(0);
1728   }
1729   static unsigned getAggregateOperandIndex() {
1730     return 0U;                      // get index for modifying correct operand
1731   }
1732
1733   Value *getInsertedValueOperand() {
1734     return getOperand(1);
1735   }
1736   const Value *getInsertedValueOperand() const {
1737     return getOperand(1);
1738   }
1739   static unsigned getInsertedValueOperandIndex() {
1740     return 1U;                      // get index for modifying correct operand
1741   }
1742
1743   unsigned getNumIndices() const {  // Note: always non-negative
1744     return (unsigned)Indices.size();
1745   }
1746
1747   bool hasIndices() const {
1748     return true;
1749   }
1750
1751   // Methods for support type inquiry through isa, cast, and dyn_cast:
1752   static inline bool classof(const InsertValueInst *) { return true; }
1753   static inline bool classof(const Instruction *I) {
1754     return I->getOpcode() == Instruction::InsertValue;
1755   }
1756   static inline bool classof(const Value *V) {
1757     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1758   }
1759 };
1760
1761 template <>
1762 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1763 };
1764
1765 template<typename InputIterator>
1766 InsertValueInst::InsertValueInst(Value *Agg,
1767                                  Value *Val,
1768                                  InputIterator IdxBegin,
1769                                  InputIterator IdxEnd,
1770                                  const std::string &NameStr,
1771                                  Instruction *InsertBefore)
1772   : Instruction(Agg->getType(), InsertValue,
1773                 OperandTraits<InsertValueInst>::op_begin(this),
1774                 2, InsertBefore) {
1775   init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1776        typename std::iterator_traits<InputIterator>::iterator_category());
1777 }
1778 template<typename InputIterator>
1779 InsertValueInst::InsertValueInst(Value *Agg,
1780                                  Value *Val,
1781                                  InputIterator IdxBegin,
1782                                  InputIterator IdxEnd,
1783                                  const std::string &NameStr,
1784                                  BasicBlock *InsertAtEnd)
1785   : Instruction(Agg->getType(), InsertValue,
1786                 OperandTraits<InsertValueInst>::op_begin(this),
1787                 2, InsertAtEnd) {
1788   init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1789        typename std::iterator_traits<InputIterator>::iterator_category());
1790 }
1791
1792 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1793
1794 //===----------------------------------------------------------------------===//
1795 //                               PHINode Class
1796 //===----------------------------------------------------------------------===//
1797
1798 // PHINode - The PHINode class is used to represent the magical mystical PHI
1799 // node, that can not exist in nature, but can be synthesized in a computer
1800 // scientist's overactive imagination.
1801 //
1802 class PHINode : public Instruction {
1803   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
1804   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
1805   /// the number actually in use.
1806   unsigned ReservedSpace;
1807   PHINode(const PHINode &PN);
1808   // allocate space for exactly zero operands
1809   void *operator new(size_t s) {
1810     return User::operator new(s, 0);
1811   }
1812   explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1813                    Instruction *InsertBefore = 0)
1814     : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1815       ReservedSpace(0) {
1816     setName(NameStr);
1817   }
1818
1819   PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1820     : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1821       ReservedSpace(0) {
1822     setName(NameStr);
1823   }
1824 public:
1825   static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1826                          Instruction *InsertBefore = 0) {
1827     return new PHINode(Ty, NameStr, InsertBefore);
1828   }
1829   static PHINode *Create(const Type *Ty, const std::string &NameStr,
1830                          BasicBlock *InsertAtEnd) {
1831     return new PHINode(Ty, NameStr, InsertAtEnd);
1832   }
1833   ~PHINode();
1834
1835   /// reserveOperandSpace - This method can be used to avoid repeated
1836   /// reallocation of PHI operand lists by reserving space for the correct
1837   /// number of operands before adding them.  Unlike normal vector reserves,
1838   /// this method can also be used to trim the operand space.
1839   void reserveOperandSpace(unsigned NumValues) {
1840     resizeOperands(NumValues*2);
1841   }
1842
1843   virtual PHINode *clone(LLVMContext &Context) const;
1844
1845   /// Provide fast operand accessors
1846   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1847
1848   /// getNumIncomingValues - Return the number of incoming edges
1849   ///
1850   unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1851
1852   /// getIncomingValue - Return incoming value number x
1853   ///
1854   Value *getIncomingValue(unsigned i) const {
1855     assert(i*2 < getNumOperands() && "Invalid value number!");
1856     return getOperand(i*2);
1857   }
1858   void setIncomingValue(unsigned i, Value *V) {
1859     assert(i*2 < getNumOperands() && "Invalid value number!");
1860     setOperand(i*2, V);
1861   }
1862   static unsigned getOperandNumForIncomingValue(unsigned i) {
1863     return i*2;
1864   }
1865   static unsigned getIncomingValueNumForOperand(unsigned i) {
1866     assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1867     return i/2;
1868   }
1869
1870   /// getIncomingBlock - Return incoming basic block corresponding
1871   /// to value use iterator
1872   ///
1873   template <typename U>
1874   BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1875     assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1876     return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1877   }
1878   /// getIncomingBlock - Return incoming basic block number x
1879   ///
1880   BasicBlock *getIncomingBlock(unsigned i) const {
1881     return static_cast<BasicBlock*>(getOperand(i*2+1));
1882   }
1883   void setIncomingBlock(unsigned i, BasicBlock *BB) {
1884     setOperand(i*2+1, BB);
1885   }
1886   static unsigned getOperandNumForIncomingBlock(unsigned i) {
1887     return i*2+1;
1888   }
1889   static unsigned getIncomingBlockNumForOperand(unsigned i) {
1890     assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1891     return i/2;
1892   }
1893
1894   /// addIncoming - Add an incoming value to the end of the PHI list
1895   ///
1896   void addIncoming(Value *V, BasicBlock *BB) {
1897     assert(V && "PHI node got a null value!");
1898     assert(BB && "PHI node got a null basic block!");
1899     assert(getType() == V->getType() &&
1900            "All operands to PHI node must be the same type as the PHI node!");
1901     unsigned OpNo = NumOperands;
1902     if (OpNo+2 > ReservedSpace)
1903       resizeOperands(0);  // Get more space!
1904     // Initialize some new operands.
1905     NumOperands = OpNo+2;
1906     OperandList[OpNo] = V;
1907     OperandList[OpNo+1] = BB;
1908   }
1909
1910   /// removeIncomingValue - Remove an incoming value.  This is useful if a
1911   /// predecessor basic block is deleted.  The value removed is returned.
1912   ///
1913   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1914   /// is true), the PHI node is destroyed and any uses of it are replaced with
1915   /// dummy values.  The only time there should be zero incoming values to a PHI
1916   /// node is when the block is dead, so this strategy is sound.
1917   ///
1918   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1919
1920   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1921     int Idx = getBasicBlockIndex(BB);
1922     assert(Idx >= 0 && "Invalid basic block argument to remove!");
1923     return removeIncomingValue(Idx, DeletePHIIfEmpty);
1924   }
1925
1926   /// getBasicBlockIndex - Return the first index of the specified basic
1927   /// block in the value list for this PHI.  Returns -1 if no instance.
1928   ///
1929   int getBasicBlockIndex(const BasicBlock *BB) const {
1930     Use *OL = OperandList;
1931     for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1932       if (OL[i+1].get() == BB) return i/2;
1933     return -1;
1934   }
1935
1936   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1937     return getIncomingValue(getBasicBlockIndex(BB));
1938   }
1939
1940   /// hasConstantValue - If the specified PHI node always merges together the
1941   /// same value, return the value, otherwise return null.
1942   ///
1943   Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1944
1945   /// Methods for support type inquiry through isa, cast, and dyn_cast:
1946   static inline bool classof(const PHINode *) { return true; }
1947   static inline bool classof(const Instruction *I) {
1948     return I->getOpcode() == Instruction::PHI;
1949   }
1950   static inline bool classof(const Value *V) {
1951     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1952   }
1953  private:
1954   void resizeOperands(unsigned NumOperands);
1955 };
1956
1957 template <>
1958 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1959 };
1960
1961 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1962
1963
1964 //===----------------------------------------------------------------------===//
1965 //                               ReturnInst Class
1966 //===----------------------------------------------------------------------===//
1967
1968 //===---------------------------------------------------------------------------
1969 /// ReturnInst - Return a value (possibly void), from a function.  Execution
1970 /// does not continue in this function any longer.
1971 ///
1972 class ReturnInst : public TerminatorInst {
1973   ReturnInst(const ReturnInst &RI);
1974
1975 private:
1976   // ReturnInst constructors:
1977   // ReturnInst()                  - 'ret void' instruction
1978   // ReturnInst(    null)          - 'ret void' instruction
1979   // ReturnInst(Value* X)          - 'ret X'    instruction
1980   // ReturnInst(    null, Inst *I) - 'ret void' instruction, insert before I
1981   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
1982   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of B
1983   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of B
1984   //
1985   // NOTE: If the Value* passed is of type void then the constructor behaves as
1986   // if it was passed NULL.
1987   explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1988   ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1989   explicit ReturnInst(BasicBlock *InsertAtEnd);
1990 public:
1991   static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
1992     return new(!!retVal) ReturnInst(retVal, InsertBefore);
1993   }
1994   static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
1995     return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
1996   }
1997   static ReturnInst* Create(BasicBlock *InsertAtEnd) {
1998     return new(0) ReturnInst(InsertAtEnd);
1999   }
2000   virtual ~ReturnInst();
2001
2002   virtual ReturnInst *clone(LLVMContext &Context) const;
2003
2004   /// Provide fast operand accessors
2005   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2006
2007   /// Convenience accessor
2008   Value *getReturnValue(unsigned n = 0) const {
2009     return n < getNumOperands()
2010       ? getOperand(n)
2011       : 0;
2012   }
2013
2014   unsigned getNumSuccessors() const { return 0; }
2015
2016   // Methods for support type inquiry through isa, cast, and dyn_cast:
2017   static inline bool classof(const ReturnInst *) { return true; }
2018   static inline bool classof(const Instruction *I) {
2019     return (I->getOpcode() == Instruction::Ret);
2020   }
2021   static inline bool classof(const Value *V) {
2022     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2023   }
2024  private:
2025   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2026   virtual unsigned getNumSuccessorsV() const;
2027   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2028 };
2029
2030 template <>
2031 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2032 };
2033
2034 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2035
2036 //===----------------------------------------------------------------------===//
2037 //                               BranchInst Class
2038 //===----------------------------------------------------------------------===//
2039
2040 //===---------------------------------------------------------------------------
2041 /// BranchInst - Conditional or Unconditional Branch instruction.
2042 ///
2043 class BranchInst : public TerminatorInst {
2044   /// Ops list - Branches are strange.  The operands are ordered:
2045   ///  [Cond, FalseDest,] TrueDest.  This makes some accessors faster because
2046   /// they don't have to check for cond/uncond branchness. These are mostly
2047   /// accessed relative from op_end().
2048   BranchInst(const BranchInst &BI);
2049   void AssertOK();
2050   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2051   // BranchInst(BB *B)                           - 'br B'
2052   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
2053   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
2054   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2055   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
2056   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
2057   explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2058   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2059              Instruction *InsertBefore = 0);
2060   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2061   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2062              BasicBlock *InsertAtEnd);
2063 public:
2064   static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2065     return new(1, true) BranchInst(IfTrue, InsertBefore);
2066   }
2067   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2068                             Value *Cond, Instruction *InsertBefore = 0) {
2069     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2070   }
2071   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2072     return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2073   }
2074   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2075                             Value *Cond, BasicBlock *InsertAtEnd) {
2076     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2077   }
2078
2079   ~BranchInst();
2080
2081   /// Transparently provide more efficient getOperand methods.
2082   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2083
2084   virtual BranchInst *clone(LLVMContext &Context) const;
2085
2086   bool isUnconditional() const { return getNumOperands() == 1; }
2087   bool isConditional()   const { return getNumOperands() == 3; }
2088
2089   Value *getCondition() const {
2090     assert(isConditional() && "Cannot get condition of an uncond branch!");
2091     return Op<-3>();
2092   }
2093
2094   void setCondition(Value *V) {
2095     assert(isConditional() && "Cannot set condition of unconditional branch!");
2096     Op<-3>() = V;
2097   }
2098
2099   // setUnconditionalDest - Change the current branch to an unconditional branch
2100   // targeting the specified block.
2101   // FIXME: Eliminate this ugly method.
2102   void setUnconditionalDest(BasicBlock *Dest) {
2103     Op<-1>() = Dest;
2104     if (isConditional()) {  // Convert this to an uncond branch.
2105       Op<-2>() = 0;
2106       Op<-3>() = 0;
2107       NumOperands = 1;
2108       OperandList = op_begin();
2109     }
2110   }
2111
2112   unsigned getNumSuccessors() const { return 1+isConditional(); }
2113
2114   BasicBlock *getSuccessor(unsigned i) const {
2115     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2116     return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2117   }
2118
2119   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2120     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2121     *(&Op<-1>() - idx) = NewSucc;
2122   }
2123
2124   // Methods for support type inquiry through isa, cast, and dyn_cast:
2125   static inline bool classof(const BranchInst *) { return true; }
2126   static inline bool classof(const Instruction *I) {
2127     return (I->getOpcode() == Instruction::Br);
2128   }
2129   static inline bool classof(const Value *V) {
2130     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2131   }
2132 private:
2133   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2134   virtual unsigned getNumSuccessorsV() const;
2135   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2136 };
2137
2138 template <>
2139 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2140
2141 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2142
2143 //===----------------------------------------------------------------------===//
2144 //                               SwitchInst Class
2145 //===----------------------------------------------------------------------===//
2146
2147 //===---------------------------------------------------------------------------
2148 /// SwitchInst - Multiway switch
2149 ///
2150 class SwitchInst : public TerminatorInst {
2151   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2152   unsigned ReservedSpace;
2153   // Operand[0]    = Value to switch on
2154   // Operand[1]    = Default basic block destination
2155   // Operand[2n  ] = Value to match
2156   // Operand[2n+1] = BasicBlock to go to on match
2157   SwitchInst(const SwitchInst &RI);
2158   void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2159   void resizeOperands(unsigned No);
2160   // allocate space for exactly zero operands
2161   void *operator new(size_t s) {
2162     return User::operator new(s, 0);
2163   }
2164   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2165   /// switch on and a default destination.  The number of additional cases can
2166   /// be specified here to make memory allocation more efficient.  This
2167   /// constructor can also autoinsert before another instruction.
2168   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2169              Instruction *InsertBefore = 0);
2170
2171   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2172   /// switch on and a default destination.  The number of additional cases can
2173   /// be specified here to make memory allocation more efficient.  This
2174   /// constructor also autoinserts at the end of the specified BasicBlock.
2175   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2176              BasicBlock *InsertAtEnd);
2177 public:
2178   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2179                             unsigned NumCases, Instruction *InsertBefore = 0) {
2180     return new SwitchInst(Value, Default, NumCases, InsertBefore);
2181   }
2182   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2183                             unsigned NumCases, BasicBlock *InsertAtEnd) {
2184     return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2185   }
2186   ~SwitchInst();
2187
2188   /// Provide fast operand accessors
2189   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2190
2191   // Accessor Methods for Switch stmt
2192   Value *getCondition() const { return getOperand(0); }
2193   void setCondition(Value *V) { setOperand(0, V); }
2194
2195   BasicBlock *getDefaultDest() const {
2196     return cast<BasicBlock>(getOperand(1));
2197   }
2198
2199   /// getNumCases - return the number of 'cases' in this switch instruction.
2200   /// Note that case #0 is always the default case.
2201   unsigned getNumCases() const {
2202     return getNumOperands()/2;
2203   }
2204
2205   /// getCaseValue - Return the specified case value.  Note that case #0, the
2206   /// default destination, does not have a case value.
2207   ConstantInt *getCaseValue(unsigned i) {
2208     assert(i && i < getNumCases() && "Illegal case value to get!");
2209     return getSuccessorValue(i);
2210   }
2211
2212   /// getCaseValue - Return the specified case value.  Note that case #0, the
2213   /// default destination, does not have a case value.
2214   const ConstantInt *getCaseValue(unsigned i) const {
2215     assert(i && i < getNumCases() && "Illegal case value to get!");
2216     return getSuccessorValue(i);
2217   }
2218
2219   /// findCaseValue - Search all of the case values for the specified constant.
2220   /// If it is explicitly handled, return the case number of it, otherwise
2221   /// return 0 to indicate that it is handled by the default handler.
2222   unsigned findCaseValue(const ConstantInt *C) const {
2223     for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2224       if (getCaseValue(i) == C)
2225         return i;
2226     return 0;
2227   }
2228
2229   /// findCaseDest - Finds the unique case value for a given successor. Returns
2230   /// null if the successor is not found, not unique, or is the default case.
2231   ConstantInt *findCaseDest(BasicBlock *BB) {
2232     if (BB == getDefaultDest()) return NULL;
2233
2234     ConstantInt *CI = NULL;
2235     for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2236       if (getSuccessor(i) == BB) {
2237         if (CI) return NULL;   // Multiple cases lead to BB.
2238         else CI = getCaseValue(i);
2239       }
2240     }
2241     return CI;
2242   }
2243
2244   /// addCase - Add an entry to the switch instruction...
2245   ///
2246   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2247
2248   /// removeCase - This method removes the specified successor from the switch
2249   /// instruction.  Note that this cannot be used to remove the default
2250   /// destination (successor #0).
2251   ///
2252   void removeCase(unsigned idx);
2253
2254   virtual SwitchInst *clone(LLVMContext &Context) const;
2255
2256   unsigned getNumSuccessors() const { return getNumOperands()/2; }
2257   BasicBlock *getSuccessor(unsigned idx) const {
2258     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2259     return cast<BasicBlock>(getOperand(idx*2+1));
2260   }
2261   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2262     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2263     setOperand(idx*2+1, NewSucc);
2264   }
2265
2266   // getSuccessorValue - Return the value associated with the specified
2267   // successor.
2268   ConstantInt *getSuccessorValue(unsigned idx) const {
2269     assert(idx < getNumSuccessors() && "Successor # out of range!");
2270     return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2271   }
2272
2273   // Methods for support type inquiry through isa, cast, and dyn_cast:
2274   static inline bool classof(const SwitchInst *) { return true; }
2275   static inline bool classof(const Instruction *I) {
2276     return I->getOpcode() == Instruction::Switch;
2277   }
2278   static inline bool classof(const Value *V) {
2279     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2280   }
2281 private:
2282   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2283   virtual unsigned getNumSuccessorsV() const;
2284   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2285 };
2286
2287 template <>
2288 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2289 };
2290
2291 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2292
2293
2294 //===----------------------------------------------------------------------===//
2295 //                               InvokeInst Class
2296 //===----------------------------------------------------------------------===//
2297
2298 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
2299 /// calling convention of the call.
2300 ///
2301 class InvokeInst : public TerminatorInst {
2302   AttrListPtr AttributeList;
2303   InvokeInst(const InvokeInst &BI);
2304   void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2305             Value* const *Args, unsigned NumArgs);
2306
2307   template<typename InputIterator>
2308   void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2309             InputIterator ArgBegin, InputIterator ArgEnd,
2310             const std::string &NameStr,
2311             // This argument ensures that we have an iterator we can
2312             // do arithmetic on in constant time
2313             std::random_access_iterator_tag) {
2314     unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2315
2316     // This requires that the iterator points to contiguous memory.
2317     init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2318     setName(NameStr);
2319   }
2320
2321   /// Construct an InvokeInst given a range of arguments.
2322   /// InputIterator must be a random-access iterator pointing to
2323   /// contiguous storage (e.g. a std::vector<>::iterator).  Checks are
2324   /// made for random-accessness but not for contiguous storage as
2325   /// that would incur runtime overhead.
2326   ///
2327   /// @brief Construct an InvokeInst from a range of arguments
2328   template<typename InputIterator>
2329   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2330                     InputIterator ArgBegin, InputIterator ArgEnd,
2331                     unsigned Values,
2332                     const std::string &NameStr, Instruction *InsertBefore);
2333
2334   /// Construct an InvokeInst given a range of arguments.
2335   /// InputIterator must be a random-access iterator pointing to
2336   /// contiguous storage (e.g. a std::vector<>::iterator).  Checks are
2337   /// made for random-accessness but not for contiguous storage as
2338   /// that would incur runtime overhead.
2339   ///
2340   /// @brief Construct an InvokeInst from a range of arguments
2341   template<typename InputIterator>
2342   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2343                     InputIterator ArgBegin, InputIterator ArgEnd,
2344                     unsigned Values,
2345                     const std::string &NameStr, BasicBlock *InsertAtEnd);
2346 public:
2347   template<typename InputIterator>
2348   static InvokeInst *Create(Value *Func,
2349                             BasicBlock *IfNormal, BasicBlock *IfException,
2350                             InputIterator ArgBegin, InputIterator ArgEnd,
2351                             const std::string &NameStr = "",
2352                             Instruction *InsertBefore = 0) {
2353     unsigned Values(ArgEnd - ArgBegin + 3);
2354     return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2355                                   Values, NameStr, InsertBefore);
2356   }
2357   template<typename InputIterator>
2358   static InvokeInst *Create(Value *Func,
2359                             BasicBlock *IfNormal, BasicBlock *IfException,
2360                             InputIterator ArgBegin, InputIterator ArgEnd,
2361                             const std::string &NameStr,
2362                             BasicBlock *InsertAtEnd) {
2363     unsigned Values(ArgEnd - ArgBegin + 3);
2364     return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2365                                   Values, NameStr, InsertAtEnd);
2366   }
2367
2368   virtual InvokeInst *clone(LLVMContext &Context) const;
2369
2370   /// Provide fast operand accessors
2371   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2372
2373   /// getCallingConv/setCallingConv - Get or set the calling convention of this
2374   /// function call.
2375   unsigned getCallingConv() const { return SubclassData; }
2376   void setCallingConv(unsigned CC) {
2377     SubclassData = CC;
2378   }
2379
2380   /// getAttributes - Return the parameter attributes for this invoke.
2381   ///
2382   const AttrListPtr &getAttributes() const { return AttributeList; }
2383
2384   /// setAttributes - Set the parameter attributes for this invoke.
2385   ///
2386   void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2387
2388   /// addAttribute - adds the attribute to the list of attributes.
2389   void addAttribute(unsigned i, Attributes attr);
2390
2391   /// removeAttribute - removes the attribute from the list of attributes.
2392   void removeAttribute(unsigned i, Attributes attr);
2393
2394   /// @brief Determine whether the call or the callee has the given attribute.
2395   bool paramHasAttr(unsigned i, Attributes attr) const;
2396
2397   /// @brief Extract the alignment for a call or parameter (0=unknown).
2398   unsigned getParamAlignment(unsigned i) const {
2399     return AttributeList.getParamAlignment(i);
2400   }
2401
2402   /// @brief Determine if the call does not access memory.
2403   bool doesNotAccessMemory() const {
2404     return paramHasAttr(0, Attribute::ReadNone);
2405   }
2406   void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2407     if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2408     else removeAttribute(~0, Attribute::ReadNone);
2409   }
2410
2411   /// @brief Determine if the call does not access or only reads memory.
2412   bool onlyReadsMemory() const {
2413     return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2414   }
2415   void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2416     if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2417     else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2418   }
2419
2420   /// @brief Determine if the call cannot return.
2421   bool doesNotReturn() const {
2422     return paramHasAttr(~0, Attribute::NoReturn);
2423   }
2424   void setDoesNotReturn(bool DoesNotReturn = true) {
2425     if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2426     else removeAttribute(~0, Attribute::NoReturn);
2427   }
2428
2429   /// @brief Determine if the call cannot unwind.
2430   bool doesNotThrow() const {
2431     return paramHasAttr(~0, Attribute::NoUnwind);
2432   }
2433   void setDoesNotThrow(bool DoesNotThrow = true) {
2434     if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2435     else removeAttribute(~0, Attribute::NoUnwind);
2436   }
2437
2438   /// @brief Determine if the call returns a structure through first
2439   /// pointer argument.
2440   bool hasStructRetAttr() const {
2441     // Be friendly and also check the callee.
2442     return paramHasAttr(1, Attribute::StructRet);
2443   }
2444
2445   /// @brief Determine if any call argument is an aggregate passed by value.
2446   bool hasByValArgument() const {
2447     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2448   }
2449
2450   /// getCalledFunction - Return the function called, or null if this is an
2451   /// indirect function invocation.
2452   ///
2453   Function *getCalledFunction() const {
2454     return dyn_cast<Function>(getOperand(0));
2455   }
2456
2457   /// getCalledValue - Get a pointer to the function that is invoked by this
2458   /// instruction
2459   const Value *getCalledValue() const { return getOperand(0); }
2460         Value *getCalledValue()       { return getOperand(0); }
2461
2462   // get*Dest - Return the destination basic blocks...
2463   BasicBlock *getNormalDest() const {
2464     return cast<BasicBlock>(getOperand(1));
2465   }
2466   BasicBlock *getUnwindDest() const {
2467     return cast<BasicBlock>(getOperand(2));
2468   }
2469   void setNormalDest(BasicBlock *B) {
2470     setOperand(1, B);
2471   }
2472
2473   void setUnwindDest(BasicBlock *B) {
2474     setOperand(2, B);
2475   }
2476
2477   BasicBlock *getSuccessor(unsigned i) const {
2478     assert(i < 2 && "Successor # out of range for invoke!");
2479     return i == 0 ? getNormalDest() : getUnwindDest();
2480   }
2481
2482   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2483     assert(idx < 2 && "Successor # out of range for invoke!");
2484     setOperand(idx+1, NewSucc);
2485   }
2486
2487   unsigned getNumSuccessors() const { return 2; }
2488
2489   // Methods for support type inquiry through isa, cast, and dyn_cast:
2490   static inline bool classof(const InvokeInst *) { return true; }
2491   static inline bool classof(const Instruction *I) {
2492     return (I->getOpcode() == Instruction::Invoke);
2493   }
2494   static inline bool classof(const Value *V) {
2495     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2496   }
2497 private:
2498   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2499   virtual unsigned getNumSuccessorsV() const;
2500   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2501 };
2502
2503 template <>
2504 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2505 };
2506
2507 template<typename InputIterator>
2508 InvokeInst::InvokeInst(Value *Func,
2509                        BasicBlock *IfNormal, BasicBlock *IfException,
2510                        InputIterator ArgBegin, InputIterator ArgEnd,
2511                        unsigned Values,
2512                        const std::string &NameStr, Instruction *InsertBefore)
2513   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2514                                       ->getElementType())->getReturnType(),
2515                    Instruction::Invoke,
2516                    OperandTraits<InvokeInst>::op_end(this) - Values,
2517                    Values, InsertBefore) {
2518   init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2519        typename std::iterator_traits<InputIterator>::iterator_category());
2520 }
2521 template<typename InputIterator>
2522 InvokeInst::InvokeInst(Value *Func,
2523                        BasicBlock *IfNormal, BasicBlock *IfException,
2524                        InputIterator ArgBegin, InputIterator ArgEnd,
2525                        unsigned Values,
2526                        const std::string &NameStr, BasicBlock *InsertAtEnd)
2527   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2528                                       ->getElementType())->getReturnType(),
2529                    Instruction::Invoke,
2530                    OperandTraits<InvokeInst>::op_end(this) - Values,
2531                    Values, InsertAtEnd) {
2532   init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2533        typename std::iterator_traits<InputIterator>::iterator_category());
2534 }
2535
2536 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2537
2538 //===----------------------------------------------------------------------===//
2539 //                              UnwindInst Class
2540 //===----------------------------------------------------------------------===//
2541
2542 //===---------------------------------------------------------------------------
2543 /// UnwindInst - Immediately exit the current function, unwinding the stack
2544 /// until an invoke instruction is found.
2545 ///
2546 class UnwindInst : public TerminatorInst {
2547   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2548 public:
2549   // allocate space for exactly zero operands
2550   void *operator new(size_t s) {
2551     return User::operator new(s, 0);
2552   }
2553   explicit UnwindInst(Instruction *InsertBefore = 0);
2554   explicit UnwindInst(BasicBlock *InsertAtEnd);
2555
2556   virtual UnwindInst *clone(LLVMContext &Context) const;
2557
2558   unsigned getNumSuccessors() const { return 0; }
2559
2560   // Methods for support type inquiry through isa, cast, and dyn_cast:
2561   static inline bool classof(const UnwindInst *) { return true; }
2562   static inline bool classof(const Instruction *I) {
2563     return I->getOpcode() == Instruction::Unwind;
2564   }
2565   static inline bool classof(const Value *V) {
2566     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2567   }
2568 private:
2569   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2570   virtual unsigned getNumSuccessorsV() const;
2571   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2572 };
2573
2574 //===----------------------------------------------------------------------===//
2575 //                           UnreachableInst Class
2576 //===----------------------------------------------------------------------===//
2577
2578 //===---------------------------------------------------------------------------
2579 /// UnreachableInst - This function has undefined behavior.  In particular, the
2580 /// presence of this instruction indicates some higher level knowledge that the
2581 /// end of the block cannot be reached.
2582 ///
2583 class UnreachableInst : public TerminatorInst {
2584   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
2585 public:
2586   // allocate space for exactly zero operands
2587   void *operator new(size_t s) {
2588     return User::operator new(s, 0);
2589   }
2590   explicit UnreachableInst(Instruction *InsertBefore = 0);
2591   explicit UnreachableInst(BasicBlock *InsertAtEnd);
2592
2593   virtual UnreachableInst *clone(LLVMContext &Context) const;
2594
2595   unsigned getNumSuccessors() const { return 0; }
2596
2597   // Methods for support type inquiry through isa, cast, and dyn_cast:
2598   static inline bool classof(const UnreachableInst *) { return true; }
2599   static inline bool classof(const Instruction *I) {
2600     return I->getOpcode() == Instruction::Unreachable;
2601   }
2602   static inline bool classof(const Value *V) {
2603     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2604   }
2605 private:
2606   virtual BasicBlock *getSuccessorV(unsigned idx) const;
2607   virtual unsigned getNumSuccessorsV() const;
2608   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2609 };
2610
2611 //===----------------------------------------------------------------------===//
2612 //                                 TruncInst Class
2613 //===----------------------------------------------------------------------===//
2614
2615 /// @brief This class represents a truncation of integer types.
2616 class TruncInst : public CastInst {
2617   /// Private copy constructor
2618   TruncInst(const TruncInst &CI)
2619     : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2620   }
2621 public:
2622   /// @brief Constructor with insert-before-instruction semantics
2623   TruncInst(
2624     Value *S,                     ///< The value to be truncated
2625     const Type *Ty,               ///< The (smaller) type to truncate to
2626     const std::string &NameStr = "", ///< A name for the new instruction
2627     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2628   );
2629
2630   /// @brief Constructor with insert-at-end-of-block semantics
2631   TruncInst(
2632     Value *S,                     ///< The value to be truncated
2633     const Type *Ty,               ///< The (smaller) type to truncate to
2634     const std::string &NameStr,   ///< A name for the new instruction
2635     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2636   );
2637
2638   /// @brief Clone an identical TruncInst
2639   virtual CastInst *clone(LLVMContext &Context) const;
2640
2641   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2642   static inline bool classof(const TruncInst *) { return true; }
2643   static inline bool classof(const Instruction *I) {
2644     return I->getOpcode() == Trunc;
2645   }
2646   static inline bool classof(const Value *V) {
2647     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2648   }
2649 };
2650
2651 //===----------------------------------------------------------------------===//
2652 //                                 ZExtInst Class
2653 //===----------------------------------------------------------------------===//
2654
2655 /// @brief This class represents zero extension of integer types.
2656 class ZExtInst : public CastInst {
2657   /// @brief Private copy constructor
2658   ZExtInst(const ZExtInst &CI)
2659     : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2660   }
2661 public:
2662   /// @brief Constructor with insert-before-instruction semantics
2663   ZExtInst(
2664     Value *S,                     ///< The value to be zero extended
2665     const Type *Ty,               ///< The type to zero extend to
2666     const std::string &NameStr = "", ///< A name for the new instruction
2667     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2668   );
2669
2670   /// @brief Constructor with insert-at-end semantics.
2671   ZExtInst(
2672     Value *S,                     ///< The value to be zero extended
2673     const Type *Ty,               ///< The type to zero extend to
2674     const std::string &NameStr,   ///< A name for the new instruction
2675     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2676   );
2677
2678   /// @brief Clone an identical ZExtInst
2679   virtual CastInst *clone(LLVMContext &Context) const;
2680
2681   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2682   static inline bool classof(const ZExtInst *) { return true; }
2683   static inline bool classof(const Instruction *I) {
2684     return I->getOpcode() == ZExt;
2685   }
2686   static inline bool classof(const Value *V) {
2687     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2688   }
2689 };
2690
2691 //===----------------------------------------------------------------------===//
2692 //                                 SExtInst Class
2693 //===----------------------------------------------------------------------===//
2694
2695 /// @brief This class represents a sign extension of integer types.
2696 class SExtInst : public CastInst {
2697   /// @brief Private copy constructor
2698   SExtInst(const SExtInst &CI)
2699     : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2700   }
2701 public:
2702   /// @brief Constructor with insert-before-instruction semantics
2703   SExtInst(
2704     Value *S,                     ///< The value to be sign extended
2705     const Type *Ty,               ///< The type to sign extend to
2706     const std::string &NameStr = "", ///< A name for the new instruction
2707     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2708   );
2709
2710   /// @brief Constructor with insert-at-end-of-block semantics
2711   SExtInst(
2712     Value *S,                     ///< The value to be sign extended
2713     const Type *Ty,               ///< The type to sign extend to
2714     const std::string &NameStr,   ///< A name for the new instruction
2715     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2716   );
2717
2718   /// @brief Clone an identical SExtInst
2719   virtual CastInst *clone(LLVMContext &Context) const;
2720
2721   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2722   static inline bool classof(const SExtInst *) { return true; }
2723   static inline bool classof(const Instruction *I) {
2724     return I->getOpcode() == SExt;
2725   }
2726   static inline bool classof(const Value *V) {
2727     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2728   }
2729 };
2730
2731 //===----------------------------------------------------------------------===//
2732 //                                 FPTruncInst Class
2733 //===----------------------------------------------------------------------===//
2734
2735 /// @brief This class represents a truncation of floating point types.
2736 class FPTruncInst : public CastInst {
2737   FPTruncInst(const FPTruncInst &CI)
2738     : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2739   }
2740 public:
2741   /// @brief Constructor with insert-before-instruction semantics
2742   FPTruncInst(
2743     Value *S,                     ///< The value to be truncated
2744     const Type *Ty,               ///< The type to truncate to
2745     const std::string &NameStr = "", ///< A name for the new instruction
2746     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2747   );
2748
2749   /// @brief Constructor with insert-before-instruction semantics
2750   FPTruncInst(
2751     Value *S,                     ///< The value to be truncated
2752     const Type *Ty,               ///< The type to truncate to
2753     const std::string &NameStr,   ///< A name for the new instruction
2754     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2755   );
2756
2757   /// @brief Clone an identical FPTruncInst
2758   virtual CastInst *clone(LLVMContext &Context) const;
2759
2760   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2761   static inline bool classof(const FPTruncInst *) { return true; }
2762   static inline bool classof(const Instruction *I) {
2763     return I->getOpcode() == FPTrunc;
2764   }
2765   static inline bool classof(const Value *V) {
2766     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2767   }
2768 };
2769
2770 //===----------------------------------------------------------------------===//
2771 //                                 FPExtInst Class
2772 //===----------------------------------------------------------------------===//
2773
2774 /// @brief This class represents an extension of floating point types.
2775 class FPExtInst : public CastInst {
2776   FPExtInst(const FPExtInst &CI)
2777     : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2778   }
2779 public:
2780   /// @brief Constructor with insert-before-instruction semantics
2781   FPExtInst(
2782     Value *S,                     ///< The value to be extended
2783     const Type *Ty,               ///< The type to extend to
2784     const std::string &NameStr = "", ///< A name for the new instruction
2785     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2786   );
2787
2788   /// @brief Constructor with insert-at-end-of-block semantics
2789   FPExtInst(
2790     Value *S,                     ///< The value to be extended
2791     const Type *Ty,               ///< The type to extend to
2792     const std::string &NameStr,   ///< A name for the new instruction
2793     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2794   );
2795
2796   /// @brief Clone an identical FPExtInst
2797   virtual CastInst *clone(LLVMContext &Context) const;
2798
2799   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2800   static inline bool classof(const FPExtInst *) { return true; }
2801   static inline bool classof(const Instruction *I) {
2802     return I->getOpcode() == FPExt;
2803   }
2804   static inline bool classof(const Value *V) {
2805     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2806   }
2807 };
2808
2809 //===----------------------------------------------------------------------===//
2810 //                                 UIToFPInst Class
2811 //===----------------------------------------------------------------------===//
2812
2813 /// @brief This class represents a cast unsigned integer to floating point.
2814 class UIToFPInst : public CastInst {
2815   UIToFPInst(const UIToFPInst &CI)
2816     : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2817   }
2818 public:
2819   /// @brief Constructor with insert-before-instruction semantics
2820   UIToFPInst(
2821     Value *S,                     ///< The value to be converted
2822     const Type *Ty,               ///< The type to convert to
2823     const std::string &NameStr = "", ///< A name for the new instruction
2824     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2825   );
2826
2827   /// @brief Constructor with insert-at-end-of-block semantics
2828   UIToFPInst(
2829     Value *S,                     ///< The value to be converted
2830     const Type *Ty,               ///< The type to convert to
2831     const std::string &NameStr,   ///< A name for the new instruction
2832     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2833   );
2834
2835   /// @brief Clone an identical UIToFPInst
2836   virtual CastInst *clone(LLVMContext &Context) const;
2837
2838   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2839   static inline bool classof(const UIToFPInst *) { return true; }
2840   static inline bool classof(const Instruction *I) {
2841     return I->getOpcode() == UIToFP;
2842   }
2843   static inline bool classof(const Value *V) {
2844     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2845   }
2846 };
2847
2848 //===----------------------------------------------------------------------===//
2849 //                                 SIToFPInst Class
2850 //===----------------------------------------------------------------------===//
2851
2852 /// @brief This class represents a cast from signed integer to floating point.
2853 class SIToFPInst : public CastInst {
2854   SIToFPInst(const SIToFPInst &CI)
2855     : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2856   }
2857 public:
2858   /// @brief Constructor with insert-before-instruction semantics
2859   SIToFPInst(
2860     Value *S,                     ///< The value to be converted
2861     const Type *Ty,               ///< The type to convert to
2862     const std::string &NameStr = "", ///< A name for the new instruction
2863     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2864   );
2865
2866   /// @brief Constructor with insert-at-end-of-block semantics
2867   SIToFPInst(
2868     Value *S,                     ///< The value to be converted
2869     const Type *Ty,               ///< The type to convert to
2870     const std::string &NameStr,   ///< A name for the new instruction
2871     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2872   );
2873
2874   /// @brief Clone an identical SIToFPInst
2875   virtual CastInst *clone(LLVMContext &Context) const;
2876
2877   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2878   static inline bool classof(const SIToFPInst *) { return true; }
2879   static inline bool classof(const Instruction *I) {
2880     return I->getOpcode() == SIToFP;
2881   }
2882   static inline bool classof(const Value *V) {
2883     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2884   }
2885 };
2886
2887 //===----------------------------------------------------------------------===//
2888 //                                 FPToUIInst Class
2889 //===----------------------------------------------------------------------===//
2890
2891 /// @brief This class represents a cast from floating point to unsigned integer
2892 class FPToUIInst  : public CastInst {
2893   FPToUIInst(const FPToUIInst &CI)
2894     : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2895   }
2896 public:
2897   /// @brief Constructor with insert-before-instruction semantics
2898   FPToUIInst(
2899     Value *S,                     ///< The value to be converted
2900     const Type *Ty,               ///< The type to convert to
2901     const std::string &NameStr = "", ///< A name for the new instruction
2902     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2903   );
2904
2905   /// @brief Constructor with insert-at-end-of-block semantics
2906   FPToUIInst(
2907     Value *S,                     ///< The value to be converted
2908     const Type *Ty,               ///< The type to convert to
2909     const std::string &NameStr,   ///< A name for the new instruction
2910     BasicBlock *InsertAtEnd       ///< Where to insert the new instruction
2911   );
2912
2913   /// @brief Clone an identical FPToUIInst
2914   virtual CastInst *clone(LLVMContext &Context) const;
2915
2916   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2917   static inline bool classof(const FPToUIInst *) { return true; }
2918   static inline bool classof(const Instruction *I) {
2919     return I->getOpcode() == FPToUI;
2920   }
2921   static inline bool classof(const Value *V) {
2922     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2923   }
2924 };
2925
2926 //===----------------------------------------------------------------------===//
2927 //                                 FPToSIInst Class
2928 //===----------------------------------------------------------------------===//
2929
2930 /// @brief This class represents a cast from floating point to signed integer.
2931 class FPToSIInst  : public CastInst {
2932   FPToSIInst(const FPToSIInst &CI)
2933     : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2934   }
2935 public:
2936   /// @brief Constructor with insert-before-instruction semantics
2937   FPToSIInst(
2938     Value *S,                     ///< The value to be converted
2939     const Type *Ty,               ///< The type to convert to
2940     const std::string &NameStr = "", ///< A name for the new instruction
2941     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2942   );
2943
2944   /// @brief Constructor with insert-at-end-of-block semantics
2945   FPToSIInst(
2946     Value *S,                     ///< The value to be converted
2947     const Type *Ty,               ///< The type to convert to
2948     const std::string &NameStr,   ///< A name for the new instruction
2949     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2950   );
2951
2952   /// @brief Clone an identical FPToSIInst
2953   virtual CastInst *clone(LLVMContext &Context) const;
2954
2955   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2956   static inline bool classof(const FPToSIInst *) { return true; }
2957   static inline bool classof(const Instruction *I) {
2958     return I->getOpcode() == FPToSI;
2959   }
2960   static inline bool classof(const Value *V) {
2961     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2962   }
2963 };
2964
2965 //===----------------------------------------------------------------------===//
2966 //                                 IntToPtrInst Class
2967 //===----------------------------------------------------------------------===//
2968
2969 /// @brief This class represents a cast from an integer to a pointer.
2970 class IntToPtrInst : public CastInst {
2971   IntToPtrInst(const IntToPtrInst &CI)
2972     : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2973   }
2974 public:
2975   /// @brief Constructor with insert-before-instruction semantics
2976   IntToPtrInst(
2977     Value *S,                     ///< The value to be converted
2978     const Type *Ty,               ///< The type to convert to
2979     const std::string &NameStr = "", ///< A name for the new instruction
2980     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2981   );
2982
2983   /// @brief Constructor with insert-at-end-of-block semantics
2984   IntToPtrInst(
2985     Value *S,                     ///< The value to be converted
2986     const Type *Ty,               ///< The type to convert to
2987     const std::string &NameStr,   ///< A name for the new instruction
2988     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2989   );
2990
2991   /// @brief Clone an identical IntToPtrInst
2992   virtual CastInst *clone(LLVMContext &Context) const;
2993
2994   // Methods for support type inquiry through isa, cast, and dyn_cast:
2995   static inline bool classof(const IntToPtrInst *) { return true; }
2996   static inline bool classof(const Instruction *I) {
2997     return I->getOpcode() == IntToPtr;
2998   }
2999   static inline bool classof(const Value *V) {
3000     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3001   }
3002 };
3003
3004 //===----------------------------------------------------------------------===//
3005 //                                 PtrToIntInst Class
3006 //===----------------------------------------------------------------------===//
3007
3008 /// @brief This class represents a cast from a pointer to an integer
3009 class PtrToIntInst : public CastInst {
3010   PtrToIntInst(const PtrToIntInst &CI)
3011     : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3012   }
3013 public:
3014   /// @brief Constructor with insert-before-instruction semantics
3015   PtrToIntInst(
3016     Value *S,                     ///< The value to be converted
3017     const Type *Ty,               ///< The type to convert to
3018     const std::string &NameStr = "", ///< A name for the new instruction
3019     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3020   );
3021
3022   /// @brief Constructor with insert-at-end-of-block semantics
3023   PtrToIntInst(
3024     Value *S,                     ///< The value to be converted
3025     const Type *Ty,               ///< The type to convert to
3026     const std::string &NameStr,   ///< A name for the new instruction
3027     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3028   );
3029
3030   /// @brief Clone an identical PtrToIntInst
3031   virtual CastInst *clone(LLVMContext &Context) const;
3032
3033   // Methods for support type inquiry through isa, cast, and dyn_cast:
3034   static inline bool classof(const PtrToIntInst *) { return true; }
3035   static inline bool classof(const Instruction *I) {
3036     return I->getOpcode() == PtrToInt;
3037   }
3038   static inline bool classof(const Value *V) {
3039     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3040   }
3041 };
3042
3043 //===----------------------------------------------------------------------===//
3044 //                             BitCastInst Class
3045 //===----------------------------------------------------------------------===//
3046
3047 /// @brief This class represents a no-op cast from one type to another.
3048 class BitCastInst : public CastInst {
3049   BitCastInst(const BitCastInst &CI)
3050     : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3051   }
3052 public:
3053   /// @brief Constructor with insert-before-instruction semantics
3054   BitCastInst(
3055     Value *S,                     ///< The value to be casted
3056     const Type *Ty,               ///< The type to casted to
3057     const std::string &NameStr = "", ///< A name for the new instruction
3058     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3059   );
3060
3061   /// @brief Constructor with insert-at-end-of-block semantics
3062   BitCastInst(
3063     Value *S,                     ///< The value to be casted
3064     const Type *Ty,               ///< The type to casted to
3065     const std::string &NameStr,      ///< A name for the new instruction
3066     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3067   );
3068
3069   /// @brief Clone an identical BitCastInst
3070   virtual CastInst *clone(LLVMContext &Context) const;
3071
3072   // Methods for support type inquiry through isa, cast, and dyn_cast:
3073   static inline bool classof(const BitCastInst *) { return true; }
3074   static inline bool classof(const Instruction *I) {
3075     return I->getOpcode() == BitCast;
3076   }
3077   static inline bool classof(const Value *V) {
3078     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3079   }
3080 };
3081
3082 } // End llvm namespace
3083
3084 #endif