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