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 std::string &Name = "", 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 std::string &Name, 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, Name, 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, Name, InsertAtEnd) {
275 /// Provides a way to construct any of the CastInst subclasses using an
276 /// opcode instead of the subclass's constructor. The opcode must be in the
277 /// CastOps category (Instruction::isCast(opcode) returns true). This
278 /// constructor has insert-before-instruction semantics to automatically
279 /// insert the new CastInst before InsertBefore (if it is non-null).
280 /// @brief Construct any of the CastInst subclasses
281 static CastInst *create(
282 Instruction::CastOps, ///< The opcode of the cast instruction
283 Value *S, ///< The value to be casted (operand 0)
284 const Type *Ty, ///< The type to which cast should be made
285 const std::string &Name = "", ///< Name for the instruction
286 Instruction *InsertBefore = 0 ///< Place to insert the instruction
288 /// Provides a way to construct any of the CastInst subclasses using an
289 /// opcode instead of the subclass's constructor. The opcode must be in the
290 /// CastOps category. This constructor has insert-at-end-of-block semantics
291 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
293 /// @brief Construct any of the CastInst subclasses
294 static CastInst *create(
295 Instruction::CastOps, ///< The opcode for the cast instruction
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 /// Returns the opcode necessary to cast Val into Ty using usual casting
304 static Instruction::CastOps getCastOpcode(
305 const Value *Val, ///< The value to cast
306 const Type *Ty ///< The Type to which the value should be casted
309 /// Joins the create method (with insert-before-instruction semantics) above
310 /// with the getCastOpcode method. getOpcode(S,Ty) is called first to
311 /// obtain the opcode for casting S to type Ty. Then the get(...) method is
312 /// called to create the CastInst and insert it. The instruction is
313 /// inserted before InsertBefore (if it is non-null). The cast created is
314 /// inferred, because only the types involved are used in determining which
315 /// cast opcode to use. For specific casts, use one of the create methods.
316 /// @brief Inline helper method to join create with getCastOpcode.
317 inline static CastInst *createInferredCast(
318 Value *S, ///< The value to be casted (operand 0)
319 const Type *Ty, ///< Type to which operand should be casted
320 const std::string &Name = "", ///< Name for the instruction
321 Instruction *InsertBefore = 0 ///< Place to insert the CastInst
323 return create(getCastOpcode(S, Ty), S, Ty, Name, InsertBefore);
326 /// Joins the get method (with insert-at-end-of-block semantics) method
327 /// above with the getCastOpcode method. getOpcode(S,Ty) is called first to
328 /// obtain the usual casting opcode for casting S to type Ty. Then the
329 /// get(...) method is called to create the CastInst and insert it. The
330 /// instruction is inserted at the end of InsertAtEnd (if it is non-null).
331 /// The created cast is inferred, because only the types involved are used
332 /// in determining which cast opcode to use. For specific casts, use one of
333 /// the create methods.
334 /// @brief Inline helper method to join create with getCastOpcode.
335 inline static CastInst *createInferredCast(
336 Value *S, ///< The value to be casted (operand 0)
337 const Type *Ty, ///< Type to which operand should be casted
338 const std::string &Name, ///< Name for the instruction
339 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
341 return create(getCastOpcode(S, Ty), S, Ty, Name, InsertAtEnd);
344 /// There are several places where we need to know if a cast instruction
345 /// only deals with integer source and destination types. To simplify that
346 /// logic, this method is provided.
347 /// @returns true iff the cast has only integral typed operand and dest type.
348 /// @brief Determine if this is an integer-only cast.
349 bool isIntegerCast() const;
351 /// A lossless cast is one that does not alter the basic value. It implies
352 /// a no-op cast but is more stringent, preventing things like int->float,
353 /// long->double, int->ptr, or packed->anything.
354 /// @returns true iff the cast is lossless.
355 /// @brief Determine if this is a lossless cast.
356 bool isLosslessCast() const;
358 /// A no-op cast is one that can be effected without changing any bits.
359 /// It implies that the source and destination types are the same size. The
360 /// IntPtrTy argument is used to make accurate determinations for casts
361 /// involving Integer and Pointer types. They are no-op casts if the integer
362 /// is the same size as the pointer. However, pointer size varies with
363 /// platform. Generally, the result of TargetData::getIntPtrType() should be
364 /// passed in. If that's not available, use Type::ULongTy, which will make
365 /// the isNoopCast call conservative.
366 /// @brief Determine if this cast is a no-op cast.
368 const Type *IntPtrTy ///< Integer type corresponding to pointer
371 /// Determine how a pair of casts can be eliminated, if they can be at all.
372 /// This is a helper function for both CastInst and ConstantExpr.
373 /// @returns 0 if the CastInst pair can't be eliminated
374 /// @returns Instruction::CastOps value for a cast that can replace
375 /// the pair, casting SrcTy to DstTy.
376 /// @brief Determine if a cast pair is eliminable
377 static unsigned isEliminableCastPair(
378 Instruction::CastOps firstOpcode, ///< Opcode of first cast
379 Instruction::CastOps secondOpcode, ///< Opcode of second cast
380 const Type *SrcTy, ///< SrcTy of 1st cast
381 const Type *MidTy, ///< DstTy of 1st cast & SrcTy of 2nd cast
382 const Type *DstTy, ///< DstTy of 2nd cast
383 const Type *IntPtrTy ///< Integer type corresponding to Ptr types
386 /// @brief Return the opcode of this CastInst
387 Instruction::CastOps getOpcode() const {
388 return Instruction::CastOps(Instruction::getOpcode());
391 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
392 static inline bool classof(const CastInst *) { return true; }
393 static inline bool classof(const Instruction *I) {
394 return I->getOpcode() >= CastOpsBegin && I->getOpcode() < CastOpsEnd;
396 static inline bool classof(const Value *V) {
397 return isa<Instruction>(V) && classof(cast<Instruction>(V));
401 //===----------------------------------------------------------------------===//
403 //===----------------------------------------------------------------------===//
405 /// This class is the base class for the comparison instructions.
406 /// @brief Abstract base class of comparison instructions.
407 class CmpInst: public Instruction {
408 CmpInst(); // do not implement
410 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
411 const std::string &Name = "", Instruction *InsertBefore = 0);
413 CmpInst(Instruction::OtherOps op, unsigned short pred, Value *LHS, Value *RHS,
414 const std::string &Name, BasicBlock *InsertAtEnd);
416 Use Ops[2]; // CmpInst instructions always have 2 operands, optimize
419 /// Construct a compare instruction, given the opcode, the predicate and
420 /// the two operands. Optionally (if InstBefore is specified) insert the
421 /// instruction into a BasicBlock right before the specified instruction.
422 /// The specified Instruction is allowed to be a dereferenced end iterator.
423 /// @brief Create a CmpInst
424 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
425 Value *S2, const std::string &Name = "",
426 Instruction *InsertBefore = 0);
428 /// Construct a compare instruction, given the opcode, the predicate and the
429 /// two operands. Also automatically insert this instruction to the end of
430 /// the BasicBlock specified.
431 /// @brief Create a CmpInst
432 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
433 Value *S2, const std::string &Name,
434 BasicBlock *InsertAtEnd);
436 /// @brief Implement superclass method.
437 virtual CmpInst *clone() const;
439 /// The predicate for CmpInst is defined by the subclasses but stored in
440 /// the SubclassData field (see Value.h). We allow it to be fetched here
441 /// as the predicate but there is no enum type for it, just the raw unsigned
442 /// short. This facilitates comparison of CmpInst instances without delving
443 /// into the subclasses since predicate values are distinct between the
444 /// CmpInst subclasses.
445 /// @brief Return the predicate for this instruction.
446 unsigned short getPredicate() const {
450 /// @brief Provide more efficient getOperand methods.
451 Value *getOperand(unsigned i) const {
452 assert(i < 2 && "getOperand() out of range!");
455 void setOperand(unsigned i, Value *Val) {
456 assert(i < 2 && "setOperand() out of range!");
460 /// @brief CmpInst instructions always have 2 operands.
461 unsigned getNumOperands() const { return 2; }
463 /// This is just a convenience that dispatches to the subclasses.
464 /// @brief Swap the operands.
467 /// This is just a convenience that dispatches to the subclasses.
468 /// @brief Determine if this CmpInst is commutative.
469 bool isCommutative();
471 /// This is just a convenience that dispatches to the subclasses.
472 /// @brief Determine if this is an equals/not equals predicate.
475 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
476 static inline bool classof(const CmpInst *) { return true; }
477 static inline bool classof(const Instruction *I) {
478 return I->getOpcode() == Instruction::ICmp ||
479 I->getOpcode() == Instruction::FCmp;
481 static inline bool classof(const Value *V) {
482 return isa<Instruction>(V) && classof(cast<Instruction>(V));
486 } // End llvm namespace