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