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