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