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"
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 /// clone() - Create a copy of 'this' instruction that is identical in all
46 /// ways except the following:
47 /// * The instruction has no parent
48 /// * The instruction has no name
50 virtual Instruction *clone(LLVMContext &Context) const = 0;
52 /// isIdenticalTo - Return true if the specified instruction is exactly
53 /// identical to the current one. This means that all operands match and any
54 /// extra information (e.g. load is volatile) agree.
55 bool isIdenticalTo(const Instruction *I) const;
57 /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
58 /// ignores the SubclassOptionalData flags, which specify conditions
59 /// under which the instruction's result is undefined.
60 bool isIdenticalToWhenDefined(const Instruction *I) const;
62 /// This function determines if the specified instruction executes the same
63 /// operation as the current one. This means that the opcodes, type, operand
64 /// types and any other factors affecting the operation must be the same. This
65 /// is similar to isIdenticalTo except the operands themselves don't have to
67 /// @returns true if the specified instruction is the same operation as
69 /// @brief Determine if one instruction is the same operation as another.
70 bool isSameOperationAs(const Instruction *I) const;
72 /// isUsedOutsideOfBlock - Return true if there are any uses of this
73 /// instruction in blocks other than the specified block. Note that PHI nodes
74 /// are considered to evaluate their operands in the corresponding predecessor
76 bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
79 /// use_back - Specialize the methods defined in Value, as we know that an
80 /// instruction can only be used by other instructions.
81 Instruction *use_back() { return cast<Instruction>(*use_begin());}
82 const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
84 // Accessor methods...
86 inline const BasicBlock *getParent() const { return Parent; }
87 inline BasicBlock *getParent() { return Parent; }
89 /// removeFromParent - This method unlinks 'this' from the containing basic
90 /// block, but does not delete it.
92 void removeFromParent();
94 /// eraseFromParent - This method unlinks 'this' from the containing basic
95 /// block and deletes it.
97 void eraseFromParent();
99 /// insertBefore - Insert an unlinked instructions into a basic block
100 /// immediately before the specified instruction.
101 void insertBefore(Instruction *InsertPos);
103 /// insertAfter - Insert an unlinked instructions into a basic block
104 /// immediately after the specified instruction.
105 void insertAfter(Instruction *InsertPos);
107 /// moveBefore - Unlink this instruction from its current basic block and
108 /// insert it into the basic block that MovePos lives in, right before
110 void moveBefore(Instruction *MovePos);
112 // ---------------------------------------------------------------------------
113 /// Subclass classification... getOpcode() returns a member of
114 /// one of the enums that is coming soon (down below)...
116 unsigned getOpcode() const { return getValueID() - InstructionVal; }
117 const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
118 bool isTerminator() const { return isTerminator(getOpcode()); }
119 bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
120 bool isShift() { return isShift(getOpcode()); }
121 bool isCast() const { return isCast(getOpcode()); }
125 static const char* getOpcodeName(unsigned OpCode);
127 static inline bool isTerminator(unsigned OpCode) {
128 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
131 static inline bool isBinaryOp(unsigned Opcode) {
132 return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
135 /// @brief Determine if the Opcode is one of the shift instructions.
136 static inline bool isShift(unsigned Opcode) {
137 return Opcode >= Shl && Opcode <= AShr;
140 /// isLogicalShift - Return true if this is a logical shift left or a logical
142 inline bool isLogicalShift() const {
143 return getOpcode() == Shl || getOpcode() == LShr;
146 /// isArithmeticShift - Return true if this is an arithmetic shift right.
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 /// mayWriteToMemory - Return true if this instruction may modify memory.
178 bool mayWriteToMemory() const;
180 /// mayReadFromMemory - Return true if this instruction may read memory.
182 bool mayReadFromMemory() const;
184 /// mayThrow - Return true if this instruction may throw an exception.
186 bool mayThrow() const;
188 /// mayHaveSideEffects - Return true if the instruction may have side effects.
190 /// Note that this does not consider malloc and alloca to have side
191 /// effects because the newly allocated memory is completely invisible to
192 /// instructions which don't used the returned value. For cases where this
193 /// matters, isSafeToSpeculativelyExecute may be more appropriate.
194 bool mayHaveSideEffects() const {
195 return mayWriteToMemory() || mayThrow();
198 /// isSafeToSpeculativelyExecute - Return true if the instruction does not
199 /// have any effects besides calculating the result and does not have
200 /// undefined behavior.
202 /// This method never returns true for an instruction that returns true for
203 /// mayHaveSideEffects; however, this method also does some other checks in
204 /// addition. It checks for undefined behavior, like dividing by zero or
205 /// loading from an invalid pointer (but not for undefined results, like a
206 /// shift with a shift amount larger than the width of the result). It checks
207 /// for malloc and alloca because speculatively executing them might cause a
208 /// memory leak. It also returns false for instructions related to control
209 /// flow, specifically terminators and PHI nodes.
211 /// This method only looks at the instruction itself and its operands, so if
212 /// this method returns true, it is safe to move the instruction as long as
213 /// the correct dominance relationships for the operands and users hold.
214 /// However, this method can return true for instructions that read memory;
215 /// for such instructions, moving them may change the resulting value.
216 bool isSafeToSpeculativelyExecute() const;
218 /// Methods for support type inquiry through isa, cast, and dyn_cast:
219 static inline bool classof(const Instruction *) { return true; }
220 static inline bool classof(const Value *V) {
221 return V->getValueID() >= Value::InstructionVal;
224 //----------------------------------------------------------------------
225 // Exported enumerations...
227 enum TermOps { // These terminate basic blocks
228 #define FIRST_TERM_INST(N) TermOpsBegin = N,
229 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
230 #define LAST_TERM_INST(N) TermOpsEnd = N+1
231 #include "llvm/Instruction.def"
235 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
236 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
237 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
238 #include "llvm/Instruction.def"
242 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
243 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
244 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
245 #include "llvm/Instruction.def"
249 #define FIRST_CAST_INST(N) CastOpsBegin = N,
250 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
251 #define LAST_CAST_INST(N) CastOpsEnd = N+1
252 #include "llvm/Instruction.def"
256 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
257 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
258 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
259 #include "llvm/Instruction.def"
263 // Instruction* is only 4-byte aligned.
265 class PointerLikeTypeTraits<Instruction*> {
266 typedef Instruction* PT;
268 static inline void *getAsVoidPointer(PT P) { return P; }
269 static inline PT getFromVoidPointer(void *P) {
270 return static_cast<PT>(P);
272 enum { NumLowBitsAvailable = 2 };
275 } // End llvm namespace