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"
20 #include "llvm/Support/DebugLoc.h"
27 template<typename ValueSubClass
, typename ItemParentClass
>
28 class SymbolTableListTraits
;
30 class Instruction
: public User
, public ilist_node
<Instruction
> {
31 void operator=(const Instruction
&) LLVM_DELETED_FUNCTION
;
32 Instruction(const Instruction
&) LLVM_DELETED_FUNCTION
;
35 DebugLoc DbgLoc
; // 'dbg' Metadata cache.
38 /// HasMetadataBit - This is a bit stored in the SubClassData field which
39 /// indicates whether this instruction has metadata attached to it or not.
40 HasMetadataBit
= 1 << 15
43 // Out of line virtual method, so the vtable, etc has a home.
46 /// use_back - Specialize the methods defined in Value, as we know that an
47 /// instruction can only be used by other instructions.
48 Instruction
*use_back() { return cast
<Instruction
>(*use_begin());}
49 const Instruction
*use_back() const { return cast
<Instruction
>(*use_begin());}
51 inline const BasicBlock
*getParent() const { return Parent
; }
52 inline BasicBlock
*getParent() { return Parent
; }
54 /// removeFromParent - This method unlinks 'this' from the containing basic
55 /// block, but does not delete it.
57 void removeFromParent();
59 /// eraseFromParent - This method unlinks 'this' from the containing basic
60 /// block and deletes it.
62 void eraseFromParent();
64 /// insertBefore - Insert an unlinked instructions into a basic block
65 /// immediately before the specified instruction.
66 void insertBefore(Instruction
*InsertPos
);
68 /// insertAfter - Insert an unlinked instructions into a basic block
69 /// immediately after the specified instruction.
70 void insertAfter(Instruction
*InsertPos
);
72 /// moveBefore - Unlink this instruction from its current basic block and
73 /// insert it into the basic block that MovePos lives in, right before
75 void moveBefore(Instruction
*MovePos
);
77 //===--------------------------------------------------------------------===//
78 // Subclass classification.
79 //===--------------------------------------------------------------------===//
81 /// getOpcode() returns a member of one of the enums like Instruction::Add.
82 unsigned getOpcode() const { return getValueID() - InstructionVal
; }
84 const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
85 bool isTerminator() const { return isTerminator(getOpcode()); }
86 bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
87 bool isShift() { return isShift(getOpcode()); }
88 bool isCast() const { return isCast(getOpcode()); }
90 static const char* getOpcodeName(unsigned OpCode
);
92 static inline bool isTerminator(unsigned OpCode
) {
93 return OpCode
>= TermOpsBegin
&& OpCode
< TermOpsEnd
;
96 static inline bool isBinaryOp(unsigned Opcode
) {
97 return Opcode
>= BinaryOpsBegin
&& Opcode
< BinaryOpsEnd
;
100 /// @brief Determine if the Opcode is one of the shift instructions.
101 static inline bool isShift(unsigned Opcode
) {
102 return Opcode
>= Shl
&& Opcode
<= AShr
;
105 /// isLogicalShift - Return true if this is a logical shift left or a logical
107 inline bool isLogicalShift() const {
108 return getOpcode() == Shl
|| getOpcode() == LShr
;
111 /// isArithmeticShift - Return true if this is an arithmetic shift right.
112 inline bool isArithmeticShift() const {
113 return getOpcode() == AShr
;
116 /// @brief Determine if the OpCode is one of the CastInst instructions.
117 static inline bool isCast(unsigned OpCode
) {
118 return OpCode
>= CastOpsBegin
&& OpCode
< CastOpsEnd
;
121 //===--------------------------------------------------------------------===//
122 // Metadata manipulation.
123 //===--------------------------------------------------------------------===//
125 /// hasMetadata() - Return true if this instruction has any metadata attached
127 bool hasMetadata() const {
128 return !DbgLoc
.isUnknown() || hasMetadataHashEntry();
131 /// hasMetadataOtherThanDebugLoc - Return true if this instruction has
132 /// metadata attached to it other than a debug location.
133 bool hasMetadataOtherThanDebugLoc() const {
134 return hasMetadataHashEntry();
137 /// getMetadata - Get the metadata of given kind attached to this Instruction.
138 /// If the metadata is not found then return null.
139 MDNode
*getMetadata(unsigned KindID
) const {
140 if (!hasMetadata()) return 0;
141 return getMetadataImpl(KindID
);
144 /// getMetadata - Get the metadata of given kind attached to this Instruction.
145 /// If the metadata is not found then return null.
146 MDNode
*getMetadata(StringRef Kind
) const {
147 if (!hasMetadata()) return 0;
148 return getMetadataImpl(Kind
);
151 /// getAllMetadata - Get all metadata attached to this Instruction. The first
152 /// element of each pair returned is the KindID, the second element is the
153 /// metadata value. This list is returned sorted by the KindID.
154 void getAllMetadata(SmallVectorImpl
<std::pair
<unsigned, MDNode
*> > &MDs
)const{
156 getAllMetadataImpl(MDs
);
159 /// getAllMetadataOtherThanDebugLoc - This does the same thing as
160 /// getAllMetadata, except that it filters out the debug location.
161 void getAllMetadataOtherThanDebugLoc(SmallVectorImpl
<std::pair
<unsigned,
162 MDNode
*> > &MDs
) const {
163 if (hasMetadataOtherThanDebugLoc())
164 getAllMetadataOtherThanDebugLocImpl(MDs
);
167 /// setMetadata - Set the metadata of the specified kind to the specified
168 /// node. This updates/replaces metadata if already present, or removes it if
170 void setMetadata(unsigned KindID
, MDNode
*Node
);
171 void setMetadata(StringRef Kind
, MDNode
*Node
);
173 /// setDebugLoc - Set the debug location information for this instruction.
174 void setDebugLoc(const DebugLoc
&Loc
) { DbgLoc
= Loc
; }
176 /// getDebugLoc - Return the debug location for this node as a DebugLoc.
177 const DebugLoc
&getDebugLoc() const { return DbgLoc
; }
180 /// hasMetadataHashEntry - Return true if we have an entry in the on-the-side
182 bool hasMetadataHashEntry() const {
183 return (getSubclassDataFromValue() & HasMetadataBit
) != 0;
186 // These are all implemented in Metadata.cpp.
187 MDNode
*getMetadataImpl(unsigned KindID
) const;
188 MDNode
*getMetadataImpl(StringRef Kind
) const;
189 void getAllMetadataImpl(SmallVectorImpl
<std::pair
<unsigned,MDNode
*> > &)const;
190 void getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl
<std::pair
<unsigned,
192 void clearMetadataHashEntries();
194 //===--------------------------------------------------------------------===//
195 // Predicates and helper methods.
196 //===--------------------------------------------------------------------===//
199 /// isAssociative - Return true if the instruction is associative:
201 /// Associative operators satisfy: x op (y op z) === (x op y) op z
203 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
205 bool isAssociative() const { return isAssociative(getOpcode()); }
206 static bool isAssociative(unsigned op
);
208 /// isCommutative - Return true if the instruction is commutative:
210 /// Commutative operators satisfy: (x op y) === (y op x)
212 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
213 /// applied to any type.
215 bool isCommutative() const { return isCommutative(getOpcode()); }
216 static bool isCommutative(unsigned op
);
218 /// isIdempotent - Return true if the instruction is idempotent:
220 /// Idempotent operators satisfy: x op x === x
222 /// In LLVM, the And and Or operators are idempotent.
224 bool isIdempotent() const { return isIdempotent(getOpcode()); }
225 static bool isIdempotent(unsigned op
);
227 /// isNilpotent - Return true if the instruction is nilpotent:
229 /// Nilpotent operators satisfy: x op x === Id,
231 /// where Id is the identity for the operator, i.e. a constant such that
232 /// x op Id === x and Id op x === x for all x.
234 /// In LLVM, the Xor operator is nilpotent.
236 bool isNilpotent() const { return isNilpotent(getOpcode()); }
237 static bool isNilpotent(unsigned op
);
239 /// mayWriteToMemory - Return true if this instruction may modify memory.
241 bool mayWriteToMemory() const;
243 /// mayReadFromMemory - Return true if this instruction may read memory.
245 bool mayReadFromMemory() const;
247 /// mayReadOrWriteMemory - Return true if this instruction may read or
250 bool mayReadOrWriteMemory() const {
251 return mayReadFromMemory() || mayWriteToMemory();
254 /// mayThrow - Return true if this instruction may throw an exception.
256 bool mayThrow() const;
258 /// mayHaveSideEffects - Return true if the instruction may have side effects.
260 /// Note that this does not consider malloc and alloca to have side
261 /// effects because the newly allocated memory is completely invisible to
262 /// instructions which don't used the returned value. For cases where this
263 /// matters, isSafeToSpeculativelyExecute may be more appropriate.
264 bool mayHaveSideEffects() const {
265 return mayWriteToMemory() || mayThrow();
268 /// clone() - Create a copy of 'this' instruction that is identical in all
269 /// ways except the following:
270 /// * The instruction has no parent
271 /// * The instruction has no name
273 Instruction
*clone() const;
275 /// isIdenticalTo - Return true if the specified instruction is exactly
276 /// identical to the current one. This means that all operands match and any
277 /// extra information (e.g. load is volatile) agree.
278 bool isIdenticalTo(const Instruction
*I
) const;
280 /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
281 /// ignores the SubclassOptionalData flags, which specify conditions
282 /// under which the instruction's result is undefined.
283 bool isIdenticalToWhenDefined(const Instruction
*I
) const;
285 /// When checking for operation equivalence (using isSameOperationAs) it is
286 /// sometimes useful to ignore certain attributes.
287 enum OperationEquivalenceFlags
{
288 /// Check for equivalence ignoring load/store alignment.
289 CompareIgnoringAlignment
= 1<<0,
290 /// Check for equivalence treating a type and a vector of that type
292 CompareUsingScalarTypes
= 1<<1
295 /// This function determines if the specified instruction executes the same
296 /// operation as the current one. This means that the opcodes, type, operand
297 /// types and any other factors affecting the operation must be the same. This
298 /// is similar to isIdenticalTo except the operands themselves don't have to
300 /// @returns true if the specified instruction is the same operation as
302 /// @brief Determine if one instruction is the same operation as another.
303 bool isSameOperationAs(const Instruction
*I
, unsigned flags
= 0) const;
305 /// isUsedOutsideOfBlock - Return true if there are any uses of this
306 /// instruction in blocks other than the specified block. Note that PHI nodes
307 /// are considered to evaluate their operands in the corresponding predecessor
309 bool isUsedOutsideOfBlock(const BasicBlock
*BB
) const;
312 /// Methods for support type inquiry through isa, cast, and dyn_cast:
313 static inline bool classof(const Instruction
*) { return true; }
314 static inline bool classof(const Value
*V
) {
315 return V
->getValueID() >= Value::InstructionVal
;
318 //----------------------------------------------------------------------
319 // Exported enumerations.
321 enum TermOps
{ // These terminate basic blocks
322 #define FIRST_TERM_INST(N) TermOpsBegin = N,
323 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
324 #define LAST_TERM_INST(N) TermOpsEnd = N+1
325 #include "llvm/Instruction.def"
329 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
330 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
331 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
332 #include "llvm/Instruction.def"
336 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
337 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
338 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
339 #include "llvm/Instruction.def"
343 #define FIRST_CAST_INST(N) CastOpsBegin = N,
344 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
345 #define LAST_CAST_INST(N) CastOpsEnd = N+1
346 #include "llvm/Instruction.def"
350 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
351 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
352 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
353 #include "llvm/Instruction.def"
356 // Shadow Value::setValueSubclassData with a private forwarding method so that
357 // subclasses cannot accidentally use it.
358 void setValueSubclassData(unsigned short D
) {
359 Value::setValueSubclassData(D
);
361 unsigned short getSubclassDataFromValue() const {
362 return Value::getSubclassDataFromValue();
365 void setHasMetadataHashEntry(bool V
) {
366 setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit
) |
367 (V
? HasMetadataBit
: 0));
370 friend class SymbolTableListTraits
<Instruction
, BasicBlock
>;
371 void setParent(BasicBlock
*P
);
373 // Instruction subclasses can stick up to 15 bits of stuff into the
374 // SubclassData field of instruction with these members.
376 // Verify that only the low 15 bits are used.
377 void setInstructionSubclassData(unsigned short D
) {
378 assert((D
& HasMetadataBit
) == 0 && "Out of range value put into field");
379 setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit
) | D
);
382 unsigned getSubclassDataFromInstruction() const {
383 return getSubclassDataFromValue() & ~HasMetadataBit
;
386 Instruction(Type
*Ty
, unsigned iType
, Use
*Ops
, unsigned NumOps
,
387 Instruction
*InsertBefore
= 0);
388 Instruction(Type
*Ty
, unsigned iType
, Use
*Ops
, unsigned NumOps
,
389 BasicBlock
*InsertAtEnd
);
390 virtual Instruction
*clone_impl() const = 0;
394 // Instruction* is only 4-byte aligned.
396 class PointerLikeTypeTraits
<Instruction
*> {
397 typedef Instruction
* PT
;
399 static inline void *getAsVoidPointer(PT P
) { return P
; }
400 static inline PT
getFromVoidPointer(void *P
) {
401 return static_cast<PT
>(P
);
403 enum { NumLowBitsAvailable
= 2 };
406 } // End llvm namespace