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
11 #include "llvm/User.h"
13 template<typename SC> struct ilist_traits;
14 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
15 typename SubClass> class SymbolTableListTraits;
17 class Instruction : public User {
19 Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
21 void setNext(Instruction *N) { Next = N; }
22 void setPrev(Instruction *N) { Prev = N; }
24 friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
25 ilist_traits<Instruction> >;
26 void setParent(BasicBlock *P);
28 unsigned iType; // InstructionType: The opcode of the instruction
30 Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
31 Instruction *InsertBefore = 0);
33 virtual ~Instruction() {
34 assert(Parent == 0 && "Instruction still embedded in basic block!");
37 // Specialize setName to handle symbol table majik...
38 virtual void setName(const std::string &name, SymbolTable *ST = 0);
40 /// clone() - Create a copy of 'this' instruction that is identical in all
41 /// ways except the following:
42 /// * The instruction has no parent
43 /// * The instruction has no name
45 virtual Instruction *clone() const = 0;
47 // Accessor methods...
49 inline const BasicBlock *getParent() const { return Parent; }
50 inline BasicBlock *getParent() { return Parent; }
52 // getNext/Prev - Return the next or previous instruction in the list. The
53 // last node in the list is a terminator instruction.
54 Instruction *getNext() { return Next; }
55 const Instruction *getNext() const { return Next; }
56 Instruction *getPrev() { return Prev; }
57 const Instruction *getPrev() const { return Prev; }
59 /// mayWriteToMemory - Return true if this instruction may modify memory.
61 virtual bool mayWriteToMemory() const { return false; }
63 // ---------------------------------------------------------------------------
64 /// Subclass classification... getOpcode() returns a member of
65 /// one of the enums that is coming soon (down below)...
67 unsigned getOpcode() const { return iType; }
68 virtual const char *getOpcodeName() const {
69 return getOpcodeName(getOpcode());
71 static const char* getOpcodeName(unsigned OpCode);
73 inline bool isTerminator() const { // Instance of TerminatorInst?
74 return iType >= TermOpsBegin && iType < TermOpsEnd;
76 inline bool isBinaryOp() const {
77 return iType >= BinaryOpsBegin && iType < BinaryOpsEnd;
80 /// isAssociative - Return true if the instruction is associative:
82 /// Associative operators satisfy: x op (y op z) === (x op y) op z
84 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
85 /// not applied to floating point types.
87 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
88 static bool isAssociative(unsigned op, const Type *Ty);
90 /// isCommutative - Return true if the instruction is commutative:
92 /// Commutative operators satisfy: (x op y) === (y op x)
94 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
95 /// applied to any type.
97 bool isCommutative() const { return isCommutative(getOpcode()); }
98 static bool isCommutative(unsigned op);
100 /// isTrappingInstruction - Return true if the instruction may trap.
102 bool isTrapping() const {
103 return isTrapping(getOpcode());
105 static bool isTrapping(unsigned op);
107 virtual void print(std::ostream &OS) const;
109 /// Methods for support type inquiry through isa, cast, and dyn_cast:
110 static inline bool classof(const Instruction *I) { return true; }
111 static inline bool classof(const Value *V) {
112 return V->getValueType() == Value::InstructionVal;
115 //----------------------------------------------------------------------
116 // Exported enumerations...
118 enum TermOps { // These terminate basic blocks
119 #define FIRST_TERM_INST(N) TermOpsBegin = N,
120 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
121 #define LAST_TERM_INST(N) TermOpsEnd = N+1,
122 #include "llvm/Instruction.def"
126 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
127 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
128 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1,
129 #include "llvm/Instruction.def"
133 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
134 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
135 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1,
136 #include "llvm/Instruction.def"
140 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
141 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
142 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1,
143 #include "llvm/Instruction.def"