remove legacy interfaces
[oota-llvm.git] / include / llvm / InstrTypes.h
1 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines various meta classes of instructions that exist in the VM
11 // representation.  Specific concrete subclasses of these may be found in the
12 // i*.h files...
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_INSTRUCTION_TYPES_H
17 #define LLVM_INSTRUCTION_TYPES_H
18
19 #include "llvm/Instruction.h"
20 #include "llvm/OperandTraits.h"
21 #include "llvm/DerivedTypes.h"
22
23 namespace llvm {
24
25 //===----------------------------------------------------------------------===//
26 //                            TerminatorInst Class
27 //===----------------------------------------------------------------------===//
28
29 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
30 /// block.  Thus, these are all the flow control type of operations.
31 ///
32 class TerminatorInst : public Instruction {
33 protected:
34   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
35                  Use *Ops, unsigned NumOps,
36                  Instruction *InsertBefore = 0)
37     : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
38
39   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
40                  Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
41     : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
42
43   // Out of line virtual method, so the vtable, etc has a home.
44   ~TerminatorInst();
45
46   /// Virtual methods - Terminators should overload these and provide inline
47   /// overrides of non-V methods.
48   virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
49   virtual unsigned getNumSuccessorsV() const = 0;
50   virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
51 public:
52
53   virtual Instruction *clone() const = 0;
54
55   /// getNumSuccessors - Return the number of successors that this terminator
56   /// has.
57   unsigned getNumSuccessors() const {
58     return getNumSuccessorsV();
59   }
60
61   /// getSuccessor - Return the specified successor.
62   ///
63   BasicBlock *getSuccessor(unsigned idx) const {
64     return getSuccessorV(idx);
65   }
66
67   /// setSuccessor - Update the specified successor to point at the provided
68   /// block.
69   void setSuccessor(unsigned idx, BasicBlock *B) {
70     setSuccessorV(idx, B);
71   }
72
73   // Methods for support type inquiry through isa, cast, and dyn_cast:
74   static inline bool classof(const TerminatorInst *) { return true; }
75   static inline bool classof(const Instruction *I) {
76     return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
77   }
78   static inline bool classof(const Value *V) {
79     return isa<Instruction>(V) && classof(cast<Instruction>(V));
80   }
81 };
82
83
84 //===----------------------------------------------------------------------===//
85 //                          UnaryInstruction Class
86 //===----------------------------------------------------------------------===//
87
88 class UnaryInstruction : public Instruction {
89   void *operator new(size_t, unsigned);      // Do not implement
90   UnaryInstruction(const UnaryInstruction&); // Do not implement
91
92 protected:
93   UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB = 0)
94     : Instruction(Ty, iType, &Op<0>(), 1, IB) {
95     Op<0>() = V;
96   }
97   UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
98     : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
99     Op<0>() = V;
100   }
101 public:
102   // allocate space for exactly one operand
103   void *operator new(size_t s) {
104     return User::operator new(s, 1);
105   }
106
107   // Out of line virtual method, so the vtable, etc has a home.
108   ~UnaryInstruction();
109
110   /// Transparently provide more efficient getOperand methods.
111   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
112   
113   // Methods for support type inquiry through isa, cast, and dyn_cast:
114   static inline bool classof(const UnaryInstruction *) { return true; }
115   static inline bool classof(const Instruction *I) {
116     return I->getOpcode() == Instruction::Malloc ||
117            I->getOpcode() == Instruction::Alloca ||
118            I->getOpcode() == Instruction::Free ||
119            I->getOpcode() == Instruction::Load ||
120            I->getOpcode() == Instruction::VAArg ||
121            I->getOpcode() == Instruction::ExtractValue ||
122            (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
123   }
124   static inline bool classof(const Value *V) {
125     return isa<Instruction>(V) && classof(cast<Instruction>(V));
126   }
127 };
128
129 template <>
130 struct OperandTraits<UnaryInstruction> : FixedNumOperandTraits<1> {
131 };
132
133 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
134
135 //===----------------------------------------------------------------------===//
136 //                           BinaryOperator Class
137 //===----------------------------------------------------------------------===//
138
139 class BinaryOperator : public Instruction {
140   void *operator new(size_t, unsigned); // Do not implement
141 protected:
142   void init(BinaryOps iType);
143   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
144                  const std::string &Name, Instruction *InsertBefore);
145   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
146                  const std::string &Name, BasicBlock *InsertAtEnd);
147 public:
148   // allocate space for exactly two operands
149   void *operator new(size_t s) {
150     return User::operator new(s, 2);
151   }
152
153   /// Transparently provide more efficient getOperand methods.
154   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
155
156   /// Create() - Construct a binary instruction, given the opcode and the two
157   /// operands.  Optionally (if InstBefore is specified) insert the instruction
158   /// into a BasicBlock right before the specified instruction.  The specified
159   /// Instruction is allowed to be a dereferenced end iterator.
160   ///
161   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
162                                 const std::string &Name = "",
163                                 Instruction *InsertBefore = 0);
164
165   /// Create() - Construct a binary instruction, given the opcode and the two
166   /// operands.  Also automatically insert this instruction to the end of the
167   /// BasicBlock specified.
168   ///
169   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
170                                 const std::string &Name,
171                                 BasicBlock *InsertAtEnd);
172
173   /// Create* - These methods just forward to Create, and are useful when you
174   /// statically know what type of instruction you're going to create.  These
175   /// helpers just save some typing.
176 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
177   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
178                                      const std::string &Name = "") {\
179     return Create(Instruction::OPC, V1, V2, Name);\
180   }
181 #include "llvm/Instruction.def"
182 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
183   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
184                                      const std::string &Name, BasicBlock *BB) {\
185     return Create(Instruction::OPC, V1, V2, Name, BB);\
186   }
187 #include "llvm/Instruction.def"
188 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
189   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
190                                      const std::string &Name, Instruction *I) {\
191     return Create(Instruction::OPC, V1, V2, Name, I);\
192   }
193 #include "llvm/Instruction.def"
194
195
196   /// Helper functions to construct and inspect unary operations (NEG and NOT)
197   /// via binary operators SUB and XOR:
198   ///
199   /// CreateNeg, CreateNot - Create the NEG and NOT
200   ///     instructions out of SUB and XOR instructions.
201   ///
202   static BinaryOperator *CreateNeg(Value *Op, const std::string &Name = "",
203                                    Instruction *InsertBefore = 0);
204   static BinaryOperator *CreateNeg(Value *Op, const std::string &Name,
205                                    BasicBlock *InsertAtEnd);
206   static BinaryOperator *CreateNot(Value *Op, const std::string &Name = "",
207                                    Instruction *InsertBefore = 0);
208   static BinaryOperator *CreateNot(Value *Op, const std::string &Name,
209                                    BasicBlock *InsertAtEnd);
210
211   /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
212   ///
213   static bool isNeg(const Value *V);
214   static bool isNot(const Value *V);
215
216   /// getNegArgument, getNotArgument - Helper functions to extract the
217   ///     unary argument of a NEG or NOT operation implemented via Sub or Xor.
218   ///
219   static const Value *getNegArgument(const Value *BinOp);
220   static       Value *getNegArgument(      Value *BinOp);
221   static const Value *getNotArgument(const Value *BinOp);
222   static       Value *getNotArgument(      Value *BinOp);
223
224   BinaryOps getOpcode() const {
225     return static_cast<BinaryOps>(Instruction::getOpcode());
226   }
227
228   virtual BinaryOperator *clone() const;
229
230   /// swapOperands - Exchange the two operands to this instruction.
231   /// This instruction is safe to use on any binary instruction and
232   /// does not modify the semantics of the instruction.  If the instruction
233   /// cannot be reversed (ie, it's a Div), then return true.
234   ///
235   bool swapOperands();
236
237   // Methods for support type inquiry through isa, cast, and dyn_cast:
238   static inline bool classof(const BinaryOperator *) { return true; }
239   static inline bool classof(const Instruction *I) {
240     return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
241   }
242   static inline bool classof(const Value *V) {
243     return isa<Instruction>(V) && classof(cast<Instruction>(V));
244   }
245 };
246
247 template <>
248 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
249 };
250
251 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
252
253 //===----------------------------------------------------------------------===//
254 //                               CastInst Class
255 //===----------------------------------------------------------------------===//
256
257 /// CastInst - This is the base class for all instructions that perform data
258 /// casts. It is simply provided so that instruction category testing
259 /// can be performed with code like:
260 ///
261 /// if (isa<CastInst>(Instr)) { ... }
262 /// @brief Base class of casting instructions.
263 class CastInst : public UnaryInstruction {
264   /// @brief Copy constructor
265   CastInst(const CastInst &CI)
266     : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
267   }
268   /// @brief Do not allow default construction
269   CastInst(); 
270 protected:
271   /// @brief Constructor with insert-before-instruction semantics for subclasses
272   CastInst(const Type *Ty, unsigned iType, Value *S, 
273            const std::string &NameStr = "", Instruction *InsertBefore = 0)
274     : UnaryInstruction(Ty, iType, S, InsertBefore) {
275     setName(NameStr);
276   }
277   /// @brief Constructor with insert-at-end-of-block semantics for subclasses
278   CastInst(const Type *Ty, unsigned iType, Value *S, 
279            const std::string &NameStr, BasicBlock *InsertAtEnd)
280     : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
281     setName(NameStr);
282   }
283 public:
284   /// Provides a way to construct any of the CastInst subclasses using an 
285   /// opcode instead of the subclass's constructor. The opcode must be in the
286   /// CastOps category (Instruction::isCast(opcode) returns true). This
287   /// constructor has insert-before-instruction semantics to automatically
288   /// insert the new CastInst before InsertBefore (if it is non-null).
289   /// @brief Construct any of the CastInst subclasses
290   static CastInst *Create(
291     Instruction::CastOps,    ///< The opcode of the cast instruction
292     Value *S,                ///< The value to be casted (operand 0)
293     const Type *Ty,          ///< The type to which cast should be made
294     const std::string &Name = "", ///< Name for the instruction
295     Instruction *InsertBefore = 0 ///< Place to insert the instruction
296   );
297   /// Provides a way to construct any of the CastInst subclasses using an
298   /// opcode instead of the subclass's constructor. The opcode must be in the
299   /// CastOps category. This constructor has insert-at-end-of-block semantics
300   /// to automatically insert the new CastInst at the end of InsertAtEnd (if
301   /// its non-null).
302   /// @brief Construct any of the CastInst subclasses
303   static CastInst *Create(
304     Instruction::CastOps,    ///< The opcode for the cast instruction
305     Value *S,                ///< The value to be casted (operand 0)
306     const Type *Ty,          ///< The type to which operand is casted
307     const std::string &Name, ///< The name for the instruction
308     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
309   );
310
311   /// @brief Create a ZExt or BitCast cast instruction
312   static CastInst *CreateZExtOrBitCast(
313     Value *S,                ///< The value to be casted (operand 0)
314     const Type *Ty,          ///< The type to which cast should be made
315     const std::string &Name = "", ///< Name for the instruction
316     Instruction *InsertBefore = 0 ///< Place to insert the instruction
317   );
318
319   /// @brief Create a ZExt or BitCast cast instruction
320   static CastInst *CreateZExtOrBitCast(
321     Value *S,                ///< The value to be casted (operand 0)
322     const Type *Ty,          ///< The type to which operand is casted
323     const std::string &Name, ///< The name for the instruction
324     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
325   );
326
327   /// @brief Create a SExt or BitCast cast instruction
328   static CastInst *CreateSExtOrBitCast(
329     Value *S,                ///< The value to be casted (operand 0)
330     const Type *Ty,          ///< The type to which cast should be made
331     const std::string &Name = "", ///< Name for the instruction
332     Instruction *InsertBefore = 0 ///< Place to insert the instruction
333   );
334
335   /// @brief Create a SExt or BitCast cast instruction
336   static CastInst *CreateSExtOrBitCast(
337     Value *S,                ///< The value to be casted (operand 0)
338     const Type *Ty,          ///< The type to which operand is casted
339     const std::string &Name, ///< The name for the instruction
340     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
341   );
342
343   /// @brief Create a BitCast or a PtrToInt cast instruction
344   static CastInst *CreatePointerCast(
345     Value *S,                ///< The pointer value to be casted (operand 0)
346     const Type *Ty,          ///< The type to which operand is casted
347     const std::string &Name, ///< The name for the instruction
348     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
349   );
350
351   /// @brief Create a BitCast or a PtrToInt cast instruction
352   static CastInst *CreatePointerCast(
353     Value *S,                ///< The pointer value to be casted (operand 0)
354     const Type *Ty,          ///< The type to which cast should be made
355     const std::string &Name = "", ///< Name for the instruction
356     Instruction *InsertBefore = 0 ///< Place to insert the instruction
357   );
358
359   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
360   static CastInst *CreateIntegerCast(
361     Value *S,                ///< The pointer value to be casted (operand 0)
362     const Type *Ty,          ///< The type to which cast should be made
363     bool isSigned,           ///< Whether to regard S as signed or not
364     const std::string &Name = "", ///< Name for the instruction
365     Instruction *InsertBefore = 0 ///< Place to insert the instruction
366   );
367
368   /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
369   static CastInst *CreateIntegerCast(
370     Value *S,                ///< The integer value to be casted (operand 0)
371     const Type *Ty,          ///< The integer type to which operand is casted
372     bool isSigned,           ///< Whether to regard S as signed or not
373     const std::string &Name, ///< The name for the instruction
374     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
375   );
376
377   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
378   static CastInst *CreateFPCast(
379     Value *S,                ///< The floating point value to be casted 
380     const Type *Ty,          ///< The floating point type to cast to
381     const std::string &Name = "", ///< Name for the instruction
382     Instruction *InsertBefore = 0 ///< Place to insert the instruction
383   );
384
385   /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
386   static CastInst *CreateFPCast(
387     Value *S,                ///< The floating point value to be casted 
388     const Type *Ty,          ///< The floating point type to cast to
389     const std::string &Name, ///< The name for the instruction
390     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
391   );
392
393   /// @brief Create a Trunc or BitCast cast instruction
394   static CastInst *CreateTruncOrBitCast(
395     Value *S,                ///< The value to be casted (operand 0)
396     const Type *Ty,          ///< The type to which cast should be made
397     const std::string &Name = "", ///< Name for the instruction
398     Instruction *InsertBefore = 0 ///< Place to insert the instruction
399   );
400
401   /// @brief Create a Trunc or BitCast cast instruction
402   static CastInst *CreateTruncOrBitCast(
403     Value *S,                ///< The value to be casted (operand 0)
404     const Type *Ty,          ///< The type to which operand is casted
405     const std::string &Name, ///< The name for the instruction
406     BasicBlock *InsertAtEnd  ///< The block to insert the instruction into
407   );
408
409   /// @brief Check whether it is valid to call getCastOpcode for these types.
410   static bool isCastable(
411     const Type *SrcTy, ///< The Type from which the value should be cast.
412     const Type *DestTy ///< The Type to which the value should be cast.
413   );
414
415   /// Returns the opcode necessary to cast Val into Ty using usual casting
416   /// rules.
417   /// @brief Infer the opcode for cast operand and type
418   static Instruction::CastOps getCastOpcode(
419     const Value *Val, ///< The value to cast
420     bool SrcIsSigned, ///< Whether to treat the source as signed
421     const Type *Ty,   ///< The Type to which the value should be casted
422     bool DstIsSigned  ///< Whether to treate the dest. as signed
423   );
424
425   /// There are several places where we need to know if a cast instruction 
426   /// only deals with integer source and destination types. To simplify that
427   /// logic, this method is provided.
428   /// @returns true iff the cast has only integral typed operand and dest type.
429   /// @brief Determine if this is an integer-only cast.
430   bool isIntegerCast() const;
431
432   /// A lossless cast is one that does not alter the basic value. It implies
433   /// a no-op cast but is more stringent, preventing things like int->float,
434   /// long->double, int->ptr, or vector->anything. 
435   /// @returns true iff the cast is lossless.
436   /// @brief Determine if this is a lossless cast.
437   bool isLosslessCast() const;
438
439   /// A no-op cast is one that can be effected without changing any bits. 
440   /// It implies that the source and destination types are the same size. The
441   /// IntPtrTy argument is used to make accurate determinations for casts 
442   /// involving Integer and Pointer types. They are no-op casts if the integer
443   /// is the same size as the pointer. However, pointer size varies with 
444   /// platform. Generally, the result of TargetData::getIntPtrType() should be
445   /// passed in. If that's not available, use Type::Int64Ty, which will make
446   /// the isNoopCast call conservative.
447   /// @brief Determine if this cast is a no-op cast. 
448   bool isNoopCast(
449     const Type *IntPtrTy ///< Integer type corresponding to pointer
450   ) const;
451
452   /// Determine how a pair of casts can be eliminated, if they can be at all.
453   /// This is a helper function for both CastInst and ConstantExpr.
454   /// @returns 0 if the CastInst pair can't be eliminated
455   /// @returns Instruction::CastOps value for a cast that can replace 
456   /// the pair, casting SrcTy to DstTy.
457   /// @brief Determine if a cast pair is eliminable
458   static unsigned isEliminableCastPair(
459     Instruction::CastOps firstOpcode,  ///< Opcode of first cast
460     Instruction::CastOps secondOpcode, ///< Opcode of second cast
461     const Type *SrcTy, ///< SrcTy of 1st cast
462     const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
463     const Type *DstTy, ///< DstTy of 2nd cast
464     const Type *IntPtrTy ///< Integer type corresponding to Ptr types
465   );
466
467   /// @brief Return the opcode of this CastInst
468   Instruction::CastOps getOpcode() const { 
469     return Instruction::CastOps(Instruction::getOpcode()); 
470   }
471
472   /// @brief Return the source type, as a convenience
473   const Type* getSrcTy() const { return getOperand(0)->getType(); }
474   /// @brief Return the destination type, as a convenience
475   const Type* getDestTy() const { return getType(); }
476
477   /// This method can be used to determine if a cast from S to DstTy using
478   /// Opcode op is valid or not. 
479   /// @returns true iff the proposed cast is valid.
480   /// @brief Determine if a cast is valid without creating one.
481   static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
482
483   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
484   static inline bool classof(const CastInst *) { return true; }
485   static inline bool classof(const Instruction *I) {
486     return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
487   }
488   static inline bool classof(const Value *V) {
489     return isa<Instruction>(V) && classof(cast<Instruction>(V));
490   }
491 };
492
493 //===----------------------------------------------------------------------===//
494 //                               CmpInst Class
495 //===----------------------------------------------------------------------===//
496
497 /// This class is the base class for the comparison instructions. 
498 /// @brief Abstract base class of comparison instructions.
499 // FIXME: why not derive from BinaryOperator?
500 class CmpInst: public Instruction {
501   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
502   CmpInst(); // do not implement
503 protected:
504   CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
505           Value *LHS, Value *RHS, const std::string &Name = "",
506           Instruction *InsertBefore = 0);
507   
508   CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
509           Value *LHS, Value *RHS, const std::string &Name,
510           BasicBlock *InsertAtEnd);
511
512 public:
513   /// This enumeration lists the possible predicates for CmpInst subclasses.
514   /// Values in the range 0-31 are reserved for FCmpInst, while values in the
515   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
516   /// predicate values are not overlapping between the classes.
517   enum Predicate {
518     // Opcode             U L G E    Intuitive operation
519     FCMP_FALSE =  0,  /// 0 0 0 0    Always false (always folded)
520     FCMP_OEQ   =  1,  /// 0 0 0 1    True if ordered and equal
521     FCMP_OGT   =  2,  /// 0 0 1 0    True if ordered and greater than
522     FCMP_OGE   =  3,  /// 0 0 1 1    True if ordered and greater than or equal
523     FCMP_OLT   =  4,  /// 0 1 0 0    True if ordered and less than
524     FCMP_OLE   =  5,  /// 0 1 0 1    True if ordered and less than or equal
525     FCMP_ONE   =  6,  /// 0 1 1 0    True if ordered and operands are unequal
526     FCMP_ORD   =  7,  /// 0 1 1 1    True if ordered (no nans)
527     FCMP_UNO   =  8,  /// 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
528     FCMP_UEQ   =  9,  /// 1 0 0 1    True if unordered or equal
529     FCMP_UGT   = 10,  /// 1 0 1 0    True if unordered or greater than
530     FCMP_UGE   = 11,  /// 1 0 1 1    True if unordered, greater than, or equal
531     FCMP_ULT   = 12,  /// 1 1 0 0    True if unordered or less than
532     FCMP_ULE   = 13,  /// 1 1 0 1    True if unordered, less than, or equal
533     FCMP_UNE   = 14,  /// 1 1 1 0    True if unordered or not equal
534     FCMP_TRUE  = 15,  /// 1 1 1 1    Always true (always folded)
535     FIRST_FCMP_PREDICATE = FCMP_FALSE,
536     LAST_FCMP_PREDICATE = FCMP_TRUE,
537     BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
538     ICMP_EQ    = 32,  /// equal
539     ICMP_NE    = 33,  /// not equal
540     ICMP_UGT   = 34,  /// unsigned greater than
541     ICMP_UGE   = 35,  /// unsigned greater or equal
542     ICMP_ULT   = 36,  /// unsigned less than
543     ICMP_ULE   = 37,  /// unsigned less or equal
544     ICMP_SGT   = 38,  /// signed greater than
545     ICMP_SGE   = 39,  /// signed greater or equal
546     ICMP_SLT   = 40,  /// signed less than
547     ICMP_SLE   = 41,  /// signed less or equal
548     FIRST_ICMP_PREDICATE = ICMP_EQ,
549     LAST_ICMP_PREDICATE = ICMP_SLE,
550     BAD_ICMP_PREDICATE = ICMP_SLE + 1
551   };
552
553   // allocate space for exactly two operands
554   void *operator new(size_t s) {
555     return User::operator new(s, 2);
556   }
557   /// Construct a compare instruction, given the opcode, the predicate and 
558   /// the two operands.  Optionally (if InstBefore is specified) insert the 
559   /// instruction into a BasicBlock right before the specified instruction.  
560   /// The specified Instruction is allowed to be a dereferenced end iterator.
561   /// @brief Create a CmpInst
562   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1, 
563                          Value *S2, const std::string &Name = "",
564                          Instruction *InsertBefore = 0);
565
566   /// Construct a compare instruction, given the opcode, the predicate and the 
567   /// two operands.  Also automatically insert this instruction to the end of 
568   /// the BasicBlock specified.
569   /// @brief Create a CmpInst
570   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1, 
571                          Value *S2, const std::string &Name, 
572                          BasicBlock *InsertAtEnd);
573
574   /// @brief Get the opcode casted to the right type
575   OtherOps getOpcode() const {
576     return static_cast<OtherOps>(Instruction::getOpcode());
577   }
578
579   /// @brief Return the predicate for this instruction.
580   Predicate getPredicate() const { return Predicate(SubclassData); }
581
582   /// @brief Set the predicate for this instruction to the specified value.
583   void setPredicate(Predicate P) { SubclassData = P; }
584   
585   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
586   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
587   /// @returns the inverse predicate for the instruction's current predicate. 
588   /// @brief Return the inverse of the instruction's predicate.
589   Predicate getInversePredicate() const {
590     return getInversePredicate(getPredicate());
591   }
592
593   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
594   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
595   /// @returns the inverse predicate for predicate provided in \p pred. 
596   /// @brief Return the inverse of a given predicate
597   static Predicate getInversePredicate(Predicate pred);
598
599   /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
600   ///              OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
601   /// @returns the predicate that would be the result of exchanging the two 
602   /// operands of the CmpInst instruction without changing the result 
603   /// produced.  
604   /// @brief Return the predicate as if the operands were swapped
605   Predicate getSwappedPredicate() const {
606     return getSwappedPredicate(getPredicate());
607   }
608
609   /// This is a static version that you can use without an instruction 
610   /// available.
611   /// @brief Return the predicate as if the operands were swapped.
612   static Predicate getSwappedPredicate(Predicate pred);
613
614   /// @brief Provide more efficient getOperand methods.
615   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
616
617   /// This is just a convenience that dispatches to the subclasses.
618   /// @brief Swap the operands and adjust predicate accordingly to retain
619   /// the same comparison.
620   void swapOperands();
621
622   /// This is just a convenience that dispatches to the subclasses.
623   /// @brief Determine if this CmpInst is commutative.
624   bool isCommutative();
625
626   /// This is just a convenience that dispatches to the subclasses.
627   /// @brief Determine if this is an equals/not equals predicate.
628   bool isEquality();
629
630   /// @returns true if the predicate is unsigned, false otherwise.
631   /// @brief Determine if the predicate is an unsigned operation.
632   static bool isUnsigned(unsigned short predicate);
633
634   /// @returns true if the predicate is signed, false otherwise.
635   /// @brief Determine if the predicate is an signed operation.
636   static bool isSigned(unsigned short predicate);
637
638   /// @brief Determine if the predicate is an ordered operation.
639   static bool isOrdered(unsigned short predicate);
640
641   /// @brief Determine if the predicate is an unordered operation.
642   static bool isUnordered(unsigned short predicate);
643
644   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
645   static inline bool classof(const CmpInst *) { return true; }
646   static inline bool classof(const Instruction *I) {
647     return I->getOpcode() == Instruction::ICmp || 
648            I->getOpcode() == Instruction::FCmp ||
649            I->getOpcode() == Instruction::VICmp ||
650            I->getOpcode() == Instruction::VFCmp;
651   }
652   static inline bool classof(const Value *V) {
653     return isa<Instruction>(V) && classof(cast<Instruction>(V));
654   }
655   /// @brief Create a result type for fcmp/icmp (but not vicmp/vfcmp)
656   static const Type* makeCmpResultType(const Type* opnd_type) {
657     if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
658       return VectorType::get(Type::Int1Ty, vt->getNumElements());
659     }
660     return Type::Int1Ty;
661   }
662 };
663
664
665 // FIXME: these are redundant if CmpInst < BinaryOperator
666 template <>
667 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
668 };
669
670 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
671
672 } // End llvm namespace
673
674 #endif