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