InstructionPrecedenceTracking.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- InstructionPrecedenceTracking.h -------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. // Implements a class that is able to define some instructions as "special"
  14. // (e.g. as having implicit control flow, or writing memory, or having another
  15. // interesting property) and then efficiently answers queries of the types:
  16. // 1. Are there any special instructions in the block of interest?
  17. // 2. Return first of the special instructions in the given block;
  18. // 3. Check if the given instruction is preceeded by the first special
  19. // instruction in the same block.
  20. // The class provides caching that allows to answer these queries quickly. The
  21. // user must make sure that the cached data is invalidated properly whenever
  22. // a content of some tracked block is changed.
  23. //===----------------------------------------------------------------------===//
  24. #ifndef LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H
  25. #define LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H
  26. #include "llvm/ADT/DenseMap.h"
  27. namespace llvm {
  28. class BasicBlock;
  29. class Instruction;
  30. class InstructionPrecedenceTracking {
  31. // Maps a block to the topmost special instruction in it. If the value is
  32. // nullptr, it means that it is known that this block does not contain any
  33. // special instructions.
  34. DenseMap<const BasicBlock *, const Instruction *> FirstSpecialInsts;
  35. // Fills information about the given block's special instructions.
  36. void fill(const BasicBlock *BB);
  37. #ifndef NDEBUG
  38. /// Asserts that the cached info for \p BB is up-to-date. This helps to catch
  39. /// the usage error of accessing a block without properly invalidating after a
  40. /// previous transform.
  41. void validate(const BasicBlock *BB) const;
  42. /// Asserts whether or not the contents of this tracking is up-to-date. This
  43. /// helps to catch the usage error of accessing a block without properly
  44. /// invalidating after a previous transform.
  45. void validateAll() const;
  46. #endif
  47. protected:
  48. /// Returns the topmost special instruction from the block \p BB. Returns
  49. /// nullptr if there is no special instructions in the block.
  50. const Instruction *getFirstSpecialInstruction(const BasicBlock *BB);
  51. /// Returns true iff at least one instruction from the basic block \p BB is
  52. /// special.
  53. bool hasSpecialInstructions(const BasicBlock *BB);
  54. /// Returns true iff the first special instruction of \p Insn's block exists
  55. /// and dominates \p Insn.
  56. bool isPreceededBySpecialInstruction(const Instruction *Insn);
  57. /// A predicate that defines whether or not the instruction \p Insn is
  58. /// considered special and needs to be tracked. Implementing this method in
  59. /// children classes allows to implement tracking of implicit control flow,
  60. /// memory writing instructions or any other kinds of instructions we might
  61. /// be interested in.
  62. virtual bool isSpecialInstruction(const Instruction *Insn) const = 0;
  63. virtual ~InstructionPrecedenceTracking() = default;
  64. public:
  65. /// Notifies this tracking that we are going to insert a new instruction \p
  66. /// Inst to the basic block \p BB. It makes all necessary updates to internal
  67. /// caches to keep them consistent.
  68. void insertInstructionTo(const Instruction *Inst, const BasicBlock *BB);
  69. /// Notifies this tracking that we are going to remove the instruction \p Inst
  70. /// It makes all necessary updates to internal caches to keep them consistent.
  71. void removeInstruction(const Instruction *Inst);
  72. /// Notifies this tracking that we are going to replace all uses of \p Inst.
  73. /// It makes all necessary updates to internal caches to keep them consistent.
  74. /// Should typically be called before a RAUW.
  75. void removeUsersOf(const Instruction *Inst);
  76. /// Invalidates all information from this tracking.
  77. void clear();
  78. };
  79. /// This class allows to keep track on instructions with implicit control flow.
  80. /// These are instructions that may not pass execution to their successors. For
  81. /// example, throwing calls and guards do not always do this. If we need to know
  82. /// for sure that some instruction is guaranteed to execute if the given block
  83. /// is reached, then we need to make sure that there is no implicit control flow
  84. /// instruction (ICFI) preceding it. For example, this check is required if we
  85. /// perform PRE moving non-speculable instruction to other place.
  86. class ImplicitControlFlowTracking : public InstructionPrecedenceTracking {
  87. public:
  88. /// Returns the topmost instruction with implicit control flow from the given
  89. /// basic block. Returns nullptr if there is no such instructions in the block.
  90. const Instruction *getFirstICFI(const BasicBlock *BB) {
  91. return getFirstSpecialInstruction(BB);
  92. }
  93. /// Returns true if at least one instruction from the given basic block has
  94. /// implicit control flow.
  95. bool hasICF(const BasicBlock *BB) {
  96. return hasSpecialInstructions(BB);
  97. }
  98. /// Returns true if the first ICFI of Insn's block exists and dominates Insn.
  99. bool isDominatedByICFIFromSameBlock(const Instruction *Insn) {
  100. return isPreceededBySpecialInstruction(Insn);
  101. }
  102. bool isSpecialInstruction(const Instruction *Insn) const override;
  103. };
  104. class MemoryWriteTracking : public InstructionPrecedenceTracking {
  105. public:
  106. /// Returns the topmost instruction that may write memory from the given
  107. /// basic block. Returns nullptr if there is no such instructions in the block.
  108. const Instruction *getFirstMemoryWrite(const BasicBlock *BB) {
  109. return getFirstSpecialInstruction(BB);
  110. }
  111. /// Returns true if at least one instruction from the given basic block may
  112. /// write memory.
  113. bool mayWriteToMemory(const BasicBlock *BB) {
  114. return hasSpecialInstructions(BB);
  115. }
  116. /// Returns true if the first memory writing instruction of Insn's block
  117. /// exists and dominates Insn.
  118. bool isDominatedByMemoryWriteFromSameBlock(const Instruction *Insn) {
  119. return isPreceededBySpecialInstruction(Insn);
  120. }
  121. bool isSpecialInstruction(const Instruction *Insn) const override;
  122. };
  123. } // llvm
  124. #endif // LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H
  125. #ifdef __GNUC__
  126. #pragma GCC diagnostic pop
  127. #endif