LLVM  8.0.0svn
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/ArrayRef.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/ilist_node.h"
22 #include "llvm/IR/DebugLoc.h"
24 #include "llvm/IR/User.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Support/Casting.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <cstdint>
30 #include <utility>
31 
32 namespace llvm {
33 
34 class BasicBlock;
35 class FastMathFlags;
36 class MDNode;
37 class Module;
38 struct AAMDNodes;
39 
40 template <> struct ilist_alloc_traits<Instruction> {
41  static inline void deleteNode(Instruction *V);
42 };
43 
44 class Instruction : public User,
45  public ilist_node_with_parent<Instruction, BasicBlock> {
46  BasicBlock *Parent;
47  DebugLoc DbgLoc; // 'dbg' Metadata cache.
48 
49  enum {
50  /// This is a bit stored in the SubClassData field which indicates whether
51  /// this instruction has metadata attached to it or not.
52  HasMetadataBit = 1 << 15
53  };
54 
55 protected:
56  ~Instruction(); // Use deleteValue() to delete a generic Instruction.
57 
58 public:
59  Instruction(const Instruction &) = delete;
60  Instruction &operator=(const Instruction &) = delete;
61 
62  /// Specialize the methods defined in Value, as we know that an instruction
63  /// can only be used by other instructions.
64  Instruction *user_back() { return cast<Instruction>(*user_begin());}
65  const Instruction *user_back() const { return cast<Instruction>(*user_begin());}
66 
67  inline const BasicBlock *getParent() const { return Parent; }
68  inline BasicBlock *getParent() { return Parent; }
69 
70  /// Return the module owning the function this instruction belongs to
71  /// or nullptr it the function does not have a module.
72  ///
73  /// Note: this is undefined behavior if the instruction does not have a
74  /// parent, or the parent basic block does not have a parent function.
75  const Module *getModule() const;
77  return const_cast<Module *>(
78  static_cast<const Instruction *>(this)->getModule());
79  }
80 
81  /// Return the function this instruction belongs to.
82  ///
83  /// Note: it is undefined behavior to call this on an instruction not
84  /// currently inserted into a function.
85  const Function *getFunction() const;
87  return const_cast<Function *>(
88  static_cast<const Instruction *>(this)->getFunction());
89  }
90 
91  /// This method unlinks 'this' from the containing basic block, but does not
92  /// delete it.
93  void removeFromParent();
94 
95  /// This method unlinks 'this' from the containing basic block and deletes it.
96  ///
97  /// \returns an iterator pointing to the element after the erased one
99 
100  /// Insert an unlinked instruction into a basic block immediately before
101  /// the specified instruction.
102  void insertBefore(Instruction *InsertPos);
103 
104  /// Insert an unlinked instruction into a basic block immediately after the
105  /// specified instruction.
106  void insertAfter(Instruction *InsertPos);
107 
108  /// Unlink this instruction from its current basic block and insert it into
109  /// the basic block that MovePos lives in, right before MovePos.
110  void moveBefore(Instruction *MovePos);
111 
112  /// Unlink this instruction and insert into BB before I.
113  ///
114  /// \pre I is a valid iterator into BB.
116 
117  /// Unlink this instruction from its current basic block and insert it into
118  /// the basic block that MovePos lives in, right after MovePos.
119  void moveAfter(Instruction *MovePos);
120 
121  //===--------------------------------------------------------------------===//
122  // Subclass classification.
123  //===--------------------------------------------------------------------===//
124 
125  /// Returns a member of one of the enums like Instruction::Add.
126  unsigned getOpcode() const { return getValueID() - InstructionVal; }
127 
128  const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
129  bool isTerminator() const { return isTerminator(getOpcode()); }
130  bool isUnaryOp() const { return isUnaryOp(getOpcode()); }
131  bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
132  bool isIntDivRem() const { return isIntDivRem(getOpcode()); }
133  bool isShift() { return isShift(getOpcode()); }
134  bool isCast() const { return isCast(getOpcode()); }
135  bool isFuncletPad() const { return isFuncletPad(getOpcode()); }
136  bool isExceptionalTerminator() const {
137  return isExceptionalTerminator(getOpcode());
138  }
139 
140  static const char* getOpcodeName(unsigned OpCode);
141 
142  static inline bool isTerminator(unsigned OpCode) {
143  return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
144  }
145 
146  static inline bool isUnaryOp(unsigned Opcode) {
147  return Opcode >= UnaryOpsBegin && Opcode < UnaryOpsEnd;
148  }
149  static inline bool isBinaryOp(unsigned Opcode) {
150  return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
151  }
152 
153  static inline bool isIntDivRem(unsigned Opcode) {
154  return Opcode == UDiv || Opcode == SDiv || Opcode == URem || Opcode == SRem;
155  }
156 
157  /// Determine if the Opcode is one of the shift instructions.
158  static inline bool isShift(unsigned Opcode) {
159  return Opcode >= Shl && Opcode <= AShr;
160  }
161 
162  /// Return true if this is a logical shift left or a logical shift right.
163  inline bool isLogicalShift() const {
164  return getOpcode() == Shl || getOpcode() == LShr;
165  }
166 
167  /// Return true if this is an arithmetic shift right.
168  inline bool isArithmeticShift() const {
169  return getOpcode() == AShr;
170  }
171 
172  /// Determine if the Opcode is and/or/xor.
173  static inline bool isBitwiseLogicOp(unsigned Opcode) {
174  return Opcode == And || Opcode == Or || Opcode == Xor;
175  }
176 
177  /// Return true if this is and/or/xor.
178  inline bool isBitwiseLogicOp() const {
179  return isBitwiseLogicOp(getOpcode());
180  }
181 
182  /// Determine if the OpCode is one of the CastInst instructions.
183  static inline bool isCast(unsigned OpCode) {
184  return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
185  }
186 
187  /// Determine if the OpCode is one of the FuncletPadInst instructions.
188  static inline bool isFuncletPad(unsigned OpCode) {
189  return OpCode >= FuncletPadOpsBegin && OpCode < FuncletPadOpsEnd;
190  }
191 
192  /// Returns true if the OpCode is a terminator related to exception handling.
193  static inline bool isExceptionalTerminator(unsigned OpCode) {
194  switch (OpCode) {
195  case Instruction::CatchSwitch:
196  case Instruction::CatchRet:
197  case Instruction::CleanupRet:
198  case Instruction::Invoke:
199  case Instruction::Resume:
200  return true;
201  default:
202  return false;
203  }
204  }
205 
206  //===--------------------------------------------------------------------===//
207  // Metadata manipulation.
208  //===--------------------------------------------------------------------===//
209 
210  /// Return true if this instruction has any metadata attached to it.
211  bool hasMetadata() const { return DbgLoc || hasMetadataHashEntry(); }
212 
213  /// Return true if this instruction has metadata attached to it other than a
214  /// debug location.
216  return hasMetadataHashEntry();
217  }
218 
219  /// Get the metadata of given kind attached to this Instruction.
220  /// If the metadata is not found then return null.
221  MDNode *getMetadata(unsigned KindID) const {
222  if (!hasMetadata()) return nullptr;
223  return getMetadataImpl(KindID);
224  }
225 
226  /// Get the metadata of given kind attached to this Instruction.
227  /// If the metadata is not found then return null.
229  if (!hasMetadata()) return nullptr;
230  return getMetadataImpl(Kind);
231  }
232 
233  /// Get all metadata attached to this Instruction. The first element of each
234  /// pair returned is the KindID, the second element is the metadata value.
235  /// This list is returned sorted by the KindID.
236  void
237  getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
238  if (hasMetadata())
239  getAllMetadataImpl(MDs);
240  }
241 
242  /// This does the same thing as getAllMetadata, except that it filters out the
243  /// debug location.
245  SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
247  getAllMetadataOtherThanDebugLocImpl(MDs);
248  }
249 
250  /// Fills the AAMDNodes structure with AA metadata from this instruction.
251  /// When Merge is true, the existing AA metadata is merged with that from this
252  /// instruction providing the most-general result.
253  void getAAMetadata(AAMDNodes &N, bool Merge = false) const;
254 
255  /// Set the metadata of the specified kind to the specified node. This updates
256  /// or replaces metadata if already present, or removes it if Node is null.
257  void setMetadata(unsigned KindID, MDNode *Node);
258  void setMetadata(StringRef Kind, MDNode *Node);
259 
260  /// Copy metadata from \p SrcInst to this instruction. \p WL, if not empty,
261  /// specifies the list of meta data that needs to be copied. If \p WL is
262  /// empty, all meta data will be copied.
263  void copyMetadata(const Instruction &SrcInst,
265 
266  /// If the instruction has "branch_weights" MD_prof metadata and the MDNode
267  /// has three operands (including name string), swap the order of the
268  /// metadata.
269  void swapProfMetadata();
270 
271  /// Drop all unknown metadata except for debug locations.
272  /// @{
273  /// Passes are required to drop metadata they don't understand. This is a
274  /// convenience method for passes to do so.
275  void dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs);
277  return dropUnknownNonDebugMetadata(None);
278  }
279  void dropUnknownNonDebugMetadata(unsigned ID1) {
280  return dropUnknownNonDebugMetadata(makeArrayRef(ID1));
281  }
282  void dropUnknownNonDebugMetadata(unsigned ID1, unsigned ID2) {
283  unsigned IDs[] = {ID1, ID2};
284  return dropUnknownNonDebugMetadata(IDs);
285  }
286  /// @}
287 
288  /// Sets the metadata on this instruction from the AAMDNodes structure.
289  void setAAMetadata(const AAMDNodes &N);
290 
291  /// Retrieve the raw weight values of a conditional branch or select.
292  /// Returns true on success with profile weights filled in.
293  /// Returns false if no metadata or invalid metadata was found.
294  bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const;
295 
296  /// Retrieve total raw weight values of a branch.
297  /// Returns true on success with profile total weights filled in.
298  /// Returns false if no metadata was found.
299  bool extractProfTotalWeight(uint64_t &TotalVal) const;
300 
301  /// Updates branch_weights metadata by scaling it by \p S / \p T.
302  void updateProfWeight(uint64_t S, uint64_t T);
303 
304  /// Sets the branch_weights metadata to \p W for CallInst.
305  void setProfWeight(uint64_t W);
306 
307  /// Set the debug location information for this instruction.
308  void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); }
309 
310  /// Return the debug location for this node as a DebugLoc.
311  const DebugLoc &getDebugLoc() const { return DbgLoc; }
312 
313  /// Set or clear the nuw flag on this instruction, which must be an operator
314  /// which supports this flag. See LangRef.html for the meaning of this flag.
315  void setHasNoUnsignedWrap(bool b = true);
316 
317  /// Set or clear the nsw flag on this instruction, which must be an operator
318  /// which supports this flag. See LangRef.html for the meaning of this flag.
319  void setHasNoSignedWrap(bool b = true);
320 
321  /// Set or clear the exact flag on this instruction, which must be an operator
322  /// which supports this flag. See LangRef.html for the meaning of this flag.
323  void setIsExact(bool b = true);
324 
325  /// Determine whether the no unsigned wrap flag is set.
326  bool hasNoUnsignedWrap() const;
327 
328  /// Determine whether the no signed wrap flag is set.
329  bool hasNoSignedWrap() const;
330 
331  /// Drops flags that may cause this instruction to evaluate to poison despite
332  /// having non-poison inputs.
333  void dropPoisonGeneratingFlags();
334 
335  /// Determine whether the exact flag is set.
336  bool isExact() const;
337 
338  /// Set or clear all fast-math-flags on this instruction, which must be an
339  /// operator which supports this flag. See LangRef.html for the meaning of
340  /// this flag.
341  void setFast(bool B);
342 
343  /// Set or clear the reassociation flag on this instruction, which must be
344  /// an operator which supports this flag. See LangRef.html for the meaning of
345  /// this flag.
346  void setHasAllowReassoc(bool B);
347 
348  /// Set or clear the no-nans flag on this instruction, which must be an
349  /// operator which supports this flag. See LangRef.html for the meaning of
350  /// this flag.
351  void setHasNoNaNs(bool B);
352 
353  /// Set or clear the no-infs flag on this instruction, which must be an
354  /// operator which supports this flag. See LangRef.html for the meaning of
355  /// this flag.
356  void setHasNoInfs(bool B);
357 
358  /// Set or clear the no-signed-zeros flag on this instruction, which must be
359  /// an operator which supports this flag. See LangRef.html for the meaning of
360  /// this flag.
361  void setHasNoSignedZeros(bool B);
362 
363  /// Set or clear the allow-reciprocal flag on this instruction, which must be
364  /// an operator which supports this flag. See LangRef.html for the meaning of
365  /// this flag.
366  void setHasAllowReciprocal(bool B);
367 
368  /// Set or clear the approximate-math-functions flag on this instruction,
369  /// which must be an operator which supports this flag. See LangRef.html for
370  /// the meaning of this flag.
371  void setHasApproxFunc(bool B);
372 
373  /// Convenience function for setting multiple fast-math flags on this
374  /// instruction, which must be an operator which supports these flags. See
375  /// LangRef.html for the meaning of these flags.
376  void setFastMathFlags(FastMathFlags FMF);
377 
378  /// Convenience function for transferring all fast-math flag values to this
379  /// instruction, which must be an operator which supports these flags. See
380  /// LangRef.html for the meaning of these flags.
381  void copyFastMathFlags(FastMathFlags FMF);
382 
383  /// Determine whether all fast-math-flags are set.
384  bool isFast() const;
385 
386  /// Determine whether the allow-reassociation flag is set.
387  bool hasAllowReassoc() const;
388 
389  /// Determine whether the no-NaNs flag is set.
390  bool hasNoNaNs() const;
391 
392  /// Determine whether the no-infs flag is set.
393  bool hasNoInfs() const;
394 
395  /// Determine whether the no-signed-zeros flag is set.
396  bool hasNoSignedZeros() const;
397 
398  /// Determine whether the allow-reciprocal flag is set.
399  bool hasAllowReciprocal() const;
400 
401  /// Determine whether the allow-contract flag is set.
402  bool hasAllowContract() const;
403 
404  /// Determine whether the approximate-math-functions flag is set.
405  bool hasApproxFunc() const;
406 
407  /// Convenience function for getting all the fast-math flags, which must be an
408  /// operator which supports these flags. See LangRef.html for the meaning of
409  /// these flags.
410  FastMathFlags getFastMathFlags() const;
411 
412  /// Copy I's fast-math flags
413  void copyFastMathFlags(const Instruction *I);
414 
415  /// Convenience method to copy supported exact, fast-math, and (optionally)
416  /// wrapping flags from V to this instruction.
417  void copyIRFlags(const Value *V, bool IncludeWrapFlags = true);
418 
419  /// Logical 'and' of any supported wrapping, exact, and fast-math flags of
420  /// V and this instruction.
421  void andIRFlags(const Value *V);
422 
423  /// Merge 2 debug locations and apply it to the Instruction. If the
424  /// instruction is a CallIns, we need to traverse the inline chain to find
425  /// the common scope. This is not efficient for N-way merging as each time
426  /// you merge 2 iterations, you need to rebuild the hashmap to find the
427  /// common scope. However, we still choose this API because:
428  /// 1) Simplicity: it takes 2 locations instead of a list of locations.
429  /// 2) In worst case, it increases the complexity from O(N*I) to
430  /// O(2*N*I), where N is # of Instructions to merge, and I is the
431  /// maximum level of inline stack. So it is still linear.
432  /// 3) Merging of call instructions should be extremely rare in real
433  /// applications, thus the N-way merging should be in code path.
434  /// The DebugLoc attached to this instruction will be overwritten by the
435  /// merged DebugLoc.
436  void applyMergedLocation(const DILocation *LocA, const DILocation *LocB);
437 
438 private:
439  /// Return true if we have an entry in the on-the-side metadata hash.
440  bool hasMetadataHashEntry() const {
441  return (getSubclassDataFromValue() & HasMetadataBit) != 0;
442  }
443 
444  // These are all implemented in Metadata.cpp.
445  MDNode *getMetadataImpl(unsigned KindID) const;
446  MDNode *getMetadataImpl(StringRef Kind) const;
447  void
448  getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const;
449  void getAllMetadataOtherThanDebugLocImpl(
450  SmallVectorImpl<std::pair<unsigned, MDNode *>> &) const;
451  /// Clear all hashtable-based metadata from this instruction.
452  void clearMetadataHashEntries();
453 
454 public:
455  //===--------------------------------------------------------------------===//
456  // Predicates and helper methods.
457  //===--------------------------------------------------------------------===//
458 
459  /// Return true if the instruction is associative:
460  ///
461  /// Associative operators satisfy: x op (y op z) === (x op y) op z
462  ///
463  /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
464  ///
465  bool isAssociative() const LLVM_READONLY;
466  static bool isAssociative(unsigned Opcode) {
467  return Opcode == And || Opcode == Or || Opcode == Xor ||
468  Opcode == Add || Opcode == Mul;
469  }
470 
471  /// Return true if the instruction is commutative:
472  ///
473  /// Commutative operators satisfy: (x op y) === (y op x)
474  ///
475  /// In LLVM, these are the commutative operators, plus SetEQ and SetNE, when
476  /// applied to any type.
477  ///
478  bool isCommutative() const { return isCommutative(getOpcode()); }
479  static bool isCommutative(unsigned Opcode) {
480  switch (Opcode) {
481  case Add: case FAdd:
482  case Mul: case FMul:
483  case And: case Or: case Xor:
484  return true;
485  default:
486  return false;
487  }
488  }
489 
490  /// Return true if the instruction is idempotent:
491  ///
492  /// Idempotent operators satisfy: x op x === x
493  ///
494  /// In LLVM, the And and Or operators are idempotent.
495  ///
496  bool isIdempotent() const { return isIdempotent(getOpcode()); }
497  static bool isIdempotent(unsigned Opcode) {
498  return Opcode == And || Opcode == Or;
499  }
500 
501  /// Return true if the instruction is nilpotent:
502  ///
503  /// Nilpotent operators satisfy: x op x === Id,
504  ///
505  /// where Id is the identity for the operator, i.e. a constant such that
506  /// x op Id === x and Id op x === x for all x.
507  ///
508  /// In LLVM, the Xor operator is nilpotent.
509  ///
510  bool isNilpotent() const { return isNilpotent(getOpcode()); }
511  static bool isNilpotent(unsigned Opcode) {
512  return Opcode == Xor;
513  }
514 
515  /// Return true if this instruction may modify memory.
516  bool mayWriteToMemory() const;
517 
518  /// Return true if this instruction may read memory.
519  bool mayReadFromMemory() const;
520 
521  /// Return true if this instruction may read or write memory.
522  bool mayReadOrWriteMemory() const {
523  return mayReadFromMemory() || mayWriteToMemory();
524  }
525 
526  /// Return true if this instruction has an AtomicOrdering of unordered or
527  /// higher.
528  bool isAtomic() const;
529 
530  /// Return true if this atomic instruction loads from memory.
531  bool hasAtomicLoad() const;
532 
533  /// Return true if this atomic instruction stores to memory.
534  bool hasAtomicStore() const;
535 
536  /// Return true if this instruction may throw an exception.
537  bool mayThrow() const;
538 
539  /// Return true if this instruction behaves like a memory fence: it can load
540  /// or store to memory location without being given a memory location.
541  bool isFenceLike() const {
542  switch (getOpcode()) {
543  default:
544  return false;
545  // This list should be kept in sync with the list in mayWriteToMemory for
546  // all opcodes which don't have a memory location.
547  case Instruction::Fence:
548  case Instruction::CatchPad:
549  case Instruction::CatchRet:
550  case Instruction::Call:
551  case Instruction::Invoke:
552  return true;
553  }
554  }
555 
556  /// Return true if the instruction may have side effects.
557  ///
558  /// Note that this does not consider malloc and alloca to have side
559  /// effects because the newly allocated memory is completely invisible to
560  /// instructions which don't use the returned value. For cases where this
561  /// matters, isSafeToSpeculativelyExecute may be more appropriate.
562  bool mayHaveSideEffects() const { return mayWriteToMemory() || mayThrow(); }
563 
564  /// Return true if the instruction can be removed if the result is unused.
565  ///
566  /// When constant folding some instructions cannot be removed even if their
567  /// results are unused. Specifically terminator instructions and calls that
568  /// may have side effects cannot be removed without semantically changing the
569  /// generated program.
570  bool isSafeToRemove() const;
571 
572  /// Return true if the instruction is a variety of EH-block.
573  bool isEHPad() const {
574  switch (getOpcode()) {
575  case Instruction::CatchSwitch:
576  case Instruction::CatchPad:
577  case Instruction::CleanupPad:
578  case Instruction::LandingPad:
579  return true;
580  default:
581  return false;
582  }
583  }
584 
585  /// Return a pointer to the next non-debug instruction in the same basic
586  /// block as 'this', or nullptr if no such instruction exists.
587  const Instruction *getNextNonDebugInstruction() const;
589  return const_cast<Instruction *>(
590  static_cast<const Instruction *>(this)->getNextNonDebugInstruction());
591  }
592 
593  /// Return a pointer to the previous non-debug instruction in the same basic
594  /// block as 'this', or nullptr if no such instruction exists.
595  const Instruction *getPrevNonDebugInstruction() const;
597  return const_cast<Instruction *>(
598  static_cast<const Instruction *>(this)->getPrevNonDebugInstruction());
599  }
600 
601  /// Create a copy of 'this' instruction that is identical in all ways except
602  /// the following:
603  /// * The instruction has no parent
604  /// * The instruction has no name
605  ///
606  Instruction *clone() const;
607 
608  /// Return true if the specified instruction is exactly identical to the
609  /// current one. This means that all operands match and any extra information
610  /// (e.g. load is volatile) agree.
611  bool isIdenticalTo(const Instruction *I) const;
612 
613  /// This is like isIdenticalTo, except that it ignores the
614  /// SubclassOptionalData flags, which may specify conditions under which the
615  /// instruction's result is undefined.
616  bool isIdenticalToWhenDefined(const Instruction *I) const;
617 
618  /// When checking for operation equivalence (using isSameOperationAs) it is
619  /// sometimes useful to ignore certain attributes.
621  /// Check for equivalence ignoring load/store alignment.
622  CompareIgnoringAlignment = 1<<0,
623  /// Check for equivalence treating a type and a vector of that type
624  /// as equivalent.
625  CompareUsingScalarTypes = 1<<1
626  };
627 
628  /// This function determines if the specified instruction executes the same
629  /// operation as the current one. This means that the opcodes, type, operand
630  /// types and any other factors affecting the operation must be the same. This
631  /// is similar to isIdenticalTo except the operands themselves don't have to
632  /// be identical.
633  /// @returns true if the specified instruction is the same operation as
634  /// the current one.
635  /// Determine if one instruction is the same operation as another.
636  bool isSameOperationAs(const Instruction *I, unsigned flags = 0) const;
637 
638  /// Return true if there are any uses of this instruction in blocks other than
639  /// the specified block. Note that PHI nodes are considered to evaluate their
640  /// operands in the corresponding predecessor block.
641  bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
642 
643  /// Return the number of successors that this instruction has. The instruction
644  /// must be a terminator.
645  unsigned getNumSuccessors() const;
646 
647  /// Return the specified successor. This instruction must be a terminator.
648  BasicBlock *getSuccessor(unsigned Idx) const;
649 
650  /// Update the specified successor to point at the provided block. This
651  /// instruction must be a terminator.
652  void setSuccessor(unsigned Idx, BasicBlock *BB);
653 
654  /// Methods for support type inquiry through isa, cast, and dyn_cast:
655  static bool classof(const Value *V) {
656  return V->getValueID() >= Value::InstructionVal;
657  }
658 
659  //----------------------------------------------------------------------
660  // Exported enumerations.
661  //
662  enum TermOps { // These terminate basic blocks
663 #define FIRST_TERM_INST(N) TermOpsBegin = N,
664 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
665 #define LAST_TERM_INST(N) TermOpsEnd = N+1
666 #include "llvm/IR/Instruction.def"
667  };
668 
669  enum UnaryOps {
670 #define FIRST_UNARY_INST(N) UnaryOpsBegin = N,
671 #define HANDLE_UNARY_INST(N, OPC, CLASS) OPC = N,
672 #define LAST_UNARY_INST(N) UnaryOpsEnd = N+1
673 #include "llvm/IR/Instruction.def"
674  };
675 
676  enum BinaryOps {
677 #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
678 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
679 #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
680 #include "llvm/IR/Instruction.def"
681  };
682 
683  enum MemoryOps {
684 #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
685 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
686 #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
687 #include "llvm/IR/Instruction.def"
688  };
689 
690  enum CastOps {
691 #define FIRST_CAST_INST(N) CastOpsBegin = N,
692 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
693 #define LAST_CAST_INST(N) CastOpsEnd = N+1
694 #include "llvm/IR/Instruction.def"
695  };
696 
698 #define FIRST_FUNCLETPAD_INST(N) FuncletPadOpsBegin = N,
699 #define HANDLE_FUNCLETPAD_INST(N, OPC, CLASS) OPC = N,
700 #define LAST_FUNCLETPAD_INST(N) FuncletPadOpsEnd = N+1
701 #include "llvm/IR/Instruction.def"
702  };
703 
704  enum OtherOps {
705 #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
706 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
707 #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
708 #include "llvm/IR/Instruction.def"
709  };
710 
711 private:
713 
714  // Shadow Value::setValueSubclassData with a private forwarding method so that
715  // subclasses cannot accidentally use it.
716  void setValueSubclassData(unsigned short D) {
718  }
719 
720  unsigned short getSubclassDataFromValue() const {
722  }
723 
724  void setHasMetadataHashEntry(bool V) {
725  setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
726  (V ? HasMetadataBit : 0));
727  }
728 
729  void setParent(BasicBlock *P);
730 
731 protected:
732  // Instruction subclasses can stick up to 15 bits of stuff into the
733  // SubclassData field of instruction with these members.
734 
735  // Verify that only the low 15 bits are used.
736  void setInstructionSubclassData(unsigned short D) {
737  assert((D & HasMetadataBit) == 0 && "Out of range value put into field");
738  setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D);
739  }
740 
742  return getSubclassDataFromValue() & ~HasMetadataBit;
743  }
744 
745  Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
746  Instruction *InsertBefore = nullptr);
747  Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
748  BasicBlock *InsertAtEnd);
749 
750 private:
751  /// Create a copy of this instruction.
752  Instruction *cloneImpl() const;
753 };
754 
756  V->deleteValue();
757 }
758 
759 } // end namespace llvm
760 
761 #endif // LLVM_IR_INSTRUCTION_H
Function * getFunction()
Definition: Instruction.h:86
void getAllMetadataOtherThanDebugLoc(SmallVectorImpl< std::pair< unsigned, MDNode *>> &MDs) const
This does the same thing as getAllMetadata, except that it filters out the debug location.
Definition: Instruction.h:244
unsigned short getSubclassDataFromValue() const
Definition: Value.h:655
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:464
bool isFuncletPad() const
Definition: Instruction.h:135
bool isFenceLike() const
Return true if this instruction behaves like a memory fence: it can load or store to memory location ...
Definition: Instruction.h:541
This class represents lattice values for constants.
Definition: AllocatorList.h:24
unsigned getSubclassDataFromInstruction() const
Definition: Instruction.h:741
Various leaf nodes.
Definition: ISDOpcodes.h:60
bool hasMetadataOtherThanDebugLoc() const
Return true if this instruction has metadata attached to it other than a debug location.
Definition: Instruction.h:215
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool isIdempotent() const
Return true if the instruction is idempotent:
Definition: Instruction.h:496
bool mayThrow(const MachineInstr &MI)
MDNode * getMetadata(StringRef Kind) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:228
bool isTerminator() const
Definition: Instruction.h:129
void deleteValue()
Delete a pointer to a generic Value.
Definition: Value.cpp:99
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:864
bool isArithmeticShift() const
Return true if this is an arithmetic shift right.
Definition: Instruction.h:168
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
Definition: Instruction.h:173
static bool isShift(unsigned Opcode)
Determine if the Opcode is one of the shift instructions.
Definition: Instruction.h:158
static bool isCommutative(unsigned Opcode)
Definition: Instruction.h:479
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
bool isNilpotent() const
Return true if the instruction is nilpotent:
Definition: Instruction.h:510
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:197
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:211
void dropUnknownNonDebugMetadata(unsigned ID1)
Definition: Instruction.h:279
OperationEquivalenceFlags
When checking for operation equivalence (using isSameOperationAs) it is sometimes useful to ignore ce...
Definition: Instruction.h:620
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
Definition: Instruction.h:178
void dropUnknownNonDebugMetadata()
Definition: Instruction.h:276
static bool isBinaryOp(unsigned Opcode)
Definition: Instruction.h:149
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:221
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:126
void setInstructionSubclassData(unsigned short D)
Definition: Instruction.h:736
Instruction * getNextNonDebugInstruction()
Definition: Instruction.h:588
static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV)
static Function * getFunction(Constant *C)
Definition: Evaluator.cpp:221
Debug location.
BasicBlock * getParent()
Definition: Instruction.h:68
static bool isTerminator(unsigned OpCode)
Definition: Instruction.h:142
Use delete by default for iplist and ilist.
Definition: ilist.h:41
#define P(N)
An ilist node that can access its parent list.
Definition: ilist_node.h:257
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:308
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
const char * getOpcodeName() const
Definition: Instruction.h:128
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
Definition: Instruction.h:562
static bool isUnaryOp(unsigned Opcode)
Definition: Instruction.h:146
static bool classof(const Value *V)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Instruction.h:655
bool isBinaryOp() const
Definition: Instruction.h:131
R600 Clause Merge
bool isExceptionalTerminator() const
Definition: Instruction.h:136
bool isCast() const
Definition: Instruction.h:134
static bool isAtomic(Instruction *I)
C setMetadata(LLVMContext::MD_range, MDNode::get(Context, LowAndHigh))
Module * getModule()
Definition: Instruction.h:76
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Definition: Instruction.h:64
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:644
static bool isCast(unsigned OpCode)
Determine if the OpCode is one of the CastInst instructions.
Definition: Instruction.h:183
void dropUnknownNonDebugMetadata(unsigned ID1, unsigned ID2)
Definition: Instruction.h:282
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static void deleteNode(NodeTy *V)
Definition: ilist.h:42
bool isCommutative() const
Return true if the instruction is commutative:
Definition: Instruction.h:478
void setValueSubclassData(unsigned short D)
Definition: Value.h:656
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode *>> &MDs) const
Get all metadata attached to this Instruction.
Definition: Instruction.h:237
static bool isExceptionalTerminator(unsigned OpCode)
Returns true if the OpCode is a terminator related to exception handling.
Definition: Instruction.h:193
const Instruction * user_back() const
Definition: Instruction.h:65
List that automatically updates parent links and symbol tables.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:311
static bool isNilpotent(unsigned Opcode)
Definition: Instruction.h:511
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
#define LLVM_READONLY
Definition: Compiler.h:184
bool isLogicalShift() const
Return true if this is a logical shift left or a logical shift right.
Definition: Instruction.h:163
static bool isFuncletPad(unsigned OpCode)
Determine if the OpCode is one of the FuncletPadInst instructions.
Definition: Instruction.h:188
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
static bool isIntDivRem(unsigned Opcode)
Definition: Instruction.h:153
Instruction * getPrevNonDebugInstruction()
Definition: Instruction.h:596
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:573
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:160
bool isIntDivRem() const
Definition: Instruction.h:132
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static bool isIdempotent(unsigned Opcode)
Definition: Instruction.h:497
static bool isAssociative(const COFFSection &Section)
bool isUnaryOp() const
Definition: Instruction.h:130
static bool isAssociative(unsigned Opcode)
Definition: Instruction.h:466
const BasicBlock * getParent() const
Definition: Instruction.h:67
bool mayReadOrWriteMemory() const
Return true if this instruction may read or write memory.
Definition: Instruction.h:522