1 //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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;
25 template<typename SC> struct ilist_traits;
26 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
27 typename SubClass> class SymbolTableListTraits;
29 class Instruction : public User {
30 void operator=(const Instruction &); // Do not implement
31 Instruction(const Instruction &); // Do not implement
34 Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
36 void setNext(Instruction *N) { Next = N; }
37 void setPrev(Instruction *N) { Prev = N; }
39 friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
40 ilist_traits<Instruction> >;
41 void setParent(BasicBlock *P);
44 // FIXME: This is a dirty hack. Setcc instructions shouldn't encode the CC
45 // into the opcode field. When they don't, this will be unneeded.
46 void setOpcode(unsigned NewOpcode);
47 friend class BinaryOperator;
49 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
50 const std::string &Name = "",
51 Instruction *InsertBefore = 0);
52 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
53 const std::string &Name, BasicBlock *InsertAtEnd);
55 // Out of line virtual method, so the vtable, etc has a home.
58 /// mayWriteToMemory - Return true if this instruction may modify memory.
60 virtual bool mayWriteToMemory() const { return false; }
62 /// clone() - Create a copy of 'this' instruction that is identical in all
63 /// ways except the following:
64 /// * The instruction has no parent
65 /// * The instruction has no name
67 virtual Instruction *clone() const = 0;
69 /// isIdenticalTo - Return true if the specified instruction is exactly
70 /// identical to the current one. This means that all operands match and any
71 /// extra information (e.g. load is volatile) agree.
72 bool isIdenticalTo(Instruction *I) const;
75 // Accessor methods...
77 inline const BasicBlock *getParent() const { return Parent; }
78 inline BasicBlock *getParent() { return Parent; }
80 // getNext/Prev - Return the next or previous instruction in the list. The
81 // last node in the list is a terminator instruction.
82 Instruction *getNext() { return Next; }
83 const Instruction *getNext() const { return Next; }
84 Instruction *getPrev() { return Prev; }
85 const Instruction *getPrev() const { return Prev; }
87 /// removeFromParent - This method unlinks 'this' from the containing basic
88 /// block, but does not delete it.
90 void removeFromParent();
92 /// eraseFromParent - This method unlinks 'this' from the containing basic
93 /// block and deletes it.
95 void eraseFromParent();
97 /// moveBefore - Unlink this instruction from its current basic block and
98 /// insert it into the basic block that MovePos lives in, right before
100 void moveBefore(Instruction *MovePos);
102 // ---------------------------------------------------------------------------
103 /// Subclass classification... getOpcode() returns a member of
104 /// one of the enums that is coming soon (down below)...
106 unsigned getOpcode() const { return getValueType() - InstructionVal; }
107 const char *getOpcodeName() const {
108 return getOpcodeName(getOpcode());
110 static const char* getOpcodeName(unsigned OpCode);
112 static inline bool isTerminator(unsigned OpCode) {
113 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
116 inline bool isTerminator() const { // Instance of TerminatorInst?
117 return isTerminator(getOpcode());
120 inline bool isBinaryOp() const {
121 return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd;
124 /// isAssociative - Return true if the instruction is associative:
126 /// Associative operators satisfy: x op (y op z) === (x op y) op z
128 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
129 /// not applied to floating point types.
131 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
132 static bool isAssociative(unsigned op, const Type *Ty);
134 /// isCommutative - Return true if the instruction is commutative:
136 /// Commutative operators satisfy: (x op y) === (y op x)
138 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
139 /// applied to any type.
141 bool isCommutative() const { return isCommutative(getOpcode()); }
142 static bool isCommutative(unsigned op);
144 /// isRelational - Return true if the instruction is a Set* instruction:
146 bool isRelational() const { return isRelational(getOpcode()); }
147 static bool isRelational(unsigned op);
150 /// isTrappingInstruction - Return true if the instruction may trap.
152 bool isTrapping() const {
153 return isTrapping(getOpcode());
155 static bool isTrapping(unsigned op);
157 virtual void print(std::ostream &OS) const { print(OS, 0); }
158 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
160 /// Methods for support type inquiry through isa, cast, and dyn_cast:
161 static inline bool classof(const Instruction *) { return true; }
162 static inline bool classof(const Value *V) {
163 return V->getValueType() >= Value::InstructionVal;
166 //----------------------------------------------------------------------
167 // Exported enumerations...
169 enum TermOps { // These terminate basic blocks
170 #define FIRST_TERM_INST(N) TermOpsBegin = N,
171 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
172 #define LAST_TERM_INST(N) TermOpsEnd = N+1
173 #include "llvm/Instruction.def"
177 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
178 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
179 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
180 #include "llvm/Instruction.def"
184 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
185 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
186 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
187 #include "llvm/Instruction.def"
191 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
192 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
193 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
194 #include "llvm/Instruction.def"
198 } // End llvm namespace