Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 1 | //===-- InstructionPrecedenceTracking.cpp -----------------------*- 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 | // Implements a class that is able to define some instructions as "special" |
| 10 | // (e.g. as having implicit control flow, or writing memory, or having another |
| 11 | // interesting property) and then efficiently answers queries of the types: |
| 12 | // 1. Are there any special instructions in the block of interest? |
| 13 | // 2. Return first of the special instructions in the given block; |
| 14 | // 3. Check if the given instruction is preceeded by the first special |
| 15 | // instruction in the same block. |
| 16 | // The class provides caching that allows to answer these queries quickly. The |
| 17 | // user must make sure that the cached data is invalidated properly whenever |
| 18 | // a content of some tracked block is changed. |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Max Kazantsev | d3a4cbe | 2018-08-30 04:49:03 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/InstructionPrecedenceTracking.h" |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 26 | #ifndef NDEBUG |
| 27 | static cl::opt<bool> ExpensiveAsserts( |
| 28 | "ipt-expensive-asserts", |
| 29 | cl::desc("Perform expensive assert validation on every query to Instruction" |
| 30 | " Precedence Tracking"), |
| 31 | cl::init(false), cl::Hidden); |
| 32 | #endif |
| 33 | |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 34 | const Instruction *InstructionPrecedenceTracking::getFirstSpecialInstruction( |
| 35 | const BasicBlock *BB) { |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 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 | |
Max Kazantsev | 6afebe1 | 2018-09-06 09:29:42 +0000 | [diff] [blame] | 45 | if (FirstSpecialInsts.find(BB) == FirstSpecialInsts.end()) { |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 46 | fill(BB); |
Max Kazantsev | 6afebe1 | 2018-09-06 09:29:42 +0000 | [diff] [blame] | 47 | assert(FirstSpecialInsts.find(BB) != FirstSpecialInsts.end() && "Must be!"); |
| 48 | } |
| 49 | return FirstSpecialInsts[BB]; |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool InstructionPrecedenceTracking::hasSpecialInstructions( |
| 53 | const BasicBlock *BB) { |
| 54 | return getFirstSpecialInstruction(BB) != nullptr; |
| 55 | } |
| 56 | |
| 57 | bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction( |
| 58 | const Instruction *Insn) { |
Max Kazantsev | 90edc98 | 2018-09-11 05:10:01 +0000 | [diff] [blame] | 59 | const Instruction *MaybeFirstSpecial = |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 60 | getFirstSpecialInstruction(Insn->getParent()); |
Max Kazantsev | 90edc98 | 2018-09-11 05:10:01 +0000 | [diff] [blame] | 61 | return MaybeFirstSpecial && OI.dominates(MaybeFirstSpecial, Insn); |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void InstructionPrecedenceTracking::fill(const BasicBlock *BB) { |
Max Kazantsev | d3487bd | 2018-08-30 09:24:33 +0000 | [diff] [blame] | 65 | FirstSpecialInsts.erase(BB); |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 66 | for (auto &I : *BB) |
| 67 | if (isSpecialInstruction(&I)) { |
Max Kazantsev | d3487bd | 2018-08-30 09:24:33 +0000 | [diff] [blame] | 68 | FirstSpecialInsts[BB] = &I; |
Max Kazantsev | 6afebe1 | 2018-09-06 09:29:42 +0000 | [diff] [blame] | 69 | return; |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Max Kazantsev | 6afebe1 | 2018-09-06 09:29:42 +0000 | [diff] [blame] | 72 | // Mark this block as having no special instructions. |
| 73 | FirstSpecialInsts[BB] = nullptr; |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 76 | #ifndef NDEBUG |
| 77 | void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const { |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 78 | auto It = FirstSpecialInsts.find(BB); |
Max Kazantsev | 6afebe1 | 2018-09-06 09:29:42 +0000 | [diff] [blame] | 79 | // Bail if we don't have anything cached for this block. |
| 80 | if (It == FirstSpecialInsts.end()) |
| 81 | return; |
| 82 | |
| 83 | for (const Instruction &Insn : *BB) |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 84 | if (isSpecialInstruction(&Insn)) { |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 85 | assert(It->second == &Insn && |
| 86 | "Cached first special instruction is wrong!"); |
Max Kazantsev | 6afebe1 | 2018-09-06 09:29:42 +0000 | [diff] [blame] | 87 | return; |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 88 | } |
Max Kazantsev | 6afebe1 | 2018-09-06 09:29:42 +0000 | [diff] [blame] | 89 | |
| 90 | assert(It->second == nullptr && |
| 91 | "Block is marked as having special instructions but in fact it has " |
| 92 | "none!"); |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void InstructionPrecedenceTracking::validateAll() const { |
| 96 | // Check that for every known block the cached value is correct. |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 97 | for (auto &It : FirstSpecialInsts) |
Max Kazantsev | 6afebe1 | 2018-09-06 09:29:42 +0000 | [diff] [blame] | 98 | validate(It.first); |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 99 | } |
| 100 | #endif |
| 101 | |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 102 | void InstructionPrecedenceTracking::invalidateBlock(const BasicBlock *BB) { |
| 103 | OI.invalidateBlock(BB); |
Max Kazantsev | d3487bd | 2018-08-30 09:24:33 +0000 | [diff] [blame] | 104 | FirstSpecialInsts.erase(BB); |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void InstructionPrecedenceTracking::clear() { |
Max Kazantsev | d3487bd | 2018-08-30 09:24:33 +0000 | [diff] [blame] | 108 | for (auto It : FirstSpecialInsts) |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 109 | OI.invalidateBlock(It.first); |
Max Kazantsev | d3487bd | 2018-08-30 09:24:33 +0000 | [diff] [blame] | 110 | FirstSpecialInsts.clear(); |
Max Kazantsev | 8a9e059 | 2018-09-06 08:33:02 +0000 | [diff] [blame] | 111 | #ifndef NDEBUG |
| 112 | // The map should be valid after clearing (at least empty). |
| 113 | validateAll(); |
| 114 | #endif |
Max Kazantsev | b5dd092 | 2018-08-27 09:43:16 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool ImplicitControlFlowTracking::isSpecialInstruction( |
| 118 | const Instruction *Insn) const { |
| 119 | // If a block's instruction doesn't always pass the control to its successor |
| 120 | // instruction, mark the block as having implicit control flow. We use them |
| 121 | // to avoid wrong assumptions of sort "if A is executed and B post-dominates |
| 122 | // A, then B is also executed". This is not true is there is an implicit |
| 123 | // control flow instruction (e.g. a guard) between them. |
| 124 | // |
| 125 | // TODO: Currently, isGuaranteedToTransferExecutionToSuccessor returns false |
| 126 | // for volatile stores and loads because they can trap. The discussion on |
| 127 | // whether or not it is correct is still ongoing. We might want to get rid |
| 128 | // of this logic in the future. Anyways, trapping instructions shouldn't |
| 129 | // introduce implicit control flow, so we explicitly allow them here. This |
| 130 | // must be removed once isGuaranteedToTransferExecutionToSuccessor is fixed. |
| 131 | if (isGuaranteedToTransferExecutionToSuccessor(Insn)) |
| 132 | return false; |
| 133 | if (isa<LoadInst>(Insn)) { |
| 134 | assert(cast<LoadInst>(Insn)->isVolatile() && |
| 135 | "Non-volatile load should transfer execution to successor!"); |
| 136 | return false; |
| 137 | } |
| 138 | if (isa<StoreInst>(Insn)) { |
| 139 | assert(cast<StoreInst>(Insn)->isVolatile() && |
| 140 | "Non-volatile store should transfer execution to successor!"); |
| 141 | return false; |
| 142 | } |
| 143 | return true; |
| 144 | } |