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