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