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"
20 template<typename SC> struct ilist_traits;
21 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
22 typename SubClass> class SymbolTableListTraits;
24 class Instruction : public User {
26 Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
28 void setNext(Instruction *N) { Next = N; }
29 void setPrev(Instruction *N) { Prev = N; }
31 friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
32 ilist_traits<Instruction> >;
33 void setParent(BasicBlock *P);
35 unsigned iType; // InstructionType: The opcode of the instruction
37 Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
38 Instruction *InsertBefore = 0);
41 // Specialize setName to handle symbol table majik...
42 virtual void setName(const std::string &name, SymbolTable *ST = 0);
44 /// clone() - Create a copy of 'this' instruction that is identical in all
45 /// ways except the following:
46 /// * The instruction has no parent
47 /// * The instruction has no name
49 virtual Instruction *clone() const = 0;
51 // Accessor methods...
53 inline const BasicBlock *getParent() const { return Parent; }
54 inline BasicBlock *getParent() { return Parent; }
56 // getNext/Prev - Return the next or previous instruction in the list. The
57 // last node in the list is a terminator instruction.
58 Instruction *getNext() { return Next; }
59 const Instruction *getNext() const { return Next; }
60 Instruction *getPrev() { return Prev; }
61 const Instruction *getPrev() const { return Prev; }
63 /// mayWriteToMemory - Return true if this instruction may modify memory.
65 virtual bool mayWriteToMemory() const { return false; }
67 // ---------------------------------------------------------------------------
68 /// Subclass classification... getOpcode() returns a member of
69 /// one of the enums that is coming soon (down below)...
71 unsigned getOpcode() const { return iType; }
72 virtual const char *getOpcodeName() const {
73 return getOpcodeName(getOpcode());
75 static const char* getOpcodeName(unsigned OpCode);
77 inline bool isTerminator() const { // Instance of TerminatorInst?
78 return iType >= TermOpsBegin && iType < TermOpsEnd;
80 inline bool isBinaryOp() const {
81 return iType >= BinaryOpsBegin && iType < BinaryOpsEnd;
84 /// isAssociative - Return true if the instruction is associative:
86 /// Associative operators satisfy: x op (y op z) === (x op y) op z
88 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
89 /// not applied to floating point types.
91 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
92 static bool isAssociative(unsigned op, const Type *Ty);
94 /// isCommutative - Return true if the instruction is commutative:
96 /// Commutative operators satisfy: (x op y) === (y op x)
98 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
99 /// applied to any type.
101 bool isCommutative() const { return isCommutative(getOpcode()); }
102 static bool isCommutative(unsigned op);
104 /// isTrappingInstruction - Return true if the instruction may trap.
106 bool isTrapping() const {
107 return isTrapping(getOpcode());
109 static bool isTrapping(unsigned op);
111 virtual void print(std::ostream &OS) const;
113 /// Methods for support type inquiry through isa, cast, and dyn_cast:
114 static inline bool classof(const Instruction *I) { return true; }
115 static inline bool classof(const Value *V) {
116 return V->getValueType() == Value::InstructionVal;
119 //----------------------------------------------------------------------
120 // Exported enumerations...
122 enum TermOps { // These terminate basic blocks
123 #define FIRST_TERM_INST(N) TermOpsBegin = N,
124 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
125 #define LAST_TERM_INST(N) TermOpsEnd = N+1,
126 #include "llvm/Instruction.def"
130 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
131 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
132 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1,
133 #include "llvm/Instruction.def"
137 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
138 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
139 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1,
140 #include "llvm/Instruction.def"
144 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
145 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
146 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1,
147 #include "llvm/Instruction.def"