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