improve portability to avoid conflicting with std::next in c++'0x.
[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 class LLVMContext;
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   virtual Instruction *clone_impl() const = 0;
42 public:
43   // Out of line virtual method, so the vtable, etc has a home.
44   ~Instruction();
45   
46   /// clone() - Create a copy of 'this' instruction that is identical in all
47   /// ways except the following:
48   ///   * The instruction has no parent
49   ///   * The instruction has no name
50   ///
51   Instruction *clone() const;
52
53   /// isIdenticalTo - Return true if the specified instruction is exactly
54   /// identical to the current one.  This means that all operands match and any
55   /// extra information (e.g. load is volatile) agree.
56   bool isIdenticalTo(const Instruction *I) const;
57
58   /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
59   /// ignores the SubclassOptionalData flags, which specify conditions
60   /// under which the instruction's result is undefined.
61   bool isIdenticalToWhenDefined(const Instruction *I) const;
62
63   /// This function determines if the specified instruction executes the same
64   /// operation as the current one. This means that the opcodes, type, operand
65   /// types and any other factors affecting the operation must be the same. This
66   /// is similar to isIdenticalTo except the operands themselves don't have to
67   /// be identical.
68   /// @returns true if the specified instruction is the same operation as
69   /// the current one.
70   /// @brief Determine if one instruction is the same operation as another.
71   bool isSameOperationAs(const Instruction *I) const;
72
73   /// isUsedOutsideOfBlock - Return true if there are any uses of this
74   /// instruction in blocks other than the specified block.  Note that PHI nodes
75   /// are considered to evaluate their operands in the corresponding predecessor
76   /// block.
77   bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
78   
79   
80   /// use_back - Specialize the methods defined in Value, as we know that an
81   /// instruction can only be used by other instructions.
82   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
83   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
84   
85   // Accessor methods...
86   //
87   inline const BasicBlock *getParent() const { return Parent; }
88   inline       BasicBlock *getParent()       { return Parent; }
89
90   /// removeFromParent - This method unlinks 'this' from the containing basic
91   /// block, but does not delete it.
92   ///
93   void removeFromParent();
94
95   /// eraseFromParent - This method unlinks 'this' from the containing basic
96   /// block and deletes it.
97   ///
98   void eraseFromParent();
99
100   /// insertBefore - Insert an unlinked instructions into a basic block
101   /// immediately before the specified instruction.
102   void insertBefore(Instruction *InsertPos);
103
104   /// insertAfter - Insert an unlinked instructions into a basic block
105   /// immediately after the specified instruction.
106   void insertAfter(Instruction *InsertPos);
107
108   /// moveBefore - Unlink this instruction from its current basic block and
109   /// insert it into the basic block that MovePos lives in, right before
110   /// MovePos.
111   void moveBefore(Instruction *MovePos);
112
113   // ---------------------------------------------------------------------------
114   /// Subclass classification... getOpcode() returns a member of
115   /// one of the enums that is coming soon (down below)...
116   ///
117   unsigned getOpcode() const { return getValueID() - InstructionVal; }
118   const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
119   bool isTerminator() const { return isTerminator(getOpcode()); }
120   bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
121   bool isShift() { return isShift(getOpcode()); }
122   bool isCast() const { return isCast(getOpcode()); }
123   
124   
125   
126   static const char* getOpcodeName(unsigned OpCode);
127
128   static inline bool isTerminator(unsigned OpCode) {
129     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
130   }
131
132   static inline bool isBinaryOp(unsigned Opcode) {
133     return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
134   }
135
136   /// @brief Determine if the Opcode is one of the shift instructions.
137   static inline bool isShift(unsigned Opcode) {
138     return Opcode >= Shl && Opcode <= AShr;
139   }
140
141   /// isLogicalShift - Return true if this is a logical shift left or a logical
142   /// shift right.
143   inline bool isLogicalShift() const {
144     return getOpcode() == Shl || getOpcode() == LShr;
145   }
146
147   /// isArithmeticShift - Return true if this is an arithmetic shift right.
148   inline bool isArithmeticShift() const {
149     return getOpcode() == AShr;
150   }
151
152   /// @brief Determine if the OpCode is one of the CastInst instructions.
153   static inline bool isCast(unsigned OpCode) {
154     return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
155   }
156
157   /// isAssociative - Return true if the instruction is associative:
158   ///
159   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
160   ///
161   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
162   /// not applied to floating point types.
163   ///
164   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
165   static bool isAssociative(unsigned op, const Type *Ty);
166
167   /// isCommutative - Return true if the instruction is commutative:
168   ///
169   ///   Commutative operators satisfy: (x op y) === (y op x)
170   ///
171   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
172   /// applied to any type.
173   ///
174   bool isCommutative() const { return isCommutative(getOpcode()); }
175   static bool isCommutative(unsigned op);
176
177   /// mayWriteToMemory - Return true if this instruction may modify memory.
178   ///
179   bool mayWriteToMemory() const;
180
181   /// mayReadFromMemory - Return true if this instruction may read memory.
182   ///
183   bool mayReadFromMemory() const;
184
185   /// mayThrow - Return true if this instruction may throw an exception.
186   ///
187   bool mayThrow() const;
188
189   /// mayHaveSideEffects - Return true if the instruction may have side effects.
190   ///
191   /// Note that this does not consider malloc and alloca to have side
192   /// effects because the newly allocated memory is completely invisible to
193   /// instructions which don't used the returned value.  For cases where this
194   /// matters, isSafeToSpeculativelyExecute may be more appropriate.
195   bool mayHaveSideEffects() const {
196     return mayWriteToMemory() || mayThrow();
197   }
198
199   /// isSafeToSpeculativelyExecute - Return true if the instruction does not
200   /// have any effects besides calculating the result and does not have
201   /// undefined behavior.
202   ///
203   /// This method never returns true for an instruction that returns true for
204   /// mayHaveSideEffects; however, this method also does some other checks in
205   /// addition. It checks for undefined behavior, like dividing by zero or
206   /// loading from an invalid pointer (but not for undefined results, like a
207   /// shift with a shift amount larger than the width of the result). It checks
208   /// for malloc and alloca because speculatively executing them might cause a
209   /// memory leak. It also returns false for instructions related to control
210   /// flow, specifically terminators and PHI nodes.
211   ///
212   /// This method only looks at the instruction itself and its operands, so if
213   /// this method returns true, it is safe to move the instruction as long as
214   /// the correct dominance relationships for the operands and users hold.
215   /// However, this method can return true for instructions that read memory;
216   /// for such instructions, moving them may change the resulting value.
217   bool isSafeToSpeculativelyExecute() const;
218
219   /// Methods for support type inquiry through isa, cast, and dyn_cast:
220   static inline bool classof(const Instruction *) { return true; }
221   static inline bool classof(const Value *V) {
222     return V->getValueID() >= Value::InstructionVal;
223   }
224
225   //----------------------------------------------------------------------
226   // Exported enumerations...
227   //
228   enum TermOps {       // These terminate basic blocks
229 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
230 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
231 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
232 #include "llvm/Instruction.def"
233   };
234
235   enum BinaryOps {
236 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
237 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
238 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
239 #include "llvm/Instruction.def"
240   };
241
242   enum MemoryOps {
243 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
244 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
245 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
246 #include "llvm/Instruction.def"
247   };
248
249   enum CastOps {
250 #define  FIRST_CAST_INST(N)             CastOpsBegin = N,
251 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
252 #define   LAST_CAST_INST(N)             CastOpsEnd = N+1
253 #include "llvm/Instruction.def"
254   };
255
256   enum OtherOps {
257 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
258 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
259 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
260 #include "llvm/Instruction.def"
261   };
262 };
263
264 // Instruction* is only 4-byte aligned.
265 template<>
266 class PointerLikeTypeTraits<Instruction*> {
267   typedef Instruction* PT;
268 public:
269   static inline void *getAsVoidPointer(PT P) { return P; }
270   static inline PT getFromVoidPointer(void *P) {
271     return static_cast<PT>(P);
272   }
273   enum { NumLowBitsAvailable = 2 };
274 };
275   
276 } // End llvm namespace
277
278 #endif