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/DerivedTypes.h"
22 #include "llvm/ADT/Twine.h"
28 //===----------------------------------------------------------------------===//
29 // TerminatorInst Class
30 //===----------------------------------------------------------------------===//
32 /// TerminatorInst - 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 = 0)
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;
54 virtual TerminatorInst *clone_impl() const = 0;
57 /// getNumSuccessors - Return the number of successors that this terminator
59 unsigned getNumSuccessors() const {
60 return getNumSuccessorsV();
63 /// getSuccessor - Return the specified successor.
65 BasicBlock *getSuccessor(unsigned idx) const {
66 return getSuccessorV(idx);
69 /// setSuccessor - Update the specified successor to point at the provided
71 void setSuccessor(unsigned idx, BasicBlock *B) {
72 setSuccessorV(idx, B);
75 // Methods for support type inquiry through isa, cast, and dyn_cast:
76 static inline bool classof(const TerminatorInst *) { return true; }
77 static inline bool classof(const Instruction *I) {
78 return I->isTerminator();
80 static inline bool classof(const Value *V) {
81 return isa<Instruction>(V) && classof(cast<Instruction>(V));
86 //===----------------------------------------------------------------------===//
87 // UnaryInstruction Class
88 //===----------------------------------------------------------------------===//
90 class UnaryInstruction : public Instruction {
91 void *operator new(size_t, unsigned); // Do not implement
94 UnaryInstruction(Type *Ty, unsigned iType, Value *V,
96 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
99 UnaryInstruction(Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
100 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
104 // allocate space for exactly one operand
105 void *operator new(size_t s) {
106 return User::operator new(s, 1);
109 // Out of line virtual method, so the vtable, etc has a home.
112 /// Transparently provide more efficient getOperand methods.
113 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
115 // Methods for support type inquiry through isa, cast, and dyn_cast:
116 static inline bool classof(const UnaryInstruction *) { return true; }
117 static inline bool classof(const Instruction *I) {
118 return I->getOpcode() == Instruction::Alloca ||
119 I->getOpcode() == Instruction::Load ||
120 I->getOpcode() == Instruction::VAArg ||
121 I->getOpcode() == Instruction::ExtractValue ||
122 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
124 static inline bool classof(const Value *V) {
125 return isa<Instruction>(V) && classof(cast<Instruction>(V));
130 struct OperandTraits<UnaryInstruction> :
131 public FixedNumOperandTraits<UnaryInstruction, 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, Type *Ty,
145 const Twine &Name, Instruction *InsertBefore);
146 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 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"
196 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
197 const Twine &Name = "") {
198 BinaryOperator *BO = Create(Opc, V1, V2, Name);
199 BO->setHasNoSignedWrap(true);
202 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
203 const Twine &Name, BasicBlock *BB) {
204 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
205 BO->setHasNoSignedWrap(true);
208 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
209 const Twine &Name, Instruction *I) {
210 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
211 BO->setHasNoSignedWrap(true);
215 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
216 const Twine &Name = "") {
217 BinaryOperator *BO = Create(Opc, V1, V2, Name);
218 BO->setHasNoUnsignedWrap(true);
221 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
222 const Twine &Name, BasicBlock *BB) {
223 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
224 BO->setHasNoUnsignedWrap(true);
227 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
228 const Twine &Name, Instruction *I) {
229 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
230 BO->setHasNoUnsignedWrap(true);
234 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
235 const Twine &Name = "") {
236 BinaryOperator *BO = Create(Opc, V1, V2, Name);
237 BO->setIsExact(true);
240 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
241 const Twine &Name, BasicBlock *BB) {
242 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
243 BO->setIsExact(true);
246 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
247 const Twine &Name, Instruction *I) {
248 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
249 BO->setIsExact(true);
253 #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
254 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
255 (Value *V1, Value *V2, const Twine &Name = "") { \
256 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \
258 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
259 (Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \
260 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \
262 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
263 (Value *V1, Value *V2, const Twine &Name, Instruction *I) { \
264 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \
267 DEFINE_HELPERS(Add, NSW) // CreateNSWAdd
268 DEFINE_HELPERS(Add, NUW) // CreateNUWAdd
269 DEFINE_HELPERS(Sub, NSW) // CreateNSWSub
270 DEFINE_HELPERS(Sub, NUW) // CreateNUWSub
271 DEFINE_HELPERS(Mul, NSW) // CreateNSWMul
272 DEFINE_HELPERS(Mul, NUW) // CreateNUWMul
273 DEFINE_HELPERS(Shl, NSW) // CreateNSWShl
274 DEFINE_HELPERS(Shl, NUW) // CreateNUWShl
276 DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv
277 DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv
278 DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
279 DEFINE_HELPERS(LShr, Exact) // CreateExactLShr
281 #undef DEFINE_HELPERS
283 /// Helper functions to construct and inspect unary operations (NEG and NOT)
284 /// via binary operators SUB and XOR:
286 /// CreateNeg, CreateNot - Create the NEG and NOT
287 /// instructions out of SUB and XOR instructions.
289 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
290 Instruction *InsertBefore = 0);
291 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
292 BasicBlock *InsertAtEnd);
293 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
294 Instruction *InsertBefore = 0);
295 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
296 BasicBlock *InsertAtEnd);
297 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
298 Instruction *InsertBefore = 0);
299 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
300 BasicBlock *InsertAtEnd);
301 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
302 Instruction *InsertBefore = 0);
303 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
304 BasicBlock *InsertAtEnd);
305 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
306 Instruction *InsertBefore = 0);
307 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
308 BasicBlock *InsertAtEnd);
310 /// isNeg, isFNeg, isNot - Check if the given Value is a
311 /// NEG, FNeg, or NOT instruction.
313 static bool isNeg(const Value *V);
314 static bool isFNeg(const Value *V);
315 static bool isNot(const Value *V);
317 /// getNegArgument, getNotArgument - Helper functions to extract the
318 /// unary argument of a NEG, FNEG or NOT operation implemented via
319 /// Sub, FSub, or Xor.
321 static const Value *getNegArgument(const Value *BinOp);
322 static Value *getNegArgument( Value *BinOp);
323 static const Value *getFNegArgument(const Value *BinOp);
324 static Value *getFNegArgument( Value *BinOp);
325 static const Value *getNotArgument(const Value *BinOp);
326 static Value *getNotArgument( Value *BinOp);
328 BinaryOps getOpcode() const {
329 return static_cast<BinaryOps>(Instruction::getOpcode());
332 /// swapOperands - Exchange the two operands to this instruction.
333 /// This instruction is safe to use on any binary instruction and
334 /// does not modify the semantics of the instruction. If the instruction
335 /// cannot be reversed (ie, it's a Div), then return true.
339 /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
340 /// which must be an operator which supports this flag. See LangRef.html
341 /// for the meaning of this flag.
342 void setHasNoUnsignedWrap(bool b = true);
344 /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
345 /// which must be an operator which supports this flag. See LangRef.html
346 /// for the meaning of this flag.
347 void setHasNoSignedWrap(bool b = true);
349 /// setIsExact - Set or clear the exact flag on this instruction,
350 /// which must be an operator which supports this flag. See LangRef.html
351 /// for the meaning of this flag.
352 void setIsExact(bool b = true);
354 /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
355 bool hasNoUnsignedWrap() const;
357 /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
358 bool hasNoSignedWrap() const;
360 /// isExact - Determine whether the exact flag is set.
361 bool isExact() const;
363 // Methods for support type inquiry through isa, cast, and dyn_cast:
364 static inline bool classof(const BinaryOperator *) { return true; }
365 static inline bool classof(const Instruction *I) {
366 return I->isBinaryOp();
368 static inline bool classof(const Value *V) {
369 return isa<Instruction>(V) && classof(cast<Instruction>(V));
374 struct OperandTraits<BinaryOperator> :
375 public FixedNumOperandTraits<BinaryOperator, 2> {
378 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
380 //===----------------------------------------------------------------------===//
382 //===----------------------------------------------------------------------===//
384 /// CastInst - This is the base class for all instructions that perform data
385 /// casts. It is simply provided so that instruction category testing
386 /// can be performed with code like:
388 /// if (isa<CastInst>(Instr)) { ... }
389 /// @brief Base class of casting instructions.
390 class CastInst : public UnaryInstruction {
392 /// @brief Constructor with insert-before-instruction semantics for subclasses
393 CastInst(Type *Ty, unsigned iType, Value *S,
394 const Twine &NameStr = "", Instruction *InsertBefore = 0)
395 : UnaryInstruction(Ty, iType, S, InsertBefore) {
398 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
399 CastInst(Type *Ty, unsigned iType, Value *S,
400 const Twine &NameStr, BasicBlock *InsertAtEnd)
401 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
405 /// Provides a way to construct any of the CastInst subclasses using an
406 /// opcode instead of the subclass's constructor. The opcode must be in the
407 /// CastOps category (Instruction::isCast(opcode) returns true). This
408 /// constructor has insert-before-instruction semantics to automatically
409 /// insert the new CastInst before InsertBefore (if it is non-null).
410 /// @brief Construct any of the CastInst subclasses
411 static CastInst *Create(
412 Instruction::CastOps, ///< The opcode of the cast instruction
413 Value *S, ///< The value to be casted (operand 0)
414 Type *Ty, ///< The type to which cast should be made
415 const Twine &Name = "", ///< Name for the instruction
416 Instruction *InsertBefore = 0 ///< Place to insert the instruction
418 /// Provides a way to construct any of the CastInst subclasses using an
419 /// opcode instead of the subclass's constructor. The opcode must be in the
420 /// CastOps category. This constructor has insert-at-end-of-block semantics
421 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
423 /// @brief Construct any of the CastInst subclasses
424 static CastInst *Create(
425 Instruction::CastOps, ///< The opcode for the cast instruction
426 Value *S, ///< The value to be casted (operand 0)
427 Type *Ty, ///< The type to which operand is casted
428 const Twine &Name, ///< The name for the instruction
429 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
432 /// @brief Create a ZExt or BitCast cast instruction
433 static CastInst *CreateZExtOrBitCast(
434 Value *S, ///< The value to be casted (operand 0)
435 Type *Ty, ///< The type to which cast should be made
436 const Twine &Name = "", ///< Name for the instruction
437 Instruction *InsertBefore = 0 ///< Place to insert the instruction
440 /// @brief Create a ZExt or BitCast cast instruction
441 static CastInst *CreateZExtOrBitCast(
442 Value *S, ///< The value to be casted (operand 0)
443 Type *Ty, ///< The type to which operand is casted
444 const Twine &Name, ///< The name for the instruction
445 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
448 /// @brief Create a SExt or BitCast cast instruction
449 static CastInst *CreateSExtOrBitCast(
450 Value *S, ///< The value to be casted (operand 0)
451 Type *Ty, ///< The type to which cast should be made
452 const Twine &Name = "", ///< Name for the instruction
453 Instruction *InsertBefore = 0 ///< Place to insert the instruction
456 /// @brief Create a SExt or BitCast cast instruction
457 static CastInst *CreateSExtOrBitCast(
458 Value *S, ///< The value to be casted (operand 0)
459 Type *Ty, ///< The type to which operand is casted
460 const Twine &Name, ///< The name for the instruction
461 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
464 /// @brief Create a BitCast or a PtrToInt cast instruction
465 static CastInst *CreatePointerCast(
466 Value *S, ///< The pointer value to be casted (operand 0)
467 Type *Ty, ///< The type to which operand is casted
468 const Twine &Name, ///< The name for the instruction
469 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
472 /// @brief Create a BitCast or a PtrToInt cast instruction
473 static CastInst *CreatePointerCast(
474 Value *S, ///< The pointer value to be casted (operand 0)
475 Type *Ty, ///< The type to which cast should be made
476 const Twine &Name = "", ///< Name for the instruction
477 Instruction *InsertBefore = 0 ///< Place to insert the instruction
480 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
481 static CastInst *CreateIntegerCast(
482 Value *S, ///< The pointer value to be casted (operand 0)
483 Type *Ty, ///< The type to which cast should be made
484 bool isSigned, ///< Whether to regard S as signed or not
485 const Twine &Name = "", ///< Name for the instruction
486 Instruction *InsertBefore = 0 ///< Place to insert the instruction
489 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
490 static CastInst *CreateIntegerCast(
491 Value *S, ///< The integer value to be casted (operand 0)
492 Type *Ty, ///< The integer type to which operand is casted
493 bool isSigned, ///< Whether to regard S as signed or not
494 const Twine &Name, ///< The name for the instruction
495 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
498 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
499 static CastInst *CreateFPCast(
500 Value *S, ///< The floating point value to be casted
501 Type *Ty, ///< The floating point type to cast to
502 const Twine &Name = "", ///< Name for the instruction
503 Instruction *InsertBefore = 0 ///< Place to insert the instruction
506 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
507 static CastInst *CreateFPCast(
508 Value *S, ///< The floating point value to be casted
509 Type *Ty, ///< The floating point type to cast to
510 const Twine &Name, ///< The name for the instruction
511 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
514 /// @brief Create a Trunc or BitCast cast instruction
515 static CastInst *CreateTruncOrBitCast(
516 Value *S, ///< The value to be casted (operand 0)
517 Type *Ty, ///< The type to which cast should be made
518 const Twine &Name = "", ///< Name for the instruction
519 Instruction *InsertBefore = 0 ///< Place to insert the instruction
522 /// @brief Create a Trunc or BitCast cast instruction
523 static CastInst *CreateTruncOrBitCast(
524 Value *S, ///< The value to be casted (operand 0)
525 Type *Ty, ///< The type to which operand is casted
526 const Twine &Name, ///< The name for the instruction
527 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
530 /// @brief Check whether it is valid to call getCastOpcode for these types.
531 static bool isCastable(
532 Type *SrcTy, ///< The Type from which the value should be cast.
533 Type *DestTy ///< The Type to which the value should be cast.
536 /// Returns the opcode necessary to cast Val into Ty using usual casting
538 /// @brief Infer the opcode for cast operand and type
539 static Instruction::CastOps getCastOpcode(
540 const Value *Val, ///< The value to cast
541 bool SrcIsSigned, ///< Whether to treat the source as signed
542 Type *Ty, ///< The Type to which the value should be casted
543 bool DstIsSigned ///< Whether to treate the dest. as signed
546 /// There are several places where we need to know if a cast instruction
547 /// only deals with integer source and destination types. To simplify that
548 /// logic, this method is provided.
549 /// @returns true iff the cast has only integral typed operand and dest type.
550 /// @brief Determine if this is an integer-only cast.
551 bool isIntegerCast() const;
553 /// A lossless cast is one that does not alter the basic value. It implies
554 /// a no-op cast but is more stringent, preventing things like int->float,
555 /// long->double, or int->ptr.
556 /// @returns true iff the cast is lossless.
557 /// @brief Determine if this is a lossless cast.
558 bool isLosslessCast() const;
560 /// A no-op cast is one that can be effected without changing any bits.
561 /// It implies that the source and destination types are the same size. The
562 /// IntPtrTy argument is used to make accurate determinations for casts
563 /// involving Integer and Pointer types. They are no-op casts if the integer
564 /// is the same size as the pointer. However, pointer size varies with
565 /// platform. Generally, the result of TargetData::getIntPtrType() should be
566 /// passed in. If that's not available, use Type::Int64Ty, which will make
567 /// the isNoopCast call conservative.
568 /// @brief Determine if the described cast is a no-op cast.
569 static bool isNoopCast(
570 Instruction::CastOps Opcode, ///< Opcode of cast
571 Type *SrcTy, ///< SrcTy of cast
572 Type *DstTy, ///< DstTy of cast
573 Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
576 /// @brief Determine if this cast is a no-op cast.
578 Type *IntPtrTy ///< Integer type corresponding to pointer
581 /// Determine how a pair of casts can be eliminated, if they can be at all.
582 /// This is a helper function for both CastInst and ConstantExpr.
583 /// @returns 0 if the CastInst pair can't be eliminated
584 /// @returns Instruction::CastOps value for a cast that can replace
585 /// the pair, casting SrcTy to DstTy.
586 /// @brief Determine if a cast pair is eliminable
587 static unsigned isEliminableCastPair(
588 Instruction::CastOps firstOpcode, ///< Opcode of first cast
589 Instruction::CastOps secondOpcode, ///< Opcode of second cast
590 Type *SrcTy, ///< SrcTy of 1st cast
591 Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
592 Type *DstTy, ///< DstTy of 2nd cast
593 Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
596 /// @brief Return the opcode of this CastInst
597 Instruction::CastOps getOpcode() const {
598 return Instruction::CastOps(Instruction::getOpcode());
601 /// @brief Return the source type, as a convenience
602 Type* getSrcTy() const { return getOperand(0)->getType(); }
603 /// @brief Return the destination type, as a convenience
604 Type* getDestTy() const { return getType(); }
606 /// This method can be used to determine if a cast from S to DstTy using
607 /// Opcode op is valid or not.
608 /// @returns true iff the proposed cast is valid.
609 /// @brief Determine if a cast is valid without creating one.
610 static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
612 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
613 static inline bool classof(const CastInst *) { return true; }
614 static inline bool classof(const Instruction *I) {
617 static inline bool classof(const Value *V) {
618 return isa<Instruction>(V) && classof(cast<Instruction>(V));
622 //===----------------------------------------------------------------------===//
624 //===----------------------------------------------------------------------===//
626 /// This class is the base class for the comparison instructions.
627 /// @brief Abstract base class of comparison instructions.
628 class CmpInst : public Instruction {
629 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
630 CmpInst(); // do not implement
632 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
633 Value *LHS, Value *RHS, const Twine &Name = "",
634 Instruction *InsertBefore = 0);
636 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
637 Value *LHS, Value *RHS, const Twine &Name,
638 BasicBlock *InsertAtEnd);
640 virtual void Anchor() const; // Out of line virtual method.
642 /// This enumeration lists the possible predicates for CmpInst subclasses.
643 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
644 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
645 /// predicate values are not overlapping between the classes.
647 // Opcode U L G E Intuitive operation
648 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
649 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
650 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
651 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
652 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
653 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
654 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
655 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
656 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
657 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
658 FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
659 FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
660 FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
661 FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
662 FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
663 FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
664 FIRST_FCMP_PREDICATE = FCMP_FALSE,
665 LAST_FCMP_PREDICATE = FCMP_TRUE,
666 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
667 ICMP_EQ = 32, ///< equal
668 ICMP_NE = 33, ///< not equal
669 ICMP_UGT = 34, ///< unsigned greater than
670 ICMP_UGE = 35, ///< unsigned greater or equal
671 ICMP_ULT = 36, ///< unsigned less than
672 ICMP_ULE = 37, ///< unsigned less or equal
673 ICMP_SGT = 38, ///< signed greater than
674 ICMP_SGE = 39, ///< signed greater or equal
675 ICMP_SLT = 40, ///< signed less than
676 ICMP_SLE = 41, ///< signed less or equal
677 FIRST_ICMP_PREDICATE = ICMP_EQ,
678 LAST_ICMP_PREDICATE = ICMP_SLE,
679 BAD_ICMP_PREDICATE = ICMP_SLE + 1
682 // allocate space for exactly two operands
683 void *operator new(size_t s) {
684 return User::operator new(s, 2);
686 /// Construct a compare instruction, given the opcode, the predicate and
687 /// the two operands. Optionally (if InstBefore is specified) insert the
688 /// instruction into a BasicBlock right before the specified instruction.
689 /// The specified Instruction is allowed to be a dereferenced end iterator.
690 /// @brief Create a CmpInst
691 static CmpInst *Create(OtherOps Op,
692 unsigned short predicate, Value *S1,
693 Value *S2, const Twine &Name = "",
694 Instruction *InsertBefore = 0);
696 /// Construct a compare instruction, given the opcode, the predicate and the
697 /// two operands. Also automatically insert this instruction to the end of
698 /// the BasicBlock specified.
699 /// @brief Create a CmpInst
700 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
701 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
703 /// @brief Get the opcode casted to the right type
704 OtherOps getOpcode() const {
705 return static_cast<OtherOps>(Instruction::getOpcode());
708 /// @brief Return the predicate for this instruction.
709 Predicate getPredicate() const {
710 return Predicate(getSubclassDataFromInstruction());
713 /// @brief Set the predicate for this instruction to the specified value.
714 void setPredicate(Predicate P) { setInstructionSubclassData(P); }
716 static bool isFPPredicate(Predicate P) {
717 return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
720 static bool isIntPredicate(Predicate P) {
721 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
724 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
725 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
728 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
729 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
730 /// @returns the inverse predicate for the instruction's current predicate.
731 /// @brief Return the inverse of the instruction's predicate.
732 Predicate getInversePredicate() const {
733 return getInversePredicate(getPredicate());
736 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
737 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
738 /// @returns the inverse predicate for predicate provided in \p pred.
739 /// @brief Return the inverse of a given predicate
740 static Predicate getInversePredicate(Predicate pred);
742 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
743 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
744 /// @returns the predicate that would be the result of exchanging the two
745 /// operands of the CmpInst instruction without changing the result
747 /// @brief Return the predicate as if the operands were swapped
748 Predicate getSwappedPredicate() const {
749 return getSwappedPredicate(getPredicate());
752 /// This is a static version that you can use without an instruction
754 /// @brief Return the predicate as if the operands were swapped.
755 static Predicate getSwappedPredicate(Predicate pred);
757 /// @brief Provide more efficient getOperand methods.
758 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
760 /// This is just a convenience that dispatches to the subclasses.
761 /// @brief Swap the operands and adjust predicate accordingly to retain
762 /// the same comparison.
765 /// This is just a convenience that dispatches to the subclasses.
766 /// @brief Determine if this CmpInst is commutative.
767 bool isCommutative() const;
769 /// This is just a convenience that dispatches to the subclasses.
770 /// @brief Determine if this is an equals/not equals predicate.
771 bool isEquality() const;
773 /// @returns true if the comparison is signed, false otherwise.
774 /// @brief Determine if this instruction is using a signed comparison.
775 bool isSigned() const {
776 return isSigned(getPredicate());
779 /// @returns true if the comparison is unsigned, false otherwise.
780 /// @brief Determine if this instruction is using an unsigned comparison.
781 bool isUnsigned() const {
782 return isUnsigned(getPredicate());
785 /// This is just a convenience.
786 /// @brief Determine if this is true when both operands are the same.
787 bool isTrueWhenEqual() const {
788 return isTrueWhenEqual(getPredicate());
791 /// This is just a convenience.
792 /// @brief Determine if this is false when both operands are the same.
793 bool isFalseWhenEqual() const {
794 return isFalseWhenEqual(getPredicate());
797 /// @returns true if the predicate is unsigned, false otherwise.
798 /// @brief Determine if the predicate is an unsigned operation.
799 static bool isUnsigned(unsigned short predicate);
801 /// @returns true if the predicate is signed, false otherwise.
802 /// @brief Determine if the predicate is an signed operation.
803 static bool isSigned(unsigned short predicate);
805 /// @brief Determine if the predicate is an ordered operation.
806 static bool isOrdered(unsigned short predicate);
808 /// @brief Determine if the predicate is an unordered operation.
809 static bool isUnordered(unsigned short predicate);
811 /// Determine if the predicate is true when comparing a value with itself.
812 static bool isTrueWhenEqual(unsigned short predicate);
814 /// Determine if the predicate is false when comparing a value with itself.
815 static bool isFalseWhenEqual(unsigned short predicate);
817 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
818 static inline bool classof(const CmpInst *) { return true; }
819 static inline bool classof(const Instruction *I) {
820 return I->getOpcode() == Instruction::ICmp ||
821 I->getOpcode() == Instruction::FCmp;
823 static inline bool classof(const Value *V) {
824 return isa<Instruction>(V) && classof(cast<Instruction>(V));
827 /// @brief Create a result type for fcmp/icmp
828 static Type* makeCmpResultType(Type* opnd_type) {
829 if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
830 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
831 vt->getNumElements());
833 return Type::getInt1Ty(opnd_type->getContext());
836 // Shadow Value::setValueSubclassData with a private forwarding method so that
837 // subclasses cannot accidentally use it.
838 void setValueSubclassData(unsigned short D) {
839 Value::setValueSubclassData(D);
844 // FIXME: these are redundant if CmpInst < BinaryOperator
846 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
849 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
851 } // End llvm namespace