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);
41 // FIXME: This is a dirty hack. Setcc instructions shouldn't encode the CC
42 // into the opcode field. When they don't, this will be unneeded.
43 void setOpcode(unsigned NewOpcode);
44 friend class BinaryOperator;
46 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
47 const std::string &Name = "",
48 Instruction *InsertBefore = 0);
49 Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
50 const std::string &Name, BasicBlock *InsertAtEnd);
54 assert(Parent == 0 && "Instruction still linked in the program!");
57 /// mayWriteToMemory - Return true if this instruction may modify memory.
59 virtual bool mayWriteToMemory() const { return false; }
61 /// clone() - Create a copy of 'this' instruction that is identical in all
62 /// ways except the following:
63 /// * The instruction has no parent
64 /// * The instruction has no name
66 virtual Instruction *clone() const = 0;
68 /// isIdenticalTo - Return true if the specified instruction is exactly
69 /// identical to the current one. This means that all operands match and any
70 /// extra information (e.g. load is volatile) agree.
71 bool isIdenticalTo(Instruction *I) const;
74 // Accessor methods...
76 inline const BasicBlock *getParent() const { return Parent; }
77 inline BasicBlock *getParent() { return Parent; }
79 // getNext/Prev - Return the next or previous instruction in the list. The
80 // last node in the list is a terminator instruction.
81 Instruction *getNext() { return Next; }
82 const Instruction *getNext() const { return Next; }
83 Instruction *getPrev() { return Prev; }
84 const Instruction *getPrev() const { return Prev; }
86 /// removeFromParent - This method unlinks 'this' from the containing basic
87 /// block, but does not delete it.
89 void removeFromParent();
91 /// eraseFromParent - This method unlinks 'this' from the containing basic
92 /// block and deletes it.
94 void eraseFromParent();
96 // ---------------------------------------------------------------------------
97 /// Subclass classification... getOpcode() returns a member of
98 /// one of the enums that is coming soon (down below)...
100 unsigned getOpcode() const { return getValueType() - InstructionVal; }
101 virtual const char *getOpcodeName() const {
102 return getOpcodeName(getOpcode());
104 static const char* getOpcodeName(unsigned OpCode);
106 static inline bool isTerminator(unsigned OpCode) {
107 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
110 inline bool isTerminator() const { // Instance of TerminatorInst?
111 return isTerminator(getOpcode());
114 inline bool isBinaryOp() const {
115 return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd;
118 /// isAssociative - Return true if the instruction is associative:
120 /// Associative operators satisfy: x op (y op z) === (x op y) op z
122 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
123 /// not applied to floating point types.
125 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
126 static bool isAssociative(unsigned op, const Type *Ty);
128 /// isCommutative - Return true if the instruction is commutative:
130 /// Commutative operators satisfy: (x op y) === (y op x)
132 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
133 /// applied to any type.
135 bool isCommutative() const { return isCommutative(getOpcode()); }
136 static bool isCommutative(unsigned op);
138 /// isRelational - Return true if the instruction is a Set* instruction:
140 bool isRelational() const { return isRelational(getOpcode()); }
141 static bool isRelational(unsigned op);
144 /// isTrappingInstruction - Return true if the instruction may trap.
146 bool isTrapping() const {
147 return isTrapping(getOpcode());
149 static bool isTrapping(unsigned op);
151 virtual void print(std::ostream &OS) const { print(OS, 0); }
152 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
154 /// Methods for support type inquiry through isa, cast, and dyn_cast:
155 static inline bool classof(const Instruction *I) { return true; }
156 static inline bool classof(const Value *V) {
157 return V->getValueType() >= Value::InstructionVal;
160 //----------------------------------------------------------------------
161 // Exported enumerations...
163 enum TermOps { // These terminate basic blocks
164 #define FIRST_TERM_INST(N) TermOpsBegin = N,
165 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
166 #define LAST_TERM_INST(N) TermOpsEnd = N+1
167 #include "llvm/Instruction.def"
171 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
172 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
173 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
174 #include "llvm/Instruction.def"
178 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
179 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
180 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
181 #include "llvm/Instruction.def"
185 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
186 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
187 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
188 #include "llvm/Instruction.def"
192 } // End llvm namespace