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