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(Instruction::TermOps iType, Use *Ops, unsigned NumOps,
33 Instruction *InsertBefore = 0);
34 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
35 Use *Ops, unsigned NumOps,
36 const std::string &Name = "", Instruction *InsertBefore = 0)
37 : Instruction(Ty, iType, Ops, NumOps, Name, InsertBefore) {}
39 TerminatorInst(Instruction::TermOps iType, Use *Ops, unsigned NumOps,
40 BasicBlock *InsertAtEnd);
41 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
42 Use *Ops, unsigned NumOps,
43 const std::string &Name, BasicBlock *InsertAtEnd)
44 : Instruction(Ty, iType, Ops, NumOps, Name, InsertAtEnd) {}
46 // Out of line virtual method, so the vtable, etc has a home.
49 /// Virtual methods - Terminators should overload these and provide inline
50 /// overrides of non-V methods.
51 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
52 virtual unsigned getNumSuccessorsV() const = 0;
53 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
56 virtual Instruction *clone() const = 0;
58 /// getNumSuccessors - Return the number of successors that this terminator
60 unsigned getNumSuccessors() const {
61 return getNumSuccessorsV();
64 /// getSuccessor - Return the specified successor.
66 BasicBlock *getSuccessor(unsigned idx) const {
67 return getSuccessorV(idx);
70 /// setSuccessor - Update the specified successor to point at the provided
72 void setSuccessor(unsigned idx, BasicBlock *B) {
73 setSuccessorV(idx, B);
76 // Methods for support type inquiry through isa, cast, and dyn_cast:
77 static inline bool classof(const TerminatorInst *) { return true; }
78 static inline bool classof(const Instruction *I) {
79 return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
81 static inline bool classof(const Value *V) {
82 return isa<Instruction>(V) && classof(cast<Instruction>(V));
86 //===----------------------------------------------------------------------===//
87 // UnaryInstruction Class
88 //===----------------------------------------------------------------------===//
90 class UnaryInstruction : public Instruction {
93 UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
94 const char *Name = 0, Instruction *IB = 0)
95 : Instruction(Ty, iType, &Op, 1, Name, IB), Op(V, this) {
97 UnaryInstruction(const Type *Ty, unsigned iType, Value *V,
98 const char *Name = 0, BasicBlock *IAE)
99 : Instruction(Ty, iType, &Op, 1, Name, IAE), Op(V, this) {
102 // Out of line virtual method, so the vtable, etc has a home.
105 // Transparently provide more efficient getOperand methods.
106 Value *getOperand(unsigned i) const {
107 assert(i == 0 && "getOperand() out of range!");
110 void setOperand(unsigned i, Value *Val) {
111 assert(i == 0 && "setOperand() out of range!");
114 unsigned getNumOperands() const { return 1; }
117 //===----------------------------------------------------------------------===//
118 // BinaryOperator Class
119 //===----------------------------------------------------------------------===//
121 class BinaryOperator : public Instruction {
124 void init(BinaryOps iType);
125 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
126 const std::string &Name, Instruction *InsertBefore)
127 : Instruction(Ty, iType, Ops, 2, Name, InsertBefore) {
128 Ops[0].init(S1, this);
129 Ops[1].init(S2, this);
132 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
133 const std::string &Name, BasicBlock *InsertAtEnd)
134 : Instruction(Ty, iType, Ops, 2, Name, InsertAtEnd) {
135 Ops[0].init(S1, this);
136 Ops[1].init(S2, this);
142 /// Transparently provide more efficient getOperand methods.
143 Value *getOperand(unsigned i) const {
144 assert(i < 2 && "getOperand() out of range!");
147 void setOperand(unsigned i, Value *Val) {
148 assert(i < 2 && "setOperand() out of range!");
151 unsigned getNumOperands() const { return 2; }
153 /// create() - Construct a binary instruction, given the opcode and the two
154 /// operands. Optionally (if InstBefore is specified) insert the instruction
155 /// into a BasicBlock right before the specified instruction. The specified
156 /// Instruction is allowed to be a dereferenced end iterator.
158 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
159 const std::string &Name = "",
160 Instruction *InsertBefore = 0);
162 /// create() - Construct a binary instruction, given the opcode and the two
163 /// operands. Also automatically insert this instruction to the end of the
164 /// BasicBlock specified.
166 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
167 const std::string &Name,
168 BasicBlock *InsertAtEnd);
170 /// create* - These methods just forward to create, and are useful when you
171 /// statically know what type of instruction you're going to create. These
172 /// helpers just save some typing.
173 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
174 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
175 const std::string &Name = "") {\
176 return create(Instruction::OPC, V1, V2, Name);\
178 #include "llvm/Instruction.def"
179 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
180 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
181 const std::string &Name, BasicBlock *BB) {\
182 return create(Instruction::OPC, V1, V2, Name, BB);\
184 #include "llvm/Instruction.def"
185 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
186 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
187 const std::string &Name, Instruction *I) {\
188 return create(Instruction::OPC, V1, V2, Name, I);\
190 #include "llvm/Instruction.def"
193 /// Helper functions to construct and inspect unary operations (NEG and NOT)
194 /// via binary operators SUB and XOR:
196 /// createNeg, createNot - Create the NEG and NOT
197 /// instructions out of SUB and XOR instructions.
199 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
200 Instruction *InsertBefore = 0);
201 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
202 BasicBlock *InsertAtEnd);
203 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
204 Instruction *InsertBefore = 0);
205 static BinaryOperator *createNot(Value *Op, const std::string &Name,
206 BasicBlock *InsertAtEnd);
208 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
210 static bool isNeg(const Value *V);
211 static bool isNot(const Value *V);
213 /// getNegArgument, getNotArgument - Helper functions to extract the
214 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
216 static const Value *getNegArgument(const Value *BinOp);
217 static Value *getNegArgument( Value *BinOp);
218 static const Value *getNotArgument(const Value *BinOp);
219 static Value *getNotArgument( Value *BinOp);
221 BinaryOps getOpcode() const {
222 return static_cast<BinaryOps>(Instruction::getOpcode());
225 virtual BinaryOperator *clone() const;
227 /// swapOperands - Exchange the two operands to this instruction.
228 /// This instruction is safe to use on any binary instruction and
229 /// does not modify the semantics of the instruction. If the
230 /// instruction is order dependent (SetLT f.e.) the opcode is
231 /// changed. If the instruction cannot be reversed (ie, it's a Div),
232 /// then return true.
236 // Methods for support type inquiry through isa, cast, and dyn_cast:
237 static inline bool classof(const BinaryOperator *) { return true; }
238 static inline bool classof(const Instruction *I) {
239 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
241 static inline bool classof(const Value *V) {
242 return isa<Instruction>(V) && classof(cast<Instruction>(V));
246 //===----------------------------------------------------------------------===//
248 //===----------------------------------------------------------------------===//
250 /// CastInst - This is the base class for all instructions that perform data
251 /// casts. It is simply provided so that instruction category testing
252 /// can be performed with code like:
254 /// if (isa<CastInst>(Instr)) { ... }
255 /// @brief Base class of casting instructions.
256 class CastInst : public UnaryInstruction {
257 /// @brief Copy constructor
258 CastInst(const CastInst &CI)
259 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
261 /// @brief Do not allow default construction
264 /// @brief Constructor with insert-before-instruction semantics for subclasses
265 CastInst(const Type *Ty, unsigned iType, Value *S,
266 const std::string &Name = "", Instruction *InsertBefore = 0)
267 : UnaryInstruction(Ty, iType, S, 0, InsertBefore) {
270 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
271 CastInst(const Type *Ty, unsigned iType, Value *S,
272 const std::string &Name, BasicBlock *InsertAtEnd)
273 : UnaryInstruction(Ty, iType, S, 0, InsertAtEnd) {
277 /// Provides a way to construct any of the CastInst subclasses using an
278 /// opcode instead of the subclass's constructor. The opcode must be in the
279 /// CastOps category (Instruction::isCast(opcode) returns true). This
280 /// constructor has insert-before-instruction semantics to automatically
281 /// insert the new CastInst before InsertBefore (if it is non-null).
282 /// @brief Construct any of the CastInst subclasses
283 static CastInst *create(
284 Instruction::CastOps, ///< The opcode of the cast instruction
285 Value *S, ///< The value to be casted (operand 0)
286 const Type *Ty, ///< The type to which cast should be made
287 const std::string &Name = "", ///< Name for the instruction
288 Instruction *InsertBefore = 0 ///< Place to insert the instruction
290 /// Provides a way to construct any of the CastInst subclasses using an
291 /// opcode instead of the subclass's constructor. The opcode must be in the
292 /// CastOps category. This constructor has insert-at-end-of-block semantics
293 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
295 /// @brief Construct any of the CastInst subclasses
296 static CastInst *create(
297 Instruction::CastOps, ///< The opcode for the cast instruction
298 Value *S, ///< The value to be casted (operand 0)
299 const Type *Ty, ///< The type to which operand is casted
300 const std::string &Name, ///< The name for the instruction
301 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
304 /// @brief Create a ZExt or BitCast cast instruction
305 static CastInst *createZExtOrBitCast(
306 Value *S, ///< The value to be casted (operand 0)
307 const Type *Ty, ///< The type to which cast should be made
308 const std::string &Name = "", ///< Name for the instruction
309 Instruction *InsertBefore = 0 ///< Place to insert the instruction
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 operand is casted
316 const std::string &Name, ///< The name for the instruction
317 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
320 /// @brief Create a SExt or BitCast cast instruction
321 static CastInst *createSExtOrBitCast(
322 Value *S, ///< The value to be casted (operand 0)
323 const Type *Ty, ///< The type to which cast should be made
324 const std::string &Name = "", ///< Name for the instruction
325 Instruction *InsertBefore = 0 ///< Place to insert the instruction
328 /// @brief Create a BitCast or a PtrToInt cast instruction
329 static CastInst *createPointerCast(
330 Value *S, ///< The pointer value to be casted (operand 0)
331 const Type *Ty, ///< The type to which operand is casted
332 const std::string &Name, ///< The name for the instruction
333 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
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 cast should be made
340 const std::string &Name = "", ///< Name for the instruction
341 Instruction *InsertBefore = 0 ///< Place to insert the instruction
344 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
345 static CastInst *createIntegerCast(
346 Value *S, ///< The pointer value to be casted (operand 0)
347 const Type *Ty, ///< The type to which cast should be made
348 bool isSigned, ///< Whether to regard S as signed or not
349 const std::string &Name = "", ///< Name for the instruction
350 Instruction *InsertBefore = 0 ///< Place to insert the instruction
353 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
354 static CastInst *createIntegerCast(
355 Value *S, ///< The integer value to be casted (operand 0)
356 const Type *Ty, ///< The integer type to which operand is casted
357 bool isSigned, ///< Whether to regard S as signed or not
358 const std::string &Name, ///< The name for the instruction
359 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
362 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
363 static CastInst *createFPCast(
364 Value *S, ///< The floating point value to be casted
365 const Type *Ty, ///< The floating point type to cast to
366 const std::string &Name = "", ///< Name for the instruction
367 Instruction *InsertBefore = 0 ///< Place to insert the instruction
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, ///< The name for the instruction
375 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
378 /// @brief Create a SExt or BitCast cast instruction
379 static CastInst *createSExtOrBitCast(
380 Value *S, ///< The value to be casted (operand 0)
381 const Type *Ty, ///< The type to which operand is casted
382 const std::string &Name, ///< The name for the instruction
383 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
386 /// @brief Create a Trunc or BitCast cast instruction
387 static CastInst *createTruncOrBitCast(
388 Value *S, ///< The value to be casted (operand 0)
389 const Type *Ty, ///< The type to which cast should be made
390 const std::string &Name = "", ///< Name for the instruction
391 Instruction *InsertBefore = 0 ///< Place to insert the instruction
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 operand is casted
398 const std::string &Name, ///< The name for the instruction
399 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
402 /// Returns the opcode necessary to cast Val into Ty using usual casting
404 /// @brief Infer the opcode for cast operand and type
405 static Instruction::CastOps getCastOpcode(
406 const Value *Val, ///< The value to cast
407 bool SrcIsSigned, ///< Whether to treat the source as signed
408 const Type *Ty, ///< The Type to which the value should be casted
409 bool DstIsSigned ///< Whether to treate the dest. as signed
412 /// There are several places where we need to know if a cast instruction
413 /// only deals with integer source and destination types. To simplify that
414 /// logic, this method is provided.
415 /// @returns true iff the cast has only integral typed operand and dest type.
416 /// @brief Determine if this is an integer-only cast.
417 bool isIntegerCast() const;
419 /// A lossless cast is one that does not alter the basic value. It implies
420 /// a no-op cast but is more stringent, preventing things like int->float,
421 /// long->double, int->ptr, or packed->anything.
422 /// @returns true iff the cast is lossless.
423 /// @brief Determine if this is a lossless cast.
424 bool isLosslessCast() const;
426 /// A no-op cast is one that can be effected without changing any bits.
427 /// It implies that the source and destination types are the same size. The
428 /// IntPtrTy argument is used to make accurate determinations for casts
429 /// involving Integer and Pointer types. They are no-op casts if the integer
430 /// is the same size as the pointer. However, pointer size varies with
431 /// platform. Generally, the result of TargetData::getIntPtrType() should be
432 /// passed in. If that's not available, use Type::Int64Ty, which will make
433 /// the isNoopCast call conservative.
434 /// @brief Determine if this cast is a no-op cast.
436 const Type *IntPtrTy ///< Integer type corresponding to pointer
439 /// Determine how a pair of casts can be eliminated, if they can be at all.
440 /// This is a helper function for both CastInst and ConstantExpr.
441 /// @returns 0 if the CastInst pair can't be eliminated
442 /// @returns Instruction::CastOps value for a cast that can replace
443 /// the pair, casting SrcTy to DstTy.
444 /// @brief Determine if a cast pair is eliminable
445 static unsigned isEliminableCastPair(
446 Instruction::CastOps firstOpcode, ///< Opcode of first cast
447 Instruction::CastOps secondOpcode, ///< Opcode of second cast
448 const Type *SrcTy, ///< SrcTy of 1st cast
449 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
450 const Type *DstTy, ///< DstTy of 2nd cast
451 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
454 /// @brief Return the opcode of this CastInst
455 Instruction::CastOps getOpcode() const {
456 return Instruction::CastOps(Instruction::getOpcode());
459 /// @brief Return the source type, as a convenience
460 const Type* getSrcTy() const { return getOperand(0)->getType(); }
461 /// @brief Return the destination type, as a convenience
462 const Type* getDestTy() const { return getType(); }
464 /// This method can be used to determine if a cast from S to DstTy using
465 /// Opcode op is valid or not.
466 /// @returns true iff the proposed cast is valid.
467 /// @brief Determine if a cast is valid without creating one.
468 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
470 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
471 static inline bool classof(const CastInst *) { return true; }
472 static inline bool classof(const Instruction *I) {
473 return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
475 static inline bool classof(const Value *V) {
476 return isa<Instruction>(V) && classof(cast<Instruction>(V));
480 //===----------------------------------------------------------------------===//
482 //===----------------------------------------------------------------------===//
484 /// This class is the base class for the comparison instructions.
485 /// @brief Abstract base class of comparison instructions.
486 class CmpInst: public Instruction {
487 CmpInst(); // do not implement
489 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
490 const std::string &Name = "", Instruction *InsertBefore = 0);
492 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
493 const std::string &Name, BasicBlock *InsertAtEnd);
495 Use Ops[2]; // CmpInst instructions always have 2 operands, optimize
498 /// Construct a compare instruction, given the opcode, the predicate and
499 /// the two operands. Optionally (if InstBefore is specified) insert the
500 /// instruction into a BasicBlock right before the specified instruction.
501 /// The specified Instruction is allowed to be a dereferenced end iterator.
502 /// @brief Create a CmpInst
503 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
504 Value *S2, const std::string &Name = "",
505 Instruction *InsertBefore = 0);
507 /// Construct a compare instruction, given the opcode, the predicate and the
508 /// two operands. Also automatically insert this instruction to the end of
509 /// the BasicBlock specified.
510 /// @brief Create a CmpInst
511 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
512 Value *S2, const std::string &Name,
513 BasicBlock *InsertAtEnd);
515 /// @brief Implement superclass method.
516 virtual CmpInst *clone() const;
518 /// @brief Get the opcode casted to the right type
519 OtherOps getOpcode() const {
520 return static_cast<OtherOps>(Instruction::getOpcode());
523 /// The predicate for CmpInst is defined by the subclasses but stored in
524 /// the SubclassData field (see Value.h). We allow it to be fetched here
525 /// as the predicate but there is no enum type for it, just the raw unsigned
526 /// short. This facilitates comparison of CmpInst instances without delving
527 /// into the subclasses since predicate values are distinct between the
528 /// CmpInst subclasses.
529 /// @brief Return the predicate for this instruction.
530 unsigned short getPredicate() const {
534 /// @brief Provide more efficient getOperand methods.
535 Value *getOperand(unsigned i) const {
536 assert(i < 2 && "getOperand() out of range!");
539 void setOperand(unsigned i, Value *Val) {
540 assert(i < 2 && "setOperand() out of range!");
544 /// @brief CmpInst instructions always have 2 operands.
545 unsigned getNumOperands() const { return 2; }
547 /// This is just a convenience that dispatches to the subclasses.
548 /// @brief Swap the operands and adjust predicate accordingly to retain
549 /// the same comparison.
552 /// This is just a convenience that dispatches to the subclasses.
553 /// @brief Determine if this CmpInst is commutative.
554 bool isCommutative();
556 /// This is just a convenience that dispatches to the subclasses.
557 /// @brief Determine if this is an equals/not equals predicate.
560 /// @returns true if the predicate is unsigned, false otherwise.
561 /// @brief Determine if the predicate is an unsigned operation.
562 static bool isUnsigned(unsigned short predicate);
564 /// @returns true if the predicate is signed, false otherwise.
565 /// @brief Determine if the predicate is an signed operation.
566 static bool isSigned(unsigned short predicate);
568 /// @brief Determine if the predicate is an ordered operation.
569 static bool isOrdered(unsigned short predicate);
571 /// @brief Determine if the predicate is an unordered operation.
572 static bool isUnordered(unsigned short predicate);
574 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
575 static inline bool classof(const CmpInst *) { return true; }
576 static inline bool classof(const Instruction *I) {
577 return I->getOpcode() == Instruction::ICmp ||
578 I->getOpcode() == Instruction::FCmp;
580 static inline bool classof(const Value *V) {
581 return isa<Instruction>(V) && classof(cast<Instruction>(V));
585 } // End llvm namespace