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 {
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; }
112 // Methods for support type inquiry through isa, cast, and dyn_cast:
113 static inline bool classof(const UnaryInstruction *) { return true; }
114 static inline bool classof(const Instruction *I) {
115 return I->getOpcode() == Instruction::Malloc ||
116 I->getOpcode() == Instruction::Alloca ||
117 I->getOpcode() == Instruction::Free ||
118 I->getOpcode() == Instruction::Load ||
119 I->getOpcode() == Instruction::VAArg ||
120 (I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd);
122 static inline bool classof(const Value *V) {
123 return isa<Instruction>(V) && classof(cast<Instruction>(V));
127 //===----------------------------------------------------------------------===//
128 // BinaryOperator Class
129 //===----------------------------------------------------------------------===//
131 class BinaryOperator : public Instruction {
134 void init(BinaryOps iType);
135 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
136 const std::string &Name, Instruction *InsertBefore);
137 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
138 const std::string &Name, BasicBlock *InsertAtEnd);
141 /// Transparently provide more efficient getOperand methods.
142 Value *getOperand(unsigned i) const {
143 assert(i < 2 && "getOperand() out of range!");
146 void setOperand(unsigned i, Value *Val) {
147 assert(i < 2 && "setOperand() out of range!");
150 unsigned getNumOperands() const { return 2; }
152 /// create() - Construct a binary instruction, given the opcode and the two
153 /// operands. Optionally (if InstBefore is specified) insert the instruction
154 /// into a BasicBlock right before the specified instruction. The specified
155 /// Instruction is allowed to be a dereferenced end iterator.
157 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
158 const std::string &Name = "",
159 Instruction *InsertBefore = 0);
161 /// create() - Construct a binary instruction, given the opcode and the two
162 /// operands. Also automatically insert this instruction to the end of the
163 /// BasicBlock specified.
165 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
166 const std::string &Name,
167 BasicBlock *InsertAtEnd);
169 /// create* - These methods just forward to create, and are useful when you
170 /// statically know what type of instruction you're going to create. These
171 /// helpers just save some typing.
172 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
173 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
174 const std::string &Name = "") {\
175 return create(Instruction::OPC, V1, V2, Name);\
177 #include "llvm/Instruction.def"
178 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
179 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
180 const std::string &Name, BasicBlock *BB) {\
181 return create(Instruction::OPC, V1, V2, Name, BB);\
183 #include "llvm/Instruction.def"
184 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
185 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
186 const std::string &Name, Instruction *I) {\
187 return create(Instruction::OPC, V1, V2, Name, I);\
189 #include "llvm/Instruction.def"
192 /// Helper functions to construct and inspect unary operations (NEG and NOT)
193 /// via binary operators SUB and XOR:
195 /// createNeg, createNot - Create the NEG and NOT
196 /// instructions out of SUB and XOR instructions.
198 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
199 Instruction *InsertBefore = 0);
200 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
201 BasicBlock *InsertAtEnd);
202 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
203 Instruction *InsertBefore = 0);
204 static BinaryOperator *createNot(Value *Op, const std::string &Name,
205 BasicBlock *InsertAtEnd);
207 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
209 static bool isNeg(const Value *V);
210 static bool isNot(const Value *V);
212 /// getNegArgument, getNotArgument - Helper functions to extract the
213 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
215 static const Value *getNegArgument(const Value *BinOp);
216 static Value *getNegArgument( Value *BinOp);
217 static const Value *getNotArgument(const Value *BinOp);
218 static Value *getNotArgument( Value *BinOp);
220 BinaryOps getOpcode() const {
221 return static_cast<BinaryOps>(Instruction::getOpcode());
224 virtual BinaryOperator *clone() const;
226 /// swapOperands - Exchange the two operands to this instruction.
227 /// This instruction is safe to use on any binary instruction and
228 /// does not modify the semantics of the instruction. If the
229 /// instruction is order dependent (SetLT f.e.) the opcode is
230 /// changed. If the instruction cannot be reversed (ie, it's a Div),
231 /// then return true.
235 // Methods for support type inquiry through isa, cast, and dyn_cast:
236 static inline bool classof(const BinaryOperator *) { return true; }
237 static inline bool classof(const Instruction *I) {
238 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
240 static inline bool classof(const Value *V) {
241 return isa<Instruction>(V) && classof(cast<Instruction>(V));
245 //===----------------------------------------------------------------------===//
247 //===----------------------------------------------------------------------===//
249 /// CastInst - This is the base class for all instructions that perform data
250 /// casts. It is simply provided so that instruction category testing
251 /// can be performed with code like:
253 /// if (isa<CastInst>(Instr)) { ... }
254 /// @brief Base class of casting instructions.
255 class CastInst : public UnaryInstruction {
256 /// @brief Copy constructor
257 CastInst(const CastInst &CI)
258 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
260 /// @brief Do not allow default construction
263 /// @brief Constructor with insert-before-instruction semantics for subclasses
264 CastInst(const Type *Ty, unsigned iType, Value *S,
265 const std::string &Name = "", Instruction *InsertBefore = 0)
266 : UnaryInstruction(Ty, iType, S, InsertBefore) {
269 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
270 CastInst(const Type *Ty, unsigned iType, Value *S,
271 const std::string &Name, BasicBlock *InsertAtEnd)
272 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
276 /// Provides a way to construct any of the CastInst subclasses using an
277 /// opcode instead of the subclass's constructor. The opcode must be in the
278 /// CastOps category (Instruction::isCast(opcode) returns true). This
279 /// constructor has insert-before-instruction semantics to automatically
280 /// insert the new CastInst before InsertBefore (if it is non-null).
281 /// @brief Construct any of the CastInst subclasses
282 static CastInst *create(
283 Instruction::CastOps, ///< The opcode of the cast instruction
284 Value *S, ///< The value to be casted (operand 0)
285 const Type *Ty, ///< The type to which cast should be made
286 const std::string &Name = "", ///< Name for the instruction
287 Instruction *InsertBefore = 0 ///< Place to insert the instruction
289 /// Provides a way to construct any of the CastInst subclasses using an
290 /// opcode instead of the subclass's constructor. The opcode must be in the
291 /// CastOps category. This constructor has insert-at-end-of-block semantics
292 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
294 /// @brief Construct any of the CastInst subclasses
295 static CastInst *create(
296 Instruction::CastOps, ///< The opcode for the cast instruction
297 Value *S, ///< The value to be casted (operand 0)
298 const Type *Ty, ///< The type to which operand is casted
299 const std::string &Name, ///< The name for the instruction
300 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
303 /// @brief Create a ZExt or BitCast cast instruction
304 static CastInst *createZExtOrBitCast(
305 Value *S, ///< The value to be casted (operand 0)
306 const Type *Ty, ///< The type to which cast should be made
307 const std::string &Name = "", ///< Name for the instruction
308 Instruction *InsertBefore = 0 ///< Place to insert the instruction
311 /// @brief Create a ZExt or BitCast cast instruction
312 static CastInst *createZExtOrBitCast(
313 Value *S, ///< The value to be casted (operand 0)
314 const Type *Ty, ///< The type to which operand is casted
315 const std::string &Name, ///< The name for the instruction
316 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
319 /// @brief Create a SExt or BitCast cast instruction
320 static CastInst *createSExtOrBitCast(
321 Value *S, ///< The value to be casted (operand 0)
322 const Type *Ty, ///< The type to which cast should be made
323 const std::string &Name = "", ///< Name for the instruction
324 Instruction *InsertBefore = 0 ///< Place to insert the instruction
327 /// @brief Create a BitCast or a PtrToInt cast instruction
328 static CastInst *createPointerCast(
329 Value *S, ///< The pointer value to be casted (operand 0)
330 const Type *Ty, ///< The type to which operand is casted
331 const std::string &Name, ///< The name for the instruction
332 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
335 /// @brief Create a BitCast or a PtrToInt cast instruction
336 static CastInst *createPointerCast(
337 Value *S, ///< The pointer value to be casted (operand 0)
338 const Type *Ty, ///< The type to which cast should be made
339 const std::string &Name = "", ///< Name for the instruction
340 Instruction *InsertBefore = 0 ///< Place to insert the instruction
343 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
344 static CastInst *createIntegerCast(
345 Value *S, ///< The pointer value to be casted (operand 0)
346 const Type *Ty, ///< The type to which cast should be made
347 bool isSigned, ///< Whether to regard S as signed or not
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 integer value to be casted (operand 0)
355 const Type *Ty, ///< The integer type to which operand is casted
356 bool isSigned, ///< Whether to regard S as signed or not
357 const std::string &Name, ///< The name for the instruction
358 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
361 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
362 static CastInst *createFPCast(
363 Value *S, ///< The floating point value to be casted
364 const Type *Ty, ///< The floating point type to cast to
365 const std::string &Name = "", ///< Name for the instruction
366 Instruction *InsertBefore = 0 ///< Place to insert the instruction
369 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
370 static CastInst *createFPCast(
371 Value *S, ///< The floating point value to be casted
372 const Type *Ty, ///< The floating point type to cast to
373 const std::string &Name, ///< The name for the instruction
374 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
377 /// @brief Create a SExt or BitCast cast instruction
378 static CastInst *createSExtOrBitCast(
379 Value *S, ///< The value to be casted (operand 0)
380 const Type *Ty, ///< The type to which operand is casted
381 const std::string &Name, ///< The name for the instruction
382 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
385 /// @brief Create a Trunc or BitCast cast instruction
386 static CastInst *createTruncOrBitCast(
387 Value *S, ///< The value to be casted (operand 0)
388 const Type *Ty, ///< The type to which cast should be made
389 const std::string &Name = "", ///< Name for the instruction
390 Instruction *InsertBefore = 0 ///< Place to insert the instruction
393 /// @brief Create a Trunc or BitCast cast instruction
394 static CastInst *createTruncOrBitCast(
395 Value *S, ///< The value to be casted (operand 0)
396 const Type *Ty, ///< The type to which operand is casted
397 const std::string &Name, ///< The name for the instruction
398 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
401 /// @brief Check whether it is valid to call getCastOpcode for these types.
402 static bool isCastable(
403 const Type *SrcTy, ///< The Type from which the value should be cast.
404 const Type *DestTy ///< The Type to which the value should be cast.
407 /// Returns the opcode necessary to cast Val into Ty using usual casting
409 /// @brief Infer the opcode for cast operand and type
410 static Instruction::CastOps getCastOpcode(
411 const Value *Val, ///< The value to cast
412 bool SrcIsSigned, ///< Whether to treat the source as signed
413 const Type *Ty, ///< The Type to which the value should be casted
414 bool DstIsSigned ///< Whether to treate the dest. as signed
417 /// There are several places where we need to know if a cast instruction
418 /// only deals with integer source and destination types. To simplify that
419 /// logic, this method is provided.
420 /// @returns true iff the cast has only integral typed operand and dest type.
421 /// @brief Determine if this is an integer-only cast.
422 bool isIntegerCast() const;
424 /// A lossless cast is one that does not alter the basic value. It implies
425 /// a no-op cast but is more stringent, preventing things like int->float,
426 /// long->double, int->ptr, or vector->anything.
427 /// @returns true iff the cast is lossless.
428 /// @brief Determine if this is a lossless cast.
429 bool isLosslessCast() const;
431 /// A no-op cast is one that can be effected without changing any bits.
432 /// It implies that the source and destination types are the same size. The
433 /// IntPtrTy argument is used to make accurate determinations for casts
434 /// involving Integer and Pointer types. They are no-op casts if the integer
435 /// is the same size as the pointer. However, pointer size varies with
436 /// platform. Generally, the result of TargetData::getIntPtrType() should be
437 /// passed in. If that's not available, use Type::Int64Ty, which will make
438 /// the isNoopCast call conservative.
439 /// @brief Determine if this cast is a no-op cast.
441 const Type *IntPtrTy ///< Integer type corresponding to pointer
444 /// Determine how a pair of casts can be eliminated, if they can be at all.
445 /// This is a helper function for both CastInst and ConstantExpr.
446 /// @returns 0 if the CastInst pair can't be eliminated
447 /// @returns Instruction::CastOps value for a cast that can replace
448 /// the pair, casting SrcTy to DstTy.
449 /// @brief Determine if a cast pair is eliminable
450 static unsigned isEliminableCastPair(
451 Instruction::CastOps firstOpcode, ///< Opcode of first cast
452 Instruction::CastOps secondOpcode, ///< Opcode of second cast
453 const Type *SrcTy, ///< SrcTy of 1st cast
454 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
455 const Type *DstTy, ///< DstTy of 2nd cast
456 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
459 /// @brief Return the opcode of this CastInst
460 Instruction::CastOps getOpcode() const {
461 return Instruction::CastOps(Instruction::getOpcode());
464 /// @brief Return the source type, as a convenience
465 const Type* getSrcTy() const { return getOperand(0)->getType(); }
466 /// @brief Return the destination type, as a convenience
467 const Type* getDestTy() const { return getType(); }
469 /// This method can be used to determine if a cast from S to DstTy using
470 /// Opcode op is valid or not.
471 /// @returns true iff the proposed cast is valid.
472 /// @brief Determine if a cast is valid without creating one.
473 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
475 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
476 static inline bool classof(const CastInst *) { return true; }
477 static inline bool classof(const Instruction *I) {
478 return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
480 static inline bool classof(const Value *V) {
481 return isa<Instruction>(V) && classof(cast<Instruction>(V));
485 //===----------------------------------------------------------------------===//
487 //===----------------------------------------------------------------------===//
489 /// This class is the base class for the comparison instructions.
490 /// @brief Abstract base class of comparison instructions.
491 class CmpInst: public Instruction {
492 CmpInst(); // do not implement
494 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
495 const std::string &Name = "", Instruction *InsertBefore = 0);
497 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
498 const std::string &Name, BasicBlock *InsertAtEnd);
500 Use Ops[2]; // CmpInst instructions always have 2 operands, optimize
503 /// Construct a compare instruction, given the opcode, the predicate and
504 /// the two operands. Optionally (if InstBefore is specified) insert the
505 /// instruction into a BasicBlock right before the specified instruction.
506 /// The specified Instruction is allowed to be a dereferenced end iterator.
507 /// @brief Create a CmpInst
508 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
509 Value *S2, const std::string &Name = "",
510 Instruction *InsertBefore = 0);
512 /// Construct a compare instruction, given the opcode, the predicate and the
513 /// two operands. Also automatically insert this instruction to the end of
514 /// the BasicBlock specified.
515 /// @brief Create a CmpInst
516 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
517 Value *S2, const std::string &Name,
518 BasicBlock *InsertAtEnd);
520 /// @brief Get the opcode casted to the right type
521 OtherOps getOpcode() const {
522 return static_cast<OtherOps>(Instruction::getOpcode());
525 /// The predicate for CmpInst is defined by the subclasses but stored in
526 /// the SubclassData field (see Value.h). We allow it to be fetched here
527 /// as the predicate but there is no enum type for it, just the raw unsigned
528 /// short. This facilitates comparison of CmpInst instances without delving
529 /// into the subclasses since predicate values are distinct between the
530 /// CmpInst subclasses.
531 /// @brief Return the predicate for this instruction.
532 unsigned short getPredicate() const {
536 /// @brief Provide more efficient getOperand methods.
537 Value *getOperand(unsigned i) const {
538 assert(i < 2 && "getOperand() out of range!");
541 void setOperand(unsigned i, Value *Val) {
542 assert(i < 2 && "setOperand() out of range!");
546 /// @brief CmpInst instructions always have 2 operands.
547 unsigned getNumOperands() const { return 2; }
549 /// This is just a convenience that dispatches to the subclasses.
550 /// @brief Swap the operands and adjust predicate accordingly to retain
551 /// the same comparison.
554 /// This is just a convenience that dispatches to the subclasses.
555 /// @brief Determine if this CmpInst is commutative.
556 bool isCommutative();
558 /// This is just a convenience that dispatches to the subclasses.
559 /// @brief Determine if this is an equals/not equals predicate.
562 /// @returns true if the predicate is unsigned, false otherwise.
563 /// @brief Determine if the predicate is an unsigned operation.
564 static bool isUnsigned(unsigned short predicate);
566 /// @returns true if the predicate is signed, false otherwise.
567 /// @brief Determine if the predicate is an signed operation.
568 static bool isSigned(unsigned short predicate);
570 /// @brief Determine if the predicate is an ordered operation.
571 static bool isOrdered(unsigned short predicate);
573 /// @brief Determine if the predicate is an unordered operation.
574 static bool isUnordered(unsigned short predicate);
576 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
577 static inline bool classof(const CmpInst *) { return true; }
578 static inline bool classof(const Instruction *I) {
579 return I->getOpcode() == Instruction::ICmp ||
580 I->getOpcode() == Instruction::FCmp;
582 static inline bool classof(const Value *V) {
583 return isa<Instruction>(V) && classof(cast<Instruction>(V));
587 } // End llvm namespace