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