1 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
23 //===----------------------------------------------------------------------===//
24 // TerminatorInst Class
25 //===----------------------------------------------------------------------===//
27 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
28 /// block. Thus, these are all the flow control type of operations.
30 class TerminatorInst : public Instruction {
32 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
33 Use *Ops, unsigned NumOps,
34 Instruction *InsertBefore = 0)
35 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
37 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
38 Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
39 : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
41 /// Virtual methods - Terminators should overload these and provide inline
42 /// overrides of non-V methods.
43 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
44 virtual unsigned getNumSuccessorsV() const = 0;
45 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
47 static void destroyThis(TerminatorInst* v) {
48 Instruction::destroyThis(v);
53 virtual Instruction *clone() const = 0;
55 /// getNumSuccessors - Return the number of successors that this terminator
57 unsigned getNumSuccessors() const {
58 return getNumSuccessorsV();
61 /// getSuccessor - Return the specified successor.
63 BasicBlock *getSuccessor(unsigned idx) const {
64 return getSuccessorV(idx);
67 /// setSuccessor - Update the specified successor to point at the provided
69 void setSuccessor(unsigned idx, BasicBlock *B) {
70 setSuccessorV(idx, B);
73 // Methods for support type inquiry through isa, cast, and dyn_cast:
74 static inline bool classof(const TerminatorInst *) { return true; }
75 static inline bool classof(const Instruction *I) {
76 return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
78 static inline bool classof(const Value *V) {
79 return isa<Instruction>(V) && classof(cast<Instruction>(V));
83 //===----------------------------------------------------------------------===//
84 // UnaryInstruction Class
85 //===----------------------------------------------------------------------===//
87 class UnaryInstruction : public Instruction {
90 // avoiding warning: 'this' : used in base member initializer list
91 UnaryInstruction* this_() { return this; }
93 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB =0)
94 : Instruction(Ty, iType, &Op, 1, IB), Op(V, this_()) {
96 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
97 : Instruction(Ty, iType, &Op, 1, IAE), Op(V, this_()) {
100 static void destroyThis(UnaryInstruction* v) {
101 Instruction::destroyThis(v);
105 // Transparently provide more efficient getOperand methods.
106 Value *getOperand(unsigned i) const {
107 assert(i == 0 && "getOperand() out of range!");
110 void setOperand(unsigned i, Value *Val) {
111 assert(i == 0 && "setOperand() out of range!");
114 unsigned getNumOperands() const { return 1; }
116 // Methods for support type inquiry through isa, cast, and dyn_cast:
117 static inline bool classof(const UnaryInstruction *) { return true; }
118 static inline bool classof(const Instruction *I) {
119 return I->getOpcode() == Instruction::Malloc ||
120 I->getOpcode() == Instruction::Alloca ||
121 I->getOpcode() == Instruction::Free ||
122 I->getOpcode() == Instruction::Load ||
123 I->getOpcode() == Instruction::VAArg ||
124 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
126 static inline bool classof(const Value *V) {
127 return isa<Instruction>(V) && classof(cast<Instruction>(V));
131 //===----------------------------------------------------------------------===//
132 // BinaryOperator Class
133 //===----------------------------------------------------------------------===//
135 class BinaryOperator : public Instruction {
138 void init(BinaryOps iType);
139 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
140 const std::string &Name, Instruction *InsertBefore);
141 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
142 const std::string &Name, BasicBlock *InsertAtEnd);
144 static void destroyThis(BinaryOperator* v) {
145 Instruction::destroyThis(v);
150 /// Transparently provide more efficient getOperand methods.
151 Value *getOperand(unsigned i) const {
152 assert(i < 2 && "getOperand() out of range!");
155 void setOperand(unsigned i, Value *Val) {
156 assert(i < 2 && "setOperand() out of range!");
159 unsigned getNumOperands() const { return 2; }
161 /// create() - Construct a binary instruction, given the opcode and the two
162 /// operands. Optionally (if InstBefore is specified) insert the instruction
163 /// into a BasicBlock right before the specified instruction. The specified
164 /// Instruction is allowed to be a dereferenced end iterator.
166 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
167 const std::string &Name = "",
168 Instruction *InsertBefore = 0);
170 /// create() - Construct a binary instruction, given the opcode and the two
171 /// operands. Also automatically insert this instruction to the end of the
172 /// BasicBlock specified.
174 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
175 const std::string &Name,
176 BasicBlock *InsertAtEnd);
178 /// create* - These methods just forward to create, and are useful when you
179 /// statically know what type of instruction you're going to create. These
180 /// helpers just save some typing.
181 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
182 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
183 const std::string &Name = "") {\
184 return create(Instruction::OPC, V1, V2, Name);\
186 #include "llvm/Instruction.def"
187 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
188 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
189 const std::string &Name, BasicBlock *BB) {\
190 return create(Instruction::OPC, V1, V2, Name, BB);\
192 #include "llvm/Instruction.def"
193 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
194 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
195 const std::string &Name, Instruction *I) {\
196 return create(Instruction::OPC, V1, V2, Name, I);\
198 #include "llvm/Instruction.def"
201 /// Helper functions to construct and inspect unary operations (NEG and NOT)
202 /// via binary operators SUB and XOR:
204 /// createNeg, createNot - Create the NEG and NOT
205 /// instructions out of SUB and XOR instructions.
207 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
208 Instruction *InsertBefore = 0);
209 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
210 BasicBlock *InsertAtEnd);
211 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
212 Instruction *InsertBefore = 0);
213 static BinaryOperator *createNot(Value *Op, const std::string &Name,
214 BasicBlock *InsertAtEnd);
216 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
218 static bool isNeg(const Value *V);
219 static bool isNot(const Value *V);
221 /// getNegArgument, getNotArgument - Helper functions to extract the
222 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
224 static const Value *getNegArgument(const Value *BinOp);
225 static Value *getNegArgument( Value *BinOp);
226 static const Value *getNotArgument(const Value *BinOp);
227 static Value *getNotArgument( Value *BinOp);
229 BinaryOps getOpcode() const {
230 return static_cast<BinaryOps>(Instruction::getOpcode());
233 virtual BinaryOperator *clone() const;
235 /// swapOperands - Exchange the two operands to this instruction.
236 /// This instruction is safe to use on any binary instruction and
237 /// does not modify the semantics of the instruction. If the
238 /// instruction is order dependent (SetLT f.e.) the opcode is
239 /// changed. If the instruction cannot be reversed (ie, it's a Div),
240 /// then return true.
244 // Methods for support type inquiry through isa, cast, and dyn_cast:
245 static inline bool classof(const BinaryOperator *) { return true; }
246 static inline bool classof(const Instruction *I) {
247 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
249 static inline bool classof(const Value *V) {
250 return isa<Instruction>(V) && classof(cast<Instruction>(V));
254 //===----------------------------------------------------------------------===//
256 //===----------------------------------------------------------------------===//
258 /// CastInst - This is the base class for all instructions that perform data
259 /// casts. It is simply provided so that instruction category testing
260 /// can be performed with code like:
262 /// if (isa<CastInst>(Instr)) { ... }
263 /// @brief Base class of casting instructions.
264 class CastInst : public UnaryInstruction {
265 /// @brief Copy constructor
266 CastInst(const CastInst &CI)
267 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
269 /// @brief Do not allow default construction
272 /// @brief Constructor with insert-before-instruction semantics for subclasses
273 CastInst(const Type *Ty, unsigned iType, Value *S,
274 const std::string &Name = "", Instruction *InsertBefore = 0)
275 : UnaryInstruction(Ty, iType, S, InsertBefore) {
278 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
279 CastInst(const Type *Ty, unsigned iType, Value *S,
280 const std::string &Name, BasicBlock *InsertAtEnd)
281 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
286 static void destroyThis(CastInst* v) {
287 UnaryInstruction::destroyThis(v);
291 /// Provides a way to construct any of the CastInst subclasses using an
292 /// opcode instead of the subclass's constructor. The opcode must be in the
293 /// CastOps category (Instruction::isCast(opcode) returns true). This
294 /// constructor has insert-before-instruction semantics to automatically
295 /// insert the new CastInst before InsertBefore (if it is non-null).
296 /// @brief Construct any of the CastInst subclasses
297 static CastInst *create(
298 Instruction::CastOps, ///< The opcode of the cast instruction
299 Value *S, ///< The value to be casted (operand 0)
300 const Type *Ty, ///< The type to which cast should be made
301 const std::string &Name = "", ///< Name for the instruction
302 Instruction *InsertBefore = 0 ///< Place to insert the instruction
304 /// Provides a way to construct any of the CastInst subclasses using an
305 /// opcode instead of the subclass's constructor. The opcode must be in the
306 /// CastOps category. This constructor has insert-at-end-of-block semantics
307 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
309 /// @brief Construct any of the CastInst subclasses
310 static CastInst *create(
311 Instruction::CastOps, ///< The opcode for the cast instruction
312 Value *S, ///< The value to be casted (operand 0)
313 const Type *Ty, ///< The type to which operand is casted
314 const std::string &Name, ///< The name for the instruction
315 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
318 /// @brief Create a ZExt or BitCast cast instruction
319 static CastInst *createZExtOrBitCast(
320 Value *S, ///< The value to be casted (operand 0)
321 const Type *Ty, ///< The type to which cast should be made
322 const std::string &Name = "", ///< Name for the instruction
323 Instruction *InsertBefore = 0 ///< Place to insert the instruction
326 /// @brief Create a ZExt or BitCast cast instruction
327 static CastInst *createZExtOrBitCast(
328 Value *S, ///< The value to be casted (operand 0)
329 const Type *Ty, ///< The type to which operand is casted
330 const std::string &Name, ///< The name for the instruction
331 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
334 /// @brief Create a SExt or BitCast cast instruction
335 static CastInst *createSExtOrBitCast(
336 Value *S, ///< The value to be casted (operand 0)
337 const Type *Ty, ///< The type to which cast should be made
338 const std::string &Name = "", ///< Name for the instruction
339 Instruction *InsertBefore = 0 ///< Place to insert the instruction
342 /// @brief Create a BitCast or a PtrToInt cast instruction
343 static CastInst *createPointerCast(
344 Value *S, ///< The pointer value to be casted (operand 0)
345 const Type *Ty, ///< The type to which operand is casted
346 const std::string &Name, ///< The name for the instruction
347 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
350 /// @brief Create a BitCast or a PtrToInt cast instruction
351 static CastInst *createPointerCast(
352 Value *S, ///< The pointer value to be casted (operand 0)
353 const Type *Ty, ///< The type to which cast should be made
354 const std::string &Name = "", ///< Name for the instruction
355 Instruction *InsertBefore = 0 ///< Place to insert the instruction
358 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
359 static CastInst *createIntegerCast(
360 Value *S, ///< The pointer value to be casted (operand 0)
361 const Type *Ty, ///< The type to which cast should be made
362 bool isSigned, ///< Whether to regard S as signed or not
363 const std::string &Name = "", ///< Name for the instruction
364 Instruction *InsertBefore = 0 ///< Place to insert the instruction
367 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
368 static CastInst *createIntegerCast(
369 Value *S, ///< The integer value to be casted (operand 0)
370 const Type *Ty, ///< The integer type to which operand is casted
371 bool isSigned, ///< Whether to regard S as signed or not
372 const std::string &Name, ///< The name for the instruction
373 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
376 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
377 static CastInst *createFPCast(
378 Value *S, ///< The floating point value to be casted
379 const Type *Ty, ///< The floating point type to cast to
380 const std::string &Name = "", ///< Name for the instruction
381 Instruction *InsertBefore = 0 ///< Place to insert the instruction
384 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
385 static CastInst *createFPCast(
386 Value *S, ///< The floating point value to be casted
387 const Type *Ty, ///< The floating point type to cast to
388 const std::string &Name, ///< The name for the instruction
389 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
392 /// @brief Create a SExt or BitCast cast instruction
393 static CastInst *createSExtOrBitCast(
394 Value *S, ///< The value to be casted (operand 0)
395 const Type *Ty, ///< The type to which operand is casted
396 const std::string &Name, ///< The name for the instruction
397 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
400 /// @brief Create a Trunc or BitCast cast instruction
401 static CastInst *createTruncOrBitCast(
402 Value *S, ///< The value to be casted (operand 0)
403 const Type *Ty, ///< The type to which cast should be made
404 const std::string &Name = "", ///< Name for the instruction
405 Instruction *InsertBefore = 0 ///< Place to insert the instruction
408 /// @brief Create a Trunc or BitCast cast instruction
409 static CastInst *createTruncOrBitCast(
410 Value *S, ///< The value to be casted (operand 0)
411 const Type *Ty, ///< The type to which operand is casted
412 const std::string &Name, ///< The name for the instruction
413 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
416 /// Returns the opcode necessary to cast Val into Ty using usual casting
418 /// @brief Infer the opcode for cast operand and type
419 static Instruction::CastOps getCastOpcode(
420 const Value *Val, ///< The value to cast
421 bool SrcIsSigned, ///< Whether to treat the source as signed
422 const Type *Ty, ///< The Type to which the value should be casted
423 bool DstIsSigned ///< Whether to treate the dest. as signed
426 /// There are several places where we need to know if a cast instruction
427 /// only deals with integer source and destination types. To simplify that
428 /// logic, this method is provided.
429 /// @returns true iff the cast has only integral typed operand and dest type.
430 /// @brief Determine if this is an integer-only cast.
431 bool isIntegerCast() const;
433 /// A lossless cast is one that does not alter the basic value. It implies
434 /// a no-op cast but is more stringent, preventing things like int->float,
435 /// long->double, int->ptr, or vector->anything.
436 /// @returns true iff the cast is lossless.
437 /// @brief Determine if this is a lossless cast.
438 bool isLosslessCast() const;
440 /// A no-op cast is one that can be effected without changing any bits.
441 /// It implies that the source and destination types are the same size. The
442 /// IntPtrTy argument is used to make accurate determinations for casts
443 /// involving Integer and Pointer types. They are no-op casts if the integer
444 /// is the same size as the pointer. However, pointer size varies with
445 /// platform. Generally, the result of TargetData::getIntPtrType() should be
446 /// passed in. If that's not available, use Type::Int64Ty, which will make
447 /// the isNoopCast call conservative.
448 /// @brief Determine if this cast is a no-op cast.
450 const Type *IntPtrTy ///< Integer type corresponding to pointer
453 /// Determine how a pair of casts can be eliminated, if they can be at all.
454 /// This is a helper function for both CastInst and ConstantExpr.
455 /// @returns 0 if the CastInst pair can't be eliminated
456 /// @returns Instruction::CastOps value for a cast that can replace
457 /// the pair, casting SrcTy to DstTy.
458 /// @brief Determine if a cast pair is eliminable
459 static unsigned isEliminableCastPair(
460 Instruction::CastOps firstOpcode, ///< Opcode of first cast
461 Instruction::CastOps secondOpcode, ///< Opcode of second cast
462 const Type *SrcTy, ///< SrcTy of 1st cast
463 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
464 const Type *DstTy, ///< DstTy of 2nd cast
465 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
468 /// @brief Return the opcode of this CastInst
469 Instruction::CastOps getOpcode() const {
470 return Instruction::CastOps(Instruction::getOpcode());
473 /// @brief Return the source type, as a convenience
474 const Type* getSrcTy() const { return getOperand(0)->getType(); }
475 /// @brief Return the destination type, as a convenience
476 const Type* getDestTy() const { return getType(); }
478 /// This method can be used to determine if a cast from S to DstTy using
479 /// Opcode op is valid or not.
480 /// @returns true iff the proposed cast is valid.
481 /// @brief Determine if a cast is valid without creating one.
482 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
484 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
485 static inline bool classof(const CastInst *) { return true; }
486 static inline bool classof(const Instruction *I) {
487 return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
489 static inline bool classof(const Value *V) {
490 return isa<Instruction>(V) && classof(cast<Instruction>(V));
494 //===----------------------------------------------------------------------===//
496 //===----------------------------------------------------------------------===//
498 /// This class is the base class for the comparison instructions.
499 /// @brief Abstract base class of comparison instructions.
500 class CmpInst: public Instruction {
501 CmpInst(); // do not implement
503 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
504 const std::string &Name = "", Instruction *InsertBefore = 0);
506 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
507 const std::string &Name, BasicBlock *InsertAtEnd);
509 Use Ops[2]; // CmpInst instructions always have 2 operands, optimize
511 static void destroyThis(CmpInst* v) {
512 Instruction::destroyThis(v);
516 /// Construct a compare instruction, given the opcode, the predicate and
517 /// the two operands. Optionally (if InstBefore is specified) insert the
518 /// instruction into a BasicBlock right before the specified instruction.
519 /// The specified Instruction is allowed to be a dereferenced end iterator.
520 /// @brief Create a CmpInst
521 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
522 Value *S2, const std::string &Name = "",
523 Instruction *InsertBefore = 0);
525 /// Construct a compare instruction, given the opcode, the predicate and the
526 /// two operands. Also automatically insert this instruction to the end of
527 /// the BasicBlock specified.
528 /// @brief Create a CmpInst
529 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
530 Value *S2, const std::string &Name,
531 BasicBlock *InsertAtEnd);
533 /// @brief Get the opcode casted to the right type
534 OtherOps getOpcode() const {
535 return static_cast<OtherOps>(Instruction::getOpcode());
538 /// The predicate for CmpInst is defined by the subclasses but stored in
539 /// the SubclassData field (see Value.h). We allow it to be fetched here
540 /// as the predicate but there is no enum type for it, just the raw unsigned
541 /// short. This facilitates comparison of CmpInst instances without delving
542 /// into the subclasses since predicate values are distinct between the
543 /// CmpInst subclasses.
544 /// @brief Return the predicate for this instruction.
545 unsigned short getPredicate() const {
549 /// @brief Provide more efficient getOperand methods.
550 Value *getOperand(unsigned i) const {
551 assert(i < 2 && "getOperand() out of range!");
554 void setOperand(unsigned i, Value *Val) {
555 assert(i < 2 && "setOperand() out of range!");
559 /// @brief CmpInst instructions always have 2 operands.
560 unsigned getNumOperands() const { return 2; }
562 /// This is just a convenience that dispatches to the subclasses.
563 /// @brief Swap the operands and adjust predicate accordingly to retain
564 /// the same comparison.
567 /// This is just a convenience that dispatches to the subclasses.
568 /// @brief Determine if this CmpInst is commutative.
569 bool isCommutative();
571 /// This is just a convenience that dispatches to the subclasses.
572 /// @brief Determine if this is an equals/not equals predicate.
575 /// @returns true if the predicate is unsigned, false otherwise.
576 /// @brief Determine if the predicate is an unsigned operation.
577 static bool isUnsigned(unsigned short predicate);
579 /// @returns true if the predicate is signed, false otherwise.
580 /// @brief Determine if the predicate is an signed operation.
581 static bool isSigned(unsigned short predicate);
583 /// @brief Determine if the predicate is an ordered operation.
584 static bool isOrdered(unsigned short predicate);
586 /// @brief Determine if the predicate is an unordered operation.
587 static bool isUnordered(unsigned short predicate);
589 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
590 static inline bool classof(const CmpInst *) { return true; }
591 static inline bool classof(const Instruction *I) {
592 return I->getOpcode() == Instruction::ICmp ||
593 I->getOpcode() == Instruction::FCmp;
595 static inline bool classof(const Value *V) {
596 return isa<Instruction>(V) && classof(cast<Instruction>(V));
600 } // End llvm namespace