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