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