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