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