1 //===-- llvm/Instruction.h - Instruction class definition -------*- 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 contains the declaration of the Instruction class, which is the
11 // base class for all of the LLVM instructions.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_INSTRUCTION_H
16 #define LLVM_INSTRUCTION_H
18 #include "llvm/User.h"
22 struct AssemblyAnnotationWriter;
25 template<typename SC> struct ilist_traits;
26 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
27 typename SubClass> class SymbolTableListTraits;
29 class Instruction : public User {
30 void operator=(const Instruction &); // Do not implement
31 Instruction(const Instruction &); // Do not implement
34 Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
36 void setNext(Instruction *N) { Next = N; }
37 void setPrev(Instruction *N) { Prev = N; }
39 friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
40 ilist_traits<Instruction> >;
41 void setParent(BasicBlock *P);
44 // FIXME: This is a dirty hack. Setcc instructions shouldn't encode the CC
45 // into the opcode field. When they don't, this will be unneeded.
46 void setOpcode(unsigned NewOpcode);
47 friend class BinaryOperator;
49 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
50 const std::string &Name = "",
51 Instruction *InsertBefore = 0);
52 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
53 const std::string &Name, BasicBlock *InsertAtEnd);
55 // Out of line virtual method, so the vtable, etc has a home.
58 /// mayWriteToMemory - Return true if this instruction may modify memory.
60 virtual bool mayWriteToMemory() const { return false; }
62 /// clone() - Create a copy of 'this' instruction that is identical in all
63 /// ways except the following:
64 /// * The instruction has no parent
65 /// * The instruction has no name
67 virtual Instruction *clone() const = 0;
69 /// isIdenticalTo - Return true if the specified instruction is exactly
70 /// identical to the current one. This means that all operands match and any
71 /// extra information (e.g. load is volatile) agree.
72 bool isIdenticalTo(Instruction *I) const;
74 /// This function determines if the specified instruction executes the same
75 /// operation as the current one. This means that the opcodes, type, operand
76 /// types and any other factors affecting the operation must be the same. This
77 /// is similar to isIdenticalTo except the operands themselves don't have to
79 /// @returns true if the specified instruction is the same operation as
81 /// @brief Determine if one instruction is the same operation as another.
82 bool isSameOperationAs(Instruction *I) const;
84 /// use_back - Specialize the methods defined in Value, as we know that an
85 /// instruction can only be used by other instructions.
86 Instruction *use_back() { return cast<Instruction>(*use_begin());}
87 const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
89 // Accessor methods...
91 inline const BasicBlock *getParent() const { return Parent; }
92 inline BasicBlock *getParent() { return Parent; }
94 // getNext/Prev - Return the next or previous instruction in the list. The
95 // last node in the list is a terminator instruction.
96 Instruction *getNext() { return Next; }
97 const Instruction *getNext() const { return Next; }
98 Instruction *getPrev() { return Prev; }
99 const Instruction *getPrev() const { return Prev; }
101 /// removeFromParent - This method unlinks 'this' from the containing basic
102 /// block, but does not delete it.
104 void removeFromParent();
106 /// eraseFromParent - This method unlinks 'this' from the containing basic
107 /// block and deletes it.
109 void eraseFromParent();
111 /// moveBefore - Unlink this instruction from its current basic block and
112 /// insert it into the basic block that MovePos lives in, right before
114 void moveBefore(Instruction *MovePos);
116 // ---------------------------------------------------------------------------
117 /// Subclass classification... getOpcode() returns a member of
118 /// one of the enums that is coming soon (down below)...
120 unsigned getOpcode() const { return getValueType() - InstructionVal; }
121 const char *getOpcodeName() const {
122 return getOpcodeName(getOpcode());
124 static const char* getOpcodeName(unsigned OpCode);
126 static inline bool isTerminator(unsigned OpCode) {
127 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
130 inline bool isTerminator() const { // Instance of TerminatorInst?
131 return isTerminator(getOpcode());
134 inline bool isBinaryOp() const {
135 return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd;
138 /// @brief Determine if the OpCode is one of the CastInst instructions.
139 static inline bool isCast(unsigned OpCode) {
140 return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
143 /// @brief Determine if this is one of the CastInst instructions.
144 inline bool isCast() const {
145 return isCast(getOpcode());
148 /// isAssociative - Return true if the instruction is associative:
150 /// Associative operators satisfy: x op (y op z) === (x op y) op z
152 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
153 /// not applied to floating point types.
155 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
156 static bool isAssociative(unsigned op, const Type *Ty);
158 /// isCommutative - Return true if the instruction is commutative:
160 /// Commutative operators satisfy: (x op y) === (y op x)
162 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
163 /// applied to any type.
165 bool isCommutative() const { return isCommutative(getOpcode()); }
166 static bool isCommutative(unsigned op);
168 /// isTrappingInstruction - Return true if the instruction may trap.
170 bool isTrapping() const {
171 return isTrapping(getOpcode());
173 static bool isTrapping(unsigned op);
175 virtual void print(std::ostream &OS) const { print(OS, 0); }
176 void print(std::ostream *OS) const { if (OS) print(*OS); }
177 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
179 /// Methods for support type inquiry through isa, cast, and dyn_cast:
180 static inline bool classof(const Instruction *) { return true; }
181 static inline bool classof(const Value *V) {
182 return V->getValueType() >= Value::InstructionVal;
185 //----------------------------------------------------------------------
186 // Exported enumerations...
188 enum TermOps { // These terminate basic blocks
189 #define FIRST_TERM_INST(N) TermOpsBegin = N,
190 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
191 #define LAST_TERM_INST(N) TermOpsEnd = N+1
192 #include "llvm/Instruction.def"
196 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
197 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
198 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
199 #include "llvm/Instruction.def"
203 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
204 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
205 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
206 #include "llvm/Instruction.def"
210 #define FIRST_CAST_INST(N) CastOpsBegin = N,
211 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
212 #define LAST_CAST_INST(N) CastOpsEnd = N+1
213 #include "llvm/Instruction.def"
217 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
218 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
219 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
220 #include "llvm/Instruction.def"
224 } // End llvm namespace