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/Operator.h"
22 #include "llvm/DerivedTypes.h"
28 //===----------------------------------------------------------------------===//
29 // TerminatorInst Class
30 //===----------------------------------------------------------------------===//
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.
35 class TerminatorInst : public Instruction {
37 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
38 Use *Ops, unsigned NumOps,
39 Instruction *InsertBefore = 0)
40 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
42 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
43 Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
44 : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
46 // Out of line virtual method, so the vtable, etc has a home.
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;
56 virtual TerminatorInst *clone() const = 0;
58 /// getNumSuccessors - Return the number of successors that this terminator
60 unsigned getNumSuccessors() const {
61 return getNumSuccessorsV();
64 /// getSuccessor - Return the specified successor.
66 BasicBlock *getSuccessor(unsigned idx) const {
67 return getSuccessorV(idx);
70 /// setSuccessor - Update the specified successor to point at the provided
72 void setSuccessor(unsigned idx, BasicBlock *B) {
73 setSuccessorV(idx, B);
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();
81 static inline bool classof(const Value *V) {
82 return isa<Instruction>(V) && classof(cast<Instruction>(V));
87 //===----------------------------------------------------------------------===//
88 // UnaryInstruction Class
89 //===----------------------------------------------------------------------===//
91 class UnaryInstruction : public Instruction {
92 void *operator new(size_t, unsigned); // 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::Alloca ||
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> : public 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 Twine &Name, Instruction *InsertBefore);
146 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
147 const Twine &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 Twine &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 Twine &Name, BasicBlock *InsertAtEnd);
173 /// Create* - These methods just forward to Create, and are useful when you
174 /// statically know what type of instruction you're going to create. These
175 /// helpers just save some typing.
176 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
177 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
178 const Twine &Name = "") {\
179 return Create(Instruction::OPC, V1, V2, Name);\
181 #include "llvm/Instruction.def"
182 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
183 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
184 const Twine &Name, BasicBlock *BB) {\
185 return Create(Instruction::OPC, V1, V2, Name, BB);\
187 #include "llvm/Instruction.def"
188 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
189 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
190 const Twine &Name, Instruction *I) {\
191 return Create(Instruction::OPC, V1, V2, Name, I);\
193 #include "llvm/Instruction.def"
196 /// CreateNSWAdd - Create an Add operator with the NSW flag set.
198 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
199 const Twine &Name = "") {
200 BinaryOperator *BO = CreateAdd(V1, V2, Name);
201 BO->setHasNoSignedWrap(true);
204 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
205 const Twine &Name, BasicBlock *BB) {
206 BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
207 BO->setHasNoSignedWrap(true);
210 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
211 const Twine &Name, Instruction *I) {
212 BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
213 BO->setHasNoSignedWrap(true);
217 /// CreateNSWSub - Create an Sub operator with the NSW flag set.
219 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
220 const Twine &Name = "") {
221 BinaryOperator *BO = CreateSub(V1, V2, Name);
222 BO->setHasNoSignedWrap(true);
225 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
226 const Twine &Name, BasicBlock *BB) {
227 BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
228 BO->setHasNoSignedWrap(true);
231 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
232 const Twine &Name, Instruction *I) {
233 BinaryOperator *BO = CreateSub(V1, V2, Name, I);
234 BO->setHasNoSignedWrap(true);
238 /// CreateExactSDiv - Create an SDiv operator with the exact flag set.
240 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
241 const Twine &Name = "") {
242 BinaryOperator *BO = CreateSDiv(V1, V2, Name);
243 BO->setIsExact(true);
246 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
247 const Twine &Name, BasicBlock *BB) {
248 BinaryOperator *BO = CreateSDiv(V1, V2, Name, BB);
249 BO->setIsExact(true);
252 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
253 const Twine &Name, Instruction *I) {
254 BinaryOperator *BO = CreateSDiv(V1, V2, Name, I);
255 BO->setIsExact(true);
259 /// Helper functions to construct and inspect unary operations (NEG and NOT)
260 /// via binary operators SUB and XOR:
262 /// CreateNeg, CreateNot - Create the NEG and NOT
263 /// instructions out of SUB and XOR instructions.
265 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
266 Instruction *InsertBefore = 0);
267 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
268 BasicBlock *InsertAtEnd);
269 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
270 Instruction *InsertBefore = 0);
271 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
272 BasicBlock *InsertAtEnd);
273 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
274 Instruction *InsertBefore = 0);
275 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
276 BasicBlock *InsertAtEnd);
278 /// isNeg, isFNeg, isNot - Check if the given Value is a
279 /// NEG, FNeg, or NOT instruction.
281 static bool isNeg(const Value *V);
282 static bool isFNeg(const Value *V);
283 static bool isNot(const Value *V);
285 /// getNegArgument, getNotArgument - Helper functions to extract the
286 /// unary argument of a NEG, FNEG or NOT operation implemented via
287 /// Sub, FSub, or Xor.
289 static const Value *getNegArgument(const Value *BinOp);
290 static Value *getNegArgument( Value *BinOp);
291 static const Value *getFNegArgument(const Value *BinOp);
292 static Value *getFNegArgument( Value *BinOp);
293 static const Value *getNotArgument(const Value *BinOp);
294 static Value *getNotArgument( Value *BinOp);
296 BinaryOps getOpcode() const {
297 return static_cast<BinaryOps>(Instruction::getOpcode());
300 virtual BinaryOperator *clone() const;
302 /// swapOperands - Exchange the two operands to this instruction.
303 /// This instruction is safe to use on any binary instruction and
304 /// does not modify the semantics of the instruction. If the instruction
305 /// cannot be reversed (ie, it's a Div), then return true.
309 /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
310 /// which must be an operator which supports this flag. See LangRef.html
311 /// for the meaning of this flag.
312 void setHasNoUnsignedWrap(bool b = true);
314 /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
315 /// which must be an operator which supports this flag. See LangRef.html
316 /// for the meaning of this flag.
317 void setHasNoSignedWrap(bool b = true);
319 /// setIsExact - Set or clear the exact flag on this instruction,
320 /// which must be an operator which supports this flag. See LangRef.html
321 /// for the meaning of this flag.
322 void setIsExact(bool b = true);
324 /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
325 bool hasNoUnsignedWrap() const;
327 /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
328 bool hasNoSignedWrap() const;
330 /// isExact - Determine whether the exact flag is set.
331 bool isExact() const;
333 // Methods for support type inquiry through isa, cast, and dyn_cast:
334 static inline bool classof(const BinaryOperator *) { return true; }
335 static inline bool classof(const Instruction *I) {
336 return I->isBinaryOp();
338 static inline bool classof(const Value *V) {
339 return isa<Instruction>(V) && classof(cast<Instruction>(V));
344 struct OperandTraits<BinaryOperator> : public FixedNumOperandTraits<2> {
347 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
349 //===----------------------------------------------------------------------===//
351 //===----------------------------------------------------------------------===//
353 /// CastInst - This is the base class for all instructions that perform data
354 /// casts. It is simply provided so that instruction category testing
355 /// can be performed with code like:
357 /// if (isa<CastInst>(Instr)) { ... }
358 /// @brief Base class of casting instructions.
359 class CastInst : public UnaryInstruction {
361 /// @brief Constructor with insert-before-instruction semantics for subclasses
362 CastInst(const Type *Ty, unsigned iType, Value *S,
363 const Twine &NameStr = "", Instruction *InsertBefore = 0)
364 : UnaryInstruction(Ty, iType, S, InsertBefore) {
367 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
368 CastInst(const Type *Ty, unsigned iType, Value *S,
369 const Twine &NameStr, BasicBlock *InsertAtEnd)
370 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
374 /// Provides a way to construct any of the CastInst subclasses using an
375 /// opcode instead of the subclass's constructor. The opcode must be in the
376 /// CastOps category (Instruction::isCast(opcode) returns true). This
377 /// constructor has insert-before-instruction semantics to automatically
378 /// insert the new CastInst before InsertBefore (if it is non-null).
379 /// @brief Construct any of the CastInst subclasses
380 static CastInst *Create(
381 Instruction::CastOps, ///< The opcode of the cast instruction
382 Value *S, ///< The value to be casted (operand 0)
383 const Type *Ty, ///< The type to which cast should be made
384 const Twine &Name = "", ///< Name for the instruction
385 Instruction *InsertBefore = 0 ///< Place to insert the instruction
387 /// Provides a way to construct any of the CastInst subclasses using an
388 /// opcode instead of the subclass's constructor. The opcode must be in the
389 /// CastOps category. This constructor has insert-at-end-of-block semantics
390 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
392 /// @brief Construct any of the CastInst subclasses
393 static CastInst *Create(
394 Instruction::CastOps, ///< The opcode for the cast instruction
395 Value *S, ///< The value to be casted (operand 0)
396 const Type *Ty, ///< The type to which operand is casted
397 const Twine &Name, ///< The name for the instruction
398 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
401 /// @brief Create a ZExt or BitCast cast instruction
402 static CastInst *CreateZExtOrBitCast(
403 Value *S, ///< The value to be casted (operand 0)
404 const Type *Ty, ///< The type to which cast should be made
405 const Twine &Name = "", ///< Name for the instruction
406 Instruction *InsertBefore = 0 ///< Place to insert the instruction
409 /// @brief Create a ZExt or BitCast cast instruction
410 static CastInst *CreateZExtOrBitCast(
411 Value *S, ///< The value to be casted (operand 0)
412 const Type *Ty, ///< The type to which operand is casted
413 const Twine &Name, ///< The name for the instruction
414 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
417 /// @brief Create a SExt or BitCast cast instruction
418 static CastInst *CreateSExtOrBitCast(
419 Value *S, ///< The value to be casted (operand 0)
420 const Type *Ty, ///< The type to which cast should be made
421 const Twine &Name = "", ///< Name for the instruction
422 Instruction *InsertBefore = 0 ///< Place to insert the instruction
425 /// @brief Create a SExt or BitCast cast instruction
426 static CastInst *CreateSExtOrBitCast(
427 Value *S, ///< The value to be casted (operand 0)
428 const Type *Ty, ///< The type to which operand is casted
429 const Twine &Name, ///< The name for the instruction
430 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
433 /// @brief Create a BitCast or a PtrToInt cast instruction
434 static CastInst *CreatePointerCast(
435 Value *S, ///< The pointer value to be casted (operand 0)
436 const Type *Ty, ///< The type to which operand is casted
437 const Twine &Name, ///< The name for the instruction
438 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
441 /// @brief Create a BitCast or a PtrToInt cast instruction
442 static CastInst *CreatePointerCast(
443 Value *S, ///< The pointer value to be casted (operand 0)
444 const Type *Ty, ///< The type to which cast should be made
445 const Twine &Name = "", ///< Name for the instruction
446 Instruction *InsertBefore = 0 ///< Place to insert the instruction
449 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
450 static CastInst *CreateIntegerCast(
451 Value *S, ///< The pointer value to be casted (operand 0)
452 const Type *Ty, ///< The type to which cast should be made
453 bool isSigned, ///< Whether to regard S as signed or not
454 const Twine &Name = "", ///< Name for the instruction
455 Instruction *InsertBefore = 0 ///< Place to insert the instruction
458 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
459 static CastInst *CreateIntegerCast(
460 Value *S, ///< The integer value to be casted (operand 0)
461 const Type *Ty, ///< The integer type to which operand is casted
462 bool isSigned, ///< Whether to regard S as signed or not
463 const Twine &Name, ///< The name for the instruction
464 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
467 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
468 static CastInst *CreateFPCast(
469 Value *S, ///< The floating point value to be casted
470 const Type *Ty, ///< The floating point type to cast to
471 const Twine &Name = "", ///< Name for the instruction
472 Instruction *InsertBefore = 0 ///< Place to insert the instruction
475 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
476 static CastInst *CreateFPCast(
477 Value *S, ///< The floating point value to be casted
478 const Type *Ty, ///< The floating point type to cast to
479 const Twine &Name, ///< The name for the instruction
480 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
483 /// @brief Create a Trunc or BitCast cast instruction
484 static CastInst *CreateTruncOrBitCast(
485 Value *S, ///< The value to be casted (operand 0)
486 const Type *Ty, ///< The type to which cast should be made
487 const Twine &Name = "", ///< Name for the instruction
488 Instruction *InsertBefore = 0 ///< Place to insert the instruction
491 /// @brief Create a Trunc or BitCast cast instruction
492 static CastInst *CreateTruncOrBitCast(
493 Value *S, ///< The value to be casted (operand 0)
494 const Type *Ty, ///< The type to which operand is casted
495 const Twine &Name, ///< The name for the instruction
496 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
499 /// @brief Check whether it is valid to call getCastOpcode for these types.
500 static bool isCastable(
501 const Type *SrcTy, ///< The Type from which the value should be cast.
502 const Type *DestTy ///< The Type to which the value should be cast.
505 /// Returns the opcode necessary to cast Val into Ty using usual casting
507 /// @brief Infer the opcode for cast operand and type
508 static Instruction::CastOps getCastOpcode(
509 const Value *Val, ///< The value to cast
510 bool SrcIsSigned, ///< Whether to treat the source as signed
511 const Type *Ty, ///< The Type to which the value should be casted
512 bool DstIsSigned ///< Whether to treate the dest. as signed
515 /// There are several places where we need to know if a cast instruction
516 /// only deals with integer source and destination types. To simplify that
517 /// logic, this method is provided.
518 /// @returns true iff the cast has only integral typed operand and dest type.
519 /// @brief Determine if this is an integer-only cast.
520 bool isIntegerCast() const;
522 /// A lossless cast is one that does not alter the basic value. It implies
523 /// a no-op cast but is more stringent, preventing things like int->float,
524 /// long->double, int->ptr, or vector->anything.
525 /// @returns true iff the cast is lossless.
526 /// @brief Determine if this is a lossless cast.
527 bool isLosslessCast() const;
529 /// A no-op cast is one that can be effected without changing any bits.
530 /// It implies that the source and destination types are the same size. The
531 /// IntPtrTy argument is used to make accurate determinations for casts
532 /// involving Integer and Pointer types. They are no-op casts if the integer
533 /// is the same size as the pointer. However, pointer size varies with
534 /// platform. Generally, the result of TargetData::getIntPtrType() should be
535 /// passed in. If that's not available, use Type::Int64Ty, which will make
536 /// the isNoopCast call conservative.
537 /// @brief Determine if this cast is a no-op cast.
539 const Type *IntPtrTy ///< Integer type corresponding to pointer
542 /// Determine how a pair of casts can be eliminated, if they can be at all.
543 /// This is a helper function for both CastInst and ConstantExpr.
544 /// @returns 0 if the CastInst pair can't be eliminated
545 /// @returns Instruction::CastOps value for a cast that can replace
546 /// the pair, casting SrcTy to DstTy.
547 /// @brief Determine if a cast pair is eliminable
548 static unsigned isEliminableCastPair(
549 Instruction::CastOps firstOpcode, ///< Opcode of first cast
550 Instruction::CastOps secondOpcode, ///< Opcode of second cast
551 const Type *SrcTy, ///< SrcTy of 1st cast
552 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
553 const Type *DstTy, ///< DstTy of 2nd cast
554 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
557 /// @brief Return the opcode of this CastInst
558 Instruction::CastOps getOpcode() const {
559 return Instruction::CastOps(Instruction::getOpcode());
562 /// @brief Return the source type, as a convenience
563 const Type* getSrcTy() const { return getOperand(0)->getType(); }
564 /// @brief Return the destination type, as a convenience
565 const Type* getDestTy() const { return getType(); }
567 /// This method can be used to determine if a cast from S to DstTy using
568 /// Opcode op is valid or not.
569 /// @returns true iff the proposed cast is valid.
570 /// @brief Determine if a cast is valid without creating one.
571 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
573 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
574 static inline bool classof(const CastInst *) { return true; }
575 static inline bool classof(const Instruction *I) {
578 static inline bool classof(const Value *V) {
579 return isa<Instruction>(V) && classof(cast<Instruction>(V));
583 //===----------------------------------------------------------------------===//
585 //===----------------------------------------------------------------------===//
587 /// This class is the base class for the comparison instructions.
588 /// @brief Abstract base class of comparison instructions.
589 // FIXME: why not derive from BinaryOperator?
590 class CmpInst: public Instruction {
591 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
592 CmpInst(); // do not implement
594 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
595 Value *LHS, Value *RHS, const Twine &Name = "",
596 Instruction *InsertBefore = 0);
598 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
599 Value *LHS, Value *RHS, const Twine &Name,
600 BasicBlock *InsertAtEnd);
603 /// This enumeration lists the possible predicates for CmpInst subclasses.
604 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
605 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
606 /// predicate values are not overlapping between the classes.
608 // Opcode U L G E Intuitive operation
609 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
610 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
611 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
612 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
613 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
614 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
615 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
616 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
617 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
618 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
619 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
620 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
621 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
622 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
623 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
624 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
625 FIRST_FCMP_PREDICATE = FCMP_FALSE,
626 LAST_FCMP_PREDICATE = FCMP_TRUE,
627 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
628 ICMP_EQ = 32, /// equal
629 ICMP_NE = 33, /// not equal
630 ICMP_UGT = 34, /// unsigned greater than
631 ICMP_UGE = 35, /// unsigned greater or equal
632 ICMP_ULT = 36, /// unsigned less than
633 ICMP_ULE = 37, /// unsigned less or equal
634 ICMP_SGT = 38, /// signed greater than
635 ICMP_SGE = 39, /// signed greater or equal
636 ICMP_SLT = 40, /// signed less than
637 ICMP_SLE = 41, /// signed less or equal
638 FIRST_ICMP_PREDICATE = ICMP_EQ,
639 LAST_ICMP_PREDICATE = ICMP_SLE,
640 BAD_ICMP_PREDICATE = ICMP_SLE + 1
643 // allocate space for exactly two operands
644 void *operator new(size_t s) {
645 return User::operator new(s, 2);
647 /// Construct a compare instruction, given the opcode, the predicate and
648 /// the two operands. Optionally (if InstBefore is specified) insert the
649 /// instruction into a BasicBlock right before the specified instruction.
650 /// The specified Instruction is allowed to be a dereferenced end iterator.
651 /// @brief Create a CmpInst
652 static CmpInst *Create(OtherOps Op,
653 unsigned short predicate, Value *S1,
654 Value *S2, const Twine &Name = "",
655 Instruction *InsertBefore = 0);
657 /// Construct a compare instruction, given the opcode, the predicate and the
658 /// two operands. Also automatically insert this instruction to the end of
659 /// the BasicBlock specified.
660 /// @brief Create a CmpInst
661 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
662 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
664 /// @brief Get the opcode casted to the right type
665 OtherOps getOpcode() const {
666 return static_cast<OtherOps>(Instruction::getOpcode());
669 /// @brief Return the predicate for this instruction.
670 Predicate getPredicate() const { return Predicate(SubclassData); }
672 /// @brief Set the predicate for this instruction to the specified value.
673 void setPredicate(Predicate P) { SubclassData = P; }
675 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
676 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
677 /// @returns the inverse predicate for the instruction's current predicate.
678 /// @brief Return the inverse of the instruction's predicate.
679 Predicate getInversePredicate() const {
680 return getInversePredicate(getPredicate());
683 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
684 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
685 /// @returns the inverse predicate for predicate provided in \p pred.
686 /// @brief Return the inverse of a given predicate
687 static Predicate getInversePredicate(Predicate pred);
689 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
690 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
691 /// @returns the predicate that would be the result of exchanging the two
692 /// operands of the CmpInst instruction without changing the result
694 /// @brief Return the predicate as if the operands were swapped
695 Predicate getSwappedPredicate() const {
696 return getSwappedPredicate(getPredicate());
699 /// This is a static version that you can use without an instruction
701 /// @brief Return the predicate as if the operands were swapped.
702 static Predicate getSwappedPredicate(Predicate pred);
704 /// @brief Provide more efficient getOperand methods.
705 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
707 /// This is just a convenience that dispatches to the subclasses.
708 /// @brief Swap the operands and adjust predicate accordingly to retain
709 /// the same comparison.
712 /// This is just a convenience that dispatches to the subclasses.
713 /// @brief Determine if this CmpInst is commutative.
714 bool isCommutative();
716 /// This is just a convenience that dispatches to the subclasses.
717 /// @brief Determine if this is an equals/not equals predicate.
720 /// @returns true if the comparison is signed, false otherwise.
721 /// @brief Determine if this instruction is using a signed comparison.
722 bool isSigned() const {
723 return isSigned(getPredicate());
726 /// @returns true if the comparison is unsigned, false otherwise.
727 /// @brief Determine if this instruction is using an unsigned comparison.
728 bool isUnsigned() const {
729 return isUnsigned(getPredicate());
732 /// This is just a convenience.
733 /// @brief Determine if this is true when both operands are the same.
734 bool isTrueWhenEqual() const {
735 return isTrueWhenEqual(getPredicate());
738 /// This is just a convenience.
739 /// @brief Determine if this is false when both operands are the same.
740 bool isFalseWhenEqual() const {
741 return isFalseWhenEqual(getPredicate());
744 /// @returns true if the predicate is unsigned, false otherwise.
745 /// @brief Determine if the predicate is an unsigned operation.
746 static bool isUnsigned(unsigned short predicate);
748 /// @returns true if the predicate is signed, false otherwise.
749 /// @brief Determine if the predicate is an signed operation.
750 static bool isSigned(unsigned short predicate);
752 /// @brief Determine if the predicate is an ordered operation.
753 static bool isOrdered(unsigned short predicate);
755 /// @brief Determine if the predicate is an unordered operation.
756 static bool isUnordered(unsigned short predicate);
758 /// Determine if the predicate is true when comparing a value with itself.
759 static bool isTrueWhenEqual(unsigned short predicate);
761 /// Determine if the predicate is false when comparing a value with itself.
762 static bool isFalseWhenEqual(unsigned short predicate);
764 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
765 static inline bool classof(const CmpInst *) { return true; }
766 static inline bool classof(const Instruction *I) {
767 return I->getOpcode() == Instruction::ICmp ||
768 I->getOpcode() == Instruction::FCmp;
770 static inline bool classof(const Value *V) {
771 return isa<Instruction>(V) && classof(cast<Instruction>(V));
774 /// @brief Create a result type for fcmp/icmp
775 static const Type* makeCmpResultType(const Type* opnd_type) {
776 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
777 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
778 vt->getNumElements());
780 return Type::getInt1Ty(opnd_type->getContext());
785 // FIXME: these are redundant if CmpInst < BinaryOperator
787 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<2> {
790 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
792 } // End llvm namespace