annotate llvm/lib/Analysis/InstructionPrecedenceTracking.cpp @ 220:42394fc6a535

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