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"
22 struct AssemblyAnnotationWriter;
24 template<typename ValueSubClass, typename ItemParentClass>
25 class SymbolTableListTraits;
27 class Instruction : public User {
28 void operator=(const Instruction &); // Do not implement
29 Instruction(const Instruction &); // Do not implement
32 Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
34 void setNext(Instruction *N) { Next = N; }
35 void setPrev(Instruction *N) { Prev = N; }
37 friend class SymbolTableListTraits<Instruction, BasicBlock>;
38 void setParent(BasicBlock *P);
40 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
41 Instruction *InsertBefore = 0);
42 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
43 BasicBlock *InsertAtEnd);
45 // Out of line virtual method, so the vtable, etc has a home.
48 /// mayWriteToMemory - Return true if this instruction may modify memory.
50 bool mayWriteToMemory() const;
52 /// mayReadFromMemory - Return true if this instruction may read memory.
54 bool mayReadFromMemory() const;
56 /// clone() - Create a copy of 'this' instruction that is identical in all
57 /// ways except the following:
58 /// * The instruction has no parent
59 /// * The instruction has no name
61 virtual Instruction *clone() const = 0;
63 /// isIdenticalTo - Return true if the specified instruction is exactly
64 /// identical to the current one. This means that all operands match and any
65 /// extra information (e.g. load is volatile) agree.
66 bool isIdenticalTo(Instruction *I) const;
68 /// This function determines if the specified instruction executes the same
69 /// operation as the current one. This means that the opcodes, type, operand
70 /// types and any other factors affecting the operation must be the same. This
71 /// is similar to isIdenticalTo except the operands themselves don't have to
73 /// @returns true if the specified instruction is the same operation as
75 /// @brief Determine if one instruction is the same operation as another.
76 bool isSameOperationAs(Instruction *I) const;
78 /// isUsedOutsideOfBlock - Return true if there are any uses of this
79 /// instruction in blocks other than the specified block. Note that PHI nodes
80 /// are considered to evaluate their operands in the corresponding predecessor
82 bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
85 /// use_back - Specialize the methods defined in Value, as we know that an
86 /// instruction can only be used by other instructions.
87 Instruction *use_back() { return cast<Instruction>(*use_begin());}
88 const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
90 // Accessor methods...
92 inline const BasicBlock *getParent() const { return Parent; }
93 inline BasicBlock *getParent() { return Parent; }
95 /// removeFromParent - This method unlinks 'this' from the containing basic
96 /// block, but does not delete it.
98 void removeFromParent();
100 /// eraseFromParent - This method unlinks 'this' from the containing basic
101 /// block and deletes it.
103 void eraseFromParent();
105 /// insertBefore - Insert an unlinked instructions into a basic block
106 /// immediately before the specified instruction.
107 void insertBefore(Instruction *InsertPos);
109 /// moveBefore - Unlink this instruction from its current basic block and
110 /// insert it into the basic block that MovePos lives in, right before
112 void moveBefore(Instruction *MovePos);
114 // ---------------------------------------------------------------------------
115 /// Subclass classification... getOpcode() returns a member of
116 /// one of the enums that is coming soon (down below)...
118 unsigned getOpcode() const { return getValueID() - InstructionVal; }
119 const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
120 bool isTerminator() const { return isTerminator(getOpcode()); }
121 bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
122 bool isShift() { return isShift(getOpcode()); }
123 bool isCast() const { return isCast(getOpcode()); }
127 static const char* getOpcodeName(unsigned OpCode);
129 static inline bool isTerminator(unsigned OpCode) {
130 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
133 static inline bool isBinaryOp(unsigned Opcode) {
134 return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
137 /// @brief Determine if the Opcode is one of the shift instructions.
138 static inline bool isShift(unsigned Opcode) {
139 return Opcode >= Shl && Opcode <= AShr;
142 /// isLogicalShift - Return true if this is a logical shift left or a logical
144 inline bool isLogicalShift() const {
145 return getOpcode() == Shl || getOpcode() == LShr;
148 /// isLogicalShift - Return true if this is a logical shift left or a logical
150 inline bool isArithmeticShift() const {
151 return getOpcode() == AShr;
154 /// @brief Determine if the OpCode is one of the CastInst instructions.
155 static inline bool isCast(unsigned OpCode) {
156 return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
159 /// isAssociative - Return true if the instruction is associative:
161 /// Associative operators satisfy: x op (y op z) === (x op y) op z
163 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
164 /// not applied to floating point types.
166 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
167 static bool isAssociative(unsigned op, const Type *Ty);
169 /// isCommutative - Return true if the instruction is commutative:
171 /// Commutative operators satisfy: (x op y) === (y op x)
173 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
174 /// applied to any type.
176 bool isCommutative() const { return isCommutative(getOpcode()); }
177 static bool isCommutative(unsigned op);
179 /// isTrappingInstruction - Return true if the instruction may trap.
181 bool isTrapping() const {
182 return isTrapping(getOpcode());
184 static bool isTrapping(unsigned op);
186 virtual void print(std::ostream &OS) const { print(OS, 0); }
187 void print(std::ostream *OS) const { if (OS) print(*OS); }
188 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
190 /// Methods for support type inquiry through isa, cast, and dyn_cast:
191 static inline bool classof(const Instruction *) { return true; }
192 static inline bool classof(const Value *V) {
193 return V->getValueID() >= Value::InstructionVal;
196 //----------------------------------------------------------------------
197 // Exported enumerations...
199 enum TermOps { // These terminate basic blocks
200 #define FIRST_TERM_INST(N) TermOpsBegin = N,
201 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
202 #define LAST_TERM_INST(N) TermOpsEnd = N+1
203 #include "llvm/Instruction.def"
207 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
208 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
209 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
210 #include "llvm/Instruction.def"
214 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
215 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
216 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
217 #include "llvm/Instruction.def"
221 #define FIRST_CAST_INST(N) CastOpsBegin = N,
222 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
223 #define LAST_CAST_INST(N) CastOpsEnd = N+1
224 #include "llvm/Instruction.def"
228 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
229 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
230 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
231 #include "llvm/Instruction.def"
235 // getNext/Prev - Return the next or previous instruction in the list. The
236 // last node in the list is a terminator instruction.
237 Instruction *getNext() { return Next; }
238 const Instruction *getNext() const { return Next; }
239 Instruction *getPrev() { return Prev; }
240 const Instruction *getPrev() const { return Prev; }
243 } // End llvm namespace