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