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