1 //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
3 // This file contains the declaration of the Instruction class, which is the
4 // base class for all of the LLVM instructions.
6 //===----------------------------------------------------------------------===//
8 #ifndef LLVM_INSTRUCTION_H
9 #define LLVM_INSTRUCTION_H
13 #include "llvm/User.h"
14 template<typename SC> struct ilist_traits;
15 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
16 typename SubClass> class SymbolTableListTraits;
18 class Instruction : public User {
20 Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
22 void setNext(Instruction *N) { Next = N; }
23 void setPrev(Instruction *N) { Prev = N; }
25 friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
26 ilist_traits<Instruction> >;
27 void setParent(BasicBlock *P);
29 unsigned iType; // InstructionType: The opcode of the instruction
31 Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
32 Instruction *InsertBefore = 0);
34 virtual ~Instruction() {
35 assert(Parent == 0 && "Instruction still embedded in basic block!");
38 // Specialize setName to handle symbol table majik...
39 virtual void setName(const std::string &name, SymbolTable *ST = 0);
41 /// clone() - Create a copy of 'this' instruction that is identical in all
42 /// ways except the following:
43 /// * The instruction has no parent
44 /// * The instruction has no name
46 virtual Instruction *clone() const = 0;
48 // Accessor methods...
50 inline const BasicBlock *getParent() const { return Parent; }
51 inline BasicBlock *getParent() { return Parent; }
53 // getNext/Prev - Return the next or previous instruction in the list. The
54 // last node in the list is a terminator instruction.
55 Instruction *getNext() { return Next; }
56 const Instruction *getNext() const { return Next; }
57 Instruction *getPrev() { return Prev; }
58 const Instruction *getPrev() const { return Prev; }
60 /// mayWriteToMemory - Return true if this instruction may modify memory.
62 virtual bool mayWriteToMemory() const { return false; }
64 // ---------------------------------------------------------------------------
65 /// Subclass classification... getOpcode() returns a member of
66 /// one of the enums that is coming soon (down below)...
68 unsigned getOpcode() const { return iType; }
69 virtual const char *getOpcodeName() const {
70 return getOpcodeName(getOpcode());
72 static const char* getOpcodeName(unsigned OpCode);
74 inline bool isTerminator() const { // Instance of TerminatorInst?
75 return iType >= TermOpsBegin && iType < TermOpsEnd;
77 inline bool isBinaryOp() const {
78 return iType >= BinaryOpsBegin && iType < BinaryOpsEnd;
81 /// isAssociative - Return true if the instruction is associative:
83 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
85 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
86 /// not applied to floating point types.
88 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
89 static bool isAssociative(unsigned op, const Type *Ty);
91 /// isCommutative - Return true if the instruction is commutative:
93 /// Commutative operators satistify: (x op y) === (y op x)
95 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
96 /// applied to any type.
98 bool isCommutative() const { return isCommutative(getOpcode()); }
99 static bool isCommutative(unsigned op);
102 virtual void print(std::ostream &OS) const;
104 /// Methods for support type inquiry through isa, cast, and dyn_cast:
105 static inline bool classof(const Instruction *I) { return true; }
106 static inline bool classof(const Value *V) {
107 return V->getValueType() == Value::InstructionVal;
110 //----------------------------------------------------------------------
111 // Exported enumerations...
113 enum TermOps { // These terminate basic blocks
114 #define FIRST_TERM_INST(N) TermOpsBegin = N,
115 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
116 #define LAST_TERM_INST(N) TermOpsEnd = N+1,
117 #include "llvm/Instruction.def"
121 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
122 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
123 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1,
124 #include "llvm/Instruction.def"
128 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
129 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
130 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1,
131 #include "llvm/Instruction.def"
135 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
136 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
137 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1,
138 #include "llvm/Instruction.def"