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