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