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;
54 virtual TerminatorInst *clone_impl() 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
94 UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
96 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
99 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
100 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
104 // allocate space for exactly one operand
105 void *operator new(size_t s) {
106 return User::operator new(s, 1);
109 // Out of line virtual method, so the vtable, etc has a home.
112 /// Transparently provide more efficient getOperand methods.
113 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
115 // Methods for support type inquiry through isa, cast, and dyn_cast:
116 static inline bool classof(const UnaryInstruction *) { return true; }
117 static inline bool classof(const Instruction *I) {
118 return I->getOpcode() == Instruction::Alloca ||
119 I->getOpcode() == Instruction::Load ||
120 I->getOpcode() == Instruction::VAArg ||
121 I->getOpcode() == Instruction::ExtractValue ||
122 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
124 static inline bool classof(const Value *V) {
125 return isa<Instruction>(V) && classof(cast<Instruction>(V));
130 struct OperandTraits<UnaryInstruction> : public FixedNumOperandTraits<1> {
133 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
135 //===----------------------------------------------------------------------===//
136 // BinaryOperator Class
137 //===----------------------------------------------------------------------===//
139 class BinaryOperator : public Instruction {
140 void *operator new(size_t, unsigned); // Do not implement
142 void init(BinaryOps iType);
143 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
144 const Twine &Name, Instruction *InsertBefore);
145 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
146 const Twine &Name, BasicBlock *InsertAtEnd);
147 virtual BinaryOperator *clone_impl() const;
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 /// swapOperands - Exchange the two operands to this instruction.
301 /// This instruction is safe to use on any binary instruction and
302 /// does not modify the semantics of the instruction. If the instruction
303 /// cannot be reversed (ie, it's a Div), then return true.
307 /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
308 /// which must be an operator which supports this flag. See LangRef.html
309 /// for the meaning of this flag.
310 void setHasNoUnsignedWrap(bool b = true);
312 /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
313 /// which must be an operator which supports this flag. See LangRef.html
314 /// for the meaning of this flag.
315 void setHasNoSignedWrap(bool b = true);
317 /// setIsExact - Set or clear the exact flag on this instruction,
318 /// which must be an operator which supports this flag. See LangRef.html
319 /// for the meaning of this flag.
320 void setIsExact(bool b = true);
322 /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
323 bool hasNoUnsignedWrap() const;
325 /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
326 bool hasNoSignedWrap() const;
328 /// isExact - Determine whether the exact flag is set.
329 bool isExact() const;
331 // Methods for support type inquiry through isa, cast, and dyn_cast:
332 static inline bool classof(const BinaryOperator *) { return true; }
333 static inline bool classof(const Instruction *I) {
334 return I->isBinaryOp();
336 static inline bool classof(const Value *V) {
337 return isa<Instruction>(V) && classof(cast<Instruction>(V));
342 struct OperandTraits<BinaryOperator> : public FixedNumOperandTraits<2> {
345 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
347 //===----------------------------------------------------------------------===//
349 //===----------------------------------------------------------------------===//
351 /// CastInst - This is the base class for all instructions that perform data
352 /// casts. It is simply provided so that instruction category testing
353 /// can be performed with code like:
355 /// if (isa<CastInst>(Instr)) { ... }
356 /// @brief Base class of casting instructions.
357 class CastInst : public UnaryInstruction {
359 /// @brief Constructor with insert-before-instruction semantics for subclasses
360 CastInst(const Type *Ty, unsigned iType, Value *S,
361 const Twine &NameStr = "", Instruction *InsertBefore = 0)
362 : UnaryInstruction(Ty, iType, S, InsertBefore) {
365 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
366 CastInst(const Type *Ty, unsigned iType, Value *S,
367 const Twine &NameStr, BasicBlock *InsertAtEnd)
368 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
372 /// Provides a way to construct any of the CastInst subclasses using an
373 /// opcode instead of the subclass's constructor. The opcode must be in the
374 /// CastOps category (Instruction::isCast(opcode) returns true). This
375 /// constructor has insert-before-instruction semantics to automatically
376 /// insert the new CastInst before InsertBefore (if it is non-null).
377 /// @brief Construct any of the CastInst subclasses
378 static CastInst *Create(
379 Instruction::CastOps, ///< The opcode of the cast instruction
380 Value *S, ///< The value to be casted (operand 0)
381 const Type *Ty, ///< The type to which cast should be made
382 const Twine &Name = "", ///< Name for the instruction
383 Instruction *InsertBefore = 0 ///< Place to insert the instruction
385 /// Provides a way to construct any of the CastInst subclasses using an
386 /// opcode instead of the subclass's constructor. The opcode must be in the
387 /// CastOps category. This constructor has insert-at-end-of-block semantics
388 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
390 /// @brief Construct any of the CastInst subclasses
391 static CastInst *Create(
392 Instruction::CastOps, ///< The opcode for the cast instruction
393 Value *S, ///< The value to be casted (operand 0)
394 const Type *Ty, ///< The type to which operand is casted
395 const Twine &Name, ///< The name for the instruction
396 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
399 /// @brief Create a ZExt or BitCast cast instruction
400 static CastInst *CreateZExtOrBitCast(
401 Value *S, ///< The value to be casted (operand 0)
402 const Type *Ty, ///< The type to which cast should be made
403 const Twine &Name = "", ///< Name for the instruction
404 Instruction *InsertBefore = 0 ///< Place to insert the instruction
407 /// @brief Create a ZExt or BitCast cast instruction
408 static CastInst *CreateZExtOrBitCast(
409 Value *S, ///< The value to be casted (operand 0)
410 const Type *Ty, ///< The type to which operand is casted
411 const Twine &Name, ///< The name for the instruction
412 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
415 /// @brief Create a SExt or BitCast cast instruction
416 static CastInst *CreateSExtOrBitCast(
417 Value *S, ///< The value to be casted (operand 0)
418 const Type *Ty, ///< The type to which cast should be made
419 const Twine &Name = "", ///< Name for the instruction
420 Instruction *InsertBefore = 0 ///< Place to insert the instruction
423 /// @brief Create a SExt or BitCast cast instruction
424 static CastInst *CreateSExtOrBitCast(
425 Value *S, ///< The value to be casted (operand 0)
426 const Type *Ty, ///< The type to which operand is casted
427 const Twine &Name, ///< The name for the instruction
428 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
431 /// @brief Create a BitCast or a PtrToInt cast instruction
432 static CastInst *CreatePointerCast(
433 Value *S, ///< The pointer value to be casted (operand 0)
434 const Type *Ty, ///< The type to which operand is casted
435 const Twine &Name, ///< The name for the instruction
436 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
439 /// @brief Create a BitCast or a PtrToInt cast instruction
440 static CastInst *CreatePointerCast(
441 Value *S, ///< The pointer value to be casted (operand 0)
442 const Type *Ty, ///< The type to which cast should be made
443 const Twine &Name = "", ///< Name for the instruction
444 Instruction *InsertBefore = 0 ///< Place to insert the instruction
447 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
448 static CastInst *CreateIntegerCast(
449 Value *S, ///< The pointer value to be casted (operand 0)
450 const Type *Ty, ///< The type to which cast should be made
451 bool isSigned, ///< Whether to regard S as signed or not
452 const Twine &Name = "", ///< Name for the instruction
453 Instruction *InsertBefore = 0 ///< Place to insert the instruction
456 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
457 static CastInst *CreateIntegerCast(
458 Value *S, ///< The integer value to be casted (operand 0)
459 const Type *Ty, ///< The integer type to which operand is casted
460 bool isSigned, ///< Whether to regard S as signed or not
461 const Twine &Name, ///< The name for the instruction
462 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
465 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
466 static CastInst *CreateFPCast(
467 Value *S, ///< The floating point value to be casted
468 const Type *Ty, ///< The floating point type to cast to
469 const Twine &Name = "", ///< Name for the instruction
470 Instruction *InsertBefore = 0 ///< Place to insert the instruction
473 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
474 static CastInst *CreateFPCast(
475 Value *S, ///< The floating point value to be casted
476 const Type *Ty, ///< The floating point type to cast to
477 const Twine &Name, ///< The name for the instruction
478 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
481 /// @brief Create a Trunc or BitCast cast instruction
482 static CastInst *CreateTruncOrBitCast(
483 Value *S, ///< The value to be casted (operand 0)
484 const Type *Ty, ///< The type to which cast should be made
485 const Twine &Name = "", ///< Name for the instruction
486 Instruction *InsertBefore = 0 ///< Place to insert the instruction
489 /// @brief Create a Trunc or BitCast cast instruction
490 static CastInst *CreateTruncOrBitCast(
491 Value *S, ///< The value to be casted (operand 0)
492 const Type *Ty, ///< The type to which operand is casted
493 const Twine &Name, ///< The name for the instruction
494 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
497 /// @brief Check whether it is valid to call getCastOpcode for these types.
498 static bool isCastable(
499 const Type *SrcTy, ///< The Type from which the value should be cast.
500 const Type *DestTy ///< The Type to which the value should be cast.
503 /// Returns the opcode necessary to cast Val into Ty using usual casting
505 /// @brief Infer the opcode for cast operand and type
506 static Instruction::CastOps getCastOpcode(
507 const Value *Val, ///< The value to cast
508 bool SrcIsSigned, ///< Whether to treat the source as signed
509 const Type *Ty, ///< The Type to which the value should be casted
510 bool DstIsSigned ///< Whether to treate the dest. as signed
513 /// There are several places where we need to know if a cast instruction
514 /// only deals with integer source and destination types. To simplify that
515 /// logic, this method is provided.
516 /// @returns true iff the cast has only integral typed operand and dest type.
517 /// @brief Determine if this is an integer-only cast.
518 bool isIntegerCast() const;
520 /// A lossless cast is one that does not alter the basic value. It implies
521 /// a no-op cast but is more stringent, preventing things like int->float,
522 /// long->double, int->ptr, or vector->anything.
523 /// @returns true iff the cast is lossless.
524 /// @brief Determine if this is a lossless cast.
525 bool isLosslessCast() const;
527 /// A no-op cast is one that can be effected without changing any bits.
528 /// It implies that the source and destination types are the same size. The
529 /// IntPtrTy argument is used to make accurate determinations for casts
530 /// involving Integer and Pointer types. They are no-op casts if the integer
531 /// is the same size as the pointer. However, pointer size varies with
532 /// platform. Generally, the result of TargetData::getIntPtrType() should be
533 /// passed in. If that's not available, use Type::Int64Ty, which will make
534 /// the isNoopCast call conservative.
535 /// @brief Determine if this cast is a no-op cast.
537 const Type *IntPtrTy ///< Integer type corresponding to pointer
540 /// Determine how a pair of casts can be eliminated, if they can be at all.
541 /// This is a helper function for both CastInst and ConstantExpr.
542 /// @returns 0 if the CastInst pair can't be eliminated
543 /// @returns Instruction::CastOps value for a cast that can replace
544 /// the pair, casting SrcTy to DstTy.
545 /// @brief Determine if a cast pair is eliminable
546 static unsigned isEliminableCastPair(
547 Instruction::CastOps firstOpcode, ///< Opcode of first cast
548 Instruction::CastOps secondOpcode, ///< Opcode of second cast
549 const Type *SrcTy, ///< SrcTy of 1st cast
550 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
551 const Type *DstTy, ///< DstTy of 2nd cast
552 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
555 /// @brief Return the opcode of this CastInst
556 Instruction::CastOps getOpcode() const {
557 return Instruction::CastOps(Instruction::getOpcode());
560 /// @brief Return the source type, as a convenience
561 const Type* getSrcTy() const { return getOperand(0)->getType(); }
562 /// @brief Return the destination type, as a convenience
563 const Type* getDestTy() const { return getType(); }
565 /// This method can be used to determine if a cast from S to DstTy using
566 /// Opcode op is valid or not.
567 /// @returns true iff the proposed cast is valid.
568 /// @brief Determine if a cast is valid without creating one.
569 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
571 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
572 static inline bool classof(const CastInst *) { return true; }
573 static inline bool classof(const Instruction *I) {
576 static inline bool classof(const Value *V) {
577 return isa<Instruction>(V) && classof(cast<Instruction>(V));
581 //===----------------------------------------------------------------------===//
583 //===----------------------------------------------------------------------===//
585 /// This class is the base class for the comparison instructions.
586 /// @brief Abstract base class of comparison instructions.
587 // FIXME: why not derive from BinaryOperator?
588 class CmpInst: public Instruction {
589 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
590 CmpInst(); // do not implement
592 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
593 Value *LHS, Value *RHS, const Twine &Name = "",
594 Instruction *InsertBefore = 0);
596 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
597 Value *LHS, Value *RHS, const Twine &Name,
598 BasicBlock *InsertAtEnd);
601 /// This enumeration lists the possible predicates for CmpInst subclasses.
602 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
603 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
604 /// predicate values are not overlapping between the classes.
606 // Opcode U L G E Intuitive operation
607 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
608 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
609 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
610 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
611 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
612 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
613 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
614 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
615 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
616 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
617 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
618 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
619 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
620 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
621 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
622 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
623 FIRST_FCMP_PREDICATE = FCMP_FALSE,
624 LAST_FCMP_PREDICATE = FCMP_TRUE,
625 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
626 ICMP_EQ = 32, /// equal
627 ICMP_NE = 33, /// not equal
628 ICMP_UGT = 34, /// unsigned greater than
629 ICMP_UGE = 35, /// unsigned greater or equal
630 ICMP_ULT = 36, /// unsigned less than
631 ICMP_ULE = 37, /// unsigned less or equal
632 ICMP_SGT = 38, /// signed greater than
633 ICMP_SGE = 39, /// signed greater or equal
634 ICMP_SLT = 40, /// signed less than
635 ICMP_SLE = 41, /// signed less or equal
636 FIRST_ICMP_PREDICATE = ICMP_EQ,
637 LAST_ICMP_PREDICATE = ICMP_SLE,
638 BAD_ICMP_PREDICATE = ICMP_SLE + 1
641 // allocate space for exactly two operands
642 void *operator new(size_t s) {
643 return User::operator new(s, 2);
645 /// Construct a compare instruction, given the opcode, the predicate and
646 /// the two operands. Optionally (if InstBefore is specified) insert the
647 /// instruction into a BasicBlock right before the specified instruction.
648 /// The specified Instruction is allowed to be a dereferenced end iterator.
649 /// @brief Create a CmpInst
650 static CmpInst *Create(OtherOps Op,
651 unsigned short predicate, Value *S1,
652 Value *S2, const Twine &Name = "",
653 Instruction *InsertBefore = 0);
655 /// Construct a compare instruction, given the opcode, the predicate and the
656 /// two operands. Also automatically insert this instruction to the end of
657 /// the BasicBlock specified.
658 /// @brief Create a CmpInst
659 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
660 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
662 /// @brief Get the opcode casted to the right type
663 OtherOps getOpcode() const {
664 return static_cast<OtherOps>(Instruction::getOpcode());
667 /// @brief Return the predicate for this instruction.
668 Predicate getPredicate() const { return Predicate(SubclassData); }
670 /// @brief Set the predicate for this instruction to the specified value.
671 void setPredicate(Predicate P) { SubclassData = P; }
673 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
674 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
675 /// @returns the inverse predicate for the instruction's current predicate.
676 /// @brief Return the inverse of the instruction's predicate.
677 Predicate getInversePredicate() const {
678 return getInversePredicate(getPredicate());
681 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
682 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
683 /// @returns the inverse predicate for predicate provided in \p pred.
684 /// @brief Return the inverse of a given predicate
685 static Predicate getInversePredicate(Predicate pred);
687 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
688 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
689 /// @returns the predicate that would be the result of exchanging the two
690 /// operands of the CmpInst instruction without changing the result
692 /// @brief Return the predicate as if the operands were swapped
693 Predicate getSwappedPredicate() const {
694 return getSwappedPredicate(getPredicate());
697 /// This is a static version that you can use without an instruction
699 /// @brief Return the predicate as if the operands were swapped.
700 static Predicate getSwappedPredicate(Predicate pred);
702 /// @brief Provide more efficient getOperand methods.
703 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
705 /// This is just a convenience that dispatches to the subclasses.
706 /// @brief Swap the operands and adjust predicate accordingly to retain
707 /// the same comparison.
710 /// This is just a convenience that dispatches to the subclasses.
711 /// @brief Determine if this CmpInst is commutative.
712 bool isCommutative();
714 /// This is just a convenience that dispatches to the subclasses.
715 /// @brief Determine if this is an equals/not equals predicate.
718 /// @returns true if the comparison is signed, false otherwise.
719 /// @brief Determine if this instruction is using a signed comparison.
720 bool isSigned() const {
721 return isSigned(getPredicate());
724 /// @returns true if the comparison is unsigned, false otherwise.
725 /// @brief Determine if this instruction is using an unsigned comparison.
726 bool isUnsigned() const {
727 return isUnsigned(getPredicate());
730 /// This is just a convenience.
731 /// @brief Determine if this is true when both operands are the same.
732 bool isTrueWhenEqual() const {
733 return isTrueWhenEqual(getPredicate());
736 /// This is just a convenience.
737 /// @brief Determine if this is false when both operands are the same.
738 bool isFalseWhenEqual() const {
739 return isFalseWhenEqual(getPredicate());
742 /// @returns true if the predicate is unsigned, false otherwise.
743 /// @brief Determine if the predicate is an unsigned operation.
744 static bool isUnsigned(unsigned short predicate);
746 /// @returns true if the predicate is signed, false otherwise.
747 /// @brief Determine if the predicate is an signed operation.
748 static bool isSigned(unsigned short predicate);
750 /// @brief Determine if the predicate is an ordered operation.
751 static bool isOrdered(unsigned short predicate);
753 /// @brief Determine if the predicate is an unordered operation.
754 static bool isUnordered(unsigned short predicate);
756 /// Determine if the predicate is true when comparing a value with itself.
757 static bool isTrueWhenEqual(unsigned short predicate);
759 /// Determine if the predicate is false when comparing a value with itself.
760 static bool isFalseWhenEqual(unsigned short predicate);
762 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
763 static inline bool classof(const CmpInst *) { return true; }
764 static inline bool classof(const Instruction *I) {
765 return I->getOpcode() == Instruction::ICmp ||
766 I->getOpcode() == Instruction::FCmp;
768 static inline bool classof(const Value *V) {
769 return isa<Instruction>(V) && classof(cast<Instruction>(V));
772 /// @brief Create a result type for fcmp/icmp
773 static const Type* makeCmpResultType(const Type* opnd_type) {
774 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
775 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
776 vt->getNumElements());
778 return Type::getInt1Ty(opnd_type->getContext());
783 // FIXME: these are redundant if CmpInst < BinaryOperator
785 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<2> {
788 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
790 } // End llvm namespace