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"
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 {
86 void *operator new(size_t, unsigned); // Do not implement
89 // avoiding warning: 'this' : used in base member initializer list
90 UnaryInstruction* this_() { return this; }
92 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB =0)
93 : Instruction(Ty, iType, &Op, 1, IB), Op(V, this_()) {
95 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
96 : Instruction(Ty, iType, &Op, 1, IAE), Op(V, this_()) {
99 // allocate space for exactly one operand
100 void *operator new(size_t s) {
101 return User::operator new(s, 1);
104 // Out of line virtual method, so the vtable, etc has a home.
107 // Transparently provide more efficient getOperand methods.
108 Value *getOperand(unsigned i) const {
109 assert(i == 0 && "getOperand() out of range!");
112 void setOperand(unsigned i, Value *Val) {
113 assert(i == 0 && "setOperand() out of range!");
116 unsigned getNumOperands() const { return 1; }
118 // Methods for support type inquiry through isa, cast, and dyn_cast:
119 static inline bool classof(const UnaryInstruction *) { return true; }
120 static inline bool classof(const Instruction *I) {
121 return I->getOpcode() == Instruction::Malloc ||
122 I->getOpcode() == Instruction::Alloca ||
123 I->getOpcode() == Instruction::Free ||
124 I->getOpcode() == Instruction::Load ||
125 I->getOpcode() == Instruction::VAArg ||
126 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
128 static inline bool classof(const Value *V) {
129 return isa<Instruction>(V) && classof(cast<Instruction>(V));
133 //===----------------------------------------------------------------------===//
134 // BinaryOperator Class
135 //===----------------------------------------------------------------------===//
137 class BinaryOperator : public Instruction {
138 void *operator new(size_t, unsigned); // Do not implement
141 void init(BinaryOps iType);
142 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
143 const std::string &Name, Instruction *InsertBefore);
144 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
145 const std::string &Name, BasicBlock *InsertAtEnd);
147 // allocate space for exactly two operands
148 void *operator new(size_t s) {
149 return User::operator new(s, 2);
152 /// Transparently provide more efficient getOperand methods.
153 Value *getOperand(unsigned i) const {
154 assert(i < 2 && "getOperand() out of range!");
157 void setOperand(unsigned i, Value *Val) {
158 assert(i < 2 && "setOperand() out of range!");
161 unsigned getNumOperands() const { return 2; }
163 /// create() - Construct a binary instruction, given the opcode and the two
164 /// operands. Optionally (if InstBefore is specified) insert the instruction
165 /// into a BasicBlock right before the specified instruction. The specified
166 /// Instruction is allowed to be a dereferenced end iterator.
168 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
169 const std::string &Name = "",
170 Instruction *InsertBefore = 0);
172 /// create() - Construct a binary instruction, given the opcode and the two
173 /// operands. Also automatically insert this instruction to the end of the
174 /// BasicBlock specified.
176 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
177 const std::string &Name,
178 BasicBlock *InsertAtEnd);
180 /// create* - These methods just forward to create, and are useful when you
181 /// statically know what type of instruction you're going to create. These
182 /// helpers just save some typing.
183 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
184 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
185 const std::string &Name = "") {\
186 return create(Instruction::OPC, V1, V2, Name);\
188 #include "llvm/Instruction.def"
189 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
190 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
191 const std::string &Name, BasicBlock *BB) {\
192 return create(Instruction::OPC, V1, V2, Name, BB);\
194 #include "llvm/Instruction.def"
195 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
196 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
197 const std::string &Name, Instruction *I) {\
198 return create(Instruction::OPC, V1, V2, Name, I);\
200 #include "llvm/Instruction.def"
203 /// Helper functions to construct and inspect unary operations (NEG and NOT)
204 /// via binary operators SUB and XOR:
206 /// createNeg, createNot - Create the NEG and NOT
207 /// instructions out of SUB and XOR instructions.
209 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
210 Instruction *InsertBefore = 0);
211 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
212 BasicBlock *InsertAtEnd);
213 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
214 Instruction *InsertBefore = 0);
215 static BinaryOperator *createNot(Value *Op, const std::string &Name,
216 BasicBlock *InsertAtEnd);
218 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
220 static bool isNeg(const Value *V);
221 static bool isNot(const Value *V);
223 /// getNegArgument, getNotArgument - Helper functions to extract the
224 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
226 static const Value *getNegArgument(const Value *BinOp);
227 static Value *getNegArgument( Value *BinOp);
228 static const Value *getNotArgument(const Value *BinOp);
229 static Value *getNotArgument( Value *BinOp);
231 BinaryOps getOpcode() const {
232 return static_cast<BinaryOps>(Instruction::getOpcode());
235 virtual BinaryOperator *clone() const;
237 /// swapOperands - Exchange the two operands to this instruction.
238 /// This instruction is safe to use on any binary instruction and
239 /// does not modify the semantics of the instruction. If the
240 /// instruction is order dependent (SetLT f.e.) the opcode is
241 /// changed. If the instruction cannot be reversed (ie, it's a Div),
242 /// then return true.
246 // Methods for support type inquiry through isa, cast, and dyn_cast:
247 static inline bool classof(const BinaryOperator *) { return true; }
248 static inline bool classof(const Instruction *I) {
249 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
251 static inline bool classof(const Value *V) {
252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
256 //===----------------------------------------------------------------------===//
258 //===----------------------------------------------------------------------===//
260 /// CastInst - This is the base class for all instructions that perform data
261 /// casts. It is simply provided so that instruction category testing
262 /// can be performed with code like:
264 /// if (isa<CastInst>(Instr)) { ... }
265 /// @brief Base class of casting instructions.
266 class CastInst : public UnaryInstruction {
267 /// @brief Copy constructor
268 CastInst(const CastInst &CI)
269 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
271 /// @brief Do not allow default construction
274 /// @brief Constructor with insert-before-instruction semantics for subclasses
275 CastInst(const Type *Ty, unsigned iType, Value *S,
276 const std::string &Name = "", Instruction *InsertBefore = 0)
277 : UnaryInstruction(Ty, iType, S, InsertBefore) {
280 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
281 CastInst(const Type *Ty, unsigned iType, Value *S,
282 const std::string &Name, BasicBlock *InsertAtEnd)
283 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
287 /// Provides a way to construct any of the CastInst subclasses using an
288 /// opcode instead of the subclass's constructor. The opcode must be in the
289 /// CastOps category (Instruction::isCast(opcode) returns true). This
290 /// constructor has insert-before-instruction semantics to automatically
291 /// insert the new CastInst before InsertBefore (if it is non-null).
292 /// @brief Construct any of the CastInst subclasses
293 static CastInst *create(
294 Instruction::CastOps, ///< The opcode of the cast instruction
295 Value *S, ///< The value to be casted (operand 0)
296 const Type *Ty, ///< The type to which cast should be made
297 const std::string &Name = "", ///< Name for the instruction
298 Instruction *InsertBefore = 0 ///< Place to insert the instruction
300 /// Provides a way to construct any of the CastInst subclasses using an
301 /// opcode instead of the subclass's constructor. The opcode must be in the
302 /// CastOps category. This constructor has insert-at-end-of-block semantics
303 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
305 /// @brief Construct any of the CastInst subclasses
306 static CastInst *create(
307 Instruction::CastOps, ///< The opcode for the cast instruction
308 Value *S, ///< The value to be casted (operand 0)
309 const Type *Ty, ///< The type to which operand is casted
310 const std::string &Name, ///< The name for the instruction
311 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
314 /// @brief Create a ZExt or BitCast cast instruction
315 static CastInst *createZExtOrBitCast(
316 Value *S, ///< The value to be casted (operand 0)
317 const Type *Ty, ///< The type to which cast should be made
318 const std::string &Name = "", ///< Name for the instruction
319 Instruction *InsertBefore = 0 ///< Place to insert the instruction
322 /// @brief Create a ZExt or BitCast cast instruction
323 static CastInst *createZExtOrBitCast(
324 Value *S, ///< The value to be casted (operand 0)
325 const Type *Ty, ///< The type to which operand is casted
326 const std::string &Name, ///< The name for the instruction
327 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
330 /// @brief Create a SExt or BitCast cast instruction
331 static CastInst *createSExtOrBitCast(
332 Value *S, ///< The value to be casted (operand 0)
333 const Type *Ty, ///< The type to which cast should be made
334 const std::string &Name = "", ///< Name for the instruction
335 Instruction *InsertBefore = 0 ///< Place to insert the instruction
338 /// @brief Create a BitCast or a PtrToInt cast instruction
339 static CastInst *createPointerCast(
340 Value *S, ///< The pointer value to be casted (operand 0)
341 const Type *Ty, ///< The type to which operand is casted
342 const std::string &Name, ///< The name for the instruction
343 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
346 /// @brief Create a BitCast or a PtrToInt cast instruction
347 static CastInst *createPointerCast(
348 Value *S, ///< The pointer value to be casted (operand 0)
349 const Type *Ty, ///< The type to which cast should be made
350 const std::string &Name = "", ///< Name for the instruction
351 Instruction *InsertBefore = 0 ///< Place to insert the instruction
354 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
355 static CastInst *createIntegerCast(
356 Value *S, ///< The pointer value to be casted (operand 0)
357 const Type *Ty, ///< The type to which cast should be made
358 bool isSigned, ///< Whether to regard S as signed or not
359 const std::string &Name = "", ///< Name for the instruction
360 Instruction *InsertBefore = 0 ///< Place to insert the instruction
363 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
364 static CastInst *createIntegerCast(
365 Value *S, ///< The integer value to be casted (operand 0)
366 const Type *Ty, ///< The integer type to which operand is casted
367 bool isSigned, ///< Whether to regard S as signed or not
368 const std::string &Name, ///< The name for the instruction
369 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
372 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
373 static CastInst *createFPCast(
374 Value *S, ///< The floating point value to be casted
375 const Type *Ty, ///< The floating point type to cast to
376 const std::string &Name = "", ///< Name for the instruction
377 Instruction *InsertBefore = 0 ///< Place to insert the instruction
380 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
381 static CastInst *createFPCast(
382 Value *S, ///< The floating point value to be casted
383 const Type *Ty, ///< The floating point type to cast to
384 const std::string &Name, ///< The name for the instruction
385 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
388 /// @brief Create a SExt or BitCast cast instruction
389 static CastInst *createSExtOrBitCast(
390 Value *S, ///< The value to be casted (operand 0)
391 const Type *Ty, ///< The type to which operand is casted
392 const std::string &Name, ///< The name for the instruction
393 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
396 /// @brief Create a Trunc or BitCast cast instruction
397 static CastInst *createTruncOrBitCast(
398 Value *S, ///< The value to be casted (operand 0)
399 const Type *Ty, ///< The type to which cast should be made
400 const std::string &Name = "", ///< Name for the instruction
401 Instruction *InsertBefore = 0 ///< Place to insert the instruction
404 /// @brief Create a Trunc or BitCast cast instruction
405 static CastInst *createTruncOrBitCast(
406 Value *S, ///< The value to be casted (operand 0)
407 const Type *Ty, ///< The type to which operand is casted
408 const std::string &Name, ///< The name for the instruction
409 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
412 /// @brief Check whether it is valid to call getCastOpcode for these types.
413 static bool isCastable(
414 const Type *SrcTy, ///< The Type from which the value should be cast.
415 const Type *DestTy ///< The Type to which the value should be cast.
418 /// Returns the opcode necessary to cast Val into Ty using usual casting
420 /// @brief Infer the opcode for cast operand and type
421 static Instruction::CastOps getCastOpcode(
422 const Value *Val, ///< The value to cast
423 bool SrcIsSigned, ///< Whether to treat the source as signed
424 const Type *Ty, ///< The Type to which the value should be casted
425 bool DstIsSigned ///< Whether to treate the dest. as signed
428 /// There are several places where we need to know if a cast instruction
429 /// only deals with integer source and destination types. To simplify that
430 /// logic, this method is provided.
431 /// @returns true iff the cast has only integral typed operand and dest type.
432 /// @brief Determine if this is an integer-only cast.
433 bool isIntegerCast() const;
435 /// A lossless cast is one that does not alter the basic value. It implies
436 /// a no-op cast but is more stringent, preventing things like int->float,
437 /// long->double, int->ptr, or vector->anything.
438 /// @returns true iff the cast is lossless.
439 /// @brief Determine if this is a lossless cast.
440 bool isLosslessCast() const;
442 /// A no-op cast is one that can be effected without changing any bits.
443 /// It implies that the source and destination types are the same size. The
444 /// IntPtrTy argument is used to make accurate determinations for casts
445 /// involving Integer and Pointer types. They are no-op casts if the integer
446 /// is the same size as the pointer. However, pointer size varies with
447 /// platform. Generally, the result of TargetData::getIntPtrType() should be
448 /// passed in. If that's not available, use Type::Int64Ty, which will make
449 /// the isNoopCast call conservative.
450 /// @brief Determine if this cast is a no-op cast.
452 const Type *IntPtrTy ///< Integer type corresponding to pointer
455 /// Determine how a pair of casts can be eliminated, if they can be at all.
456 /// This is a helper function for both CastInst and ConstantExpr.
457 /// @returns 0 if the CastInst pair can't be eliminated
458 /// @returns Instruction::CastOps value for a cast that can replace
459 /// the pair, casting SrcTy to DstTy.
460 /// @brief Determine if a cast pair is eliminable
461 static unsigned isEliminableCastPair(
462 Instruction::CastOps firstOpcode, ///< Opcode of first cast
463 Instruction::CastOps secondOpcode, ///< Opcode of second cast
464 const Type *SrcTy, ///< SrcTy of 1st cast
465 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
466 const Type *DstTy, ///< DstTy of 2nd cast
467 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
470 /// @brief Return the opcode of this CastInst
471 Instruction::CastOps getOpcode() const {
472 return Instruction::CastOps(Instruction::getOpcode());
475 /// @brief Return the source type, as a convenience
476 const Type* getSrcTy() const { return getOperand(0)->getType(); }
477 /// @brief Return the destination type, as a convenience
478 const Type* getDestTy() const { return getType(); }
480 /// This method can be used to determine if a cast from S to DstTy using
481 /// Opcode op is valid or not.
482 /// @returns true iff the proposed cast is valid.
483 /// @brief Determine if a cast is valid without creating one.
484 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
486 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
487 static inline bool classof(const CastInst *) { return true; }
488 static inline bool classof(const Instruction *I) {
489 return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
491 static inline bool classof(const Value *V) {
492 return isa<Instruction>(V) && classof(cast<Instruction>(V));
496 //===----------------------------------------------------------------------===//
498 //===----------------------------------------------------------------------===//
500 /// This class is the base class for the comparison instructions.
501 /// @brief Abstract base class of comparison instructions.
502 class CmpInst: public Instruction {
503 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
504 CmpInst(); // do not implement
506 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
507 const std::string &Name = "", Instruction *InsertBefore = 0);
509 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
510 const std::string &Name, BasicBlock *InsertAtEnd);
512 Use Ops[2]; // CmpInst instructions always have 2 operands, optimize
515 // allocate space for exactly two operands
516 void *operator new(size_t s) {
517 return User::operator new(s, 2);
519 /// Construct a compare instruction, given the opcode, the predicate and
520 /// the two operands. Optionally (if InstBefore is specified) insert the
521 /// instruction into a BasicBlock right before the specified instruction.
522 /// The specified Instruction is allowed to be a dereferenced end iterator.
523 /// @brief Create a CmpInst
524 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
525 Value *S2, const std::string &Name = "",
526 Instruction *InsertBefore = 0);
528 /// Construct a compare instruction, given the opcode, the predicate and the
529 /// two operands. Also automatically insert this instruction to the end of
530 /// the BasicBlock specified.
531 /// @brief Create a CmpInst
532 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
533 Value *S2, const std::string &Name,
534 BasicBlock *InsertAtEnd);
536 /// @brief Get the opcode casted to the right type
537 OtherOps getOpcode() const {
538 return static_cast<OtherOps>(Instruction::getOpcode());
541 /// The predicate for CmpInst is defined by the subclasses but stored in
542 /// the SubclassData field (see Value.h). We allow it to be fetched here
543 /// as the predicate but there is no enum type for it, just the raw unsigned
544 /// short. This facilitates comparison of CmpInst instances without delving
545 /// into the subclasses since predicate values are distinct between the
546 /// CmpInst subclasses.
547 /// @brief Return the predicate for this instruction.
548 unsigned short getPredicate() const {
552 /// @brief Provide more efficient getOperand methods.
553 Value *getOperand(unsigned i) const {
554 assert(i < 2 && "getOperand() out of range!");
557 void setOperand(unsigned i, Value *Val) {
558 assert(i < 2 && "setOperand() out of range!");
562 /// @brief CmpInst instructions always have 2 operands.
563 unsigned getNumOperands() const { return 2; }
565 /// This is just a convenience that dispatches to the subclasses.
566 /// @brief Swap the operands and adjust predicate accordingly to retain
567 /// the same comparison.
570 /// This is just a convenience that dispatches to the subclasses.
571 /// @brief Determine if this CmpInst is commutative.
572 bool isCommutative();
574 /// This is just a convenience that dispatches to the subclasses.
575 /// @brief Determine if this is an equals/not equals predicate.
578 /// @returns true if the predicate is unsigned, false otherwise.
579 /// @brief Determine if the predicate is an unsigned operation.
580 static bool isUnsigned(unsigned short predicate);
582 /// @returns true if the predicate is signed, false otherwise.
583 /// @brief Determine if the predicate is an signed operation.
584 static bool isSigned(unsigned short predicate);
586 /// @brief Determine if the predicate is an ordered operation.
587 static bool isOrdered(unsigned short predicate);
589 /// @brief Determine if the predicate is an unordered operation.
590 static bool isUnordered(unsigned short predicate);
592 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
593 static inline bool classof(const CmpInst *) { return true; }
594 static inline bool classof(const Instruction *I) {
595 return I->getOpcode() == Instruction::ICmp ||
596 I->getOpcode() == Instruction::FCmp;
598 static inline bool classof(const Value *V) {
599 return isa<Instruction>(V) && classof(cast<Instruction>(V));
603 } // End llvm namespace