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