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"
24 //===----------------------------------------------------------------------===//
25 // TerminatorInst Class
26 //===----------------------------------------------------------------------===//
28 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
29 /// block. Thus, these are all the flow control type of operations.
31 class TerminatorInst : public Instruction {
33 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
34 Use *Ops, unsigned NumOps,
35 Instruction *InsertBefore = 0)
36 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
38 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
39 Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
40 : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
42 // Out of line virtual method, so the vtable, etc has a home.
45 /// Virtual methods - Terminators should overload these and provide inline
46 /// overrides of non-V methods.
47 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
48 virtual unsigned getNumSuccessorsV() const = 0;
49 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
52 virtual Instruction *clone() const = 0;
54 /// getNumSuccessors - Return the number of successors that this terminator
56 unsigned getNumSuccessors() const {
57 return getNumSuccessorsV();
60 /// getSuccessor - Return the specified successor.
62 BasicBlock *getSuccessor(unsigned idx) const {
63 return getSuccessorV(idx);
66 /// setSuccessor - Update the specified successor to point at the provided
68 void setSuccessor(unsigned idx, BasicBlock *B) {
69 setSuccessorV(idx, B);
72 // Methods for support type inquiry through isa, cast, and dyn_cast:
73 static inline bool classof(const TerminatorInst *) { return true; }
74 static inline bool classof(const Instruction *I) {
75 return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
77 static inline bool classof(const Value *V) {
78 return isa<Instruction>(V) && classof(cast<Instruction>(V));
83 //===----------------------------------------------------------------------===//
84 // UnaryInstruction Class
85 //===----------------------------------------------------------------------===//
87 class UnaryInstruction : public Instruction {
88 void *operator new(size_t, unsigned); // Do not implement
91 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB = 0)
92 : Instruction(Ty, iType, &Op<0>(), 1, IB) {
95 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
96 : Instruction(Ty, iType, &Op<0>(), 1, IAE) {
100 // allocate space for exactly one operand
101 void *operator new(size_t s) {
102 return User::operator new(s, 1);
105 // Out of line virtual method, so the vtable, etc has a home.
108 /// Transparently provide more efficient getOperand methods.
109 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
111 // Methods for support type inquiry through isa, cast, and dyn_cast:
112 static inline bool classof(const UnaryInstruction *) { return true; }
113 static inline bool classof(const Instruction *I) {
114 return I->getOpcode() == Instruction::Malloc ||
115 I->getOpcode() == Instruction::Alloca ||
116 I->getOpcode() == Instruction::Free ||
117 I->getOpcode() == Instruction::Load ||
118 I->getOpcode() == Instruction::VAArg ||
119 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
121 static inline bool classof(const Value *V) {
122 return isa<Instruction>(V) && classof(cast<Instruction>(V));
127 struct OperandTraits<UnaryInstruction> : FixedNumOperandTraits<1> {
130 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryInstruction, Value)
132 //===----------------------------------------------------------------------===//
133 // BinaryOperator Class
134 //===----------------------------------------------------------------------===//
136 class BinaryOperator : public Instruction {
137 void *operator new(size_t, unsigned); // Do not implement
139 void init(BinaryOps iType);
140 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
141 const std::string &Name, Instruction *InsertBefore);
142 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
143 const std::string &Name, BasicBlock *InsertAtEnd);
145 // allocate space for exactly two operands
146 void *operator new(size_t s) {
147 return User::operator new(s, 2);
150 /// Transparently provide more efficient getOperand methods.
151 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
153 /// create() - Construct a binary instruction, given the opcode and the two
154 /// operands. Optionally (if InstBefore is specified) insert the instruction
155 /// into a BasicBlock right before the specified instruction. The specified
156 /// Instruction is allowed to be a dereferenced end iterator.
158 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
159 const std::string &Name = "",
160 Instruction *InsertBefore = 0);
162 /// create() - Construct a binary instruction, given the opcode and the two
163 /// operands. Also automatically insert this instruction to the end of the
164 /// BasicBlock specified.
166 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
167 const std::string &Name,
168 BasicBlock *InsertAtEnd);
170 /// create* - These methods just forward to create, and are useful when you
171 /// statically know what type of instruction you're going to create. These
172 /// helpers just save some typing.
173 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
174 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
175 const std::string &Name = "") {\
176 return create(Instruction::OPC, V1, V2, Name);\
178 #include "llvm/Instruction.def"
179 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
180 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
181 const std::string &Name, BasicBlock *BB) {\
182 return create(Instruction::OPC, V1, V2, Name, BB);\
184 #include "llvm/Instruction.def"
185 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
186 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
187 const std::string &Name, Instruction *I) {\
188 return create(Instruction::OPC, V1, V2, Name, I);\
190 #include "llvm/Instruction.def"
193 /// Helper functions to construct and inspect unary operations (NEG and NOT)
194 /// via binary operators SUB and XOR:
196 /// createNeg, createNot - Create the NEG and NOT
197 /// instructions out of SUB and XOR instructions.
199 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
200 Instruction *InsertBefore = 0);
201 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
202 BasicBlock *InsertAtEnd);
203 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
204 Instruction *InsertBefore = 0);
205 static BinaryOperator *createNot(Value *Op, const std::string &Name,
206 BasicBlock *InsertAtEnd);
208 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
210 static bool isNeg(const Value *V);
211 static bool isNot(const Value *V);
213 /// getNegArgument, getNotArgument - Helper functions to extract the
214 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
216 static const Value *getNegArgument(const Value *BinOp);
217 static Value *getNegArgument( Value *BinOp);
218 static const Value *getNotArgument(const Value *BinOp);
219 static Value *getNotArgument( Value *BinOp);
221 BinaryOps getOpcode() const {
222 return static_cast<BinaryOps>(Instruction::getOpcode());
225 virtual BinaryOperator *clone() const;
227 /// swapOperands - Exchange the two operands to this instruction.
228 /// This instruction is safe to use on any binary instruction and
229 /// does not modify the semantics of the instruction. If the instruction
230 /// cannot be reversed (ie, it's a Div), then return true.
234 // Methods for support type inquiry through isa, cast, and dyn_cast:
235 static inline bool classof(const BinaryOperator *) { return true; }
236 static inline bool classof(const Instruction *I) {
237 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
239 static inline bool classof(const Value *V) {
240 return isa<Instruction>(V) && classof(cast<Instruction>(V));
245 struct OperandTraits<BinaryOperator> : FixedNumOperandTraits<2> {
248 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryOperator, Value)
250 //===----------------------------------------------------------------------===//
252 //===----------------------------------------------------------------------===//
254 /// CastInst - This is the base class for all instructions that perform data
255 /// casts. It is simply provided so that instruction category testing
256 /// can be performed with code like:
258 /// if (isa<CastInst>(Instr)) { ... }
259 /// @brief Base class of casting instructions.
260 class CastInst : public UnaryInstruction {
261 /// @brief Copy constructor
262 CastInst(const CastInst &CI)
263 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
265 /// @brief Do not allow default construction
268 /// @brief Constructor with insert-before-instruction semantics for subclasses
269 CastInst(const Type *Ty, unsigned iType, Value *S,
270 const std::string &Name = "", Instruction *InsertBefore = 0)
271 : UnaryInstruction(Ty, iType, S, InsertBefore) {
274 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
275 CastInst(const Type *Ty, unsigned iType, Value *S,
276 const std::string &Name, BasicBlock *InsertAtEnd)
277 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
281 /// Provides a way to construct any of the CastInst subclasses using an
282 /// opcode instead of the subclass's constructor. The opcode must be in the
283 /// CastOps category (Instruction::isCast(opcode) returns true). This
284 /// constructor has insert-before-instruction semantics to automatically
285 /// insert the new CastInst before InsertBefore (if it is non-null).
286 /// @brief Construct any of the CastInst subclasses
287 static CastInst *create(
288 Instruction::CastOps, ///< The opcode of the cast instruction
289 Value *S, ///< The value to be casted (operand 0)
290 const Type *Ty, ///< The type to which cast should be made
291 const std::string &Name = "", ///< Name for the instruction
292 Instruction *InsertBefore = 0 ///< Place to insert the instruction
294 /// Provides a way to construct any of the CastInst subclasses using an
295 /// opcode instead of the subclass's constructor. The opcode must be in the
296 /// CastOps category. This constructor has insert-at-end-of-block semantics
297 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
299 /// @brief Construct any of the CastInst subclasses
300 static CastInst *create(
301 Instruction::CastOps, ///< The opcode for the cast instruction
302 Value *S, ///< The value to be casted (operand 0)
303 const Type *Ty, ///< The type to which operand is casted
304 const std::string &Name, ///< The name for the instruction
305 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
308 /// @brief Create a ZExt or BitCast cast instruction
309 static CastInst *createZExtOrBitCast(
310 Value *S, ///< The value to be casted (operand 0)
311 const Type *Ty, ///< The type to which cast should be made
312 const std::string &Name = "", ///< Name for the instruction
313 Instruction *InsertBefore = 0 ///< Place to insert the instruction
316 /// @brief Create a ZExt or BitCast cast instruction
317 static CastInst *createZExtOrBitCast(
318 Value *S, ///< The value to be casted (operand 0)
319 const Type *Ty, ///< The type to which operand is casted
320 const std::string &Name, ///< The name for the instruction
321 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
324 /// @brief Create a SExt or BitCast cast instruction
325 static CastInst *createSExtOrBitCast(
326 Value *S, ///< The value to be casted (operand 0)
327 const Type *Ty, ///< The type to which cast should be made
328 const std::string &Name = "", ///< Name for the instruction
329 Instruction *InsertBefore = 0 ///< Place to insert the instruction
332 /// @brief Create a BitCast or a PtrToInt cast instruction
333 static CastInst *createPointerCast(
334 Value *S, ///< The pointer value to be casted (operand 0)
335 const Type *Ty, ///< The type to which operand is casted
336 const std::string &Name, ///< The name for the instruction
337 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
340 /// @brief Create a BitCast or a PtrToInt cast instruction
341 static CastInst *createPointerCast(
342 Value *S, ///< The pointer value to be casted (operand 0)
343 const Type *Ty, ///< The type to which cast should be made
344 const std::string &Name = "", ///< Name for the instruction
345 Instruction *InsertBefore = 0 ///< Place to insert the instruction
348 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
349 static CastInst *createIntegerCast(
350 Value *S, ///< The pointer value to be casted (operand 0)
351 const Type *Ty, ///< The type to which cast should be made
352 bool isSigned, ///< Whether to regard S as signed or not
353 const std::string &Name = "", ///< Name for the instruction
354 Instruction *InsertBefore = 0 ///< Place to insert the instruction
357 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
358 static CastInst *createIntegerCast(
359 Value *S, ///< The integer value to be casted (operand 0)
360 const Type *Ty, ///< The integer type to which operand is casted
361 bool isSigned, ///< Whether to regard S as signed or not
362 const std::string &Name, ///< The name for the instruction
363 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
366 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
367 static CastInst *createFPCast(
368 Value *S, ///< The floating point value to be casted
369 const Type *Ty, ///< The floating point type to cast to
370 const std::string &Name = "", ///< Name for the instruction
371 Instruction *InsertBefore = 0 ///< Place to insert the instruction
374 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
375 static CastInst *createFPCast(
376 Value *S, ///< The floating point value to be casted
377 const Type *Ty, ///< The floating point type to cast to
378 const std::string &Name, ///< The name for the instruction
379 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
382 /// @brief Create a SExt or BitCast cast instruction
383 static CastInst *createSExtOrBitCast(
384 Value *S, ///< The value to be casted (operand 0)
385 const Type *Ty, ///< The type to which operand is casted
386 const std::string &Name, ///< The name for the instruction
387 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
390 /// @brief Create a Trunc or BitCast cast instruction
391 static CastInst *createTruncOrBitCast(
392 Value *S, ///< The value to be casted (operand 0)
393 const Type *Ty, ///< The type to which cast should be made
394 const std::string &Name = "", ///< Name for the instruction
395 Instruction *InsertBefore = 0 ///< Place to insert the instruction
398 /// @brief Create a Trunc or BitCast cast instruction
399 static CastInst *createTruncOrBitCast(
400 Value *S, ///< The value to be casted (operand 0)
401 const Type *Ty, ///< The type to which operand is casted
402 const std::string &Name, ///< The name for the instruction
403 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
406 /// @brief Check whether it is valid to call getCastOpcode for these types.
407 static bool isCastable(
408 const Type *SrcTy, ///< The Type from which the value should be cast.
409 const Type *DestTy ///< The Type to which the value should be cast.
412 /// Returns the opcode necessary to cast Val into Ty using usual casting
414 /// @brief Infer the opcode for cast operand and type
415 static Instruction::CastOps getCastOpcode(
416 const Value *Val, ///< The value to cast
417 bool SrcIsSigned, ///< Whether to treat the source as signed
418 const Type *Ty, ///< The Type to which the value should be casted
419 bool DstIsSigned ///< Whether to treate the dest. as signed
422 /// There are several places where we need to know if a cast instruction
423 /// only deals with integer source and destination types. To simplify that
424 /// logic, this method is provided.
425 /// @returns true iff the cast has only integral typed operand and dest type.
426 /// @brief Determine if this is an integer-only cast.
427 bool isIntegerCast() const;
429 /// A lossless cast is one that does not alter the basic value. It implies
430 /// a no-op cast but is more stringent, preventing things like int->float,
431 /// long->double, int->ptr, or vector->anything.
432 /// @returns true iff the cast is lossless.
433 /// @brief Determine if this is a lossless cast.
434 bool isLosslessCast() const;
436 /// A no-op cast is one that can be effected without changing any bits.
437 /// It implies that the source and destination types are the same size. The
438 /// IntPtrTy argument is used to make accurate determinations for casts
439 /// involving Integer and Pointer types. They are no-op casts if the integer
440 /// is the same size as the pointer. However, pointer size varies with
441 /// platform. Generally, the result of TargetData::getIntPtrType() should be
442 /// passed in. If that's not available, use Type::Int64Ty, which will make
443 /// the isNoopCast call conservative.
444 /// @brief Determine if this cast is a no-op cast.
446 const Type *IntPtrTy ///< Integer type corresponding to pointer
449 /// Determine how a pair of casts can be eliminated, if they can be at all.
450 /// This is a helper function for both CastInst and ConstantExpr.
451 /// @returns 0 if the CastInst pair can't be eliminated
452 /// @returns Instruction::CastOps value for a cast that can replace
453 /// the pair, casting SrcTy to DstTy.
454 /// @brief Determine if a cast pair is eliminable
455 static unsigned isEliminableCastPair(
456 Instruction::CastOps firstOpcode, ///< Opcode of first cast
457 Instruction::CastOps secondOpcode, ///< Opcode of second cast
458 const Type *SrcTy, ///< SrcTy of 1st cast
459 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
460 const Type *DstTy, ///< DstTy of 2nd cast
461 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
464 /// @brief Return the opcode of this CastInst
465 Instruction::CastOps getOpcode() const {
466 return Instruction::CastOps(Instruction::getOpcode());
469 /// @brief Return the source type, as a convenience
470 const Type* getSrcTy() const { return getOperand(0)->getType(); }
471 /// @brief Return the destination type, as a convenience
472 const Type* getDestTy() const { return getType(); }
474 /// This method can be used to determine if a cast from S to DstTy using
475 /// Opcode op is valid or not.
476 /// @returns true iff the proposed cast is valid.
477 /// @brief Determine if a cast is valid without creating one.
478 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
480 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
481 static inline bool classof(const CastInst *) { return true; }
482 static inline bool classof(const Instruction *I) {
483 return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
485 static inline bool classof(const Value *V) {
486 return isa<Instruction>(V) && classof(cast<Instruction>(V));
490 //===----------------------------------------------------------------------===//
492 //===----------------------------------------------------------------------===//
494 /// This class is the base class for the comparison instructions.
495 /// @brief Abstract base class of comparison instructions.
496 // FIXME: why not derive from BinaryOperator?
497 class CmpInst: public Instruction {
498 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
499 CmpInst(); // do not implement
501 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
502 const std::string &Name = "", Instruction *InsertBefore = 0);
504 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
505 const std::string &Name, BasicBlock *InsertAtEnd);
508 // allocate space for exactly two operands
509 void *operator new(size_t s) {
510 return User::operator new(s, 2);
512 /// Construct a compare instruction, given the opcode, the predicate and
513 /// the two operands. Optionally (if InstBefore is specified) insert the
514 /// instruction into a BasicBlock right before the specified instruction.
515 /// The specified Instruction is allowed to be a dereferenced end iterator.
516 /// @brief Create a CmpInst
517 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
518 Value *S2, const std::string &Name = "",
519 Instruction *InsertBefore = 0);
521 /// Construct a compare instruction, given the opcode, the predicate and the
522 /// two operands. Also automatically insert this instruction to the end of
523 /// the BasicBlock specified.
524 /// @brief Create a CmpInst
525 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
526 Value *S2, const std::string &Name,
527 BasicBlock *InsertAtEnd);
529 /// @brief Get the opcode casted to the right type
530 OtherOps getOpcode() const {
531 return static_cast<OtherOps>(Instruction::getOpcode());
534 /// The predicate for CmpInst is defined by the subclasses but stored in
535 /// the SubclassData field (see Value.h). We allow it to be fetched here
536 /// as the predicate but there is no enum type for it, just the raw unsigned
537 /// short. This facilitates comparison of CmpInst instances without delving
538 /// into the subclasses since predicate values are distinct between the
539 /// CmpInst subclasses.
540 /// @brief Return the predicate for this instruction.
541 unsigned short getPredicate() const {
545 /// @brief Provide more efficient getOperand methods.
546 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
548 /// This is just a convenience that dispatches to the subclasses.
549 /// @brief Swap the operands and adjust predicate accordingly to retain
550 /// the same comparison.
553 /// This is just a convenience that dispatches to the subclasses.
554 /// @brief Determine if this CmpInst is commutative.
555 bool isCommutative();
557 /// This is just a convenience that dispatches to the subclasses.
558 /// @brief Determine if this is an equals/not equals predicate.
561 /// @returns true if the predicate is unsigned, false otherwise.
562 /// @brief Determine if the predicate is an unsigned operation.
563 static bool isUnsigned(unsigned short predicate);
565 /// @returns true if the predicate is signed, false otherwise.
566 /// @brief Determine if the predicate is an signed operation.
567 static bool isSigned(unsigned short predicate);
569 /// @brief Determine if the predicate is an ordered operation.
570 static bool isOrdered(unsigned short predicate);
572 /// @brief Determine if the predicate is an unordered operation.
573 static bool isUnordered(unsigned short predicate);
575 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
576 static inline bool classof(const CmpInst *) { return true; }
577 static inline bool classof(const Instruction *I) {
578 return I->getOpcode() == Instruction::ICmp ||
579 I->getOpcode() == Instruction::FCmp;
581 static inline bool classof(const Value *V) {
582 return isa<Instruction>(V) && classof(cast<Instruction>(V));
587 // FIXME: these are redundant if CmpInst < BinaryOperator
589 struct OperandTraits<CmpInst> : FixedNumOperandTraits<2> {
592 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CmpInst, Value)
594 } // End llvm namespace