1 //===-- llvm/Instruction.h - Instruction class definition -------*- 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 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"
19 #include "llvm/ADT/ilist_node.h"
23 struct AssemblyAnnotationWriter;
25 template<typename ValueSubClass, typename ItemParentClass>
26 class SymbolTableListTraits;
28 class Instruction : public User, public ilist_node<Instruction> {
29 void operator=(const Instruction &); // Do not implement
30 Instruction(const Instruction &); // Do not implement
34 friend class SymbolTableListTraits<Instruction, BasicBlock>;
35 void setParent(BasicBlock *P);
37 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
38 Instruction *InsertBefore = 0);
39 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
40 BasicBlock *InsertAtEnd);
42 // Out of line virtual method, so the vtable, etc has a home.
45 /// mayWriteToMemory - Return true if this instruction may modify memory.
47 bool mayWriteToMemory() const;
49 /// mayReadFromMemory - Return true if this instruction may read memory.
51 bool mayReadFromMemory() const;
53 /// clone() - Create a copy of 'this' instruction that is identical in all
54 /// ways except the following:
55 /// * The instruction has no parent
56 /// * The instruction has no name
58 virtual Instruction *clone() const = 0;
60 /// isIdenticalTo - Return true if the specified instruction is exactly
61 /// identical to the current one. This means that all operands match and any
62 /// extra information (e.g. load is volatile) agree.
63 bool isIdenticalTo(Instruction *I) const;
65 /// This function determines if the specified instruction executes the same
66 /// operation as the current one. This means that the opcodes, type, operand
67 /// types and any other factors affecting the operation must be the same. This
68 /// is similar to isIdenticalTo except the operands themselves don't have to
70 /// @returns true if the specified instruction is the same operation as
72 /// @brief Determine if one instruction is the same operation as another.
73 bool isSameOperationAs(Instruction *I) const;
75 /// isUsedOutsideOfBlock - Return true if there are any uses of this
76 /// instruction in blocks other than the specified block. Note that PHI nodes
77 /// are considered to evaluate their operands in the corresponding predecessor
79 bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
82 /// use_back - Specialize the methods defined in Value, as we know that an
83 /// instruction can only be used by other instructions.
84 Instruction *use_back() { return cast<Instruction>(*use_begin());}
85 const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
87 // Accessor methods...
89 inline const BasicBlock *getParent() const { return Parent; }
90 inline BasicBlock *getParent() { return Parent; }
92 /// removeFromParent - This method unlinks 'this' from the containing basic
93 /// block, but does not delete it.
95 void removeFromParent();
97 /// eraseFromParent - This method unlinks 'this' from the containing basic
98 /// block and deletes it.
100 void eraseFromParent();
102 /// insertBefore - Insert an unlinked instructions into a basic block
103 /// immediately before the specified instruction.
104 void insertBefore(Instruction *InsertPos);
106 /// moveBefore - Unlink this instruction from its current basic block and
107 /// insert it into the basic block that MovePos lives in, right before
109 void moveBefore(Instruction *MovePos);
111 // ---------------------------------------------------------------------------
112 /// Subclass classification... getOpcode() returns a member of
113 /// one of the enums that is coming soon (down below)...
115 unsigned getOpcode() const { return getValueID() - InstructionVal; }
116 const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
117 bool isTerminator() const { return isTerminator(getOpcode()); }
118 bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
119 bool isShift() { return isShift(getOpcode()); }
120 bool isCast() const { return isCast(getOpcode()); }
124 static const char* getOpcodeName(unsigned OpCode);
126 static inline bool isTerminator(unsigned OpCode) {
127 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
130 static inline bool isBinaryOp(unsigned Opcode) {
131 return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
134 /// @brief Determine if the Opcode is one of the shift instructions.
135 static inline bool isShift(unsigned Opcode) {
136 return Opcode >= Shl && Opcode <= AShr;
139 /// isLogicalShift - Return true if this is a logical shift left or a logical
141 inline bool isLogicalShift() const {
142 return getOpcode() == Shl || getOpcode() == LShr;
145 /// isLogicalShift - Return true if this is a logical shift left or a logical
147 inline bool isArithmeticShift() const {
148 return getOpcode() == AShr;
151 /// @brief Determine if the OpCode is one of the CastInst instructions.
152 static inline bool isCast(unsigned OpCode) {
153 return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
156 /// isAssociative - Return true if the instruction is associative:
158 /// Associative operators satisfy: x op (y op z) === (x op y) op z
160 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
161 /// not applied to floating point types.
163 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
164 static bool isAssociative(unsigned op, const Type *Ty);
166 /// isCommutative - Return true if the instruction is commutative:
168 /// Commutative operators satisfy: (x op y) === (y op x)
170 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
171 /// applied to any type.
173 bool isCommutative() const { return isCommutative(getOpcode()); }
174 static bool isCommutative(unsigned op);
176 /// isTrappingInstruction - Return true if the instruction may trap.
178 bool isTrapping() const {
179 return isTrapping(getOpcode());
181 static bool isTrapping(unsigned op);
183 virtual void print(std::ostream &OS) const { print(OS, 0); }
184 void print(std::ostream *OS) const { if (OS) print(*OS); }
185 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
187 /// Methods for support type inquiry through isa, cast, and dyn_cast:
188 static inline bool classof(const Instruction *) { return true; }
189 static inline bool classof(const Value *V) {
190 return V->getValueID() >= Value::InstructionVal;
193 //----------------------------------------------------------------------
194 // Exported enumerations...
196 enum TermOps { // These terminate basic blocks
197 #define FIRST_TERM_INST(N) TermOpsBegin = N,
198 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
199 #define LAST_TERM_INST(N) TermOpsEnd = N+1
200 #include "llvm/Instruction.def"
204 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
205 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
206 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
207 #include "llvm/Instruction.def"
211 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
212 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
213 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
214 #include "llvm/Instruction.def"
218 #define FIRST_CAST_INST(N) CastOpsBegin = N,
219 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
220 #define LAST_CAST_INST(N) CastOpsEnd = N+1
221 #include "llvm/Instruction.def"
225 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
226 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
227 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
228 #include "llvm/Instruction.def"
232 } // End llvm namespace