blob: 816126f407cafd135cca5f75711ec6920856f5b4 [file] [log] [blame]
Max Kazantsev4b12e2f2018-08-27 09:43:16 +00001//===-- 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 Kazantsev3a864552018-08-30 04:49:03 +000021#include "llvm/Analysis/InstructionPrecedenceTracking.h"
Max Kazantsev4b12e2f2018-08-27 09:43:16 +000022#include "llvm/Analysis/ValueTracking.h"
Max Kazantsev4b12e2f2018-08-27 09:43:16 +000023
24using namespace llvm;
25
Max Kazantsevd8ffa612018-09-06 08:33:02 +000026#ifndef NDEBUG
27static 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 Kazantsev4b12e2f2018-08-27 09:43:16 +000034const Instruction *InstructionPrecedenceTracking::getFirstSpecialInstruction(
35 const BasicBlock *BB) {
Max Kazantsevd8ffa612018-09-06 08:33:02 +000036#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 Kazantsev62f16662018-09-06 09:29:42 +000045 if (FirstSpecialInsts.find(BB) == FirstSpecialInsts.end()) {
Max Kazantsev4b12e2f2018-08-27 09:43:16 +000046 fill(BB);
Max Kazantsev62f16662018-09-06 09:29:42 +000047 assert(FirstSpecialInsts.find(BB) != FirstSpecialInsts.end() && "Must be!");
48 }
49 return FirstSpecialInsts[BB];
Max Kazantsev4b12e2f2018-08-27 09:43:16 +000050}
51
52bool InstructionPrecedenceTracking::hasSpecialInstructions(
53 const BasicBlock *BB) {
54 return getFirstSpecialInstruction(BB) != nullptr;
55}
56
57bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction(
58 const Instruction *Insn) {
Max Kazantsev281457b2018-09-11 05:10:01 +000059 const Instruction *MaybeFirstSpecial =
Max Kazantsev4b12e2f2018-08-27 09:43:16 +000060 getFirstSpecialInstruction(Insn->getParent());
Max Kazantsev281457b2018-09-11 05:10:01 +000061 return MaybeFirstSpecial && OI.dominates(MaybeFirstSpecial, Insn);
Max Kazantsev4b12e2f2018-08-27 09:43:16 +000062}
63
64void InstructionPrecedenceTracking::fill(const BasicBlock *BB) {
Max Kazantsev009ed302018-08-30 09:24:33 +000065 FirstSpecialInsts.erase(BB);
Max Kazantsev4b12e2f2018-08-27 09:43:16 +000066 for (auto &I : *BB)
67 if (isSpecialInstruction(&I)) {
Max Kazantsev009ed302018-08-30 09:24:33 +000068 FirstSpecialInsts[BB] = &I;
Max Kazantsev62f16662018-09-06 09:29:42 +000069 return;
Max Kazantsev4b12e2f2018-08-27 09:43:16 +000070 }
71
Max Kazantsev62f16662018-09-06 09:29:42 +000072 // Mark this block as having no special instructions.
73 FirstSpecialInsts[BB] = nullptr;
Max Kazantsev4b12e2f2018-08-27 09:43:16 +000074}
75
Max Kazantsevd8ffa612018-09-06 08:33:02 +000076#ifndef NDEBUG
77void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
Max Kazantsevd8ffa612018-09-06 08:33:02 +000078 auto It = FirstSpecialInsts.find(BB);
Max Kazantsev62f16662018-09-06 09:29:42 +000079 // 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 Kazantsevd8ffa612018-09-06 08:33:02 +000084 if (isSpecialInstruction(&Insn)) {
Max Kazantsevd8ffa612018-09-06 08:33:02 +000085 assert(It->second == &Insn &&
86 "Cached first special instruction is wrong!");
Max Kazantsev62f16662018-09-06 09:29:42 +000087 return;
Max Kazantsevd8ffa612018-09-06 08:33:02 +000088 }
Max Kazantsev62f16662018-09-06 09:29:42 +000089
90 assert(It->second == nullptr &&
91 "Block is marked as having special instructions but in fact it has "
92 "none!");
Max Kazantsevd8ffa612018-09-06 08:33:02 +000093}
94
95void InstructionPrecedenceTracking::validateAll() const {
96 // Check that for every known block the cached value is correct.
Max Kazantsevd8ffa612018-09-06 08:33:02 +000097 for (auto &It : FirstSpecialInsts)
Max Kazantsev62f16662018-09-06 09:29:42 +000098 validate(It.first);
Max Kazantsevd8ffa612018-09-06 08:33:02 +000099}
100#endif
101
Max Kazantsevc80a38c2019-01-09 07:28:13 +0000102void InstructionPrecedenceTracking::insertInstructionTo(const Instruction *Inst,
103 const BasicBlock *BB) {
104 if (isSpecialInstruction(Inst))
105 FirstSpecialInsts.erase(BB);
Max Kazantsev4b12e2f2018-08-27 09:43:16 +0000106 OI.invalidateBlock(BB);
Max Kazantsevc80a38c2019-01-09 07:28:13 +0000107}
108
109void InstructionPrecedenceTracking::removeInstruction(const Instruction *Inst) {
110 if (isSpecialInstruction(Inst))
111 FirstSpecialInsts.erase(Inst->getParent());
112 OI.invalidateBlock(Inst->getParent());
Max Kazantsev4b12e2f2018-08-27 09:43:16 +0000113}
114
115void InstructionPrecedenceTracking::clear() {
Max Kazantsev009ed302018-08-30 09:24:33 +0000116 for (auto It : FirstSpecialInsts)
Max Kazantsev4b12e2f2018-08-27 09:43:16 +0000117 OI.invalidateBlock(It.first);
Max Kazantsev009ed302018-08-30 09:24:33 +0000118 FirstSpecialInsts.clear();
Max Kazantsevd8ffa612018-09-06 08:33:02 +0000119#ifndef NDEBUG
120 // The map should be valid after clearing (at least empty).
121 validateAll();
122#endif
Max Kazantsev4b12e2f2018-08-27 09:43:16 +0000123}
124
125bool ImplicitControlFlowTracking::isSpecialInstruction(
126 const Instruction *Insn) const {
127 // If a block's instruction doesn't always pass the control to its successor
128 // instruction, mark the block as having implicit control flow. We use them
129 // to avoid wrong assumptions of sort "if A is executed and B post-dominates
130 // A, then B is also executed". This is not true is there is an implicit
131 // control flow instruction (e.g. a guard) between them.
132 //
133 // TODO: Currently, isGuaranteedToTransferExecutionToSuccessor returns false
134 // for volatile stores and loads because they can trap. The discussion on
135 // whether or not it is correct is still ongoing. We might want to get rid
136 // of this logic in the future. Anyways, trapping instructions shouldn't
137 // introduce implicit control flow, so we explicitly allow them here. This
138 // must be removed once isGuaranteedToTransferExecutionToSuccessor is fixed.
139 if (isGuaranteedToTransferExecutionToSuccessor(Insn))
140 return false;
141 if (isa<LoadInst>(Insn)) {
142 assert(cast<LoadInst>(Insn)->isVolatile() &&
143 "Non-volatile load should transfer execution to successor!");
144 return false;
145 }
146 if (isa<StoreInst>(Insn)) {
147 assert(cast<StoreInst>(Insn)->isVolatile() &&
148 "Non-volatile store should transfer execution to successor!");
149 return false;
150 }
151 return true;
152}
Max Kazantsev8e59cd12018-11-12 09:29:58 +0000153
154bool MemoryWriteTracking::isSpecialInstruction(
155 const Instruction *Insn) const {
156 return Insn->mayWriteToMemory();
157}