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);
40 unsigned iType; // InstructionType: The opcode of the instruction
42 Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
43 Instruction *InsertBefore = 0);
46 // Specialize setName to handle symbol table majik...
47 virtual void setName(const std::string &name, SymbolTable *ST = 0);
49 /// clone() - Create a copy of 'this' instruction that is identical in all
50 /// ways except the following:
51 /// * The instruction has no parent
52 /// * The instruction has no name
54 virtual Instruction *clone() const = 0;
56 // Accessor methods...
58 inline const BasicBlock *getParent() const { return Parent; }
59 inline BasicBlock *getParent() { return Parent; }
61 // getNext/Prev - Return the next or previous instruction in the list. The
62 // last node in the list is a terminator instruction.
63 Instruction *getNext() { return Next; }
64 const Instruction *getNext() const { return Next; }
65 Instruction *getPrev() { return Prev; }
66 const Instruction *getPrev() const { return Prev; }
68 /// mayWriteToMemory - Return true if this instruction may modify memory.
70 virtual bool mayWriteToMemory() const { return false; }
72 // ---------------------------------------------------------------------------
73 /// Subclass classification... getOpcode() returns a member of
74 /// one of the enums that is coming soon (down below)...
76 unsigned getOpcode() const { return iType; }
77 virtual const char *getOpcodeName() const {
78 return getOpcodeName(getOpcode());
80 static const char* getOpcodeName(unsigned OpCode);
82 inline bool isTerminator() const { // Instance of TerminatorInst?
83 return iType >= TermOpsBegin && iType < TermOpsEnd;
85 inline bool isBinaryOp() const {
86 return iType >= BinaryOpsBegin && iType < BinaryOpsEnd;
89 /// isAssociative - Return true if the instruction is associative:
91 /// Associative operators satisfy: x op (y op z) === (x op y) op z
93 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
94 /// not applied to floating point types.
96 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
97 static bool isAssociative(unsigned op, const Type *Ty);
99 /// isCommutative - Return true if the instruction is commutative:
101 /// Commutative operators satisfy: (x op y) === (y op x)
103 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
104 /// applied to any type.
106 bool isCommutative() const { return isCommutative(getOpcode()); }
107 static bool isCommutative(unsigned op);
109 /// isRelational - Return true if the instruction is a Set* instruction:
111 bool isRelational() const { return isRelational(getOpcode()); }
112 static bool isRelational(unsigned op);
115 /// isTrappingInstruction - Return true if the instruction may trap.
117 bool isTrapping() const {
118 return isTrapping(getOpcode());
120 static bool isTrapping(unsigned op);
122 virtual void print(std::ostream &OS) const { print(OS, 0); }
123 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
125 /// Methods for support type inquiry through isa, cast, and dyn_cast:
126 static inline bool classof(const Instruction *I) { return true; }
127 static inline bool classof(const Value *V) {
128 return V->getValueType() == Value::InstructionVal;
131 //----------------------------------------------------------------------
132 // Exported enumerations...
134 enum TermOps { // These terminate basic blocks
135 #define FIRST_TERM_INST(N) TermOpsBegin = N,
136 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
137 #define LAST_TERM_INST(N) TermOpsEnd = N+1,
138 #include "llvm/Instruction.def"
142 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
143 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
144 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1,
145 #include "llvm/Instruction.def"
149 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
150 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
151 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1,
152 #include "llvm/Instruction.def"
156 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
157 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
158 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1,
159 #include "llvm/Instruction.def"
163 } // End llvm namespace