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"
25 //===----------------------------------------------------------------------===//
26 // TerminatorInst Class
27 //===----------------------------------------------------------------------===//
29 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
30 /// block. Thus, these are all the flow control type of operations.
32 class TerminatorInst : public Instruction {
34 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
35 Use *Ops, unsigned NumOps,
36 Instruction *InsertBefore = 0)
37 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
39 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
40 Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
41 : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
43 // Out of line virtual method, so the vtable, etc has a home.
46 /// Virtual methods - Terminators should overload these and provide inline
47 /// overrides of non-V methods.
48 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
49 virtual unsigned getNumSuccessorsV() const = 0;
50 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
53 virtual Instruction *clone() const = 0;
55 /// getNumSuccessors - Return the number of successors that this terminator
57 unsigned getNumSuccessors() const {
58 return getNumSuccessorsV();
61 /// getSuccessor - Return the specified successor.
63 BasicBlock *getSuccessor(unsigned idx) const {
64 return getSuccessorV(idx);
67 /// setSuccessor - Update the specified successor to point at the provided
69 void setSuccessor(unsigned idx, BasicBlock *B) {
70 setSuccessorV(idx, B);
73 // Methods for support type inquiry through isa, cast, and dyn_cast:
74 static inline bool classof(const TerminatorInst *) { return true; }
75 static inline bool classof(const Instruction *I) {
76 return I->isTerminator();
78 static inline bool classof(const Value *V) {
79 return isa<Instruction>(V) && classof(cast<Instruction>(V));
84 //===----------------------------------------------------------------------===//
85 // UnaryInstruction Class
86 //===----------------------------------------------------------------------===//
88 class UnaryInstruction : public Instruction {
89 void *operator new(size_t, unsigned); // Do not implement
90 UnaryInstruction(const UnaryInstruction&); // Do not implement
93 UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
95 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
98 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
99 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
103 // allocate space for exactly one operand
104 void *operator new(size_t s) {
105 return User::operator new(s, 1);
108 // Out of line virtual method, so the vtable, etc has a home.
111 /// Transparently provide more efficient getOperand methods.
112 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
114 // Methods for support type inquiry through isa, cast, and dyn_cast:
115 static inline bool classof(const UnaryInstruction *) { return true; }
116 static inline bool classof(const Instruction *I) {
117 return I->getOpcode() == Instruction::Malloc ||
118 I->getOpcode() == Instruction::Alloca ||
119 I->getOpcode() == Instruction::Free ||
120 I->getOpcode() == Instruction::Load ||
121 I->getOpcode() == Instruction::VAArg ||
122 I->getOpcode() == Instruction::ExtractValue ||
123 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
125 static inline bool classof(const Value *V) {
126 return isa<Instruction>(V) && classof(cast<Instruction>(V));
131 struct OperandTraits<UnaryInstruction> : FixedNumOperandTraits<1> {
134 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
136 //===----------------------------------------------------------------------===//
137 // BinaryOperator Class
138 //===----------------------------------------------------------------------===//
140 class BinaryOperator : public Instruction {
141 void *operator new(size_t, unsigned); // Do not implement
143 void init(BinaryOps iType);
144 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
145 const std::string &Name, Instruction *InsertBefore);
146 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
147 const std::string &Name, BasicBlock *InsertAtEnd);
149 // allocate space for exactly two operands
150 void *operator new(size_t s) {
151 return User::operator new(s, 2);
154 /// Transparently provide more efficient getOperand methods.
155 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
157 /// Create() - Construct a binary instruction, given the opcode and the two
158 /// operands. Optionally (if InstBefore is specified) insert the instruction
159 /// into a BasicBlock right before the specified instruction. The specified
160 /// Instruction is allowed to be a dereferenced end iterator.
162 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
163 const std::string &Name = "",
164 Instruction *InsertBefore = 0);
166 /// Create() - Construct a binary instruction, given the opcode and the two
167 /// operands. Also automatically insert this instruction to the end of the
168 /// BasicBlock specified.
170 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
171 const std::string &Name,
172 BasicBlock *InsertAtEnd);
174 /// Create* - These methods just forward to Create, and are useful when you
175 /// statically know what type of instruction you're going to create. These
176 /// helpers just save some typing.
177 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
178 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
179 const std::string &Name = "") {\
180 return Create(Instruction::OPC, V1, V2, Name);\
182 #include "llvm/Instruction.def"
183 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
184 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
185 const std::string &Name, BasicBlock *BB) {\
186 return Create(Instruction::OPC, V1, V2, Name, BB);\
188 #include "llvm/Instruction.def"
189 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
190 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
191 const std::string &Name, Instruction *I) {\
192 return Create(Instruction::OPC, V1, V2, Name, I);\
194 #include "llvm/Instruction.def"
197 /// Helper functions to construct and inspect unary operations (NEG and NOT)
198 /// via binary operators SUB and XOR:
200 /// CreateNeg, CreateNot - Create the NEG and NOT
201 /// instructions out of SUB and XOR instructions.
203 static BinaryOperator *CreateNeg(Value *Op, const std::string &Name = "",
204 Instruction *InsertBefore = 0);
205 static BinaryOperator *CreateNeg(Value *Op, const std::string &Name,
206 BasicBlock *InsertAtEnd);
207 static BinaryOperator *CreateNot(Value *Op, const std::string &Name = "",
208 Instruction *InsertBefore = 0);
209 static BinaryOperator *CreateNot(Value *Op, const std::string &Name,
210 BasicBlock *InsertAtEnd);
212 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
214 static bool isNeg(const Value *V);
215 static bool isNot(const Value *V);
217 /// getNegArgument, getNotArgument - Helper functions to extract the
218 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
220 static const Value *getNegArgument(const Value *BinOp);
221 static Value *getNegArgument( Value *BinOp);
222 static const Value *getNotArgument(const Value *BinOp);
223 static Value *getNotArgument( Value *BinOp);
225 BinaryOps getOpcode() const {
226 return static_cast<BinaryOps>(Instruction::getOpcode());
229 virtual BinaryOperator *clone() const;
231 /// swapOperands - Exchange the two operands to this instruction.
232 /// This instruction is safe to use on any binary instruction and
233 /// does not modify the semantics of the instruction. If the instruction
234 /// cannot be reversed (ie, it's a Div), then return true.
238 // Methods for support type inquiry through isa, cast, and dyn_cast:
239 static inline bool classof(const BinaryOperator *) { return true; }
240 static inline bool classof(const Instruction *I) {
241 return I->isBinaryOp();
243 static inline bool classof(const Value *V) {
244 return isa<Instruction>(V) && classof(cast<Instruction>(V));
249 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
252 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
254 //===----------------------------------------------------------------------===//
256 //===----------------------------------------------------------------------===//
258 /// CastInst - This is the base class for all instructions that perform data
259 /// casts. It is simply provided so that instruction category testing
260 /// can be performed with code like:
262 /// if (isa<CastInst>(Instr)) { ... }
263 /// @brief Base class of casting instructions.
264 class CastInst : public UnaryInstruction {
265 /// @brief Copy constructor
266 CastInst(const CastInst &CI)
267 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
269 /// @brief Do not allow default construction
272 /// @brief Constructor with insert-before-instruction semantics for subclasses
273 CastInst(const Type *Ty, unsigned iType, Value *S,
274 const std::string &NameStr = "", Instruction *InsertBefore = 0)
275 : UnaryInstruction(Ty, iType, S, InsertBefore) {
278 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
279 CastInst(const Type *Ty, unsigned iType, Value *S,
280 const std::string &NameStr, BasicBlock *InsertAtEnd)
281 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
285 /// Provides a way to construct any of the CastInst subclasses using an
286 /// opcode instead of the subclass's constructor. The opcode must be in the
287 /// CastOps category (Instruction::isCast(opcode) returns true). This
288 /// constructor has insert-before-instruction semantics to automatically
289 /// insert the new CastInst before InsertBefore (if it is non-null).
290 /// @brief Construct any of the CastInst subclasses
291 static CastInst *Create(
292 Instruction::CastOps, ///< The opcode of the cast instruction
293 Value *S, ///< The value to be casted (operand 0)
294 const Type *Ty, ///< The type to which cast should be made
295 const std::string &Name = "", ///< Name for the instruction
296 Instruction *InsertBefore = 0 ///< Place to insert the instruction
298 /// Provides a way to construct any of the CastInst subclasses using an
299 /// opcode instead of the subclass's constructor. The opcode must be in the
300 /// CastOps category. This constructor has insert-at-end-of-block semantics
301 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
303 /// @brief Construct any of the CastInst subclasses
304 static CastInst *Create(
305 Instruction::CastOps, ///< The opcode for the cast instruction
306 Value *S, ///< The value to be casted (operand 0)
307 const Type *Ty, ///< The type to which operand is casted
308 const std::string &Name, ///< The name for the instruction
309 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
312 /// @brief Create a ZExt or BitCast cast instruction
313 static CastInst *CreateZExtOrBitCast(
314 Value *S, ///< The value to be casted (operand 0)
315 const Type *Ty, ///< The type to which cast should be made
316 const std::string &Name = "", ///< Name for the instruction
317 Instruction *InsertBefore = 0 ///< Place to insert the instruction
320 /// @brief Create a ZExt or BitCast cast instruction
321 static CastInst *CreateZExtOrBitCast(
322 Value *S, ///< The value to be casted (operand 0)
323 const Type *Ty, ///< The type to which operand is casted
324 const std::string &Name, ///< The name for the instruction
325 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
328 /// @brief Create a SExt or BitCast cast instruction
329 static CastInst *CreateSExtOrBitCast(
330 Value *S, ///< The value to be casted (operand 0)
331 const Type *Ty, ///< The type to which cast should be made
332 const std::string &Name = "", ///< Name for the instruction
333 Instruction *InsertBefore = 0 ///< Place to insert the instruction
336 /// @brief Create a SExt or BitCast cast instruction
337 static CastInst *CreateSExtOrBitCast(
338 Value *S, ///< The value to be casted (operand 0)
339 const Type *Ty, ///< The type to which operand is casted
340 const std::string &Name, ///< The name for the instruction
341 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
344 /// @brief Create a BitCast or a PtrToInt cast instruction
345 static CastInst *CreatePointerCast(
346 Value *S, ///< The pointer value to be casted (operand 0)
347 const Type *Ty, ///< The type to which operand is casted
348 const std::string &Name, ///< The name for the instruction
349 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
352 /// @brief Create a BitCast or a PtrToInt cast instruction
353 static CastInst *CreatePointerCast(
354 Value *S, ///< The pointer value to be casted (operand 0)
355 const Type *Ty, ///< The type to which cast should be made
356 const std::string &Name = "", ///< Name for the instruction
357 Instruction *InsertBefore = 0 ///< Place to insert the instruction
360 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
361 static CastInst *CreateIntegerCast(
362 Value *S, ///< The pointer value to be casted (operand 0)
363 const Type *Ty, ///< The type to which cast should be made
364 bool isSigned, ///< Whether to regard S as signed or not
365 const std::string &Name = "", ///< Name for the instruction
366 Instruction *InsertBefore = 0 ///< Place to insert the instruction
369 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
370 static CastInst *CreateIntegerCast(
371 Value *S, ///< The integer value to be casted (operand 0)
372 const Type *Ty, ///< The integer type to which operand is casted
373 bool isSigned, ///< Whether to regard S as signed or not
374 const std::string &Name, ///< The name for the instruction
375 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
378 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
379 static CastInst *CreateFPCast(
380 Value *S, ///< The floating point value to be casted
381 const Type *Ty, ///< The floating point type to cast to
382 const std::string &Name = "", ///< Name for the instruction
383 Instruction *InsertBefore = 0 ///< Place to insert the instruction
386 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
387 static CastInst *CreateFPCast(
388 Value *S, ///< The floating point value to be casted
389 const Type *Ty, ///< The floating point type to cast to
390 const std::string &Name, ///< The name for the instruction
391 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
394 /// @brief Create a Trunc or BitCast cast instruction
395 static CastInst *CreateTruncOrBitCast(
396 Value *S, ///< The value to be casted (operand 0)
397 const Type *Ty, ///< The type to which cast should be made
398 const std::string &Name = "", ///< Name for the instruction
399 Instruction *InsertBefore = 0 ///< Place to insert the instruction
402 /// @brief Create a Trunc or BitCast cast instruction
403 static CastInst *CreateTruncOrBitCast(
404 Value *S, ///< The value to be casted (operand 0)
405 const Type *Ty, ///< The type to which operand is casted
406 const std::string &Name, ///< The name for the instruction
407 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
410 /// @brief Check whether it is valid to call getCastOpcode for these types.
411 static bool isCastable(
412 const Type *SrcTy, ///< The Type from which the value should be cast.
413 const Type *DestTy ///< The Type to which the value should be cast.
416 /// Returns the opcode necessary to cast Val into Ty using usual casting
418 /// @brief Infer the opcode for cast operand and type
419 static Instruction::CastOps getCastOpcode(
420 const Value *Val, ///< The value to cast
421 bool SrcIsSigned, ///< Whether to treat the source as signed
422 const Type *Ty, ///< The Type to which the value should be casted
423 bool DstIsSigned ///< Whether to treate the dest. as signed
426 /// There are several places where we need to know if a cast instruction
427 /// only deals with integer source and destination types. To simplify that
428 /// logic, this method is provided.
429 /// @returns true iff the cast has only integral typed operand and dest type.
430 /// @brief Determine if this is an integer-only cast.
431 bool isIntegerCast() const;
433 /// A lossless cast is one that does not alter the basic value. It implies
434 /// a no-op cast but is more stringent, preventing things like int->float,
435 /// long->double, int->ptr, or vector->anything.
436 /// @returns true iff the cast is lossless.
437 /// @brief Determine if this is a lossless cast.
438 bool isLosslessCast() const;
440 /// A no-op cast is one that can be effected without changing any bits.
441 /// It implies that the source and destination types are the same size. The
442 /// IntPtrTy argument is used to make accurate determinations for casts
443 /// involving Integer and Pointer types. They are no-op casts if the integer
444 /// is the same size as the pointer. However, pointer size varies with
445 /// platform. Generally, the result of TargetData::getIntPtrType() should be
446 /// passed in. If that's not available, use Type::Int64Ty, which will make
447 /// the isNoopCast call conservative.
448 /// @brief Determine if this cast is a no-op cast.
450 const Type *IntPtrTy ///< Integer type corresponding to pointer
453 /// Determine how a pair of casts can be eliminated, if they can be at all.
454 /// This is a helper function for both CastInst and ConstantExpr.
455 /// @returns 0 if the CastInst pair can't be eliminated
456 /// @returns Instruction::CastOps value for a cast that can replace
457 /// the pair, casting SrcTy to DstTy.
458 /// @brief Determine if a cast pair is eliminable
459 static unsigned isEliminableCastPair(
460 Instruction::CastOps firstOpcode, ///< Opcode of first cast
461 Instruction::CastOps secondOpcode, ///< Opcode of second cast
462 const Type *SrcTy, ///< SrcTy of 1st cast
463 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
464 const Type *DstTy, ///< DstTy of 2nd cast
465 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
468 /// @brief Return the opcode of this CastInst
469 Instruction::CastOps getOpcode() const {
470 return Instruction::CastOps(Instruction::getOpcode());
473 /// @brief Return the source type, as a convenience
474 const Type* getSrcTy() const { return getOperand(0)->getType(); }
475 /// @brief Return the destination type, as a convenience
476 const Type* getDestTy() const { return getType(); }
478 /// This method can be used to determine if a cast from S to DstTy using
479 /// Opcode op is valid or not.
480 /// @returns true iff the proposed cast is valid.
481 /// @brief Determine if a cast is valid without creating one.
482 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
484 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
485 static inline bool classof(const CastInst *) { return true; }
486 static inline bool classof(const Instruction *I) {
489 static inline bool classof(const Value *V) {
490 return isa<Instruction>(V) && classof(cast<Instruction>(V));
494 //===----------------------------------------------------------------------===//
496 //===----------------------------------------------------------------------===//
498 /// This class is the base class for the comparison instructions.
499 /// @brief Abstract base class of comparison instructions.
500 // FIXME: why not derive from BinaryOperator?
501 class CmpInst: public Instruction {
502 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
503 CmpInst(); // do not implement
505 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
506 Value *LHS, Value *RHS, const std::string &Name = "",
507 Instruction *InsertBefore = 0);
509 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
510 Value *LHS, Value *RHS, const std::string &Name,
511 BasicBlock *InsertAtEnd);
514 /// This enumeration lists the possible predicates for CmpInst subclasses.
515 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
516 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
517 /// predicate values are not overlapping between the classes.
519 // Opcode U L G E Intuitive operation
520 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
521 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
522 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
523 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
524 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
525 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
526 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
527 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
528 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
529 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
530 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
531 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
532 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
533 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
534 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
535 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
536 FIRST_FCMP_PREDICATE = FCMP_FALSE,
537 LAST_FCMP_PREDICATE = FCMP_TRUE,
538 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
539 ICMP_EQ = 32, /// equal
540 ICMP_NE = 33, /// not equal
541 ICMP_UGT = 34, /// unsigned greater than
542 ICMP_UGE = 35, /// unsigned greater or equal
543 ICMP_ULT = 36, /// unsigned less than
544 ICMP_ULE = 37, /// unsigned less or equal
545 ICMP_SGT = 38, /// signed greater than
546 ICMP_SGE = 39, /// signed greater or equal
547 ICMP_SLT = 40, /// signed less than
548 ICMP_SLE = 41, /// signed less or equal
549 FIRST_ICMP_PREDICATE = ICMP_EQ,
550 LAST_ICMP_PREDICATE = ICMP_SLE,
551 BAD_ICMP_PREDICATE = ICMP_SLE + 1
554 // allocate space for exactly two operands
555 void *operator new(size_t s) {
556 return User::operator new(s, 2);
558 /// Construct a compare instruction, given the opcode, the predicate and
559 /// the two operands. Optionally (if InstBefore is specified) insert the
560 /// instruction into a BasicBlock right before the specified instruction.
561 /// The specified Instruction is allowed to be a dereferenced end iterator.
562 /// @brief Create a CmpInst
563 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
564 Value *S2, const std::string &Name = "",
565 Instruction *InsertBefore = 0);
567 /// Construct a compare instruction, given the opcode, the predicate and the
568 /// two operands. Also automatically insert this instruction to the end of
569 /// the BasicBlock specified.
570 /// @brief Create a CmpInst
571 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
572 Value *S2, const std::string &Name,
573 BasicBlock *InsertAtEnd);
575 /// @brief Get the opcode casted to the right type
576 OtherOps getOpcode() const {
577 return static_cast<OtherOps>(Instruction::getOpcode());
580 /// @brief Return the predicate for this instruction.
581 Predicate getPredicate() const { return Predicate(SubclassData); }
583 /// @brief Set the predicate for this instruction to the specified value.
584 void setPredicate(Predicate P) { SubclassData = P; }
586 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
587 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
588 /// @returns the inverse predicate for the instruction's current predicate.
589 /// @brief Return the inverse of the instruction's predicate.
590 Predicate getInversePredicate() const {
591 return getInversePredicate(getPredicate());
594 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
595 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
596 /// @returns the inverse predicate for predicate provided in \p pred.
597 /// @brief Return the inverse of a given predicate
598 static Predicate getInversePredicate(Predicate pred);
600 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
601 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
602 /// @returns the predicate that would be the result of exchanging the two
603 /// operands of the CmpInst instruction without changing the result
605 /// @brief Return the predicate as if the operands were swapped
606 Predicate getSwappedPredicate() const {
607 return getSwappedPredicate(getPredicate());
610 /// This is a static version that you can use without an instruction
612 /// @brief Return the predicate as if the operands were swapped.
613 static Predicate getSwappedPredicate(Predicate pred);
615 /// @brief Provide more efficient getOperand methods.
616 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
618 /// This is just a convenience that dispatches to the subclasses.
619 /// @brief Swap the operands and adjust predicate accordingly to retain
620 /// the same comparison.
623 /// This is just a convenience that dispatches to the subclasses.
624 /// @brief Determine if this CmpInst is commutative.
625 bool isCommutative();
627 /// This is just a convenience that dispatches to the subclasses.
628 /// @brief Determine if this is an equals/not equals predicate.
631 /// @returns true if the predicate is unsigned, false otherwise.
632 /// @brief Determine if the predicate is an unsigned operation.
633 static bool isUnsigned(unsigned short predicate);
635 /// @returns true if the predicate is signed, false otherwise.
636 /// @brief Determine if the predicate is an signed operation.
637 static bool isSigned(unsigned short predicate);
639 /// @brief Determine if the predicate is an ordered operation.
640 static bool isOrdered(unsigned short predicate);
642 /// @brief Determine if the predicate is an unordered operation.
643 static bool isUnordered(unsigned short predicate);
645 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
646 static inline bool classof(const CmpInst *) { return true; }
647 static inline bool classof(const Instruction *I) {
648 return I->getOpcode() == Instruction::ICmp ||
649 I->getOpcode() == Instruction::FCmp ||
650 I->getOpcode() == Instruction::VICmp ||
651 I->getOpcode() == Instruction::VFCmp;
653 static inline bool classof(const Value *V) {
654 return isa<Instruction>(V) && classof(cast<Instruction>(V));
656 /// @brief Create a result type for fcmp/icmp (but not vicmp/vfcmp)
657 static const Type* makeCmpResultType(const Type* opnd_type) {
658 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
659 return VectorType::get(Type::Int1Ty, vt->getNumElements());
666 // FIXME: these are redundant if CmpInst < BinaryOperator
668 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
671 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
673 } // End llvm namespace