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