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