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