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> :
132 public FixedNumOperandTraits<UnaryInstruction, 1> {
135 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
137 //===----------------------------------------------------------------------===//
138 // BinaryOperator Class
139 //===----------------------------------------------------------------------===//
141 class BinaryOperator : public Instruction {
142 void *operator new(size_t, unsigned); // Do not implement
144 void init(BinaryOps iType);
145 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
146 const Twine &Name, Instruction *InsertBefore);
147 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
148 const Twine &Name, BasicBlock *InsertAtEnd);
149 virtual BinaryOperator *clone_impl() const;
151 // allocate space for exactly two operands
152 void *operator new(size_t s) {
153 return User::operator new(s, 2);
156 /// Transparently provide more efficient getOperand methods.
157 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
159 /// Create() - Construct a binary instruction, given the opcode and the two
160 /// operands. Optionally (if InstBefore is specified) insert the instruction
161 /// into a BasicBlock right before the specified instruction. The specified
162 /// Instruction is allowed to be a dereferenced end iterator.
164 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
165 const Twine &Name = Twine(),
166 Instruction *InsertBefore = 0);
168 /// Create() - Construct a binary instruction, given the opcode and the two
169 /// operands. Also automatically insert this instruction to the end of the
170 /// BasicBlock specified.
172 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
173 const Twine &Name, BasicBlock *InsertAtEnd);
175 /// Create* - These methods just forward to Create, and are useful when you
176 /// statically know what type of instruction you're going to create. These
177 /// helpers just save some typing.
178 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
179 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
180 const Twine &Name = "") {\
181 return Create(Instruction::OPC, V1, V2, Name);\
183 #include "llvm/Instruction.def"
184 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
185 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
186 const Twine &Name, BasicBlock *BB) {\
187 return Create(Instruction::OPC, V1, V2, Name, BB);\
189 #include "llvm/Instruction.def"
190 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
191 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
192 const Twine &Name, Instruction *I) {\
193 return Create(Instruction::OPC, V1, V2, Name, I);\
195 #include "llvm/Instruction.def"
198 /// CreateNSWAdd - Create an Add operator with the NSW flag set.
200 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
201 const Twine &Name = "") {
202 BinaryOperator *BO = CreateAdd(V1, V2, Name);
203 BO->setHasNoSignedWrap(true);
206 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
207 const Twine &Name, BasicBlock *BB) {
208 BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
209 BO->setHasNoSignedWrap(true);
212 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
213 const Twine &Name, Instruction *I) {
214 BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
215 BO->setHasNoSignedWrap(true);
219 /// CreateNUWAdd - Create an Add operator with the NUW flag set.
221 static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
222 const Twine &Name = "") {
223 BinaryOperator *BO = CreateAdd(V1, V2, Name);
224 BO->setHasNoUnsignedWrap(true);
227 static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
228 const Twine &Name, BasicBlock *BB) {
229 BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
230 BO->setHasNoUnsignedWrap(true);
233 static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
234 const Twine &Name, Instruction *I) {
235 BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
236 BO->setHasNoUnsignedWrap(true);
240 /// CreateNSWSub - Create an Sub operator with the NSW flag set.
242 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
243 const Twine &Name = "") {
244 BinaryOperator *BO = CreateSub(V1, V2, Name);
245 BO->setHasNoSignedWrap(true);
248 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
249 const Twine &Name, BasicBlock *BB) {
250 BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
251 BO->setHasNoSignedWrap(true);
254 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
255 const Twine &Name, Instruction *I) {
256 BinaryOperator *BO = CreateSub(V1, V2, Name, I);
257 BO->setHasNoSignedWrap(true);
261 /// CreateNUWSub - Create an Sub operator with the NUW flag set.
263 static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
264 const Twine &Name = "") {
265 BinaryOperator *BO = CreateSub(V1, V2, Name);
266 BO->setHasNoUnsignedWrap(true);
269 static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
270 const Twine &Name, BasicBlock *BB) {
271 BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
272 BO->setHasNoUnsignedWrap(true);
275 static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
276 const Twine &Name, Instruction *I) {
277 BinaryOperator *BO = CreateSub(V1, V2, Name, I);
278 BO->setHasNoUnsignedWrap(true);
282 /// CreateNSWMul - Create a Mul operator with the NSW flag set.
284 static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
285 const Twine &Name = "") {
286 BinaryOperator *BO = CreateMul(V1, V2, Name);
287 BO->setHasNoSignedWrap(true);
290 static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
291 const Twine &Name, BasicBlock *BB) {
292 BinaryOperator *BO = CreateMul(V1, V2, Name, BB);
293 BO->setHasNoSignedWrap(true);
296 static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
297 const Twine &Name, Instruction *I) {
298 BinaryOperator *BO = CreateMul(V1, V2, Name, I);
299 BO->setHasNoSignedWrap(true);
303 /// CreateNUWMul - Create a Mul operator with the NUW flag set.
305 static BinaryOperator *CreateNUWMul(Value *V1, Value *V2,
306 const Twine &Name = "") {
307 BinaryOperator *BO = CreateMul(V1, V2, Name);
308 BO->setHasNoUnsignedWrap(true);
311 static BinaryOperator *CreateNUWMul(Value *V1, Value *V2,
312 const Twine &Name, BasicBlock *BB) {
313 BinaryOperator *BO = CreateMul(V1, V2, Name, BB);
314 BO->setHasNoUnsignedWrap(true);
317 static BinaryOperator *CreateNUWMul(Value *V1, Value *V2,
318 const Twine &Name, Instruction *I) {
319 BinaryOperator *BO = CreateMul(V1, V2, Name, I);
320 BO->setHasNoUnsignedWrap(true);
324 /// CreateExactSDiv - Create an SDiv operator with the exact flag set.
326 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
327 const Twine &Name = "") {
328 BinaryOperator *BO = CreateSDiv(V1, V2, Name);
329 BO->setIsExact(true);
332 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
333 const Twine &Name, BasicBlock *BB) {
334 BinaryOperator *BO = CreateSDiv(V1, V2, Name, BB);
335 BO->setIsExact(true);
338 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
339 const Twine &Name, Instruction *I) {
340 BinaryOperator *BO = CreateSDiv(V1, V2, Name, I);
341 BO->setIsExact(true);
345 /// Helper functions to construct and inspect unary operations (NEG and NOT)
346 /// via binary operators SUB and XOR:
348 /// CreateNeg, CreateNot - Create the NEG and NOT
349 /// instructions out of SUB and XOR instructions.
351 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
352 Instruction *InsertBefore = 0);
353 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
354 BasicBlock *InsertAtEnd);
355 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
356 Instruction *InsertBefore = 0);
357 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
358 BasicBlock *InsertAtEnd);
359 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
360 Instruction *InsertBefore = 0);
361 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
362 BasicBlock *InsertAtEnd);
363 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
364 Instruction *InsertBefore = 0);
365 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
366 BasicBlock *InsertAtEnd);
367 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
368 Instruction *InsertBefore = 0);
369 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
370 BasicBlock *InsertAtEnd);
372 /// isNeg, isFNeg, isNot - Check if the given Value is a
373 /// NEG, FNeg, or NOT instruction.
375 static bool isNeg(const Value *V);
376 static bool isFNeg(const Value *V);
377 static bool isNot(const Value *V);
379 /// getNegArgument, getNotArgument - Helper functions to extract the
380 /// unary argument of a NEG, FNEG or NOT operation implemented via
381 /// Sub, FSub, or Xor.
383 static const Value *getNegArgument(const Value *BinOp);
384 static Value *getNegArgument( Value *BinOp);
385 static const Value *getFNegArgument(const Value *BinOp);
386 static Value *getFNegArgument( Value *BinOp);
387 static const Value *getNotArgument(const Value *BinOp);
388 static Value *getNotArgument( Value *BinOp);
390 BinaryOps getOpcode() const {
391 return static_cast<BinaryOps>(Instruction::getOpcode());
394 /// swapOperands - Exchange the two operands to this instruction.
395 /// This instruction is safe to use on any binary instruction and
396 /// does not modify the semantics of the instruction. If the instruction
397 /// cannot be reversed (ie, it's a Div), then return true.
401 /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
402 /// which must be an operator which supports this flag. See LangRef.html
403 /// for the meaning of this flag.
404 void setHasNoUnsignedWrap(bool b = true);
406 /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
407 /// which must be an operator which supports this flag. See LangRef.html
408 /// for the meaning of this flag.
409 void setHasNoSignedWrap(bool b = true);
411 /// setIsExact - Set or clear the exact flag on this instruction,
412 /// which must be an operator which supports this flag. See LangRef.html
413 /// for the meaning of this flag.
414 void setIsExact(bool b = true);
416 /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
417 bool hasNoUnsignedWrap() const;
419 /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
420 bool hasNoSignedWrap() const;
422 /// isExact - Determine whether the exact flag is set.
423 bool isExact() const;
425 // Methods for support type inquiry through isa, cast, and dyn_cast:
426 static inline bool classof(const BinaryOperator *) { return true; }
427 static inline bool classof(const Instruction *I) {
428 return I->isBinaryOp();
430 static inline bool classof(const Value *V) {
431 return isa<Instruction>(V) && classof(cast<Instruction>(V));
436 struct OperandTraits<BinaryOperator> :
437 public FixedNumOperandTraits<BinaryOperator, 2> {
440 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
442 //===----------------------------------------------------------------------===//
444 //===----------------------------------------------------------------------===//
446 /// CastInst - This is the base class for all instructions that perform data
447 /// casts. It is simply provided so that instruction category testing
448 /// can be performed with code like:
450 /// if (isa<CastInst>(Instr)) { ... }
451 /// @brief Base class of casting instructions.
452 class CastInst : public UnaryInstruction {
454 /// @brief Constructor with insert-before-instruction semantics for subclasses
455 CastInst(const Type *Ty, unsigned iType, Value *S,
456 const Twine &NameStr = "", Instruction *InsertBefore = 0)
457 : UnaryInstruction(Ty, iType, S, InsertBefore) {
460 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
461 CastInst(const Type *Ty, unsigned iType, Value *S,
462 const Twine &NameStr, BasicBlock *InsertAtEnd)
463 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
467 /// Provides a way to construct any of the CastInst subclasses using an
468 /// opcode instead of the subclass's constructor. The opcode must be in the
469 /// CastOps category (Instruction::isCast(opcode) returns true). This
470 /// constructor has insert-before-instruction semantics to automatically
471 /// insert the new CastInst before InsertBefore (if it is non-null).
472 /// @brief Construct any of the CastInst subclasses
473 static CastInst *Create(
474 Instruction::CastOps, ///< The opcode of the cast instruction
475 Value *S, ///< The value to be casted (operand 0)
476 const Type *Ty, ///< The type to which cast should be made
477 const Twine &Name = "", ///< Name for the instruction
478 Instruction *InsertBefore = 0 ///< Place to insert the instruction
480 /// Provides a way to construct any of the CastInst subclasses using an
481 /// opcode instead of the subclass's constructor. The opcode must be in the
482 /// CastOps category. This constructor has insert-at-end-of-block semantics
483 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
485 /// @brief Construct any of the CastInst subclasses
486 static CastInst *Create(
487 Instruction::CastOps, ///< The opcode for the cast instruction
488 Value *S, ///< The value to be casted (operand 0)
489 const Type *Ty, ///< The type to which operand is casted
490 const Twine &Name, ///< The name for the instruction
491 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
494 /// @brief Create a ZExt or BitCast cast instruction
495 static CastInst *CreateZExtOrBitCast(
496 Value *S, ///< The value to be casted (operand 0)
497 const Type *Ty, ///< The type to which cast should be made
498 const Twine &Name = "", ///< Name for the instruction
499 Instruction *InsertBefore = 0 ///< Place to insert the instruction
502 /// @brief Create a ZExt or BitCast cast instruction
503 static CastInst *CreateZExtOrBitCast(
504 Value *S, ///< The value to be casted (operand 0)
505 const Type *Ty, ///< The type to which operand is casted
506 const Twine &Name, ///< The name for the instruction
507 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
510 /// @brief Create a SExt or BitCast cast instruction
511 static CastInst *CreateSExtOrBitCast(
512 Value *S, ///< The value to be casted (operand 0)
513 const Type *Ty, ///< The type to which cast should be made
514 const Twine &Name = "", ///< Name for the instruction
515 Instruction *InsertBefore = 0 ///< Place to insert the instruction
518 /// @brief Create a SExt or BitCast cast instruction
519 static CastInst *CreateSExtOrBitCast(
520 Value *S, ///< The value to be casted (operand 0)
521 const Type *Ty, ///< The type to which operand is casted
522 const Twine &Name, ///< The name for the instruction
523 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
526 /// @brief Create a BitCast or a PtrToInt cast instruction
527 static CastInst *CreatePointerCast(
528 Value *S, ///< The pointer value to be casted (operand 0)
529 const Type *Ty, ///< The type to which operand is casted
530 const Twine &Name, ///< The name for the instruction
531 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
534 /// @brief Create a BitCast or a PtrToInt cast instruction
535 static CastInst *CreatePointerCast(
536 Value *S, ///< The pointer value to be casted (operand 0)
537 const Type *Ty, ///< The type to which cast should be made
538 const Twine &Name = "", ///< Name for the instruction
539 Instruction *InsertBefore = 0 ///< Place to insert the instruction
542 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
543 static CastInst *CreateIntegerCast(
544 Value *S, ///< The pointer value to be casted (operand 0)
545 const Type *Ty, ///< The type to which cast should be made
546 bool isSigned, ///< Whether to regard S as signed or not
547 const Twine &Name = "", ///< Name for the instruction
548 Instruction *InsertBefore = 0 ///< Place to insert the instruction
551 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
552 static CastInst *CreateIntegerCast(
553 Value *S, ///< The integer value to be casted (operand 0)
554 const Type *Ty, ///< The integer type to which operand is casted
555 bool isSigned, ///< Whether to regard S as signed or not
556 const Twine &Name, ///< The name for the instruction
557 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
560 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
561 static CastInst *CreateFPCast(
562 Value *S, ///< The floating point value to be casted
563 const Type *Ty, ///< The floating point type to cast to
564 const Twine &Name = "", ///< Name for the instruction
565 Instruction *InsertBefore = 0 ///< Place to insert the instruction
568 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
569 static CastInst *CreateFPCast(
570 Value *S, ///< The floating point value to be casted
571 const Type *Ty, ///< The floating point type to cast to
572 const Twine &Name, ///< The name for the instruction
573 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
576 /// @brief Create a Trunc or BitCast cast instruction
577 static CastInst *CreateTruncOrBitCast(
578 Value *S, ///< The value to be casted (operand 0)
579 const Type *Ty, ///< The type to which cast should be made
580 const Twine &Name = "", ///< Name for the instruction
581 Instruction *InsertBefore = 0 ///< Place to insert the instruction
584 /// @brief Create a Trunc or BitCast cast instruction
585 static CastInst *CreateTruncOrBitCast(
586 Value *S, ///< The value to be casted (operand 0)
587 const Type *Ty, ///< The type to which operand is casted
588 const Twine &Name, ///< The name for the instruction
589 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
592 /// @brief Check whether it is valid to call getCastOpcode for these types.
593 static bool isCastable(
594 const Type *SrcTy, ///< The Type from which the value should be cast.
595 const Type *DestTy ///< The Type to which the value should be cast.
598 /// Returns the opcode necessary to cast Val into Ty using usual casting
600 /// @brief Infer the opcode for cast operand and type
601 static Instruction::CastOps getCastOpcode(
602 const Value *Val, ///< The value to cast
603 bool SrcIsSigned, ///< Whether to treat the source as signed
604 const Type *Ty, ///< The Type to which the value should be casted
605 bool DstIsSigned ///< Whether to treate the dest. as signed
608 /// There are several places where we need to know if a cast instruction
609 /// only deals with integer source and destination types. To simplify that
610 /// logic, this method is provided.
611 /// @returns true iff the cast has only integral typed operand and dest type.
612 /// @brief Determine if this is an integer-only cast.
613 bool isIntegerCast() const;
615 /// A lossless cast is one that does not alter the basic value. It implies
616 /// a no-op cast but is more stringent, preventing things like int->float,
617 /// long->double, or int->ptr.
618 /// @returns true iff the cast is lossless.
619 /// @brief Determine if this is a lossless cast.
620 bool isLosslessCast() const;
622 /// A no-op cast is one that can be effected without changing any bits.
623 /// It implies that the source and destination types are the same size. The
624 /// IntPtrTy argument is used to make accurate determinations for casts
625 /// involving Integer and Pointer types. They are no-op casts if the integer
626 /// is the same size as the pointer. However, pointer size varies with
627 /// platform. Generally, the result of TargetData::getIntPtrType() should be
628 /// passed in. If that's not available, use Type::Int64Ty, which will make
629 /// the isNoopCast call conservative.
630 /// @brief Determine if the described cast is a no-op cast.
631 static bool isNoopCast(
632 Instruction::CastOps Opcode, ///< Opcode of cast
633 const Type *SrcTy, ///< SrcTy of cast
634 const Type *DstTy, ///< DstTy of cast
635 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
638 /// @brief Determine if this cast is a no-op cast.
640 const Type *IntPtrTy ///< Integer type corresponding to pointer
643 /// Determine how a pair of casts can be eliminated, if they can be at all.
644 /// This is a helper function for both CastInst and ConstantExpr.
645 /// @returns 0 if the CastInst pair can't be eliminated
646 /// @returns Instruction::CastOps value for a cast that can replace
647 /// the pair, casting SrcTy to DstTy.
648 /// @brief Determine if a cast pair is eliminable
649 static unsigned isEliminableCastPair(
650 Instruction::CastOps firstOpcode, ///< Opcode of first cast
651 Instruction::CastOps secondOpcode, ///< Opcode of second cast
652 const Type *SrcTy, ///< SrcTy of 1st cast
653 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
654 const Type *DstTy, ///< DstTy of 2nd cast
655 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
658 /// @brief Return the opcode of this CastInst
659 Instruction::CastOps getOpcode() const {
660 return Instruction::CastOps(Instruction::getOpcode());
663 /// @brief Return the source type, as a convenience
664 const Type* getSrcTy() const { return getOperand(0)->getType(); }
665 /// @brief Return the destination type, as a convenience
666 const Type* getDestTy() const { return getType(); }
668 /// This method can be used to determine if a cast from S to DstTy using
669 /// Opcode op is valid or not.
670 /// @returns true iff the proposed cast is valid.
671 /// @brief Determine if a cast is valid without creating one.
672 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
674 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
675 static inline bool classof(const CastInst *) { return true; }
676 static inline bool classof(const Instruction *I) {
679 static inline bool classof(const Value *V) {
680 return isa<Instruction>(V) && classof(cast<Instruction>(V));
684 //===----------------------------------------------------------------------===//
686 //===----------------------------------------------------------------------===//
688 /// This class is the base class for the comparison instructions.
689 /// @brief Abstract base class of comparison instructions.
690 class CmpInst : public Instruction {
691 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
692 CmpInst(); // do not implement
694 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
695 Value *LHS, Value *RHS, const Twine &Name = "",
696 Instruction *InsertBefore = 0);
698 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
699 Value *LHS, Value *RHS, const Twine &Name,
700 BasicBlock *InsertAtEnd);
702 virtual void Anchor() const; // Out of line virtual method.
704 /// This enumeration lists the possible predicates for CmpInst subclasses.
705 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
706 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
707 /// predicate values are not overlapping between the classes.
709 // Opcode U L G E Intuitive operation
710 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
711 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
712 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
713 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
714 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
715 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
716 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
717 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
718 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
719 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
720 FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
721 FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
722 FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
723 FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
724 FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
725 FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
726 FIRST_FCMP_PREDICATE = FCMP_FALSE,
727 LAST_FCMP_PREDICATE = FCMP_TRUE,
728 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
729 ICMP_EQ = 32, ///< equal
730 ICMP_NE = 33, ///< not equal
731 ICMP_UGT = 34, ///< unsigned greater than
732 ICMP_UGE = 35, ///< unsigned greater or equal
733 ICMP_ULT = 36, ///< unsigned less than
734 ICMP_ULE = 37, ///< unsigned less or equal
735 ICMP_SGT = 38, ///< signed greater than
736 ICMP_SGE = 39, ///< signed greater or equal
737 ICMP_SLT = 40, ///< signed less than
738 ICMP_SLE = 41, ///< signed less or equal
739 FIRST_ICMP_PREDICATE = ICMP_EQ,
740 LAST_ICMP_PREDICATE = ICMP_SLE,
741 BAD_ICMP_PREDICATE = ICMP_SLE + 1
744 // allocate space for exactly two operands
745 void *operator new(size_t s) {
746 return User::operator new(s, 2);
748 /// Construct a compare instruction, given the opcode, the predicate and
749 /// the two operands. Optionally (if InstBefore is specified) insert the
750 /// instruction into a BasicBlock right before the specified instruction.
751 /// The specified Instruction is allowed to be a dereferenced end iterator.
752 /// @brief Create a CmpInst
753 static CmpInst *Create(OtherOps Op,
754 unsigned short predicate, Value *S1,
755 Value *S2, const Twine &Name = "",
756 Instruction *InsertBefore = 0);
758 /// Construct a compare instruction, given the opcode, the predicate and the
759 /// two operands. Also automatically insert this instruction to the end of
760 /// the BasicBlock specified.
761 /// @brief Create a CmpInst
762 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
763 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
765 /// @brief Get the opcode casted to the right type
766 OtherOps getOpcode() const {
767 return static_cast<OtherOps>(Instruction::getOpcode());
770 /// @brief Return the predicate for this instruction.
771 Predicate getPredicate() const {
772 return Predicate(getSubclassDataFromInstruction());
775 /// @brief Set the predicate for this instruction to the specified value.
776 void setPredicate(Predicate P) { setInstructionSubclassData(P); }
778 static bool isFPPredicate(Predicate P) {
779 return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
782 static bool isIntPredicate(Predicate P) {
783 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
786 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
787 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
790 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
791 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
792 /// @returns the inverse predicate for the instruction's current predicate.
793 /// @brief Return the inverse of the instruction's predicate.
794 Predicate getInversePredicate() const {
795 return getInversePredicate(getPredicate());
798 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
799 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
800 /// @returns the inverse predicate for predicate provided in \p pred.
801 /// @brief Return the inverse of a given predicate
802 static Predicate getInversePredicate(Predicate pred);
804 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
805 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
806 /// @returns the predicate that would be the result of exchanging the two
807 /// operands of the CmpInst instruction without changing the result
809 /// @brief Return the predicate as if the operands were swapped
810 Predicate getSwappedPredicate() const {
811 return getSwappedPredicate(getPredicate());
814 /// This is a static version that you can use without an instruction
816 /// @brief Return the predicate as if the operands were swapped.
817 static Predicate getSwappedPredicate(Predicate pred);
819 /// @brief Provide more efficient getOperand methods.
820 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
822 /// This is just a convenience that dispatches to the subclasses.
823 /// @brief Swap the operands and adjust predicate accordingly to retain
824 /// the same comparison.
827 /// This is just a convenience that dispatches to the subclasses.
828 /// @brief Determine if this CmpInst is commutative.
829 bool isCommutative() const;
831 /// This is just a convenience that dispatches to the subclasses.
832 /// @brief Determine if this is an equals/not equals predicate.
833 bool isEquality() const;
835 /// @returns true if the comparison is signed, false otherwise.
836 /// @brief Determine if this instruction is using a signed comparison.
837 bool isSigned() const {
838 return isSigned(getPredicate());
841 /// @returns true if the comparison is unsigned, false otherwise.
842 /// @brief Determine if this instruction is using an unsigned comparison.
843 bool isUnsigned() const {
844 return isUnsigned(getPredicate());
847 /// This is just a convenience.
848 /// @brief Determine if this is true when both operands are the same.
849 bool isTrueWhenEqual() const {
850 return isTrueWhenEqual(getPredicate());
853 /// This is just a convenience.
854 /// @brief Determine if this is false when both operands are the same.
855 bool isFalseWhenEqual() const {
856 return isFalseWhenEqual(getPredicate());
859 /// @returns true if the predicate is unsigned, false otherwise.
860 /// @brief Determine if the predicate is an unsigned operation.
861 static bool isUnsigned(unsigned short predicate);
863 /// @returns true if the predicate is signed, false otherwise.
864 /// @brief Determine if the predicate is an signed operation.
865 static bool isSigned(unsigned short predicate);
867 /// @brief Determine if the predicate is an ordered operation.
868 static bool isOrdered(unsigned short predicate);
870 /// @brief Determine if the predicate is an unordered operation.
871 static bool isUnordered(unsigned short predicate);
873 /// Determine if the predicate is true when comparing a value with itself.
874 static bool isTrueWhenEqual(unsigned short predicate);
876 /// Determine if the predicate is false when comparing a value with itself.
877 static bool isFalseWhenEqual(unsigned short predicate);
879 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
880 static inline bool classof(const CmpInst *) { return true; }
881 static inline bool classof(const Instruction *I) {
882 return I->getOpcode() == Instruction::ICmp ||
883 I->getOpcode() == Instruction::FCmp;
885 static inline bool classof(const Value *V) {
886 return isa<Instruction>(V) && classof(cast<Instruction>(V));
889 /// @brief Create a result type for fcmp/icmp
890 static const Type* makeCmpResultType(const Type* opnd_type) {
891 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
892 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
893 vt->getNumElements());
895 return Type::getInt1Ty(opnd_type->getContext());
898 // Shadow Value::setValueSubclassData with a private forwarding method so that
899 // subclasses cannot accidentally use it.
900 void setValueSubclassData(unsigned short D) {
901 Value::setValueSubclassData(D);
906 // FIXME: these are redundant if CmpInst < BinaryOperator
908 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
911 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
913 } // End llvm namespace