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"
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(const Type *Ty, Instruction::TermOps iType,
38 Use *Ops, unsigned NumOps,
39 Instruction *InsertBefore = 0)
40 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
42 TerminatorInst(const 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(const Type *Ty, unsigned iType, Value *V,
96 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
99 UnaryInstruction(const 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> : public FixedNumOperandTraits<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); // Do not implement
142 void init(BinaryOps iType);
143 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
144 const Twine &Name, Instruction *InsertBefore);
145 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
146 const Twine &Name, BasicBlock *InsertAtEnd);
147 virtual BinaryOperator *clone_impl() const;
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 = "",
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"
196 /// CreateNSWAdd - Create an Add operator with the NSW flag set.
198 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
199 const Twine &Name = "") {
200 BinaryOperator *BO = CreateAdd(V1, V2, Name);
201 BO->setHasNoSignedWrap(true);
204 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
205 const Twine &Name, BasicBlock *BB) {
206 BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
207 BO->setHasNoSignedWrap(true);
210 static BinaryOperator *CreateNSWAdd(Value *V1, Value *V2,
211 const Twine &Name, Instruction *I) {
212 BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
213 BO->setHasNoSignedWrap(true);
217 /// CreateNUWAdd - Create an Add operator with the NUW flag set.
219 static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
220 const Twine &Name = "") {
221 BinaryOperator *BO = CreateAdd(V1, V2, Name);
222 BO->setHasNoUnsignedWrap(true);
225 static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
226 const Twine &Name, BasicBlock *BB) {
227 BinaryOperator *BO = CreateAdd(V1, V2, Name, BB);
228 BO->setHasNoUnsignedWrap(true);
231 static BinaryOperator *CreateNUWAdd(Value *V1, Value *V2,
232 const Twine &Name, Instruction *I) {
233 BinaryOperator *BO = CreateAdd(V1, V2, Name, I);
234 BO->setHasNoUnsignedWrap(true);
238 /// CreateNSWSub - Create an Sub operator with the NSW flag set.
240 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
241 const Twine &Name = "") {
242 BinaryOperator *BO = CreateSub(V1, V2, Name);
243 BO->setHasNoSignedWrap(true);
246 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
247 const Twine &Name, BasicBlock *BB) {
248 BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
249 BO->setHasNoSignedWrap(true);
252 static BinaryOperator *CreateNSWSub(Value *V1, Value *V2,
253 const Twine &Name, Instruction *I) {
254 BinaryOperator *BO = CreateSub(V1, V2, Name, I);
255 BO->setHasNoSignedWrap(true);
259 /// CreateNUWSub - Create an Sub operator with the NUW flag set.
261 static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
262 const Twine &Name = "") {
263 BinaryOperator *BO = CreateSub(V1, V2, Name);
264 BO->setHasNoUnsignedWrap(true);
267 static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
268 const Twine &Name, BasicBlock *BB) {
269 BinaryOperator *BO = CreateSub(V1, V2, Name, BB);
270 BO->setHasNoUnsignedWrap(true);
273 static BinaryOperator *CreateNUWSub(Value *V1, Value *V2,
274 const Twine &Name, Instruction *I) {
275 BinaryOperator *BO = CreateSub(V1, V2, Name, I);
276 BO->setHasNoUnsignedWrap(true);
280 /// CreateNSWMul - Create a Mul operator with the NSW flag set.
282 static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
283 const Twine &Name = "") {
284 BinaryOperator *BO = CreateMul(V1, V2, Name);
285 BO->setHasNoSignedWrap(true);
288 static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
289 const Twine &Name, BasicBlock *BB) {
290 BinaryOperator *BO = CreateMul(V1, V2, Name, BB);
291 BO->setHasNoSignedWrap(true);
294 static BinaryOperator *CreateNSWMul(Value *V1, Value *V2,
295 const Twine &Name, Instruction *I) {
296 BinaryOperator *BO = CreateMul(V1, V2, Name, I);
297 BO->setHasNoSignedWrap(true);
301 /// CreateExactSDiv - Create an SDiv operator with the exact flag set.
303 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
304 const Twine &Name = "") {
305 BinaryOperator *BO = CreateSDiv(V1, V2, Name);
306 BO->setIsExact(true);
309 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
310 const Twine &Name, BasicBlock *BB) {
311 BinaryOperator *BO = CreateSDiv(V1, V2, Name, BB);
312 BO->setIsExact(true);
315 static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
316 const Twine &Name, Instruction *I) {
317 BinaryOperator *BO = CreateSDiv(V1, V2, Name, I);
318 BO->setIsExact(true);
322 /// Helper functions to construct and inspect unary operations (NEG and NOT)
323 /// via binary operators SUB and XOR:
325 /// CreateNeg, CreateNot - Create the NEG and NOT
326 /// instructions out of SUB and XOR instructions.
328 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name = "",
329 Instruction *InsertBefore = 0);
330 static BinaryOperator *CreateNeg(Value *Op, const Twine &Name,
331 BasicBlock *InsertAtEnd);
332 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name = "",
333 Instruction *InsertBefore = 0);
334 static BinaryOperator *CreateNSWNeg(Value *Op, const Twine &Name,
335 BasicBlock *InsertAtEnd);
336 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name = "",
337 Instruction *InsertBefore = 0);
338 static BinaryOperator *CreateFNeg(Value *Op, const Twine &Name,
339 BasicBlock *InsertAtEnd);
340 static BinaryOperator *CreateNot(Value *Op, const Twine &Name = "",
341 Instruction *InsertBefore = 0);
342 static BinaryOperator *CreateNot(Value *Op, const Twine &Name,
343 BasicBlock *InsertAtEnd);
345 /// isNeg, isFNeg, isNot - Check if the given Value is a
346 /// NEG, FNeg, or NOT instruction.
348 static bool isNeg(const Value *V);
349 static bool isFNeg(const Value *V);
350 static bool isNot(const Value *V);
352 /// getNegArgument, getNotArgument - Helper functions to extract the
353 /// unary argument of a NEG, FNEG or NOT operation implemented via
354 /// Sub, FSub, or Xor.
356 static const Value *getNegArgument(const Value *BinOp);
357 static Value *getNegArgument( Value *BinOp);
358 static const Value *getFNegArgument(const Value *BinOp);
359 static Value *getFNegArgument( Value *BinOp);
360 static const Value *getNotArgument(const Value *BinOp);
361 static Value *getNotArgument( Value *BinOp);
363 BinaryOps getOpcode() const {
364 return static_cast<BinaryOps>(Instruction::getOpcode());
367 /// swapOperands - Exchange the two operands to this instruction.
368 /// This instruction is safe to use on any binary instruction and
369 /// does not modify the semantics of the instruction. If the instruction
370 /// cannot be reversed (ie, it's a Div), then return true.
374 /// setHasNoUnsignedWrap - Set or clear the nsw flag on this instruction,
375 /// which must be an operator which supports this flag. See LangRef.html
376 /// for the meaning of this flag.
377 void setHasNoUnsignedWrap(bool b = true);
379 /// setHasNoSignedWrap - Set or clear the nsw flag on this instruction,
380 /// which must be an operator which supports this flag. See LangRef.html
381 /// for the meaning of this flag.
382 void setHasNoSignedWrap(bool b = true);
384 /// setIsExact - Set or clear the exact flag on this instruction,
385 /// which must be an operator which supports this flag. See LangRef.html
386 /// for the meaning of this flag.
387 void setIsExact(bool b = true);
389 /// hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
390 bool hasNoUnsignedWrap() const;
392 /// hasNoSignedWrap - Determine whether the no signed wrap flag is set.
393 bool hasNoSignedWrap() const;
395 /// isExact - Determine whether the exact flag is set.
396 bool isExact() const;
398 // Methods for support type inquiry through isa, cast, and dyn_cast:
399 static inline bool classof(const BinaryOperator *) { return true; }
400 static inline bool classof(const Instruction *I) {
401 return I->isBinaryOp();
403 static inline bool classof(const Value *V) {
404 return isa<Instruction>(V) && classof(cast<Instruction>(V));
409 struct OperandTraits<BinaryOperator> : public FixedNumOperandTraits<2> {
412 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
414 //===----------------------------------------------------------------------===//
416 //===----------------------------------------------------------------------===//
418 /// CastInst - This is the base class for all instructions that perform data
419 /// casts. It is simply provided so that instruction category testing
420 /// can be performed with code like:
422 /// if (isa<CastInst>(Instr)) { ... }
423 /// @brief Base class of casting instructions.
424 class CastInst : public UnaryInstruction {
426 /// @brief Constructor with insert-before-instruction semantics for subclasses
427 CastInst(const Type *Ty, unsigned iType, Value *S,
428 const Twine &NameStr = "", Instruction *InsertBefore = 0)
429 : UnaryInstruction(Ty, iType, S, InsertBefore) {
432 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
433 CastInst(const Type *Ty, unsigned iType, Value *S,
434 const Twine &NameStr, BasicBlock *InsertAtEnd)
435 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
439 /// Provides a way to construct any of the CastInst subclasses using an
440 /// opcode instead of the subclass's constructor. The opcode must be in the
441 /// CastOps category (Instruction::isCast(opcode) returns true). This
442 /// constructor has insert-before-instruction semantics to automatically
443 /// insert the new CastInst before InsertBefore (if it is non-null).
444 /// @brief Construct any of the CastInst subclasses
445 static CastInst *Create(
446 Instruction::CastOps, ///< The opcode of the cast instruction
447 Value *S, ///< The value to be casted (operand 0)
448 const Type *Ty, ///< The type to which cast should be made
449 const Twine &Name = "", ///< Name for the instruction
450 Instruction *InsertBefore = 0 ///< Place to insert the instruction
452 /// Provides a way to construct any of the CastInst subclasses using an
453 /// opcode instead of the subclass's constructor. The opcode must be in the
454 /// CastOps category. This constructor has insert-at-end-of-block semantics
455 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
457 /// @brief Construct any of the CastInst subclasses
458 static CastInst *Create(
459 Instruction::CastOps, ///< The opcode for the cast instruction
460 Value *S, ///< The value to be casted (operand 0)
461 const Type *Ty, ///< The type to which operand is casted
462 const Twine &Name, ///< The name for the instruction
463 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
466 /// @brief Create a ZExt or BitCast cast instruction
467 static CastInst *CreateZExtOrBitCast(
468 Value *S, ///< The value to be casted (operand 0)
469 const Type *Ty, ///< The type to which cast should be made
470 const Twine &Name = "", ///< Name for the instruction
471 Instruction *InsertBefore = 0 ///< Place to insert the instruction
474 /// @brief Create a ZExt or BitCast cast instruction
475 static CastInst *CreateZExtOrBitCast(
476 Value *S, ///< The value to be casted (operand 0)
477 const Type *Ty, ///< The type to which operand is casted
478 const Twine &Name, ///< The name for the instruction
479 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
482 /// @brief Create a SExt or BitCast cast instruction
483 static CastInst *CreateSExtOrBitCast(
484 Value *S, ///< The value to be casted (operand 0)
485 const Type *Ty, ///< The type to which cast should be made
486 const Twine &Name = "", ///< Name for the instruction
487 Instruction *InsertBefore = 0 ///< Place to insert the instruction
490 /// @brief Create a SExt or BitCast cast instruction
491 static CastInst *CreateSExtOrBitCast(
492 Value *S, ///< The value to be casted (operand 0)
493 const Type *Ty, ///< The type to which operand is casted
494 const Twine &Name, ///< The name for the instruction
495 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
498 /// @brief Create a BitCast or a PtrToInt cast instruction
499 static CastInst *CreatePointerCast(
500 Value *S, ///< The pointer value to be casted (operand 0)
501 const Type *Ty, ///< The type to which operand is casted
502 const Twine &Name, ///< The name for the instruction
503 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
506 /// @brief Create a BitCast or a PtrToInt cast instruction
507 static CastInst *CreatePointerCast(
508 Value *S, ///< The pointer value to be casted (operand 0)
509 const Type *Ty, ///< The type to which cast should be made
510 const Twine &Name = "", ///< Name for the instruction
511 Instruction *InsertBefore = 0 ///< Place to insert the instruction
514 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
515 static CastInst *CreateIntegerCast(
516 Value *S, ///< The pointer value to be casted (operand 0)
517 const Type *Ty, ///< The type to which cast should be made
518 bool isSigned, ///< Whether to regard S as signed or not
519 const Twine &Name = "", ///< Name for the instruction
520 Instruction *InsertBefore = 0 ///< Place to insert the instruction
523 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
524 static CastInst *CreateIntegerCast(
525 Value *S, ///< The integer value to be casted (operand 0)
526 const Type *Ty, ///< The integer type to which operand is casted
527 bool isSigned, ///< Whether to regard S as signed or not
528 const Twine &Name, ///< The name for the instruction
529 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
532 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
533 static CastInst *CreateFPCast(
534 Value *S, ///< The floating point value to be casted
535 const Type *Ty, ///< The floating point type to cast to
536 const Twine &Name = "", ///< Name for the instruction
537 Instruction *InsertBefore = 0 ///< Place to insert the instruction
540 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
541 static CastInst *CreateFPCast(
542 Value *S, ///< The floating point value to be casted
543 const Type *Ty, ///< The floating point type to cast to
544 const Twine &Name, ///< The name for the instruction
545 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
548 /// @brief Create a Trunc or BitCast cast instruction
549 static CastInst *CreateTruncOrBitCast(
550 Value *S, ///< The value to be casted (operand 0)
551 const Type *Ty, ///< The type to which cast should be made
552 const Twine &Name = "", ///< Name for the instruction
553 Instruction *InsertBefore = 0 ///< Place to insert the instruction
556 /// @brief Create a Trunc or BitCast cast instruction
557 static CastInst *CreateTruncOrBitCast(
558 Value *S, ///< The value to be casted (operand 0)
559 const Type *Ty, ///< The type to which operand is casted
560 const Twine &Name, ///< The name for the instruction
561 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
564 /// @brief Check whether it is valid to call getCastOpcode for these types.
565 static bool isCastable(
566 const Type *SrcTy, ///< The Type from which the value should be cast.
567 const Type *DestTy ///< The Type to which the value should be cast.
570 /// Returns the opcode necessary to cast Val into Ty using usual casting
572 /// @brief Infer the opcode for cast operand and type
573 static Instruction::CastOps getCastOpcode(
574 const Value *Val, ///< The value to cast
575 bool SrcIsSigned, ///< Whether to treat the source as signed
576 const Type *Ty, ///< The Type to which the value should be casted
577 bool DstIsSigned ///< Whether to treate the dest. as signed
580 /// There are several places where we need to know if a cast instruction
581 /// only deals with integer source and destination types. To simplify that
582 /// logic, this method is provided.
583 /// @returns true iff the cast has only integral typed operand and dest type.
584 /// @brief Determine if this is an integer-only cast.
585 bool isIntegerCast() const;
587 /// A lossless cast is one that does not alter the basic value. It implies
588 /// a no-op cast but is more stringent, preventing things like int->float,
589 /// long->double, int->ptr, or vector->anything.
590 /// @returns true iff the cast is lossless.
591 /// @brief Determine if this is a lossless cast.
592 bool isLosslessCast() const;
594 /// A no-op cast is one that can be effected without changing any bits.
595 /// It implies that the source and destination types are the same size. The
596 /// IntPtrTy argument is used to make accurate determinations for casts
597 /// involving Integer and Pointer types. They are no-op casts if the integer
598 /// is the same size as the pointer. However, pointer size varies with
599 /// platform. Generally, the result of TargetData::getIntPtrType() should be
600 /// passed in. If that's not available, use Type::Int64Ty, which will make
601 /// the isNoopCast call conservative.
602 /// @brief Determine if this cast is a no-op cast.
604 const Type *IntPtrTy ///< Integer type corresponding to pointer
607 /// Determine how a pair of casts can be eliminated, if they can be at all.
608 /// This is a helper function for both CastInst and ConstantExpr.
609 /// @returns 0 if the CastInst pair can't be eliminated
610 /// @returns Instruction::CastOps value for a cast that can replace
611 /// the pair, casting SrcTy to DstTy.
612 /// @brief Determine if a cast pair is eliminable
613 static unsigned isEliminableCastPair(
614 Instruction::CastOps firstOpcode, ///< Opcode of first cast
615 Instruction::CastOps secondOpcode, ///< Opcode of second cast
616 const Type *SrcTy, ///< SrcTy of 1st cast
617 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
618 const Type *DstTy, ///< DstTy of 2nd cast
619 const Type *IntPtrTy ///< Integer type corresponding to Ptr types, or null
622 /// @brief Return the opcode of this CastInst
623 Instruction::CastOps getOpcode() const {
624 return Instruction::CastOps(Instruction::getOpcode());
627 /// @brief Return the source type, as a convenience
628 const Type* getSrcTy() const { return getOperand(0)->getType(); }
629 /// @brief Return the destination type, as a convenience
630 const Type* getDestTy() const { return getType(); }
632 /// This method can be used to determine if a cast from S to DstTy using
633 /// Opcode op is valid or not.
634 /// @returns true iff the proposed cast is valid.
635 /// @brief Determine if a cast is valid without creating one.
636 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
638 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
639 static inline bool classof(const CastInst *) { return true; }
640 static inline bool classof(const Instruction *I) {
643 static inline bool classof(const Value *V) {
644 return isa<Instruction>(V) && classof(cast<Instruction>(V));
648 //===----------------------------------------------------------------------===//
650 //===----------------------------------------------------------------------===//
652 /// This class is the base class for the comparison instructions.
653 /// @brief Abstract base class of comparison instructions.
654 // FIXME: why not derive from BinaryOperator?
655 class CmpInst: public Instruction {
656 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
657 CmpInst(); // do not implement
659 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
660 Value *LHS, Value *RHS, const Twine &Name = "",
661 Instruction *InsertBefore = 0);
663 CmpInst(const Type *ty, Instruction::OtherOps op, unsigned short pred,
664 Value *LHS, Value *RHS, const Twine &Name,
665 BasicBlock *InsertAtEnd);
668 /// This enumeration lists the possible predicates for CmpInst subclasses.
669 /// Values in the range 0-31 are reserved for FCmpInst, while values in the
670 /// range 32-64 are reserved for ICmpInst. This is necessary to ensure the
671 /// predicate values are not overlapping between the classes.
673 // Opcode U L G E Intuitive operation
674 FCMP_FALSE = 0, /// 0 0 0 0 Always false (always folded)
675 FCMP_OEQ = 1, /// 0 0 0 1 True if ordered and equal
676 FCMP_OGT = 2, /// 0 0 1 0 True if ordered and greater than
677 FCMP_OGE = 3, /// 0 0 1 1 True if ordered and greater than or equal
678 FCMP_OLT = 4, /// 0 1 0 0 True if ordered and less than
679 FCMP_OLE = 5, /// 0 1 0 1 True if ordered and less than or equal
680 FCMP_ONE = 6, /// 0 1 1 0 True if ordered and operands are unequal
681 FCMP_ORD = 7, /// 0 1 1 1 True if ordered (no nans)
682 FCMP_UNO = 8, /// 1 0 0 0 True if unordered: isnan(X) | isnan(Y)
683 FCMP_UEQ = 9, /// 1 0 0 1 True if unordered or equal
684 FCMP_UGT = 10, /// 1 0 1 0 True if unordered or greater than
685 FCMP_UGE = 11, /// 1 0 1 1 True if unordered, greater than, or equal
686 FCMP_ULT = 12, /// 1 1 0 0 True if unordered or less than
687 FCMP_ULE = 13, /// 1 1 0 1 True if unordered, less than, or equal
688 FCMP_UNE = 14, /// 1 1 1 0 True if unordered or not equal
689 FCMP_TRUE = 15, /// 1 1 1 1 Always true (always folded)
690 FIRST_FCMP_PREDICATE = FCMP_FALSE,
691 LAST_FCMP_PREDICATE = FCMP_TRUE,
692 BAD_FCMP_PREDICATE = FCMP_TRUE + 1,
693 ICMP_EQ = 32, /// equal
694 ICMP_NE = 33, /// not equal
695 ICMP_UGT = 34, /// unsigned greater than
696 ICMP_UGE = 35, /// unsigned greater or equal
697 ICMP_ULT = 36, /// unsigned less than
698 ICMP_ULE = 37, /// unsigned less or equal
699 ICMP_SGT = 38, /// signed greater than
700 ICMP_SGE = 39, /// signed greater or equal
701 ICMP_SLT = 40, /// signed less than
702 ICMP_SLE = 41, /// signed less or equal
703 FIRST_ICMP_PREDICATE = ICMP_EQ,
704 LAST_ICMP_PREDICATE = ICMP_SLE,
705 BAD_ICMP_PREDICATE = ICMP_SLE + 1
708 // allocate space for exactly two operands
709 void *operator new(size_t s) {
710 return User::operator new(s, 2);
712 /// Construct a compare instruction, given the opcode, the predicate and
713 /// the two operands. Optionally (if InstBefore is specified) insert the
714 /// instruction into a BasicBlock right before the specified instruction.
715 /// The specified Instruction is allowed to be a dereferenced end iterator.
716 /// @brief Create a CmpInst
717 static CmpInst *Create(OtherOps Op,
718 unsigned short predicate, Value *S1,
719 Value *S2, const Twine &Name = "",
720 Instruction *InsertBefore = 0);
722 /// Construct a compare instruction, given the opcode, the predicate and the
723 /// two operands. Also automatically insert this instruction to the end of
724 /// the BasicBlock specified.
725 /// @brief Create a CmpInst
726 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
727 Value *S2, const Twine &Name, BasicBlock *InsertAtEnd);
729 /// @brief Get the opcode casted to the right type
730 OtherOps getOpcode() const {
731 return static_cast<OtherOps>(Instruction::getOpcode());
734 /// @brief Return the predicate for this instruction.
735 Predicate getPredicate() const { return Predicate(SubclassData); }
737 /// @brief Set the predicate for this instruction to the specified value.
738 void setPredicate(Predicate P) { SubclassData = P; }
740 static bool isFPPredicate(Predicate P) {
741 return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE;
744 static bool isIntPredicate(Predicate P) {
745 return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE;
748 bool isFPPredicate() const { return isFPPredicate(getPredicate()); }
749 bool isIntPredicate() const { return isIntPredicate(getPredicate()); }
752 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
753 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
754 /// @returns the inverse predicate for the instruction's current predicate.
755 /// @brief Return the inverse of the instruction's predicate.
756 Predicate getInversePredicate() const {
757 return getInversePredicate(getPredicate());
760 /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE,
761 /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
762 /// @returns the inverse predicate for predicate provided in \p pred.
763 /// @brief Return the inverse of a given predicate
764 static Predicate getInversePredicate(Predicate pred);
766 /// For example, EQ->EQ, SLE->SGE, ULT->UGT,
767 /// OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
768 /// @returns the predicate that would be the result of exchanging the two
769 /// operands of the CmpInst instruction without changing the result
771 /// @brief Return the predicate as if the operands were swapped
772 Predicate getSwappedPredicate() const {
773 return getSwappedPredicate(getPredicate());
776 /// This is a static version that you can use without an instruction
778 /// @brief Return the predicate as if the operands were swapped.
779 static Predicate getSwappedPredicate(Predicate pred);
781 /// @brief Provide more efficient getOperand methods.
782 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
784 /// This is just a convenience that dispatches to the subclasses.
785 /// @brief Swap the operands and adjust predicate accordingly to retain
786 /// the same comparison.
789 /// This is just a convenience that dispatches to the subclasses.
790 /// @brief Determine if this CmpInst is commutative.
791 bool isCommutative();
793 /// This is just a convenience that dispatches to the subclasses.
794 /// @brief Determine if this is an equals/not equals predicate.
797 /// @returns true if the comparison is signed, false otherwise.
798 /// @brief Determine if this instruction is using a signed comparison.
799 bool isSigned() const {
800 return isSigned(getPredicate());
803 /// @returns true if the comparison is unsigned, false otherwise.
804 /// @brief Determine if this instruction is using an unsigned comparison.
805 bool isUnsigned() const {
806 return isUnsigned(getPredicate());
809 /// This is just a convenience.
810 /// @brief Determine if this is true when both operands are the same.
811 bool isTrueWhenEqual() const {
812 return isTrueWhenEqual(getPredicate());
815 /// This is just a convenience.
816 /// @brief Determine if this is false when both operands are the same.
817 bool isFalseWhenEqual() const {
818 return isFalseWhenEqual(getPredicate());
821 /// @returns true if the predicate is unsigned, false otherwise.
822 /// @brief Determine if the predicate is an unsigned operation.
823 static bool isUnsigned(unsigned short predicate);
825 /// @returns true if the predicate is signed, false otherwise.
826 /// @brief Determine if the predicate is an signed operation.
827 static bool isSigned(unsigned short predicate);
829 /// @brief Determine if the predicate is an ordered operation.
830 static bool isOrdered(unsigned short predicate);
832 /// @brief Determine if the predicate is an unordered operation.
833 static bool isUnordered(unsigned short predicate);
835 /// Determine if the predicate is true when comparing a value with itself.
836 static bool isTrueWhenEqual(unsigned short predicate);
838 /// Determine if the predicate is false when comparing a value with itself.
839 static bool isFalseWhenEqual(unsigned short predicate);
841 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
842 static inline bool classof(const CmpInst *) { return true; }
843 static inline bool classof(const Instruction *I) {
844 return I->getOpcode() == Instruction::ICmp ||
845 I->getOpcode() == Instruction::FCmp;
847 static inline bool classof(const Value *V) {
848 return isa<Instruction>(V) && classof(cast<Instruction>(V));
851 /// @brief Create a result type for fcmp/icmp
852 static const Type* makeCmpResultType(const Type* opnd_type) {
853 if (const VectorType* vt = dyn_cast<const VectorType>(opnd_type)) {
854 return VectorType::get(Type::getInt1Ty(opnd_type->getContext()),
855 vt->getNumElements());
857 return Type::getInt1Ty(opnd_type->getContext());
862 // FIXME: these are redundant if CmpInst < BinaryOperator
864 struct OperandTraits<CmpInst> : public FixedNumOperandTraits<2> {
867 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
869 } // End llvm namespace