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