Extend the vcmp/fcmp LLVM IR instructions to take vectors as arguments
[oota-llvm.git] / include / llvm / InstrTypes.h
1 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines various meta classes of instructions that exist in the VM
11 // representation.  Specific concrete subclasses of these may be found in the
12 // i*.h files...
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_INSTRUCTION_TYPES_H
17 #define LLVM_INSTRUCTION_TYPES_H
18
19 #include "llvm/Instruction.h"
20 #include "llvm/OperandTraits.h"
21 #include "llvm/DerivedTypes.h"
22
23 namespace llvm {
24
25 //===----------------------------------------------------------------------===//
26 //                            TerminatorInst Class
27 //===----------------------------------------------------------------------===//
28
29 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
30 /// block.  Thus, these are all the flow control type of operations.
31 ///
32 class TerminatorInst : public Instruction {
33 protected:
34   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
35                  Use *Ops, unsigned NumOps,
36                  Instruction *InsertBefore = 0)
37     : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
38
39   TerminatorInst(const Type *Ty, Instruction::TermOps iType,
40                  Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
41     : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
42
43   // Out of line virtual method, so the vtable, etc has a home.
44   ~TerminatorInst();
45
46   /// Virtual methods - Terminators should overload these and provide inline
47   /// overrides of non-V methods.
48   virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
49   virtual unsigned getNumSuccessorsV() const = 0;
50   virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
51 public:
52
53   virtual Instruction *clone() const = 0;
54
55   /// getNumSuccessors - Return the number of successors that this terminator
56   /// has.
57   unsigned getNumSuccessors() const {
58     return getNumSuccessorsV();
59   }
60
61   /// getSuccessor - Return the specified successor.
62   ///
63   BasicBlock *getSuccessor(unsigned idx) const {
64     return getSuccessorV(idx);
65   }
66
67   /// setSuccessor - Update the specified successor to point at the provided
68   /// block.
69   void setSuccessor(unsigned idx, BasicBlock *B) {
70     setSuccessorV(idx, B);
71   }
72
73   // Methods for support type inquiry through isa, cast, and dyn_cast:
74   static inline bool classof(const TerminatorInst *) { return true; }
75   static inline bool classof(const Instruction *I) {
76     return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
77   }
78   static inline bool classof(const Value *V) {
79     return isa<Instruction>(V) && classof(cast<Instruction>(V));
80   }
81 };
82
83
84 //===----------------------------------------------------------------------===//
85 //                          UnaryInstruction Class
86 //===----------------------------------------------------------------------===//
87
88 class UnaryInstruction : public Instruction {
89   void *operator new(size_t, unsigned);      // Do not implement
90   UnaryInstruction(const UnaryInstruction&); // Do not implement
91
92 protected:
93   UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB = 0)
94     : Instruction(Ty, iType, &Op<0>(), 1, IB) {
95     Op<0>() = V;
96   }
97   UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
98     : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
99     Op<0>() = V;
100   }
101 public:
102   // allocate space for exactly one operand
103   void *operator new(size_t s) {
104     return User::operator new(s, 1);
105   }
106
107   // Out of line virtual method, so the vtable, etc has a home.
108   ~UnaryInstruction();
109
110   /// Transparently provide more efficient getOperand methods.
111   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
112   
113   // Methods for support type inquiry through isa, cast, and dyn_cast:
114   static inline bool classof(const UnaryInstruction *) { return true; }
115   static inline bool classof(const Instruction *I) {
116     return I->getOpcode() == Instruction::Malloc ||
117            I->getOpcode() == Instruction::Alloca ||
118            I->getOpcode() == Instruction::Free ||
119            I->getOpcode() == Instruction::Load ||
120            I->getOpcode() == Instruction::VAArg ||
121            I->getOpcode() == Instruction::ExtractValue ||
122            (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
123   }
124   static inline bool classof(const Value *V) {
125     return isa<Instruction>(V) && classof(cast<Instruction>(V));
126   }
127 };
128
129 template <>
130 struct OperandTraits<UnaryInstruction> : FixedNumOperandTraits<1> {
131 };
132
133 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
134
135 //===----------------------------------------------------------------------===//
136 //                           BinaryOperator Class
137 //===----------------------------------------------------------------------===//
138
139 class BinaryOperator : public Instruction {
140   void *operator new(size_t, unsigned); // Do not implement
141 protected:
142   void init(BinaryOps iType);
143   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
144                  const std::string &Name, Instruction *InsertBefore);
145   BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
146                  const std::string &Name, BasicBlock *InsertAtEnd);
147 public:
148   // allocate space for exactly two operands
149   void *operator new(size_t s) {
150     return User::operator new(s, 2);
151   }
152
153   /// Transparently provide more efficient getOperand methods.
154   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
155
156   /// Create() - Construct a binary instruction, given the opcode and the two
157   /// operands.  Optionally (if InstBefore is specified) insert the instruction
158   /// into a BasicBlock right before the specified instruction.  The specified
159   /// Instruction is allowed to be a dereferenced end iterator.
160   ///
161   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
162                                 const std::string &Name = "",
163                                 Instruction *InsertBefore = 0);
164
165   /// Create() - Construct a binary instruction, given the opcode and the two
166   /// operands.  Also automatically insert this instruction to the end of the
167   /// BasicBlock specified.
168   ///
169   static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
170                                 const std::string &Name,
171                                 BasicBlock *InsertAtEnd);
172
173   /// Create* - These methods just forward to Create, and are useful when you
174   /// statically know what type of instruction you're going to create.  These
175   /// helpers just save some typing.
176 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
177   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
178                                      const std::string &Name = "") {\
179     return Create(Instruction::OPC, V1, V2, Name);\
180   }
181 #include "llvm/Instruction.def"
182 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
183   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
184                                      const std::string &Name, BasicBlock *BB) {\
185     return Create(Instruction::OPC, V1, V2, Name, BB);\
186   }
187 #include "llvm/Instruction.def"
188 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
189   static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
190                                      const std::string &Name, Instruction *I) {\
191     return Create(Instruction::OPC, V1, V2, Name, I);\
192   }
193 #include "llvm/Instruction.def"
194
195
196   /// Helper functions to construct and inspect unary operations (NEG and NOT)
197   /// via binary operators SUB and XOR:
198   ///
199   /// CreateNeg, CreateNot - Create the NEG and NOT
200   ///     instructions out of SUB and XOR instructions.
201   ///
202   static BinaryOperator *CreateNeg(Value *Op, const std::string &Name = "",
203                                    Instruction *InsertBefore = 0);
204   static BinaryOperator *CreateNeg(Value *Op, const std::string &Name,
205                                    BasicBlock *InsertAtEnd);
206   static BinaryOperator *CreateNot(Value *Op, const std::string &Name = "",
207                                    Instruction *InsertBefore = 0);
208   static BinaryOperator *CreateNot(Value *Op, const std::string &Name,
209                                    BasicBlock *InsertAtEnd);
210
211   /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
212   ///
213   static bool isNeg(const Value *V);
214   static bool isNot(const Value *V);
215
216   /// getNegArgument, getNotArgument - Helper functions to extract the
217   ///     unary argument of a NEG or NOT operation implemented via Sub or Xor.
218   ///
219   static const Value *getNegArgument(const Value *BinOp);
220   static       Value *getNegArgument(      Value *BinOp);
221   static const Value *getNotArgument(const Value *BinOp);
222   static       Value *getNotArgument(      Value *BinOp);
223
224   BinaryOps getOpcode() const {
225     return static_cast<BinaryOps>(Instruction::getOpcode());
226   }
227
228   virtual BinaryOperator *clone() const;
229
230   /// swapOperands - Exchange the two operands to this instruction.
231   /// This instruction is safe to use on any binary instruction and
232   /// does not modify the semantics of the instruction.  If the instruction
233   /// cannot be reversed (ie, it's a Div), then return true.
234   ///
235   bool swapOperands();
236
237   // Methods for support type inquiry through isa, cast, and dyn_cast:
238   static inline bool classof(const BinaryOperator *) { return true; }
239   static inline bool classof(const Instruction *I) {
240     return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
241   }
242   static inline bool classof(const Value *V) {
243     return isa<Instruction>(V) && classof(cast<Instruction>(V));
244   }
245
246   /// Backward-compatible interfaces
247   /// @deprecated in 2.4, do not use, will disappear soon
248   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
249                                 const std::string &Name = "",
250                                 Instruction *InsertBefore = 0) {
251     return Create(Op, S1, S2, Name, InsertBefore);
252   }
253   static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
254                                 const std::string &Name,
255                                 BasicBlock *InsertAtEnd) {
256     return Create(Op, S1, S2, Name, InsertAtEnd);
257   }
258 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
259   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
260                                      const std::string &Name = "") {\
261     return Create(Instruction::OPC, V1, V2, Name);\
262   }
263 #include "llvm/Instruction.def"
264 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
265   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
266                                      const std::string &Name, BasicBlock *BB) {\
267     return Create(Instruction::OPC, V1, V2, Name, BB);\
268   }
269 #include "llvm/Instruction.def"
270 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
271   static BinaryOperator *create##OPC(Value *V1, Value *V2, \
272                                      const std::string &Name, Instruction *I) {\
273     return Create(Instruction::OPC, V1, V2, Name, I);\
274   }
275 #include "llvm/Instruction.def"
276   static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
277                                    Instruction *InsertBefore = 0) {
278     return CreateNeg(Op, Name, InsertBefore);
279   }
280   static BinaryOperator *createNeg(Value *Op, const std::string &Name,
281                                    BasicBlock *InsertAtEnd) {
282     return CreateNeg(Op, Name, InsertAtEnd);
283   }
284   static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
285                                    Instruction *InsertBefore = 0) {
286     return CreateNot(Op, Name, InsertBefore);
287   }
288   static BinaryOperator *createNot(Value *Op, const std::string &Name,
289                                    BasicBlock *InsertAtEnd) {
290     return CreateNot(Op, Name, InsertAtEnd);
291   }
292 };
293
294 template <>
295 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
296 };
297
298 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
299
300 //===----------------------------------------------------------------------===//
301 //                               CastInst Class
302 //===----------------------------------------------------------------------===//
303
304 /// CastInst - This is the base class for all instructions that perform data
305 /// casts. It is simply provided so that instruction category testing
306 /// can be performed with code like:
307 ///
308 /// if (isa<CastInst>(Instr)) { ... }
309 /// @brief Base class of casting instructions.
310 class CastInst : public UnaryInstruction {
311   /// @brief Copy constructor
312   CastInst(const CastInst &CI)
313     : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
314   }
315   /// @brief Do not allow default construction
316   CastInst(); 
317 protected:
318   /// @brief Constructor with insert-before-instruction semantics for subclasses
319   CastInst(const Type *Ty, unsigned iType, Value *S, 
320            const std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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 std::string &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
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->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
534   }
535   static inline bool classof(const Value *V) {
536     return isa<Instruction>(V) && classof(cast<Instruction>(V));
537   }
538   /// Backward-compatible interfaces
539   /// @deprecated in 2.4, do not use, will disappear soon
540   static CastInst *create(Instruction::CastOps Op,Value *S,const Type *Ty,
541     const std::string &Name = "",Instruction *InsertBefore = 0) {
542     return Create(Op,S,Ty,Name,InsertBefore);
543   }
544   static CastInst *create(Instruction::CastOps Op,Value *S,const Type *Ty,
545     const std::string &Name,BasicBlock *InsertAtEnd) {
546     return Create(Op,S,Ty,Name,InsertAtEnd);
547   }
548
549 #define DEFINE_CASTINST_DEPRECATED(OP)                                  \
550   static CastInst *create ## OP ## Cast(Value *S, const Type *Ty,       \
551     const std::string &Name = "", Instruction *InsertBefore = 0) {      \
552     return Create ## OP ## Cast(S, Ty, Name, InsertBefore);             \
553   }                                                                     \
554   static CastInst *create ## OP ## Cast(Value *S, const Type *Ty,       \
555     const std::string &Name, BasicBlock *InsertAtEnd) {                 \
556     return Create ## OP ## Cast(S, Ty, Name, InsertAtEnd);              \
557   }
558   DEFINE_CASTINST_DEPRECATED(ZExtOrBit)
559   DEFINE_CASTINST_DEPRECATED(SExtOrBit)
560   DEFINE_CASTINST_DEPRECATED(Pointer)
561   DEFINE_CASTINST_DEPRECATED(FP)
562   DEFINE_CASTINST_DEPRECATED(TruncOrBit)
563 #undef DEFINE_CASTINST_DEPRECATED
564   static CastInst *createIntegerCast(Value *S, const Type *Ty, bool isSigned,
565     const std::string &Name = "", Instruction *InsertBefore = 0) {
566     return CreateIntegerCast(S, Ty, isSigned, Name, InsertBefore);
567   }
568   static CastInst *createIntegerCast(Value *S, const Type *Ty, bool isSigned,
569     const std::string &Name, BasicBlock *InsertAtEnd) {
570     return CreateIntegerCast(S, Ty, isSigned, Name, InsertAtEnd);
571   }
572 };
573
574 //===----------------------------------------------------------------------===//
575 //                               CmpInst Class
576 //===----------------------------------------------------------------------===//
577
578 /// This class is the base class for the comparison instructions. 
579 /// @brief Abstract base class of comparison instructions.
580 // FIXME: why not derive from BinaryOperator?
581 class CmpInst: public Instruction {
582   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
583   CmpInst(); // do not implement
584 protected:
585   CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
586           Value *LHS, Value *RHS, const std::string &Name = "",
587           Instruction *InsertBefore = 0);
588   
589   CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
590           Value *LHS, Value *RHS, const std::string &Name,
591           BasicBlock *InsertAtEnd);
592
593 public:
594   /// This enumeration lists the possible predicates for CmpInst subclasses.
595   /// Values in the range 0-31 are reserved for FCmpInst, while values in the
596   /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
597   /// predicate values are not overlapping between the classes.
598   enum Predicate {
599     // Opcode             U L G E    Intuitive operation
600     FCMP_FALSE =  0,  /// 0 0 0 0    Always false (always folded)
601     FCMP_OEQ   =  1,  /// 0 0 0 1    True if ordered and equal
602     FCMP_OGT   =  2,  /// 0 0 1 0    True if ordered and greater than
603     FCMP_OGE   =  3,  /// 0 0 1 1    True if ordered and greater than or equal
604     FCMP_OLT   =  4,  /// 0 1 0 0    True if ordered and less than
605     FCMP_OLE   =  5,  /// 0 1 0 1    True if ordered and less than or equal
606     FCMP_ONE   =  6,  /// 0 1 1 0    True if ordered and operands are unequal
607     FCMP_ORD   =  7,  /// 0 1 1 1    True if ordered (no nans)
608     FCMP_UNO   =  8,  /// 1 0 0 0    True if unordered: isnan(X) | isnan(Y)
609     FCMP_UEQ   =  9,  /// 1 0 0 1    True if unordered or equal
610     FCMP_UGT   = 10,  /// 1 0 1 0    True if unordered or greater than
611     FCMP_UGE   = 11,  /// 1 0 1 1    True if unordered, greater than, or equal
612     FCMP_ULT   = 12,  /// 1 1 0 0    True if unordered or less than
613     FCMP_ULE   = 13,  /// 1 1 0 1    True if unordered, less than, or equal
614     FCMP_UNE   = 14,  /// 1 1 1 0    True if unordered or not equal
615     FCMP_TRUE  = 15,  /// 1 1 1 1    Always true (always folded)
616     FIRST_FCMP_PREDICATE = FCMP_FALSE,
617     LAST_FCMP_PREDICATE = FCMP_TRUE,
618     BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
619     ICMP_EQ    = 32,  /// equal
620     ICMP_NE    = 33,  /// not equal
621     ICMP_UGT   = 34,  /// unsigned greater than
622     ICMP_UGE   = 35,  /// unsigned greater or equal
623     ICMP_ULT   = 36,  /// unsigned less than
624     ICMP_ULE   = 37,  /// unsigned less or equal
625     ICMP_SGT   = 38,  /// signed greater than
626     ICMP_SGE   = 39,  /// signed greater or equal
627     ICMP_SLT   = 40,  /// signed less than
628     ICMP_SLE   = 41,  /// signed less or equal
629     FIRST_ICMP_PREDICATE = ICMP_EQ,
630     LAST_ICMP_PREDICATE = ICMP_SLE,
631     BAD_ICMP_PREDICATE = ICMP_SLE + 1
632   };
633
634   // allocate space for exactly two operands
635   void *operator new(size_t s) {
636     return User::operator new(s, 2);
637   }
638   /// Construct a compare instruction, given the opcode, the predicate and 
639   /// the two operands.  Optionally (if InstBefore is specified) insert the 
640   /// instruction into a BasicBlock right before the specified instruction.  
641   /// The specified Instruction is allowed to be a dereferenced end iterator.
642   /// @brief Create a CmpInst
643   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1, 
644                          Value *S2, const std::string &Name = "",
645                          Instruction *InsertBefore = 0);
646
647   /// Construct a compare instruction, given the opcode, the predicate and the 
648   /// two operands.  Also automatically insert this instruction to the end of 
649   /// the BasicBlock specified.
650   /// @brief Create a CmpInst
651   static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1, 
652                          Value *S2, const std::string &Name, 
653                          BasicBlock *InsertAtEnd);
654
655   /// @brief Get the opcode casted to the right type
656   OtherOps getOpcode() const {
657     return static_cast<OtherOps>(Instruction::getOpcode());
658   }
659
660   /// @brief Return the predicate for this instruction.
661   Predicate getPredicate() const { return Predicate(SubclassData); }
662
663   /// @brief Set the predicate for this instruction to the specified value.
664   void setPredicate(Predicate P) { SubclassData = P; }
665   
666   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
667   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
668   /// @returns the inverse predicate for the instruction's current predicate. 
669   /// @brief Return the inverse of the instruction's predicate.
670   Predicate getInversePredicate() const {
671     return getInversePredicate(getPredicate());
672   }
673
674   /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
675   ///              OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
676   /// @returns the inverse predicate for predicate provided in \p pred. 
677   /// @brief Return the inverse of a given predicate
678   static Predicate getInversePredicate(Predicate pred);
679
680   /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
681   ///              OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
682   /// @returns the predicate that would be the result of exchanging the two 
683   /// operands of the CmpInst instruction without changing the result 
684   /// produced.  
685   /// @brief Return the predicate as if the operands were swapped
686   Predicate getSwappedPredicate() const {
687     return getSwappedPredicate(getPredicate());
688   }
689
690   /// This is a static version that you can use without an instruction 
691   /// available.
692   /// @brief Return the predicate as if the operands were swapped.
693   static Predicate getSwappedPredicate(Predicate pred);
694
695   /// @brief Provide more efficient getOperand methods.
696   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
697
698   /// This is just a convenience that dispatches to the subclasses.
699   /// @brief Swap the operands and adjust predicate accordingly to retain
700   /// the same comparison.
701   void swapOperands();
702
703   /// This is just a convenience that dispatches to the subclasses.
704   /// @brief Determine if this CmpInst is commutative.
705   bool isCommutative();
706
707   /// This is just a convenience that dispatches to the subclasses.
708   /// @brief Determine if this is an equals/not equals predicate.
709   bool isEquality();
710
711   /// @returns true if the predicate is unsigned, false otherwise.
712   /// @brief Determine if the predicate is an unsigned operation.
713   static bool isUnsigned(unsigned short predicate);
714
715   /// @returns true if the predicate is signed, false otherwise.
716   /// @brief Determine if the predicate is an signed operation.
717   static bool isSigned(unsigned short predicate);
718
719   /// @brief Determine if the predicate is an ordered operation.
720   static bool isOrdered(unsigned short predicate);
721
722   /// @brief Determine if the predicate is an unordered operation.
723   static bool isUnordered(unsigned short predicate);
724
725   /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
726   static inline bool classof(const CmpInst *) { return true; }
727   static inline bool classof(const Instruction *I) {
728     return I->getOpcode() == Instruction::ICmp || 
729            I->getOpcode() == Instruction::FCmp ||
730            I->getOpcode() == Instruction::VICmp ||
731            I->getOpcode() == Instruction::VFCmp;
732   }
733   static inline bool classof(const Value *V) {
734     return isa<Instruction>(V) && classof(cast<Instruction>(V));
735   }
736   /// @brief Create a result type for fcmp/icmp (but not vicmp/vfcmp)
737   static const Type* makeCmpResultType(const Type* opnd_type) {
738     if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
739       return VectorType::get(Type::Int1Ty, vt->getNumElements());
740     }
741     return Type::Int1Ty;
742   }
743   /// Backward-compatible interfaces
744   /// @deprecated in 2.4, do not use, will disappear soon
745   static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1, 
746                          Value *S2, const std::string &Name = "",
747                          Instruction *InsertBefore = 0) {
748     return Create(Op, predicate, S1, S2, Name, InsertBefore);
749   }
750   static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1, 
751                          Value *S2, const std::string &Name, 
752                          BasicBlock *InsertAtEnd) {
753     return Create(Op, predicate, S1, S2, Name, InsertAtEnd);
754   }
755 };
756
757
758 // FIXME: these are redundant if CmpInst < BinaryOperator
759 template <>
760 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
761 };
762
763 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
764
765 } // End llvm namespace
766
767 #endif