add #include
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 <vector>  // fixme remove.
21
22 namespace llvm {
23
24 class BasicBlock;
25 class ConstantInt;
26 class PointerType;
27 class PackedType;
28
29 //===----------------------------------------------------------------------===//
30 //                             AllocationInst Class
31 //===----------------------------------------------------------------------===//
32
33 /// AllocationInst - This class is the common base class of MallocInst and
34 /// AllocaInst.
35 ///
36 class AllocationInst : public UnaryInstruction {
37   unsigned Alignment;
38 protected:
39   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
40                  const std::string &Name = "", Instruction *InsertBefore = 0);
41   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
42                  const std::string &Name, BasicBlock *InsertAtEnd);
43 public:
44   // Out of line virtual method, so the vtable, etc has a home.
45   virtual ~AllocationInst();
46
47   /// isArrayAllocation - Return true if there is an allocation size parameter
48   /// to the allocation instruction that is not 1.
49   ///
50   bool isArrayAllocation() const;
51
52   /// getArraySize - Get the number of element allocated, for a simple
53   /// allocation of a single element, this will return a constant 1 value.
54   ///
55   inline const Value *getArraySize() const { return getOperand(0); }
56   inline Value *getArraySize() { return getOperand(0); }
57
58   /// getType - Overload to return most specific pointer type
59   ///
60   inline const PointerType *getType() const {
61     return reinterpret_cast<const PointerType*>(Instruction::getType());
62   }
63
64   /// getAllocatedType - Return the type that is being allocated by the
65   /// instruction.
66   ///
67   const Type *getAllocatedType() const;
68
69   /// getAlignment - Return the alignment of the memory that is being allocated
70   /// by the instruction.
71   ///
72   unsigned getAlignment() const { return Alignment; }
73   void setAlignment(unsigned Align) {
74     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
75     Alignment = Align;
76   }
77
78   virtual Instruction *clone() const = 0;
79
80   // Methods for support type inquiry through isa, cast, and dyn_cast:
81   static inline bool classof(const AllocationInst *) { return true; }
82   static inline bool classof(const Instruction *I) {
83     return I->getOpcode() == Instruction::Alloca ||
84            I->getOpcode() == Instruction::Malloc;
85   }
86   static inline bool classof(const Value *V) {
87     return isa<Instruction>(V) && classof(cast<Instruction>(V));
88   }
89 };
90
91
92 //===----------------------------------------------------------------------===//
93 //                                MallocInst Class
94 //===----------------------------------------------------------------------===//
95
96 /// MallocInst - an instruction to allocated memory on the heap
97 ///
98 class MallocInst : public AllocationInst {
99   MallocInst(const MallocInst &MI);
100 public:
101   explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
102                       const std::string &Name = "",
103                       Instruction *InsertBefore = 0)
104     : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
105   MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
106              BasicBlock *InsertAtEnd)
107     : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
108
109   MallocInst(const Type *Ty, const std::string &Name,
110              Instruction *InsertBefore = 0)
111     : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
112   MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
113     : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
114
115   MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
116              const std::string &Name, BasicBlock *InsertAtEnd)
117     : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
118   MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
119                       const std::string &Name = "",
120                       Instruction *InsertBefore = 0)
121     : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
122
123   virtual MallocInst *clone() const;
124
125   // Methods for support type inquiry through isa, cast, and dyn_cast:
126   static inline bool classof(const MallocInst *) { return true; }
127   static inline bool classof(const Instruction *I) {
128     return (I->getOpcode() == Instruction::Malloc);
129   }
130   static inline bool classof(const Value *V) {
131     return isa<Instruction>(V) && classof(cast<Instruction>(V));
132   }
133 };
134
135
136 //===----------------------------------------------------------------------===//
137 //                                AllocaInst Class
138 //===----------------------------------------------------------------------===//
139
140 /// AllocaInst - an instruction to allocate memory on the stack
141 ///
142 class AllocaInst : public AllocationInst {
143   AllocaInst(const AllocaInst &);
144 public:
145   explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
146                       const std::string &Name = "",
147                       Instruction *InsertBefore = 0)
148     : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
149   AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
150              BasicBlock *InsertAtEnd)
151     : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
152
153   AllocaInst(const Type *Ty, const std::string &Name,
154              Instruction *InsertBefore = 0)
155     : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
156   AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
157     : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
158
159   AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
160              const std::string &Name = "", Instruction *InsertBefore = 0)
161     : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
162   AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
163              const std::string &Name, BasicBlock *InsertAtEnd)
164     : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
165
166   virtual AllocaInst *clone() const;
167
168   // Methods for support type inquiry through isa, cast, and dyn_cast:
169   static inline bool classof(const AllocaInst *) { return true; }
170   static inline bool classof(const Instruction *I) {
171     return (I->getOpcode() == Instruction::Alloca);
172   }
173   static inline bool classof(const Value *V) {
174     return isa<Instruction>(V) && classof(cast<Instruction>(V));
175   }
176 };
177
178
179 //===----------------------------------------------------------------------===//
180 //                                 FreeInst Class
181 //===----------------------------------------------------------------------===//
182
183 /// FreeInst - an instruction to deallocate memory
184 ///
185 class FreeInst : public UnaryInstruction {
186   void AssertOK();
187 public:
188   explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
189   FreeInst(Value *Ptr, BasicBlock *InsertAfter);
190
191   virtual FreeInst *clone() const;
192
193   virtual bool mayWriteToMemory() const { return true; }
194
195   // Methods for support type inquiry through isa, cast, and dyn_cast:
196   static inline bool classof(const FreeInst *) { return true; }
197   static inline bool classof(const Instruction *I) {
198     return (I->getOpcode() == Instruction::Free);
199   }
200   static inline bool classof(const Value *V) {
201     return isa<Instruction>(V) && classof(cast<Instruction>(V));
202   }
203 };
204
205
206 //===----------------------------------------------------------------------===//
207 //                                LoadInst Class
208 //===----------------------------------------------------------------------===//
209
210 /// LoadInst - an instruction for reading from memory.  This uses the
211 /// SubclassData field in Value to store whether or not the load is volatile.
212 ///
213 class LoadInst : public UnaryInstruction {
214   LoadInst(const LoadInst &LI)
215     : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
216     setVolatile(LI.isVolatile());
217
218 #ifndef NDEBUG
219     AssertOK();
220 #endif
221   }
222   void AssertOK();
223 public:
224   LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
225   LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
226   explicit LoadInst(Value *Ptr, const std::string &Name = "",
227                     bool isVolatile = false, Instruction *InsertBefore = 0);
228   LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
229            BasicBlock *InsertAtEnd);
230
231   /// isVolatile - Return true if this is a load from a volatile memory
232   /// location.
233   ///
234   bool isVolatile() const { return SubclassData; }
235
236   /// setVolatile - Specify whether this is a volatile load or not.
237   ///
238   void setVolatile(bool V) { SubclassData = V; }
239
240   virtual LoadInst *clone() const;
241
242   virtual bool mayWriteToMemory() const { return isVolatile(); }
243
244   Value *getPointerOperand() { return getOperand(0); }
245   const Value *getPointerOperand() const { return getOperand(0); }
246   static unsigned getPointerOperandIndex() { return 0U; }
247
248   // Methods for support type inquiry through isa, cast, and dyn_cast:
249   static inline bool classof(const LoadInst *) { return true; }
250   static inline bool classof(const Instruction *I) {
251     return I->getOpcode() == Instruction::Load;
252   }
253   static inline bool classof(const Value *V) {
254     return isa<Instruction>(V) && classof(cast<Instruction>(V));
255   }
256 };
257
258
259 //===----------------------------------------------------------------------===//
260 //                                StoreInst Class
261 //===----------------------------------------------------------------------===//
262
263 /// StoreInst - an instruction for storing to memory
264 ///
265 class StoreInst : public Instruction {
266   Use Ops[2];
267   StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
268     Ops[0].init(SI.Ops[0], this);
269     Ops[1].init(SI.Ops[1], this);
270     setVolatile(SI.isVolatile());
271 #ifndef NDEBUG
272     AssertOK();
273 #endif
274   }
275   void AssertOK();
276 public:
277   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
278   StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
279   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
280             Instruction *InsertBefore = 0);
281   StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
282
283
284   /// isVolatile - Return true if this is a load from a volatile memory
285   /// location.
286   ///
287   bool isVolatile() const { return SubclassData; }
288
289   /// setVolatile - Specify whether this is a volatile load or not.
290   ///
291   void setVolatile(bool V) { SubclassData = V; }
292
293   /// Transparently provide more efficient getOperand methods.
294   Value *getOperand(unsigned i) const {
295     assert(i < 2 && "getOperand() out of range!");
296     return Ops[i];
297   }
298   void setOperand(unsigned i, Value *Val) {
299     assert(i < 2 && "setOperand() out of range!");
300     Ops[i] = Val;
301   }
302   unsigned getNumOperands() const { return 2; }
303
304
305   virtual StoreInst *clone() const;
306
307   virtual bool mayWriteToMemory() const { return true; }
308
309   Value *getPointerOperand() { return getOperand(1); }
310   const Value *getPointerOperand() const { return getOperand(1); }
311   static unsigned getPointerOperandIndex() { return 1U; }
312
313   // Methods for support type inquiry through isa, cast, and dyn_cast:
314   static inline bool classof(const StoreInst *) { return true; }
315   static inline bool classof(const Instruction *I) {
316     return I->getOpcode() == Instruction::Store;
317   }
318   static inline bool classof(const Value *V) {
319     return isa<Instruction>(V) && classof(cast<Instruction>(V));
320   }
321 };
322
323
324 //===----------------------------------------------------------------------===//
325 //                             GetElementPtrInst Class
326 //===----------------------------------------------------------------------===//
327
328 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
329 /// access elements of arrays and structs
330 ///
331 class GetElementPtrInst : public Instruction {
332   GetElementPtrInst(const GetElementPtrInst &GEPI)
333     : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
334                   0, GEPI.getNumOperands()) {
335     Use *OL = OperandList = new Use[NumOperands];
336     Use *GEPIOL = GEPI.OperandList;
337     for (unsigned i = 0, E = NumOperands; i != E; ++i)
338       OL[i].init(GEPIOL[i], this);
339   }
340   void init(Value *Ptr, Value* const *Idx, unsigned NumIdx);
341   void init(Value *Ptr, Value *Idx0, Value *Idx1);
342   void init(Value *Ptr, Value *Idx);
343 public:
344   /// Constructors - Create a getelementptr instruction with a base pointer an
345   /// list of indices.  The first ctor can optionally insert before an existing
346   /// instruction, the second appends the new instruction to the specified
347   /// BasicBlock.
348   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
349                     const std::string &Name = "", Instruction *InsertBefore =0);
350   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
351                     const std::string &Name, BasicBlock *InsertAtEnd);
352   GetElementPtrInst(Value *Ptr, Value* const *Idx, unsigned NumIdx,
353                     const std::string &Name = "", Instruction *InsertBefore =0);
354   GetElementPtrInst(Value *Ptr, Value* const *Idx, unsigned NumIdx,
355                     const std::string &Name, BasicBlock *InsertAtEnd);
356   
357   /// Constructors - These two constructors are convenience methods because one
358   /// and two index getelementptr instructions are so common.
359   GetElementPtrInst(Value *Ptr, Value *Idx,
360                     const std::string &Name = "", Instruction *InsertBefore =0);
361   GetElementPtrInst(Value *Ptr, Value *Idx,
362                     const std::string &Name, BasicBlock *InsertAtEnd);
363   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
364                     const std::string &Name = "", Instruction *InsertBefore =0);
365   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
366                     const std::string &Name, BasicBlock *InsertAtEnd);
367   ~GetElementPtrInst();
368
369   virtual GetElementPtrInst *clone() const;
370
371   // getType - Overload to return most specific pointer type...
372   inline const PointerType *getType() const {
373     return reinterpret_cast<const PointerType*>(Instruction::getType());
374   }
375
376   /// getIndexedType - Returns the type of the element that would be loaded with
377   /// a load instruction with the specified parameters.
378   ///
379   /// A null type is returned if the indices are invalid for the specified
380   /// pointer type.
381   ///
382   static const Type *getIndexedType(const Type *Ptr,
383                                     Value* const *Idx, unsigned NumIdx,
384                                     bool AllowStructLeaf = false);
385   
386   static const Type *getIndexedType(const Type *Ptr,
387                                     const std::vector<Value*> &Indices,
388                                     bool AllowStructLeaf = false) {
389     return getIndexedType(Ptr, &Indices[0], Indices.size(), AllowStructLeaf);
390   }
391   static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
392                                     bool AllowStructLeaf = false);
393   static const Type *getIndexedType(const Type *Ptr, Value *Idx);
394
395   inline op_iterator       idx_begin()       { return op_begin()+1; }
396   inline const_op_iterator idx_begin() const { return op_begin()+1; }
397   inline op_iterator       idx_end()         { return op_end(); }
398   inline const_op_iterator idx_end()   const { return op_end(); }
399
400   Value *getPointerOperand() {
401     return getOperand(0);
402   }
403   const Value *getPointerOperand() const {
404     return getOperand(0);
405   }
406   static unsigned getPointerOperandIndex() {
407     return 0U;                      // get index for modifying correct operand
408   }
409
410   inline unsigned getNumIndices() const {  // Note: always non-negative
411     return getNumOperands() - 1;
412   }
413
414   inline bool hasIndices() const {
415     return getNumOperands() > 1;
416   }
417
418   // Methods for support type inquiry through isa, cast, and dyn_cast:
419   static inline bool classof(const GetElementPtrInst *) { return true; }
420   static inline bool classof(const Instruction *I) {
421     return (I->getOpcode() == Instruction::GetElementPtr);
422   }
423   static inline bool classof(const Value *V) {
424     return isa<Instruction>(V) && classof(cast<Instruction>(V));
425   }
426 };
427
428 //===----------------------------------------------------------------------===//
429 //                               ICmpInst Class
430 //===----------------------------------------------------------------------===//
431
432 /// This instruction compares its operands according to the predicate given
433 /// to the constructor. It only operates on integers, pointers, or packed 
434 /// vectors of integrals. The two operands must be the same type.
435 /// @brief Represent an integer comparison operator.
436 class ICmpInst: public CmpInst {
437 public:
438   /// This enumeration lists the possible predicates for the ICmpInst. The
439   /// values in the range 0-31 are reserved for FCmpInst while values in the
440   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
441   /// predicate values are not overlapping between the classes.
442   enum Predicate {
443     ICMP_EQ  = 32,    ///< equal
444     ICMP_NE  = 33,    ///< not equal
445     ICMP_UGT = 34,    ///< unsigned greater than
446     ICMP_UGE = 35,    ///< unsigned greater or equal
447     ICMP_ULT = 36,    ///< unsigned less than
448     ICMP_ULE = 37,    ///< unsigned less or equal
449     ICMP_SGT = 38,    ///< signed greater than
450     ICMP_SGE = 39,    ///< signed greater or equal
451     ICMP_SLT = 40,    ///< signed less than
452     ICMP_SLE = 41,    ///< signed less or equal
453     FIRST_ICMP_PREDICATE = ICMP_EQ,
454     LAST_ICMP_PREDICATE = ICMP_SLE,
455     BAD_ICMP_PREDICATE = ICMP_SLE + 1
456   };
457
458   /// @brief Constructor with insert-before-instruction semantics.
459   ICmpInst(
460     Predicate pred,  ///< The predicate to use for the comparison
461     Value *LHS,      ///< The left-hand-side of the expression
462     Value *RHS,      ///< The right-hand-side of the expression
463     const std::string &Name = "",  ///< Name of the instruction
464     Instruction *InsertBefore = 0  ///< Where to insert
465   ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertBefore) {
466   }
467
468   /// @brief Constructor with insert-at-block-end semantics.
469   ICmpInst(
470     Predicate pred, ///< The predicate to use for the comparison
471     Value *LHS,     ///< The left-hand-side of the expression
472     Value *RHS,     ///< The right-hand-side of the expression
473     const std::string &Name,  ///< Name of the instruction
474     BasicBlock *InsertAtEnd   ///< Block to insert into.
475   ) : CmpInst(Instruction::ICmp, pred, LHS, RHS, Name, InsertAtEnd) {
476   }
477
478   /// @brief Return the predicate for this instruction.
479   Predicate getPredicate() const { return Predicate(SubclassData); }
480
481   /// @brief Set the predicate for this instruction to the specified value.
482   void setPredicate(Predicate P) { SubclassData = P; }
483   
484   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
485   /// @returns the inverse predicate for the instruction's current predicate. 
486   /// @brief Return the inverse of the instruction's predicate.
487   Predicate getInversePredicate() const {
488     return getInversePredicate(getPredicate());
489   }
490
491   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, etc.
492   /// @returns the inverse predicate for predicate provided in \p pred. 
493   /// @brief Return the inverse of a given predicate
494   static Predicate getInversePredicate(Predicate pred);
495
496   /// For example, EQ->EQ, SLE->SGE, ULT->UGT, etc.
497   /// @returns the predicate that would be the result of exchanging the two 
498   /// operands of the ICmpInst instruction without changing the result 
499   /// produced.  
500   /// @brief Return the predicate as if the operands were swapped
501   Predicate getSwappedPredicate() const {
502     return getSwappedPredicate(getPredicate());
503   }
504
505   /// This is a static version that you can use without an instruction 
506   /// available.
507   /// @brief Return the predicate as if the operands were swapped.
508   static Predicate getSwappedPredicate(Predicate pred);
509
510   /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
511   /// @returns the predicate that would be the result if the operand were
512   /// regarded as signed.
513   /// @brief Return the signed version of the predicate
514   Predicate getSignedPredicate() const {
515     return getSignedPredicate(getPredicate());
516   }
517
518   /// This is a static version that you can use without an instruction.
519   /// @brief Return the signed version of the predicate.
520   static Predicate getSignedPredicate(Predicate pred);
521
522   /// This also tests for commutativity. If isEquality() returns true then
523   /// the predicate is also commutative. 
524   /// @returns true if the predicate of this instruction is EQ or NE.
525   /// @brief Determine if this is an equality predicate.
526   bool isEquality() const {
527     return SubclassData == ICMP_EQ || SubclassData == ICMP_NE;
528   }
529
530   /// @returns true if the predicate of this ICmpInst is commutative
531   /// @brief Determine if this relation is commutative.
532   bool isCommutative() const { return isEquality(); }
533
534   /// @returns true if the predicate is relational (not EQ or NE). 
535   /// @brief Determine if this a relational predicate.
536   bool isRelational() const {
537     return !isEquality();
538   }
539
540   /// @returns true if the predicate of this ICmpInst is signed, false otherwise
541   /// @brief Determine if this instruction's predicate is signed.
542   bool isSignedPredicate() { return isSignedPredicate(getPredicate()); }
543
544   /// @returns true if the predicate provided is signed, false otherwise
545   /// @brief Determine if the predicate is signed.
546   static bool isSignedPredicate(Predicate pred);
547
548   /// Exchange the two operands to this instruction in such a way that it does
549   /// not modify the semantics of the instruction. The predicate value may be
550   /// changed to retain the same result if the predicate is order dependent
551   /// (e.g. ult). 
552   /// @brief Swap operands and adjust predicate.
553   void swapOperands() {
554     SubclassData = getSwappedPredicate();
555     std::swap(Ops[0], Ops[1]);
556   }
557
558   // Methods for support type inquiry through isa, cast, and dyn_cast:
559   static inline bool classof(const ICmpInst *) { return true; }
560   static inline bool classof(const Instruction *I) {
561     return I->getOpcode() == Instruction::ICmp;
562   }
563   static inline bool classof(const Value *V) {
564     return isa<Instruction>(V) && classof(cast<Instruction>(V));
565   }
566 };
567
568 //===----------------------------------------------------------------------===//
569 //                               FCmpInst Class
570 //===----------------------------------------------------------------------===//
571
572 /// This instruction compares its operands according to the predicate given
573 /// to the constructor. It only operates on floating point values or packed     
574 /// vectors of floating point values. The operands must be identical types.
575 /// @brief Represents a floating point comparison operator.
576 class FCmpInst: public CmpInst {
577 public:
578   /// This enumeration lists the possible predicates for the FCmpInst. Values
579   /// in the range 0-31 are reserved for FCmpInst.
580   enum Predicate {
581     // Opcode        U L G E    Intuitive operation
582     FCMP_FALSE = 0, ///<  0 0 0 0    Always false (always folded)
583     FCMP_OEQ   = 1, ///<  0 0 0 1    True if ordered and equal
584     FCMP_OGT   = 2, ///<  0 0 1 0    True if ordered and greater than
585     FCMP_OGE   = 3, ///<  0 0 1 1    True if ordered and greater than or equal
586     FCMP_OLT   = 4, ///<  0 1 0 0    True if ordered and less than
587     FCMP_OLE   = 5, ///<  0 1 0 1    True if ordered and less than or equal
588     FCMP_ONE   = 6, ///<  0 1 1 0    True if ordered and operands are unequal
589     FCMP_ORD   = 7, ///<  0 1 1 1    True if ordered (no nans)
590     FCMP_UNO   = 8, ///<  1 0 0 0    True if unordered: isnan(X) | isnan(Y)
591     FCMP_UEQ   = 9, ///<  1 0 0 1    True if unordered or equal
592     FCMP_UGT   =10, ///<  1 0 1 0    True if unordered or greater than
593     FCMP_UGE   =11, ///<  1 0 1 1    True if unordered, greater than, or equal
594     FCMP_ULT   =12, ///<  1 1 0 0    True if unordered or less than
595     FCMP_ULE   =13, ///<  1 1 0 1    True if unordered, less than, or equal
596     FCMP_UNE   =14, ///<  1 1 1 0    True if unordered or not equal
597     FCMP_TRUE  =15, ///<  1 1 1 1    Always true (always folded)
598     FIRST_FCMP_PREDICATE = FCMP_FALSE,
599     LAST_FCMP_PREDICATE = FCMP_TRUE,
600     BAD_FCMP_PREDICATE = FCMP_TRUE + 1
601   };
602
603   /// @brief Constructor with insert-before-instruction semantics.
604   FCmpInst(
605     Predicate pred,  ///< The predicate to use for the comparison
606     Value *LHS,      ///< The left-hand-side of the expression
607     Value *RHS,      ///< The right-hand-side of the expression
608     const std::string &Name = "",  ///< Name of the instruction
609     Instruction *InsertBefore = 0  ///< Where to insert
610   ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertBefore) {
611   }
612
613   /// @brief Constructor with insert-at-block-end semantics.
614   FCmpInst(
615     Predicate pred, ///< The predicate to use for the comparison
616     Value *LHS,     ///< The left-hand-side of the expression
617     Value *RHS,     ///< The right-hand-side of the expression
618     const std::string &Name,  ///< Name of the instruction
619     BasicBlock *InsertAtEnd   ///< Block to insert into.
620   ) : CmpInst(Instruction::FCmp, pred, LHS, RHS, Name, InsertAtEnd) {
621   }
622
623   /// @brief Return the predicate for this instruction.
624   Predicate getPredicate() const { return Predicate(SubclassData); }
625
626   /// @brief Set the predicate for this instruction to the specified value.
627   void setPredicate(Predicate P) { SubclassData = P; }
628
629   /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
630   /// @returns the inverse predicate for the instructions current predicate. 
631   /// @brief Return the inverse of the predicate
632   Predicate getInversePredicate() const {
633     return getInversePredicate(getPredicate());
634   }
635
636   /// For example, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
637   /// @returns the inverse predicate for \p pred.
638   /// @brief Return the inverse of a given predicate
639   static Predicate getInversePredicate(Predicate pred);
640
641   /// For example, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
642   /// @returns the predicate that would be the result of exchanging the two 
643   /// operands of the ICmpInst instruction without changing the result 
644   /// produced.  
645   /// @brief Return the predicate as if the operands were swapped
646   Predicate getSwappedPredicate() const {
647     return getSwappedPredicate(getPredicate());
648   }
649
650   /// This is a static version that you can use without an instruction 
651   /// available.
652   /// @brief Return the predicate as if the operands were swapped.
653   static Predicate getSwappedPredicate(Predicate Opcode);
654
655   /// This also tests for commutativity. If isEquality() returns true then
656   /// the predicate is also commutative. Only the equality predicates are
657   /// commutative.
658   /// @returns true if the predicate of this instruction is EQ or NE.
659   /// @brief Determine if this is an equality predicate.
660   bool isEquality() const {
661     return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
662            SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
663   }
664   bool isCommutative() const { return isEquality(); }
665
666   /// @returns true if the predicate is relational (not EQ or NE). 
667   /// @brief Determine if this a relational predicate.
668   bool isRelational() const { return !isEquality(); }
669
670   /// Exchange the two operands to this instruction in such a way that it does
671   /// not modify the semantics of the instruction. The predicate value may be
672   /// changed to retain the same result if the predicate is order dependent
673   /// (e.g. ult). 
674   /// @brief Swap operands and adjust predicate.
675   void swapOperands() {
676     SubclassData = getSwappedPredicate();
677     std::swap(Ops[0], Ops[1]);
678   }
679
680   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
681   static inline bool classof(const FCmpInst *) { return true; }
682   static inline bool classof(const Instruction *I) {
683     return I->getOpcode() == Instruction::FCmp;
684   }
685   static inline bool classof(const Value *V) {
686     return isa<Instruction>(V) && classof(cast<Instruction>(V));
687   }
688 };
689
690 //===----------------------------------------------------------------------===//
691 //                                 CallInst Class
692 //===----------------------------------------------------------------------===//
693
694 /// CallInst - This class represents a function call, abstracting a target
695 /// machine's calling convention.  This class uses low bit of the SubClassData
696 /// field to indicate whether or not this is a tail call.  The rest of the bits
697 /// hold the calling convention of the call.
698 ///
699 class CallInst : public Instruction {
700   CallInst(const CallInst &CI);
701   void init(Value *Func, const std::vector<Value*> &Params);
702   void init(Value *Func, Value *Actual1, Value *Actual2);
703   void init(Value *Func, Value *Actual);
704   void init(Value *Func);
705
706 public:
707   CallInst(Value *F, const std::vector<Value*> &Par,
708            const std::string &Name = "", Instruction *InsertBefore = 0);
709   CallInst(Value *F, const std::vector<Value*> &Par,
710            const std::string &Name, BasicBlock *InsertAtEnd);
711
712   // Alternate CallInst ctors w/ two actuals, w/ one actual and no
713   // actuals, respectively.
714   CallInst(Value *F, Value *Actual1, Value *Actual2,
715            const std::string& Name = "", Instruction *InsertBefore = 0);
716   CallInst(Value *F, Value *Actual1, Value *Actual2,
717            const std::string& Name, BasicBlock *InsertAtEnd);
718   CallInst(Value *F, Value *Actual, const std::string& Name = "",
719            Instruction *InsertBefore = 0);
720   CallInst(Value *F, Value *Actual, const std::string& Name,
721            BasicBlock *InsertAtEnd);
722   explicit CallInst(Value *F, const std::string &Name = "",
723                     Instruction *InsertBefore = 0);
724   CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
725   ~CallInst();
726
727   virtual CallInst *clone() const;
728   bool mayWriteToMemory() const { return true; }
729
730   bool isTailCall() const           { return SubclassData & 1; }
731   void setTailCall(bool isTailCall = true) {
732     SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
733   }
734
735   /// getCallingConv/setCallingConv - Get or set the calling convention of this
736   /// function call.
737   unsigned getCallingConv() const { return SubclassData >> 1; }
738   void setCallingConv(unsigned CC) {
739     SubclassData = (SubclassData & 1) | (CC << 1);
740   }
741
742   /// getCalledFunction - Return the function being called by this instruction
743   /// if it is a direct call.  If it is a call through a function pointer,
744   /// return null.
745   Function *getCalledFunction() const {
746     return static_cast<Function*>(dyn_cast<Function>(getOperand(0)));
747   }
748
749   /// getCalledValue - Get a pointer to the function that is invoked by this 
750   /// instruction
751   inline const Value *getCalledValue() const { return getOperand(0); }
752   inline       Value *getCalledValue()       { return getOperand(0); }
753
754   // Methods for support type inquiry through isa, cast, and dyn_cast:
755   static inline bool classof(const CallInst *) { return true; }
756   static inline bool classof(const Instruction *I) {
757     return I->getOpcode() == Instruction::Call;
758   }
759   static inline bool classof(const Value *V) {
760     return isa<Instruction>(V) && classof(cast<Instruction>(V));
761   }
762 };
763
764 //===----------------------------------------------------------------------===//
765 //                               SelectInst Class
766 //===----------------------------------------------------------------------===//
767
768 /// SelectInst - This class represents the LLVM 'select' instruction.
769 ///
770 class SelectInst : public Instruction {
771   Use Ops[3];
772
773   void init(Value *C, Value *S1, Value *S2) {
774     Ops[0].init(C, this);
775     Ops[1].init(S1, this);
776     Ops[2].init(S2, this);
777   }
778
779   SelectInst(const SelectInst &SI)
780     : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
781     init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
782   }
783 public:
784   SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
785              Instruction *InsertBefore = 0)
786     : Instruction(S1->getType(), Instruction::Select, Ops, 3,
787                   Name, InsertBefore) {
788     init(C, S1, S2);
789   }
790   SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
791              BasicBlock *InsertAtEnd)
792     : Instruction(S1->getType(), Instruction::Select, Ops, 3,
793                   Name, InsertAtEnd) {
794     init(C, S1, S2);
795   }
796
797   Value *getCondition() const { return Ops[0]; }
798   Value *getTrueValue() const { return Ops[1]; }
799   Value *getFalseValue() const { return Ops[2]; }
800
801   /// Transparently provide more efficient getOperand methods.
802   Value *getOperand(unsigned i) const {
803     assert(i < 3 && "getOperand() out of range!");
804     return Ops[i];
805   }
806   void setOperand(unsigned i, Value *Val) {
807     assert(i < 3 && "setOperand() out of range!");
808     Ops[i] = Val;
809   }
810   unsigned getNumOperands() const { return 3; }
811
812   OtherOps getOpcode() const {
813     return static_cast<OtherOps>(Instruction::getOpcode());
814   }
815
816   virtual SelectInst *clone() const;
817
818   // Methods for support type inquiry through isa, cast, and dyn_cast:
819   static inline bool classof(const SelectInst *) { return true; }
820   static inline bool classof(const Instruction *I) {
821     return I->getOpcode() == Instruction::Select;
822   }
823   static inline bool classof(const Value *V) {
824     return isa<Instruction>(V) && classof(cast<Instruction>(V));
825   }
826 };
827
828 //===----------------------------------------------------------------------===//
829 //                                VAArgInst Class
830 //===----------------------------------------------------------------------===//
831
832 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
833 /// an argument of the specified type given a va_list and increments that list
834 ///
835 class VAArgInst : public UnaryInstruction {
836   VAArgInst(const VAArgInst &VAA)
837     : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
838 public:
839   VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
840              Instruction *InsertBefore = 0)
841     : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
842   }
843   VAArgInst(Value *List, const Type *Ty, const std::string &Name,
844             BasicBlock *InsertAtEnd)
845     : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
846   }
847
848   virtual VAArgInst *clone() const;
849   bool mayWriteToMemory() const { return true; }
850
851   // Methods for support type inquiry through isa, cast, and dyn_cast:
852   static inline bool classof(const VAArgInst *) { return true; }
853   static inline bool classof(const Instruction *I) {
854     return I->getOpcode() == VAArg;
855   }
856   static inline bool classof(const Value *V) {
857     return isa<Instruction>(V) && classof(cast<Instruction>(V));
858   }
859 };
860
861 //===----------------------------------------------------------------------===//
862 //                                ExtractElementInst Class
863 //===----------------------------------------------------------------------===//
864
865 /// ExtractElementInst - This instruction extracts a single (scalar)
866 /// element from a PackedType value
867 ///
868 class ExtractElementInst : public Instruction {
869   Use Ops[2];
870   ExtractElementInst(const ExtractElementInst &EE) :
871     Instruction(EE.getType(), ExtractElement, Ops, 2) {
872     Ops[0].init(EE.Ops[0], this);
873     Ops[1].init(EE.Ops[1], this);
874   }
875
876 public:
877   ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
878                      Instruction *InsertBefore = 0);
879   ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
880                      Instruction *InsertBefore = 0);
881   ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
882                      BasicBlock *InsertAtEnd);
883   ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
884                      BasicBlock *InsertAtEnd);
885
886   /// isValidOperands - Return true if an extractelement instruction can be
887   /// formed with the specified operands.
888   static bool isValidOperands(const Value *Vec, const Value *Idx);
889
890   virtual ExtractElementInst *clone() const;
891
892   virtual bool mayWriteToMemory() const { return false; }
893
894   /// Transparently provide more efficient getOperand methods.
895   Value *getOperand(unsigned i) const {
896     assert(i < 2 && "getOperand() out of range!");
897     return Ops[i];
898   }
899   void setOperand(unsigned i, Value *Val) {
900     assert(i < 2 && "setOperand() out of range!");
901     Ops[i] = Val;
902   }
903   unsigned getNumOperands() const { return 2; }
904
905   // Methods for support type inquiry through isa, cast, and dyn_cast:
906   static inline bool classof(const ExtractElementInst *) { return true; }
907   static inline bool classof(const Instruction *I) {
908     return I->getOpcode() == Instruction::ExtractElement;
909   }
910   static inline bool classof(const Value *V) {
911     return isa<Instruction>(V) && classof(cast<Instruction>(V));
912   }
913 };
914
915 //===----------------------------------------------------------------------===//
916 //                                InsertElementInst Class
917 //===----------------------------------------------------------------------===//
918
919 /// InsertElementInst - This instruction inserts a single (scalar)
920 /// element into a PackedType value
921 ///
922 class InsertElementInst : public Instruction {
923   Use Ops[3];
924   InsertElementInst(const InsertElementInst &IE);
925 public:
926   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
927                     const std::string &Name = "",Instruction *InsertBefore = 0);
928   InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
929                     const std::string &Name = "",Instruction *InsertBefore = 0);
930   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
931                     const std::string &Name, BasicBlock *InsertAtEnd);
932   InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
933                     const std::string &Name, BasicBlock *InsertAtEnd);
934
935   /// isValidOperands - Return true if an insertelement instruction can be
936   /// formed with the specified operands.
937   static bool isValidOperands(const Value *Vec, const Value *NewElt,
938                               const Value *Idx);
939
940   virtual InsertElementInst *clone() const;
941
942   virtual bool mayWriteToMemory() const { return false; }
943
944   /// getType - Overload to return most specific packed type.
945   ///
946   inline const PackedType *getType() const {
947     return reinterpret_cast<const PackedType*>(Instruction::getType());
948   }
949
950   /// Transparently provide more efficient getOperand methods.
951   Value *getOperand(unsigned i) const {
952     assert(i < 3 && "getOperand() out of range!");
953     return Ops[i];
954   }
955   void setOperand(unsigned i, Value *Val) {
956     assert(i < 3 && "setOperand() out of range!");
957     Ops[i] = Val;
958   }
959   unsigned getNumOperands() const { return 3; }
960
961   // Methods for support type inquiry through isa, cast, and dyn_cast:
962   static inline bool classof(const InsertElementInst *) { return true; }
963   static inline bool classof(const Instruction *I) {
964     return I->getOpcode() == Instruction::InsertElement;
965   }
966   static inline bool classof(const Value *V) {
967     return isa<Instruction>(V) && classof(cast<Instruction>(V));
968   }
969 };
970
971 //===----------------------------------------------------------------------===//
972 //                           ShuffleVectorInst Class
973 //===----------------------------------------------------------------------===//
974
975 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
976 /// input vectors.
977 ///
978 class ShuffleVectorInst : public Instruction {
979   Use Ops[3];
980   ShuffleVectorInst(const ShuffleVectorInst &IE);
981 public:
982   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
983                     const std::string &Name = "", Instruction *InsertBefor = 0);
984   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
985                     const std::string &Name, BasicBlock *InsertAtEnd);
986
987   /// isValidOperands - Return true if a shufflevector instruction can be
988   /// formed with the specified operands.
989   static bool isValidOperands(const Value *V1, const Value *V2,
990                               const Value *Mask);
991
992   virtual ShuffleVectorInst *clone() const;
993
994   virtual bool mayWriteToMemory() const { return false; }
995
996   /// getType - Overload to return most specific packed type.
997   ///
998   inline const PackedType *getType() const {
999     return reinterpret_cast<const PackedType*>(Instruction::getType());
1000   }
1001
1002   /// Transparently provide more efficient getOperand methods.
1003   Value *getOperand(unsigned i) const {
1004     assert(i < 3 && "getOperand() out of range!");
1005     return Ops[i];
1006   }
1007   void setOperand(unsigned i, Value *Val) {
1008     assert(i < 3 && "setOperand() out of range!");
1009     Ops[i] = Val;
1010   }
1011   unsigned getNumOperands() const { return 3; }
1012
1013   // Methods for support type inquiry through isa, cast, and dyn_cast:
1014   static inline bool classof(const ShuffleVectorInst *) { return true; }
1015   static inline bool classof(const Instruction *I) {
1016     return I->getOpcode() == Instruction::ShuffleVector;
1017   }
1018   static inline bool classof(const Value *V) {
1019     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1020   }
1021 };
1022
1023
1024 //===----------------------------------------------------------------------===//
1025 //                               PHINode Class
1026 //===----------------------------------------------------------------------===//
1027
1028 // PHINode - The PHINode class is used to represent the magical mystical PHI
1029 // node, that can not exist in nature, but can be synthesized in a computer
1030 // scientist's overactive imagination.
1031 //
1032 class PHINode : public Instruction {
1033   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
1034   /// the number actually in use.
1035   unsigned ReservedSpace;
1036   PHINode(const PHINode &PN);
1037 public:
1038   explicit PHINode(const Type *Ty, const std::string &Name = "",
1039                    Instruction *InsertBefore = 0)
1040     : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
1041       ReservedSpace(0) {
1042   }
1043
1044   PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
1045     : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
1046       ReservedSpace(0) {
1047   }
1048
1049   ~PHINode();
1050
1051   /// reserveOperandSpace - This method can be used to avoid repeated
1052   /// reallocation of PHI operand lists by reserving space for the correct
1053   /// number of operands before adding them.  Unlike normal vector reserves,
1054   /// this method can also be used to trim the operand space.
1055   void reserveOperandSpace(unsigned NumValues) {
1056     resizeOperands(NumValues*2);
1057   }
1058
1059   virtual PHINode *clone() const;
1060
1061   /// getNumIncomingValues - Return the number of incoming edges
1062   ///
1063   unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1064
1065   /// getIncomingValue - Return incoming value number x
1066   ///
1067   Value *getIncomingValue(unsigned i) const {
1068     assert(i*2 < getNumOperands() && "Invalid value number!");
1069     return getOperand(i*2);
1070   }
1071   void setIncomingValue(unsigned i, Value *V) {
1072     assert(i*2 < getNumOperands() && "Invalid value number!");
1073     setOperand(i*2, V);
1074   }
1075   unsigned getOperandNumForIncomingValue(unsigned i) {
1076     return i*2;
1077   }
1078
1079   /// getIncomingBlock - Return incoming basic block number x
1080   ///
1081   BasicBlock *getIncomingBlock(unsigned i) const {
1082     return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
1083   }
1084   void setIncomingBlock(unsigned i, BasicBlock *BB) {
1085     setOperand(i*2+1, reinterpret_cast<Value*>(BB));
1086   }
1087   unsigned getOperandNumForIncomingBlock(unsigned i) {
1088     return i*2+1;
1089   }
1090
1091   /// addIncoming - Add an incoming value to the end of the PHI list
1092   ///
1093   void addIncoming(Value *V, BasicBlock *BB) {
1094     assert(getType() == V->getType() &&
1095            "All operands to PHI node must be the same type as the PHI node!");
1096     unsigned OpNo = NumOperands;
1097     if (OpNo+2 > ReservedSpace)
1098       resizeOperands(0);  // Get more space!
1099     // Initialize some new operands.
1100     NumOperands = OpNo+2;
1101     OperandList[OpNo].init(V, this);
1102     OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
1103   }
1104
1105   /// removeIncomingValue - Remove an incoming value.  This is useful if a
1106   /// predecessor basic block is deleted.  The value removed is returned.
1107   ///
1108   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1109   /// is true), the PHI node is destroyed and any uses of it are replaced with
1110   /// dummy values.  The only time there should be zero incoming values to a PHI
1111   /// node is when the block is dead, so this strategy is sound.
1112   ///
1113   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1114
1115   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1116     int Idx = getBasicBlockIndex(BB);
1117     assert(Idx >= 0 && "Invalid basic block argument to remove!");
1118     return removeIncomingValue(Idx, DeletePHIIfEmpty);
1119   }
1120
1121   /// getBasicBlockIndex - Return the first index of the specified basic
1122   /// block in the value list for this PHI.  Returns -1 if no instance.
1123   ///
1124   int getBasicBlockIndex(const BasicBlock *BB) const {
1125     Use *OL = OperandList;
1126     for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1127       if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
1128     return -1;
1129   }
1130
1131   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1132     return getIncomingValue(getBasicBlockIndex(BB));
1133   }
1134
1135   /// hasConstantValue - If the specified PHI node always merges together the
1136   /// same value, return the value, otherwise return null.
1137   ///
1138   Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1139
1140   /// Methods for support type inquiry through isa, cast, and dyn_cast:
1141   static inline bool classof(const PHINode *) { return true; }
1142   static inline bool classof(const Instruction *I) {
1143     return I->getOpcode() == Instruction::PHI;
1144   }
1145   static inline bool classof(const Value *V) {
1146     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1147   }
1148  private:
1149   void resizeOperands(unsigned NumOperands);
1150 };
1151
1152 //===----------------------------------------------------------------------===//
1153 //                               ReturnInst Class
1154 //===----------------------------------------------------------------------===//
1155
1156 //===---------------------------------------------------------------------------
1157 /// ReturnInst - Return a value (possibly void), from a function.  Execution
1158 /// does not continue in this function any longer.
1159 ///
1160 class ReturnInst : public TerminatorInst {
1161   Use RetVal;  // Possibly null retval.
1162   ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
1163                                                     RI.getNumOperands()) {
1164     if (RI.getNumOperands())
1165       RetVal.init(RI.RetVal, this);
1166   }
1167
1168   void init(Value *RetVal);
1169
1170 public:
1171   // ReturnInst constructors:
1172   // ReturnInst()                  - 'ret void' instruction
1173   // ReturnInst(    null)          - 'ret void' instruction
1174   // ReturnInst(Value* X)          - 'ret X'    instruction
1175   // ReturnInst(    null, Inst *)  - 'ret void' instruction, insert before I
1176   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
1177   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of BB
1178   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of BB
1179   //
1180   // NOTE: If the Value* passed is of type void then the constructor behaves as
1181   // if it was passed NULL.
1182   explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
1183     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
1184     init(retVal);
1185   }
1186   ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
1187     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1188     init(retVal);
1189   }
1190   explicit ReturnInst(BasicBlock *InsertAtEnd)
1191     : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1192   }
1193
1194   virtual ReturnInst *clone() const;
1195
1196   // Transparently provide more efficient getOperand methods.
1197   Value *getOperand(unsigned i) const {
1198     assert(i < getNumOperands() && "getOperand() out of range!");
1199     return RetVal;
1200   }
1201   void setOperand(unsigned i, Value *Val) {
1202     assert(i < getNumOperands() && "setOperand() out of range!");
1203     RetVal = Val;
1204   }
1205
1206   Value *getReturnValue() const { return RetVal; }
1207
1208   unsigned getNumSuccessors() const { return 0; }
1209
1210   // Methods for support type inquiry through isa, cast, and dyn_cast:
1211   static inline bool classof(const ReturnInst *) { return true; }
1212   static inline bool classof(const Instruction *I) {
1213     return (I->getOpcode() == Instruction::Ret);
1214   }
1215   static inline bool classof(const Value *V) {
1216     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1217   }
1218  private:
1219   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1220   virtual unsigned getNumSuccessorsV() const;
1221   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1222 };
1223
1224 //===----------------------------------------------------------------------===//
1225 //                               BranchInst Class
1226 //===----------------------------------------------------------------------===//
1227
1228 //===---------------------------------------------------------------------------
1229 /// BranchInst - Conditional or Unconditional Branch instruction.
1230 ///
1231 class BranchInst : public TerminatorInst {
1232   /// Ops list - Branches are strange.  The operands are ordered:
1233   ///  TrueDest, FalseDest, Cond.  This makes some accessors faster because
1234   /// they don't have to check for cond/uncond branchness.
1235   Use Ops[3];
1236   BranchInst(const BranchInst &BI);
1237   void AssertOK();
1238 public:
1239   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1240   // BranchInst(BB *B)                           - 'br B'
1241   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
1242   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
1243   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1244   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
1245   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
1246   explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
1247     : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
1248     assert(IfTrue != 0 && "Branch destination may not be null!");
1249     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1250   }
1251   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1252              Instruction *InsertBefore = 0)
1253     : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
1254     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1255     Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1256     Ops[2].init(Cond, this);
1257 #ifndef NDEBUG
1258     AssertOK();
1259 #endif
1260   }
1261
1262   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
1263     : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
1264     assert(IfTrue != 0 && "Branch destination may not be null!");
1265     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1266   }
1267
1268   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1269              BasicBlock *InsertAtEnd)
1270     : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
1271     Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1272     Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1273     Ops[2].init(Cond, this);
1274 #ifndef NDEBUG
1275     AssertOK();
1276 #endif
1277   }
1278
1279
1280   /// Transparently provide more efficient getOperand methods.
1281   Value *getOperand(unsigned i) const {
1282     assert(i < getNumOperands() && "getOperand() out of range!");
1283     return Ops[i];
1284   }
1285   void setOperand(unsigned i, Value *Val) {
1286     assert(i < getNumOperands() && "setOperand() out of range!");
1287     Ops[i] = Val;
1288   }
1289
1290   virtual BranchInst *clone() const;
1291
1292   inline bool isUnconditional() const { return getNumOperands() == 1; }
1293   inline bool isConditional()   const { return getNumOperands() == 3; }
1294
1295   inline Value *getCondition() const {
1296     assert(isConditional() && "Cannot get condition of an uncond branch!");
1297     return getOperand(2);
1298   }
1299
1300   void setCondition(Value *V) {
1301     assert(isConditional() && "Cannot set condition of unconditional branch!");
1302     setOperand(2, V);
1303   }
1304
1305   // setUnconditionalDest - Change the current branch to an unconditional branch
1306   // targeting the specified block.
1307   // FIXME: Eliminate this ugly method.
1308   void setUnconditionalDest(BasicBlock *Dest) {
1309     if (isConditional()) {  // Convert this to an uncond branch.
1310       NumOperands = 1;
1311       Ops[1].set(0);
1312       Ops[2].set(0);
1313     }
1314     setOperand(0, reinterpret_cast<Value*>(Dest));
1315   }
1316
1317   unsigned getNumSuccessors() const { return 1+isConditional(); }
1318
1319   BasicBlock *getSuccessor(unsigned i) const {
1320     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1321     return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1322                       cast<BasicBlock>(getOperand(1));
1323   }
1324
1325   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1326     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1327     setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1328   }
1329
1330   // Methods for support type inquiry through isa, cast, and dyn_cast:
1331   static inline bool classof(const BranchInst *) { return true; }
1332   static inline bool classof(const Instruction *I) {
1333     return (I->getOpcode() == Instruction::Br);
1334   }
1335   static inline bool classof(const Value *V) {
1336     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1337   }
1338 private:
1339   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1340   virtual unsigned getNumSuccessorsV() const;
1341   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1342 };
1343
1344 //===----------------------------------------------------------------------===//
1345 //                               SwitchInst Class
1346 //===----------------------------------------------------------------------===//
1347
1348 //===---------------------------------------------------------------------------
1349 /// SwitchInst - Multiway switch
1350 ///
1351 class SwitchInst : public TerminatorInst {
1352   unsigned ReservedSpace;
1353   // Operand[0]    = Value to switch on
1354   // Operand[1]    = Default basic block destination
1355   // Operand[2n  ] = Value to match
1356   // Operand[2n+1] = BasicBlock to go to on match
1357   SwitchInst(const SwitchInst &RI);
1358   void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1359   void resizeOperands(unsigned No);
1360 public:
1361   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1362   /// switch on and a default destination.  The number of additional cases can
1363   /// be specified here to make memory allocation more efficient.  This
1364   /// constructor can also autoinsert before another instruction.
1365   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1366              Instruction *InsertBefore = 0)
1367     : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1368     init(Value, Default, NumCases);
1369   }
1370
1371   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1372   /// switch on and a default destination.  The number of additional cases can
1373   /// be specified here to make memory allocation more efficient.  This
1374   /// constructor also autoinserts at the end of the specified BasicBlock.
1375   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1376              BasicBlock *InsertAtEnd)
1377     : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1378     init(Value, Default, NumCases);
1379   }
1380   ~SwitchInst();
1381
1382
1383   // Accessor Methods for Switch stmt
1384   inline Value *getCondition() const { return getOperand(0); }
1385   void setCondition(Value *V) { setOperand(0, V); }
1386
1387   inline BasicBlock *getDefaultDest() const {
1388     return cast<BasicBlock>(getOperand(1));
1389   }
1390
1391   /// getNumCases - return the number of 'cases' in this switch instruction.
1392   /// Note that case #0 is always the default case.
1393   unsigned getNumCases() const {
1394     return getNumOperands()/2;
1395   }
1396
1397   /// getCaseValue - Return the specified case value.  Note that case #0, the
1398   /// default destination, does not have a case value.
1399   ConstantInt *getCaseValue(unsigned i) {
1400     assert(i && i < getNumCases() && "Illegal case value to get!");
1401     return getSuccessorValue(i);
1402   }
1403
1404   /// getCaseValue - Return the specified case value.  Note that case #0, the
1405   /// default destination, does not have a case value.
1406   const ConstantInt *getCaseValue(unsigned i) const {
1407     assert(i && i < getNumCases() && "Illegal case value to get!");
1408     return getSuccessorValue(i);
1409   }
1410
1411   /// findCaseValue - Search all of the case values for the specified constant.
1412   /// If it is explicitly handled, return the case number of it, otherwise
1413   /// return 0 to indicate that it is handled by the default handler.
1414   unsigned findCaseValue(const ConstantInt *C) const {
1415     for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1416       if (getCaseValue(i) == C)
1417         return i;
1418     return 0;
1419   }
1420
1421   /// findCaseDest - Finds the unique case value for a given successor. Returns
1422   /// null if the successor is not found, not unique, or is the default case.
1423   ConstantInt *findCaseDest(BasicBlock *BB) {
1424     if (BB == getDefaultDest()) return NULL;
1425
1426     ConstantInt *CI = NULL;
1427     for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
1428       if (getSuccessor(i) == BB) {
1429         if (CI) return NULL;   // Multiple cases lead to BB.
1430         else CI = getCaseValue(i);
1431       }
1432     }
1433     return CI;
1434   }
1435
1436   /// addCase - Add an entry to the switch instruction...
1437   ///
1438   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1439
1440   /// removeCase - This method removes the specified successor from the switch
1441   /// instruction.  Note that this cannot be used to remove the default
1442   /// destination (successor #0).
1443   ///
1444   void removeCase(unsigned idx);
1445
1446   virtual SwitchInst *clone() const;
1447
1448   unsigned getNumSuccessors() const { return getNumOperands()/2; }
1449   BasicBlock *getSuccessor(unsigned idx) const {
1450     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1451     return cast<BasicBlock>(getOperand(idx*2+1));
1452   }
1453   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1454     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1455     setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1456   }
1457
1458   // getSuccessorValue - Return the value associated with the specified
1459   // successor.
1460   inline ConstantInt *getSuccessorValue(unsigned idx) const {
1461     assert(idx < getNumSuccessors() && "Successor # out of range!");
1462     return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
1463   }
1464
1465   // Methods for support type inquiry through isa, cast, and dyn_cast:
1466   static inline bool classof(const SwitchInst *) { return true; }
1467   static inline bool classof(const Instruction *I) {
1468     return I->getOpcode() == Instruction::Switch;
1469   }
1470   static inline bool classof(const Value *V) {
1471     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1472   }
1473 private:
1474   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1475   virtual unsigned getNumSuccessorsV() const;
1476   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1477 };
1478
1479 //===----------------------------------------------------------------------===//
1480 //                               InvokeInst Class
1481 //===----------------------------------------------------------------------===//
1482
1483 //===---------------------------------------------------------------------------
1484
1485 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
1486 /// calling convention of the call.
1487 ///
1488 class InvokeInst : public TerminatorInst {
1489   InvokeInst(const InvokeInst &BI);
1490   void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1491             const std::vector<Value*> &Params);
1492 public:
1493   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1494              const std::vector<Value*> &Params, const std::string &Name = "",
1495              Instruction *InsertBefore = 0);
1496   InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1497              const std::vector<Value*> &Params, const std::string &Name,
1498              BasicBlock *InsertAtEnd);
1499   ~InvokeInst();
1500
1501   virtual InvokeInst *clone() const;
1502
1503   bool mayWriteToMemory() const { return true; }
1504
1505   /// getCallingConv/setCallingConv - Get or set the calling convention of this
1506   /// function call.
1507   unsigned getCallingConv() const { return SubclassData; }
1508   void setCallingConv(unsigned CC) {
1509     SubclassData = CC;
1510   }
1511
1512   /// getCalledFunction - Return the function called, or null if this is an
1513   /// indirect function invocation.
1514   ///
1515   Function *getCalledFunction() const {
1516     return dyn_cast<Function>(getOperand(0));
1517   }
1518
1519   // getCalledValue - Get a pointer to a function that is invoked by this inst.
1520   inline Value *getCalledValue() const { return getOperand(0); }
1521
1522   // get*Dest - Return the destination basic blocks...
1523   BasicBlock *getNormalDest() const {
1524     return cast<BasicBlock>(getOperand(1));
1525   }
1526   BasicBlock *getUnwindDest() const {
1527     return cast<BasicBlock>(getOperand(2));
1528   }
1529   void setNormalDest(BasicBlock *B) {
1530     setOperand(1, reinterpret_cast<Value*>(B));
1531   }
1532
1533   void setUnwindDest(BasicBlock *B) {
1534     setOperand(2, reinterpret_cast<Value*>(B));
1535   }
1536
1537   inline BasicBlock *getSuccessor(unsigned i) const {
1538     assert(i < 2 && "Successor # out of range for invoke!");
1539     return i == 0 ? getNormalDest() : getUnwindDest();
1540   }
1541
1542   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1543     assert(idx < 2 && "Successor # out of range for invoke!");
1544     setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1545   }
1546
1547   unsigned getNumSuccessors() const { return 2; }
1548
1549   // Methods for support type inquiry through isa, cast, and dyn_cast:
1550   static inline bool classof(const InvokeInst *) { return true; }
1551   static inline bool classof(const Instruction *I) {
1552     return (I->getOpcode() == Instruction::Invoke);
1553   }
1554   static inline bool classof(const Value *V) {
1555     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1556   }
1557 private:
1558   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1559   virtual unsigned getNumSuccessorsV() const;
1560   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1561 };
1562
1563
1564 //===----------------------------------------------------------------------===//
1565 //                              UnwindInst Class
1566 //===----------------------------------------------------------------------===//
1567
1568 //===---------------------------------------------------------------------------
1569 /// UnwindInst - Immediately exit the current function, unwinding the stack
1570 /// until an invoke instruction is found.
1571 ///
1572 class UnwindInst : public TerminatorInst {
1573 public:
1574   explicit UnwindInst(Instruction *InsertBefore = 0)
1575     : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
1576   }
1577   explicit UnwindInst(BasicBlock *InsertAtEnd)
1578     : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
1579   }
1580
1581   virtual UnwindInst *clone() const;
1582
1583   unsigned getNumSuccessors() const { return 0; }
1584
1585   // Methods for support type inquiry through isa, cast, and dyn_cast:
1586   static inline bool classof(const UnwindInst *) { return true; }
1587   static inline bool classof(const Instruction *I) {
1588     return I->getOpcode() == Instruction::Unwind;
1589   }
1590   static inline bool classof(const Value *V) {
1591     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1592   }
1593 private:
1594   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1595   virtual unsigned getNumSuccessorsV() const;
1596   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1597 };
1598
1599 //===----------------------------------------------------------------------===//
1600 //                           UnreachableInst Class
1601 //===----------------------------------------------------------------------===//
1602
1603 //===---------------------------------------------------------------------------
1604 /// UnreachableInst - This function has undefined behavior.  In particular, the
1605 /// presence of this instruction indicates some higher level knowledge that the
1606 /// end of the block cannot be reached.
1607 ///
1608 class UnreachableInst : public TerminatorInst {
1609 public:
1610   explicit UnreachableInst(Instruction *InsertBefore = 0)
1611     : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
1612   }
1613   explicit UnreachableInst(BasicBlock *InsertAtEnd)
1614     : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
1615   }
1616
1617   virtual UnreachableInst *clone() const;
1618
1619   unsigned getNumSuccessors() const { return 0; }
1620
1621   // Methods for support type inquiry through isa, cast, and dyn_cast:
1622   static inline bool classof(const UnreachableInst *) { return true; }
1623   static inline bool classof(const Instruction *I) {
1624     return I->getOpcode() == Instruction::Unreachable;
1625   }
1626   static inline bool classof(const Value *V) {
1627     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1628   }
1629 private:
1630   virtual BasicBlock *getSuccessorV(unsigned idx) const;
1631   virtual unsigned getNumSuccessorsV() const;
1632   virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1633 };
1634
1635 //===----------------------------------------------------------------------===//
1636 //                                 TruncInst Class
1637 //===----------------------------------------------------------------------===//
1638
1639 /// @brief This class represents a truncation of integer types.
1640 class TruncInst : public CastInst {
1641   /// Private copy constructor
1642   TruncInst(const TruncInst &CI)
1643     : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
1644   }
1645 public:
1646   /// @brief Constructor with insert-before-instruction semantics
1647   TruncInst(
1648     Value *S,                     ///< The value to be truncated
1649     const Type *Ty,               ///< The (smaller) type to truncate to
1650     const std::string &Name = "", ///< A name for the new instruction
1651     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1652   );
1653
1654   /// @brief Constructor with insert-at-end-of-block semantics
1655   TruncInst(
1656     Value *S,                     ///< The value to be truncated
1657     const Type *Ty,               ///< The (smaller) type to truncate to
1658     const std::string &Name,      ///< A name for the new instruction
1659     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
1660   );
1661
1662   /// @brief Clone an identical TruncInst
1663   virtual CastInst *clone() const;
1664
1665   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1666   static inline bool classof(const TruncInst *) { return true; }
1667   static inline bool classof(const Instruction *I) {
1668     return I->getOpcode() == Trunc;
1669   }
1670   static inline bool classof(const Value *V) {
1671     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1672   }
1673 };
1674
1675 //===----------------------------------------------------------------------===//
1676 //                                 ZExtInst Class
1677 //===----------------------------------------------------------------------===//
1678
1679 /// @brief This class represents zero extension of integer types.
1680 class ZExtInst : public CastInst {
1681   /// @brief Private copy constructor
1682   ZExtInst(const ZExtInst &CI)
1683     : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
1684   }
1685 public:
1686   /// @brief Constructor with insert-before-instruction semantics
1687   ZExtInst(
1688     Value *S,                     ///< The value to be zero extended
1689     const Type *Ty,               ///< The type to zero extend to
1690     const std::string &Name = "", ///< A name for the new instruction
1691     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1692   );
1693
1694   /// @brief Constructor with insert-at-end semantics.
1695   ZExtInst(
1696     Value *S,                     ///< The value to be zero extended
1697     const Type *Ty,               ///< The type to zero extend to
1698     const std::string &Name,      ///< A name for the new instruction
1699     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
1700   );
1701
1702   /// @brief Clone an identical ZExtInst
1703   virtual CastInst *clone() const;
1704
1705   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1706   static inline bool classof(const ZExtInst *) { return true; }
1707   static inline bool classof(const Instruction *I) {
1708     return I->getOpcode() == ZExt;
1709   }
1710   static inline bool classof(const Value *V) {
1711     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1712   }
1713 };
1714
1715 //===----------------------------------------------------------------------===//
1716 //                                 SExtInst Class
1717 //===----------------------------------------------------------------------===//
1718
1719 /// @brief This class represents a sign extension of integer types.
1720 class SExtInst : public CastInst {
1721   /// @brief Private copy constructor
1722   SExtInst(const SExtInst &CI)
1723     : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
1724   }
1725 public:
1726   /// @brief Constructor with insert-before-instruction semantics
1727   SExtInst(
1728     Value *S,                     ///< The value to be sign extended
1729     const Type *Ty,               ///< The type to sign extend to
1730     const std::string &Name = "", ///< A name for the new instruction
1731     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1732   );
1733
1734   /// @brief Constructor with insert-at-end-of-block semantics
1735   SExtInst(
1736     Value *S,                     ///< The value to be sign extended
1737     const Type *Ty,               ///< The type to sign extend to
1738     const std::string &Name,      ///< A name for the new instruction
1739     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
1740   );
1741
1742   /// @brief Clone an identical SExtInst
1743   virtual CastInst *clone() const;
1744
1745   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1746   static inline bool classof(const SExtInst *) { return true; }
1747   static inline bool classof(const Instruction *I) {
1748     return I->getOpcode() == SExt;
1749   }
1750   static inline bool classof(const Value *V) {
1751     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1752   }
1753 };
1754
1755 //===----------------------------------------------------------------------===//
1756 //                                 FPTruncInst Class
1757 //===----------------------------------------------------------------------===//
1758
1759 /// @brief This class represents a truncation of floating point types.
1760 class FPTruncInst : public CastInst {
1761   FPTruncInst(const FPTruncInst &CI)
1762     : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
1763   }
1764 public:
1765   /// @brief Constructor with insert-before-instruction semantics
1766   FPTruncInst(
1767     Value *S,                     ///< The value to be truncated
1768     const Type *Ty,               ///< The type to truncate to
1769     const std::string &Name = "", ///< A name for the new instruction
1770     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1771   );
1772
1773   /// @brief Constructor with insert-before-instruction semantics
1774   FPTruncInst(
1775     Value *S,                     ///< The value to be truncated
1776     const Type *Ty,               ///< The type to truncate to
1777     const std::string &Name,      ///< A name for the new instruction
1778     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
1779   );
1780
1781   /// @brief Clone an identical FPTruncInst
1782   virtual CastInst *clone() const;
1783
1784   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1785   static inline bool classof(const FPTruncInst *) { return true; }
1786   static inline bool classof(const Instruction *I) {
1787     return I->getOpcode() == FPTrunc;
1788   }
1789   static inline bool classof(const Value *V) {
1790     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1791   }
1792 };
1793
1794 //===----------------------------------------------------------------------===//
1795 //                                 FPExtInst Class
1796 //===----------------------------------------------------------------------===//
1797
1798 /// @brief This class represents an extension of floating point types.
1799 class FPExtInst : public CastInst {
1800   FPExtInst(const FPExtInst &CI)
1801     : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
1802   }
1803 public:
1804   /// @brief Constructor with insert-before-instruction semantics
1805   FPExtInst(
1806     Value *S,                     ///< The value to be extended
1807     const Type *Ty,               ///< The type to extend to
1808     const std::string &Name = "", ///< A name for the new instruction
1809     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1810   );
1811
1812   /// @brief Constructor with insert-at-end-of-block semantics
1813   FPExtInst(
1814     Value *S,                     ///< The value to be extended
1815     const Type *Ty,               ///< The type to extend to
1816     const std::string &Name,      ///< A name for the new instruction
1817     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
1818   );
1819
1820   /// @brief Clone an identical FPExtInst
1821   virtual CastInst *clone() const;
1822
1823   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1824   static inline bool classof(const FPExtInst *) { return true; }
1825   static inline bool classof(const Instruction *I) {
1826     return I->getOpcode() == FPExt;
1827   }
1828   static inline bool classof(const Value *V) {
1829     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1830   }
1831 };
1832
1833 //===----------------------------------------------------------------------===//
1834 //                                 UIToFPInst Class
1835 //===----------------------------------------------------------------------===//
1836
1837 /// @brief This class represents a cast unsigned integer to floating point.
1838 class UIToFPInst : public CastInst {
1839   UIToFPInst(const UIToFPInst &CI)
1840     : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
1841   }
1842 public:
1843   /// @brief Constructor with insert-before-instruction semantics
1844   UIToFPInst(
1845     Value *S,                     ///< The value to be converted
1846     const Type *Ty,               ///< The type to convert to
1847     const std::string &Name = "", ///< A name for the new instruction
1848     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1849   );
1850
1851   /// @brief Constructor with insert-at-end-of-block semantics
1852   UIToFPInst(
1853     Value *S,                     ///< The value to be converted
1854     const Type *Ty,               ///< The type to convert to
1855     const std::string &Name,      ///< A name for the new instruction
1856     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
1857   );
1858
1859   /// @brief Clone an identical UIToFPInst
1860   virtual CastInst *clone() const;
1861
1862   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1863   static inline bool classof(const UIToFPInst *) { return true; }
1864   static inline bool classof(const Instruction *I) {
1865     return I->getOpcode() == UIToFP;
1866   }
1867   static inline bool classof(const Value *V) {
1868     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1869   }
1870 };
1871
1872 //===----------------------------------------------------------------------===//
1873 //                                 SIToFPInst Class
1874 //===----------------------------------------------------------------------===//
1875
1876 /// @brief This class represents a cast from signed integer to floating point.
1877 class SIToFPInst : public CastInst {
1878   SIToFPInst(const SIToFPInst &CI)
1879     : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
1880   }
1881 public:
1882   /// @brief Constructor with insert-before-instruction semantics
1883   SIToFPInst(
1884     Value *S,                     ///< The value to be converted
1885     const Type *Ty,               ///< The type to convert to
1886     const std::string &Name = "", ///< A name for the new instruction
1887     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1888   );
1889
1890   /// @brief Constructor with insert-at-end-of-block semantics
1891   SIToFPInst(
1892     Value *S,                     ///< The value to be converted
1893     const Type *Ty,               ///< The type to convert to
1894     const std::string &Name,      ///< A name for the new instruction
1895     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
1896   );
1897
1898   /// @brief Clone an identical SIToFPInst
1899   virtual CastInst *clone() const;
1900
1901   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1902   static inline bool classof(const SIToFPInst *) { return true; }
1903   static inline bool classof(const Instruction *I) {
1904     return I->getOpcode() == SIToFP;
1905   }
1906   static inline bool classof(const Value *V) {
1907     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1908   }
1909 };
1910
1911 //===----------------------------------------------------------------------===//
1912 //                                 FPToUIInst Class
1913 //===----------------------------------------------------------------------===//
1914
1915 /// @brief This class represents a cast from floating point to unsigned integer
1916 class FPToUIInst  : public CastInst {
1917   FPToUIInst(const FPToUIInst &CI)
1918     : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
1919   }
1920 public:
1921   /// @brief Constructor with insert-before-instruction semantics
1922   FPToUIInst(
1923     Value *S,                     ///< The value to be converted
1924     const Type *Ty,               ///< The type to convert to
1925     const std::string &Name = "", ///< A name for the new instruction
1926     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1927   );
1928
1929   /// @brief Constructor with insert-at-end-of-block semantics
1930   FPToUIInst(
1931     Value *S,                     ///< The value to be converted
1932     const Type *Ty,               ///< The type to convert to
1933     const std::string &Name,      ///< A name for the new instruction
1934     BasicBlock *InsertAtEnd       ///< Where to insert the new instruction
1935   );
1936
1937   /// @brief Clone an identical FPToUIInst
1938   virtual CastInst *clone() const;
1939
1940   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1941   static inline bool classof(const FPToUIInst *) { return true; }
1942   static inline bool classof(const Instruction *I) {
1943     return I->getOpcode() == FPToUI;
1944   }
1945   static inline bool classof(const Value *V) {
1946     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1947   }
1948 };
1949
1950 //===----------------------------------------------------------------------===//
1951 //                                 FPToSIInst Class
1952 //===----------------------------------------------------------------------===//
1953
1954 /// @brief This class represents a cast from floating point to signed integer.
1955 class FPToSIInst  : public CastInst {
1956   FPToSIInst(const FPToSIInst &CI)
1957     : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
1958   }
1959 public:
1960   /// @brief Constructor with insert-before-instruction semantics
1961   FPToSIInst(
1962     Value *S,                     ///< The value to be converted
1963     const Type *Ty,               ///< The type to convert to
1964     const std::string &Name = "", ///< A name for the new instruction
1965     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
1966   );
1967
1968   /// @brief Constructor with insert-at-end-of-block semantics
1969   FPToSIInst(
1970     Value *S,                     ///< The value to be converted
1971     const Type *Ty,               ///< The type to convert to
1972     const std::string &Name,      ///< A name for the new instruction
1973     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
1974   );
1975
1976   /// @brief Clone an identical FPToSIInst
1977   virtual CastInst *clone() const;
1978
1979   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
1980   static inline bool classof(const FPToSIInst *) { return true; }
1981   static inline bool classof(const Instruction *I) {
1982     return I->getOpcode() == FPToSI;
1983   }
1984   static inline bool classof(const Value *V) {
1985     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1986   }
1987 };
1988
1989 //===----------------------------------------------------------------------===//
1990 //                                 IntToPtrInst Class
1991 //===----------------------------------------------------------------------===//
1992
1993 /// @brief This class represents a cast from an integer to a pointer.
1994 class IntToPtrInst : public CastInst {
1995   IntToPtrInst(const IntToPtrInst &CI)
1996     : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
1997   }
1998 public:
1999   /// @brief Constructor with insert-before-instruction semantics
2000   IntToPtrInst(
2001     Value *S,                     ///< The value to be converted
2002     const Type *Ty,               ///< The type to convert to
2003     const std::string &Name = "", ///< A name for the new instruction
2004     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2005   );
2006
2007   /// @brief Constructor with insert-at-end-of-block semantics
2008   IntToPtrInst(
2009     Value *S,                     ///< The value to be converted
2010     const Type *Ty,               ///< The type to convert to
2011     const std::string &Name,      ///< A name for the new instruction
2012     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2013   );
2014
2015   /// @brief Clone an identical IntToPtrInst
2016   virtual CastInst *clone() const;
2017
2018   // Methods for support type inquiry through isa, cast, and dyn_cast:
2019   static inline bool classof(const IntToPtrInst *) { return true; }
2020   static inline bool classof(const Instruction *I) {
2021     return I->getOpcode() == IntToPtr;
2022   }
2023   static inline bool classof(const Value *V) {
2024     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2025   }
2026 };
2027
2028 //===----------------------------------------------------------------------===//
2029 //                                 PtrToIntInst Class
2030 //===----------------------------------------------------------------------===//
2031
2032 /// @brief This class represents a cast from a pointer to an integer
2033 class PtrToIntInst : public CastInst {
2034   PtrToIntInst(const PtrToIntInst &CI)
2035     : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2036   }
2037 public:
2038   /// @brief Constructor with insert-before-instruction semantics
2039   PtrToIntInst(
2040     Value *S,                     ///< The value to be converted
2041     const Type *Ty,               ///< The type to convert to
2042     const std::string &Name = "", ///< A name for the new instruction
2043     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2044   );
2045
2046   /// @brief Constructor with insert-at-end-of-block semantics
2047   PtrToIntInst(
2048     Value *S,                     ///< The value to be converted
2049     const Type *Ty,               ///< The type to convert to
2050     const std::string &Name,      ///< A name for the new instruction
2051     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2052   );
2053
2054   /// @brief Clone an identical PtrToIntInst
2055   virtual CastInst *clone() const;
2056
2057   // Methods for support type inquiry through isa, cast, and dyn_cast:
2058   static inline bool classof(const PtrToIntInst *) { return true; }
2059   static inline bool classof(const Instruction *I) {
2060     return I->getOpcode() == PtrToInt;
2061   }
2062   static inline bool classof(const Value *V) {
2063     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2064   }
2065 };
2066
2067 //===----------------------------------------------------------------------===//
2068 //                             BitCastInst Class
2069 //===----------------------------------------------------------------------===//
2070
2071 /// @brief This class represents a no-op cast from one type to another.
2072 class BitCastInst : public CastInst {
2073   BitCastInst(const BitCastInst &CI)
2074     : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
2075   }
2076 public:
2077   /// @brief Constructor with insert-before-instruction semantics
2078   BitCastInst(
2079     Value *S,                     ///< The value to be casted
2080     const Type *Ty,               ///< The type to casted to
2081     const std::string &Name = "", ///< A name for the new instruction
2082     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2083   );
2084
2085   /// @brief Constructor with insert-at-end-of-block semantics
2086   BitCastInst(
2087     Value *S,                     ///< The value to be casted
2088     const Type *Ty,               ///< The type to casted to
2089     const std::string &Name,      ///< A name for the new instruction
2090     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
2091   );
2092
2093   /// @brief Clone an identical BitCastInst
2094   virtual CastInst *clone() const;
2095
2096   // Methods for support type inquiry through isa, cast, and dyn_cast:
2097   static inline bool classof(const BitCastInst *) { return true; }
2098   static inline bool classof(const Instruction *I) {
2099     return I->getOpcode() == BitCast;
2100   }
2101   static inline bool classof(const Value *V) {
2102     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2103   }
2104 };
2105
2106 } // End llvm namespace
2107
2108 #endif