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