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