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