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