blob: 0afce7ebbd311862e2d026df9d9031558e2ca458 [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 Kazantsevb5dd0922018-08-27 09:43:16 +000022
23using namespace llvm;
24
Max Kazantsev8a9e0592018-09-06 08:33:02 +000025#ifndef NDEBUG
26static cl::opt<bool> ExpensiveAsserts(
27 "ipt-expensive-asserts",
28 cl::desc("Perform expensive assert validation on every query to Instruction"
29 " Precedence Tracking"),
30 cl::init(false), cl::Hidden);
31#endif
32
Max Kazantsevb5dd0922018-08-27 09:43:16 +000033const Instruction *InstructionPrecedenceTracking::getFirstSpecialInstruction(
34 const BasicBlock *BB) {
Max Kazantsev8a9e0592018-09-06 08:33:02 +000035#ifndef NDEBUG
36 // If there is a bug connected to invalid cache, turn on ExpensiveAsserts to
37 // catch this situation as early as possible.
38 if (ExpensiveAsserts)
39 validateAll();
40 else
41 validate(BB);
42#endif
43
Max Kazantsev6afebe12018-09-06 09:29:42 +000044 if (FirstSpecialInsts.find(BB) == FirstSpecialInsts.end()) {
Max Kazantsevb5dd0922018-08-27 09:43:16 +000045 fill(BB);
Max Kazantsev6afebe12018-09-06 09:29:42 +000046 assert(FirstSpecialInsts.find(BB) != FirstSpecialInsts.end() && "Must be!");
47 }
48 return FirstSpecialInsts[BB];
Max Kazantsevb5dd0922018-08-27 09:43:16 +000049}
50
51bool InstructionPrecedenceTracking::hasSpecialInstructions(
52 const BasicBlock *BB) {
53 return getFirstSpecialInstruction(BB) != nullptr;
54}
55
56bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction(
57 const Instruction *Insn) {
Max Kazantsev90edc982018-09-11 05:10:01 +000058 const Instruction *MaybeFirstSpecial =
Max Kazantsevb5dd0922018-08-27 09:43:16 +000059 getFirstSpecialInstruction(Insn->getParent());
Max Kazantsev90edc982018-09-11 05:10:01 +000060 return MaybeFirstSpecial && OI.dominates(MaybeFirstSpecial, Insn);
Max Kazantsevb5dd0922018-08-27 09:43:16 +000061}
62
63void InstructionPrecedenceTracking::fill(const BasicBlock *BB) {
Max Kazantsevd3487bd2018-08-30 09:24:33 +000064 FirstSpecialInsts.erase(BB);
Max Kazantsevb5dd0922018-08-27 09:43:16 +000065 for (auto &I : *BB)
66 if (isSpecialInstruction(&I)) {
Max Kazantsevd3487bd2018-08-30 09:24:33 +000067 FirstSpecialInsts[BB] = &I;
Max Kazantsev6afebe12018-09-06 09:29:42 +000068 return;
Max Kazantsevb5dd0922018-08-27 09:43:16 +000069 }
70
Max Kazantsev6afebe12018-09-06 09:29:42 +000071 // Mark this block as having no special instructions.
72 FirstSpecialInsts[BB] = nullptr;
Max Kazantsevb5dd0922018-08-27 09:43:16 +000073}
74
Max Kazantsev8a9e0592018-09-06 08:33:02 +000075#ifndef NDEBUG
76void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
Max Kazantsev8a9e0592018-09-06 08:33:02 +000077 auto It = FirstSpecialInsts.find(BB);
Max Kazantsev6afebe12018-09-06 09:29:42 +000078 // Bail if we don't have anything cached for this block.
79 if (It == FirstSpecialInsts.end())
80 return;
81
82 for (const Instruction &Insn : *BB)
Max Kazantsev8a9e0592018-09-06 08:33:02 +000083 if (isSpecialInstruction(&Insn)) {
Max Kazantsev8a9e0592018-09-06 08:33:02 +000084 assert(It->second == &Insn &&
85 "Cached first special instruction is wrong!");
Max Kazantsev6afebe12018-09-06 09:29:42 +000086 return;
Max Kazantsev8a9e0592018-09-06 08:33:02 +000087 }
Max Kazantsev6afebe12018-09-06 09:29:42 +000088
89 assert(It->second == nullptr &&
90 "Block is marked as having special instructions but in fact it has "
91 "none!");
Max Kazantsev8a9e0592018-09-06 08:33:02 +000092}
93
94void InstructionPrecedenceTracking::validateAll() const {
95 // Check that for every known block the cached value is correct.
Max Kazantsev8a9e0592018-09-06 08:33:02 +000096 for (auto &It : FirstSpecialInsts)
Max Kazantsev6afebe12018-09-06 09:29:42 +000097 validate(It.first);
Max Kazantsev8a9e0592018-09-06 08:33:02 +000098}
99#endif
100
Max Kazantsev4615a502019-01-09 07:28:13 +0000101void InstructionPrecedenceTracking::insertInstructionTo(const Instruction *Inst,
102 const BasicBlock *BB) {
103 if (isSpecialInstruction(Inst))
104 FirstSpecialInsts.erase(BB);
Max Kazantsevb5dd0922018-08-27 09:43:16 +0000105 OI.invalidateBlock(BB);
Max Kazantsev4615a502019-01-09 07:28:13 +0000106}
107
108void InstructionPrecedenceTracking::removeInstruction(const Instruction *Inst) {
109 if (isSpecialInstruction(Inst))
110 FirstSpecialInsts.erase(Inst->getParent());
111 OI.invalidateBlock(Inst->getParent());
Max Kazantsevb5dd0922018-08-27 09:43:16 +0000112}
113
114void InstructionPrecedenceTracking::clear() {
Max Kazantsevd3487bd2018-08-30 09:24:33 +0000115 for (auto It : FirstSpecialInsts)
Max Kazantsevb5dd0922018-08-27 09:43:16 +0000116 OI.invalidateBlock(It.first);
Max Kazantsevd3487bd2018-08-30 09:24:33 +0000117 FirstSpecialInsts.clear();
Max Kazantsev8a9e0592018-09-06 08:33:02 +0000118#ifndef NDEBUG
119 // The map should be valid after clearing (at least empty).
120 validateAll();
121#endif
Max Kazantsevb5dd0922018-08-27 09:43:16 +0000122}
123
124bool ImplicitControlFlowTracking::isSpecialInstruction(
125 const Instruction *Insn) const {
126 // If a block's instruction doesn't always pass the control to its successor
127 // instruction, mark the block as having implicit control flow. We use them
128 // to avoid wrong assumptions of sort "if A is executed and B post-dominates
129 // A, then B is also executed". This is not true is there is an implicit
130 // control flow instruction (e.g. a guard) between them.
131 //
132 // TODO: Currently, isGuaranteedToTransferExecutionToSuccessor returns false
133 // for volatile stores and loads because they can trap. The discussion on
134 // whether or not it is correct is still ongoing. We might want to get rid
135 // of this logic in the future. Anyways, trapping instructions shouldn't
136 // introduce implicit control flow, so we explicitly allow them here. This
137 // must be removed once isGuaranteedToTransferExecutionToSuccessor is fixed.
138 if (isGuaranteedToTransferExecutionToSuccessor(Insn))
139 return false;
140 if (isa<LoadInst>(Insn)) {
141 assert(cast<LoadInst>(Insn)->isVolatile() &&
142 "Non-volatile load should transfer execution to successor!");
143 return false;
144 }
145 if (isa<StoreInst>(Insn)) {
146 assert(cast<StoreInst>(Insn)->isVolatile() &&
147 "Non-volatile store should transfer execution to successor!");
148 return false;
149 }
150 return true;
151}
Max Kazantsev7d49a3a2018-11-12 09:29:58 +0000152
153bool MemoryWriteTracking::isSpecialInstruction(
154 const Instruction *Insn) const {
155 return Insn->mayWriteToMemory();
156}