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