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 // Out of line virtual method, so the vtable, etc has a home.
44 /// Virtual methods - Terminators should overload these and provide inline
45 /// overrides of non-V methods.
46 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
47 virtual unsigned getNumSuccessorsV() const = 0;
48 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
51 virtual Instruction *clone() const = 0;
53 /// getNumSuccessors - Return the number of successors that this terminator
55 unsigned getNumSuccessors() const {
56 return getNumSuccessorsV();
59 /// getSuccessor - Return the specified successor.
61 BasicBlock *getSuccessor(unsigned idx) const {
62 return getSuccessorV(idx);
65 /// setSuccessor - Update the specified successor to point at the provided
67 void setSuccessor(unsigned idx, BasicBlock *B) {
68 setSuccessorV(idx, B);
71 // Methods for support type inquiry through isa, cast, and dyn_cast:
72 static inline bool classof(const TerminatorInst *) { return true; }
73 static inline bool classof(const Instruction *I) {
74 return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
76 static inline bool classof(const Value *V) {
77 return isa<Instruction>(V) && classof(cast<Instruction>(V));
81 //===----------------------------------------------------------------------===//
82 // UnaryInstruction Class
83 //===----------------------------------------------------------------------===//
85 class UnaryInstruction : public Instruction {
88 // avoiding warning: 'this' : used in base member initializer list
89 UnaryInstruction* this_() { return this; }
91 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB =0)
92 : Instruction(Ty, iType, &Op, 1, IB), Op(V, this_()) {
94 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
95 : Instruction(Ty, iType, &Op, 1, IAE), Op(V, this_()) {
98 // Out of line virtual method, so the vtable, etc has a home.
101 // Transparently provide more efficient getOperand methods.
102 Value *getOperand(unsigned i) const {
103 assert(i == 0 && "getOperand() out of range!");
106 void setOperand(unsigned i, Value *Val) {
107 assert(i == 0 && "setOperand() out of range!");
110 unsigned getNumOperands() const { return 1; }
113 //===----------------------------------------------------------------------===//
114 // BinaryOperator Class
115 //===----------------------------------------------------------------------===//
117 class BinaryOperator : public Instruction {
120 void init(BinaryOps iType);
121 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
122 const std::string &Name, Instruction *InsertBefore);
123 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
124 const std::string &Name, BasicBlock *InsertAtEnd);
127 /// Transparently provide more efficient getOperand methods.
128 Value *getOperand(unsigned i) const {
129 assert(i < 2 && "getOperand() out of range!");
132 void setOperand(unsigned i, Value *Val) {
133 assert(i < 2 && "setOperand() out of range!");
136 unsigned getNumOperands() const { return 2; }
138 /// create() - Construct a binary instruction, given the opcode and the two
139 /// operands. Optionally (if InstBefore is specified) insert the instruction
140 /// into a BasicBlock right before the specified instruction. The specified
141 /// Instruction is allowed to be a dereferenced end iterator.
143 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
144 const std::string &Name = "",
145 Instruction *InsertBefore = 0);
147 /// create() - Construct a binary instruction, given the opcode and the two
148 /// operands. Also automatically insert this instruction to the end of the
149 /// BasicBlock specified.
151 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
152 const std::string &Name,
153 BasicBlock *InsertAtEnd);
155 /// create* - These methods just forward to create, and are useful when you
156 /// statically know what type of instruction you're going to create. These
157 /// helpers just save some typing.
158 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
159 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
160 const std::string &Name = "") {\
161 return create(Instruction::OPC, V1, V2, Name);\
163 #include "llvm/Instruction.def"
164 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
165 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
166 const std::string &Name, BasicBlock *BB) {\
167 return create(Instruction::OPC, V1, V2, Name, BB);\
169 #include "llvm/Instruction.def"
170 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
171 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
172 const std::string &Name, Instruction *I) {\
173 return create(Instruction::OPC, V1, V2, Name, I);\
175 #include "llvm/Instruction.def"
178 /// Helper functions to construct and inspect unary operations (NEG and NOT)
179 /// via binary operators SUB and XOR:
181 /// createNeg, createNot - Create the NEG and NOT
182 /// instructions out of SUB and XOR instructions.
184 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
185 Instruction *InsertBefore = 0);
186 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
187 BasicBlock *InsertAtEnd);
188 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
189 Instruction *InsertBefore = 0);
190 static BinaryOperator *createNot(Value *Op, const std::string &Name,
191 BasicBlock *InsertAtEnd);
193 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
195 static bool isNeg(const Value *V);
196 static bool isNot(const Value *V);
198 /// getNegArgument, getNotArgument - Helper functions to extract the
199 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
201 static const Value *getNegArgument(const Value *BinOp);
202 static Value *getNegArgument( Value *BinOp);
203 static const Value *getNotArgument(const Value *BinOp);
204 static Value *getNotArgument( Value *BinOp);
206 BinaryOps getOpcode() const {
207 return static_cast<BinaryOps>(Instruction::getOpcode());
210 virtual BinaryOperator *clone() const;
212 /// swapOperands - Exchange the two operands to this instruction.
213 /// This instruction is safe to use on any binary instruction and
214 /// does not modify the semantics of the instruction. If the
215 /// instruction is order dependent (SetLT f.e.) the opcode is
216 /// changed. If the instruction cannot be reversed (ie, it's a Div),
217 /// then return true.
221 // Methods for support type inquiry through isa, cast, and dyn_cast:
222 static inline bool classof(const BinaryOperator *) { return true; }
223 static inline bool classof(const Instruction *I) {
224 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
226 static inline bool classof(const Value *V) {
227 return isa<Instruction>(V) && classof(cast<Instruction>(V));
231 //===----------------------------------------------------------------------===//
233 //===----------------------------------------------------------------------===//
235 /// CastInst - This is the base class for all instructions that perform data
236 /// casts. It is simply provided so that instruction category testing
237 /// can be performed with code like:
239 /// if (isa<CastInst>(Instr)) { ... }
240 /// @brief Base class of casting instructions.
241 class CastInst : public UnaryInstruction {
242 /// @brief Copy constructor
243 CastInst(const CastInst &CI)
244 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
246 /// @brief Do not allow default construction
249 /// @brief Constructor with insert-before-instruction semantics for subclasses
250 CastInst(const Type *Ty, unsigned iType, Value *S,
251 const std::string &Name = "", Instruction *InsertBefore = 0)
252 : UnaryInstruction(Ty, iType, S, InsertBefore) {
255 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
256 CastInst(const Type *Ty, unsigned iType, Value *S,
257 const std::string &Name, BasicBlock *InsertAtEnd)
258 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
262 /// Provides a way to construct any of the CastInst subclasses using an
263 /// opcode instead of the subclass's constructor. The opcode must be in the
264 /// CastOps category (Instruction::isCast(opcode) returns true). This
265 /// constructor has insert-before-instruction semantics to automatically
266 /// insert the new CastInst before InsertBefore (if it is non-null).
267 /// @brief Construct any of the CastInst subclasses
268 static CastInst *create(
269 Instruction::CastOps, ///< The opcode of the cast instruction
270 Value *S, ///< The value to be casted (operand 0)
271 const Type *Ty, ///< The type to which cast should be made
272 const std::string &Name = "", ///< Name for the instruction
273 Instruction *InsertBefore = 0 ///< Place to insert the instruction
275 /// Provides a way to construct any of the CastInst subclasses using an
276 /// opcode instead of the subclass's constructor. The opcode must be in the
277 /// CastOps category. This constructor has insert-at-end-of-block semantics
278 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
280 /// @brief Construct any of the CastInst subclasses
281 static CastInst *create(
282 Instruction::CastOps, ///< The opcode for the cast instruction
283 Value *S, ///< The value to be casted (operand 0)
284 const Type *Ty, ///< The type to which operand is casted
285 const std::string &Name, ///< The name for the instruction
286 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
289 /// @brief Create a ZExt or BitCast cast instruction
290 static CastInst *createZExtOrBitCast(
291 Value *S, ///< The value to be casted (operand 0)
292 const Type *Ty, ///< The type to which cast should be made
293 const std::string &Name = "", ///< Name for the instruction
294 Instruction *InsertBefore = 0 ///< Place to insert the instruction
297 /// @brief Create a ZExt or BitCast cast instruction
298 static CastInst *createZExtOrBitCast(
299 Value *S, ///< The value to be casted (operand 0)
300 const Type *Ty, ///< The type to which operand is casted
301 const std::string &Name, ///< The name for the instruction
302 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
305 /// @brief Create a SExt or BitCast cast instruction
306 static CastInst *createSExtOrBitCast(
307 Value *S, ///< The value to be casted (operand 0)
308 const Type *Ty, ///< The type to which cast should be made
309 const std::string &Name = "", ///< Name for the instruction
310 Instruction *InsertBefore = 0 ///< Place to insert the instruction
313 /// @brief Create a BitCast or a PtrToInt cast instruction
314 static CastInst *createPointerCast(
315 Value *S, ///< The pointer value to be casted (operand 0)
316 const Type *Ty, ///< The type to which operand is casted
317 const std::string &Name, ///< The name for the instruction
318 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
321 /// @brief Create a BitCast or a PtrToInt cast instruction
322 static CastInst *createPointerCast(
323 Value *S, ///< The pointer value to be casted (operand 0)
324 const Type *Ty, ///< The type to which cast should be made
325 const std::string &Name = "", ///< Name for the instruction
326 Instruction *InsertBefore = 0 ///< Place to insert the instruction
329 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
330 static CastInst *createIntegerCast(
331 Value *S, ///< The pointer value to be casted (operand 0)
332 const Type *Ty, ///< The type to which cast should be made
333 bool isSigned, ///< Whether to regard S as signed or not
334 const std::string &Name = "", ///< Name for the instruction
335 Instruction *InsertBefore = 0 ///< Place to insert the instruction
338 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
339 static CastInst *createIntegerCast(
340 Value *S, ///< The integer value to be casted (operand 0)
341 const Type *Ty, ///< The integer type to which operand is casted
342 bool isSigned, ///< Whether to regard S as signed or not
343 const std::string &Name, ///< The name for the instruction
344 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
347 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
348 static CastInst *createFPCast(
349 Value *S, ///< The floating point value to be casted
350 const Type *Ty, ///< The floating point type to cast to
351 const std::string &Name = "", ///< Name for the instruction
352 Instruction *InsertBefore = 0 ///< Place to insert the instruction
355 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
356 static CastInst *createFPCast(
357 Value *S, ///< The floating point value to be casted
358 const Type *Ty, ///< The floating point type to cast to
359 const std::string &Name, ///< The name for the instruction
360 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
363 /// @brief Create a SExt or BitCast cast instruction
364 static CastInst *createSExtOrBitCast(
365 Value *S, ///< The value to be casted (operand 0)
366 const Type *Ty, ///< The type to which operand is casted
367 const std::string &Name, ///< The name for the instruction
368 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
371 /// @brief Create a Trunc or BitCast cast instruction
372 static CastInst *createTruncOrBitCast(
373 Value *S, ///< The value to be casted (operand 0)
374 const Type *Ty, ///< The type to which cast should be made
375 const std::string &Name = "", ///< Name for the instruction
376 Instruction *InsertBefore = 0 ///< Place to insert the instruction
379 /// @brief Create a Trunc or BitCast cast instruction
380 static CastInst *createTruncOrBitCast(
381 Value *S, ///< The value to be casted (operand 0)
382 const Type *Ty, ///< The type to which operand is casted
383 const std::string &Name, ///< The name for the instruction
384 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
387 /// Returns the opcode necessary to cast Val into Ty using usual casting
389 /// @brief Infer the opcode for cast operand and type
390 static Instruction::CastOps getCastOpcode(
391 const Value *Val, ///< The value to cast
392 bool SrcIsSigned, ///< Whether to treat the source as signed
393 const Type *Ty, ///< The Type to which the value should be casted
394 bool DstIsSigned ///< Whether to treate the dest. as signed
397 /// There are several places where we need to know if a cast instruction
398 /// only deals with integer source and destination types. To simplify that
399 /// logic, this method is provided.
400 /// @returns true iff the cast has only integral typed operand and dest type.
401 /// @brief Determine if this is an integer-only cast.
402 bool isIntegerCast() const;
404 /// A lossless cast is one that does not alter the basic value. It implies
405 /// a no-op cast but is more stringent, preventing things like int->float,
406 /// long->double, int->ptr, or vector->anything.
407 /// @returns true iff the cast is lossless.
408 /// @brief Determine if this is a lossless cast.
409 bool isLosslessCast() const;
411 /// A no-op cast is one that can be effected without changing any bits.
412 /// It implies that the source and destination types are the same size. The
413 /// IntPtrTy argument is used to make accurate determinations for casts
414 /// involving Integer and Pointer types. They are no-op casts if the integer
415 /// is the same size as the pointer. However, pointer size varies with
416 /// platform. Generally, the result of TargetData::getIntPtrType() should be
417 /// passed in. If that's not available, use Type::Int64Ty, which will make
418 /// the isNoopCast call conservative.
419 /// @brief Determine if this cast is a no-op cast.
421 const Type *IntPtrTy ///< Integer type corresponding to pointer
424 /// Determine how a pair of casts can be eliminated, if they can be at all.
425 /// This is a helper function for both CastInst and ConstantExpr.
426 /// @returns 0 if the CastInst pair can't be eliminated
427 /// @returns Instruction::CastOps value for a cast that can replace
428 /// the pair, casting SrcTy to DstTy.
429 /// @brief Determine if a cast pair is eliminable
430 static unsigned isEliminableCastPair(
431 Instruction::CastOps firstOpcode, ///< Opcode of first cast
432 Instruction::CastOps secondOpcode, ///< Opcode of second cast
433 const Type *SrcTy, ///< SrcTy of 1st cast
434 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
435 const Type *DstTy, ///< DstTy of 2nd cast
436 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
439 /// @brief Return the opcode of this CastInst
440 Instruction::CastOps getOpcode() const {
441 return Instruction::CastOps(Instruction::getOpcode());
444 /// @brief Return the source type, as a convenience
445 const Type* getSrcTy() const { return getOperand(0)->getType(); }
446 /// @brief Return the destination type, as a convenience
447 const Type* getDestTy() const { return getType(); }
449 /// This method can be used to determine if a cast from S to DstTy using
450 /// Opcode op is valid or not.
451 /// @returns true iff the proposed cast is valid.
452 /// @brief Determine if a cast is valid without creating one.
453 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
455 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
456 static inline bool classof(const CastInst *) { return true; }
457 static inline bool classof(const Instruction *I) {
458 return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
460 static inline bool classof(const Value *V) {
461 return isa<Instruction>(V) && classof(cast<Instruction>(V));
465 //===----------------------------------------------------------------------===//
467 //===----------------------------------------------------------------------===//
469 /// This class is the base class for the comparison instructions.
470 /// @brief Abstract base class of comparison instructions.
471 class CmpInst: public Instruction {
472 CmpInst(); // do not implement
474 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
475 const std::string &Name = "", Instruction *InsertBefore = 0);
477 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
478 const std::string &Name, BasicBlock *InsertAtEnd);
480 Use Ops[2]; // CmpInst instructions always have 2 operands, optimize
483 /// Construct a compare instruction, given the opcode, the predicate and
484 /// the two operands. Optionally (if InstBefore is specified) insert the
485 /// instruction into a BasicBlock right before the specified instruction.
486 /// The specified Instruction is allowed to be a dereferenced end iterator.
487 /// @brief Create a CmpInst
488 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
489 Value *S2, const std::string &Name = "",
490 Instruction *InsertBefore = 0);
492 /// Construct a compare instruction, given the opcode, the predicate and the
493 /// two operands. Also automatically insert this instruction to the end of
494 /// the BasicBlock specified.
495 /// @brief Create a CmpInst
496 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
497 Value *S2, const std::string &Name,
498 BasicBlock *InsertAtEnd);
500 /// @brief Get the opcode casted to the right type
501 OtherOps getOpcode() const {
502 return static_cast<OtherOps>(Instruction::getOpcode());
505 /// The predicate for CmpInst is defined by the subclasses but stored in
506 /// the SubclassData field (see Value.h). We allow it to be fetched here
507 /// as the predicate but there is no enum type for it, just the raw unsigned
508 /// short. This facilitates comparison of CmpInst instances without delving
509 /// into the subclasses since predicate values are distinct between the
510 /// CmpInst subclasses.
511 /// @brief Return the predicate for this instruction.
512 unsigned short getPredicate() const {
516 /// @brief Provide more efficient getOperand methods.
517 Value *getOperand(unsigned i) const {
518 assert(i < 2 && "getOperand() out of range!");
521 void setOperand(unsigned i, Value *Val) {
522 assert(i < 2 && "setOperand() out of range!");
526 /// @brief CmpInst instructions always have 2 operands.
527 unsigned getNumOperands() const { return 2; }
529 /// This is just a convenience that dispatches to the subclasses.
530 /// @brief Swap the operands and adjust predicate accordingly to retain
531 /// the same comparison.
534 /// This is just a convenience that dispatches to the subclasses.
535 /// @brief Determine if this CmpInst is commutative.
536 bool isCommutative();
538 /// This is just a convenience that dispatches to the subclasses.
539 /// @brief Determine if this is an equals/not equals predicate.
542 /// @returns true if the predicate is unsigned, false otherwise.
543 /// @brief Determine if the predicate is an unsigned operation.
544 static bool isUnsigned(unsigned short predicate);
546 /// @returns true if the predicate is signed, false otherwise.
547 /// @brief Determine if the predicate is an signed operation.
548 static bool isSigned(unsigned short predicate);
550 /// @brief Determine if the predicate is an ordered operation.
551 static bool isOrdered(unsigned short predicate);
553 /// @brief Determine if the predicate is an unordered operation.
554 static bool isUnordered(unsigned short predicate);
556 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
557 static inline bool classof(const CmpInst *) { return true; }
558 static inline bool classof(const Instruction *I) {
559 return I->getOpcode() == Instruction::ICmp ||
560 I->getOpcode() == Instruction::FCmp;
562 static inline bool classof(const Value *V) {
563 return isa<Instruction>(V) && classof(cast<Instruction>(V));
567 } // End llvm namespace