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