1 //===-- llvm/InstrTypes.h - Important Instruction subclasses ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines various meta classes of instructions that exist in the VM
11 // representation. Specific concrete subclasses of these may be found in the
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTION_TYPES_H
17 #define LLVM_INSTRUCTION_TYPES_H
19 #include "llvm/Instruction.h"
23 //===----------------------------------------------------------------------===//
24 // TerminatorInst Class
25 //===----------------------------------------------------------------------===//
27 /// TerminatorInst - Subclasses of this class are all able to terminate a basic
28 /// block. Thus, these are all the flow control type of operations.
30 class TerminatorInst : public Instruction {
32 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
33 Use *Ops, unsigned NumOps,
34 Instruction *InsertBefore = 0)
35 : Instruction(Ty, iType, Ops, NumOps, InsertBefore) {}
37 TerminatorInst(const Type *Ty, Instruction::TermOps iType,
38 Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd)
39 : Instruction(Ty, iType, Ops, NumOps, InsertAtEnd) {}
41 // Out of line virtual method, so the vtable, etc has a home.
44 /// Virtual methods - Terminators should overload these and provide inline
45 /// overrides of non-V methods.
46 virtual BasicBlock *getSuccessorV(unsigned idx) const = 0;
47 virtual unsigned getNumSuccessorsV() const = 0;
48 virtual void setSuccessorV(unsigned idx, BasicBlock *B) = 0;
51 virtual Instruction *clone() const = 0;
53 /// getNumSuccessors - Return the number of successors that this terminator
55 unsigned getNumSuccessors() const {
56 return getNumSuccessorsV();
59 /// getSuccessor - Return the specified successor.
61 BasicBlock *getSuccessor(unsigned idx) const {
62 return getSuccessorV(idx);
65 /// setSuccessor - Update the specified successor to point at the provided
67 void setSuccessor(unsigned idx, BasicBlock *B) {
68 setSuccessorV(idx, B);
71 // Methods for support type inquiry through isa, cast, and dyn_cast:
72 static inline bool classof(const TerminatorInst *) { return true; }
73 static inline bool classof(const Instruction *I) {
74 return I->getOpcode() >= TermOpsBegin && I->getOpcode() < TermOpsEnd;
76 static inline bool classof(const Value *V) {
77 return isa<Instruction>(V) && classof(cast<Instruction>(V));
81 //===----------------------------------------------------------------------===//
82 // UnaryInstruction Class
83 //===----------------------------------------------------------------------===//
85 class UnaryInstruction : public Instruction {
88 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, Instruction *IB =0)
89 : Instruction(Ty, iType, &Op, 1, IB), Op(V, this) {
91 UnaryInstruction(const Type *Ty, unsigned iType, Value *V, BasicBlock *IAE)
92 : Instruction(Ty, iType, &Op, 1, IAE), Op(V, this) {
95 // Out of line virtual method, so the vtable, etc has a home.
98 // Transparently provide more efficient getOperand methods.
99 Value *getOperand(unsigned i) const {
100 assert(i == 0 && "getOperand() out of range!");
103 void setOperand(unsigned i, Value *Val) {
104 assert(i == 0 && "setOperand() out of range!");
107 unsigned getNumOperands() const { return 1; }
110 //===----------------------------------------------------------------------===//
111 // BinaryOperator Class
112 //===----------------------------------------------------------------------===//
114 class BinaryOperator : public Instruction {
117 void init(BinaryOps iType);
118 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
119 const std::string &Name, Instruction *InsertBefore);
120 BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty,
121 const std::string &Name, BasicBlock *InsertAtEnd);
124 /// Transparently provide more efficient getOperand methods.
125 Value *getOperand(unsigned i) const {
126 assert(i < 2 && "getOperand() out of range!");
129 void setOperand(unsigned i, Value *Val) {
130 assert(i < 2 && "setOperand() out of range!");
133 unsigned getNumOperands() const { return 2; }
135 /// create() - Construct a binary instruction, given the opcode and the two
136 /// operands. Optionally (if InstBefore is specified) insert the instruction
137 /// into a BasicBlock right before the specified instruction. The specified
138 /// Instruction is allowed to be a dereferenced end iterator.
140 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
141 const std::string &Name = "",
142 Instruction *InsertBefore = 0);
144 /// create() - Construct a binary instruction, given the opcode and the two
145 /// operands. Also automatically insert this instruction to the end of the
146 /// BasicBlock specified.
148 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
149 const std::string &Name,
150 BasicBlock *InsertAtEnd);
152 /// create* - These methods just forward to create, and are useful when you
153 /// statically know what type of instruction you're going to create. These
154 /// helpers just save some typing.
155 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
156 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
157 const std::string &Name = "") {\
158 return create(Instruction::OPC, V1, V2, Name);\
160 #include "llvm/Instruction.def"
161 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
162 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
163 const std::string &Name, BasicBlock *BB) {\
164 return create(Instruction::OPC, V1, V2, Name, BB);\
166 #include "llvm/Instruction.def"
167 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
168 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
169 const std::string &Name, Instruction *I) {\
170 return create(Instruction::OPC, V1, V2, Name, I);\
172 #include "llvm/Instruction.def"
175 /// Helper functions to construct and inspect unary operations (NEG and NOT)
176 /// via binary operators SUB and XOR:
178 /// createNeg, createNot - Create the NEG and NOT
179 /// instructions out of SUB and XOR instructions.
181 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
182 Instruction *InsertBefore = 0);
183 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
184 BasicBlock *InsertAtEnd);
185 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
186 Instruction *InsertBefore = 0);
187 static BinaryOperator *createNot(Value *Op, const std::string &Name,
188 BasicBlock *InsertAtEnd);
190 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
192 static bool isNeg(const Value *V);
193 static bool isNot(const Value *V);
195 /// getNegArgument, getNotArgument - Helper functions to extract the
196 /// unary argument of a NEG or NOT operation implemented via Sub or Xor.
198 static const Value *getNegArgument(const Value *BinOp);
199 static Value *getNegArgument( Value *BinOp);
200 static const Value *getNotArgument(const Value *BinOp);
201 static Value *getNotArgument( Value *BinOp);
203 BinaryOps getOpcode() const {
204 return static_cast<BinaryOps>(Instruction::getOpcode());
207 virtual BinaryOperator *clone() const;
209 /// swapOperands - Exchange the two operands to this instruction.
210 /// This instruction is safe to use on any binary instruction and
211 /// does not modify the semantics of the instruction. If the
212 /// instruction is order dependent (SetLT f.e.) the opcode is
213 /// changed. If the instruction cannot be reversed (ie, it's a Div),
214 /// then return true.
218 // Methods for support type inquiry through isa, cast, and dyn_cast:
219 static inline bool classof(const BinaryOperator *) { return true; }
220 static inline bool classof(const Instruction *I) {
221 return I->getOpcode() >= BinaryOpsBegin && I->getOpcode() < BinaryOpsEnd;
223 static inline bool classof(const Value *V) {
224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
228 //===----------------------------------------------------------------------===//
230 //===----------------------------------------------------------------------===//
232 /// CastInst - This is the base class for all instructions that perform data
233 /// casts. It is simply provided so that instruction category testing
234 /// can be performed with code like:
236 /// if (isa<CastInst>(Instr)) { ... }
237 /// @brief Base class of casting instructions.
238 class CastInst : public UnaryInstruction {
239 /// @brief Copy constructor
240 CastInst(const CastInst &CI)
241 : UnaryInstruction(CI.getType(), CI.getOpcode(), CI.getOperand(0)) {
243 /// @brief Do not allow default construction
246 /// @brief Constructor with insert-before-instruction semantics for subclasses
247 CastInst(const Type *Ty, unsigned iType, Value *S,
248 const std::string &Name = "", Instruction *InsertBefore = 0)
249 : UnaryInstruction(Ty, iType, S, InsertBefore) {
252 /// @brief Constructor with insert-at-end-of-block semantics for subclasses
253 CastInst(const Type *Ty, unsigned iType, Value *S,
254 const std::string &Name, BasicBlock *InsertAtEnd)
255 : UnaryInstruction(Ty, iType, S, InsertAtEnd) {
259 /// Provides a way to construct any of the CastInst subclasses using an
260 /// opcode instead of the subclass's constructor. The opcode must be in the
261 /// CastOps category (Instruction::isCast(opcode) returns true). This
262 /// constructor has insert-before-instruction semantics to automatically
263 /// insert the new CastInst before InsertBefore (if it is non-null).
264 /// @brief Construct any of the CastInst subclasses
265 static CastInst *create(
266 Instruction::CastOps, ///< The opcode of the cast instruction
267 Value *S, ///< The value to be casted (operand 0)
268 const Type *Ty, ///< The type to which cast should be made
269 const std::string &Name = "", ///< Name for the instruction
270 Instruction *InsertBefore = 0 ///< Place to insert the instruction
272 /// Provides a way to construct any of the CastInst subclasses using an
273 /// opcode instead of the subclass's constructor. The opcode must be in the
274 /// CastOps category. This constructor has insert-at-end-of-block semantics
275 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
277 /// @brief Construct any of the CastInst subclasses
278 static CastInst *create(
279 Instruction::CastOps, ///< The opcode for the cast instruction
280 Value *S, ///< The value to be casted (operand 0)
281 const Type *Ty, ///< The type to which operand is casted
282 const std::string &Name, ///< The name for the instruction
283 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
286 /// @brief Create a ZExt or BitCast cast instruction
287 static CastInst *createZExtOrBitCast(
288 Value *S, ///< The value to be casted (operand 0)
289 const Type *Ty, ///< The type to which cast should be made
290 const std::string &Name = "", ///< Name for the instruction
291 Instruction *InsertBefore = 0 ///< Place to insert the instruction
294 /// @brief Create a ZExt or BitCast cast instruction
295 static CastInst *createZExtOrBitCast(
296 Value *S, ///< The value to be casted (operand 0)
297 const Type *Ty, ///< The type to which operand is casted
298 const std::string &Name, ///< The name for the instruction
299 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
302 /// @brief Create a SExt or BitCast cast instruction
303 static CastInst *createSExtOrBitCast(
304 Value *S, ///< The value to be casted (operand 0)
305 const Type *Ty, ///< The type to which cast should be made
306 const std::string &Name = "", ///< Name for the instruction
307 Instruction *InsertBefore = 0 ///< Place to insert the instruction
310 /// @brief Create a BitCast or a PtrToInt cast instruction
311 static CastInst *createPointerCast(
312 Value *S, ///< The pointer value to be casted (operand 0)
313 const Type *Ty, ///< The type to which operand is casted
314 const std::string &Name, ///< The name for the instruction
315 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
318 /// @brief Create a BitCast or a PtrToInt cast instruction
319 static CastInst *createPointerCast(
320 Value *S, ///< The pointer value to be casted (operand 0)
321 const Type *Ty, ///< The type to which cast should be made
322 const std::string &Name = "", ///< Name for the instruction
323 Instruction *InsertBefore = 0 ///< Place to insert the instruction
326 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
327 static CastInst *createIntegerCast(
328 Value *S, ///< The pointer value to be casted (operand 0)
329 const Type *Ty, ///< The type to which cast should be made
330 bool isSigned, ///< Whether to regard S as signed or not
331 const std::string &Name = "", ///< Name for the instruction
332 Instruction *InsertBefore = 0 ///< Place to insert the instruction
335 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
336 static CastInst *createIntegerCast(
337 Value *S, ///< The integer value to be casted (operand 0)
338 const Type *Ty, ///< The integer type to which operand is casted
339 bool isSigned, ///< Whether to regard S as signed or not
340 const std::string &Name, ///< The name for the instruction
341 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
344 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
345 static CastInst *createFPCast(
346 Value *S, ///< The floating point value to be casted
347 const Type *Ty, ///< The floating point type to cast to
348 const std::string &Name = "", ///< Name for the instruction
349 Instruction *InsertBefore = 0 ///< Place to insert the instruction
352 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
353 static CastInst *createFPCast(
354 Value *S, ///< The floating point value to be casted
355 const Type *Ty, ///< The floating point type to cast to
356 const std::string &Name, ///< The name for the instruction
357 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
360 /// @brief Create a SExt or BitCast cast instruction
361 static CastInst *createSExtOrBitCast(
362 Value *S, ///< The value to be casted (operand 0)
363 const Type *Ty, ///< The type to which operand is casted
364 const std::string &Name, ///< The name for the instruction
365 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
368 /// @brief Create a Trunc or BitCast cast instruction
369 static CastInst *createTruncOrBitCast(
370 Value *S, ///< The value to be casted (operand 0)
371 const Type *Ty, ///< The type to which cast should be made
372 const std::string &Name = "", ///< Name for the instruction
373 Instruction *InsertBefore = 0 ///< Place to insert the instruction
376 /// @brief Create a Trunc or BitCast cast instruction
377 static CastInst *createTruncOrBitCast(
378 Value *S, ///< The value to be casted (operand 0)
379 const Type *Ty, ///< The type to which operand is casted
380 const std::string &Name, ///< The name for the instruction
381 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
384 /// Returns the opcode necessary to cast Val into Ty using usual casting
386 /// @brief Infer the opcode for cast operand and type
387 static Instruction::CastOps getCastOpcode(
388 const Value *Val, ///< The value to cast
389 bool SrcIsSigned, ///< Whether to treat the source as signed
390 const Type *Ty, ///< The Type to which the value should be casted
391 bool DstIsSigned ///< Whether to treate the dest. as signed
394 /// There are several places where we need to know if a cast instruction
395 /// only deals with integer source and destination types. To simplify that
396 /// logic, this method is provided.
397 /// @returns true iff the cast has only integral typed operand and dest type.
398 /// @brief Determine if this is an integer-only cast.
399 bool isIntegerCast() const;
401 /// A lossless cast is one that does not alter the basic value. It implies
402 /// a no-op cast but is more stringent, preventing things like int->float,
403 /// long->double, int->ptr, or vector->anything.
404 /// @returns true iff the cast is lossless.
405 /// @brief Determine if this is a lossless cast.
406 bool isLosslessCast() const;
408 /// A no-op cast is one that can be effected without changing any bits.
409 /// It implies that the source and destination types are the same size. The
410 /// IntPtrTy argument is used to make accurate determinations for casts
411 /// involving Integer and Pointer types. They are no-op casts if the integer
412 /// is the same size as the pointer. However, pointer size varies with
413 /// platform. Generally, the result of TargetData::getIntPtrType() should be
414 /// passed in. If that's not available, use Type::Int64Ty, which will make
415 /// the isNoopCast call conservative.
416 /// @brief Determine if this cast is a no-op cast.
418 const Type *IntPtrTy ///< Integer type corresponding to pointer
421 /// Determine how a pair of casts can be eliminated, if they can be at all.
422 /// This is a helper function for both CastInst and ConstantExpr.
423 /// @returns 0 if the CastInst pair can't be eliminated
424 /// @returns Instruction::CastOps value for a cast that can replace
425 /// the pair, casting SrcTy to DstTy.
426 /// @brief Determine if a cast pair is eliminable
427 static unsigned isEliminableCastPair(
428 Instruction::CastOps firstOpcode, ///< Opcode of first cast
429 Instruction::CastOps secondOpcode, ///< Opcode of second cast
430 const Type *SrcTy, ///< SrcTy of 1st cast
431 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
432 const Type *DstTy, ///< DstTy of 2nd cast
433 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
436 /// @brief Return the opcode of this CastInst
437 Instruction::CastOps getOpcode() const {
438 return Instruction::CastOps(Instruction::getOpcode());
441 /// @brief Return the source type, as a convenience
442 const Type* getSrcTy() const { return getOperand(0)->getType(); }
443 /// @brief Return the destination type, as a convenience
444 const Type* getDestTy() const { return getType(); }
446 /// This method can be used to determine if a cast from S to DstTy using
447 /// Opcode op is valid or not.
448 /// @returns true iff the proposed cast is valid.
449 /// @brief Determine if a cast is valid without creating one.
450 static bool castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy);
452 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
453 static inline bool classof(const CastInst *) { return true; }
454 static inline bool classof(const Instruction *I) {
455 return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
457 static inline bool classof(const Value *V) {
458 return isa<Instruction>(V) && classof(cast<Instruction>(V));
462 //===----------------------------------------------------------------------===//
464 //===----------------------------------------------------------------------===//
466 /// This class is the base class for the comparison instructions.
467 /// @brief Abstract base class of comparison instructions.
468 class CmpInst: public Instruction {
469 CmpInst(); // do not implement
471 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
472 const std::string &Name = "", Instruction *InsertBefore = 0);
474 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
475 const std::string &Name, BasicBlock *InsertAtEnd);
477 Use Ops[2]; // CmpInst instructions always have 2 operands, optimize
480 /// Construct a compare instruction, given the opcode, the predicate and
481 /// the two operands. Optionally (if InstBefore is specified) insert the
482 /// instruction into a BasicBlock right before the specified instruction.
483 /// The specified Instruction is allowed to be a dereferenced end iterator.
484 /// @brief Create a CmpInst
485 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
486 Value *S2, const std::string &Name = "",
487 Instruction *InsertBefore = 0);
489 /// Construct a compare instruction, given the opcode, the predicate and the
490 /// two operands. Also automatically insert this instruction to the end of
491 /// the BasicBlock specified.
492 /// @brief Create a CmpInst
493 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
494 Value *S2, const std::string &Name,
495 BasicBlock *InsertAtEnd);
497 /// @brief Get the opcode casted to the right type
498 OtherOps getOpcode() const {
499 return static_cast<OtherOps>(Instruction::getOpcode());
502 /// The predicate for CmpInst is defined by the subclasses but stored in
503 /// the SubclassData field (see Value.h). We allow it to be fetched here
504 /// as the predicate but there is no enum type for it, just the raw unsigned
505 /// short. This facilitates comparison of CmpInst instances without delving
506 /// into the subclasses since predicate values are distinct between the
507 /// CmpInst subclasses.
508 /// @brief Return the predicate for this instruction.
509 unsigned short getPredicate() const {
513 /// @brief Provide more efficient getOperand methods.
514 Value *getOperand(unsigned i) const {
515 assert(i < 2 && "getOperand() out of range!");
518 void setOperand(unsigned i, Value *Val) {
519 assert(i < 2 && "setOperand() out of range!");
523 /// @brief CmpInst instructions always have 2 operands.
524 unsigned getNumOperands() const { return 2; }
526 /// This is just a convenience that dispatches to the subclasses.
527 /// @brief Swap the operands and adjust predicate accordingly to retain
528 /// the same comparison.
531 /// This is just a convenience that dispatches to the subclasses.
532 /// @brief Determine if this CmpInst is commutative.
533 bool isCommutative();
535 /// This is just a convenience that dispatches to the subclasses.
536 /// @brief Determine if this is an equals/not equals predicate.
539 /// @returns true if the predicate is unsigned, false otherwise.
540 /// @brief Determine if the predicate is an unsigned operation.
541 static bool isUnsigned(unsigned short predicate);
543 /// @returns true if the predicate is signed, false otherwise.
544 /// @brief Determine if the predicate is an signed operation.
545 static bool isSigned(unsigned short predicate);
547 /// @brief Determine if the predicate is an ordered operation.
548 static bool isOrdered(unsigned short predicate);
550 /// @brief Determine if the predicate is an unordered operation.
551 static bool isUnordered(unsigned short predicate);
553 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
554 static inline bool classof(const CmpInst *) { return true; }
555 static inline bool classof(const Instruction *I) {
556 return I->getOpcode() == Instruction::ICmp ||
557 I->getOpcode() == Instruction::FCmp;
559 static inline bool classof(const Value *V) {
560 return isa<Instruction>(V) && classof(cast<Instruction>(V));
564 } // End llvm namespace