add a new Instruction::mayReadFromMemory predicate, make
[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 class BinaryOperator;
24
25 template<typename ValueSubClass, typename ItemParentClass>
26   class SymbolTableListTraits;
27
28 class Instruction : public User {
29   void operator=(const Instruction &);     // Do not implement
30   Instruction(const Instruction &);        // Do not implement
31
32   BasicBlock *Parent;
33   Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
34
35   void setNext(Instruction *N) { Next = N; }
36   void setPrev(Instruction *N) { Prev = N; }
37
38   friend class SymbolTableListTraits<Instruction, BasicBlock>;
39   void setParent(BasicBlock *P);
40 protected:
41   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
42               Instruction *InsertBefore = 0);
43   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
44               BasicBlock *InsertAtEnd);
45 public:
46   // Out of line virtual method, so the vtable, etc has a home.
47   ~Instruction();
48   
49   /// mayWriteToMemory - Return true if this instruction may modify memory.
50   ///
51   bool mayWriteToMemory() const;
52
53   /// mayReadFromMemory - Return true if this instruction may read memory.
54   ///
55   bool mayReadFromMemory() const;
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   /// isIdenticalTo - Return true if the specified instruction is exactly
65   /// identical to the current one.  This means that all operands match and any
66   /// extra information (e.g. load is volatile) agree.
67   bool isIdenticalTo(Instruction *I) const;
68
69   /// This function determines if the specified instruction executes the same
70   /// operation as the current one. This means that the opcodes, type, operand
71   /// types and any other factors affecting the operation must be the same. This
72   /// is similar to isIdenticalTo except the operands themselves don't have to
73   /// be identical.
74   /// @returns true if the specified instruction is the same operation as
75   /// the current one.
76   /// @brief Determine if one instruction is the same operation as another.
77   bool isSameOperationAs(Instruction *I) const;
78
79   /// isUsedOutsideOfBlock - Return true if there are any uses of this
80   /// instruction in blocks other than the specified block.  Note that PHI nodes
81   /// are considered to evaluate their operands in the corresponding predecessor
82   /// block.
83   bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
84   
85   
86   /// use_back - Specialize the methods defined in Value, as we know that an
87   /// instruction can only be used by other instructions.
88   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
89   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
90   
91   // Accessor methods...
92   //
93   inline const BasicBlock *getParent() const { return Parent; }
94   inline       BasicBlock *getParent()       { return Parent; }
95
96   /// removeFromParent - This method unlinks 'this' from the containing basic
97   /// block, but does not delete it.
98   ///
99   void removeFromParent();
100
101   /// eraseFromParent - This method unlinks 'this' from the containing basic
102   /// block and deletes it.
103   ///
104   void eraseFromParent();
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 private:
232   // getNext/Prev - Return the next or previous instruction in the list.  The
233   // last node in the list is a terminator instruction.
234   Instruction *getNext()             { return Next; }
235   const Instruction *getNext() const { return Next; }
236   Instruction *getPrev()             { return Prev; }
237   const Instruction *getPrev() const { return Prev; }
238 };
239
240 } // End llvm namespace
241
242 #endif