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