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