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