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