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