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.
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) LLVM_DELETED_FUNCTION;
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.
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) LLVM_DELETED_FUNCTION;
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 ZExt, BitCast, or Trunc for int -> int casts.
493 static CastInst *CreateIntegerCast(
494 Value *S, ///< The pointer value to be casted (operand 0)
495 Type *Ty, ///< The type to which cast should be made
496 bool isSigned, ///< Whether to regard S as signed or not
497 const Twine &Name = "", ///< Name for the instruction
498 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
501 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
502 static CastInst *CreateIntegerCast(
503 Value *S, ///< The integer value to be casted (operand 0)
504 Type *Ty, ///< The integer type to which operand is casted
505 bool isSigned, ///< Whether to regard S as signed or not
506 const Twine &Name, ///< The name for the instruction
507 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
510 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
511 static CastInst *CreateFPCast(
512 Value *S, ///< The floating point value to be casted
513 Type *Ty, ///< The floating point type to cast to
514 const Twine &Name = "", ///< Name for the instruction
515 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
518 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
519 static CastInst *CreateFPCast(
520 Value *S, ///< The floating point value to be casted
521 Type *Ty, ///< The floating point type to cast to
522 const Twine &Name, ///< The name for the instruction
523 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
526 /// @brief Create a Trunc or BitCast cast instruction
527 static CastInst *CreateTruncOrBitCast(
528 Value *S, ///< The value to be casted (operand 0)
529 Type *Ty, ///< The type to which cast should be made
530 const Twine &Name = "", ///< Name for the instruction
531 Instruction *InsertBefore = nullptr ///< Place to insert the instruction
534 /// @brief Create a Trunc or BitCast cast instruction
535 static CastInst *CreateTruncOrBitCast(
536 Value *S, ///< The value to be casted (operand 0)
537 Type *Ty, ///< The type to which operand is casted
538 const Twine &Name, ///< The name for the instruction
539 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
542 /// @brief Check whether it is valid to call getCastOpcode for these types.
543 static bool isCastable(
544 Type *SrcTy, ///< The Type from which the value should be cast.
545 Type *DestTy ///< The Type to which the value should be cast.
548 /// @brief Check whether a bitcast between these types is valid
549 static bool isBitCastable(
550 Type *SrcTy, ///< The Type from which the value should be cast.
551 Type *DestTy ///< The Type to which the value should be cast.
554 /// Returns the opcode necessary to cast Val into Ty using usual casting
556 /// @brief Infer the opcode for cast operand and type
557 static Instruction::CastOps getCastOpcode(
558 const Value *Val, ///< The value to cast
559 bool SrcIsSigned, ///< Whether to treat the source as signed
560 Type *Ty, ///< The Type to which the value should be casted
561 bool DstIsSigned ///< Whether to treate the dest. as signed
564 /// There are several places where we need to know if a cast instruction
565 /// only deals with integer source and destination types. To simplify that
566 /// logic, this method is provided.
567 /// @returns true iff the cast has only integral typed operand and dest type.
568 /// @brief Determine if this is an integer-only cast.
569 bool isIntegerCast() const;
571 /// A lossless cast is one that does not alter the basic value. It implies
572 /// a no-op cast but is more stringent, preventing things like int->float,
573 /// long->double, or int->ptr.
574 /// @returns true iff the cast is lossless.
575 /// @brief Determine if this is a lossless cast.
576 bool isLosslessCast() const;
578 /// A no-op cast is one that can be effected without changing any bits.
579 /// It implies that the source and destination types are the same size. The
580 /// IntPtrTy argument is used to make accurate determinations for casts
581 /// involving Integer and Pointer types. They are no-op casts if the integer
582 /// is the same size as the pointer. However, pointer size varies with
583 /// platform. Generally, the result of DataLayout::getIntPtrType() should be
584 /// passed in. If that's not available, use Type::Int64Ty, which will make
585 /// the isNoopCast call conservative.
586 /// @brief Determine if the described cast is a no-op cast.
587 static bool isNoopCast(
588 Instruction::CastOps Opcode, ///< Opcode of cast
589 Type *SrcTy, ///< SrcTy of cast
590 Type *DstTy, ///< DstTy of cast
591 Type *IntPtrTy ///< Integer type corresponding to Ptr types
594 /// @brief Determine if this cast is a no-op cast.
596 Type *IntPtrTy ///< Integer type corresponding to pointer
599 /// @brief Determine if this cast is a no-op cast.
601 const DataLayout *DL ///< DataLayout to get the Int Ptr type from.
604 /// Determine how a pair of casts can be eliminated, if they can be at all.
605 /// This is a helper function for both CastInst and ConstantExpr.
606 /// @returns 0 if the CastInst pair can't be eliminated, otherwise
607 /// returns Instruction::CastOps value for a cast that can replace
608 /// the pair, casting SrcTy to DstTy.
609 /// @brief Determine if a cast pair is eliminable
610 static unsigned isEliminableCastPair(
611 Instruction::CastOps firstOpcode, ///< Opcode of first cast
612 Instruction::CastOps secondOpcode, ///< Opcode of second cast
613 Type *SrcTy, ///< SrcTy of 1st cast
614 Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
615 Type *DstTy, ///< DstTy of 2nd cast
616 Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
617 Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
618 Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null
621 /// @brief Return the opcode of this CastInst
622 Instruction::CastOps getOpcode() const {
623 return Instruction::CastOps(Instruction::getOpcode());
626 /// @brief Return the source type, as a convenience
627 Type* getSrcTy() const { return getOperand(0)->getType(); }
628 /// @brief Return the destination type, as a convenience
629 Type* getDestTy() const { return getType(); }
631 /// This method can be used to determine if a cast from S to DstTy using
632 /// Opcode op is valid or not.
633 /// @returns true iff the proposed cast is valid.
634 /// @brief Determine if a cast is valid without creating one.
635 static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
637 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
638 static inline bool classof(const Instruction *I) {
641 static inline bool classof(const Value *V) {
642 return isa<Instruction>(V) && classof(cast<Instruction>(V));
646 //===----------------------------------------------------------------------===//
648 //===----------------------------------------------------------------------===//
650 /// This class is the base class for the comparison instructions.
651 /// @brief Abstract base class of comparison instructions.
652 class CmpInst : public Instruction {
653 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
654 CmpInst() LLVM_DELETED_FUNCTION;
656 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
657 Value *LHS, Value *RHS, const Twine &Name = "",
658 Instruction *InsertBefore = nullptr);
660 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
661 Value *LHS, Value *RHS, const Twine &Name,
662 BasicBlock *InsertAtEnd);
664 void anchor() override; // Out of line virtual method.
666 /// This enumeration lists the possible predicates for CmpInst subclasses.
667 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
668 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
669 /// predicate values are not overlapping between the classes.
671 // Opcode U L G E Intuitive operation
672 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
673 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
674 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
675 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
676 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
677 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
678 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
679 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
680 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
681 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
682 FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
683 FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
684 FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
685 FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
686 FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
687 FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
688 FIRST_FCMP_PREDICATE = FCMP_FALSE,
689 LAST_FCMP_PREDICATE = FCMP_TRUE,
690 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
691 ICMP_EQ = 32, ///< equal
692 ICMP_NE = 33, ///< not equal
693 ICMP_UGT = 34, ///< unsigned greater than
694 ICMP_UGE = 35, ///< unsigned greater or equal
695 ICMP_ULT = 36, ///< unsigned less than
696 ICMP_ULE = 37, ///< unsigned less or equal
697 ICMP_SGT = 38, ///< signed greater than
698 ICMP_SGE = 39, ///< signed greater or equal
699 ICMP_SLT = 40, ///< signed less than
700 ICMP_SLE = 41, ///< signed less or equal
701 FIRST_ICMP_PREDICATE = ICMP_EQ,
702 LAST_ICMP_PREDICATE = ICMP_SLE,
703 BAD_ICMP_PREDICATE = ICMP_SLE + 1
706 // allocate space for exactly two operands
707 void *operator new(size_t s) {
708 return User::operator new(s, 2);
710 /// Construct a compare instruction, given the opcode, the predicate and
711 /// the two operands. Optionally (if InstBefore is specified) insert the
712 /// instruction into a BasicBlock right before the specified instruction.
713 /// The specified Instruction is allowed to be a dereferenced end iterator.
714 /// @brief Create a CmpInst
715 static CmpInst *Create(OtherOps Op,
716 unsigned short predicate, Value *S1,
717 Value *S2, const Twine &Name = "",
718 Instruction *InsertBefore = nullptr);
720 /// Construct a compare instruction, given the opcode, the predicate and the
721 /// two operands. Also automatically insert this instruction to the end of
722 /// the BasicBlock specified.
723 /// @brief Create a CmpInst
724 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
725 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
727 /// @brief Get the opcode casted to the right type
728 OtherOps getOpcode() const {
729 return static_cast<OtherOps>(Instruction::getOpcode());
732 /// @brief Return the predicate for this instruction.
733 Predicate getPredicate() const {
734 return Predicate(getSubclassDataFromInstruction());
737 /// @brief Set the predicate for this instruction to the specified value.
738 void setPredicate(Predicate P) { setInstructionSubclassData(P); }
740 static bool isFPPredicate(Predicate P) {
741 return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
744 static bool isIntPredicate(Predicate P) {
745 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
748 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
749 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
752 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
753 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
754 /// @returns the inverse predicate for the instruction's current predicate.
755 /// @brief Return the inverse of the instruction's predicate.
756 Predicate getInversePredicate() const {
757 return getInversePredicate(getPredicate());
760 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
761 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
762 /// @returns the inverse predicate for predicate provided in \p pred.
763 /// @brief Return the inverse of a given predicate
764 static Predicate getInversePredicate(Predicate pred);
766 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
767 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
768 /// @returns the predicate that would be the result of exchanging the two
769 /// operands of the CmpInst instruction without changing the result
771 /// @brief Return the predicate as if the operands were swapped
772 Predicate getSwappedPredicate() const {
773 return getSwappedPredicate(getPredicate());
776 /// This is a static version that you can use without an instruction
778 /// @brief Return the predicate as if the operands were swapped.
779 static Predicate getSwappedPredicate(Predicate pred);
781 /// @brief Provide more efficient getOperand methods.
782 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
784 /// This is just a convenience that dispatches to the subclasses.
785 /// @brief Swap the operands and adjust predicate accordingly to retain
786 /// the same comparison.
789 /// This is just a convenience that dispatches to the subclasses.
790 /// @brief Determine if this CmpInst is commutative.
791 bool isCommutative() const;
793 /// This is just a convenience that dispatches to the subclasses.
794 /// @brief Determine if this is an equals/not equals predicate.
795 bool isEquality() const;
797 /// @returns true if the comparison is signed, false otherwise.
798 /// @brief Determine if this instruction is using a signed comparison.
799 bool isSigned() const {
800 return isSigned(getPredicate());
803 /// @returns true if the comparison is unsigned, false otherwise.
804 /// @brief Determine if this instruction is using an unsigned comparison.
805 bool isUnsigned() const {
806 return isUnsigned(getPredicate());
809 /// This is just a convenience.
810 /// @brief Determine if this is true when both operands are the same.
811 bool isTrueWhenEqual() const {
812 return isTrueWhenEqual(getPredicate());
815 /// This is just a convenience.
816 /// @brief Determine if this is false when both operands are the same.
817 bool isFalseWhenEqual() const {
818 return isFalseWhenEqual(getPredicate());
821 /// @returns true if the predicate is unsigned, false otherwise.
822 /// @brief Determine if the predicate is an unsigned operation.
823 static bool isUnsigned(unsigned short predicate);
825 /// @returns true if the predicate is signed, false otherwise.
826 /// @brief Determine if the predicate is an signed operation.
827 static bool isSigned(unsigned short predicate);
829 /// @brief Determine if the predicate is an ordered operation.
830 static bool isOrdered(unsigned short predicate);
832 /// @brief Determine if the predicate is an unordered operation.
833 static bool isUnordered(unsigned short predicate);
835 /// Determine if the predicate is true when comparing a value with itself.
836 static bool isTrueWhenEqual(unsigned short predicate);
838 /// Determine if the predicate is false when comparing a value with itself.
839 static bool isFalseWhenEqual(unsigned short predicate);
841 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
842 static inline bool classof(const Instruction *I) {
843 return I->getOpcode() == Instruction::ICmp ||
844 I->getOpcode() == Instruction::FCmp;
846 static inline bool classof(const Value *V) {
847 return isa<Instruction>(V) && classof(cast<Instruction>(V));
850 /// @brief Create a result type for fcmp/icmp
851 static Type* makeCmpResultType(Type* opnd_type) {
852 if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
853 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
854 vt->getNumElements());
856 return Type::getInt1Ty(opnd_type->getContext());
859 // Shadow Value::setValueSubclassData with a private forwarding method so that
860 // subclasses cannot accidentally use it.
861 void setValueSubclassData(unsigned short D) {
862 Value::setValueSubclassData(D);
867 // FIXME: these are redundant if CmpInst < BinaryOperator
869 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
872 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
874 } // End llvm namespace