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"
26 template<typename ValueSubClass, typename ItemParentClass>
27 class SymbolTableListTraits;
29 class Instruction : public User, public ilist_node<Instruction> {
30 void operator=(const Instruction &); // Do not implement
31 Instruction(const Instruction &); // Do not implement
36 /// HasMetadataBit - This is a bit stored in the SubClassData field which
37 /// indicates whether this instruction has metadata attached to it or not.
38 HasMetadataBit = 1 << 15
41 // Out of line virtual method, so the vtable, etc has a home.
44 /// use_back - Specialize the methods defined in Value, as we know that an
45 /// instruction can only be used by other instructions.
46 Instruction *use_back() { return cast<Instruction>(*use_begin());}
47 const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
49 inline const BasicBlock *getParent() const { return Parent; }
50 inline BasicBlock *getParent() { return Parent; }
52 /// removeFromParent - This method unlinks 'this' from the containing basic
53 /// block, but does not delete it.
55 void removeFromParent();
57 /// eraseFromParent - This method unlinks 'this' from the containing basic
58 /// block and deletes it.
60 void eraseFromParent();
62 /// insertBefore - Insert an unlinked instructions into a basic block
63 /// immediately before the specified instruction.
64 void insertBefore(Instruction *InsertPos);
66 /// insertAfter - Insert an unlinked instructions into a basic block
67 /// immediately after the specified instruction.
68 void insertAfter(Instruction *InsertPos);
70 /// moveBefore - Unlink this instruction from its current basic block and
71 /// insert it into the basic block that MovePos lives in, right before
73 void moveBefore(Instruction *MovePos);
75 //===--------------------------------------------------------------------===//
76 // Subclass classification.
77 //===--------------------------------------------------------------------===//
79 /// getOpcode() returns a member of one of the enums like Instruction::Add.
80 unsigned getOpcode() const { return getValueID() - InstructionVal; }
82 const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
83 bool isTerminator() const { return isTerminator(getOpcode()); }
84 bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
85 bool isShift() { return isShift(getOpcode()); }
86 bool isCast() const { return isCast(getOpcode()); }
88 static const char* getOpcodeName(unsigned OpCode);
90 static inline bool isTerminator(unsigned OpCode) {
91 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
94 static inline bool isBinaryOp(unsigned Opcode) {
95 return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
98 /// @brief Determine if the Opcode is one of the shift instructions.
99 static inline bool isShift(unsigned Opcode) {
100 return Opcode >= Shl && Opcode <= AShr;
103 /// isLogicalShift - Return true if this is a logical shift left or a logical
105 inline bool isLogicalShift() const {
106 return getOpcode() == Shl || getOpcode() == LShr;
109 /// isArithmeticShift - Return true if this is an arithmetic shift right.
110 inline bool isArithmeticShift() const {
111 return getOpcode() == AShr;
114 /// @brief Determine if the OpCode is one of the CastInst instructions.
115 static inline bool isCast(unsigned OpCode) {
116 return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
119 //===--------------------------------------------------------------------===//
120 // Metadata manipulation.
121 //===--------------------------------------------------------------------===//
123 /// hasMetadata() - Return true if this instruction has any metadata attached
125 bool hasMetadata() const {
126 return (getSubclassDataFromValue() & HasMetadataBit) != 0;
129 /// getMetadata - Get the metadata of given kind attached to this Instruction.
130 /// If the metadata is not found then return null.
131 MDNode *getMetadata(unsigned KindID) const {
132 if (!hasMetadata()) return 0;
133 return getMetadataImpl(KindID);
136 /// getMetadata - Get the metadata of given kind attached to this Instruction.
137 /// If the metadata is not found then return null.
138 MDNode *getMetadata(const char *Kind) const {
139 if (!hasMetadata()) return 0;
140 return getMetadataImpl(Kind);
143 /// getAllMetadata - Get all metadata attached to this Instruction. The first
144 /// element of each pair returned is the KindID, the second element is the
145 /// metadata value. This list is returned sorted by the KindID.
146 void getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs)const{
148 getAllMetadataImpl(MDs);
151 /// setMetadata - Set the metadata of the specified kind to the specified
152 /// node. This updates/replaces metadata if already present, or removes it if
154 void setMetadata(unsigned KindID, MDNode *Node);
155 void setMetadata(const char *Kind, MDNode *Node);
158 // These are all implemented in Metadata.cpp.
159 MDNode *getMetadataImpl(unsigned KindID) const;
160 MDNode *getMetadataImpl(const char *Kind) const;
161 void getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,MDNode*> > &)const;
162 void removeAllMetadata();
164 //===--------------------------------------------------------------------===//
165 // Predicates and helper methods.
166 //===--------------------------------------------------------------------===//
169 /// isAssociative - Return true if the instruction is associative:
171 /// Associative operators satisfy: x op (y op z) === (x op y) op z
173 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
174 /// not applied to floating point types.
176 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
177 static bool isAssociative(unsigned op, const Type *Ty);
179 /// isCommutative - Return true if the instruction is commutative:
181 /// Commutative operators satisfy: (x op y) === (y op x)
183 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
184 /// applied to any type.
186 bool isCommutative() const { return isCommutative(getOpcode()); }
187 static bool isCommutative(unsigned op);
189 /// mayWriteToMemory - Return true if this instruction may modify memory.
191 bool mayWriteToMemory() const;
193 /// mayReadFromMemory - Return true if this instruction may read memory.
195 bool mayReadFromMemory() const;
197 /// mayThrow - Return true if this instruction may throw an exception.
199 bool mayThrow() const;
201 /// mayHaveSideEffects - Return true if the instruction may have side effects.
203 /// Note that this does not consider malloc and alloca to have side
204 /// effects because the newly allocated memory is completely invisible to
205 /// instructions which don't used the returned value. For cases where this
206 /// matters, isSafeToSpeculativelyExecute may be more appropriate.
207 bool mayHaveSideEffects() const {
208 return mayWriteToMemory() || mayThrow();
211 /// isSafeToSpeculativelyExecute - Return true if the instruction does not
212 /// have any effects besides calculating the result and does not have
213 /// undefined behavior.
215 /// This method never returns true for an instruction that returns true for
216 /// mayHaveSideEffects; however, this method also does some other checks in
217 /// addition. It checks for undefined behavior, like dividing by zero or
218 /// loading from an invalid pointer (but not for undefined results, like a
219 /// shift with a shift amount larger than the width of the result). It checks
220 /// for malloc and alloca because speculatively executing them might cause a
221 /// memory leak. It also returns false for instructions related to control
222 /// flow, specifically terminators and PHI nodes.
224 /// This method only looks at the instruction itself and its operands, so if
225 /// this method returns true, it is safe to move the instruction as long as
226 /// the correct dominance relationships for the operands and users hold.
227 /// However, this method can return true for instructions that read memory;
228 /// for such instructions, moving them may change the resulting value.
229 bool isSafeToSpeculativelyExecute() const;
231 /// clone() - Create a copy of 'this' instruction that is identical in all
232 /// ways except the following:
233 /// * The instruction has no parent
234 /// * The instruction has no name
236 Instruction *clone() const;
238 /// isIdenticalTo - Return true if the specified instruction is exactly
239 /// identical to the current one. This means that all operands match and any
240 /// extra information (e.g. load is volatile) agree.
241 bool isIdenticalTo(const Instruction *I) const;
243 /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
244 /// ignores the SubclassOptionalData flags, which specify conditions
245 /// under which the instruction's result is undefined.
246 bool isIdenticalToWhenDefined(const Instruction *I) const;
248 /// This function determines if the specified instruction executes the same
249 /// operation as the current one. This means that the opcodes, type, operand
250 /// types and any other factors affecting the operation must be the same. This
251 /// is similar to isIdenticalTo except the operands themselves don't have to
253 /// @returns true if the specified instruction is the same operation as
255 /// @brief Determine if one instruction is the same operation as another.
256 bool isSameOperationAs(const Instruction *I) const;
258 /// isUsedOutsideOfBlock - Return true if there are any uses of this
259 /// instruction in blocks other than the specified block. Note that PHI nodes
260 /// are considered to evaluate their operands in the corresponding predecessor
262 bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
265 /// Methods for support type inquiry through isa, cast, and dyn_cast:
266 static inline bool classof(const Instruction *) { return true; }
267 static inline bool classof(const Value *V) {
268 return V->getValueID() >= Value::InstructionVal;
271 //----------------------------------------------------------------------
272 // Exported enumerations.
274 enum TermOps { // These terminate basic blocks
275 #define FIRST_TERM_INST(N) TermOpsBegin = N,
276 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
277 #define LAST_TERM_INST(N) TermOpsEnd = N+1
278 #include "llvm/Instruction.def"
282 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
283 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
284 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
285 #include "llvm/Instruction.def"
289 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
290 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
291 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
292 #include "llvm/Instruction.def"
296 #define FIRST_CAST_INST(N) CastOpsBegin = N,
297 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
298 #define LAST_CAST_INST(N) CastOpsEnd = N+1
299 #include "llvm/Instruction.def"
303 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
304 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
305 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
306 #include "llvm/Instruction.def"
309 // Shadow Value::setValueSubclassData with a private forwarding method so that
310 // subclasses cannot accidentally use it.
311 void setValueSubclassData(unsigned short D) {
312 Value::setValueSubclassData(D);
314 unsigned short getSubclassDataFromValue() const {
315 return Value::getSubclassDataFromValue();
318 void setHasMetadata(bool V) {
319 setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
320 (V ? HasMetadataBit : 0));
323 friend class SymbolTableListTraits<Instruction, BasicBlock>;
324 void setParent(BasicBlock *P);
326 // Instruction subclasses can stick up to 15 bits of stuff into the
327 // SubclassData field of instruction with these members.
329 // Verify that only the low 15 bits are used.
330 void setInstructionSubclassData(unsigned short D) {
331 assert((D & HasMetadataBit) == 0 && "Out of range value put into field");
332 setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D);
335 unsigned getSubclassDataFromInstruction() const {
336 return getSubclassDataFromValue() & ~HasMetadataBit;
339 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
340 Instruction *InsertBefore = 0);
341 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
342 BasicBlock *InsertAtEnd);
343 virtual Instruction *clone_impl() const = 0;
347 // Instruction* is only 4-byte aligned.
349 class PointerLikeTypeTraits<Instruction*> {
350 typedef Instruction* PT;
352 static inline void *getAsVoidPointer(PT P) { return P; }
353 static inline PT getFromVoidPointer(void *P) {
354 return static_cast<PT>(P);
356 enum { NumLowBitsAvailable = 2 };
359 } // End llvm namespace