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_IR_INSTRTYPES_H
17 #define LLVM_IR_INSTRTYPES_H
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/OperandTraits.h"
28 //===----------------------------------------------------------------------===//
29 // TerminatorInst Class
30 //===----------------------------------------------------------------------===//
32 /// 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(Type *Ty, Instruction::TermOps iType,
38 Use *Ops, unsigned NumOps,
39 Instruction *InsertBefore = nullptr)
40 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
42 TerminatorInst(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.
47 ~TerminatorInst() override;
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 /// Return the number of successors that this terminator has.
57 unsigned getNumSuccessors() const {
58 return getNumSuccessorsV();
61 /// Return the specified successor.
62 BasicBlock *getSuccessor(unsigned idx) const {
63 return getSuccessorV(idx);
66 /// Update the specified successor to point at the provided block.
67 void setSuccessor(unsigned idx, BasicBlock *B) {
68 setSuccessorV(idx, B);
71 // Methods for support type inquiry through isa, cast, and dyn_cast:
72 static inline bool classof(const Instruction *I) {
73 return I->isTerminator();
75 static inline bool classof(const Value *V) {
76 return isa<Instruction>(V) && classof(cast<Instruction>(V));
79 // \brief Returns true if this terminator relates to exception handling.
80 bool isExceptional() const {
81 switch (getOpcode()) {
82 case Instruction::CatchPad:
83 case Instruction::CatchEndPad:
84 case Instruction::CatchRet:
85 case Instruction::CleanupRet:
86 case Instruction::Invoke:
87 case Instruction::Resume:
88 case Instruction::TerminatePad:
97 //===----------------------------------------------------------------------===//
98 // UnaryInstruction Class
99 //===----------------------------------------------------------------------===//
101 class UnaryInstruction : public Instruction {
102 void *operator new(size_t, unsigned) = delete;
105 UnaryInstruction(Type *Ty, unsigned iType, Value *V,
106 Instruction *IB = nullptr)
107 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
110 UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
111 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
115 // allocate space for exactly one operand
116 void *operator new(size_t s) {
117 return User::operator new(s, 1);
120 // Out of line virtual method, so the vtable, etc has a home.
121 ~UnaryInstruction() override;
123 /// Transparently provide more efficient getOperand methods.
124 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
126 // Methods for support type inquiry through isa, cast, and dyn_cast:
127 static inline bool classof(const Instruction *I) {
128 return I->getOpcode() == Instruction::Alloca ||
129 I->getOpcode() == Instruction::Load ||
130 I->getOpcode() == Instruction::VAArg ||
131 I->getOpcode() == Instruction::ExtractValue ||
132 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
134 static inline bool classof(const Value *V) {
135 return isa<Instruction>(V) && classof(cast<Instruction>(V));
140 struct OperandTraits<UnaryInstruction> :
141 public FixedNumOperandTraits<UnaryInstruction, 1> {
144 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
146 //===----------------------------------------------------------------------===//
147 // BinaryOperator Class
148 //===----------------------------------------------------------------------===//
150 class BinaryOperator : public Instruction {
151 void *operator new(size_t, unsigned) = delete;
153 void init(BinaryOps iType);
154 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
155 const Twine &Name, Instruction *InsertBefore);
156 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
157 const Twine &Name, BasicBlock *InsertAtEnd);
159 // Note: Instruction needs to be a friend here to call cloneImpl.
160 friend class Instruction;
161 BinaryOperator *cloneImpl() const;
164 // allocate space for exactly two operands
165 void *operator new(size_t s) {
166 return User::operator new(s, 2);
169 /// Transparently provide more efficient getOperand methods.
170 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
172 /// Construct a binary instruction, given the opcode and the two
173 /// operands. Optionally (if InstBefore is specified) insert the instruction
174 /// into a BasicBlock right before the specified instruction. The specified
175 /// Instruction is allowed to be a dereferenced end iterator.
177 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
178 const Twine &Name = Twine(),
179 Instruction *InsertBefore = nullptr);
181 /// Construct a binary instruction, given the opcode and the two
182 /// operands. Also automatically insert this instruction to the end of the
183 /// BasicBlock specified.
185 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
186 const Twine &Name, BasicBlock *InsertAtEnd);
188 /// These methods just forward to Create, and are useful when you
189 /// statically know what type of instruction you're going to create. These
190 /// helpers just save some typing.
191 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
192 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
193 const Twine &Name = "") {\
194 return Create(Instruction::OPC, V1, V2, Name);\
196 #include "llvm/IR/Instruction.def"
197 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
198 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
199 const Twine &Name, BasicBlock *BB) {\
200 return Create(Instruction::OPC, V1, V2, Name, BB);\
202 #include "llvm/IR/Instruction.def"
203 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
204 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
205 const Twine &Name, Instruction *I) {\
206 return Create(Instruction::OPC, V1, V2, Name, I);\
208 #include "llvm/IR/Instruction.def"
210 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
211 const Twine &Name = "") {
212 BinaryOperator *BO = Create(Opc, V1, V2, Name);
213 BO->setHasNoSignedWrap(true);
216 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
217 const Twine &Name, BasicBlock *BB) {
218 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
219 BO->setHasNoSignedWrap(true);
222 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
223 const Twine &Name, Instruction *I) {
224 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
225 BO->setHasNoSignedWrap(true);
229 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
230 const Twine &Name = "") {
231 BinaryOperator *BO = Create(Opc, V1, V2, Name);
232 BO->setHasNoUnsignedWrap(true);
235 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
236 const Twine &Name, BasicBlock *BB) {
237 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
238 BO->setHasNoUnsignedWrap(true);
241 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
242 const Twine &Name, Instruction *I) {
243 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
244 BO->setHasNoUnsignedWrap(true);
248 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
249 const Twine &Name = "") {
250 BinaryOperator *BO = Create(Opc, V1, V2, Name);
251 BO->setIsExact(true);
254 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
255 const Twine &Name, BasicBlock *BB) {
256 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
257 BO->setIsExact(true);
260 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
261 const Twine &Name, Instruction *I) {
262 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
263 BO->setIsExact(true);
267 #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
268 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
269 (Value *V1, Value *V2, const Twine &Name = "") { \
270 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \
272 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
273 (Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \
274 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \
276 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
277 (Value *V1, Value *V2, const Twine &Name, Instruction *I) { \
278 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \
281 DEFINE_HELPERS(Add, NSW) // CreateNSWAdd
282 DEFINE_HELPERS(Add, NUW) // CreateNUWAdd
283 DEFINE_HELPERS(Sub, NSW) // CreateNSWSub
284 DEFINE_HELPERS(Sub, NUW) // CreateNUWSub
285 DEFINE_HELPERS(Mul, NSW) // CreateNSWMul
286 DEFINE_HELPERS(Mul, NUW) // CreateNUWMul
287 DEFINE_HELPERS(Shl, NSW) // CreateNSWShl
288 DEFINE_HELPERS(Shl, NUW) // CreateNUWShl
290 DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv
291 DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv
292 DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
293 DEFINE_HELPERS(LShr, Exact) // CreateExactLShr
295 #undef DEFINE_HELPERS
297 /// Helper functions to construct and inspect unary operations (NEG and NOT)
298 /// via binary operators SUB and XOR:
300 /// Create the NEG and NOT instructions out of SUB and XOR instructions.
302 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
303 Instruction *InsertBefore = nullptr);
304 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
305 BasicBlock *InsertAtEnd);
306 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
307 Instruction *InsertBefore = nullptr);
308 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
309 BasicBlock *InsertAtEnd);
310 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
311 Instruction *InsertBefore = nullptr);
312 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
313 BasicBlock *InsertAtEnd);
314 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
315 Instruction *InsertBefore = nullptr);
316 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
317 BasicBlock *InsertAtEnd);
318 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
319 Instruction *InsertBefore = nullptr);
320 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
321 BasicBlock *InsertAtEnd);
323 /// Check if the given Value is a NEG, FNeg, or NOT instruction.
325 static bool isNeg(const Value *V);
326 static bool isFNeg(const Value *V, bool IgnoreZeroSign=false);
327 static bool isNot(const Value *V);
329 /// Helper functions to extract the unary argument of a NEG, FNEG or NOT
330 /// operation implemented via Sub, FSub, or Xor.
332 static const Value *getNegArgument(const Value *BinOp);
333 static Value *getNegArgument( Value *BinOp);
334 static const Value *getFNegArgument(const Value *BinOp);
335 static Value *getFNegArgument( Value *BinOp);
336 static const Value *getNotArgument(const Value *BinOp);
337 static Value *getNotArgument( Value *BinOp);
339 BinaryOps getOpcode() const {
340 return static_cast<BinaryOps>(Instruction::getOpcode());
343 /// Exchange the two operands to this instruction.
344 /// This instruction is safe to use on any binary instruction and
345 /// does not modify the semantics of the instruction. If the instruction
346 /// cannot be reversed (ie, it's a Div), then return true.
350 /// Set or clear the nsw flag on this instruction, which must be an operator
351 /// which supports this flag. See LangRef.html for the meaning of this flag.
352 void setHasNoUnsignedWrap(bool b = true);
354 /// Set or clear the nsw flag on this instruction, which must be an operator
355 /// which supports this flag. See LangRef.html for the meaning of this flag.
356 void setHasNoSignedWrap(bool b = true);
358 /// Set or clear the exact flag on this instruction, which must be an operator
359 /// which supports this flag. See LangRef.html for the meaning of this flag.
360 void setIsExact(bool b = true);
362 /// Determine whether the no unsigned wrap flag is set.
363 bool hasNoUnsignedWrap() const;
365 /// Determine whether the no signed wrap flag is set.
366 bool hasNoSignedWrap() const;
368 /// Determine whether the exact flag is set.
369 bool isExact() const;
371 /// Convenience method to copy supported wrapping, exact, and fast-math flags
372 /// from V to this instruction.
373 void copyIRFlags(const Value *V);
375 /// Logical 'and' of any supported wrapping, exact, and fast-math flags of
376 /// V and this instruction.
377 void andIRFlags(const Value *V);
379 // Methods for support type inquiry through isa, cast, and dyn_cast:
380 static inline bool classof(const Instruction *I) {
381 return I->isBinaryOp();
383 static inline bool classof(const Value *V) {
384 return isa<Instruction>(V) && classof(cast<Instruction>(V));
389 struct OperandTraits<BinaryOperator> :
390 public FixedNumOperandTraits<BinaryOperator, 2> {
393 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
395 //===----------------------------------------------------------------------===//
397 //===----------------------------------------------------------------------===//
399 /// This is the base class for all instructions that perform data
400 /// casts. It is simply provided so that instruction category testing
401 /// can be performed with code like:
403 /// if (isa<CastInst>(Instr)) { ... }
404 /// @brief Base class of casting instructions.
405 class CastInst : public UnaryInstruction {
406 void anchor() override;
408 /// @brief Constructor with insert-before-instruction semantics for subclasses
409 CastInst(Type *Ty, unsigned iType, Value *S,
410 const Twine &NameStr = "", Instruction *InsertBefore = nullptr)
411 : UnaryInstruction(Ty, iType, S, InsertBefore) {
414 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
415 CastInst(Type *Ty, unsigned iType, Value *S,
416 const Twine &NameStr, BasicBlock *InsertAtEnd)
417 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
421 /// Provides a way to construct any of the CastInst subclasses using an
422 /// opcode instead of the subclass's constructor. The opcode must be in the
423 /// CastOps category (Instruction::isCast(opcode) returns true). This
424 /// constructor has insert-before-instruction semantics to automatically
425 /// insert the new CastInst before InsertBefore (if it is non-null).
426 /// @brief Construct any of the CastInst subclasses
427 static CastInst *Create(
428 Instruction::CastOps, ///< The opcode of the cast instruction
429 Value *S, ///< The value to be casted (operand 0)
430 Type *Ty, ///< The type to which cast should be made
431 const Twine &Name = "", ///< Name for the instruction
432 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
434 /// Provides a way to construct any of the CastInst subclasses using an
435 /// opcode instead of the subclass's constructor. The opcode must be in the
436 /// CastOps category. This constructor has insert-at-end-of-block semantics
437 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
439 /// @brief Construct any of the CastInst subclasses
440 static CastInst *Create(
441 Instruction::CastOps, ///< The opcode for the cast instruction
442 Value *S, ///< The value to be casted (operand 0)
443 Type *Ty, ///< The type to which operand is casted
444 const Twine &Name, ///< The name for the instruction
445 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
448 /// @brief Create a ZExt or BitCast cast instruction
449 static CastInst *CreateZExtOrBitCast(
450 Value *S, ///< The value to be casted (operand 0)
451 Type *Ty, ///< The type to which cast should be made
452 const Twine &Name = "", ///< Name for the instruction
453 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
456 /// @brief Create a ZExt or BitCast cast instruction
457 static CastInst *CreateZExtOrBitCast(
458 Value *S, ///< The value to be casted (operand 0)
459 Type *Ty, ///< The type to which operand is casted
460 const Twine &Name, ///< The name for the instruction
461 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
464 /// @brief Create a SExt or BitCast cast instruction
465 static CastInst *CreateSExtOrBitCast(
466 Value *S, ///< The value to be casted (operand 0)
467 Type *Ty, ///< The type to which cast should be made
468 const Twine &Name = "", ///< Name for the instruction
469 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
472 /// @brief Create a SExt or BitCast cast instruction
473 static CastInst *CreateSExtOrBitCast(
474 Value *S, ///< The value to be casted (operand 0)
475 Type *Ty, ///< The type to which operand is casted
476 const Twine &Name, ///< The name for the instruction
477 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
480 /// @brief Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
481 static CastInst *CreatePointerCast(
482 Value *S, ///< The pointer value to be casted (operand 0)
483 Type *Ty, ///< The type to which operand is casted
484 const Twine &Name, ///< The name for the instruction
485 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
488 /// @brief Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
489 static CastInst *CreatePointerCast(
490 Value *S, ///< The pointer value to be casted (operand 0)
491 Type *Ty, ///< The type to which cast should be made
492 const Twine &Name = "", ///< Name for the instruction
493 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
496 /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
497 static CastInst *CreatePointerBitCastOrAddrSpaceCast(
498 Value *S, ///< The pointer value to be casted (operand 0)
499 Type *Ty, ///< The type to which operand is casted
500 const Twine &Name, ///< The name for the instruction
501 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
504 /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
505 static CastInst *CreatePointerBitCastOrAddrSpaceCast(
506 Value *S, ///< The pointer value to be casted (operand 0)
507 Type *Ty, ///< The type to which cast should be made
508 const Twine &Name = "", ///< Name for the instruction
509 Instruction *InsertBefore = 0 ///< Place to insert the instruction
512 /// @brief Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
514 /// If the value is a pointer type and the destination an integer type,
515 /// creates a PtrToInt cast. If the value is an integer type and the
516 /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates
518 static CastInst *CreateBitOrPointerCast(
519 Value *S, ///< The pointer value to be casted (operand 0)
520 Type *Ty, ///< The type to which cast should be made
521 const Twine &Name = "", ///< Name for the instruction
522 Instruction *InsertBefore = 0 ///< Place to insert the instruction
525 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
526 static CastInst *CreateIntegerCast(
527 Value *S, ///< The pointer value to be casted (operand 0)
528 Type *Ty, ///< The type to which cast should be made
529 bool isSigned, ///< Whether to regard S as signed or not
530 const Twine &Name = "", ///< Name for the instruction
531 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
534 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
535 static CastInst *CreateIntegerCast(
536 Value *S, ///< The integer value to be casted (operand 0)
537 Type *Ty, ///< The integer type to which operand is casted
538 bool isSigned, ///< Whether to regard S as signed or not
539 const Twine &Name, ///< The name for the instruction
540 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
543 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
544 static CastInst *CreateFPCast(
545 Value *S, ///< The floating point value to be casted
546 Type *Ty, ///< The floating point type to cast to
547 const Twine &Name = "", ///< Name for the instruction
548 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
551 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
552 static CastInst *CreateFPCast(
553 Value *S, ///< The floating point value to be casted
554 Type *Ty, ///< The floating point type to cast to
555 const Twine &Name, ///< The name for the instruction
556 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
559 /// @brief Create a Trunc or BitCast cast instruction
560 static CastInst *CreateTruncOrBitCast(
561 Value *S, ///< The value to be casted (operand 0)
562 Type *Ty, ///< The type to which cast should be made
563 const Twine &Name = "", ///< Name for the instruction
564 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
567 /// @brief Create a Trunc or BitCast cast instruction
568 static CastInst *CreateTruncOrBitCast(
569 Value *S, ///< The value to be casted (operand 0)
570 Type *Ty, ///< The type to which operand is casted
571 const Twine &Name, ///< The name for the instruction
572 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
575 /// @brief Check whether it is valid to call getCastOpcode for these types.
576 static bool isCastable(
577 Type *SrcTy, ///< The Type from which the value should be cast.
578 Type *DestTy ///< The Type to which the value should be cast.
581 /// @brief Check whether a bitcast between these types is valid
582 static bool isBitCastable(
583 Type *SrcTy, ///< The Type from which the value should be cast.
584 Type *DestTy ///< The Type to which the value should be cast.
587 /// @brief Check whether a bitcast, inttoptr, or ptrtoint cast between these
588 /// types is valid and a no-op.
590 /// This ensures that any pointer<->integer cast has enough bits in the
591 /// integer and any other cast is a bitcast.
592 static bool isBitOrNoopPointerCastable(
593 Type *SrcTy, ///< The Type from which the value should be cast.
594 Type *DestTy, ///< The Type to which the value should be cast.
595 const DataLayout &DL);
597 /// Returns the opcode necessary to cast Val into Ty using usual casting
599 /// @brief Infer the opcode for cast operand and type
600 static Instruction::CastOps getCastOpcode(
601 const Value *Val, ///< The value to cast
602 bool SrcIsSigned, ///< Whether to treat the source as signed
603 Type *Ty, ///< The Type to which the value should be casted
604 bool DstIsSigned ///< Whether to treate the dest. as signed
607 /// There are several places where we need to know if a cast instruction
608 /// only deals with integer source and destination types. To simplify that
609 /// logic, this method is provided.
610 /// @returns true iff the cast has only integral typed operand and dest type.
611 /// @brief Determine if this is an integer-only cast.
612 bool isIntegerCast() const;
614 /// A lossless cast is one that does not alter the basic value. It implies
615 /// a no-op cast but is more stringent, preventing things like int->float,
616 /// long->double, or int->ptr.
617 /// @returns true iff the cast is lossless.
618 /// @brief Determine if this is a lossless cast.
619 bool isLosslessCast() const;
621 /// A no-op cast is one that can be effected without changing any bits.
622 /// It implies that the source and destination types are the same size. The
623 /// IntPtrTy argument is used to make accurate determinations for casts
624 /// involving Integer and Pointer types. They are no-op casts if the integer
625 /// is the same size as the pointer. However, pointer size varies with
626 /// platform. Generally, the result of DataLayout::getIntPtrType() should be
627 /// passed in. If that's not available, use Type::Int64Ty, which will make
628 /// the isNoopCast call conservative.
629 /// @brief Determine if the described cast is a no-op cast.
630 static bool isNoopCast(
631 Instruction::CastOps Opcode, ///< Opcode of cast
632 Type *SrcTy, ///< SrcTy of cast
633 Type *DstTy, ///< DstTy of cast
634 Type *IntPtrTy ///< Integer type corresponding to Ptr types
637 /// @brief Determine if this cast is a no-op cast.
639 Type *IntPtrTy ///< Integer type corresponding to pointer
642 /// @brief Determine if this cast is a no-op cast.
644 /// \param DL is the DataLayout to get the Int Ptr type from.
645 bool isNoopCast(const DataLayout &DL) const;
647 /// Determine how a pair of casts can be eliminated, if they can be at all.
648 /// This is a helper function for both CastInst and ConstantExpr.
649 /// @returns 0 if the CastInst pair can't be eliminated, otherwise
650 /// returns Instruction::CastOps value for a cast that can replace
651 /// the pair, casting SrcTy to DstTy.
652 /// @brief Determine if a cast pair is eliminable
653 static unsigned isEliminableCastPair(
654 Instruction::CastOps firstOpcode, ///< Opcode of first cast
655 Instruction::CastOps secondOpcode, ///< Opcode of second cast
656 Type *SrcTy, ///< SrcTy of 1st cast
657 Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
658 Type *DstTy, ///< DstTy of 2nd cast
659 Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
660 Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
661 Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null
664 /// @brief Return the opcode of this CastInst
665 Instruction::CastOps getOpcode() const {
666 return Instruction::CastOps(Instruction::getOpcode());
669 /// @brief Return the source type, as a convenience
670 Type* getSrcTy() const { return getOperand(0)->getType(); }
671 /// @brief Return the destination type, as a convenience
672 Type* getDestTy() const { return getType(); }
674 /// This method can be used to determine if a cast from S to DstTy using
675 /// Opcode op is valid or not.
676 /// @returns true iff the proposed cast is valid.
677 /// @brief Determine if a cast is valid without creating one.
678 static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
680 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
681 static inline bool classof(const Instruction *I) {
684 static inline bool classof(const Value *V) {
685 return isa<Instruction>(V) && classof(cast<Instruction>(V));
689 //===----------------------------------------------------------------------===//
691 //===----------------------------------------------------------------------===//
693 /// This class is the base class for the comparison instructions.
694 /// @brief Abstract base class of comparison instructions.
695 class CmpInst : public Instruction {
696 void *operator new(size_t, unsigned) = delete;
699 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
700 Value *LHS, Value *RHS, const Twine &Name = "",
701 Instruction *InsertBefore = nullptr);
703 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
704 Value *LHS, Value *RHS, const Twine &Name,
705 BasicBlock *InsertAtEnd);
707 void anchor() override; // Out of line virtual method.
709 /// This enumeration lists the possible predicates for CmpInst subclasses.
710 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
711 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
712 /// predicate values are not overlapping between the classes.
714 // Opcode U L G E Intuitive operation
715 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
716 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
717 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
718 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
719 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
720 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
721 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
722 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
723 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
724 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
725 FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
726 FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
727 FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
728 FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
729 FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
730 FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
731 FIRST_FCMP_PREDICATE = FCMP_FALSE,
732 LAST_FCMP_PREDICATE = FCMP_TRUE,
733 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
734 ICMP_EQ = 32, ///< equal
735 ICMP_NE = 33, ///< not equal
736 ICMP_UGT = 34, ///< unsigned greater than
737 ICMP_UGE = 35, ///< unsigned greater or equal
738 ICMP_ULT = 36, ///< unsigned less than
739 ICMP_ULE = 37, ///< unsigned less or equal
740 ICMP_SGT = 38, ///< signed greater than
741 ICMP_SGE = 39, ///< signed greater or equal
742 ICMP_SLT = 40, ///< signed less than
743 ICMP_SLE = 41, ///< signed less or equal
744 FIRST_ICMP_PREDICATE = ICMP_EQ,
745 LAST_ICMP_PREDICATE = ICMP_SLE,
746 BAD_ICMP_PREDICATE = ICMP_SLE + 1
749 // allocate space for exactly two operands
750 void *operator new(size_t s) {
751 return User::operator new(s, 2);
753 /// Construct a compare instruction, given the opcode, the predicate and
754 /// the two operands. Optionally (if InstBefore is specified) insert the
755 /// instruction into a BasicBlock right before the specified instruction.
756 /// The specified Instruction is allowed to be a dereferenced end iterator.
757 /// @brief Create a CmpInst
758 static CmpInst *Create(OtherOps Op,
759 unsigned short predicate, Value *S1,
760 Value *S2, const Twine &Name = "",
761 Instruction *InsertBefore = nullptr);
763 /// Construct a compare instruction, given the opcode, the predicate and the
764 /// two operands. Also automatically insert this instruction to the end of
765 /// the BasicBlock specified.
766 /// @brief Create a CmpInst
767 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
768 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
770 /// @brief Get the opcode casted to the right type
771 OtherOps getOpcode() const {
772 return static_cast<OtherOps>(Instruction::getOpcode());
775 /// @brief Return the predicate for this instruction.
776 Predicate getPredicate() const {
777 return Predicate(getSubclassDataFromInstruction());
780 /// @brief Set the predicate for this instruction to the specified value.
781 void setPredicate(Predicate P) { setInstructionSubclassData(P); }
783 static bool isFPPredicate(Predicate P) {
784 return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
787 static bool isIntPredicate(Predicate P) {
788 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
791 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
792 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
795 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
796 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
797 /// @returns the inverse predicate for the instruction's current predicate.
798 /// @brief Return the inverse of the instruction's predicate.
799 Predicate getInversePredicate() const {
800 return getInversePredicate(getPredicate());
803 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
804 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
805 /// @returns the inverse predicate for predicate provided in \p pred.
806 /// @brief Return the inverse of a given predicate
807 static Predicate getInversePredicate(Predicate pred);
809 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
810 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
811 /// @returns the predicate that would be the result of exchanging the two
812 /// operands of the CmpInst instruction without changing the result
814 /// @brief Return the predicate as if the operands were swapped
815 Predicate getSwappedPredicate() const {
816 return getSwappedPredicate(getPredicate());
819 /// This is a static version that you can use without an instruction
821 /// @brief Return the predicate as if the operands were swapped.
822 static Predicate getSwappedPredicate(Predicate pred);
824 /// @brief Provide more efficient getOperand methods.
825 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
827 /// This is just a convenience that dispatches to the subclasses.
828 /// @brief Swap the operands and adjust predicate accordingly to retain
829 /// the same comparison.
832 /// This is just a convenience that dispatches to the subclasses.
833 /// @brief Determine if this CmpInst is commutative.
834 bool isCommutative() const;
836 /// This is just a convenience that dispatches to the subclasses.
837 /// @brief Determine if this is an equals/not equals predicate.
838 bool isEquality() const;
840 /// @returns true if the comparison is signed, false otherwise.
841 /// @brief Determine if this instruction is using a signed comparison.
842 bool isSigned() const {
843 return isSigned(getPredicate());
846 /// @returns true if the comparison is unsigned, false otherwise.
847 /// @brief Determine if this instruction is using an unsigned comparison.
848 bool isUnsigned() const {
849 return isUnsigned(getPredicate());
852 /// This is just a convenience.
853 /// @brief Determine if this is true when both operands are the same.
854 bool isTrueWhenEqual() const {
855 return isTrueWhenEqual(getPredicate());
858 /// This is just a convenience.
859 /// @brief Determine if this is false when both operands are the same.
860 bool isFalseWhenEqual() const {
861 return isFalseWhenEqual(getPredicate());
864 /// @returns true if the predicate is unsigned, false otherwise.
865 /// @brief Determine if the predicate is an unsigned operation.
866 static bool isUnsigned(unsigned short predicate);
868 /// @returns true if the predicate is signed, false otherwise.
869 /// @brief Determine if the predicate is an signed operation.
870 static bool isSigned(unsigned short predicate);
872 /// @brief Determine if the predicate is an ordered operation.
873 static bool isOrdered(unsigned short predicate);
875 /// @brief Determine if the predicate is an unordered operation.
876 static bool isUnordered(unsigned short predicate);
878 /// Determine if the predicate is true when comparing a value with itself.
879 static bool isTrueWhenEqual(unsigned short predicate);
881 /// Determine if the predicate is false when comparing a value with itself.
882 static bool isFalseWhenEqual(unsigned short predicate);
884 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
885 static inline bool classof(const Instruction *I) {
886 return I->getOpcode() == Instruction::ICmp ||
887 I->getOpcode() == Instruction::FCmp;
889 static inline bool classof(const Value *V) {
890 return isa<Instruction>(V) && classof(cast<Instruction>(V));
893 /// @brief Create a result type for fcmp/icmp
894 static Type* makeCmpResultType(Type* opnd_type) {
895 if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
896 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
897 vt->getNumElements());
899 return Type::getInt1Ty(opnd_type->getContext());
902 // Shadow Value::setValueSubclassData with a private forwarding method so that
903 // subclasses cannot accidentally use it.
904 void setValueSubclassData(unsigned short D) {
905 Value::setValueSubclassData(D);
910 // FIXME: these are redundant if CmpInst < BinaryOperator
912 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
915 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
917 } // End llvm namespace