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