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