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 {
31 Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
33 void setNext(Instruction *N) { Next = N; }
34 void setPrev(Instruction *N) { Prev = N; }
36 friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
37 ilist_traits<Instruction> >;
38 void setParent(BasicBlock *P);
42 // FIXME: This is a dirty hack. Setcc instructions shouldn't encode the CC
43 // into the opcode field. When they don't, this will be unneeded.
44 void setOpcode(unsigned NewOpcode);
45 friend class BinaryOperator;
47 Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
48 Instruction *InsertBefore = 0);
49 Instruction(const Type *Ty, unsigned iType, const std::string &Name,
50 BasicBlock *InsertAtEnd);
54 assert(Parent == 0 && "Instruction still linked in the program!");
57 // Specialize setName to handle symbol table majik...
58 virtual void setName(const std::string &name, SymbolTable *ST = 0);
60 /// mayWriteToMemory - Return true if this instruction may modify memory.
62 virtual bool mayWriteToMemory() const { return false; }
64 /// clone() - Create a copy of 'this' instruction that is identical in all
65 /// ways except the following:
66 /// * The instruction has no parent
67 /// * The instruction has no name
69 virtual Instruction *clone() const = 0;
71 /// isIdenticalTo - Return true if the specified instruction is exactly
72 /// identical to the current one. This means that all operands match and any
73 /// extra information (e.g. load is volatile) agree.
74 bool isIdenticalTo(Instruction *I) const;
77 // Accessor methods...
79 inline const BasicBlock *getParent() const { return Parent; }
80 inline BasicBlock *getParent() { return Parent; }
82 // getNext/Prev - Return the next or previous instruction in the list. The
83 // last node in the list is a terminator instruction.
84 Instruction *getNext() { return Next; }
85 const Instruction *getNext() const { return Next; }
86 Instruction *getPrev() { return Prev; }
87 const Instruction *getPrev() const { return Prev; }
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 // ---------------------------------------------------------------------------
100 /// Subclass classification... getOpcode() returns a member of
101 /// one of the enums that is coming soon (down below)...
103 unsigned getOpcode() const { return getValueType() - InstructionVal; }
104 virtual const char *getOpcodeName() const {
105 return getOpcodeName(getOpcode());
107 static const char* getOpcodeName(unsigned OpCode);
109 static inline bool isTerminator(unsigned OpCode) {
110 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
113 inline bool isTerminator() const { // Instance of TerminatorInst?
114 return isTerminator(getOpcode());
117 inline bool isBinaryOp() const {
118 return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd;
121 /// isAssociative - Return true if the instruction is associative:
123 /// Associative operators satisfy: x op (y op z) === (x op y) op z
125 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
126 /// not applied to floating point types.
128 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
129 static bool isAssociative(unsigned op, const Type *Ty);
131 /// isCommutative - Return true if the instruction is commutative:
133 /// Commutative operators satisfy: (x op y) === (y op x)
135 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
136 /// applied to any type.
138 bool isCommutative() const { return isCommutative(getOpcode()); }
139 static bool isCommutative(unsigned op);
141 /// isRelational - Return true if the instruction is a Set* instruction:
143 bool isRelational() const { return isRelational(getOpcode()); }
144 static bool isRelational(unsigned op);
147 /// isTrappingInstruction - Return true if the instruction may trap.
149 bool isTrapping() const {
150 return isTrapping(getOpcode());
152 static bool isTrapping(unsigned op);
154 virtual void print(std::ostream &OS) const { print(OS, 0); }
155 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
157 /// Methods for support type inquiry through isa, cast, and dyn_cast:
158 static inline bool classof(const Instruction *I) { return true; }
159 static inline bool classof(const Value *V) {
160 return V->getValueType() >= Value::InstructionVal;
163 //----------------------------------------------------------------------
164 // Exported enumerations...
166 enum TermOps { // These terminate basic blocks
167 #define FIRST_TERM_INST(N) TermOpsBegin = N,
168 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
169 #define LAST_TERM_INST(N) TermOpsEnd = N+1,
170 #include "llvm/Instruction.def"
174 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
175 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
176 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1,
177 #include "llvm/Instruction.def"
181 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
182 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
183 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1,
184 #include "llvm/Instruction.def"
188 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
189 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
190 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1,
191 #include "llvm/Instruction.def"
195 } // End llvm namespace