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