Coverage Report

Created: 2026-06-30 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/source/opt/def_use_manager.cpp
Line
Count
Source
1
// Copyright (c) 2016 Google Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "source/opt/def_use_manager.h"
16
17
namespace spvtools {
18
namespace opt {
19
namespace analysis {
20
21
168M
void DefUseManager::AnalyzeInstDef(Instruction* inst) {
22
168M
  const uint32_t def_id = inst->result_id();
23
168M
  if (def_id != 0) {
24
113M
    auto iter = id_to_def_.find(def_id);
25
113M
    if (iter != id_to_def_.end()) {
26
      // Clear the original instruction that defining the same result id of the
27
      // new instruction.
28
13.4k
      ClearInst(iter->second);
29
13.4k
    }
30
113M
    id_to_def_[def_id] = inst;
31
113M
  } else {
32
55.3M
    ClearInst(inst);
33
55.3M
  }
34
168M
}
35
36
186M
void DefUseManager::AnalyzeInstUse(Instruction* inst) {
37
  // Create entry for the given instruction. Note that the instruction may
38
  // not have any in-operands. In such cases, we still need a entry for those
39
  // instructions so this manager knows it has seen the instruction later.
40
186M
  auto* used_ids = &inst_to_used_ids_[inst];
41
186M
  if (used_ids->size()) {
42
10.9M
    EraseUseRecordsOfOperandIds(inst);
43
10.9M
    used_ids = &inst_to_used_ids_[inst];
44
10.9M
  }
45
186M
  used_ids->clear();  // It might have existed before.
46
47
698M
  for (uint32_t i = 0; i < inst->NumOperands(); ++i) {
48
511M
    switch (inst->GetOperand(i).type) {
49
      // For any id type but result id type
50
248M
      case SPV_OPERAND_TYPE_ID:
51
339M
      case SPV_OPERAND_TYPE_TYPE_ID:
52
339M
      case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
53
339M
      case SPV_OPERAND_TYPE_SCOPE_ID: {
54
339M
        uint32_t use_id = inst->GetSingleWordOperand(i);
55
339M
        Instruction* def = GetDef(use_id);
56
339M
        assert(def && "Definition is not registered.");
57
339M
        id_to_users_.insert(UserEntry{def, inst});
58
339M
        used_ids->push_back(use_id);
59
339M
      } break;
60
172M
      default:
61
172M
        break;
62
511M
    }
63
511M
  }
64
186M
}
65
66
2.55M
void DefUseManager::AnalyzeInstDefUse(Instruction* inst) {
67
2.55M
  AnalyzeInstDef(inst);
68
2.55M
  AnalyzeInstUse(inst);
69
  // Analyze lines last otherwise they will be cleared when inst is
70
  // cleared by preceding two calls
71
2.55M
  for (auto& l_inst : inst->dbg_line_insts()) AnalyzeInstDefUse(&l_inst);
72
2.55M
}
73
74
2.15M
void DefUseManager::UpdateDefUse(Instruction* inst) {
75
2.15M
  const uint32_t def_id = inst->result_id();
76
2.15M
  if (def_id != 0) {
77
1.83M
    auto iter = id_to_def_.find(def_id);
78
1.83M
    if (iter == id_to_def_.end()) {
79
0
      AnalyzeInstDef(inst);
80
0
    }
81
1.83M
  }
82
2.15M
  AnalyzeInstUse(inst);
83
2.15M
}
84
85
585M
Instruction* DefUseManager::GetDef(uint32_t id) {
86
585M
  auto iter = id_to_def_.find(id);
87
585M
  if (iter == id_to_def_.end()) return nullptr;
88
572M
  return iter->second;
89
585M
}
90
91
31.3M
const Instruction* DefUseManager::GetDef(uint32_t id) const {
92
31.3M
  const auto iter = id_to_def_.find(id);
93
31.3M
  if (iter == id_to_def_.end()) return nullptr;
94
31.3M
  return iter->second;
95
31.3M
}
96
97
DefUseManager::IdToUsersMap::const_iterator DefUseManager::UsersBegin(
98
25.1M
    const Instruction* def) const {
99
25.1M
  return id_to_users_.lower_bound(
100
25.1M
      UserEntry{const_cast<Instruction*>(def), nullptr});
101
25.1M
}
102
103
bool DefUseManager::UsersNotEnd(const IdToUsersMap::const_iterator& iter,
104
                                const IdToUsersMap::const_iterator& cached_end,
105
64.6M
                                const Instruction* inst) const {
106
64.6M
  return (iter != cached_end && iter->def == inst);
107
64.6M
}
108
109
bool DefUseManager::UsersNotEnd(const IdToUsersMap::const_iterator& iter,
110
0
                                const Instruction* inst) const {
111
0
  return UsersNotEnd(iter, id_to_users_.end(), inst);
112
0
}
113
114
bool DefUseManager::WhileEachUser(
115
9.75M
    const Instruction* def, const std::function<bool(Instruction*)>& f) const {
116
  // Ensure that |def| has been registered.
117
9.75M
  assert(def && (!def->HasResultId() || def == GetDef(def->result_id())) &&
118
9.75M
         "Definition is not registered.");
119
9.75M
  if (!def->HasResultId()) return true;
120
121
9.73M
  auto end = id_to_users_.end();
122
38.0M
  for (auto iter = UsersBegin(def); UsersNotEnd(iter, end, def); ++iter) {
123
28.6M
    if (!f(iter->user)) return false;
124
28.6M
  }
125
9.45M
  return true;
126
9.73M
}
127
128
bool DefUseManager::WhileEachUser(
129
1.67M
    uint32_t id, const std::function<bool(Instruction*)>& f) const {
130
1.67M
  return WhileEachUser(GetDef(id), f);
131
1.67M
}
132
133
void DefUseManager::ForEachUser(
134
7.61M
    const Instruction* def, const std::function<void(Instruction*)>& f) const {
135
15.6M
  WhileEachUser(def, [&f](Instruction* user) {
136
15.6M
    f(user);
137
15.6M
    return true;
138
15.6M
  });
139
7.61M
}
140
141
void DefUseManager::ForEachUser(
142
4.11M
    uint32_t id, const std::function<void(Instruction*)>& f) const {
143
4.11M
  ForEachUser(GetDef(id), f);
144
4.11M
}
145
146
bool DefUseManager::WhileEachUse(
147
    const Instruction* def,
148
8.18M
    const std::function<bool(Instruction*, uint32_t)>& f) const {
149
  // Ensure that |def| has been registered.
150
8.18M
  assert(def && (!def->HasResultId() || def == GetDef(def->result_id())) &&
151
8.18M
         "Definition is not registered.");
152
8.18M
  if (!def->HasResultId()) return true;
153
154
8.18M
  auto end = id_to_users_.end();
155
16.4M
  for (auto iter = UsersBegin(def); UsersNotEnd(iter, end, def); ++iter) {
156
8.47M
    Instruction* user = iter->user;
157
42.3M
    for (uint32_t idx = 0; idx != user->NumOperands(); ++idx) {
158
34.0M
      const Operand& op = user->GetOperand(idx);
159
34.0M
      if (op.type != SPV_OPERAND_TYPE_RESULT_ID && spvIsIdType(op.type)) {
160
28.7M
        if (def->result_id() == op.words[0]) {
161
8.62M
          if (!f(user, idx)) return false;
162
8.62M
        }
163
28.7M
      }
164
34.0M
    }
165
8.47M
  }
166
8.02M
  return true;
167
8.18M
}
168
169
bool DefUseManager::WhileEachUse(
170
3.66M
    uint32_t id, const std::function<bool(Instruction*, uint32_t)>& f) const {
171
3.66M
  return WhileEachUse(GetDef(id), f);
172
3.66M
}
173
174
void DefUseManager::ForEachUse(
175
    const Instruction* def,
176
4.51M
    const std::function<void(Instruction*, uint32_t)>& f) const {
177
5.67M
  WhileEachUse(def, [&f](Instruction* user, uint32_t index) {
178
5.67M
    f(user, index);
179
5.67M
    return true;
180
5.67M
  });
181
4.51M
}
182
183
void DefUseManager::ForEachUse(
184
3.95M
    uint32_t id, const std::function<void(Instruction*, uint32_t)>& f) const {
185
3.95M
  ForEachUse(GetDef(id), f);
186
3.95M
}
187
188
105k
uint32_t DefUseManager::NumUsers(const Instruction* def) const {
189
105k
  uint32_t count = 0;
190
299k
  ForEachUser(def, [&count](Instruction*) { ++count; });
191
105k
  return count;
192
105k
}
193
194
0
uint32_t DefUseManager::NumUsers(uint32_t id) const {
195
0
  return NumUsers(GetDef(id));
196
0
}
197
198
19.6k
uint32_t DefUseManager::NumUses(const Instruction* def) const {
199
19.6k
  uint32_t count = 0;
200
117k
  ForEachUse(def, [&count](Instruction*, uint32_t) { ++count; });
201
19.6k
  return count;
202
19.6k
}
203
204
0
uint32_t DefUseManager::NumUses(uint32_t id) const {
205
0
  return NumUses(GetDef(id));
206
0
}
207
208
0
std::vector<Instruction*> DefUseManager::GetAnnotations(uint32_t id) const {
209
0
  std::vector<Instruction*> annos;
210
0
  const Instruction* def = GetDef(id);
211
0
  if (!def) return annos;
212
213
0
  ForEachUser(def, [&annos](Instruction* user) {
214
0
    if (IsAnnotationInst(user->opcode())) {
215
0
      annos.push_back(user);
216
0
    }
217
0
  });
218
0
  return annos;
219
0
}
220
221
616k
void DefUseManager::AnalyzeDefUse(Module* module) {
222
616k
  if (!module) return;
223
  // Analyze all the defs before any uses to catch forward references.
224
616k
  module->ForEachInst(
225
616k
      std::bind(&DefUseManager::AnalyzeInstDef, this, std::placeholders::_1),
226
616k
      true);
227
616k
  module->ForEachInst(
228
616k
      std::bind(&DefUseManager::AnalyzeInstUse, this, std::placeholders::_1),
229
616k
      true);
230
616k
}
231
232
65.1M
void DefUseManager::ClearInst(Instruction* inst) {
233
65.1M
  auto iter = inst_to_used_ids_.find(inst);
234
65.1M
  if (iter != inst_to_used_ids_.end()) {
235
9.68M
    EraseUseRecordsOfOperandIds(inst);
236
9.68M
    if (inst->result_id() != 0) {
237
      // Remove all uses of this inst.
238
7.20M
      auto users_begin = UsersBegin(inst);
239
7.20M
      auto end = id_to_users_.end();
240
7.20M
      auto new_end = users_begin;
241
10.0M
      for (; UsersNotEnd(new_end, end, inst); ++new_end) {
242
2.86M
      }
243
7.20M
      id_to_users_.erase(users_begin, new_end);
244
7.20M
      id_to_def_.erase(inst->result_id());
245
7.20M
    }
246
9.68M
  }
247
65.1M
}
248
249
25.4M
void DefUseManager::EraseUseRecordsOfOperandIds(const Instruction* inst) {
250
  // Go through all ids used by this instruction, remove this instruction's
251
  // uses of them.
252
25.4M
  auto iter = inst_to_used_ids_.find(inst);
253
25.4M
  if (iter != inst_to_used_ids_.end()) {
254
69.6M
    for (auto use_id : iter->second) {
255
69.6M
      id_to_users_.erase(
256
69.6M
          UserEntry{GetDef(use_id), const_cast<Instruction*>(inst)});
257
69.6M
    }
258
25.4M
    inst_to_used_ids_.erase(iter);
259
25.4M
  }
260
25.4M
}
261
262
bool CompareAndPrintDifferences(const DefUseManager& lhs,
263
578k
                                const DefUseManager& rhs) {
264
578k
  bool same = true;
265
266
578k
  if (lhs.id_to_def_ != rhs.id_to_def_) {
267
0
    for (auto p : lhs.id_to_def_) {
268
0
      if (rhs.id_to_def_.find(p.first) == rhs.id_to_def_.end()) {
269
0
        printf("Diff in id_to_def: missing value in rhs\n");
270
0
      }
271
0
    }
272
0
    for (auto p : rhs.id_to_def_) {
273
0
      if (lhs.id_to_def_.find(p.first) == lhs.id_to_def_.end()) {
274
0
        printf("Diff in id_to_def: missing value in lhs\n");
275
0
      }
276
0
    }
277
0
    same = false;
278
0
  }
279
280
578k
  if (lhs.id_to_users_ != rhs.id_to_users_) {
281
0
    for (auto p : lhs.id_to_users_) {
282
0
      if (rhs.id_to_users_.count(p) == 0) {
283
0
        printf("Diff in id_to_users: missing value in rhs\n");
284
0
      }
285
0
    }
286
0
    for (auto p : rhs.id_to_users_) {
287
0
      if (lhs.id_to_users_.count(p) == 0) {
288
0
        printf("Diff in id_to_users: missing value in lhs\n");
289
0
      }
290
0
    }
291
0
    same = false;
292
0
  }
293
294
578k
  if (lhs.inst_to_used_ids_ != rhs.inst_to_used_ids_) {
295
0
    for (auto p : lhs.inst_to_used_ids_) {
296
0
      if (rhs.inst_to_used_ids_.count(p.first) == 0) {
297
0
        printf("Diff in inst_to_used_ids: missing value in rhs\n");
298
0
      }
299
0
    }
300
0
    for (auto p : rhs.inst_to_used_ids_) {
301
0
      if (lhs.inst_to_used_ids_.count(p.first) == 0) {
302
0
        printf("Diff in inst_to_used_ids: missing value in lhs\n");
303
0
      }
304
0
    }
305
0
    same = false;
306
0
  }
307
308
578k
  return same;
309
578k
}
310
311
}  // namespace analysis
312
}  // namespace opt
313
}  // namespace spvtools