LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Instruction.h
Go to the documentation of this file.
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_IR_INSTRUCTION_H
16 #define LLVM_IR_INSTRUCTION_H
17 
18 #include "llvm/ADT/ilist_node.h"
19 #include "llvm/IR/User.h"
20 #include "llvm/Support/DebugLoc.h"
21 
22 namespace llvm {
23 
24 class FastMathFlags;
25 class LLVMContext;
26 class MDNode;
27 
28 template<typename ValueSubClass, typename ItemParentClass>
29  class SymbolTableListTraits;
30 
31 class Instruction : public User, public ilist_node<Instruction> {
32  void operator=(const Instruction &) LLVM_DELETED_FUNCTION;
34 
35  BasicBlock *Parent;
36  DebugLoc DbgLoc; // 'dbg' Metadata cache.
37 
38  enum {
39  /// HasMetadataBit - This is a bit stored in the SubClassData field which
40  /// indicates whether this instruction has metadata attached to it or not.
41  HasMetadataBit = 1 << 15
42  };
43 public:
44  // Out of line virtual method, so the vtable, etc has a home.
45  ~Instruction();
46 
47  /// use_back - Specialize the methods defined in Value, as we know that an
48  /// instruction can only be used by other instructions.
49  Instruction *use_back() { return cast<Instruction>(*use_begin());}
50  const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
51 
52  inline const BasicBlock *getParent() const { return Parent; }
53  inline BasicBlock *getParent() { return Parent; }
54 
55  /// removeFromParent - This method unlinks 'this' from the containing basic
56  /// block, but does not delete it.
57  ///
58  void removeFromParent();
59 
60  /// eraseFromParent - This method unlinks 'this' from the containing basic
61  /// block and deletes it.
62  ///
63  void eraseFromParent();
64 
65  /// insertBefore - Insert an unlinked instructions into a basic block
66  /// immediately before the specified instruction.
67  void insertBefore(Instruction *InsertPos);
68 
69  /// insertAfter - Insert an unlinked instructions into a basic block
70  /// immediately after the specified instruction.
71  void insertAfter(Instruction *InsertPos);
72 
73  /// moveBefore - Unlink this instruction from its current basic block and
74  /// insert it into the basic block that MovePos lives in, right before
75  /// MovePos.
76  void moveBefore(Instruction *MovePos);
77 
78  //===--------------------------------------------------------------------===//
79  // Subclass classification.
80  //===--------------------------------------------------------------------===//
81 
82  /// getOpcode() returns a member of one of the enums like Instruction::Add.
83  unsigned getOpcode() const { return getValueID() - InstructionVal; }
84 
85  const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
86  bool isTerminator() const { return isTerminator(getOpcode()); }
87  bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
88  bool isShift() { return isShift(getOpcode()); }
89  bool isCast() const { return isCast(getOpcode()); }
90 
91  static const char* getOpcodeName(unsigned OpCode);
92 
93  static inline bool isTerminator(unsigned OpCode) {
94  return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
95  }
96 
97  static inline bool isBinaryOp(unsigned Opcode) {
98  return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
99  }
100 
101  /// @brief Determine if the Opcode is one of the shift instructions.
102  static inline bool isShift(unsigned Opcode) {
103  return Opcode >= Shl && Opcode <= AShr;
104  }
105 
106  /// isLogicalShift - Return true if this is a logical shift left or a logical
107  /// shift right.
108  inline bool isLogicalShift() const {
109  return getOpcode() == Shl || getOpcode() == LShr;
110  }
111 
112  /// isArithmeticShift - Return true if this is an arithmetic shift right.
113  inline bool isArithmeticShift() const {
114  return getOpcode() == AShr;
115  }
116 
117  /// @brief Determine if the OpCode is one of the CastInst instructions.
118  static inline bool isCast(unsigned OpCode) {
119  return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
120  }
121 
122  //===--------------------------------------------------------------------===//
123  // Metadata manipulation.
124  //===--------------------------------------------------------------------===//
125 
126  /// hasMetadata() - Return true if this instruction has any metadata attached
127  /// to it.
128  bool hasMetadata() const {
129  return !DbgLoc.isUnknown() || hasMetadataHashEntry();
130  }
131 
132  /// hasMetadataOtherThanDebugLoc - Return true if this instruction has
133  /// metadata attached to it other than a debug location.
135  return hasMetadataHashEntry();
136  }
137 
138  /// getMetadata - Get the metadata of given kind attached to this Instruction.
139  /// If the metadata is not found then return null.
140  MDNode *getMetadata(unsigned KindID) const {
141  if (!hasMetadata()) return 0;
142  return getMetadataImpl(KindID);
143  }
144 
145  /// getMetadata - Get the metadata of given kind attached to this Instruction.
146  /// If the metadata is not found then return null.
148  if (!hasMetadata()) return 0;
149  return getMetadataImpl(Kind);
150  }
151 
152  /// getAllMetadata - Get all metadata attached to this Instruction. The first
153  /// element of each pair returned is the KindID, the second element is the
154  /// metadata value. This list is returned sorted by the KindID.
155  void getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs)const{
156  if (hasMetadata())
157  getAllMetadataImpl(MDs);
158  }
159 
160  /// getAllMetadataOtherThanDebugLoc - This does the same thing as
161  /// getAllMetadata, except that it filters out the debug location.
163  MDNode*> > &MDs) const {
165  getAllMetadataOtherThanDebugLocImpl(MDs);
166  }
167 
168  /// setMetadata - Set the metadata of the specified kind to the specified
169  /// node. This updates/replaces metadata if already present, or removes it if
170  /// Node is null.
171  void setMetadata(unsigned KindID, MDNode *Node);
172  void setMetadata(StringRef Kind, MDNode *Node);
173 
174  /// setDebugLoc - Set the debug location information for this instruction.
175  void setDebugLoc(const DebugLoc &Loc) { DbgLoc = Loc; }
176 
177  /// getDebugLoc - Return the debug location for this node as a DebugLoc.
178  const DebugLoc &getDebugLoc() const { return DbgLoc; }
179 
180  /// Set or clear the unsafe-algebra flag on this instruction, which must be an
181  /// operator which supports this flag. See LangRef.html for the meaning of
182  /// this flag.
183  void setHasUnsafeAlgebra(bool B);
184 
185  /// Set or clear the no-nans flag on this instruction, which must be an
186  /// operator which supports this flag. See LangRef.html for the meaning of
187  /// this flag.
188  void setHasNoNaNs(bool B);
189 
190  /// Set or clear the no-infs flag on this instruction, which must be an
191  /// operator which supports this flag. See LangRef.html for the meaning of
192  /// this flag.
193  void setHasNoInfs(bool B);
194 
195  /// Set or clear the no-signed-zeros flag on this instruction, which must be
196  /// an operator which supports this flag. See LangRef.html for the meaning of
197  /// this flag.
198  void setHasNoSignedZeros(bool B);
199 
200  /// Set or clear the allow-reciprocal flag on this instruction, which must be
201  /// an operator which supports this flag. See LangRef.html for the meaning of
202  /// this flag.
203  void setHasAllowReciprocal(bool B);
204 
205  /// Convenience function for setting all the fast-math flags on this
206  /// instruction, which must be an operator which supports these flags. See
207  /// LangRef.html for the meaning of these flats.
209 
210  /// Determine whether the unsafe-algebra flag is set.
211  bool hasUnsafeAlgebra() const;
212 
213  /// Determine whether the no-NaNs flag is set.
214  bool hasNoNaNs() const;
215 
216  /// Determine whether the no-infs flag is set.
217  bool hasNoInfs() const;
218 
219  /// Determine whether the no-signed-zeros flag is set.
220  bool hasNoSignedZeros() const;
221 
222  /// Determine whether the allow-reciprocal flag is set.
223  bool hasAllowReciprocal() const;
224 
225  /// Convenience function for getting all the fast-math flags, which must be an
226  /// operator which supports these flags. See LangRef.html for the meaning of
227  /// these flats.
229 
230  /// Copy I's fast-math flags
231  void copyFastMathFlags(const Instruction *I);
232 
233 private:
234  /// hasMetadataHashEntry - Return true if we have an entry in the on-the-side
235  /// metadata hash.
236  bool hasMetadataHashEntry() const {
237  return (getSubclassDataFromValue() & HasMetadataBit) != 0;
238  }
239 
240  // These are all implemented in Metadata.cpp.
241  MDNode *getMetadataImpl(unsigned KindID) const;
242  MDNode *getMetadataImpl(StringRef Kind) const;
243  void getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,MDNode*> > &)const;
244  void getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
245  MDNode*> > &) const;
246  void clearMetadataHashEntries();
247 public:
248  //===--------------------------------------------------------------------===//
249  // Predicates and helper methods.
250  //===--------------------------------------------------------------------===//
251 
252 
253  /// isAssociative - Return true if the instruction is associative:
254  ///
255  /// Associative operators satisfy: x op (y op z) === (x op y) op z
256  ///
257  /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
258  ///
259  bool isAssociative() const;
260  static bool isAssociative(unsigned op);
261 
262  /// isCommutative - Return true if the instruction is commutative:
263  ///
264  /// Commutative operators satisfy: (x op y) === (y op x)
265  ///
266  /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
267  /// applied to any type.
268  ///
269  bool isCommutative() const { return isCommutative(getOpcode()); }
270  static bool isCommutative(unsigned op);
271 
272  /// isIdempotent - Return true if the instruction is idempotent:
273  ///
274  /// Idempotent operators satisfy: x op x === x
275  ///
276  /// In LLVM, the And and Or operators are idempotent.
277  ///
278  bool isIdempotent() const { return isIdempotent(getOpcode()); }
279  static bool isIdempotent(unsigned op);
280 
281  /// isNilpotent - Return true if the instruction is nilpotent:
282  ///
283  /// Nilpotent operators satisfy: x op x === Id,
284  ///
285  /// where Id is the identity for the operator, i.e. a constant such that
286  /// x op Id === x and Id op x === x for all x.
287  ///
288  /// In LLVM, the Xor operator is nilpotent.
289  ///
290  bool isNilpotent() const { return isNilpotent(getOpcode()); }
291  static bool isNilpotent(unsigned op);
292 
293  /// mayWriteToMemory - Return true if this instruction may modify memory.
294  ///
295  bool mayWriteToMemory() const;
296 
297  /// mayReadFromMemory - Return true if this instruction may read memory.
298  ///
299  bool mayReadFromMemory() const;
300 
301  /// mayReadOrWriteMemory - Return true if this instruction may read or
302  /// write memory.
303  ///
304  bool mayReadOrWriteMemory() const {
305  return mayReadFromMemory() || mayWriteToMemory();
306  }
307 
308  /// mayThrow - Return true if this instruction may throw an exception.
309  ///
310  bool mayThrow() const;
311 
312  /// mayReturn - Return true if this is a function that may return.
313  /// this is true for all normal instructions. The only exception
314  /// is functions that are marked with the 'noreturn' attribute.
315  ///
316  bool mayReturn() const;
317 
318  /// mayHaveSideEffects - Return true if the instruction may have side effects.
319  ///
320  /// Note that this does not consider malloc and alloca to have side
321  /// effects because the newly allocated memory is completely invisible to
322  /// instructions which don't used the returned value. For cases where this
323  /// matters, isSafeToSpeculativelyExecute may be more appropriate.
324  bool mayHaveSideEffects() const {
325  return mayWriteToMemory() || mayThrow() || !mayReturn();
326  }
327 
328  /// clone() - Create a copy of 'this' instruction that is identical in all
329  /// ways except the following:
330  /// * The instruction has no parent
331  /// * The instruction has no name
332  ///
333  Instruction *clone() const;
334 
335  /// isIdenticalTo - Return true if the specified instruction is exactly
336  /// identical to the current one. This means that all operands match and any
337  /// extra information (e.g. load is volatile) agree.
338  bool isIdenticalTo(const Instruction *I) const;
339 
340  /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
341  /// ignores the SubclassOptionalData flags, which specify conditions
342  /// under which the instruction's result is undefined.
343  bool isIdenticalToWhenDefined(const Instruction *I) const;
344 
345  /// When checking for operation equivalence (using isSameOperationAs) it is
346  /// sometimes useful to ignore certain attributes.
348  /// Check for equivalence ignoring load/store alignment.
350  /// Check for equivalence treating a type and a vector of that type
351  /// as equivalent.
353  };
354 
355  /// This function determines if the specified instruction executes the same
356  /// operation as the current one. This means that the opcodes, type, operand
357  /// types and any other factors affecting the operation must be the same. This
358  /// is similar to isIdenticalTo except the operands themselves don't have to
359  /// be identical.
360  /// @returns true if the specified instruction is the same operation as
361  /// the current one.
362  /// @brief Determine if one instruction is the same operation as another.
363  bool isSameOperationAs(const Instruction *I, unsigned flags = 0) const;
364 
365  /// isUsedOutsideOfBlock - Return true if there are any uses of this
366  /// instruction in blocks other than the specified block. Note that PHI nodes
367  /// are considered to evaluate their operands in the corresponding predecessor
368  /// block.
369  bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
370 
371 
372  /// Methods for support type inquiry through isa, cast, and dyn_cast:
373  static inline bool classof(const Value *V) {
374  return V->getValueID() >= Value::InstructionVal;
375  }
376 
377  //----------------------------------------------------------------------
378  // Exported enumerations.
379  //
380  enum TermOps { // These terminate basic blocks
381 #define FIRST_TERM_INST(N) TermOpsBegin = N,
382 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
383 #define LAST_TERM_INST(N) TermOpsEnd = N+1
384 #include "llvm/IR/Instruction.def"
385  };
386 
387  enum BinaryOps {
388 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
389 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
390 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
391 #include "llvm/IR/Instruction.def"
392  };
393 
394  enum MemoryOps {
395 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
396 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
397 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
398 #include "llvm/IR/Instruction.def"
399  };
400 
401  enum CastOps {
402 #define FIRST_CAST_INST(N) CastOpsBegin = N,
403 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
404 #define LAST_CAST_INST(N) CastOpsEnd = N+1
405 #include "llvm/IR/Instruction.def"
406  };
407 
408  enum OtherOps {
409 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
410 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
411 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
412 #include "llvm/IR/Instruction.def"
413  };
414 private:
415  // Shadow Value::setValueSubclassData with a private forwarding method so that
416  // subclasses cannot accidentally use it.
417  void setValueSubclassData(unsigned short D) {
419  }
420  unsigned short getSubclassDataFromValue() const {
422  }
423 
424  void setHasMetadataHashEntry(bool V) {
425  setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
426  (V ? HasMetadataBit : 0));
427  }
428 
430  void setParent(BasicBlock *P);
431 protected:
432  // Instruction subclasses can stick up to 15 bits of stuff into the
433  // SubclassData field of instruction with these members.
434 
435  // Verify that only the low 15 bits are used.
436  void setInstructionSubclassData(unsigned short D) {
437  assert((D & HasMetadataBit) == 0 && "Out of range value put into field");
438  setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D);
439  }
440 
442  return getSubclassDataFromValue() & ~HasMetadataBit;
443  }
444 
445  Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
446  Instruction *InsertBefore = 0);
447  Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
448  BasicBlock *InsertAtEnd);
449  virtual Instruction *clone_impl() const = 0;
450 
451 };
452 
453 // Instruction* is only 4-byte aligned.
454 template<>
456  typedef Instruction* PT;
457 public:
458  static inline void *getAsVoidPointer(PT P) { return P; }
459  static inline PT getFromVoidPointer(void *P) {
460  return static_cast<PT>(P);
461  }
462  enum { NumLowBitsAvailable = 2 };
463 };
464 
465 } // End llvm namespace
466 
467 #endif
bool isArithmeticShift() const
isArithmeticShift - Return true if this is an arithmetic shift right.
Definition: Instruction.h:113
bool isNilpotent() const
Definition: Instruction.h:290
void setFastMathFlags(FastMathFlags FMF)
void setHasNoNaNs(bool B)
bool isUsedOutsideOfBlock(const BasicBlock *BB) const
bool mayHaveSideEffects() const
Definition: Instruction.h:324
FastMathFlags getFastMathFlags() const
MDNode - a tuple of other values.
Definition: Metadata.h:69
void setDebugLoc(const DebugLoc &Loc)
setDebugLoc - Set the debug location information for this instruction.
Definition: Instruction.h:175
void getAllMetadataOtherThanDebugLoc(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Definition: Instruction.h:162
bool isCast() const
Definition: Instruction.h:89
bool isUnknown() const
isUnknown - Return true if this is an unknown location.
Definition: DebugLoc.h:70
bool hasNoNaNs() const
Determine whether the no-NaNs flag is set.
bool mayReturn() const
static bool isShift(unsigned Opcode)
Determine if the Opcode is one of the shift instructions.
Definition: Instruction.h:102
unsigned getSubclassDataFromInstruction() const
Definition: Instruction.h:441
bool isIdenticalTo(const Instruction *I) const
Definition: Use.h:60
bool hasAllowReciprocal() const
Determine whether the allow-reciprocal flag is set.
Check for equivalence ignoring load/store alignment.
Definition: Instruction.h:349
Instruction * clone() const
bool mayReadFromMemory() const
bool isAssociative() const
void setHasUnsafeAlgebra(bool B)
Definition: Instruction.cpp:99
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
static bool isBinaryOp(unsigned Opcode)
Definition: Instruction.h:97
bool isLogicalShift() const
Definition: Instruction.h:108
void setInstructionSubclassData(unsigned short D)
Definition: Instruction.h:436
const char * getOpcodeName() const
Definition: Instruction.h:85
BasicBlock * getParent()
Definition: Instruction.h:53
bool mayReadOrWriteMemory() const
Definition: Instruction.h:304
static bool isTerminator(unsigned OpCode)
Definition: Instruction.h:93
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Definition: Instruction.h:155
#define P(N)
bool isIdenticalToWhenDefined(const Instruction *I) const
void insertBefore(Instruction *InsertPos)
Definition: Instruction.cpp:78
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
bool hasMetadata() const
Definition: Instruction.h:128
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:178
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Instruction.h:373
unsigned getValueID() const
Definition: Value.h:233
bool isCommutative() const
Definition: Instruction.h:269
void setHasNoInfs(bool B)
void setMetadata(unsigned KindID, MDNode *Node)
Definition: Metadata.cpp:589
bool mayWriteToMemory() const
bool isTerminator() const
Definition: Instruction.h:86
MDNode * getMetadata(StringRef Kind) const
Definition: Instruction.h:147
MDNode * getMetadata(unsigned KindID) const
Definition: Instruction.h:140
static bool isCast(unsigned OpCode)
Determine if the OpCode is one of the CastInst instructions.
Definition: Instruction.h:118
void copyFastMathFlags(const Instruction *I)
Copy I's fast-math flags.
void setValueSubclassData(unsigned short D)
Definition: Value.h:348
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
Instruction * use_back()
Definition: Instruction.h:49
const Instruction * use_back() const
Definition: Instruction.h:50
bool mayThrow() const
use_iterator use_begin()
Definition: Value.h:150
unsigned short getSubclassDataFromValue() const
Definition: Value.h:347
bool isBinaryOp() const
Definition: Instruction.h:87
bool isIdempotent() const
Definition: Instruction.h:278
void insertAfter(Instruction *InsertPos)
Definition: Instruction.cpp:84
#define I(x, y, z)
Definition: MD5.cpp:54
bool hasNoInfs() const
Determine whether the no-infs flag is set.
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
bool hasMetadataOtherThanDebugLoc() const
Definition: Instruction.h:134
virtual Instruction * clone_impl() const =0
LLVM Value Representation.
Definition: Value.h:66
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:83
void moveBefore(Instruction *MovePos)
Definition: Instruction.cpp:91
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:170
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
Determine if one instruction is the same operation as another.
void setHasNoSignedZeros(bool B)
void setHasAllowReciprocal(bool B)
const BasicBlock * getParent() const
Definition: Instruction.h:52