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