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