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