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 instruction
240 /// cannot be reversed (ie, it's a Div), 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) {
285 /// Provides a way to construct any of the CastInst subclasses using an
286 /// opcode instead of the subclass's constructor. The opcode must be in the
287 /// CastOps category (Instruction::isCast(opcode) returns true). This
288 /// constructor has insert-before-instruction semantics to automatically
289 /// insert the new CastInst before InsertBefore (if it is non-null).
290 /// @brief Construct any of the CastInst subclasses
291 static CastInst *create(
292 Instruction::CastOps, ///< The opcode of the cast instruction
293 Value *S, ///< The value to be casted (operand 0)
294 const Type *Ty, ///< The type to which cast should be made
295 const std::string &Name = "", ///< Name for the instruction
296 Instruction *InsertBefore = 0 ///< Place to insert the instruction
298 /// Provides a way to construct any of the CastInst subclasses using an
299 /// opcode instead of the subclass's constructor. The opcode must be in the
300 /// CastOps category. This constructor has insert-at-end-of-block semantics
301 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
303 /// @brief Construct any of the CastInst subclasses
304 static CastInst *create(
305 Instruction::CastOps, ///< The opcode for the cast instruction
306 Value *S, ///< The value to be casted (operand 0)
307 const Type *Ty, ///< The type to which operand is casted
308 const std::string &Name, ///< The name for the instruction
309 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
312 /// @brief Create a ZExt or BitCast cast instruction
313 static CastInst *createZExtOrBitCast(
314 Value *S, ///< The value to be casted (operand 0)
315 const Type *Ty, ///< The type to which cast should be made
316 const std::string &Name = "", ///< Name for the instruction
317 Instruction *InsertBefore = 0 ///< Place to insert the instruction
320 /// @brief Create a ZExt or BitCast cast instruction
321 static CastInst *createZExtOrBitCast(
322 Value *S, ///< The value to be casted (operand 0)
323 const Type *Ty, ///< The type to which operand is casted
324 const std::string &Name, ///< The name for the instruction
325 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
328 /// @brief Create a SExt or BitCast cast instruction
329 static CastInst *createSExtOrBitCast(
330 Value *S, ///< The value to be casted (operand 0)
331 const Type *Ty, ///< The type to which cast should be made
332 const std::string &Name = "", ///< Name for the instruction
333 Instruction *InsertBefore = 0 ///< Place to insert the instruction
336 /// @brief Create a BitCast or a PtrToInt cast instruction
337 static CastInst *createPointerCast(
338 Value *S, ///< The pointer value to be casted (operand 0)
339 const Type *Ty, ///< The type to which operand is casted
340 const std::string &Name, ///< The name for the instruction
341 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
344 /// @brief Create a BitCast or a PtrToInt cast instruction
345 static CastInst *createPointerCast(
346 Value *S, ///< The pointer value to be casted (operand 0)
347 const Type *Ty, ///< The type to which cast should be made
348 const std::string &Name = "", ///< Name for the instruction
349 Instruction *InsertBefore = 0 ///< Place to insert the instruction
352 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
353 static CastInst *createIntegerCast(
354 Value *S, ///< The pointer value to be casted (operand 0)
355 const Type *Ty, ///< The type to which cast should be made
356 bool isSigned, ///< Whether to regard S as signed or not
357 const std::string &Name = "", ///< Name for the instruction
358 Instruction *InsertBefore = 0 ///< Place to insert the instruction
361 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
362 static CastInst *createIntegerCast(
363 Value *S, ///< The integer value to be casted (operand 0)
364 const Type *Ty, ///< The integer type to which operand is casted
365 bool isSigned, ///< Whether to regard S as signed or not
366 const std::string &Name, ///< The name for the instruction
367 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
370 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
371 static CastInst *createFPCast(
372 Value *S, ///< The floating point value to be casted
373 const Type *Ty, ///< The floating point type to cast to
374 const std::string &Name = "", ///< Name for the instruction
375 Instruction *InsertBefore = 0 ///< Place to insert the instruction
378 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
379 static CastInst *createFPCast(
380 Value *S, ///< The floating point value to be casted
381 const Type *Ty, ///< The floating point type to cast to
382 const std::string &Name, ///< The name for the instruction
383 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
386 /// @brief Create a SExt or BitCast cast instruction
387 static CastInst *createSExtOrBitCast(
388 Value *S, ///< The value to be casted (operand 0)
389 const Type *Ty, ///< The type to which operand is casted
390 const std::string &Name, ///< The name for the instruction
391 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
394 /// @brief Create a Trunc or BitCast cast instruction
395 static CastInst *createTruncOrBitCast(
396 Value *S, ///< The value to be casted (operand 0)
397 const Type *Ty, ///< The type to which cast should be made
398 const std::string &Name = "", ///< Name for the instruction
399 Instruction *InsertBefore = 0 ///< Place to insert the instruction
402 /// @brief Create a Trunc or BitCast cast instruction
403 static CastInst *createTruncOrBitCast(
404 Value *S, ///< The value to be casted (operand 0)
405 const Type *Ty, ///< The type to which operand is casted
406 const std::string &Name, ///< The name for the instruction
407 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
410 /// @brief Check whether it is valid to call getCastOpcode for these types.
411 static bool isCastable(
412 const Type *SrcTy, ///< The Type from which the value should be cast.
413 const Type *DestTy ///< The Type to which the value should be cast.
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 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
502 CmpInst(); // do not implement
504 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
505 const std::string &Name = "", Instruction *InsertBefore = 0);
507 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
508 const std::string &Name, BasicBlock *InsertAtEnd);
510 Use Ops[2]; // CmpInst instructions always have 2 operands, optimize
513 // allocate space for exactly two operands
514 void *operator new(size_t s) {
515 return User::operator new(s, 2);
517 /// Construct a compare instruction, given the opcode, the predicate and
518 /// the two operands. Optionally (if InstBefore is specified) insert the
519 /// instruction into a BasicBlock right before the specified instruction.
520 /// The specified Instruction is allowed to be a dereferenced end iterator.
521 /// @brief Create a CmpInst
522 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
523 Value *S2, const std::string &Name = "",
524 Instruction *InsertBefore = 0);
526 /// Construct a compare instruction, given the opcode, the predicate and the
527 /// two operands. Also automatically insert this instruction to the end of
528 /// the BasicBlock specified.
529 /// @brief Create a CmpInst
530 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
531 Value *S2, const std::string &Name,
532 BasicBlock *InsertAtEnd);
534 /// @brief Get the opcode casted to the right type
535 OtherOps getOpcode() const {
536 return static_cast<OtherOps>(Instruction::getOpcode());
539 /// The predicate for CmpInst is defined by the subclasses but stored in
540 /// the SubclassData field (see Value.h). We allow it to be fetched here
541 /// as the predicate but there is no enum type for it, just the raw unsigned
542 /// short. This facilitates comparison of CmpInst instances without delving
543 /// into the subclasses since predicate values are distinct between the
544 /// CmpInst subclasses.
545 /// @brief Return the predicate for this instruction.
546 unsigned short getPredicate() const {
550 /// @brief Provide more efficient getOperand methods.
551 Value *getOperand(unsigned i) const {
552 assert(i < 2 && "getOperand() out of range!");
555 void setOperand(unsigned i, Value *Val) {
556 assert(i < 2 && "setOperand() out of range!");
560 /// @brief CmpInst instructions always have 2 operands.
561 unsigned getNumOperands() const { return 2; }
563 /// This is just a convenience that dispatches to the subclasses.
564 /// @brief Swap the operands and adjust predicate accordingly to retain
565 /// the same comparison.
568 /// This is just a convenience that dispatches to the subclasses.
569 /// @brief Determine if this CmpInst is commutative.
570 bool isCommutative();
572 /// This is just a convenience that dispatches to the subclasses.
573 /// @brief Determine if this is an equals/not equals predicate.
576 /// @returns true if the predicate is unsigned, false otherwise.
577 /// @brief Determine if the predicate is an unsigned operation.
578 static bool isUnsigned(unsigned short predicate);
580 /// @returns true if the predicate is signed, false otherwise.
581 /// @brief Determine if the predicate is an signed operation.
582 static bool isSigned(unsigned short predicate);
584 /// @brief Determine if the predicate is an ordered operation.
585 static bool isOrdered(unsigned short predicate);
587 /// @brief Determine if the predicate is an unordered operation.
588 static bool isUnordered(unsigned short predicate);
590 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
591 static inline bool classof(const CmpInst *) { return true; }
592 static inline bool classof(const Instruction *I) {
593 return I->getOpcode() == Instruction::ICmp ||
594 I->getOpcode() == Instruction::FCmp;
596 static inline bool classof(const Value *V) {
597 return isa<Instruction>(V) && classof(cast<Instruction>(V));
601 } // End llvm namespace