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"
24 //===----------------------------------------------------------------------===//
25 // TerminatorInst Class
26 //===----------------------------------------------------------------------===//
28 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
29 /// block. Thus, these are all the flow control type of operations.
31 class TerminatorInst : public Instruction {
33 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
34 Use *Ops, unsigned NumOps,
35 Instruction *InsertBefore = 0)
36 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
38 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
39 Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
40 : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
42 // Out of line virtual method, so the vtable, etc has a home.
45 /// Virtual methods - Terminators should overload these and provide inline
46 /// overrides of non-V methods.
47 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
48 virtual unsigned getNumSuccessorsV() const = 0;
49 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
52 virtual Instruction *clone() const = 0;
54 /// getNumSuccessors - Return the number of successors that this terminator
56 unsigned getNumSuccessors() const {
57 return getNumSuccessorsV();
60 /// getSuccessor - Return the specified successor.
62 BasicBlock *getSuccessor(unsigned idx) const {
63 return getSuccessorV(idx);
66 /// setSuccessor - Update the specified successor to point at the provided
68 void setSuccessor(unsigned idx, BasicBlock *B) {
69 setSuccessorV(idx, B);
72 // Methods for support type inquiry through isa, cast, and dyn_cast:
73 static inline bool classof(const TerminatorInst *) { return true; }
74 static inline bool classof(const Instruction *I) {
75 return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
77 static inline bool classof(const Value *V) {
78 return isa<Instruction>(V) && classof(cast<Instruction>(V));
83 //===----------------------------------------------------------------------===//
84 // UnaryInstruction Class
85 //===----------------------------------------------------------------------===//
87 class UnaryInstruction : public Instruction {
88 void *operator new(size_t, unsigned); // Do not implement
89 UnaryInstruction(const UnaryInstruction&); // Do not implement
92 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB = 0)
93 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
96 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
97 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
101 // allocate space for exactly one operand
102 void *operator new(size_t s) {
103 return User::operator new(s, 1);
106 // Out of line virtual method, so the vtable, etc has a home.
109 /// Transparently provide more efficient getOperand methods.
110 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
112 // Methods for support type inquiry through isa, cast, and dyn_cast:
113 static inline bool classof(const UnaryInstruction *) { return true; }
114 static inline bool classof(const Instruction *I) {
115 return I->getOpcode() == Instruction::Malloc ||
116 I->getOpcode() == Instruction::Alloca ||
117 I->getOpcode() == Instruction::Free ||
118 I->getOpcode() == Instruction::Load ||
119 I->getOpcode() == Instruction::VAArg ||
120 I->getOpcode() == Instruction::GetResult ||
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> : 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 std::string &Name, Instruction *InsertBefore);
145 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
146 const std::string &Name, BasicBlock *InsertAtEnd);
148 // allocate space for exactly two operands
149 void *operator new(size_t s) {
150 return User::operator new(s, 2);
153 /// Transparently provide more efficient getOperand methods.
154 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
156 /// Create() - Construct a binary instruction, given the opcode and the two
157 /// operands. Optionally (if InstBefore is specified) insert the instruction
158 /// into a BasicBlock right before the specified instruction. The specified
159 /// Instruction is allowed to be a dereferenced end iterator.
161 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
162 const std::string &Name = "",
163 Instruction *InsertBefore = 0);
165 /// Create() - Construct a binary instruction, given the opcode and the two
166 /// operands. Also automatically insert this instruction to the end of the
167 /// BasicBlock specified.
169 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
170 const std::string &Name,
171 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 std::string &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 std::string &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 std::string &Name, Instruction *I) {\
191 return Create(Instruction::OPC, V1, V2, Name, I);\
193 #include "llvm/Instruction.def"
196 /// Helper functions to construct and inspect unary operations (NEG and NOT)
197 /// via binary operators SUB and XOR:
199 /// CreateNeg, CreateNot - Create the NEG and NOT
200 /// instructions out of SUB and XOR instructions.
202 static BinaryOperator *CreateNeg(Value *Op, const std::string &Name = "",
203 Instruction *InsertBefore = 0);
204 static BinaryOperator *CreateNeg(Value *Op, const std::string &Name,
205 BasicBlock *InsertAtEnd);
206 static BinaryOperator *CreateNot(Value *Op, const std::string &Name = "",
207 Instruction *InsertBefore = 0);
208 static BinaryOperator *CreateNot(Value *Op, const std::string &Name,
209 BasicBlock *InsertAtEnd);
211 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
213 static bool isNeg(const Value *V);
214 static bool isNot(const Value *V);
216 /// getNegArgument, getNotArgument - Helper functions to extract the
217 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
219 static const Value *getNegArgument(const Value *BinOp);
220 static Value *getNegArgument( Value *BinOp);
221 static const Value *getNotArgument(const Value *BinOp);
222 static Value *getNotArgument( Value *BinOp);
224 BinaryOps getOpcode() const {
225 return static_cast<BinaryOps>(Instruction::getOpcode());
228 virtual BinaryOperator *clone() const;
230 /// swapOperands - Exchange the two operands to this instruction.
231 /// This instruction is safe to use on any binary instruction and
232 /// does not modify the semantics of the instruction. If the instruction
233 /// cannot be reversed (ie, it's a Div), then return true.
237 // Methods for support type inquiry through isa, cast, and dyn_cast:
238 static inline bool classof(const BinaryOperator *) { return true; }
239 static inline bool classof(const Instruction *I) {
240 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
242 static inline bool classof(const Value *V) {
243 return isa<Instruction>(V) && classof(cast<Instruction>(V));
246 /// Backward-compatible interfaces
247 /// @deprecated in 2.4, do not use, will disappear soon
248 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
249 const std::string &Name = "",
250 Instruction *InsertBefore = 0) {
251 return Create(Op, S1, S2, Name, InsertBefore);
253 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
254 const std::string &Name,
255 BasicBlock *InsertAtEnd) {
256 return Create(Op, S1, S2, Name, InsertAtEnd);
258 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
259 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
260 const std::string &Name = "") {\
261 return Create(Instruction::OPC, V1, V2, Name);\
263 #include "llvm/Instruction.def"
264 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
265 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
266 const std::string &Name, BasicBlock *BB) {\
267 return Create(Instruction::OPC, V1, V2, Name, BB);\
269 #include "llvm/Instruction.def"
270 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
271 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
272 const std::string &Name, Instruction *I) {\
273 return Create(Instruction::OPC, V1, V2, Name, I);\
275 #include "llvm/Instruction.def"
276 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
277 Instruction *InsertBefore = 0) {
278 return CreateNeg(Op, Name, InsertBefore);
280 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
281 BasicBlock *InsertAtEnd) {
282 return CreateNeg(Op, Name, InsertAtEnd);
284 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
285 Instruction *InsertBefore = 0) {
286 return CreateNot(Op, Name, InsertBefore);
288 static BinaryOperator *createNot(Value *Op, const std::string &Name,
289 BasicBlock *InsertAtEnd) {
290 return CreateNot(Op, Name, InsertAtEnd);
295 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
298 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
300 //===----------------------------------------------------------------------===//
302 //===----------------------------------------------------------------------===//
304 /// CastInst - This is the base class for all instructions that perform data
305 /// casts. It is simply provided so that instruction category testing
306 /// can be performed with code like:
308 /// if (isa<CastInst>(Instr)) { ... }
309 /// @brief Base class of casting instructions.
310 class CastInst : public UnaryInstruction {
311 /// @brief Copy constructor
312 CastInst(const CastInst &CI)
313 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
315 /// @brief Do not allow default construction
318 /// @brief Constructor with insert-before-instruction semantics for subclasses
319 CastInst(const Type *Ty, unsigned iType, Value *S,
320 const std::string &Name = "", Instruction *InsertBefore = 0)
321 : UnaryInstruction(Ty, iType, S, InsertBefore) {
324 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
325 CastInst(const Type *Ty, unsigned iType, Value *S,
326 const std::string &Name, BasicBlock *InsertAtEnd)
327 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
331 /// Provides a way to construct any of the CastInst subclasses using an
332 /// opcode instead of the subclass's constructor. The opcode must be in the
333 /// CastOps category (Instruction::isCast(opcode) returns true). This
334 /// constructor has insert-before-instruction semantics to automatically
335 /// insert the new CastInst before InsertBefore (if it is non-null).
336 /// @brief Construct any of the CastInst subclasses
337 static CastInst *Create(
338 Instruction::CastOps, ///< The opcode of the cast instruction
339 Value *S, ///< The value to be casted (operand 0)
340 const Type *Ty, ///< The type to which cast should be made
341 const std::string &Name = "", ///< Name for the instruction
342 Instruction *InsertBefore = 0 ///< Place to insert the instruction
344 /// Provides a way to construct any of the CastInst subclasses using an
345 /// opcode instead of the subclass's constructor. The opcode must be in the
346 /// CastOps category. This constructor has insert-at-end-of-block semantics
347 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
349 /// @brief Construct any of the CastInst subclasses
350 static CastInst *Create(
351 Instruction::CastOps, ///< The opcode for the cast instruction
352 Value *S, ///< The value to be casted (operand 0)
353 const Type *Ty, ///< The type to which operand is casted
354 const std::string &Name, ///< The name for the instruction
355 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
358 /// @brief Create a ZExt or BitCast cast instruction
359 static CastInst *CreateZExtOrBitCast(
360 Value *S, ///< The value to be casted (operand 0)
361 const Type *Ty, ///< The type to which cast should be made
362 const std::string &Name = "", ///< Name for the instruction
363 Instruction *InsertBefore = 0 ///< Place to insert the instruction
366 /// @brief Create a ZExt or BitCast cast instruction
367 static CastInst *CreateZExtOrBitCast(
368 Value *S, ///< The value to be casted (operand 0)
369 const Type *Ty, ///< The type to which operand is casted
370 const std::string &Name, ///< The name for the instruction
371 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
374 /// @brief Create a SExt or BitCast cast instruction
375 static CastInst *CreateSExtOrBitCast(
376 Value *S, ///< The value to be casted (operand 0)
377 const Type *Ty, ///< The type to which cast should be made
378 const std::string &Name = "", ///< Name for the instruction
379 Instruction *InsertBefore = 0 ///< Place to insert the instruction
382 /// @brief Create a SExt or BitCast cast instruction
383 static CastInst *CreateSExtOrBitCast(
384 Value *S, ///< The value to be casted (operand 0)
385 const Type *Ty, ///< The type to which operand is casted
386 const std::string &Name, ///< The name for the instruction
387 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
390 /// @brief Create a BitCast or a PtrToInt cast instruction
391 static CastInst *CreatePointerCast(
392 Value *S, ///< The pointer value to be casted (operand 0)
393 const Type *Ty, ///< The type to which operand is casted
394 const std::string &Name, ///< The name for the instruction
395 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
398 /// @brief Create a BitCast or a PtrToInt cast instruction
399 static CastInst *CreatePointerCast(
400 Value *S, ///< The pointer value to be casted (operand 0)
401 const Type *Ty, ///< The type to which cast should be made
402 const std::string &Name = "", ///< Name for the instruction
403 Instruction *InsertBefore = 0 ///< Place to insert the instruction
406 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
407 static CastInst *CreateIntegerCast(
408 Value *S, ///< The pointer value to be casted (operand 0)
409 const Type *Ty, ///< The type to which cast should be made
410 bool isSigned, ///< Whether to regard S as signed or not
411 const std::string &Name = "", ///< Name for the instruction
412 Instruction *InsertBefore = 0 ///< Place to insert the instruction
415 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
416 static CastInst *CreateIntegerCast(
417 Value *S, ///< The integer value to be casted (operand 0)
418 const Type *Ty, ///< The integer type to which operand is casted
419 bool isSigned, ///< Whether to regard S as signed or not
420 const std::string &Name, ///< The name for the instruction
421 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
424 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
425 static CastInst *CreateFPCast(
426 Value *S, ///< The floating point value to be casted
427 const Type *Ty, ///< The floating point type to cast to
428 const std::string &Name = "", ///< Name for the instruction
429 Instruction *InsertBefore = 0 ///< Place to insert the instruction
432 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
433 static CastInst *CreateFPCast(
434 Value *S, ///< The floating point value to be casted
435 const Type *Ty, ///< The floating point type to cast to
436 const std::string &Name, ///< The name for the instruction
437 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
440 /// @brief Create a Trunc or BitCast cast instruction
441 static CastInst *CreateTruncOrBitCast(
442 Value *S, ///< The value to be casted (operand 0)
443 const Type *Ty, ///< The type to which cast should be made
444 const std::string &Name = "", ///< Name for the instruction
445 Instruction *InsertBefore = 0 ///< Place to insert the instruction
448 /// @brief Create a Trunc or BitCast cast instruction
449 static CastInst *CreateTruncOrBitCast(
450 Value *S, ///< The value to be casted (operand 0)
451 const Type *Ty, ///< The type to which operand is casted
452 const std::string &Name, ///< The name for the instruction
453 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
456 /// @brief Check whether it is valid to call getCastOpcode for these types.
457 static bool isCastable(
458 const Type *SrcTy, ///< The Type from which the value should be cast.
459 const Type *DestTy ///< The Type to which the value should be cast.
462 /// Returns the opcode necessary to cast Val into Ty using usual casting
464 /// @brief Infer the opcode for cast operand and type
465 static Instruction::CastOps getCastOpcode(
466 const Value *Val, ///< The value to cast
467 bool SrcIsSigned, ///< Whether to treat the source as signed
468 const Type *Ty, ///< The Type to which the value should be casted
469 bool DstIsSigned ///< Whether to treate the dest. as signed
472 /// There are several places where we need to know if a cast instruction
473 /// only deals with integer source and destination types. To simplify that
474 /// logic, this method is provided.
475 /// @returns true iff the cast has only integral typed operand and dest type.
476 /// @brief Determine if this is an integer-only cast.
477 bool isIntegerCast() const;
479 /// A lossless cast is one that does not alter the basic value. It implies
480 /// a no-op cast but is more stringent, preventing things like int->float,
481 /// long->double, int->ptr, or vector->anything.
482 /// @returns true iff the cast is lossless.
483 /// @brief Determine if this is a lossless cast.
484 bool isLosslessCast() const;
486 /// A no-op cast is one that can be effected without changing any bits.
487 /// It implies that the source and destination types are the same size. The
488 /// IntPtrTy argument is used to make accurate determinations for casts
489 /// involving Integer and Pointer types. They are no-op casts if the integer
490 /// is the same size as the pointer. However, pointer size varies with
491 /// platform. Generally, the result of TargetData::getIntPtrType() should be
492 /// passed in. If that's not available, use Type::Int64Ty, which will make
493 /// the isNoopCast call conservative.
494 /// @brief Determine if this cast is a no-op cast.
496 const Type *IntPtrTy ///< Integer type corresponding to pointer
499 /// Determine how a pair of casts can be eliminated, if they can be at all.
500 /// This is a helper function for both CastInst and ConstantExpr.
501 /// @returns 0 if the CastInst pair can't be eliminated
502 /// @returns Instruction::CastOps value for a cast that can replace
503 /// the pair, casting SrcTy to DstTy.
504 /// @brief Determine if a cast pair is eliminable
505 static unsigned isEliminableCastPair(
506 Instruction::CastOps firstOpcode, ///< Opcode of first cast
507 Instruction::CastOps secondOpcode, ///< Opcode of second cast
508 const Type *SrcTy, ///< SrcTy of 1st cast
509 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
510 const Type *DstTy, ///< DstTy of 2nd cast
511 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
514 /// @brief Return the opcode of this CastInst
515 Instruction::CastOps getOpcode() const {
516 return Instruction::CastOps(Instruction::getOpcode());
519 /// @brief Return the source type, as a convenience
520 const Type* getSrcTy() const { return getOperand(0)->getType(); }
521 /// @brief Return the destination type, as a convenience
522 const Type* getDestTy() const { return getType(); }
524 /// This method can be used to determine if a cast from S to DstTy using
525 /// Opcode op is valid or not.
526 /// @returns true iff the proposed cast is valid.
527 /// @brief Determine if a cast is valid without creating one.
528 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
530 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
531 static inline bool classof(const CastInst *) { return true; }
532 static inline bool classof(const Instruction *I) {
533 return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
535 static inline bool classof(const Value *V) {
536 return isa<Instruction>(V) && classof(cast<Instruction>(V));
538 /// Backward-compatible interfaces
539 /// @deprecated in 2.4, do not use, will disappear soon
540 static CastInst *create(Instruction::CastOps Op,Value *S,const Type *Ty,
541 const std::string &Name = "",Instruction *InsertBefore = 0) {
542 return Create(Op,S,Ty,Name,InsertBefore);
544 static CastInst *create(Instruction::CastOps Op,Value *S,const Type *Ty,
545 const std::string &Name,BasicBlock *InsertAtEnd) {
546 return Create(Op,S,Ty,Name,InsertAtEnd);
549 #define DEFINE_CASTINST_DEPRECATED(OP) \
550 static CastInst *create ## OP ## Cast(Value *S, const Type *Ty, \
551 const std::string &Name = "", Instruction *InsertBefore = 0) { \
552 return Create ## OP ## Cast(S, Ty, Name, InsertBefore); \
554 static CastInst *create ## OP ## Cast(Value *S, const Type *Ty, \
555 const std::string &Name, BasicBlock *InsertAtEnd) { \
556 return Create ## OP ## Cast(S, Ty, Name, InsertAtEnd); \
558 DEFINE_CASTINST_DEPRECATED(ZExtOrBit)
559 DEFINE_CASTINST_DEPRECATED(SExtOrBit)
560 DEFINE_CASTINST_DEPRECATED(Pointer)
561 DEFINE_CASTINST_DEPRECATED(FP)
562 DEFINE_CASTINST_DEPRECATED(TruncOrBit)
563 #undef DEFINE_CASTINST_DEPRECATED
564 static CastInst *createIntegerCast(Value *S, const Type *Ty, bool isSigned,
565 const std::string &Name = "", Instruction *InsertBefore = 0) {
566 return CreateIntegerCast(S, Ty, isSigned, Name, InsertBefore);
568 static CastInst *createIntegerCast(Value *S, const Type *Ty, bool isSigned,
569 const std::string &Name, BasicBlock *InsertAtEnd) {
570 return CreateIntegerCast(S, Ty, isSigned, Name, InsertAtEnd);
574 //===----------------------------------------------------------------------===//
576 //===----------------------------------------------------------------------===//
578 /// This class is the base class for the comparison instructions.
579 /// @brief Abstract base class of comparison instructions.
580 // FIXME: why not derive from BinaryOperator?
581 class CmpInst: public Instruction {
582 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
583 CmpInst(); // do not implement
585 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
586 Value *LHS, Value *RHS, const std::string &Name = "",
587 Instruction *InsertBefore = 0);
589 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
590 Value *LHS, Value *RHS, const std::string &Name,
591 BasicBlock *InsertAtEnd);
594 /// This enumeration lists the possible predicates for CmpInst subclasses.
595 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
596 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
597 /// predicate values are not overlapping between the classes.
599 // Opcode U L G E Intuitive operation
600 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
601 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
602 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
603 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
604 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
605 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
606 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
607 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
608 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
609 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
610 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
611 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
612 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
613 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
614 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
615 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
616 FIRST_FCMP_PREDICATE = FCMP_FALSE,
617 LAST_FCMP_PREDICATE = FCMP_TRUE,
618 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
619 ICMP_EQ = 32, /// equal
620 ICMP_NE = 33, /// not equal
621 ICMP_UGT = 34, /// unsigned greater than
622 ICMP_UGE = 35, /// unsigned greater or equal
623 ICMP_ULT = 36, /// unsigned less than
624 ICMP_ULE = 37, /// unsigned less or equal
625 ICMP_SGT = 38, /// signed greater than
626 ICMP_SGE = 39, /// signed greater or equal
627 ICMP_SLT = 40, /// signed less than
628 ICMP_SLE = 41, /// signed less or equal
629 FIRST_ICMP_PREDICATE = ICMP_EQ,
630 LAST_ICMP_PREDICATE = ICMP_SLE,
631 BAD_ICMP_PREDICATE = ICMP_SLE + 1
634 // allocate space for exactly two operands
635 void *operator new(size_t s) {
636 return User::operator new(s, 2);
638 /// Construct a compare instruction, given the opcode, the predicate and
639 /// the two operands. Optionally (if InstBefore is specified) insert the
640 /// instruction into a BasicBlock right before the specified instruction.
641 /// The specified Instruction is allowed to be a dereferenced end iterator.
642 /// @brief Create a CmpInst
643 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
644 Value *S2, const std::string &Name = "",
645 Instruction *InsertBefore = 0);
647 /// Construct a compare instruction, given the opcode, the predicate and the
648 /// two operands. Also automatically insert this instruction to the end of
649 /// the BasicBlock specified.
650 /// @brief Create a CmpInst
651 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
652 Value *S2, const std::string &Name,
653 BasicBlock *InsertAtEnd);
655 /// @brief Get the opcode casted to the right type
656 OtherOps getOpcode() const {
657 return static_cast<OtherOps>(Instruction::getOpcode());
660 /// @brief Return the predicate for this instruction.
661 Predicate getPredicate() const { return Predicate(SubclassData); }
663 /// @brief Set the predicate for this instruction to the specified value.
664 void setPredicate(Predicate P) { SubclassData = P; }
666 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
667 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
668 /// @returns the inverse predicate for the instruction's current predicate.
669 /// @brief Return the inverse of the instruction's predicate.
670 Predicate getInversePredicate() const {
671 return getInversePredicate(getPredicate());
674 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
675 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
676 /// @returns the inverse predicate for predicate provided in \p pred.
677 /// @brief Return the inverse of a given predicate
678 static Predicate getInversePredicate(Predicate pred);
680 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
681 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
682 /// @returns the predicate that would be the result of exchanging the two
683 /// operands of the CmpInst instruction without changing the result
685 /// @brief Return the predicate as if the operands were swapped
686 Predicate getSwappedPredicate() const {
687 return getSwappedPredicate(getPredicate());
690 /// This is a static version that you can use without an instruction
692 /// @brief Return the predicate as if the operands were swapped.
693 static Predicate getSwappedPredicate(Predicate pred);
695 /// @brief Provide more efficient getOperand methods.
696 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
698 /// This is just a convenience that dispatches to the subclasses.
699 /// @brief Swap the operands and adjust predicate accordingly to retain
700 /// the same comparison.
703 /// This is just a convenience that dispatches to the subclasses.
704 /// @brief Determine if this CmpInst is commutative.
705 bool isCommutative();
707 /// This is just a convenience that dispatches to the subclasses.
708 /// @brief Determine if this is an equals/not equals predicate.
711 /// @returns true if the predicate is unsigned, false otherwise.
712 /// @brief Determine if the predicate is an unsigned operation.
713 static bool isUnsigned(unsigned short predicate);
715 /// @returns true if the predicate is signed, false otherwise.
716 /// @brief Determine if the predicate is an signed operation.
717 static bool isSigned(unsigned short predicate);
719 /// @brief Determine if the predicate is an ordered operation.
720 static bool isOrdered(unsigned short predicate);
722 /// @brief Determine if the predicate is an unordered operation.
723 static bool isUnordered(unsigned short predicate);
725 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
726 static inline bool classof(const CmpInst *) { return true; }
727 static inline bool classof(const Instruction *I) {
728 return I->getOpcode() == Instruction::ICmp ||
729 I->getOpcode() == Instruction::FCmp ||
730 I->getOpcode() == Instruction::VICmp ||
731 I->getOpcode() == Instruction::VFCmp;
733 static inline bool classof(const Value *V) {
734 return isa<Instruction>(V) && classof(cast<Instruction>(V));
736 /// Backward-compatible interfaces
737 /// @deprecated in 2.4, do not use, will disappear soon
738 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
739 Value *S2, const std::string &Name = "",
740 Instruction *InsertBefore = 0) {
741 return Create(Op, predicate, S1, S2, Name, InsertBefore);
743 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
744 Value *S2, const std::string &Name,
745 BasicBlock *InsertAtEnd) {
746 return Create(Op, predicate, S1, S2, Name, InsertAtEnd);
751 // FIXME: these are redundant if CmpInst < BinaryOperator
753 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
756 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
758 } // End llvm namespace