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