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