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