blob: 415797d6a3783207adff324a32c68b6321d7b9a3 [file] [log] [blame]
Max Kazantsevb5dd0922018-08-27 09:43:16 +00001//===-- InstructionPrecedenceTracking.cpp -----------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Max Kazantsevb5dd0922018-08-27 09:43:16 +00006//
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
Max Kazantsevd3a4cbe2018-08-30 04:49:03 +000020#include "llvm/Analysis/InstructionPrecedenceTracking.h"
Max Kazantsevb5dd0922018-08-27 09:43:16 +000021#include "llvm/Analysis/ValueTracking.h"
Max Kazantsev24383cd2019-02-14 11:10:29 +000022#include "llvm/IR/PatternMatch.h"
Reid Kleckner4c1a1d32019-11-14 15:15:48 -080023#include "llvm/Support/CommandLine.h"
Max Kazantsevb5dd0922018-08-27 09:43:16 +000024
25using namespace llvm;
26
Max Kazantsev8a9e0592018-09-06 08:33:02 +000027#ifndef NDEBUG
28static 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
Max Kazantsevb5dd0922018-08-27 09:43:16 +000035const Instruction *InstructionPrecedenceTracking::getFirstSpecialInstruction(
36 const BasicBlock *BB) {
Max Kazantsev8a9e0592018-09-06 08:33:02 +000037#ifndef NDEBUG
38 // If there is a bug connected to invalid cache, turn on ExpensiveAsserts to
39 // catch this situation as early as possible.
40 if (ExpensiveAsserts)
41 validateAll();
42 else
43 validate(BB);
44#endif
45
Max Kazantsev6afebe12018-09-06 09:29:42 +000046 if (FirstSpecialInsts.find(BB) == FirstSpecialInsts.end()) {
Max Kazantsevb5dd0922018-08-27 09:43:16 +000047 fill(BB);
Max Kazantsev6afebe12018-09-06 09:29:42 +000048 assert(FirstSpecialInsts.find(BB) != FirstSpecialInsts.end() && "Must be!");
49 }
50 return FirstSpecialInsts[BB];
Max Kazantsevb5dd0922018-08-27 09:43:16 +000051}
52
53bool InstructionPrecedenceTracking::hasSpecialInstructions(
54 const BasicBlock *BB) {
55 return getFirstSpecialInstruction(BB) != nullptr;
56}
57
58bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction(
59 const Instruction *Insn) {
Max Kazantsev90edc982018-09-11 05:10:01 +000060 const Instruction *MaybeFirstSpecial =
Max Kazantsevb5dd0922018-08-27 09:43:16 +000061 getFirstSpecialInstruction(Insn->getParent());
Max Kazantsev90edc982018-09-11 05:10:01 +000062 return MaybeFirstSpecial && OI.dominates(MaybeFirstSpecial, Insn);
Max Kazantsevb5dd0922018-08-27 09:43:16 +000063}
64
65void InstructionPrecedenceTracking::fill(const BasicBlock *BB) {
Max Kazantsevd3487bd2018-08-30 09:24:33 +000066 FirstSpecialInsts.erase(BB);
Max Kazantsevb5dd0922018-08-27 09:43:16 +000067 for (auto &I : *BB)
68 if (isSpecialInstruction(&I)) {
Max Kazantsevd3487bd2018-08-30 09:24:33 +000069 FirstSpecialInsts[BB] = &I;
Max Kazantsev6afebe12018-09-06 09:29:42 +000070 return;
Max Kazantsevb5dd0922018-08-27 09:43:16 +000071 }
72
Max Kazantsev6afebe12018-09-06 09:29:42 +000073 // Mark this block as having no special instructions.
74 FirstSpecialInsts[BB] = nullptr;
Max Kazantsevb5dd0922018-08-27 09:43:16 +000075}
76
Max Kazantsev8a9e0592018-09-06 08:33:02 +000077#ifndef NDEBUG
78void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
Max Kazantsev8a9e0592018-09-06 08:33:02 +000079 auto It = FirstSpecialInsts.find(BB);
Max Kazantsev6afebe12018-09-06 09:29:42 +000080 // Bail if we don't have anything cached for this block.
81 if (It == FirstSpecialInsts.end())
82 return;
83
84 for (const Instruction &Insn : *BB)
Max Kazantsev8a9e0592018-09-06 08:33:02 +000085 if (isSpecialInstruction(&Insn)) {
Max Kazantsev8a9e0592018-09-06 08:33:02 +000086 assert(It->second == &Insn &&
87 "Cached first special instruction is wrong!");
Max Kazantsev6afebe12018-09-06 09:29:42 +000088 return;
Max Kazantsev8a9e0592018-09-06 08:33:02 +000089 }
Max Kazantsev6afebe12018-09-06 09:29:42 +000090
91 assert(It->second == nullptr &&
92 "Block is marked as having special instructions but in fact it has "
93 "none!");
Max Kazantsev8a9e0592018-09-06 08:33:02 +000094}
95
96void InstructionPrecedenceTracking::validateAll() const {
97 // Check that for every known block the cached value is correct.
Max Kazantsev8a9e0592018-09-06 08:33:02 +000098 for (auto &It : FirstSpecialInsts)
Max Kazantsev6afebe12018-09-06 09:29:42 +000099 validate(It.first);
Max Kazantsev8a9e0592018-09-06 08:33:02 +0000100}
101#endif
102
Max Kazantsev4615a502019-01-09 07:28:13 +0000103void InstructionPrecedenceTracking::insertInstructionTo(const Instruction *Inst,
104 const BasicBlock *BB) {
105 if (isSpecialInstruction(Inst))
106 FirstSpecialInsts.erase(BB);
Max Kazantsevb5dd0922018-08-27 09:43:16 +0000107 OI.invalidateBlock(BB);
Max Kazantsev4615a502019-01-09 07:28:13 +0000108}
109
110void InstructionPrecedenceTracking::removeInstruction(const Instruction *Inst) {
111 if (isSpecialInstruction(Inst))
112 FirstSpecialInsts.erase(Inst->getParent());
113 OI.invalidateBlock(Inst->getParent());
Max Kazantsevb5dd0922018-08-27 09:43:16 +0000114}
115
116void InstructionPrecedenceTracking::clear() {
Max Kazantsevd3487bd2018-08-30 09:24:33 +0000117 for (auto It : FirstSpecialInsts)
Max Kazantsevb5dd0922018-08-27 09:43:16 +0000118 OI.invalidateBlock(It.first);
Max Kazantsevd3487bd2018-08-30 09:24:33 +0000119 FirstSpecialInsts.clear();
Max Kazantsev8a9e0592018-09-06 08:33:02 +0000120#ifndef NDEBUG
121 // The map should be valid after clearing (at least empty).
122 validateAll();
123#endif
Max Kazantsevb5dd0922018-08-27 09:43:16 +0000124}
125
126bool ImplicitControlFlowTracking::isSpecialInstruction(
127 const Instruction *Insn) const {
128 // If a block's instruction doesn't always pass the control to its successor
129 // instruction, mark the block as having implicit control flow. We use them
130 // to avoid wrong assumptions of sort "if A is executed and B post-dominates
131 // A, then B is also executed". This is not true is there is an implicit
132 // control flow instruction (e.g. a guard) between them.
133 //
134 // TODO: Currently, isGuaranteedToTransferExecutionToSuccessor returns false
135 // for volatile stores and loads because they can trap. The discussion on
136 // whether or not it is correct is still ongoing. We might want to get rid
137 // of this logic in the future. Anyways, trapping instructions shouldn't
138 // introduce implicit control flow, so we explicitly allow them here. This
139 // must be removed once isGuaranteedToTransferExecutionToSuccessor is fixed.
140 if (isGuaranteedToTransferExecutionToSuccessor(Insn))
141 return false;
142 if (isa<LoadInst>(Insn)) {
143 assert(cast<LoadInst>(Insn)->isVolatile() &&
144 "Non-volatile load should transfer execution to successor!");
145 return false;
146 }
147 if (isa<StoreInst>(Insn)) {
148 assert(cast<StoreInst>(Insn)->isVolatile() &&
149 "Non-volatile store should transfer execution to successor!");
150 return false;
151 }
152 return true;
153}
Max Kazantsev7d49a3a2018-11-12 09:29:58 +0000154
155bool MemoryWriteTracking::isSpecialInstruction(
156 const Instruction *Insn) const {
Max Kazantsev24383cd2019-02-14 11:10:29 +0000157 using namespace PatternMatch;
158 if (match(Insn, m_Intrinsic<Intrinsic::experimental_widenable_condition>()))
159 return false;
Max Kazantsev7d49a3a2018-11-12 09:29:58 +0000160 return Insn->mayWriteToMemory();
161}