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