Consider anything with a ValueType that is >= Instruction to be an instruction
[oota-llvm.git] / include / llvm / Instruction.h
1 //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the Instruction class, which is the
11 // base class for all of the LLVM instructions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_INSTRUCTION_H
16 #define LLVM_INSTRUCTION_H
17
18 #include "llvm/User.h"
19 #include "Support/Annotation.h"
20
21 namespace llvm {
22
23 struct AssemblyAnnotationWriter;
24
25 template<typename SC> struct ilist_traits;
26 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
27          typename SubClass> class SymbolTableListTraits;
28
29 class Instruction : public User, public Annotable {
30   BasicBlock *Parent;
31   Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
32
33   void setNext(Instruction *N) { Next = N; }
34   void setPrev(Instruction *N) { Prev = N; }
35
36   friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
37                                      ilist_traits<Instruction> >;
38   void setParent(BasicBlock *P);
39   void init();
40
41 protected:
42   unsigned iType;      // InstructionType: The opcode of the instruction
43
44   Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
45               Instruction *InsertBefore = 0);
46   Instruction(const Type *Ty, unsigned iType, const std::string &Name,
47               BasicBlock *InsertAtEnd);
48 public:
49
50   ~Instruction() {
51     assert(Parent == 0 && "Instruction still linked in the program!");
52   }
53
54   // Specialize setName to handle symbol table majik...
55   virtual void setName(const std::string &name, SymbolTable *ST = 0);
56   
57   /// clone() - Create a copy of 'this' instruction that is identical in all
58   /// ways except the following:
59   ///   * The instruction has no parent
60   ///   * The instruction has no name
61   ///
62   virtual Instruction *clone() const = 0;
63   
64   // Accessor methods...
65   //
66   inline const BasicBlock *getParent() const { return Parent; }
67   inline       BasicBlock *getParent()       { return Parent; }
68
69   // getNext/Prev - Return the next or previous instruction in the list.  The
70   // last node in the list is a terminator instruction.
71         Instruction *getNext()       { return Next; }
72   const Instruction *getNext() const { return Next; }
73         Instruction *getPrev()       { return Prev; }
74   const Instruction *getPrev() const { return Prev; }
75
76   /// mayWriteToMemory - Return true if this instruction may modify memory.
77   ///
78   virtual bool mayWriteToMemory() const { return false; }
79
80   // ---------------------------------------------------------------------------
81   /// Subclass classification... getOpcode() returns a member of 
82   /// one of the enums that is coming soon (down below)...
83   ///
84   unsigned getOpcode() const { return iType; }
85   virtual const char *getOpcodeName() const {
86     return getOpcodeName(getOpcode());
87   }
88   static const char* getOpcodeName(unsigned OpCode);
89
90   static inline bool isTerminator(unsigned OpCode) {
91     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
92   }
93
94   inline bool isTerminator() const {   // Instance of TerminatorInst?
95     return isTerminator(iType);
96   }
97
98   inline bool isBinaryOp() const {
99     return iType >= BinaryOpsBegin && iType < BinaryOpsEnd;
100   }
101
102   /// isAssociative - Return true if the instruction is associative:
103   ///
104   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
105   ///
106   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
107   /// not applied to floating point types.
108   ///
109   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
110   static bool isAssociative(unsigned op, const Type *Ty);
111
112   /// isCommutative - Return true if the instruction is commutative:
113   ///
114   ///   Commutative operators satisfy: (x op y) === (y op x)
115   ///
116   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
117   /// applied to any type.
118   ///
119   bool isCommutative() const { return isCommutative(getOpcode()); }
120   static bool isCommutative(unsigned op);
121
122   /// isRelational - Return true if the instruction is a Set* instruction:
123   ///
124   bool isRelational() const { return isRelational(getOpcode()); }
125   static bool isRelational(unsigned op);
126
127
128   /// isTrappingInstruction - Return true if the instruction may trap.
129   ///
130   bool isTrapping() const {
131     return isTrapping(getOpcode()); 
132   }
133   static bool isTrapping(unsigned op);
134   
135   virtual void print(std::ostream &OS) const { print(OS, 0); }
136   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
137
138   /// Methods for support type inquiry through isa, cast, and dyn_cast:
139   static inline bool classof(const Instruction *I) { return true; }
140   static inline bool classof(const Value *V) {
141     return V->getValueType() >= Value::InstructionVal;
142   }
143   
144   //----------------------------------------------------------------------
145   // Exported enumerations...
146   //
147   enum TermOps {       // These terminate basic blocks
148 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
149 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
150 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1,
151 #include "llvm/Instruction.def"
152   };
153
154   enum BinaryOps {
155 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
156 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
157 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1,
158 #include "llvm/Instruction.def"
159   };
160
161   enum MemoryOps {
162 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
163 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
164 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1,
165 #include "llvm/Instruction.def"
166   };
167
168   enum OtherOps {
169 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
170 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
171 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1,
172 #include "llvm/Instruction.def"
173   };
174 };
175
176 } // End llvm namespace
177
178 #endif