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