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::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> : public FixedNumOperandTraits<1> {
136 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
138 //===----------------------------------------------------------------------===//
139 // BinaryOperator Class
140 //===----------------------------------------------------------------------===//
142 class BinaryOperator : public Instruction {
143 void *operator new(size_t, unsigned); // Do not implement
145 void init(BinaryOps iType);
146 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
147 const Twine &Name, Instruction *InsertBefore);
148 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
149 const Twine &Name, BasicBlock *InsertAtEnd);
151 // allocate space for exactly two operands
152 void *operator new(size_t s) {
153 return User::operator new(s, 2);
156 /// Transparently provide more efficient getOperand methods.
157 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
159 /// Create() - Construct a binary instruction, given the opcode and the two
160 /// operands. Optionally (if InstBefore is specified) insert the instruction
161 /// into a BasicBlock right before the specified instruction. The specified
162 /// Instruction is allowed to be a dereferenced end iterator.
164 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
165 const Twine &Name = "",
166 Instruction *InsertBefore = 0);
168 /// Create() - Construct a binary instruction, given the opcode and the two
169 /// operands. Also automatically insert this instruction to the end of the
170 /// BasicBlock specified.
172 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
173 const Twine &Name, BasicBlock *InsertAtEnd);
175 /// Create* - These methods just forward to Create, and are useful when you
176 /// statically know what type of instruction you're going to create. These
177 /// helpers just save some typing.
178 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
179 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
180 const Twine &Name = "") {\
181 return Create(Instruction::OPC, V1, V2, Name);\
183 #include "llvm/Instruction.def"
184 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
185 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
186 const Twine &Name, BasicBlock *BB) {\
187 return Create(Instruction::OPC, V1, V2, Name, BB);\
189 #include "llvm/Instruction.def"
190 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
191 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
192 const Twine &Name, Instruction *I) {\
193 return Create(Instruction::OPC, V1, V2, Name, I);\
195 #include "llvm/Instruction.def"
198 /// CreateNSWAdd - Create an Add operator with the NSW flag set.
200 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
201 const Twine &Name = "") {
202 BinaryOperator *BO = CreateAdd(V1, V2, Name);
203 BO->setHasNoSignedWrap(true);
206 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
207 const Twine &Name, BasicBlock *BB) {
208 BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
209 BO->setHasNoSignedWrap(true);
212 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
213 const Twine &Name, Instruction *I) {
214 BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
215 BO->setHasNoSignedWrap(true);
219 /// CreateNSWSub - Create an Sub operator with the NSW flag set.
221 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
222 const Twine &Name = "") {
223 BinaryOperator *BO = CreateSub(V1, V2, Name);
224 BO->setHasNoSignedWrap(true);
227 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
228 const Twine &Name, BasicBlock *BB) {
229 BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
230 BO->setHasNoSignedWrap(true);
233 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
234 const Twine &Name, Instruction *I) {
235 BinaryOperator *BO = CreateSub(V1, V2, Name, I);
236 BO->setHasNoSignedWrap(true);
240 /// CreateExactSDiv - Create an SDiv operator with the exact flag set.
242 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
243 const Twine &Name = "") {
244 BinaryOperator *BO = CreateSDiv(V1, V2, Name);
245 BO->setIsExact(true);
248 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
249 const Twine &Name, BasicBlock *BB) {
250 BinaryOperator *BO = CreateSDiv(V1, V2, Name, BB);
251 BO->setIsExact(true);
254 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
255 const Twine &Name, Instruction *I) {
256 BinaryOperator *BO = CreateSDiv(V1, V2, Name, I);
257 BO->setIsExact(true);
261 /// Helper functions to construct and inspect unary operations (NEG and NOT)
262 /// via binary operators SUB and XOR:
264 /// CreateNeg, CreateNot - Create the NEG and NOT
265 /// instructions out of SUB and XOR instructions.
267 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
268 Instruction *InsertBefore = 0);
269 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
270 BasicBlock *InsertAtEnd);
271 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
272 Instruction *InsertBefore = 0);
273 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
274 BasicBlock *InsertAtEnd);
275 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
276 Instruction *InsertBefore = 0);
277 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
278 BasicBlock *InsertAtEnd);
280 /// isNeg, isFNeg, isNot - Check if the given Value is a
281 /// NEG, FNeg, or NOT instruction.
283 static bool isNeg(const Value *V);
284 static bool isFNeg(const Value *V);
285 static bool isNot(const Value *V);
287 /// getNegArgument, getNotArgument - Helper functions to extract the
288 /// unary argument of a NEG, FNEG or NOT operation implemented via
289 /// Sub, FSub, or Xor.
291 static const Value *getNegArgument(const Value *BinOp);
292 static Value *getNegArgument( Value *BinOp);
293 static const Value *getFNegArgument(const Value *BinOp);
294 static Value *getFNegArgument( Value *BinOp);
295 static const Value *getNotArgument(const Value *BinOp);
296 static Value *getNotArgument( Value *BinOp);
298 BinaryOps getOpcode() const {
299 return static_cast<BinaryOps>(Instruction::getOpcode());
302 virtual BinaryOperator *clone() const;
304 /// swapOperands - Exchange the two operands to this instruction.
305 /// This instruction is safe to use on any binary instruction and
306 /// does not modify the semantics of the instruction. If the instruction
307 /// cannot be reversed (ie, it's a Div), then return true.
311 /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
312 /// which must be an operator which supports this flag. See LangRef.html
313 /// for the meaning of this flag.
314 void setHasNoUnsignedWrap(bool b = true);
316 /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
317 /// which must be an operator which supports this flag. See LangRef.html
318 /// for the meaning of this flag.
319 void setHasNoSignedWrap(bool b = true);
321 /// setIsExact - Set or clear the exact flag on this instruction,
322 /// which must be an operator which supports this flag. See LangRef.html
323 /// for the meaning of this flag.
324 void setIsExact(bool b = true);
326 /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
327 bool hasNoUnsignedWrap() const;
329 /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
330 bool hasNoSignedWrap() const;
332 /// isExact - Determine whether the exact flag is set.
333 bool isExact() const;
335 // Methods for support type inquiry through isa, cast, and dyn_cast:
336 static inline bool classof(const BinaryOperator *) { return true; }
337 static inline bool classof(const Instruction *I) {
338 return I->isBinaryOp();
340 static inline bool classof(const Value *V) {
341 return isa<Instruction>(V) && classof(cast<Instruction>(V));
346 struct OperandTraits<BinaryOperator> : public FixedNumOperandTraits<2> {
349 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
351 //===----------------------------------------------------------------------===//
353 //===----------------------------------------------------------------------===//
355 /// CastInst - This is the base class for all instructions that perform data
356 /// casts. It is simply provided so that instruction category testing
357 /// can be performed with code like:
359 /// if (isa<CastInst>(Instr)) { ... }
360 /// @brief Base class of casting instructions.
361 class CastInst : public UnaryInstruction {
363 /// @brief Constructor with insert-before-instruction semantics for subclasses
364 CastInst(const Type *Ty, unsigned iType, Value *S,
365 const Twine &NameStr = "", Instruction *InsertBefore = 0)
366 : UnaryInstruction(Ty, iType, S, InsertBefore) {
369 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
370 CastInst(const Type *Ty, unsigned iType, Value *S,
371 const Twine &NameStr, BasicBlock *InsertAtEnd)
372 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
376 /// Provides a way to construct any of the CastInst subclasses using an
377 /// opcode instead of the subclass's constructor. The opcode must be in the
378 /// CastOps category (Instruction::isCast(opcode) returns true). This
379 /// constructor has insert-before-instruction semantics to automatically
380 /// insert the new CastInst before InsertBefore (if it is non-null).
381 /// @brief Construct any of the CastInst subclasses
382 static CastInst *Create(
383 Instruction::CastOps, ///< The opcode of the cast instruction
384 Value *S, ///< The value to be casted (operand 0)
385 const Type *Ty, ///< The type to which cast should be made
386 const Twine &Name = "", ///< Name for the instruction
387 Instruction *InsertBefore = 0 ///< Place to insert the instruction
389 /// Provides a way to construct any of the CastInst subclasses using an
390 /// opcode instead of the subclass's constructor. The opcode must be in the
391 /// CastOps category. This constructor has insert-at-end-of-block semantics
392 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
394 /// @brief Construct any of the CastInst subclasses
395 static CastInst *Create(
396 Instruction::CastOps, ///< The opcode for the cast instruction
397 Value *S, ///< The value to be casted (operand 0)
398 const Type *Ty, ///< The type to which operand is casted
399 const Twine &Name, ///< The name for the instruction
400 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
403 /// @brief Create a ZExt or BitCast cast instruction
404 static CastInst *CreateZExtOrBitCast(
405 Value *S, ///< The value to be casted (operand 0)
406 const Type *Ty, ///< The type to which cast should be made
407 const Twine &Name = "", ///< Name for the instruction
408 Instruction *InsertBefore = 0 ///< Place to insert the instruction
411 /// @brief Create a ZExt or BitCast cast instruction
412 static CastInst *CreateZExtOrBitCast(
413 Value *S, ///< The value to be casted (operand 0)
414 const Type *Ty, ///< The type to which operand is casted
415 const Twine &Name, ///< The name for the instruction
416 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
419 /// @brief Create a SExt or BitCast cast instruction
420 static CastInst *CreateSExtOrBitCast(
421 Value *S, ///< The value to be casted (operand 0)
422 const Type *Ty, ///< The type to which cast should be made
423 const Twine &Name = "", ///< Name for the instruction
424 Instruction *InsertBefore = 0 ///< Place to insert the instruction
427 /// @brief Create a SExt or BitCast cast instruction
428 static CastInst *CreateSExtOrBitCast(
429 Value *S, ///< The value to be casted (operand 0)
430 const Type *Ty, ///< The type to which operand is casted
431 const Twine &Name, ///< The name for the instruction
432 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
435 /// @brief Create a BitCast or a PtrToInt cast instruction
436 static CastInst *CreatePointerCast(
437 Value *S, ///< The pointer value to be casted (operand 0)
438 const Type *Ty, ///< The type to which operand is casted
439 const Twine &Name, ///< The name for the instruction
440 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
443 /// @brief Create a BitCast or a PtrToInt cast instruction
444 static CastInst *CreatePointerCast(
445 Value *S, ///< The pointer value to be casted (operand 0)
446 const Type *Ty, ///< The type to which cast should be made
447 const Twine &Name = "", ///< Name for the instruction
448 Instruction *InsertBefore = 0 ///< Place to insert the instruction
451 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
452 static CastInst *CreateIntegerCast(
453 Value *S, ///< The pointer value to be casted (operand 0)
454 const Type *Ty, ///< The type to which cast should be made
455 bool isSigned, ///< Whether to regard S as signed or not
456 const Twine &Name = "", ///< Name for the instruction
457 Instruction *InsertBefore = 0 ///< Place to insert the instruction
460 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
461 static CastInst *CreateIntegerCast(
462 Value *S, ///< The integer value to be casted (operand 0)
463 const Type *Ty, ///< The integer type to which operand is casted
464 bool isSigned, ///< Whether to regard S as signed or not
465 const Twine &Name, ///< The name for the instruction
466 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
469 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
470 static CastInst *CreateFPCast(
471 Value *S, ///< The floating point value to be casted
472 const Type *Ty, ///< The floating point type to cast to
473 const Twine &Name = "", ///< Name for the instruction
474 Instruction *InsertBefore = 0 ///< Place to insert the instruction
477 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
478 static CastInst *CreateFPCast(
479 Value *S, ///< The floating point value to be casted
480 const Type *Ty, ///< The floating point type to cast to
481 const Twine &Name, ///< The name for the instruction
482 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
485 /// @brief Create a Trunc or BitCast cast instruction
486 static CastInst *CreateTruncOrBitCast(
487 Value *S, ///< The value to be casted (operand 0)
488 const Type *Ty, ///< The type to which cast should be made
489 const Twine &Name = "", ///< Name for the instruction
490 Instruction *InsertBefore = 0 ///< Place to insert the instruction
493 /// @brief Create a Trunc or BitCast cast instruction
494 static CastInst *CreateTruncOrBitCast(
495 Value *S, ///< The value to be casted (operand 0)
496 const Type *Ty, ///< The type to which operand is casted
497 const Twine &Name, ///< The name for the instruction
498 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
501 /// @brief Check whether it is valid to call getCastOpcode for these types.
502 static bool isCastable(
503 const Type *SrcTy, ///< The Type from which the value should be cast.
504 const Type *DestTy ///< The Type to which the value should be cast.
507 /// Returns the opcode necessary to cast Val into Ty using usual casting
509 /// @brief Infer the opcode for cast operand and type
510 static Instruction::CastOps getCastOpcode(
511 const Value *Val, ///< The value to cast
512 bool SrcIsSigned, ///< Whether to treat the source as signed
513 const Type *Ty, ///< The Type to which the value should be casted
514 bool DstIsSigned ///< Whether to treate the dest. as signed
517 /// There are several places where we need to know if a cast instruction
518 /// only deals with integer source and destination types. To simplify that
519 /// logic, this method is provided.
520 /// @returns true iff the cast has only integral typed operand and dest type.
521 /// @brief Determine if this is an integer-only cast.
522 bool isIntegerCast() const;
524 /// A lossless cast is one that does not alter the basic value. It implies
525 /// a no-op cast but is more stringent, preventing things like int->float,
526 /// long->double, int->ptr, or vector->anything.
527 /// @returns true iff the cast is lossless.
528 /// @brief Determine if this is a lossless cast.
529 bool isLosslessCast() const;
531 /// A no-op cast is one that can be effected without changing any bits.
532 /// It implies that the source and destination types are the same size. The
533 /// IntPtrTy argument is used to make accurate determinations for casts
534 /// involving Integer and Pointer types. They are no-op casts if the integer
535 /// is the same size as the pointer. However, pointer size varies with
536 /// platform. Generally, the result of TargetData::getIntPtrType() should be
537 /// passed in. If that's not available, use Type::Int64Ty, which will make
538 /// the isNoopCast call conservative.
539 /// @brief Determine if this cast is a no-op cast.
541 const Type *IntPtrTy ///< Integer type corresponding to pointer
544 /// Determine how a pair of casts can be eliminated, if they can be at all.
545 /// This is a helper function for both CastInst and ConstantExpr.
546 /// @returns 0 if the CastInst pair can't be eliminated
547 /// @returns Instruction::CastOps value for a cast that can replace
548 /// the pair, casting SrcTy to DstTy.
549 /// @brief Determine if a cast pair is eliminable
550 static unsigned isEliminableCastPair(
551 Instruction::CastOps firstOpcode, ///< Opcode of first cast
552 Instruction::CastOps secondOpcode, ///< Opcode of second cast
553 const Type *SrcTy, ///< SrcTy of 1st cast
554 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
555 const Type *DstTy, ///< DstTy of 2nd cast
556 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
559 /// @brief Return the opcode of this CastInst
560 Instruction::CastOps getOpcode() const {
561 return Instruction::CastOps(Instruction::getOpcode());
564 /// @brief Return the source type, as a convenience
565 const Type* getSrcTy() const { return getOperand(0)->getType(); }
566 /// @brief Return the destination type, as a convenience
567 const Type* getDestTy() const { return getType(); }
569 /// This method can be used to determine if a cast from S to DstTy using
570 /// Opcode op is valid or not.
571 /// @returns true iff the proposed cast is valid.
572 /// @brief Determine if a cast is valid without creating one.
573 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
575 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
576 static inline bool classof(const CastInst *) { return true; }
577 static inline bool classof(const Instruction *I) {
580 static inline bool classof(const Value *V) {
581 return isa<Instruction>(V) && classof(cast<Instruction>(V));
585 //===----------------------------------------------------------------------===//
587 //===----------------------------------------------------------------------===//
589 /// This class is the base class for the comparison instructions.
590 /// @brief Abstract base class of comparison instructions.
591 // FIXME: why not derive from BinaryOperator?
592 class CmpInst: public Instruction {
593 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
594 CmpInst(); // do not implement
596 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
597 Value *LHS, Value *RHS, const Twine &Name = "",
598 Instruction *InsertBefore = 0);
600 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
601 Value *LHS, Value *RHS, const Twine &Name,
602 BasicBlock *InsertAtEnd);
605 /// This enumeration lists the possible predicates for CmpInst subclasses.
606 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
607 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
608 /// predicate values are not overlapping between the classes.
610 // Opcode U L G E Intuitive operation
611 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
612 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
613 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
614 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
615 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
616 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
617 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
618 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
619 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
620 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
621 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
622 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
623 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
624 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
625 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
626 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
627 FIRST_FCMP_PREDICATE = FCMP_FALSE,
628 LAST_FCMP_PREDICATE = FCMP_TRUE,
629 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
630 ICMP_EQ = 32, /// equal
631 ICMP_NE = 33, /// not equal
632 ICMP_UGT = 34, /// unsigned greater than
633 ICMP_UGE = 35, /// unsigned greater or equal
634 ICMP_ULT = 36, /// unsigned less than
635 ICMP_ULE = 37, /// unsigned less or equal
636 ICMP_SGT = 38, /// signed greater than
637 ICMP_SGE = 39, /// signed greater or equal
638 ICMP_SLT = 40, /// signed less than
639 ICMP_SLE = 41, /// signed less or equal
640 FIRST_ICMP_PREDICATE = ICMP_EQ,
641 LAST_ICMP_PREDICATE = ICMP_SLE,
642 BAD_ICMP_PREDICATE = ICMP_SLE + 1
645 // allocate space for exactly two operands
646 void *operator new(size_t s) {
647 return User::operator new(s, 2);
649 /// Construct a compare instruction, given the opcode, the predicate and
650 /// the two operands. Optionally (if InstBefore is specified) insert the
651 /// instruction into a BasicBlock right before the specified instruction.
652 /// The specified Instruction is allowed to be a dereferenced end iterator.
653 /// @brief Create a CmpInst
654 static CmpInst *Create(OtherOps Op,
655 unsigned short predicate, Value *S1,
656 Value *S2, const Twine &Name = "",
657 Instruction *InsertBefore = 0);
659 /// Construct a compare instruction, given the opcode, the predicate and the
660 /// two operands. Also automatically insert this instruction to the end of
661 /// the BasicBlock specified.
662 /// @brief Create a CmpInst
663 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
664 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
666 /// @brief Get the opcode casted to the right type
667 OtherOps getOpcode() const {
668 return static_cast<OtherOps>(Instruction::getOpcode());
671 /// @brief Return the predicate for this instruction.
672 Predicate getPredicate() const { return Predicate(SubclassData); }
674 /// @brief Set the predicate for this instruction to the specified value.
675 void setPredicate(Predicate P) { SubclassData = P; }
677 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
678 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
679 /// @returns the inverse predicate for the instruction's current predicate.
680 /// @brief Return the inverse of the instruction's predicate.
681 Predicate getInversePredicate() const {
682 return getInversePredicate(getPredicate());
685 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
686 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
687 /// @returns the inverse predicate for predicate provided in \p pred.
688 /// @brief Return the inverse of a given predicate
689 static Predicate getInversePredicate(Predicate pred);
691 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
692 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
693 /// @returns the predicate that would be the result of exchanging the two
694 /// operands of the CmpInst instruction without changing the result
696 /// @brief Return the predicate as if the operands were swapped
697 Predicate getSwappedPredicate() const {
698 return getSwappedPredicate(getPredicate());
701 /// This is a static version that you can use without an instruction
703 /// @brief Return the predicate as if the operands were swapped.
704 static Predicate getSwappedPredicate(Predicate pred);
706 /// @brief Provide more efficient getOperand methods.
707 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
709 /// This is just a convenience that dispatches to the subclasses.
710 /// @brief Swap the operands and adjust predicate accordingly to retain
711 /// the same comparison.
714 /// This is just a convenience that dispatches to the subclasses.
715 /// @brief Determine if this CmpInst is commutative.
716 bool isCommutative();
718 /// This is just a convenience that dispatches to the subclasses.
719 /// @brief Determine if this is an equals/not equals predicate.
722 /// @returns true if the predicate is unsigned, false otherwise.
723 /// @brief Determine if the predicate is an unsigned operation.
724 static bool isUnsigned(unsigned short predicate);
726 /// @returns true if the predicate is signed, false otherwise.
727 /// @brief Determine if the predicate is an signed operation.
728 static bool isSigned(unsigned short predicate);
730 /// @brief Determine if the predicate is an ordered operation.
731 static bool isOrdered(unsigned short predicate);
733 /// @brief Determine if the predicate is an unordered operation.
734 static bool isUnordered(unsigned short predicate);
736 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
737 static inline bool classof(const CmpInst *) { return true; }
738 static inline bool classof(const Instruction *I) {
739 return I->getOpcode() == Instruction::ICmp ||
740 I->getOpcode() == Instruction::FCmp;
742 static inline bool classof(const Value *V) {
743 return isa<Instruction>(V) && classof(cast<Instruction>(V));
746 /// @brief Create a result type for fcmp/icmp
747 static const Type* makeCmpResultType(const Type* opnd_type) {
748 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
749 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
750 vt->getNumElements());
752 return Type::getInt1Ty(opnd_type->getContext());
757 // FIXME: these are redundant if CmpInst < BinaryOperator
759 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<2> {
762 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
764 } // End llvm namespace