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