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);
143 // Note: Instruction needs to be a friend here to call cloneImpl.
144 friend class Instruction;
145 BinaryOperator *cloneImpl() const;
148 // allocate space for exactly two operands
149 void *operator new(size_t s) {
150 return User::operator new(s, 2);
153 /// Transparently provide more efficient getOperand methods.
154 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
156 /// Construct a binary instruction, given the opcode and the two
157 /// operands. Optionally (if InstBefore is specified) insert the instruction
158 /// into a BasicBlock right before the specified instruction. The specified
159 /// Instruction is allowed to be a dereferenced end iterator.
161 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
162 const Twine &Name = Twine(),
163 Instruction *InsertBefore = nullptr);
165 /// Construct a binary instruction, given the opcode and the two
166 /// operands. Also automatically insert this instruction to the end of the
167 /// BasicBlock specified.
169 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
170 const Twine &Name, BasicBlock *InsertAtEnd);
172 /// These methods just forward to Create, and are useful when you
173 /// statically know what type of instruction you're going to create. These
174 /// helpers just save some typing.
175 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
176 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
177 const Twine &Name = "") {\
178 return Create(Instruction::OPC, V1, V2, Name);\
180 #include "llvm/IR/Instruction.def"
181 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
182 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
183 const Twine &Name, BasicBlock *BB) {\
184 return Create(Instruction::OPC, V1, V2, Name, BB);\
186 #include "llvm/IR/Instruction.def"
187 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
188 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
189 const Twine &Name, Instruction *I) {\
190 return Create(Instruction::OPC, V1, V2, Name, I);\
192 #include "llvm/IR/Instruction.def"
194 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
195 const Twine &Name = "") {
196 BinaryOperator *BO = Create(Opc, V1, V2, Name);
197 BO->setHasNoSignedWrap(true);
200 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
201 const Twine &Name, BasicBlock *BB) {
202 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
203 BO->setHasNoSignedWrap(true);
206 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
207 const Twine &Name, Instruction *I) {
208 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
209 BO->setHasNoSignedWrap(true);
213 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
214 const Twine &Name = "") {
215 BinaryOperator *BO = Create(Opc, V1, V2, Name);
216 BO->setHasNoUnsignedWrap(true);
219 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
220 const Twine &Name, BasicBlock *BB) {
221 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
222 BO->setHasNoUnsignedWrap(true);
225 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
226 const Twine &Name, Instruction *I) {
227 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
228 BO->setHasNoUnsignedWrap(true);
232 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
233 const Twine &Name = "") {
234 BinaryOperator *BO = Create(Opc, V1, V2, Name);
235 BO->setIsExact(true);
238 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
239 const Twine &Name, BasicBlock *BB) {
240 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
241 BO->setIsExact(true);
244 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
245 const Twine &Name, Instruction *I) {
246 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
247 BO->setIsExact(true);
251 #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
252 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
253 (Value *V1, Value *V2, const Twine &Name = "") { \
254 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \
256 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
257 (Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \
258 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \
260 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
261 (Value *V1, Value *V2, const Twine &Name, Instruction *I) { \
262 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \
265 DEFINE_HELPERS(Add, NSW) // CreateNSWAdd
266 DEFINE_HELPERS(Add, NUW) // CreateNUWAdd
267 DEFINE_HELPERS(Sub, NSW) // CreateNSWSub
268 DEFINE_HELPERS(Sub, NUW) // CreateNUWSub
269 DEFINE_HELPERS(Mul, NSW) // CreateNSWMul
270 DEFINE_HELPERS(Mul, NUW) // CreateNUWMul
271 DEFINE_HELPERS(Shl, NSW) // CreateNSWShl
272 DEFINE_HELPERS(Shl, NUW) // CreateNUWShl
274 DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv
275 DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv
276 DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
277 DEFINE_HELPERS(LShr, Exact) // CreateExactLShr
279 #undef DEFINE_HELPERS
281 /// Helper functions to construct and inspect unary operations (NEG and NOT)
282 /// via binary operators SUB and XOR:
284 /// Create the NEG and NOT instructions out of SUB and XOR instructions.
286 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
287 Instruction *InsertBefore = nullptr);
288 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
289 BasicBlock *InsertAtEnd);
290 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
291 Instruction *InsertBefore = nullptr);
292 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
293 BasicBlock *InsertAtEnd);
294 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
295 Instruction *InsertBefore = nullptr);
296 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
297 BasicBlock *InsertAtEnd);
298 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
299 Instruction *InsertBefore = nullptr);
300 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
301 BasicBlock *InsertAtEnd);
302 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
303 Instruction *InsertBefore = nullptr);
304 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
305 BasicBlock *InsertAtEnd);
307 /// Check if the given Value is a NEG, FNeg, or NOT instruction.
309 static bool isNeg(const Value *V);
310 static bool isFNeg(const Value *V, bool IgnoreZeroSign=false);
311 static bool isNot(const Value *V);
313 /// Helper functions to extract the unary argument of a NEG, FNEG or NOT
314 /// operation implemented via Sub, FSub, or Xor.
316 static const Value *getNegArgument(const Value *BinOp);
317 static Value *getNegArgument( Value *BinOp);
318 static const Value *getFNegArgument(const Value *BinOp);
319 static Value *getFNegArgument( Value *BinOp);
320 static const Value *getNotArgument(const Value *BinOp);
321 static Value *getNotArgument( Value *BinOp);
323 BinaryOps getOpcode() const {
324 return static_cast<BinaryOps>(Instruction::getOpcode());
327 /// Exchange the two operands to this instruction.
328 /// This instruction is safe to use on any binary instruction and
329 /// does not modify the semantics of the instruction. If the instruction
330 /// cannot be reversed (ie, it's a Div), then return 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 setHasNoUnsignedWrap(bool b = true);
338 /// Set or clear the nsw 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 setHasNoSignedWrap(bool b = true);
342 /// Set or clear the exact flag on this instruction, which must be an operator
343 /// which supports this flag. See LangRef.html for the meaning of this flag.
344 void setIsExact(bool b = true);
346 /// Determine whether the no unsigned wrap flag is set.
347 bool hasNoUnsignedWrap() const;
349 /// Determine whether the no signed wrap flag is set.
350 bool hasNoSignedWrap() const;
352 /// Determine whether the exact flag is set.
353 bool isExact() const;
355 /// Convenience method to copy supported wrapping, exact, and fast-math flags
356 /// from V to this instruction.
357 void copyIRFlags(const Value *V);
359 /// Logical 'and' of any supported wrapping, exact, and fast-math flags of
360 /// V and this instruction.
361 void andIRFlags(const Value *V);
363 // Methods for support type inquiry through isa, cast, and dyn_cast:
364 static inline bool classof(const Instruction *I) {
365 return I->isBinaryOp();
367 static inline bool classof(const Value *V) {
368 return isa<Instruction>(V) && classof(cast<Instruction>(V));
373 struct OperandTraits<BinaryOperator> :
374 public FixedNumOperandTraits<BinaryOperator, 2> {
377 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
379 //===----------------------------------------------------------------------===//
381 //===----------------------------------------------------------------------===//
383 /// This is the base class for all instructions that perform data
384 /// casts. It is simply provided so that instruction category testing
385 /// can be performed with code like:
387 /// if (isa<CastInst>(Instr)) { ... }
388 /// @brief Base class of casting instructions.
389 class CastInst : public UnaryInstruction {
390 void anchor() override;
392 /// @brief Constructor with insert-before-instruction semantics for subclasses
393 CastInst(Type *Ty, unsigned iType, Value *S,
394 const Twine &NameStr = "", Instruction *InsertBefore = nullptr)
395 : UnaryInstruction(Ty, iType, S, InsertBefore) {
398 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
399 CastInst(Type *Ty, unsigned iType, Value *S,
400 const Twine &NameStr, BasicBlock *InsertAtEnd)
401 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
405 /// Provides a way to construct any of the CastInst subclasses using an
406 /// opcode instead of the subclass's constructor. The opcode must be in the
407 /// CastOps category (Instruction::isCast(opcode) returns true). This
408 /// constructor has insert-before-instruction semantics to automatically
409 /// insert the new CastInst before InsertBefore (if it is non-null).
410 /// @brief Construct any of the CastInst subclasses
411 static CastInst *Create(
412 Instruction::CastOps, ///< The opcode of the cast instruction
413 Value *S, ///< The value to be casted (operand 0)
414 Type *Ty, ///< The type to which cast should be made
415 const Twine &Name = "", ///< Name for the instruction
416 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
418 /// Provides a way to construct any of the CastInst subclasses using an
419 /// opcode instead of the subclass's constructor. The opcode must be in the
420 /// CastOps category. This constructor has insert-at-end-of-block semantics
421 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
423 /// @brief Construct any of the CastInst subclasses
424 static CastInst *Create(
425 Instruction::CastOps, ///< The opcode for the cast instruction
426 Value *S, ///< The value to be casted (operand 0)
427 Type *Ty, ///< The type to which operand is casted
428 const Twine &Name, ///< The name for the instruction
429 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
432 /// @brief Create a ZExt or BitCast cast instruction
433 static CastInst *CreateZExtOrBitCast(
434 Value *S, ///< The value to be casted (operand 0)
435 Type *Ty, ///< The type to which cast should be made
436 const Twine &Name = "", ///< Name for the instruction
437 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
440 /// @brief Create a ZExt or BitCast cast instruction
441 static CastInst *CreateZExtOrBitCast(
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 SExt or BitCast cast instruction
449 static CastInst *CreateSExtOrBitCast(
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 SExt or BitCast cast instruction
457 static CastInst *CreateSExtOrBitCast(
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 BitCast AddrSpaceCast, or a PtrToInt cast instruction.
465 static CastInst *CreatePointerCast(
466 Value *S, ///< The pointer value to be casted (operand 0)
467 Type *Ty, ///< The type to which operand is casted
468 const Twine &Name, ///< The name for the instruction
469 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
472 /// @brief Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
473 static CastInst *CreatePointerCast(
474 Value *S, ///< The pointer value to be casted (operand 0)
475 Type *Ty, ///< The type to which cast should be made
476 const Twine &Name = "", ///< Name for the instruction
477 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
480 /// @brief Create a BitCast or an AddrSpaceCast cast instruction.
481 static CastInst *CreatePointerBitCastOrAddrSpaceCast(
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 or an AddrSpaceCast cast instruction.
489 static CastInst *CreatePointerBitCastOrAddrSpaceCast(
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 = 0 ///< Place to insert the instruction
496 /// @brief Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
498 /// If the value is a pointer type and the destination an integer type,
499 /// creates a PtrToInt cast. If the value is an integer type and the
500 /// destination a pointer type, creates an IntToPtr cast. Otherwise, creates
502 static CastInst *CreateBitOrPointerCast(
503 Value *S, ///< The pointer value to be casted (operand 0)
504 Type *Ty, ///< The type to which cast should be made
505 const Twine &Name = "", ///< Name for the instruction
506 Instruction *InsertBefore = 0 ///< Place to insert the instruction
509 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
510 static CastInst *CreateIntegerCast(
511 Value *S, ///< The pointer value to be casted (operand 0)
512 Type *Ty, ///< The type to which cast should be made
513 bool isSigned, ///< Whether to regard S as signed or not
514 const Twine &Name = "", ///< Name for the instruction
515 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
518 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
519 static CastInst *CreateIntegerCast(
520 Value *S, ///< The integer value to be casted (operand 0)
521 Type *Ty, ///< The integer type to which operand is casted
522 bool isSigned, ///< Whether to regard S as signed or not
523 const Twine &Name, ///< The name for the instruction
524 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
527 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
528 static CastInst *CreateFPCast(
529 Value *S, ///< The floating point value to be casted
530 Type *Ty, ///< The floating point type to cast to
531 const Twine &Name = "", ///< Name for the instruction
532 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
535 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
536 static CastInst *CreateFPCast(
537 Value *S, ///< The floating point value to be casted
538 Type *Ty, ///< The floating point type to cast to
539 const Twine &Name, ///< The name for the instruction
540 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
543 /// @brief Create a Trunc or BitCast cast instruction
544 static CastInst *CreateTruncOrBitCast(
545 Value *S, ///< The value to be casted (operand 0)
546 Type *Ty, ///< The type to which cast should be made
547 const Twine &Name = "", ///< Name for the instruction
548 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
551 /// @brief Create a Trunc or BitCast cast instruction
552 static CastInst *CreateTruncOrBitCast(
553 Value *S, ///< The value to be casted (operand 0)
554 Type *Ty, ///< The type to which operand is casted
555 const Twine &Name, ///< The name for the instruction
556 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
559 /// @brief Check whether it is valid to call getCastOpcode for these types.
560 static bool isCastable(
561 Type *SrcTy, ///< The Type from which the value should be cast.
562 Type *DestTy ///< The Type to which the value should be cast.
565 /// @brief Check whether a bitcast between these types is valid
566 static bool isBitCastable(
567 Type *SrcTy, ///< The Type from which the value should be cast.
568 Type *DestTy ///< The Type to which the value should be cast.
571 /// @brief Check whether a bitcast, inttoptr, or ptrtoint cast between these
572 /// types is valid and a no-op.
574 /// This ensures that any pointer<->integer cast has enough bits in the
575 /// integer and any other cast is a bitcast.
576 static bool isBitOrNoopPointerCastable(
577 Type *SrcTy, ///< The Type from which the value should be cast.
578 Type *DestTy, ///< The Type to which the value should be cast.
579 const DataLayout &DL);
581 /// Returns the opcode necessary to cast Val into Ty using usual casting
583 /// @brief Infer the opcode for cast operand and type
584 static Instruction::CastOps getCastOpcode(
585 const Value *Val, ///< The value to cast
586 bool SrcIsSigned, ///< Whether to treat the source as signed
587 Type *Ty, ///< The Type to which the value should be casted
588 bool DstIsSigned ///< Whether to treate the dest. as signed
591 /// There are several places where we need to know if a cast instruction
592 /// only deals with integer source and destination types. To simplify that
593 /// logic, this method is provided.
594 /// @returns true iff the cast has only integral typed operand and dest type.
595 /// @brief Determine if this is an integer-only cast.
596 bool isIntegerCast() const;
598 /// A lossless cast is one that does not alter the basic value. It implies
599 /// a no-op cast but is more stringent, preventing things like int->float,
600 /// long->double, or int->ptr.
601 /// @returns true iff the cast is lossless.
602 /// @brief Determine if this is a lossless cast.
603 bool isLosslessCast() const;
605 /// A no-op cast is one that can be effected without changing any bits.
606 /// It implies that the source and destination types are the same size. The
607 /// IntPtrTy argument is used to make accurate determinations for casts
608 /// involving Integer and Pointer types. They are no-op casts if the integer
609 /// is the same size as the pointer. However, pointer size varies with
610 /// platform. Generally, the result of DataLayout::getIntPtrType() should be
611 /// passed in. If that's not available, use Type::Int64Ty, which will make
612 /// the isNoopCast call conservative.
613 /// @brief Determine if the described cast is a no-op cast.
614 static bool isNoopCast(
615 Instruction::CastOps Opcode, ///< Opcode of cast
616 Type *SrcTy, ///< SrcTy of cast
617 Type *DstTy, ///< DstTy of cast
618 Type *IntPtrTy ///< Integer type corresponding to Ptr types
621 /// @brief Determine if this cast is a no-op cast.
623 Type *IntPtrTy ///< Integer type corresponding to pointer
626 /// @brief Determine if this cast is a no-op cast.
628 /// \param DL is the DataLayout to get the Int Ptr type from.
629 bool isNoopCast(const DataLayout &DL) const;
631 /// Determine how a pair of casts can be eliminated, if they can be at all.
632 /// This is a helper function for both CastInst and ConstantExpr.
633 /// @returns 0 if the CastInst pair can't be eliminated, otherwise
634 /// returns Instruction::CastOps value for a cast that can replace
635 /// the pair, casting SrcTy to DstTy.
636 /// @brief Determine if a cast pair is eliminable
637 static unsigned isEliminableCastPair(
638 Instruction::CastOps firstOpcode, ///< Opcode of first cast
639 Instruction::CastOps secondOpcode, ///< Opcode of second cast
640 Type *SrcTy, ///< SrcTy of 1st cast
641 Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
642 Type *DstTy, ///< DstTy of 2nd cast
643 Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
644 Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
645 Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null
648 /// @brief Return the opcode of this CastInst
649 Instruction::CastOps getOpcode() const {
650 return Instruction::CastOps(Instruction::getOpcode());
653 /// @brief Return the source type, as a convenience
654 Type* getSrcTy() const { return getOperand(0)->getType(); }
655 /// @brief Return the destination type, as a convenience
656 Type* getDestTy() const { return getType(); }
658 /// This method can be used to determine if a cast from S to DstTy using
659 /// Opcode op is valid or not.
660 /// @returns true iff the proposed cast is valid.
661 /// @brief Determine if a cast is valid without creating one.
662 static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
664 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
665 static inline bool classof(const Instruction *I) {
668 static inline bool classof(const Value *V) {
669 return isa<Instruction>(V) && classof(cast<Instruction>(V));
673 //===----------------------------------------------------------------------===//
675 //===----------------------------------------------------------------------===//
677 /// This class is the base class for the comparison instructions.
678 /// @brief Abstract base class of comparison instructions.
679 class CmpInst : public Instruction {
680 void *operator new(size_t, unsigned) = delete;
683 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
684 Value *LHS, Value *RHS, const Twine &Name = "",
685 Instruction *InsertBefore = nullptr);
687 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
688 Value *LHS, Value *RHS, const Twine &Name,
689 BasicBlock *InsertAtEnd);
691 void anchor() override; // Out of line virtual method.
693 /// This enumeration lists the possible predicates for CmpInst subclasses.
694 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
695 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
696 /// predicate values are not overlapping between the classes.
698 // Opcode U L G E Intuitive operation
699 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
700 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
701 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
702 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
703 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
704 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
705 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
706 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
707 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
708 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
709 FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
710 FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
711 FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
712 FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
713 FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
714 FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
715 FIRST_FCMP_PREDICATE = FCMP_FALSE,
716 LAST_FCMP_PREDICATE = FCMP_TRUE,
717 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
718 ICMP_EQ = 32, ///< equal
719 ICMP_NE = 33, ///< not equal
720 ICMP_UGT = 34, ///< unsigned greater than
721 ICMP_UGE = 35, ///< unsigned greater or equal
722 ICMP_ULT = 36, ///< unsigned less than
723 ICMP_ULE = 37, ///< unsigned less or equal
724 ICMP_SGT = 38, ///< signed greater than
725 ICMP_SGE = 39, ///< signed greater or equal
726 ICMP_SLT = 40, ///< signed less than
727 ICMP_SLE = 41, ///< signed less or equal
728 FIRST_ICMP_PREDICATE = ICMP_EQ,
729 LAST_ICMP_PREDICATE = ICMP_SLE,
730 BAD_ICMP_PREDICATE = ICMP_SLE + 1
733 // allocate space for exactly two operands
734 void *operator new(size_t s) {
735 return User::operator new(s, 2);
737 /// Construct a compare instruction, given the opcode, the predicate and
738 /// the two operands. Optionally (if InstBefore is specified) insert the
739 /// instruction into a BasicBlock right before the specified instruction.
740 /// The specified Instruction is allowed to be a dereferenced end iterator.
741 /// @brief Create a CmpInst
742 static CmpInst *Create(OtherOps Op,
743 unsigned short predicate, Value *S1,
744 Value *S2, const Twine &Name = "",
745 Instruction *InsertBefore = nullptr);
747 /// Construct a compare instruction, given the opcode, the predicate and the
748 /// two operands. Also automatically insert this instruction to the end of
749 /// the BasicBlock specified.
750 /// @brief Create a CmpInst
751 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
752 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
754 /// @brief Get the opcode casted to the right type
755 OtherOps getOpcode() const {
756 return static_cast<OtherOps>(Instruction::getOpcode());
759 /// @brief Return the predicate for this instruction.
760 Predicate getPredicate() const {
761 return Predicate(getSubclassDataFromInstruction());
764 /// @brief Set the predicate for this instruction to the specified value.
765 void setPredicate(Predicate P) { setInstructionSubclassData(P); }
767 static bool isFPPredicate(Predicate P) {
768 return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
771 static bool isIntPredicate(Predicate P) {
772 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
775 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
776 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
779 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
780 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
781 /// @returns the inverse predicate for the instruction's current predicate.
782 /// @brief Return the inverse of the instruction's predicate.
783 Predicate getInversePredicate() const {
784 return getInversePredicate(getPredicate());
787 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
788 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
789 /// @returns the inverse predicate for predicate provided in \p pred.
790 /// @brief Return the inverse of a given predicate
791 static Predicate getInversePredicate(Predicate pred);
793 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
794 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
795 /// @returns the predicate that would be the result of exchanging the two
796 /// operands of the CmpInst instruction without changing the result
798 /// @brief Return the predicate as if the operands were swapped
799 Predicate getSwappedPredicate() const {
800 return getSwappedPredicate(getPredicate());
803 /// This is a static version that you can use without an instruction
805 /// @brief Return the predicate as if the operands were swapped.
806 static Predicate getSwappedPredicate(Predicate pred);
808 /// @brief Provide more efficient getOperand methods.
809 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
811 /// This is just a convenience that dispatches to the subclasses.
812 /// @brief Swap the operands and adjust predicate accordingly to retain
813 /// the same comparison.
816 /// This is just a convenience that dispatches to the subclasses.
817 /// @brief Determine if this CmpInst is commutative.
818 bool isCommutative() const;
820 /// This is just a convenience that dispatches to the subclasses.
821 /// @brief Determine if this is an equals/not equals predicate.
822 bool isEquality() const;
824 /// @returns true if the comparison is signed, false otherwise.
825 /// @brief Determine if this instruction is using a signed comparison.
826 bool isSigned() const {
827 return isSigned(getPredicate());
830 /// @returns true if the comparison is unsigned, false otherwise.
831 /// @brief Determine if this instruction is using an unsigned comparison.
832 bool isUnsigned() const {
833 return isUnsigned(getPredicate());
836 /// This is just a convenience.
837 /// @brief Determine if this is true when both operands are the same.
838 bool isTrueWhenEqual() const {
839 return isTrueWhenEqual(getPredicate());
842 /// This is just a convenience.
843 /// @brief Determine if this is false when both operands are the same.
844 bool isFalseWhenEqual() const {
845 return isFalseWhenEqual(getPredicate());
848 /// @returns true if the predicate is unsigned, false otherwise.
849 /// @brief Determine if the predicate is an unsigned operation.
850 static bool isUnsigned(unsigned short predicate);
852 /// @returns true if the predicate is signed, false otherwise.
853 /// @brief Determine if the predicate is an signed operation.
854 static bool isSigned(unsigned short predicate);
856 /// @brief Determine if the predicate is an ordered operation.
857 static bool isOrdered(unsigned short predicate);
859 /// @brief Determine if the predicate is an unordered operation.
860 static bool isUnordered(unsigned short predicate);
862 /// Determine if the predicate is true when comparing a value with itself.
863 static bool isTrueWhenEqual(unsigned short predicate);
865 /// Determine if the predicate is false when comparing a value with itself.
866 static bool isFalseWhenEqual(unsigned short predicate);
868 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
869 static inline bool classof(const Instruction *I) {
870 return I->getOpcode() == Instruction::ICmp ||
871 I->getOpcode() == Instruction::FCmp;
873 static inline bool classof(const Value *V) {
874 return isa<Instruction>(V) && classof(cast<Instruction>(V));
877 /// @brief Create a result type for fcmp/icmp
878 static Type* makeCmpResultType(Type* opnd_type) {
879 if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
880 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
881 vt->getNumElements());
883 return Type::getInt1Ty(opnd_type->getContext());
886 // Shadow Value::setValueSubclassData with a private forwarding method so that
887 // subclasses cannot accidentally use it.
888 void setValueSubclassData(unsigned short D) {
889 Value::setValueSubclassData(D);
894 // FIXME: these are redundant if CmpInst < BinaryOperator
896 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
899 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
901 } // End llvm namespace