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