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