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"
23 #include "llvm/ADT/Twine.h"
29 //===----------------------------------------------------------------------===//
30 // TerminatorInst Class
31 //===----------------------------------------------------------------------===//
33 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
34 /// block. Thus, these are all the flow control type of operations.
36 class TerminatorInst : public Instruction {
38 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
39 Use *Ops, unsigned NumOps,
40 Instruction *InsertBefore = 0)
41 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
43 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
44 Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
45 : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
47 // Out of line virtual method, so the vtable, etc has a home.
50 /// Virtual methods - Terminators should overload these and provide inline
51 /// overrides of non-V methods.
52 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
53 virtual unsigned getNumSuccessorsV() const = 0;
54 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
55 virtual TerminatorInst *clone_impl() const = 0;
58 /// getNumSuccessors - Return the number of successors that this terminator
60 unsigned getNumSuccessors() const {
61 return getNumSuccessorsV();
64 /// getSuccessor - Return the specified successor.
66 BasicBlock *getSuccessor(unsigned idx) const {
67 return getSuccessorV(idx);
70 /// setSuccessor - Update the specified successor to point at the provided
72 void setSuccessor(unsigned idx, BasicBlock *B) {
73 setSuccessorV(idx, B);
76 // Methods for support type inquiry through isa, cast, and dyn_cast:
77 static inline bool classof(const TerminatorInst *) { return true; }
78 static inline bool classof(const Instruction *I) {
79 return I->isTerminator();
81 static inline bool classof(const Value *V) {
82 return isa<Instruction>(V) && classof(cast<Instruction>(V));
87 //===----------------------------------------------------------------------===//
88 // UnaryInstruction Class
89 //===----------------------------------------------------------------------===//
91 class UnaryInstruction : public Instruction {
92 void *operator new(size_t, unsigned); // Do not implement
95 UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
97 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
100 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
101 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
105 // allocate space for exactly one operand
106 void *operator new(size_t s) {
107 return User::operator new(s, 1);
110 // Out of line virtual method, so the vtable, etc has a home.
113 /// Transparently provide more efficient getOperand methods.
114 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
116 // Methods for support type inquiry through isa, cast, and dyn_cast:
117 static inline bool classof(const UnaryInstruction *) { return true; }
118 static inline bool classof(const Instruction *I) {
119 return I->getOpcode() == Instruction::Alloca ||
120 I->getOpcode() == Instruction::Load ||
121 I->getOpcode() == Instruction::VAArg ||
122 I->getOpcode() == Instruction::ExtractValue ||
123 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
125 static inline bool classof(const Value *V) {
126 return isa<Instruction>(V) && classof(cast<Instruction>(V));
131 struct OperandTraits<UnaryInstruction> : public FixedNumOperandTraits<1> {
134 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
136 //===----------------------------------------------------------------------===//
137 // BinaryOperator Class
138 //===----------------------------------------------------------------------===//
140 class BinaryOperator : public Instruction {
141 void *operator new(size_t, unsigned); // Do not implement
143 void init(BinaryOps iType);
144 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
145 const Twine &Name, Instruction *InsertBefore);
146 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
147 const Twine &Name, BasicBlock *InsertAtEnd);
148 virtual BinaryOperator *clone_impl() const;
150 // allocate space for exactly two operands
151 void *operator new(size_t s) {
152 return User::operator new(s, 2);
155 /// Transparently provide more efficient getOperand methods.
156 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
158 /// Create() - Construct a binary instruction, given the opcode and the two
159 /// operands. Optionally (if InstBefore is specified) insert the instruction
160 /// into a BasicBlock right before the specified instruction. The specified
161 /// Instruction is allowed to be a dereferenced end iterator.
163 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
164 const Twine &Name = Twine(),
165 Instruction *InsertBefore = 0);
167 /// Create() - Construct a binary instruction, given the opcode and the two
168 /// operands. Also automatically insert this instruction to the end of the
169 /// BasicBlock specified.
171 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
172 const Twine &Name, BasicBlock *InsertAtEnd);
174 /// Create* - These methods just forward to Create, and are useful when you
175 /// statically know what type of instruction you're going to create. These
176 /// helpers just save some typing.
177 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
178 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
179 const Twine &Name = "") {\
180 return Create(Instruction::OPC, V1, V2, Name);\
182 #include "llvm/Instruction.def"
183 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
184 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
185 const Twine &Name, BasicBlock *BB) {\
186 return Create(Instruction::OPC, V1, V2, Name, BB);\
188 #include "llvm/Instruction.def"
189 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
190 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
191 const Twine &Name, Instruction *I) {\
192 return Create(Instruction::OPC, V1, V2, Name, I);\
194 #include "llvm/Instruction.def"
197 /// CreateNSWAdd - Create an Add operator with the NSW flag set.
199 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
200 const Twine &Name = "") {
201 BinaryOperator *BO = CreateAdd(V1, V2, Name);
202 BO->setHasNoSignedWrap(true);
205 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
206 const Twine &Name, BasicBlock *BB) {
207 BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
208 BO->setHasNoSignedWrap(true);
211 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
212 const Twine &Name, Instruction *I) {
213 BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
214 BO->setHasNoSignedWrap(true);
218 /// CreateNUWAdd - Create an Add operator with the NUW flag set.
220 static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
221 const Twine &Name = "") {
222 BinaryOperator *BO = CreateAdd(V1, V2, Name);
223 BO->setHasNoUnsignedWrap(true);
226 static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
227 const Twine &Name, BasicBlock *BB) {
228 BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
229 BO->setHasNoUnsignedWrap(true);
232 static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
233 const Twine &Name, Instruction *I) {
234 BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
235 BO->setHasNoUnsignedWrap(true);
239 /// CreateNSWSub - Create an Sub operator with the NSW flag set.
241 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
242 const Twine &Name = "") {
243 BinaryOperator *BO = CreateSub(V1, V2, Name);
244 BO->setHasNoSignedWrap(true);
247 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
248 const Twine &Name, BasicBlock *BB) {
249 BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
250 BO->setHasNoSignedWrap(true);
253 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
254 const Twine &Name, Instruction *I) {
255 BinaryOperator *BO = CreateSub(V1, V2, Name, I);
256 BO->setHasNoSignedWrap(true);
260 /// CreateNUWSub - Create an Sub operator with the NUW flag set.
262 static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
263 const Twine &Name = "") {
264 BinaryOperator *BO = CreateSub(V1, V2, Name);
265 BO->setHasNoUnsignedWrap(true);
268 static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
269 const Twine &Name, BasicBlock *BB) {
270 BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
271 BO->setHasNoUnsignedWrap(true);
274 static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
275 const Twine &Name, Instruction *I) {
276 BinaryOperator *BO = CreateSub(V1, V2, Name, I);
277 BO->setHasNoUnsignedWrap(true);
281 /// CreateNSWMul - Create a Mul operator with the NSW flag set.
283 static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
284 const Twine &Name = "") {
285 BinaryOperator *BO = CreateMul(V1, V2, Name);
286 BO->setHasNoSignedWrap(true);
289 static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
290 const Twine &Name, BasicBlock *BB) {
291 BinaryOperator *BO = CreateMul(V1, V2, Name, BB);
292 BO->setHasNoSignedWrap(true);
295 static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
296 const Twine &Name, Instruction *I) {
297 BinaryOperator *BO = CreateMul(V1, V2, Name, I);
298 BO->setHasNoSignedWrap(true);
302 /// CreateNUWMul - Create a Mul operator with the NUW flag set.
304 static BinaryOperator *CreateNUWMul(Value *V1, Value *V2,
305 const Twine &Name = "") {
306 BinaryOperator *BO = CreateMul(V1, V2, Name);
307 BO->setHasNoUnsignedWrap(true);
310 static BinaryOperator *CreateNUWMul(Value *V1, Value *V2,
311 const Twine &Name, BasicBlock *BB) {
312 BinaryOperator *BO = CreateMul(V1, V2, Name, BB);
313 BO->setHasNoUnsignedWrap(true);
316 static BinaryOperator *CreateNUWMul(Value *V1, Value *V2,
317 const Twine &Name, Instruction *I) {
318 BinaryOperator *BO = CreateMul(V1, V2, Name, I);
319 BO->setHasNoUnsignedWrap(true);
323 /// CreateExactSDiv - Create an SDiv operator with the exact flag set.
325 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
326 const Twine &Name = "") {
327 BinaryOperator *BO = CreateSDiv(V1, V2, Name);
328 BO->setIsExact(true);
331 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
332 const Twine &Name, BasicBlock *BB) {
333 BinaryOperator *BO = CreateSDiv(V1, V2, Name, BB);
334 BO->setIsExact(true);
337 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
338 const Twine &Name, Instruction *I) {
339 BinaryOperator *BO = CreateSDiv(V1, V2, Name, I);
340 BO->setIsExact(true);
344 /// Helper functions to construct and inspect unary operations (NEG and NOT)
345 /// via binary operators SUB and XOR:
347 /// CreateNeg, CreateNot - Create the NEG and NOT
348 /// instructions out of SUB and XOR instructions.
350 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
351 Instruction *InsertBefore = 0);
352 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
353 BasicBlock *InsertAtEnd);
354 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
355 Instruction *InsertBefore = 0);
356 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
357 BasicBlock *InsertAtEnd);
358 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
359 Instruction *InsertBefore = 0);
360 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
361 BasicBlock *InsertAtEnd);
362 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
363 Instruction *InsertBefore = 0);
364 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
365 BasicBlock *InsertAtEnd);
366 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
367 Instruction *InsertBefore = 0);
368 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
369 BasicBlock *InsertAtEnd);
371 /// isNeg, isFNeg, isNot - Check if the given Value is a
372 /// NEG, FNeg, or NOT instruction.
374 static bool isNeg(const Value *V);
375 static bool isFNeg(const Value *V);
376 static bool isNot(const Value *V);
378 /// getNegArgument, getNotArgument - Helper functions to extract the
379 /// unary argument of a NEG, FNEG or NOT operation implemented via
380 /// Sub, FSub, or Xor.
382 static const Value *getNegArgument(const Value *BinOp);
383 static Value *getNegArgument( Value *BinOp);
384 static const Value *getFNegArgument(const Value *BinOp);
385 static Value *getFNegArgument( Value *BinOp);
386 static const Value *getNotArgument(const Value *BinOp);
387 static Value *getNotArgument( Value *BinOp);
389 BinaryOps getOpcode() const {
390 return static_cast<BinaryOps>(Instruction::getOpcode());
393 /// swapOperands - Exchange the two operands to this instruction.
394 /// This instruction is safe to use on any binary instruction and
395 /// does not modify the semantics of the instruction. If the instruction
396 /// cannot be reversed (ie, it's a Div), then return true.
400 /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
401 /// which must be an operator which supports this flag. See LangRef.html
402 /// for the meaning of this flag.
403 void setHasNoUnsignedWrap(bool b = true);
405 /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
406 /// which must be an operator which supports this flag. See LangRef.html
407 /// for the meaning of this flag.
408 void setHasNoSignedWrap(bool b = true);
410 /// setIsExact - Set or clear the exact flag on this instruction,
411 /// which must be an operator which supports this flag. See LangRef.html
412 /// for the meaning of this flag.
413 void setIsExact(bool b = true);
415 /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
416 bool hasNoUnsignedWrap() const;
418 /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
419 bool hasNoSignedWrap() const;
421 /// isExact - Determine whether the exact flag is set.
422 bool isExact() const;
424 // Methods for support type inquiry through isa, cast, and dyn_cast:
425 static inline bool classof(const BinaryOperator *) { return true; }
426 static inline bool classof(const Instruction *I) {
427 return I->isBinaryOp();
429 static inline bool classof(const Value *V) {
430 return isa<Instruction>(V) && classof(cast<Instruction>(V));
435 struct OperandTraits<BinaryOperator> : public FixedNumOperandTraits<2> {
438 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
440 //===----------------------------------------------------------------------===//
442 //===----------------------------------------------------------------------===//
444 /// CastInst - This is the base class for all instructions that perform data
445 /// casts. It is simply provided so that instruction category testing
446 /// can be performed with code like:
448 /// if (isa<CastInst>(Instr)) { ... }
449 /// @brief Base class of casting instructions.
450 class CastInst : public UnaryInstruction {
452 /// @brief Constructor with insert-before-instruction semantics for subclasses
453 CastInst(const Type *Ty, unsigned iType, Value *S,
454 const Twine &NameStr = "", Instruction *InsertBefore = 0)
455 : UnaryInstruction(Ty, iType, S, InsertBefore) {
458 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
459 CastInst(const Type *Ty, unsigned iType, Value *S,
460 const Twine &NameStr, BasicBlock *InsertAtEnd)
461 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
465 /// Provides a way to construct any of the CastInst subclasses using an
466 /// opcode instead of the subclass's constructor. The opcode must be in the
467 /// CastOps category (Instruction::isCast(opcode) returns true). This
468 /// constructor has insert-before-instruction semantics to automatically
469 /// insert the new CastInst before InsertBefore (if it is non-null).
470 /// @brief Construct any of the CastInst subclasses
471 static CastInst *Create(
472 Instruction::CastOps, ///< The opcode of the cast instruction
473 Value *S, ///< The value to be casted (operand 0)
474 const Type *Ty, ///< The type to which cast should be made
475 const Twine &Name = "", ///< Name for the instruction
476 Instruction *InsertBefore = 0 ///< Place to insert the instruction
478 /// Provides a way to construct any of the CastInst subclasses using an
479 /// opcode instead of the subclass's constructor. The opcode must be in the
480 /// CastOps category. This constructor has insert-at-end-of-block semantics
481 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
483 /// @brief Construct any of the CastInst subclasses
484 static CastInst *Create(
485 Instruction::CastOps, ///< The opcode for the cast instruction
486 Value *S, ///< The value to be casted (operand 0)
487 const Type *Ty, ///< The type to which operand is casted
488 const Twine &Name, ///< The name for the instruction
489 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
492 /// @brief Create a ZExt or BitCast cast instruction
493 static CastInst *CreateZExtOrBitCast(
494 Value *S, ///< The value to be casted (operand 0)
495 const Type *Ty, ///< The type to which cast should be made
496 const Twine &Name = "", ///< Name for the instruction
497 Instruction *InsertBefore = 0 ///< Place to insert the instruction
500 /// @brief Create a ZExt or BitCast cast instruction
501 static CastInst *CreateZExtOrBitCast(
502 Value *S, ///< The value to be casted (operand 0)
503 const Type *Ty, ///< The type to which operand is casted
504 const Twine &Name, ///< The name for the instruction
505 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
508 /// @brief Create a SExt or BitCast cast instruction
509 static CastInst *CreateSExtOrBitCast(
510 Value *S, ///< The value to be casted (operand 0)
511 const Type *Ty, ///< The type to which cast should be made
512 const Twine &Name = "", ///< Name for the instruction
513 Instruction *InsertBefore = 0 ///< Place to insert the instruction
516 /// @brief Create a SExt or BitCast cast instruction
517 static CastInst *CreateSExtOrBitCast(
518 Value *S, ///< The value to be casted (operand 0)
519 const Type *Ty, ///< The type to which operand is casted
520 const Twine &Name, ///< The name for the instruction
521 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
524 /// @brief Create a BitCast or a PtrToInt cast instruction
525 static CastInst *CreatePointerCast(
526 Value *S, ///< The pointer value to be casted (operand 0)
527 const Type *Ty, ///< The type to which operand is casted
528 const Twine &Name, ///< The name for the instruction
529 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
532 /// @brief Create a BitCast or a PtrToInt cast instruction
533 static CastInst *CreatePointerCast(
534 Value *S, ///< The pointer value to be casted (operand 0)
535 const Type *Ty, ///< The type to which cast should be made
536 const Twine &Name = "", ///< Name for the instruction
537 Instruction *InsertBefore = 0 ///< Place to insert the instruction
540 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
541 static CastInst *CreateIntegerCast(
542 Value *S, ///< The pointer value to be casted (operand 0)
543 const Type *Ty, ///< The type to which cast should be made
544 bool isSigned, ///< Whether to regard S as signed or not
545 const Twine &Name = "", ///< Name for the instruction
546 Instruction *InsertBefore = 0 ///< Place to insert the instruction
549 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
550 static CastInst *CreateIntegerCast(
551 Value *S, ///< The integer value to be casted (operand 0)
552 const Type *Ty, ///< The integer type to which operand is casted
553 bool isSigned, ///< Whether to regard S as signed or not
554 const Twine &Name, ///< The name for the instruction
555 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
558 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
559 static CastInst *CreateFPCast(
560 Value *S, ///< The floating point value to be casted
561 const Type *Ty, ///< The floating point type to cast to
562 const Twine &Name = "", ///< Name for the instruction
563 Instruction *InsertBefore = 0 ///< Place to insert the instruction
566 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
567 static CastInst *CreateFPCast(
568 Value *S, ///< The floating point value to be casted
569 const Type *Ty, ///< The floating point type to cast to
570 const Twine &Name, ///< The name for the instruction
571 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
574 /// @brief Create a Trunc or BitCast cast instruction
575 static CastInst *CreateTruncOrBitCast(
576 Value *S, ///< The value to be casted (operand 0)
577 const Type *Ty, ///< The type to which cast should be made
578 const Twine &Name = "", ///< Name for the instruction
579 Instruction *InsertBefore = 0 ///< Place to insert the instruction
582 /// @brief Create a Trunc or BitCast cast instruction
583 static CastInst *CreateTruncOrBitCast(
584 Value *S, ///< The value to be casted (operand 0)
585 const Type *Ty, ///< The type to which operand is casted
586 const Twine &Name, ///< The name for the instruction
587 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
590 /// @brief Check whether it is valid to call getCastOpcode for these types.
591 static bool isCastable(
592 const Type *SrcTy, ///< The Type from which the value should be cast.
593 const Type *DestTy ///< The Type to which the value should be cast.
596 /// Returns the opcode necessary to cast Val into Ty using usual casting
598 /// @brief Infer the opcode for cast operand and type
599 static Instruction::CastOps getCastOpcode(
600 const Value *Val, ///< The value to cast
601 bool SrcIsSigned, ///< Whether to treat the source as signed
602 const Type *Ty, ///< The Type to which the value should be casted
603 bool DstIsSigned ///< Whether to treate the dest. as signed
606 /// There are several places where we need to know if a cast instruction
607 /// only deals with integer source and destination types. To simplify that
608 /// logic, this method is provided.
609 /// @returns true iff the cast has only integral typed operand and dest type.
610 /// @brief Determine if this is an integer-only cast.
611 bool isIntegerCast() const;
613 /// A lossless cast is one that does not alter the basic value. It implies
614 /// a no-op cast but is more stringent, preventing things like int->float,
615 /// long->double, or int->ptr.
616 /// @returns true iff the cast is lossless.
617 /// @brief Determine if this is a lossless cast.
618 bool isLosslessCast() const;
620 /// A no-op cast is one that can be effected without changing any bits.
621 /// It implies that the source and destination types are the same size. The
622 /// IntPtrTy argument is used to make accurate determinations for casts
623 /// involving Integer and Pointer types. They are no-op casts if the integer
624 /// is the same size as the pointer. However, pointer size varies with
625 /// platform. Generally, the result of TargetData::getIntPtrType() should be
626 /// passed in. If that's not available, use Type::Int64Ty, which will make
627 /// the isNoopCast call conservative.
628 /// @brief Determine if the described cast is a no-op cast.
629 static bool isNoopCast(
630 Instruction::CastOps Opcode, ///< Opcode of cast
631 const Type *SrcTy, ///< SrcTy of cast
632 const Type *DstTy, ///< DstTy of cast
633 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
636 /// @brief Determine if this cast is a no-op cast.
638 const Type *IntPtrTy ///< Integer type corresponding to pointer
641 /// Determine how a pair of casts can be eliminated, if they can be at all.
642 /// This is a helper function for both CastInst and ConstantExpr.
643 /// @returns 0 if the CastInst pair can't be eliminated
644 /// @returns Instruction::CastOps value for a cast that can replace
645 /// the pair, casting SrcTy to DstTy.
646 /// @brief Determine if a cast pair is eliminable
647 static unsigned isEliminableCastPair(
648 Instruction::CastOps firstOpcode, ///< Opcode of first cast
649 Instruction::CastOps secondOpcode, ///< Opcode of second cast
650 const Type *SrcTy, ///< SrcTy of 1st cast
651 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
652 const Type *DstTy, ///< DstTy of 2nd cast
653 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
656 /// @brief Return the opcode of this CastInst
657 Instruction::CastOps getOpcode() const {
658 return Instruction::CastOps(Instruction::getOpcode());
661 /// @brief Return the source type, as a convenience
662 const Type* getSrcTy() const { return getOperand(0)->getType(); }
663 /// @brief Return the destination type, as a convenience
664 const Type* getDestTy() const { return getType(); }
666 /// This method can be used to determine if a cast from S to DstTy using
667 /// Opcode op is valid or not.
668 /// @returns true iff the proposed cast is valid.
669 /// @brief Determine if a cast is valid without creating one.
670 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
672 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
673 static inline bool classof(const CastInst *) { return true; }
674 static inline bool classof(const Instruction *I) {
677 static inline bool classof(const Value *V) {
678 return isa<Instruction>(V) && classof(cast<Instruction>(V));
682 //===----------------------------------------------------------------------===//
684 //===----------------------------------------------------------------------===//
686 /// This class is the base class for the comparison instructions.
687 /// @brief Abstract base class of comparison instructions.
688 class CmpInst : public Instruction {
689 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
690 CmpInst(); // do not implement
692 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
693 Value *LHS, Value *RHS, const Twine &Name = "",
694 Instruction *InsertBefore = 0);
696 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
697 Value *LHS, Value *RHS, const Twine &Name,
698 BasicBlock *InsertAtEnd);
700 virtual void Anchor() const; // Out of line virtual method.
702 /// This enumeration lists the possible predicates for CmpInst subclasses.
703 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
704 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
705 /// predicate values are not overlapping between the classes.
707 // Opcode U L G E Intuitive operation
708 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
709 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
710 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
711 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
712 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
713 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
714 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
715 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
716 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
717 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
718 FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
719 FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
720 FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
721 FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
722 FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
723 FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
724 FIRST_FCMP_PREDICATE = FCMP_FALSE,
725 LAST_FCMP_PREDICATE = FCMP_TRUE,
726 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
727 ICMP_EQ = 32, ///< equal
728 ICMP_NE = 33, ///< not equal
729 ICMP_UGT = 34, ///< unsigned greater than
730 ICMP_UGE = 35, ///< unsigned greater or equal
731 ICMP_ULT = 36, ///< unsigned less than
732 ICMP_ULE = 37, ///< unsigned less or equal
733 ICMP_SGT = 38, ///< signed greater than
734 ICMP_SGE = 39, ///< signed greater or equal
735 ICMP_SLT = 40, ///< signed less than
736 ICMP_SLE = 41, ///< signed less or equal
737 FIRST_ICMP_PREDICATE = ICMP_EQ,
738 LAST_ICMP_PREDICATE = ICMP_SLE,
739 BAD_ICMP_PREDICATE = ICMP_SLE + 1
742 // allocate space for exactly two operands
743 void *operator new(size_t s) {
744 return User::operator new(s, 2);
746 /// Construct a compare instruction, given the opcode, the predicate and
747 /// the two operands. Optionally (if InstBefore is specified) insert the
748 /// instruction into a BasicBlock right before the specified instruction.
749 /// The specified Instruction is allowed to be a dereferenced end iterator.
750 /// @brief Create a CmpInst
751 static CmpInst *Create(OtherOps Op,
752 unsigned short predicate, Value *S1,
753 Value *S2, const Twine &Name = "",
754 Instruction *InsertBefore = 0);
756 /// Construct a compare instruction, given the opcode, the predicate and the
757 /// two operands. Also automatically insert this instruction to the end of
758 /// the BasicBlock specified.
759 /// @brief Create a CmpInst
760 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
761 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
763 /// @brief Get the opcode casted to the right type
764 OtherOps getOpcode() const {
765 return static_cast<OtherOps>(Instruction::getOpcode());
768 /// @brief Return the predicate for this instruction.
769 Predicate getPredicate() const {
770 return Predicate(getSubclassDataFromInstruction());
773 /// @brief Set the predicate for this instruction to the specified value.
774 void setPredicate(Predicate P) { setInstructionSubclassData(P); }
776 static bool isFPPredicate(Predicate P) {
777 return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
780 static bool isIntPredicate(Predicate P) {
781 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
784 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
785 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
788 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
789 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
790 /// @returns the inverse predicate for the instruction's current predicate.
791 /// @brief Return the inverse of the instruction's predicate.
792 Predicate getInversePredicate() const {
793 return getInversePredicate(getPredicate());
796 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
797 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
798 /// @returns the inverse predicate for predicate provided in \p pred.
799 /// @brief Return the inverse of a given predicate
800 static Predicate getInversePredicate(Predicate pred);
802 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
803 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
804 /// @returns the predicate that would be the result of exchanging the two
805 /// operands of the CmpInst instruction without changing the result
807 /// @brief Return the predicate as if the operands were swapped
808 Predicate getSwappedPredicate() const {
809 return getSwappedPredicate(getPredicate());
812 /// This is a static version that you can use without an instruction
814 /// @brief Return the predicate as if the operands were swapped.
815 static Predicate getSwappedPredicate(Predicate pred);
817 /// @brief Provide more efficient getOperand methods.
818 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
820 /// This is just a convenience that dispatches to the subclasses.
821 /// @brief Swap the operands and adjust predicate accordingly to retain
822 /// the same comparison.
825 /// This is just a convenience that dispatches to the subclasses.
826 /// @brief Determine if this CmpInst is commutative.
827 bool isCommutative();
829 /// This is just a convenience that dispatches to the subclasses.
830 /// @brief Determine if this is an equals/not equals predicate.
833 /// @returns true if the comparison is signed, false otherwise.
834 /// @brief Determine if this instruction is using a signed comparison.
835 bool isSigned() const {
836 return isSigned(getPredicate());
839 /// @returns true if the comparison is unsigned, false otherwise.
840 /// @brief Determine if this instruction is using an unsigned comparison.
841 bool isUnsigned() const {
842 return isUnsigned(getPredicate());
845 /// This is just a convenience.
846 /// @brief Determine if this is true when both operands are the same.
847 bool isTrueWhenEqual() const {
848 return isTrueWhenEqual(getPredicate());
851 /// This is just a convenience.
852 /// @brief Determine if this is false when both operands are the same.
853 bool isFalseWhenEqual() const {
854 return isFalseWhenEqual(getPredicate());
857 /// @returns true if the predicate is unsigned, false otherwise.
858 /// @brief Determine if the predicate is an unsigned operation.
859 static bool isUnsigned(unsigned short predicate);
861 /// @returns true if the predicate is signed, false otherwise.
862 /// @brief Determine if the predicate is an signed operation.
863 static bool isSigned(unsigned short predicate);
865 /// @brief Determine if the predicate is an ordered operation.
866 static bool isOrdered(unsigned short predicate);
868 /// @brief Determine if the predicate is an unordered operation.
869 static bool isUnordered(unsigned short predicate);
871 /// Determine if the predicate is true when comparing a value with itself.
872 static bool isTrueWhenEqual(unsigned short predicate);
874 /// Determine if the predicate is false when comparing a value with itself.
875 static bool isFalseWhenEqual(unsigned short predicate);
877 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
878 static inline bool classof(const CmpInst *) { return true; }
879 static inline bool classof(const Instruction *I) {
880 return I->getOpcode() == Instruction::ICmp ||
881 I->getOpcode() == Instruction::FCmp;
883 static inline bool classof(const Value *V) {
884 return isa<Instruction>(V) && classof(cast<Instruction>(V));
887 /// @brief Create a result type for fcmp/icmp
888 static const Type* makeCmpResultType(const Type* opnd_type) {
889 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
890 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
891 vt->getNumElements());
893 return Type::getInt1Ty(opnd_type->getContext());
896 // Shadow Value::setValueSubclassData with a private forwarding method so that
897 // subclasses cannot accidentally use it.
898 void setValueSubclassData(unsigned short D) {
899 Value::setValueSubclassData(D);
904 // FIXME: these are redundant if CmpInst < BinaryOperator
906 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<2> {
909 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
911 } // End llvm namespace