InstructionPrecedenceTracking.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===-- InstructionPrecedenceTracking.cpp -----------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Implements a class that is able to define some instructions as "special"
  9. // (e.g. as having implicit control flow, or writing memory, or having another
  10. // interesting property) and then efficiently answers queries of the types:
  11. // 1. Are there any special instructions in the block of interest?
  12. // 2. Return first of the special instructions in the given block;
  13. // 3. Check if the given instruction is preceeded by the first special
  14. // instruction in the same block.
  15. // The class provides caching that allows to answer these queries quickly. The
  16. // user must make sure that the cached data is invalidated properly whenever
  17. // a content of some tracked block is changed.
  18. //===----------------------------------------------------------------------===//
  19. #include "llvm/Analysis/InstructionPrecedenceTracking.h"
  20. #include "llvm/Analysis/ValueTracking.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/IR/PatternMatch.h"
  23. #include "llvm/Support/CommandLine.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "ipt"
  26. STATISTIC(NumInstScanned, "Number of insts scanned while updating ibt");
  27. #ifndef NDEBUG
  28. static cl::opt<bool> ExpensiveAsserts(
  29. "ipt-expensive-asserts",
  30. cl::desc("Perform expensive assert validation on every query to Instruction"
  31. " Precedence Tracking"),
  32. cl::init(false), cl::Hidden);
  33. #endif
  34. const Instruction *InstructionPrecedenceTracking::getFirstSpecialInstruction(
  35. const BasicBlock *BB) {
  36. #ifndef NDEBUG
  37. // If there is a bug connected to invalid cache, turn on ExpensiveAsserts to
  38. // catch this situation as early as possible.
  39. if (ExpensiveAsserts)
  40. validateAll();
  41. else
  42. validate(BB);
  43. #endif
  44. if (FirstSpecialInsts.find(BB) == FirstSpecialInsts.end()) {
  45. fill(BB);
  46. assert(FirstSpecialInsts.find(BB) != FirstSpecialInsts.end() && "Must be!");
  47. }
  48. return FirstSpecialInsts[BB];
  49. }
  50. bool InstructionPrecedenceTracking::hasSpecialInstructions(
  51. const BasicBlock *BB) {
  52. return getFirstSpecialInstruction(BB) != nullptr;
  53. }
  54. bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction(
  55. const Instruction *Insn) {
  56. const Instruction *MaybeFirstSpecial =
  57. getFirstSpecialInstruction(Insn->getParent());
  58. return MaybeFirstSpecial && MaybeFirstSpecial->comesBefore(Insn);
  59. }
  60. void InstructionPrecedenceTracking::fill(const BasicBlock *BB) {
  61. FirstSpecialInsts.erase(BB);
  62. for (const auto &I : *BB) {
  63. NumInstScanned++;
  64. if (isSpecialInstruction(&I)) {
  65. FirstSpecialInsts[BB] = &I;
  66. return;
  67. }
  68. }
  69. // Mark this block as having no special instructions.
  70. FirstSpecialInsts[BB] = nullptr;
  71. }
  72. #ifndef NDEBUG
  73. void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
  74. auto It = FirstSpecialInsts.find(BB);
  75. // Bail if we don't have anything cached for this block.
  76. if (It == FirstSpecialInsts.end())
  77. return;
  78. for (const Instruction &Insn : *BB)
  79. if (isSpecialInstruction(&Insn)) {
  80. assert(It->second == &Insn &&
  81. "Cached first special instruction is wrong!");
  82. return;
  83. }
  84. assert(It->second == nullptr &&
  85. "Block is marked as having special instructions but in fact it has "
  86. "none!");
  87. }
  88. void InstructionPrecedenceTracking::validateAll() const {
  89. // Check that for every known block the cached value is correct.
  90. for (const auto &It : FirstSpecialInsts)
  91. validate(It.first);
  92. }
  93. #endif
  94. void InstructionPrecedenceTracking::insertInstructionTo(const Instruction *Inst,
  95. const BasicBlock *BB) {
  96. if (isSpecialInstruction(Inst))
  97. FirstSpecialInsts.erase(BB);
  98. }
  99. void InstructionPrecedenceTracking::removeInstruction(const Instruction *Inst) {
  100. auto *BB = Inst->getParent();
  101. assert(BB && "must be called before instruction is actually removed");
  102. if (FirstSpecialInsts.count(BB) && FirstSpecialInsts[BB] == Inst)
  103. FirstSpecialInsts.erase(BB);
  104. }
  105. void InstructionPrecedenceTracking::removeUsersOf(const Instruction *Inst) {
  106. for (const auto *U : Inst->users()) {
  107. if (const auto *UI = dyn_cast<Instruction>(U))
  108. removeInstruction(UI);
  109. }
  110. }
  111. void InstructionPrecedenceTracking::clear() {
  112. FirstSpecialInsts.clear();
  113. #ifndef NDEBUG
  114. // The map should be valid after clearing (at least empty).
  115. validateAll();
  116. #endif
  117. }
  118. bool ImplicitControlFlowTracking::isSpecialInstruction(
  119. const Instruction *Insn) const {
  120. // If a block's instruction doesn't always pass the control to its successor
  121. // instruction, mark the block as having implicit control flow. We use them
  122. // to avoid wrong assumptions of sort "if A is executed and B post-dominates
  123. // A, then B is also executed". This is not true is there is an implicit
  124. // control flow instruction (e.g. a guard) between them.
  125. return !isGuaranteedToTransferExecutionToSuccessor(Insn);
  126. }
  127. bool MemoryWriteTracking::isSpecialInstruction(
  128. const Instruction *Insn) const {
  129. using namespace PatternMatch;
  130. if (match(Insn, m_Intrinsic<Intrinsic::experimental_widenable_condition>()))
  131. return false;
  132. return Insn->mayWriteToMemory();
  133. }