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));
81 //===----------------------------------------------------------------------===//
82 // UnaryInstruction Class
83 //===----------------------------------------------------------------------===//
85 class UnaryInstruction : public Instruction {
86 void *operator new(size_t, unsigned) = delete;
89 UnaryInstruction(Type *Ty, unsigned iType, Value *V,
90 Instruction *IB = nullptr)
91 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
94 UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
95 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
99 // allocate space for exactly one operand
100 void *operator new(size_t s) {
101 return User::operator new(s, 1);
104 // Out of line virtual method, so the vtable, etc has a home.
105 ~UnaryInstruction() override;
107 /// Transparently provide more efficient getOperand methods.
108 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
110 // Methods for support type inquiry through isa, cast, and dyn_cast:
111 static inline bool classof(const Instruction *I) {
112 return I->getOpcode() == Instruction::Alloca ||
113 I->getOpcode() == Instruction::Load ||
114 I->getOpcode() == Instruction::VAArg ||
115 I->getOpcode() == Instruction::ExtractValue ||
116 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
118 static inline bool classof(const Value *V) {
119 return isa<Instruction>(V) && classof(cast<Instruction>(V));
124 struct OperandTraits<UnaryInstruction> :
125 public FixedNumOperandTraits<UnaryInstruction, 1> {
128 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
130 //===----------------------------------------------------------------------===//
131 // BinaryOperator Class
132 //===----------------------------------------------------------------------===//
134 class BinaryOperator : public Instruction {
135 void *operator new(size_t, unsigned) = delete;
137 void init(BinaryOps iType);
138 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
139 const Twine &Name, Instruction *InsertBefore);
140 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
141 const Twine &Name, BasicBlock *InsertAtEnd);
142 BinaryOperator *clone_impl() const override;
144 // allocate space for exactly two operands
145 void *operator new(size_t s) {
146 return User::operator new(s, 2);
149 /// Transparently provide more efficient getOperand methods.
150 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
152 /// Construct a binary instruction, given the opcode and the two
153 /// operands. Optionally (if InstBefore is specified) insert the instruction
154 /// into a BasicBlock right before the specified instruction. The specified
155 /// Instruction is allowed to be a dereferenced end iterator.
157 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
158 const Twine &Name = Twine(),
159 Instruction *InsertBefore = nullptr);
161 /// Construct a binary instruction, given the opcode and the two
162 /// operands. Also automatically insert this instruction to the end of the
163 /// BasicBlock specified.
165 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
166 const Twine &Name, BasicBlock *InsertAtEnd);
168 /// These methods just forward to Create, and are useful when you
169 /// statically know what type of instruction you're going to create. These
170 /// helpers just save some typing.
171 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
172 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
173 const Twine &Name = "") {\
174 return Create(Instruction::OPC, V1, V2, Name);\
176 #include "llvm/IR/Instruction.def"
177 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
178 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
179 const Twine &Name, BasicBlock *BB) {\
180 return Create(Instruction::OPC, V1, V2, Name, BB);\
182 #include "llvm/IR/Instruction.def"
183 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
184 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
185 const Twine &Name, Instruction *I) {\
186 return Create(Instruction::OPC, V1, V2, Name, I);\
188 #include "llvm/IR/Instruction.def"
190 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
191 const Twine &Name = "") {
192 BinaryOperator *BO = Create(Opc, V1, V2, Name);
193 BO->setHasNoSignedWrap(true);
196 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
197 const Twine &Name, BasicBlock *BB) {
198 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
199 BO->setHasNoSignedWrap(true);
202 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
203 const Twine &Name, Instruction *I) {
204 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
205 BO->setHasNoSignedWrap(true);
209 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
210 const Twine &Name = "") {
211 BinaryOperator *BO = Create(Opc, V1, V2, Name);
212 BO->setHasNoUnsignedWrap(true);
215 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
216 const Twine &Name, BasicBlock *BB) {
217 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
218 BO->setHasNoUnsignedWrap(true);
221 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
222 const Twine &Name, Instruction *I) {
223 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
224 BO->setHasNoUnsignedWrap(true);
228 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
229 const Twine &Name = "") {
230 BinaryOperator *BO = Create(Opc, V1, V2, Name);
231 BO->setIsExact(true);
234 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
235 const Twine &Name, BasicBlock *BB) {
236 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
237 BO->setIsExact(true);
240 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
241 const Twine &Name, Instruction *I) {
242 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
243 BO->setIsExact(true);
247 #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
248 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
249 (Value *V1, Value *V2, const Twine &Name = "") { \
250 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \
252 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
253 (Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \
254 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \
256 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
257 (Value *V1, Value *V2, const Twine &Name, Instruction *I) { \
258 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \
261 DEFINE_HELPERS(Add, NSW) // CreateNSWAdd
262 DEFINE_HELPERS(Add, NUW) // CreateNUWAdd
263 DEFINE_HELPERS(Sub, NSW) // CreateNSWSub
264 DEFINE_HELPERS(Sub, NUW) // CreateNUWSub
265 DEFINE_HELPERS(Mul, NSW) // CreateNSWMul
266 DEFINE_HELPERS(Mul, NUW) // CreateNUWMul
267 DEFINE_HELPERS(Shl, NSW) // CreateNSWShl
268 DEFINE_HELPERS(Shl, NUW) // CreateNUWShl
270 DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv
271 DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv
272 DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
273 DEFINE_HELPERS(LShr, Exact) // CreateExactLShr
275 #undef DEFINE_HELPERS
277 /// Helper functions to construct and inspect unary operations (NEG and NOT)
278 /// via binary operators SUB and XOR:
280 /// Create the NEG and NOT instructions out of SUB and XOR instructions.
282 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
283 Instruction *InsertBefore = nullptr);
284 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
285 BasicBlock *InsertAtEnd);
286 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
287 Instruction *InsertBefore = nullptr);
288 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
289 BasicBlock *InsertAtEnd);
290 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
291 Instruction *InsertBefore = nullptr);
292 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
293 BasicBlock *InsertAtEnd);
294 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
295 Instruction *InsertBefore = nullptr);
296 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
297 BasicBlock *InsertAtEnd);
298 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
299 Instruction *InsertBefore = nullptr);
300 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
301 BasicBlock *InsertAtEnd);
303 /// Check if the given Value is a NEG, FNeg, or NOT instruction.
305 static bool isNeg(const Value *V);
306 static bool isFNeg(const Value *V, bool IgnoreZeroSign=false);
307 static bool isNot(const Value *V);
309 /// Helper functions to extract the unary argument of a NEG, FNEG or NOT
310 /// operation implemented via Sub, FSub, or Xor.
312 static const Value *getNegArgument(const Value *BinOp);
313 static Value *getNegArgument( Value *BinOp);
314 static const Value *getFNegArgument(const Value *BinOp);
315 static Value *getFNegArgument( Value *BinOp);
316 static const Value *getNotArgument(const Value *BinOp);
317 static Value *getNotArgument( Value *BinOp);
319 BinaryOps getOpcode() const {
320 return static_cast<BinaryOps>(Instruction::getOpcode());
323 /// Exchange the two operands to this instruction.
324 /// This instruction is safe to use on any binary instruction and
325 /// does not modify the semantics of the instruction. If the instruction
326 /// cannot be reversed (ie, it's a Div), then return true.
330 /// Set or clear the nsw flag on this instruction, which must be an operator
331 /// which supports this flag. See LangRef.html for the meaning of this flag.
332 void setHasNoUnsignedWrap(bool b = true);
334 /// Set or clear the nsw flag on this instruction, which must be an operator
335 /// which supports this flag. See LangRef.html for the meaning of this flag.
336 void setHasNoSignedWrap(bool b = true);
338 /// Set or clear the exact flag on this instruction, which must be an operator
339 /// which supports this flag. See LangRef.html for the meaning of this flag.
340 void setIsExact(bool b = true);
342 /// Determine whether the no unsigned wrap flag is set.
343 bool hasNoUnsignedWrap() const;
345 /// Determine whether the no signed wrap flag is set.
346 bool hasNoSignedWrap() const;
348 /// Determine whether the exact flag is set.
349 bool isExact() const;
351 /// Convenience method to copy supported wrapping, exact, and fast-math flags
352 /// from V to this instruction.
353 void copyIRFlags(const Value *V);
355 /// Logical 'and' of any supported wrapping, exact, and fast-math flags of
356 /// V and this instruction.
357 void andIRFlags(const Value *V);
359 // Methods for support type inquiry through isa, cast, and dyn_cast:
360 static inline bool classof(const Instruction *I) {
361 return I->isBinaryOp();
363 static inline bool classof(const Value *V) {
364 return isa<Instruction>(V) && classof(cast<Instruction>(V));
369 struct OperandTraits<BinaryOperator> :
370 public FixedNumOperandTraits<BinaryOperator, 2> {
373 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
375 //===----------------------------------------------------------------------===//
377 //===----------------------------------------------------------------------===//
379 /// This is the base class for all instructions that perform data
380 /// casts. It is simply provided so that instruction category testing
381 /// can be performed with code like:
383 /// if (isa<CastInst>(Instr)) { ... }
384 /// @brief Base class of casting instructions.
385 class CastInst : public UnaryInstruction {
386 void anchor() override;
388 /// @brief Constructor with insert-before-instruction semantics for subclasses
389 CastInst(Type *Ty, unsigned iType, Value *S,
390 const Twine &NameStr = "", Instruction *InsertBefore = nullptr)
391 : UnaryInstruction(Ty, iType, S, InsertBefore) {
394 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
395 CastInst(Type *Ty, unsigned iType, Value *S,
396 const Twine &NameStr, BasicBlock *InsertAtEnd)
397 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
401 /// Provides a way to construct any of the CastInst subclasses using an
402 /// opcode instead of the subclass's constructor. The opcode must be in the
403 /// CastOps category (Instruction::isCast(opcode) returns true). This
404 /// constructor has insert-before-instruction semantics to automatically
405 /// insert the new CastInst before InsertBefore (if it is non-null).
406 /// @brief Construct any of the CastInst subclasses
407 static CastInst *Create(
408 Instruction::CastOps, ///< The opcode of the cast instruction
409 Value *S, ///< The value to be casted (operand 0)
410 Type *Ty, ///< The type to which cast should be made
411 const Twine &Name = "", ///< Name for the instruction
412 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
414 /// Provides a way to construct any of the CastInst subclasses using an
415 /// opcode instead of the subclass's constructor. The opcode must be in the
416 /// CastOps category. This constructor has insert-at-end-of-block semantics
417 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
419 /// @brief Construct any of the CastInst subclasses
420 static CastInst *Create(
421 Instruction::CastOps, ///< The opcode for the cast instruction
422 Value *S, ///< The value to be casted (operand 0)
423 Type *Ty, ///< The type to which operand is casted
424 const Twine &Name, ///< The name for the instruction
425 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
428 /// @brief Create a ZExt or BitCast cast instruction
429 static CastInst *CreateZExtOrBitCast(
430 Value *S, ///< The value to be casted (operand 0)
431 Type *Ty, ///< The type to which cast should be made
432 const Twine &Name = "", ///< Name for the instruction
433 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
436 /// @brief Create a ZExt or BitCast cast instruction
437 static CastInst *CreateZExtOrBitCast(
438 Value *S, ///< The value to be casted (operand 0)
439 Type *Ty, ///< The type to which operand is casted
440 const Twine &Name, ///< The name for the instruction
441 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
444 /// @brief Create a SExt or BitCast cast instruction
445 static CastInst *CreateSExtOrBitCast(
446 Value *S, ///< The value to be casted (operand 0)
447 Type *Ty, ///< The type to which cast should be made
448 const Twine &Name = "", ///< Name for the instruction
449 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
452 /// @brief Create a SExt or BitCast cast instruction
453 static CastInst *CreateSExtOrBitCast(
454 Value *S, ///< The value to be casted (operand 0)
455 Type *Ty, ///< The type to which operand is casted
456 const Twine &Name, ///< The name for the instruction
457 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
460 /// @brief Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
461 static CastInst *CreatePointerCast(
462 Value *S, ///< The pointer value to be casted (operand 0)
463 Type *Ty, ///< The type to which operand is casted
464 const Twine &Name, ///< The name for the instruction
465 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
468 /// @brief Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
469 static CastInst *CreatePointerCast(
470 Value *S, ///< The pointer value to be casted (operand 0)
471 Type *Ty, ///< The type to which cast should be made
472 const Twine &Name = "", ///< Name for the instruction
473 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
476 /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
477 static CastInst *CreatePointerBitCastOrAddrSpaceCast(
478 Value *S, ///< The pointer value to be casted (operand 0)
479 Type *Ty, ///< The type to which operand is casted
480 const Twine &Name, ///< The name for the instruction
481 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
484 /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
485 static CastInst *CreatePointerBitCastOrAddrSpaceCast(
486 Value *S, ///< The pointer value to be casted (operand 0)
487 Type *Ty, ///< The type to which cast should be made
488 const Twine &Name = "", ///< Name for the instruction
489 Instruction *InsertBefore = 0 ///< Place to insert the instruction
492 /// @brief Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
494 /// If the value is a pointer type and the destination an integer type,
495 /// creates a PtrToInt cast. If the value is an integer type and the
496 /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates
498 static CastInst *CreateBitOrPointerCast(
499 Value *S, ///< The pointer value to be casted (operand 0)
500 Type *Ty, ///< The type to which cast should be made
501 const Twine &Name = "", ///< Name for the instruction
502 Instruction *InsertBefore = 0 ///< Place to insert the instruction
505 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
506 static CastInst *CreateIntegerCast(
507 Value *S, ///< The pointer value to be casted (operand 0)
508 Type *Ty, ///< The type to which cast should be made
509 bool isSigned, ///< Whether to regard S as signed or not
510 const Twine &Name = "", ///< Name for the instruction
511 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
514 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
515 static CastInst *CreateIntegerCast(
516 Value *S, ///< The integer value to be casted (operand 0)
517 Type *Ty, ///< The integer type to which operand is casted
518 bool isSigned, ///< Whether to regard S as signed or not
519 const Twine &Name, ///< The name for the instruction
520 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
523 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
524 static CastInst *CreateFPCast(
525 Value *S, ///< The floating point value to be casted
526 Type *Ty, ///< The floating point type to cast to
527 const Twine &Name = "", ///< Name for the instruction
528 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
531 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
532 static CastInst *CreateFPCast(
533 Value *S, ///< The floating point value to be casted
534 Type *Ty, ///< The floating point type to cast to
535 const Twine &Name, ///< The name for the instruction
536 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
539 /// @brief Create a Trunc or BitCast cast instruction
540 static CastInst *CreateTruncOrBitCast(
541 Value *S, ///< The value to be casted (operand 0)
542 Type *Ty, ///< The type to which cast should be made
543 const Twine &Name = "", ///< Name for the instruction
544 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
547 /// @brief Create a Trunc or BitCast cast instruction
548 static CastInst *CreateTruncOrBitCast(
549 Value *S, ///< The value to be casted (operand 0)
550 Type *Ty, ///< The type to which operand is casted
551 const Twine &Name, ///< The name for the instruction
552 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
555 /// @brief Check whether it is valid to call getCastOpcode for these types.
556 static bool isCastable(
557 Type *SrcTy, ///< The Type from which the value should be cast.
558 Type *DestTy ///< The Type to which the value should be cast.
561 /// @brief Check whether a bitcast between these types is valid
562 static bool isBitCastable(
563 Type *SrcTy, ///< The Type from which the value should be cast.
564 Type *DestTy ///< The Type to which the value should be cast.
567 /// @brief Check whether a bitcast, inttoptr, or ptrtoint cast between these
568 /// types is valid and a no-op.
570 /// This ensures that any pointer<->integer cast has enough bits in the
571 /// integer and any other cast is a bitcast.
572 static bool isBitOrNoopPointerCastable(
573 Type *SrcTy, ///< The Type from which the value should be cast.
574 Type *DestTy, ///< The Type to which the value should be cast.
575 const DataLayout &DL);
577 /// Returns the opcode necessary to cast Val into Ty using usual casting
579 /// @brief Infer the opcode for cast operand and type
580 static Instruction::CastOps getCastOpcode(
581 const Value *Val, ///< The value to cast
582 bool SrcIsSigned, ///< Whether to treat the source as signed
583 Type *Ty, ///< The Type to which the value should be casted
584 bool DstIsSigned ///< Whether to treate the dest. as signed
587 /// There are several places where we need to know if a cast instruction
588 /// only deals with integer source and destination types. To simplify that
589 /// logic, this method is provided.
590 /// @returns true iff the cast has only integral typed operand and dest type.
591 /// @brief Determine if this is an integer-only cast.
592 bool isIntegerCast() const;
594 /// A lossless cast is one that does not alter the basic value. It implies
595 /// a no-op cast but is more stringent, preventing things like int->float,
596 /// long->double, or int->ptr.
597 /// @returns true iff the cast is lossless.
598 /// @brief Determine if this is a lossless cast.
599 bool isLosslessCast() const;
601 /// A no-op cast is one that can be effected without changing any bits.
602 /// It implies that the source and destination types are the same size. The
603 /// IntPtrTy argument is used to make accurate determinations for casts
604 /// involving Integer and Pointer types. They are no-op casts if the integer
605 /// is the same size as the pointer. However, pointer size varies with
606 /// platform. Generally, the result of DataLayout::getIntPtrType() should be
607 /// passed in. If that's not available, use Type::Int64Ty, which will make
608 /// the isNoopCast call conservative.
609 /// @brief Determine if the described cast is a no-op cast.
610 static bool isNoopCast(
611 Instruction::CastOps Opcode, ///< Opcode of cast
612 Type *SrcTy, ///< SrcTy of cast
613 Type *DstTy, ///< DstTy of cast
614 Type *IntPtrTy ///< Integer type corresponding to Ptr types
617 /// @brief Determine if this cast is a no-op cast.
619 Type *IntPtrTy ///< Integer type corresponding to pointer
622 /// @brief Determine if this cast is a no-op cast.
624 /// \param DL is the DataLayout to get the Int Ptr type from.
625 bool isNoopCast(const DataLayout &DL) const;
627 /// Determine how a pair of casts can be eliminated, if they can be at all.
628 /// This is a helper function for both CastInst and ConstantExpr.
629 /// @returns 0 if the CastInst pair can't be eliminated, otherwise
630 /// returns Instruction::CastOps value for a cast that can replace
631 /// the pair, casting SrcTy to DstTy.
632 /// @brief Determine if a cast pair is eliminable
633 static unsigned isEliminableCastPair(
634 Instruction::CastOps firstOpcode, ///< Opcode of first cast
635 Instruction::CastOps secondOpcode, ///< Opcode of second cast
636 Type *SrcTy, ///< SrcTy of 1st cast
637 Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
638 Type *DstTy, ///< DstTy of 2nd cast
639 Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
640 Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
641 Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null
644 /// @brief Return the opcode of this CastInst
645 Instruction::CastOps getOpcode() const {
646 return Instruction::CastOps(Instruction::getOpcode());
649 /// @brief Return the source type, as a convenience
650 Type* getSrcTy() const { return getOperand(0)->getType(); }
651 /// @brief Return the destination type, as a convenience
652 Type* getDestTy() const { return getType(); }
654 /// This method can be used to determine if a cast from S to DstTy using
655 /// Opcode op is valid or not.
656 /// @returns true iff the proposed cast is valid.
657 /// @brief Determine if a cast is valid without creating one.
658 static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
660 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
661 static inline bool classof(const Instruction *I) {
664 static inline bool classof(const Value *V) {
665 return isa<Instruction>(V) && classof(cast<Instruction>(V));
669 //===----------------------------------------------------------------------===//
671 //===----------------------------------------------------------------------===//
673 /// This class is the base class for the comparison instructions.
674 /// @brief Abstract base class of comparison instructions.
675 class CmpInst : public Instruction {
676 void *operator new(size_t, unsigned) = delete;
679 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
680 Value *LHS, Value *RHS, const Twine &Name = "",
681 Instruction *InsertBefore = nullptr);
683 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
684 Value *LHS, Value *RHS, const Twine &Name,
685 BasicBlock *InsertAtEnd);
687 void anchor() override; // Out of line virtual method.
689 /// This enumeration lists the possible predicates for CmpInst subclasses.
690 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
691 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
692 /// predicate values are not overlapping between the classes.
694 // Opcode U L G E Intuitive operation
695 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
696 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
697 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
698 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
699 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
700 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
701 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
702 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
703 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
704 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
705 FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
706 FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
707 FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
708 FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
709 FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
710 FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
711 FIRST_FCMP_PREDICATE = FCMP_FALSE,
712 LAST_FCMP_PREDICATE = FCMP_TRUE,
713 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
714 ICMP_EQ = 32, ///< equal
715 ICMP_NE = 33, ///< not equal
716 ICMP_UGT = 34, ///< unsigned greater than
717 ICMP_UGE = 35, ///< unsigned greater or equal
718 ICMP_ULT = 36, ///< unsigned less than
719 ICMP_ULE = 37, ///< unsigned less or equal
720 ICMP_SGT = 38, ///< signed greater than
721 ICMP_SGE = 39, ///< signed greater or equal
722 ICMP_SLT = 40, ///< signed less than
723 ICMP_SLE = 41, ///< signed less or equal
724 FIRST_ICMP_PREDICATE = ICMP_EQ,
725 LAST_ICMP_PREDICATE = ICMP_SLE,
726 BAD_ICMP_PREDICATE = ICMP_SLE + 1
729 // allocate space for exactly two operands
730 void *operator new(size_t s) {
731 return User::operator new(s, 2);
733 /// Construct a compare instruction, given the opcode, the predicate and
734 /// the two operands. Optionally (if InstBefore is specified) insert the
735 /// instruction into a BasicBlock right before the specified instruction.
736 /// The specified Instruction is allowed to be a dereferenced end iterator.
737 /// @brief Create a CmpInst
738 static CmpInst *Create(OtherOps Op,
739 unsigned short predicate, Value *S1,
740 Value *S2, const Twine &Name = "",
741 Instruction *InsertBefore = nullptr);
743 /// Construct a compare instruction, given the opcode, the predicate and the
744 /// two operands. Also automatically insert this instruction to the end of
745 /// the BasicBlock specified.
746 /// @brief Create a CmpInst
747 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
748 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
750 /// @brief Get the opcode casted to the right type
751 OtherOps getOpcode() const {
752 return static_cast<OtherOps>(Instruction::getOpcode());
755 /// @brief Return the predicate for this instruction.
756 Predicate getPredicate() const {
757 return Predicate(getSubclassDataFromInstruction());
760 /// @brief Set the predicate for this instruction to the specified value.
761 void setPredicate(Predicate P) { setInstructionSubclassData(P); }
763 static bool isFPPredicate(Predicate P) {
764 return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
767 static bool isIntPredicate(Predicate P) {
768 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
771 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
772 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
775 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
776 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
777 /// @returns the inverse predicate for the instruction's current predicate.
778 /// @brief Return the inverse of the instruction's predicate.
779 Predicate getInversePredicate() const {
780 return getInversePredicate(getPredicate());
783 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
784 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
785 /// @returns the inverse predicate for predicate provided in \p pred.
786 /// @brief Return the inverse of a given predicate
787 static Predicate getInversePredicate(Predicate pred);
789 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
790 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
791 /// @returns the predicate that would be the result of exchanging the two
792 /// operands of the CmpInst instruction without changing the result
794 /// @brief Return the predicate as if the operands were swapped
795 Predicate getSwappedPredicate() const {
796 return getSwappedPredicate(getPredicate());
799 /// This is a static version that you can use without an instruction
801 /// @brief Return the predicate as if the operands were swapped.
802 static Predicate getSwappedPredicate(Predicate pred);
804 /// @brief Provide more efficient getOperand methods.
805 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
807 /// This is just a convenience that dispatches to the subclasses.
808 /// @brief Swap the operands and adjust predicate accordingly to retain
809 /// the same comparison.
812 /// This is just a convenience that dispatches to the subclasses.
813 /// @brief Determine if this CmpInst is commutative.
814 bool isCommutative() const;
816 /// This is just a convenience that dispatches to the subclasses.
817 /// @brief Determine if this is an equals/not equals predicate.
818 bool isEquality() const;
820 /// @returns true if the comparison is signed, false otherwise.
821 /// @brief Determine if this instruction is using a signed comparison.
822 bool isSigned() const {
823 return isSigned(getPredicate());
826 /// @returns true if the comparison is unsigned, false otherwise.
827 /// @brief Determine if this instruction is using an unsigned comparison.
828 bool isUnsigned() const {
829 return isUnsigned(getPredicate());
832 /// This is just a convenience.
833 /// @brief Determine if this is true when both operands are the same.
834 bool isTrueWhenEqual() const {
835 return isTrueWhenEqual(getPredicate());
838 /// This is just a convenience.
839 /// @brief Determine if this is false when both operands are the same.
840 bool isFalseWhenEqual() const {
841 return isFalseWhenEqual(getPredicate());
844 /// @returns true if the predicate is unsigned, false otherwise.
845 /// @brief Determine if the predicate is an unsigned operation.
846 static bool isUnsigned(unsigned short predicate);
848 /// @returns true if the predicate is signed, false otherwise.
849 /// @brief Determine if the predicate is an signed operation.
850 static bool isSigned(unsigned short predicate);
852 /// @brief Determine if the predicate is an ordered operation.
853 static bool isOrdered(unsigned short predicate);
855 /// @brief Determine if the predicate is an unordered operation.
856 static bool isUnordered(unsigned short predicate);
858 /// Determine if the predicate is true when comparing a value with itself.
859 static bool isTrueWhenEqual(unsigned short predicate);
861 /// Determine if the predicate is false when comparing a value with itself.
862 static bool isFalseWhenEqual(unsigned short predicate);
864 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
865 static inline bool classof(const Instruction *I) {
866 return I->getOpcode() == Instruction::ICmp ||
867 I->getOpcode() == Instruction::FCmp;
869 static inline bool classof(const Value *V) {
870 return isa<Instruction>(V) && classof(cast<Instruction>(V));
873 /// @brief Create a result type for fcmp/icmp
874 static Type* makeCmpResultType(Type* opnd_type) {
875 if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
876 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
877 vt->getNumElements());
879 return Type::getInt1Ty(opnd_type->getContext());
882 // Shadow Value::setValueSubclassData with a private forwarding method so that
883 // subclasses cannot accidentally use it.
884 void setValueSubclassData(unsigned short D) {
885 Value::setValueSubclassData(D);
890 // FIXME: these are redundant if CmpInst < BinaryOperator
892 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
895 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
897 } // End llvm namespace