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