Rename some GC classes so that their roll will hopefully be clearer.
[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 is distributed under the University of Illinois Open Source
6 // 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 "llvm/ADT/ilist_node.h"
20
21 namespace llvm {
22
23 struct AssemblyAnnotationWriter;
24
25 template<typename ValueSubClass, typename ItemParentClass>
26   class SymbolTableListTraits;
27
28 class Instruction : public User, public ilist_node<Instruction> {
29   void operator=(const Instruction &);     // Do not implement
30   Instruction(const Instruction &);        // Do not implement
31
32   BasicBlock *Parent;
33
34   friend class SymbolTableListTraits<Instruction, BasicBlock>;
35   void setParent(BasicBlock *P);
36 protected:
37   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
38               Instruction *InsertBefore = 0);
39   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
40               BasicBlock *InsertAtEnd);
41 public:
42   // Out of line virtual method, so the vtable, etc has a home.
43   ~Instruction();
44   
45   /// mayWriteToMemory - Return true if this instruction may modify memory.
46   ///
47   bool mayWriteToMemory() const;
48
49   /// mayReadFromMemory - Return true if this instruction may read memory.
50   ///
51   bool mayReadFromMemory() const;
52   
53   /// clone() - Create a copy of 'this' instruction that is identical in all
54   /// ways except the following:
55   ///   * The instruction has no parent
56   ///   * The instruction has no name
57   ///
58   virtual Instruction *clone() const = 0;
59
60   /// isIdenticalTo - Return true if the specified instruction is exactly
61   /// identical to the current one.  This means that all operands match and any
62   /// extra information (e.g. load is volatile) agree.
63   bool isIdenticalTo(Instruction *I) const;
64
65   /// This function determines if the specified instruction executes the same
66   /// operation as the current one. This means that the opcodes, type, operand
67   /// types and any other factors affecting the operation must be the same. This
68   /// is similar to isIdenticalTo except the operands themselves don't have to
69   /// be identical.
70   /// @returns true if the specified instruction is the same operation as
71   /// the current one.
72   /// @brief Determine if one instruction is the same operation as another.
73   bool isSameOperationAs(Instruction *I) const;
74
75   /// isUsedOutsideOfBlock - Return true if there are any uses of this
76   /// instruction in blocks other than the specified block.  Note that PHI nodes
77   /// are considered to evaluate their operands in the corresponding predecessor
78   /// block.
79   bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
80   
81   
82   /// use_back - Specialize the methods defined in Value, as we know that an
83   /// instruction can only be used by other instructions.
84   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
85   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
86   
87   // Accessor methods...
88   //
89   inline const BasicBlock *getParent() const { return Parent; }
90   inline       BasicBlock *getParent()       { return Parent; }
91
92   /// removeFromParent - This method unlinks 'this' from the containing basic
93   /// block, but does not delete it.
94   ///
95   void removeFromParent();
96
97   /// eraseFromParent - This method unlinks 'this' from the containing basic
98   /// block and deletes it.
99   ///
100   void eraseFromParent();
101
102   /// insertBefore - Insert an unlinked instructions into a basic block
103   /// immediately before the specified instruction.
104   void insertBefore(Instruction *InsertPos);
105
106   /// moveBefore - Unlink this instruction from its current basic block and
107   /// insert it into the basic block that MovePos lives in, right before
108   /// MovePos.
109   void moveBefore(Instruction *MovePos);
110
111   // ---------------------------------------------------------------------------
112   /// Subclass classification... getOpcode() returns a member of
113   /// one of the enums that is coming soon (down below)...
114   ///
115   unsigned getOpcode() const { return getValueID() - InstructionVal; }
116   const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
117   bool isTerminator() const { return isTerminator(getOpcode()); }
118   bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
119   bool isShift() { return isShift(getOpcode()); }
120   bool isCast() const { return isCast(getOpcode()); }
121   
122   
123   
124   static const char* getOpcodeName(unsigned OpCode);
125
126   static inline bool isTerminator(unsigned OpCode) {
127     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
128   }
129
130   static inline bool isBinaryOp(unsigned Opcode) {
131     return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
132   }
133
134   /// @brief Determine if the Opcode is one of the shift instructions.
135   static inline bool isShift(unsigned Opcode) {
136     return Opcode >= Shl && Opcode <= AShr;
137   }
138
139   /// isLogicalShift - Return true if this is a logical shift left or a logical
140   /// shift right.
141   inline bool isLogicalShift() const {
142     return getOpcode() == Shl || getOpcode() == LShr;
143   }
144
145   /// isLogicalShift - Return true if this is a logical shift left or a logical
146   /// shift right.
147   inline bool isArithmeticShift() const {
148     return getOpcode() == AShr;
149   }
150
151   /// @brief Determine if the OpCode is one of the CastInst instructions.
152   static inline bool isCast(unsigned OpCode) {
153     return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
154   }
155
156   /// isAssociative - Return true if the instruction is associative:
157   ///
158   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
159   ///
160   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
161   /// not applied to floating point types.
162   ///
163   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
164   static bool isAssociative(unsigned op, const Type *Ty);
165
166   /// isCommutative - Return true if the instruction is commutative:
167   ///
168   ///   Commutative operators satisfy: (x op y) === (y op x)
169   ///
170   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
171   /// applied to any type.
172   ///
173   bool isCommutative() const { return isCommutative(getOpcode()); }
174   static bool isCommutative(unsigned op);
175
176   /// isTrappingInstruction - Return true if the instruction may trap.
177   ///
178   bool isTrapping() const {
179     return isTrapping(getOpcode());
180   }
181   static bool isTrapping(unsigned op);
182
183   virtual void print(std::ostream &OS) const { print(OS, 0); }
184   void print(std::ostream *OS) const { if (OS) print(*OS); }
185   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
186
187   /// Methods for support type inquiry through isa, cast, and dyn_cast:
188   static inline bool classof(const Instruction *) { return true; }
189   static inline bool classof(const Value *V) {
190     return V->getValueID() >= Value::InstructionVal;
191   }
192
193   //----------------------------------------------------------------------
194   // Exported enumerations...
195   //
196   enum TermOps {       // These terminate basic blocks
197 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
198 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
199 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
200 #include "llvm/Instruction.def"
201   };
202
203   enum BinaryOps {
204 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
205 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
206 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
207 #include "llvm/Instruction.def"
208   };
209
210   enum MemoryOps {
211 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
212 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
213 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
214 #include "llvm/Instruction.def"
215   };
216
217   enum CastOps {
218 #define  FIRST_CAST_INST(N)             CastOpsBegin = N,
219 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
220 #define   LAST_CAST_INST(N)             CastOpsEnd = N+1
221 #include "llvm/Instruction.def"
222   };
223
224   enum OtherOps {
225 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
226 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
227 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
228 #include "llvm/Instruction.def"
229   };
230 };
231
232 } // End llvm namespace
233
234 #endif