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