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"
19 #include "Support/Annotation.h"
23 class 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, public Annotable {
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 unsigned iType; // InstructionType: The opcode of the instruction
44 Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
45 Instruction *InsertBefore = 0);
46 Instruction(const Type *Ty, unsigned iType, const std::string &Name,
47 BasicBlock *InsertAtEnd);
51 assert(Parent == 0 && "Instruction still linked in the program!");
54 // Specialize setName to handle symbol table majik...
55 virtual void setName(const std::string &name, SymbolTable *ST = 0);
57 /// clone() - Create a copy of 'this' instruction that is identical in all
58 /// ways except the following:
59 /// * The instruction has no parent
60 /// * The instruction has no name
62 virtual Instruction *clone() const = 0;
64 // Accessor methods...
66 inline const BasicBlock *getParent() const { return Parent; }
67 inline BasicBlock *getParent() { return Parent; }
69 // getNext/Prev - Return the next or previous instruction in the list. The
70 // last node in the list is a terminator instruction.
71 Instruction *getNext() { return Next; }
72 const Instruction *getNext() const { return Next; }
73 Instruction *getPrev() { return Prev; }
74 const Instruction *getPrev() const { return Prev; }
76 /// mayWriteToMemory - Return true if this instruction may modify memory.
78 virtual bool mayWriteToMemory() const { return false; }
80 // ---------------------------------------------------------------------------
81 /// Subclass classification... getOpcode() returns a member of
82 /// one of the enums that is coming soon (down below)...
84 unsigned getOpcode() const { return iType; }
85 virtual const char *getOpcodeName() const {
86 return getOpcodeName(getOpcode());
88 static const char* getOpcodeName(unsigned OpCode);
90 static inline bool isTerminator(unsigned OpCode) {
91 return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
94 inline bool isTerminator() const { // Instance of TerminatorInst?
95 return isTerminator(iType);
98 inline bool isBinaryOp() const {
99 return iType >= BinaryOpsBegin && iType < BinaryOpsEnd;
102 /// isAssociative - Return true if the instruction is associative:
104 /// Associative operators satisfy: x op (y op z) === (x op y) op z
106 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
107 /// not applied to floating point types.
109 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
110 static bool isAssociative(unsigned op, const Type *Ty);
112 /// isCommutative - Return true if the instruction is commutative:
114 /// Commutative operators satisfy: (x op y) === (y op x)
116 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
117 /// applied to any type.
119 bool isCommutative() const { return isCommutative(getOpcode()); }
120 static bool isCommutative(unsigned op);
122 /// isRelational - Return true if the instruction is a Set* instruction:
124 bool isRelational() const { return isRelational(getOpcode()); }
125 static bool isRelational(unsigned op);
128 /// isTrappingInstruction - Return true if the instruction may trap.
130 bool isTrapping() const {
131 return isTrapping(getOpcode());
133 static bool isTrapping(unsigned op);
135 virtual void print(std::ostream &OS) const { print(OS, 0); }
136 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
138 /// Methods for support type inquiry through isa, cast, and dyn_cast:
139 static inline bool classof(const Instruction *I) { return true; }
140 static inline bool classof(const Value *V) {
141 return V->getValueType() == Value::InstructionVal;
144 //----------------------------------------------------------------------
145 // Exported enumerations...
147 enum TermOps { // These terminate basic blocks
148 #define FIRST_TERM_INST(N) TermOpsBegin = N,
149 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
150 #define LAST_TERM_INST(N) TermOpsEnd = N+1,
151 #include "llvm/Instruction.def"
155 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
156 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
157 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1,
158 #include "llvm/Instruction.def"
162 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
163 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
164 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1,
165 #include "llvm/Instruction.def"
169 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
170 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
171 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1,
172 #include "llvm/Instruction.def"
176 } // End llvm namespace