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/DataLayout.h"
21 #include "llvm/OperandTraits.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(Type *Ty, Instruction::TermOps iType,
39 Use *Ops, unsigned NumOps,
40 Instruction *InsertBefore = 0)
41 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
43 TerminatorInst(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 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) LLVM_DELETED_FUNCTION;
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 Instruction *I) {
117 return I->getOpcode() == Instruction::Alloca ||
118 I->getOpcode() == Instruction::Load ||
119 I->getOpcode() == Instruction::VAArg ||
120 I->getOpcode() == Instruction::ExtractValue ||
121 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
123 static inline bool classof(const Value *V) {
124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
129 struct OperandTraits<UnaryInstruction> :
130 public FixedNumOperandTraits<UnaryInstruction, 1> {
133 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
135 //===----------------------------------------------------------------------===//
136 // BinaryOperator Class
137 //===----------------------------------------------------------------------===//
139 class BinaryOperator : public Instruction {
140 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
142 void init(BinaryOps iType);
143 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
144 const Twine &Name, Instruction *InsertBefore);
145 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, Type *Ty,
146 const Twine &Name, BasicBlock *InsertAtEnd);
147 virtual BinaryOperator *clone_impl() const LLVM_OVERRIDE;
149 // allocate space for exactly two operands
150 void *operator new(size_t s) {
151 return User::operator new(s, 2);
154 /// Transparently provide more efficient getOperand methods.
155 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
157 /// Create() - Construct a binary instruction, given the opcode and the two
158 /// operands. Optionally (if InstBefore is specified) insert the instruction
159 /// into a BasicBlock right before the specified instruction. The specified
160 /// Instruction is allowed to be a dereferenced end iterator.
162 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
163 const Twine &Name = Twine(),
164 Instruction *InsertBefore = 0);
166 /// Create() - Construct a binary instruction, given the opcode and the two
167 /// operands. Also automatically insert this instruction to the end of the
168 /// BasicBlock specified.
170 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
171 const Twine &Name, BasicBlock *InsertAtEnd);
173 /// Create* - These methods just forward to Create, and are useful when you
174 /// statically know what type of instruction you're going to create. These
175 /// helpers just save some typing.
176 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
177 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
178 const Twine &Name = "") {\
179 return Create(Instruction::OPC, V1, V2, Name);\
181 #include "llvm/Instruction.def"
182 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
183 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
184 const Twine &Name, BasicBlock *BB) {\
185 return Create(Instruction::OPC, V1, V2, Name, BB);\
187 #include "llvm/Instruction.def"
188 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
189 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
190 const Twine &Name, Instruction *I) {\
191 return Create(Instruction::OPC, V1, V2, Name, I);\
193 #include "llvm/Instruction.def"
195 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
196 const Twine &Name = "") {
197 BinaryOperator *BO = Create(Opc, V1, V2, Name);
198 BO->setHasNoSignedWrap(true);
201 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
202 const Twine &Name, BasicBlock *BB) {
203 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
204 BO->setHasNoSignedWrap(true);
207 static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
208 const Twine &Name, Instruction *I) {
209 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
210 BO->setHasNoSignedWrap(true);
214 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
215 const Twine &Name = "") {
216 BinaryOperator *BO = Create(Opc, V1, V2, Name);
217 BO->setHasNoUnsignedWrap(true);
220 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
221 const Twine &Name, BasicBlock *BB) {
222 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
223 BO->setHasNoUnsignedWrap(true);
226 static BinaryOperator *CreateNUW(BinaryOps Opc, Value *V1, Value *V2,
227 const Twine &Name, Instruction *I) {
228 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
229 BO->setHasNoUnsignedWrap(true);
233 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
234 const Twine &Name = "") {
235 BinaryOperator *BO = Create(Opc, V1, V2, Name);
236 BO->setIsExact(true);
239 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
240 const Twine &Name, BasicBlock *BB) {
241 BinaryOperator *BO = Create(Opc, V1, V2, Name, BB);
242 BO->setIsExact(true);
245 static BinaryOperator *CreateExact(BinaryOps Opc, Value *V1, Value *V2,
246 const Twine &Name, Instruction *I) {
247 BinaryOperator *BO = Create(Opc, V1, V2, Name, I);
248 BO->setIsExact(true);
252 #define DEFINE_HELPERS(OPC, NUWNSWEXACT) \
253 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
254 (Value *V1, Value *V2, const Twine &Name = "") { \
255 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name); \
257 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
258 (Value *V1, Value *V2, const Twine &Name, BasicBlock *BB) { \
259 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, BB); \
261 static BinaryOperator *Create ## NUWNSWEXACT ## OPC \
262 (Value *V1, Value *V2, const Twine &Name, Instruction *I) { \
263 return Create ## NUWNSWEXACT(Instruction::OPC, V1, V2, Name, I); \
266 DEFINE_HELPERS(Add, NSW) // CreateNSWAdd
267 DEFINE_HELPERS(Add, NUW) // CreateNUWAdd
268 DEFINE_HELPERS(Sub, NSW) // CreateNSWSub
269 DEFINE_HELPERS(Sub, NUW) // CreateNUWSub
270 DEFINE_HELPERS(Mul, NSW) // CreateNSWMul
271 DEFINE_HELPERS(Mul, NUW) // CreateNUWMul
272 DEFINE_HELPERS(Shl, NSW) // CreateNSWShl
273 DEFINE_HELPERS(Shl, NUW) // CreateNUWShl
275 DEFINE_HELPERS(SDiv, Exact) // CreateExactSDiv
276 DEFINE_HELPERS(UDiv, Exact) // CreateExactUDiv
277 DEFINE_HELPERS(AShr, Exact) // CreateExactAShr
278 DEFINE_HELPERS(LShr, Exact) // CreateExactLShr
280 #undef DEFINE_HELPERS
282 /// Helper functions to construct and inspect unary operations (NEG and NOT)
283 /// via binary operators SUB and XOR:
285 /// CreateNeg, CreateNot - Create the NEG and NOT
286 /// instructions out of SUB and XOR instructions.
288 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
289 Instruction *InsertBefore = 0);
290 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
291 BasicBlock *InsertAtEnd);
292 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
293 Instruction *InsertBefore = 0);
294 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
295 BasicBlock *InsertAtEnd);
296 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name = "",
297 Instruction *InsertBefore = 0);
298 static BinaryOperator *CreateNUWNeg(Value *Op, const Twine &Name,
299 BasicBlock *InsertAtEnd);
300 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
301 Instruction *InsertBefore = 0);
302 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
303 BasicBlock *InsertAtEnd);
304 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
305 Instruction *InsertBefore = 0);
306 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
307 BasicBlock *InsertAtEnd);
309 /// isNeg, isFNeg, isNot - Check if the given Value is a
310 /// NEG, FNeg, or NOT instruction.
312 static bool isNeg(const Value *V);
313 static bool isFNeg(const Value *V);
314 static bool isNot(const Value *V);
316 /// getNegArgument, getNotArgument - Helper functions to extract the
317 /// unary argument of a NEG, FNEG or NOT operation implemented via
318 /// Sub, FSub, or Xor.
320 static const Value *getNegArgument(const Value *BinOp);
321 static Value *getNegArgument( Value *BinOp);
322 static const Value *getFNegArgument(const Value *BinOp);
323 static Value *getFNegArgument( Value *BinOp);
324 static const Value *getNotArgument(const Value *BinOp);
325 static Value *getNotArgument( Value *BinOp);
327 BinaryOps getOpcode() const {
328 return static_cast<BinaryOps>(Instruction::getOpcode());
331 /// swapOperands - Exchange the two operands to this instruction.
332 /// This instruction is safe to use on any binary instruction and
333 /// does not modify the semantics of the instruction. If the instruction
334 /// cannot be reversed (ie, it's a Div), then return true.
338 /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
339 /// which must be an operator which supports this flag. See LangRef.html
340 /// for the meaning of this flag.
341 void setHasNoUnsignedWrap(bool b = true);
343 /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
344 /// which must be an operator which supports this flag. See LangRef.html
345 /// for the meaning of this flag.
346 void setHasNoSignedWrap(bool b = true);
348 /// setIsExact - Set or clear the exact flag on this instruction,
349 /// which must be an operator which supports this flag. See LangRef.html
350 /// for the meaning of this flag.
351 void setIsExact(bool b = true);
353 /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
354 bool hasNoUnsignedWrap() const;
356 /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
357 bool hasNoSignedWrap() const;
359 /// isExact - Determine whether the exact flag is set.
360 bool isExact() const;
362 // Methods for support type inquiry through isa, cast, and dyn_cast:
363 static inline bool classof(const Instruction *I) {
364 return I->isBinaryOp();
366 static inline bool classof(const Value *V) {
367 return isa<Instruction>(V) && classof(cast<Instruction>(V));
372 struct OperandTraits<BinaryOperator> :
373 public FixedNumOperandTraits<BinaryOperator, 2> {
376 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
378 //===----------------------------------------------------------------------===//
380 //===----------------------------------------------------------------------===//
382 /// CastInst - This is the base class for all instructions that perform data
383 /// casts. It is simply provided so that instruction category testing
384 /// can be performed with code like:
386 /// if (isa<CastInst>(Instr)) { ... }
387 /// @brief Base class of casting instructions.
388 class CastInst : public UnaryInstruction {
389 virtual void anchor() LLVM_OVERRIDE;
391 /// @brief Constructor with insert-before-instruction semantics for subclasses
392 CastInst(Type *Ty, unsigned iType, Value *S,
393 const Twine &NameStr = "", Instruction *InsertBefore = 0)
394 : UnaryInstruction(Ty, iType, S, InsertBefore) {
397 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
398 CastInst(Type *Ty, unsigned iType, Value *S,
399 const Twine &NameStr, BasicBlock *InsertAtEnd)
400 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
404 /// Provides a way to construct any of the CastInst subclasses using an
405 /// opcode instead of the subclass's constructor. The opcode must be in the
406 /// CastOps category (Instruction::isCast(opcode) returns true). This
407 /// constructor has insert-before-instruction semantics to automatically
408 /// insert the new CastInst before InsertBefore (if it is non-null).
409 /// @brief Construct any of the CastInst subclasses
410 static CastInst *Create(
411 Instruction::CastOps, ///< The opcode of the cast instruction
412 Value *S, ///< The value to be casted (operand 0)
413 Type *Ty, ///< The type to which cast should be made
414 const Twine &Name = "", ///< Name for the instruction
415 Instruction *InsertBefore = 0 ///< Place to insert the instruction
417 /// Provides a way to construct any of the CastInst subclasses using an
418 /// opcode instead of the subclass's constructor. The opcode must be in the
419 /// CastOps category. This constructor has insert-at-end-of-block semantics
420 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
422 /// @brief Construct any of the CastInst subclasses
423 static CastInst *Create(
424 Instruction::CastOps, ///< The opcode for the cast instruction
425 Value *S, ///< The value to be casted (operand 0)
426 Type *Ty, ///< The type to which operand is casted
427 const Twine &Name, ///< The name for the instruction
428 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
431 /// @brief Create a ZExt or BitCast cast instruction
432 static CastInst *CreateZExtOrBitCast(
433 Value *S, ///< The value to be casted (operand 0)
434 Type *Ty, ///< The type to which cast should be made
435 const Twine &Name = "", ///< Name for the instruction
436 Instruction *InsertBefore = 0 ///< Place to insert the instruction
439 /// @brief Create a ZExt or BitCast cast instruction
440 static CastInst *CreateZExtOrBitCast(
441 Value *S, ///< The value to be casted (operand 0)
442 Type *Ty, ///< The type to which operand is casted
443 const Twine &Name, ///< The name for the instruction
444 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
447 /// @brief Create a SExt or BitCast cast instruction
448 static CastInst *CreateSExtOrBitCast(
449 Value *S, ///< The value to be casted (operand 0)
450 Type *Ty, ///< The type to which cast should be made
451 const Twine &Name = "", ///< Name for the instruction
452 Instruction *InsertBefore = 0 ///< Place to insert the instruction
455 /// @brief Create a SExt or BitCast cast instruction
456 static CastInst *CreateSExtOrBitCast(
457 Value *S, ///< The value to be casted (operand 0)
458 Type *Ty, ///< The type to which operand is casted
459 const Twine &Name, ///< The name for the instruction
460 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
463 /// @brief Create a BitCast or a PtrToInt cast instruction
464 static CastInst *CreatePointerCast(
465 Value *S, ///< The pointer value to be casted (operand 0)
466 Type *Ty, ///< The type to which operand is casted
467 const Twine &Name, ///< The name for the instruction
468 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
471 /// @brief Create a BitCast or a PtrToInt cast instruction
472 static CastInst *CreatePointerCast(
473 Value *S, ///< The pointer value to be casted (operand 0)
474 Type *Ty, ///< The type to which cast should be made
475 const Twine &Name = "", ///< Name for the instruction
476 Instruction *InsertBefore = 0 ///< Place to insert the instruction
479 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
480 static CastInst *CreateIntegerCast(
481 Value *S, ///< The pointer value to be casted (operand 0)
482 Type *Ty, ///< The type to which cast should be made
483 bool isSigned, ///< Whether to regard S as signed or not
484 const Twine &Name = "", ///< Name for the instruction
485 Instruction *InsertBefore = 0 ///< Place to insert the instruction
488 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
489 static CastInst *CreateIntegerCast(
490 Value *S, ///< The integer value to be casted (operand 0)
491 Type *Ty, ///< The integer type to which operand is casted
492 bool isSigned, ///< Whether to regard S as signed or not
493 const Twine &Name, ///< The name for the instruction
494 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
497 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
498 static CastInst *CreateFPCast(
499 Value *S, ///< The floating point value to be casted
500 Type *Ty, ///< The floating point type to cast to
501 const Twine &Name = "", ///< Name for the instruction
502 Instruction *InsertBefore = 0 ///< Place to insert the instruction
505 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
506 static CastInst *CreateFPCast(
507 Value *S, ///< The floating point value to be casted
508 Type *Ty, ///< The floating point type to cast to
509 const Twine &Name, ///< The name for the instruction
510 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
513 /// @brief Create a Trunc or BitCast cast instruction
514 static CastInst *CreateTruncOrBitCast(
515 Value *S, ///< The value to be casted (operand 0)
516 Type *Ty, ///< The type to which cast should be made
517 const Twine &Name = "", ///< Name for the instruction
518 Instruction *InsertBefore = 0 ///< Place to insert the instruction
521 /// @brief Create a Trunc or BitCast cast instruction
522 static CastInst *CreateTruncOrBitCast(
523 Value *S, ///< The value to be casted (operand 0)
524 Type *Ty, ///< The type to which operand is casted
525 const Twine &Name, ///< The name for the instruction
526 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
529 /// @brief Check whether it is valid to call getCastOpcode for these types.
530 static bool isCastable(
531 Type *SrcTy, ///< The Type from which the value should be cast.
532 Type *DestTy ///< The Type to which the value should be cast.
535 /// Returns the opcode necessary to cast Val into Ty using usual casting
537 /// @brief Infer the opcode for cast operand and type
538 static Instruction::CastOps getCastOpcode(
539 const Value *Val, ///< The value to cast
540 bool SrcIsSigned, ///< Whether to treat the source as signed
541 Type *Ty, ///< The Type to which the value should be casted
542 bool DstIsSigned ///< Whether to treate the dest. as signed
545 /// There are several places where we need to know if a cast instruction
546 /// only deals with integer source and destination types. To simplify that
547 /// logic, this method is provided.
548 /// @returns true iff the cast has only integral typed operand and dest type.
549 /// @brief Determine if this is an integer-only cast.
550 bool isIntegerCast() const;
552 /// A lossless cast is one that does not alter the basic value. It implies
553 /// a no-op cast but is more stringent, preventing things like int->float,
554 /// long->double, or int->ptr.
555 /// @returns true iff the cast is lossless.
556 /// @brief Determine if this is a lossless cast.
557 bool isLosslessCast() const;
559 /// A no-op cast is one that can be effected without changing any bits.
560 /// It implies that the source and destination types are the same size. The
561 /// IntPtrTy argument is used to make accurate determinations for casts
562 /// involving Integer and Pointer types. They are no-op casts if the integer
563 /// is the same size as the pointer. However, pointer size varies with
564 /// platform. Generally, the result of DataLayout::getIntPtrType() should be
565 /// passed in. If that's not available, use Type::Int64Ty, which will make
566 /// the isNoopCast call conservative.
567 /// @brief Determine if the described cast is a no-op cast.
568 static bool isNoopCast(
569 Instruction::CastOps Opcode, ///< Opcode of cast
570 Type *SrcTy, ///< SrcTy of cast
571 Type *DstTy, ///< DstTy of cast
572 Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
575 /// @brief Determine if this cast is a no-op cast.
577 Type *IntPtrTy ///< Integer type corresponding to pointer
580 /// @brief Determine if this cast is a no-op cast.
582 const DataLayout &DL ///< DataLayout to get the Int Ptr type from.
585 /// Determine how a pair of casts can be eliminated, if they can be at all.
586 /// This is a helper function for both CastInst and ConstantExpr.
587 /// @returns 0 if the CastInst pair can't be eliminated, otherwise
588 /// returns Instruction::CastOps value for a cast that can replace
589 /// the pair, casting SrcTy to DstTy.
590 /// @brief Determine if a cast pair is eliminable
591 static unsigned isEliminableCastPair(
592 Instruction::CastOps firstOpcode, ///< Opcode of first cast
593 Instruction::CastOps secondOpcode, ///< Opcode of second cast
594 Type *SrcTy, ///< SrcTy of 1st cast
595 Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
596 Type *DstTy, ///< DstTy of 2nd cast
597 Type *SrcIntPtrTy, ///< Integer type corresponding to Ptr SrcTy, or null
598 Type *MidIntPtrTy, ///< Integer type corresponding to Ptr MidTy, or null
599 Type *DstIntPtrTy ///< Integer type corresponding to Ptr DstTy, or null
602 /// @brief Return the opcode of this CastInst
603 Instruction::CastOps getOpcode() const {
604 return Instruction::CastOps(Instruction::getOpcode());
607 /// @brief Return the source type, as a convenience
608 Type* getSrcTy() const { return getOperand(0)->getType(); }
609 /// @brief Return the destination type, as a convenience
610 Type* getDestTy() const { return getType(); }
612 /// This method can be used to determine if a cast from S to DstTy using
613 /// Opcode op is valid or not.
614 /// @returns true iff the proposed cast is valid.
615 /// @brief Determine if a cast is valid without creating one.
616 static bool castIsValid(Instruction::CastOps op, Value *S, Type *DstTy);
618 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
619 static inline bool classof(const Instruction *I) {
622 static inline bool classof(const Value *V) {
623 return isa<Instruction>(V) && classof(cast<Instruction>(V));
627 //===----------------------------------------------------------------------===//
629 //===----------------------------------------------------------------------===//
631 /// This class is the base class for the comparison instructions.
632 /// @brief Abstract base class of comparison instructions.
633 class CmpInst : public Instruction {
634 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
635 CmpInst() LLVM_DELETED_FUNCTION;
637 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
638 Value *LHS, Value *RHS, const Twine &Name = "",
639 Instruction *InsertBefore = 0);
641 CmpInst(Type *ty, Instruction::OtherOps op, unsigned short pred,
642 Value *LHS, Value *RHS, const Twine &Name,
643 BasicBlock *InsertAtEnd);
645 virtual void anchor() LLVM_OVERRIDE; // Out of line virtual method.
647 /// This enumeration lists the possible predicates for CmpInst subclasses.
648 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
649 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
650 /// predicate values are not overlapping between the classes.
652 // Opcode U L G E Intuitive operation
653 FCMP_FALSE = 0, ///< 0 0 0 0 Always false (always folded)
654 FCMP_OEQ = 1, ///< 0 0 0 1 True if ordered and equal
655 FCMP_OGT = 2, ///< 0 0 1 0 True if ordered and greater than
656 FCMP_OGE = 3, ///< 0 0 1 1 True if ordered and greater than or equal
657 FCMP_OLT = 4, ///< 0 1 0 0 True if ordered and less than
658 FCMP_OLE = 5, ///< 0 1 0 1 True if ordered and less than or equal
659 FCMP_ONE = 6, ///< 0 1 1 0 True if ordered and operands are unequal
660 FCMP_ORD = 7, ///< 0 1 1 1 True if ordered (no nans)
661 FCMP_UNO = 8, ///< 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
662 FCMP_UEQ = 9, ///< 1 0 0 1 True if unordered or equal
663 FCMP_UGT = 10, ///< 1 0 1 0 True if unordered or greater than
664 FCMP_UGE = 11, ///< 1 0 1 1 True if unordered, greater than, or equal
665 FCMP_ULT = 12, ///< 1 1 0 0 True if unordered or less than
666 FCMP_ULE = 13, ///< 1 1 0 1 True if unordered, less than, or equal
667 FCMP_UNE = 14, ///< 1 1 1 0 True if unordered or not equal
668 FCMP_TRUE = 15, ///< 1 1 1 1 Always true (always folded)
669 FIRST_FCMP_PREDICATE = FCMP_FALSE,
670 LAST_FCMP_PREDICATE = FCMP_TRUE,
671 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
672 ICMP_EQ = 32, ///< equal
673 ICMP_NE = 33, ///< not equal
674 ICMP_UGT = 34, ///< unsigned greater than
675 ICMP_UGE = 35, ///< unsigned greater or equal
676 ICMP_ULT = 36, ///< unsigned less than
677 ICMP_ULE = 37, ///< unsigned less or equal
678 ICMP_SGT = 38, ///< signed greater than
679 ICMP_SGE = 39, ///< signed greater or equal
680 ICMP_SLT = 40, ///< signed less than
681 ICMP_SLE = 41, ///< signed less or equal
682 FIRST_ICMP_PREDICATE = ICMP_EQ,
683 LAST_ICMP_PREDICATE = ICMP_SLE,
684 BAD_ICMP_PREDICATE = ICMP_SLE + 1
687 // allocate space for exactly two operands
688 void *operator new(size_t s) {
689 return User::operator new(s, 2);
691 /// Construct a compare instruction, given the opcode, the predicate and
692 /// the two operands. Optionally (if InstBefore is specified) insert the
693 /// instruction into a BasicBlock right before the specified instruction.
694 /// The specified Instruction is allowed to be a dereferenced end iterator.
695 /// @brief Create a CmpInst
696 static CmpInst *Create(OtherOps Op,
697 unsigned short predicate, Value *S1,
698 Value *S2, const Twine &Name = "",
699 Instruction *InsertBefore = 0);
701 /// Construct a compare instruction, given the opcode, the predicate and the
702 /// two operands. Also automatically insert this instruction to the end of
703 /// the BasicBlock specified.
704 /// @brief Create a CmpInst
705 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
706 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
708 /// @brief Get the opcode casted to the right type
709 OtherOps getOpcode() const {
710 return static_cast<OtherOps>(Instruction::getOpcode());
713 /// @brief Return the predicate for this instruction.
714 Predicate getPredicate() const {
715 return Predicate(getSubclassDataFromInstruction());
718 /// @brief Set the predicate for this instruction to the specified value.
719 void setPredicate(Predicate P) { setInstructionSubclassData(P); }
721 static bool isFPPredicate(Predicate P) {
722 return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
725 static bool isIntPredicate(Predicate P) {
726 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
729 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
730 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
733 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
734 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
735 /// @returns the inverse predicate for the instruction's current predicate.
736 /// @brief Return the inverse of the instruction's predicate.
737 Predicate getInversePredicate() const {
738 return getInversePredicate(getPredicate());
741 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
742 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
743 /// @returns the inverse predicate for predicate provided in \p pred.
744 /// @brief Return the inverse of a given predicate
745 static Predicate getInversePredicate(Predicate pred);
747 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
748 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
749 /// @returns the predicate that would be the result of exchanging the two
750 /// operands of the CmpInst instruction without changing the result
752 /// @brief Return the predicate as if the operands were swapped
753 Predicate getSwappedPredicate() const {
754 return getSwappedPredicate(getPredicate());
757 /// This is a static version that you can use without an instruction
759 /// @brief Return the predicate as if the operands were swapped.
760 static Predicate getSwappedPredicate(Predicate pred);
762 /// @brief Provide more efficient getOperand methods.
763 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
765 /// This is just a convenience that dispatches to the subclasses.
766 /// @brief Swap the operands and adjust predicate accordingly to retain
767 /// the same comparison.
770 /// This is just a convenience that dispatches to the subclasses.
771 /// @brief Determine if this CmpInst is commutative.
772 bool isCommutative() const;
774 /// This is just a convenience that dispatches to the subclasses.
775 /// @brief Determine if this is an equals/not equals predicate.
776 bool isEquality() const;
778 /// @returns true if the comparison is signed, false otherwise.
779 /// @brief Determine if this instruction is using a signed comparison.
780 bool isSigned() const {
781 return isSigned(getPredicate());
784 /// @returns true if the comparison is unsigned, false otherwise.
785 /// @brief Determine if this instruction is using an unsigned comparison.
786 bool isUnsigned() const {
787 return isUnsigned(getPredicate());
790 /// This is just a convenience.
791 /// @brief Determine if this is true when both operands are the same.
792 bool isTrueWhenEqual() const {
793 return isTrueWhenEqual(getPredicate());
796 /// This is just a convenience.
797 /// @brief Determine if this is false when both operands are the same.
798 bool isFalseWhenEqual() const {
799 return isFalseWhenEqual(getPredicate());
802 /// @returns true if the predicate is unsigned, false otherwise.
803 /// @brief Determine if the predicate is an unsigned operation.
804 static bool isUnsigned(unsigned short predicate);
806 /// @returns true if the predicate is signed, false otherwise.
807 /// @brief Determine if the predicate is an signed operation.
808 static bool isSigned(unsigned short predicate);
810 /// @brief Determine if the predicate is an ordered operation.
811 static bool isOrdered(unsigned short predicate);
813 /// @brief Determine if the predicate is an unordered operation.
814 static bool isUnordered(unsigned short predicate);
816 /// Determine if the predicate is true when comparing a value with itself.
817 static bool isTrueWhenEqual(unsigned short predicate);
819 /// Determine if the predicate is false when comparing a value with itself.
820 static bool isFalseWhenEqual(unsigned short predicate);
822 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
823 static inline bool classof(const Instruction *I) {
824 return I->getOpcode() == Instruction::ICmp ||
825 I->getOpcode() == Instruction::FCmp;
827 static inline bool classof(const Value *V) {
828 return isa<Instruction>(V) && classof(cast<Instruction>(V));
831 /// @brief Create a result type for fcmp/icmp
832 static Type* makeCmpResultType(Type* opnd_type) {
833 if (VectorType* vt = dyn_cast<VectorType>(opnd_type)) {
834 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
835 vt->getNumElements());
837 return Type::getInt1Ty(opnd_type->getContext());
840 // Shadow Value::setValueSubclassData with a private forwarding method so that
841 // subclasses cannot accidentally use it.
842 void setValueSubclassData(unsigned short D) {
843 Value::setValueSubclassData(D);
848 // FIXME: these are redundant if CmpInst < BinaryOperator
850 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<CmpInst, 2> {
853 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
855 } // End llvm namespace