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 std::string &Name, Instruction *InsertBefore);
148 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
149 const std::string &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 std::string &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 std::string &Name,
174 BasicBlock *InsertAtEnd);
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 std::string &Name = "") {\
182 return Create(Instruction::OPC, V1, V2, Name);\
184 #include "llvm/Instruction.def"
185 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
186 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
187 const std::string &Name, BasicBlock *BB) {\
188 return Create(Instruction::OPC, V1, V2, Name, BB);\
190 #include "llvm/Instruction.def"
191 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
192 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
193 const std::string &Name, Instruction *I) {\
194 return Create(Instruction::OPC, V1, V2, Name, I);\
196 #include "llvm/Instruction.def"
199 /// Helper functions to construct and inspect unary operations (NEG and NOT)
200 /// via binary operators SUB and XOR:
202 /// CreateNeg, CreateNot - Create the NEG and NOT
203 /// instructions out of SUB and XOR instructions.
205 static BinaryOperator *CreateNeg(LLVMContext &Context,
206 Value *Op, const std::string &Name = "",
207 Instruction *InsertBefore = 0);
208 static BinaryOperator *CreateNeg(LLVMContext &Context,
209 Value *Op, const std::string &Name,
210 BasicBlock *InsertAtEnd);
211 static BinaryOperator *CreateFNeg(LLVMContext &Context,
212 Value *Op, const std::string &Name = "",
213 Instruction *InsertBefore = 0);
214 static BinaryOperator *CreateFNeg(LLVMContext &Context,
215 Value *Op, const std::string &Name,
216 BasicBlock *InsertAtEnd);
217 static BinaryOperator *CreateNot(Value *Op, const std::string &Name = "",
218 Instruction *InsertBefore = 0);
219 static BinaryOperator *CreateNot(Value *Op, const std::string &Name,
220 BasicBlock *InsertAtEnd);
222 /// isNeg, isFNeg, isNot - Check if the given Value is a
223 /// NEG, FNeg, or NOT instruction.
225 static bool isNeg(LLVMContext &Context, const Value *V);
226 static bool isFNeg(LLVMContext &Context, const Value *V);
227 static bool isNot(const Value *V);
229 /// getNegArgument, getNotArgument - Helper functions to extract the
230 /// unary argument of a NEG, FNEG or NOT operation implemented via
231 /// Sub, FSub, or Xor.
233 static const Value *getNegArgument(const Value *BinOp);
234 static Value *getNegArgument( Value *BinOp);
235 static const Value *getFNegArgument(const Value *BinOp);
236 static Value *getFNegArgument( Value *BinOp);
237 static const Value *getNotArgument(const Value *BinOp);
238 static Value *getNotArgument( Value *BinOp);
240 BinaryOps getOpcode() const {
241 return static_cast<BinaryOps>(Instruction::getOpcode());
244 virtual BinaryOperator *clone(LLVMContext &Context) const;
246 /// swapOperands - Exchange the two operands to this instruction.
247 /// This instruction is safe to use on any binary instruction and
248 /// does not modify the semantics of the instruction. If the instruction
249 /// cannot be reversed (ie, it's a Div), then return true.
253 // Methods for support type inquiry through isa, cast, and dyn_cast:
254 static inline bool classof(const BinaryOperator *) { return true; }
255 static inline bool classof(const Instruction *I) {
256 return I->isBinaryOp();
258 static inline bool classof(const Value *V) {
259 return isa<Instruction>(V) && classof(cast<Instruction>(V));
264 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
267 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
269 //===----------------------------------------------------------------------===//
271 //===----------------------------------------------------------------------===//
273 /// CastInst - This is the base class for all instructions that perform data
274 /// casts. It is simply provided so that instruction category testing
275 /// can be performed with code like:
277 /// if (isa<CastInst>(Instr)) { ... }
278 /// @brief Base class of casting instructions.
279 class CastInst : public UnaryInstruction {
280 /// @brief Copy constructor
281 CastInst(const CastInst &CI)
282 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
284 /// @brief Do not allow default construction
287 /// @brief Constructor with insert-before-instruction semantics for subclasses
288 CastInst(const Type *Ty, unsigned iType, Value *S,
289 const std::string &NameStr = "", Instruction *InsertBefore = 0)
290 : UnaryInstruction(Ty, iType, S, InsertBefore) {
293 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
294 CastInst(const Type *Ty, unsigned iType, Value *S,
295 const std::string &NameStr, BasicBlock *InsertAtEnd)
296 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
300 /// Provides a way to construct any of the CastInst subclasses using an
301 /// opcode instead of the subclass's constructor. The opcode must be in the
302 /// CastOps category (Instruction::isCast(opcode) returns true). This
303 /// constructor has insert-before-instruction semantics to automatically
304 /// insert the new CastInst before InsertBefore (if it is non-null).
305 /// @brief Construct any of the CastInst subclasses
306 static CastInst *Create(
307 Instruction::CastOps, ///< The opcode of the cast instruction
308 Value *S, ///< The value to be casted (operand 0)
309 const Type *Ty, ///< The type to which cast should be made
310 const std::string &Name = "", ///< Name for the instruction
311 Instruction *InsertBefore = 0 ///< Place to insert the instruction
313 /// Provides a way to construct any of the CastInst subclasses using an
314 /// opcode instead of the subclass's constructor. The opcode must be in the
315 /// CastOps category. This constructor has insert-at-end-of-block semantics
316 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
318 /// @brief Construct any of the CastInst subclasses
319 static CastInst *Create(
320 Instruction::CastOps, ///< The opcode for the cast instruction
321 Value *S, ///< The value to be casted (operand 0)
322 const Type *Ty, ///< The type to which operand is casted
323 const std::string &Name, ///< The name for the instruction
324 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
327 /// @brief Create a ZExt or BitCast cast instruction
328 static CastInst *CreateZExtOrBitCast(
329 Value *S, ///< The value to be casted (operand 0)
330 const Type *Ty, ///< The type to which cast should be made
331 const std::string &Name = "", ///< Name for the instruction
332 Instruction *InsertBefore = 0 ///< Place to insert the instruction
335 /// @brief Create a ZExt or BitCast cast instruction
336 static CastInst *CreateZExtOrBitCast(
337 Value *S, ///< The value to be casted (operand 0)
338 const Type *Ty, ///< The type to which operand is casted
339 const std::string &Name, ///< The name for the instruction
340 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
343 /// @brief Create a SExt or BitCast cast instruction
344 static CastInst *CreateSExtOrBitCast(
345 Value *S, ///< The value to be casted (operand 0)
346 const Type *Ty, ///< The type to which cast should be made
347 const std::string &Name = "", ///< Name for the instruction
348 Instruction *InsertBefore = 0 ///< Place to insert the instruction
351 /// @brief Create a SExt or BitCast cast instruction
352 static CastInst *CreateSExtOrBitCast(
353 Value *S, ///< The value to be casted (operand 0)
354 const Type *Ty, ///< The type to which operand is casted
355 const std::string &Name, ///< The name for the instruction
356 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
359 /// @brief Create a BitCast or a PtrToInt cast instruction
360 static CastInst *CreatePointerCast(
361 Value *S, ///< The pointer value to be casted (operand 0)
362 const Type *Ty, ///< The type to which operand is casted
363 const std::string &Name, ///< The name for the instruction
364 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
367 /// @brief Create a BitCast or a PtrToInt cast instruction
368 static CastInst *CreatePointerCast(
369 Value *S, ///< The pointer value to be casted (operand 0)
370 const Type *Ty, ///< The type to which cast should be made
371 const std::string &Name = "", ///< Name for the instruction
372 Instruction *InsertBefore = 0 ///< Place to insert the instruction
375 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
376 static CastInst *CreateIntegerCast(
377 Value *S, ///< The pointer value to be casted (operand 0)
378 const Type *Ty, ///< The type to which cast should be made
379 bool isSigned, ///< Whether to regard S as signed or not
380 const std::string &Name = "", ///< Name for the instruction
381 Instruction *InsertBefore = 0 ///< Place to insert the instruction
384 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
385 static CastInst *CreateIntegerCast(
386 Value *S, ///< The integer value to be casted (operand 0)
387 const Type *Ty, ///< The integer type to which operand is casted
388 bool isSigned, ///< Whether to regard S as signed or not
389 const std::string &Name, ///< The name for the instruction
390 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
393 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
394 static CastInst *CreateFPCast(
395 Value *S, ///< The floating point value to be casted
396 const Type *Ty, ///< The floating point type to cast to
397 const std::string &Name = "", ///< Name for the instruction
398 Instruction *InsertBefore = 0 ///< Place to insert the instruction
401 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
402 static CastInst *CreateFPCast(
403 Value *S, ///< The floating point value to be casted
404 const Type *Ty, ///< The floating point type to cast to
405 const std::string &Name, ///< The name for the instruction
406 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
409 /// @brief Create a Trunc or BitCast cast instruction
410 static CastInst *CreateTruncOrBitCast(
411 Value *S, ///< The value to be casted (operand 0)
412 const Type *Ty, ///< The type to which cast should be made
413 const std::string &Name = "", ///< Name for the instruction
414 Instruction *InsertBefore = 0 ///< Place to insert the instruction
417 /// @brief Create a Trunc or BitCast cast instruction
418 static CastInst *CreateTruncOrBitCast(
419 Value *S, ///< The value to be casted (operand 0)
420 const Type *Ty, ///< The type to which operand is casted
421 const std::string &Name, ///< The name for the instruction
422 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
425 /// @brief Check whether it is valid to call getCastOpcode for these types.
426 static bool isCastable(
427 const Type *SrcTy, ///< The Type from which the value should be cast.
428 const Type *DestTy ///< The Type to which the value should be cast.
431 /// Returns the opcode necessary to cast Val into Ty using usual casting
433 /// @brief Infer the opcode for cast operand and type
434 static Instruction::CastOps getCastOpcode(
435 const Value *Val, ///< The value to cast
436 bool SrcIsSigned, ///< Whether to treat the source as signed
437 const Type *Ty, ///< The Type to which the value should be casted
438 bool DstIsSigned ///< Whether to treate the dest. as signed
441 /// There are several places where we need to know if a cast instruction
442 /// only deals with integer source and destination types. To simplify that
443 /// logic, this method is provided.
444 /// @returns true iff the cast has only integral typed operand and dest type.
445 /// @brief Determine if this is an integer-only cast.
446 bool isIntegerCast() const;
448 /// A lossless cast is one that does not alter the basic value. It implies
449 /// a no-op cast but is more stringent, preventing things like int->float,
450 /// long->double, int->ptr, or vector->anything.
451 /// @returns true iff the cast is lossless.
452 /// @brief Determine if this is a lossless cast.
453 bool isLosslessCast() const;
455 /// A no-op cast is one that can be effected without changing any bits.
456 /// It implies that the source and destination types are the same size. The
457 /// IntPtrTy argument is used to make accurate determinations for casts
458 /// involving Integer and Pointer types. They are no-op casts if the integer
459 /// is the same size as the pointer. However, pointer size varies with
460 /// platform. Generally, the result of TargetData::getIntPtrType() should be
461 /// passed in. If that's not available, use Type::Int64Ty, which will make
462 /// the isNoopCast call conservative.
463 /// @brief Determine if this cast is a no-op cast.
465 const Type *IntPtrTy ///< Integer type corresponding to pointer
468 /// Determine how a pair of casts can be eliminated, if they can be at all.
469 /// This is a helper function for both CastInst and ConstantExpr.
470 /// @returns 0 if the CastInst pair can't be eliminated
471 /// @returns Instruction::CastOps value for a cast that can replace
472 /// the pair, casting SrcTy to DstTy.
473 /// @brief Determine if a cast pair is eliminable
474 static unsigned isEliminableCastPair(
475 Instruction::CastOps firstOpcode, ///< Opcode of first cast
476 Instruction::CastOps secondOpcode, ///< Opcode of second cast
477 const Type *SrcTy, ///< SrcTy of 1st cast
478 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
479 const Type *DstTy, ///< DstTy of 2nd cast
480 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
483 /// @brief Return the opcode of this CastInst
484 Instruction::CastOps getOpcode() const {
485 return Instruction::CastOps(Instruction::getOpcode());
488 /// @brief Return the source type, as a convenience
489 const Type* getSrcTy() const { return getOperand(0)->getType(); }
490 /// @brief Return the destination type, as a convenience
491 const Type* getDestTy() const { return getType(); }
493 /// This method can be used to determine if a cast from S to DstTy using
494 /// Opcode op is valid or not.
495 /// @returns true iff the proposed cast is valid.
496 /// @brief Determine if a cast is valid without creating one.
497 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
499 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
500 static inline bool classof(const CastInst *) { return true; }
501 static inline bool classof(const Instruction *I) {
504 static inline bool classof(const Value *V) {
505 return isa<Instruction>(V) && classof(cast<Instruction>(V));
509 //===----------------------------------------------------------------------===//
511 //===----------------------------------------------------------------------===//
513 /// This class is the base class for the comparison instructions.
514 /// @brief Abstract base class of comparison instructions.
515 // FIXME: why not derive from BinaryOperator?
516 class CmpInst: public Instruction {
517 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
518 CmpInst(); // do not implement
520 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
521 Value *LHS, Value *RHS, const std::string &Name = "",
522 Instruction *InsertBefore = 0);
524 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
525 Value *LHS, Value *RHS, const std::string &Name,
526 BasicBlock *InsertAtEnd);
529 /// This enumeration lists the possible predicates for CmpInst subclasses.
530 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
531 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
532 /// predicate values are not overlapping between the classes.
534 // Opcode U L G E Intuitive operation
535 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
536 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
537 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
538 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
539 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
540 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
541 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
542 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
543 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
544 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
545 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
546 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
547 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
548 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
549 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
550 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
551 FIRST_FCMP_PREDICATE = FCMP_FALSE,
552 LAST_FCMP_PREDICATE = FCMP_TRUE,
553 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
554 ICMP_EQ = 32, /// equal
555 ICMP_NE = 33, /// not equal
556 ICMP_UGT = 34, /// unsigned greater than
557 ICMP_UGE = 35, /// unsigned greater or equal
558 ICMP_ULT = 36, /// unsigned less than
559 ICMP_ULE = 37, /// unsigned less or equal
560 ICMP_SGT = 38, /// signed greater than
561 ICMP_SGE = 39, /// signed greater or equal
562 ICMP_SLT = 40, /// signed less than
563 ICMP_SLE = 41, /// signed less or equal
564 FIRST_ICMP_PREDICATE = ICMP_EQ,
565 LAST_ICMP_PREDICATE = ICMP_SLE,
566 BAD_ICMP_PREDICATE = ICMP_SLE + 1
569 // allocate space for exactly two operands
570 void *operator new(size_t s) {
571 return User::operator new(s, 2);
573 /// Construct a compare instruction, given the opcode, the predicate and
574 /// the two operands. Optionally (if InstBefore is specified) insert the
575 /// instruction into a BasicBlock right before the specified instruction.
576 /// The specified Instruction is allowed to be a dereferenced end iterator.
577 /// @brief Create a CmpInst
578 static CmpInst *Create(LLVMContext &Context, OtherOps Op,
579 unsigned short predicate, Value *S1,
580 Value *S2, const std::string &Name = "",
581 Instruction *InsertBefore = 0);
583 /// Construct a compare instruction, given the opcode, the predicate and the
584 /// two operands. Also automatically insert this instruction to the end of
585 /// the BasicBlock specified.
586 /// @brief Create a CmpInst
587 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
588 Value *S2, const std::string &Name,
589 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));
673 // FIXME: these are redundant if CmpInst < BinaryOperator
675 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
678 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
680 } // End llvm namespace