2016-07-27 21:37:04 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2016-07-27 21:37:04 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-07-27 21:37:04 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-07-27 21:37:04 +00:00
|
|
|
|
|
|
|
#include "def_use_manager.h"
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2016-08-20 03:17:44 +00:00
|
|
|
#include "log.h"
|
2016-08-29 22:05:24 +00:00
|
|
|
#include "reflect.h"
|
2016-07-27 21:37:04 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
namespace analysis {
|
|
|
|
|
2017-06-16 21:37:31 +00:00
|
|
|
void DefUseManager::AnalyzeInstDef(ir::Instruction* inst) {
|
2016-08-12 14:05:58 +00:00
|
|
|
const uint32_t def_id = inst->result_id();
|
2016-08-16 17:06:03 +00:00
|
|
|
if (def_id != 0) {
|
|
|
|
auto iter = id_to_def_.find(def_id);
|
|
|
|
if (iter != id_to_def_.end()) {
|
|
|
|
// Clear the original instruction that defining the same result id of the
|
|
|
|
// new instruction.
|
|
|
|
ClearInst(iter->second);
|
|
|
|
}
|
|
|
|
id_to_def_[def_id] = inst;
|
2017-11-08 17:40:02 +00:00
|
|
|
} else {
|
2016-08-16 17:06:03 +00:00
|
|
|
ClearInst(inst);
|
|
|
|
}
|
2017-06-16 21:37:31 +00:00
|
|
|
}
|
2016-08-12 14:43:26 +00:00
|
|
|
|
2017-06-16 21:37:31 +00:00
|
|
|
void DefUseManager::AnalyzeInstUse(ir::Instruction* inst) {
|
2016-08-16 17:06:03 +00:00
|
|
|
// Create entry for the given instruction. Note that the instruction may
|
|
|
|
// not have any in-operands. In such cases, we still need a entry for those
|
|
|
|
// instructions so this manager knows it has seen the instruction later.
|
2018-01-26 12:07:10 +00:00
|
|
|
auto* used_ids = &inst_to_used_ids_[inst];
|
|
|
|
if (used_ids->size()) {
|
|
|
|
EraseUseRecordsOfOperandIds(inst);
|
|
|
|
used_ids = &inst_to_used_ids_[inst];
|
|
|
|
}
|
|
|
|
used_ids->clear(); // It might have existed before.
|
2016-08-12 14:05:58 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < inst->NumOperands(); ++i) {
|
|
|
|
switch (inst->GetOperand(i).type) {
|
|
|
|
// For any id type but result id type
|
2017-11-08 17:40:02 +00:00
|
|
|
case SPV_OPERAND_TYPE_ID:
|
|
|
|
case SPV_OPERAND_TYPE_TYPE_ID:
|
|
|
|
case SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID:
|
|
|
|
case SPV_OPERAND_TYPE_SCOPE_ID: {
|
|
|
|
uint32_t use_id = inst->GetSingleWordOperand(i);
|
2017-11-14 19:11:50 +00:00
|
|
|
ir::Instruction* def = GetDef(use_id);
|
|
|
|
assert(def && "Definition is not registered.");
|
|
|
|
id_to_users_.insert(UserEntry(def, inst));
|
2018-01-26 12:07:10 +00:00
|
|
|
used_ids->push_back(use_id);
|
2017-11-08 17:40:02 +00:00
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-08-12 14:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:37:31 +00:00
|
|
|
void DefUseManager::AnalyzeInstDefUse(ir::Instruction* inst) {
|
|
|
|
AnalyzeInstDef(inst);
|
|
|
|
AnalyzeInstUse(inst);
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:59:03 +00:00
|
|
|
void DefUseManager::UpdateDefUse(ir::Instruction* inst) {
|
|
|
|
const uint32_t def_id = inst->result_id();
|
|
|
|
if (def_id != 0) {
|
|
|
|
auto iter = id_to_def_.find(def_id);
|
2018-02-21 19:35:10 +00:00
|
|
|
if (iter == id_to_def_.end()) {
|
2018-02-08 15:59:03 +00:00
|
|
|
AnalyzeInstDef(inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AnalyzeInstUse(inst);
|
|
|
|
}
|
|
|
|
|
2016-07-27 21:37:04 +00:00
|
|
|
ir::Instruction* DefUseManager::GetDef(uint32_t id) {
|
2016-08-16 17:06:03 +00:00
|
|
|
auto iter = id_to_def_.find(id);
|
|
|
|
if (iter == id_to_def_.end()) return nullptr;
|
|
|
|
return iter->second;
|
2016-07-27 21:37:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
const ir::Instruction* DefUseManager::GetDef(uint32_t id) const {
|
|
|
|
const auto iter = id_to_def_.find(id);
|
|
|
|
if (iter == id_to_def_.end()) return nullptr;
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
DefUseManager::IdToUsersMap::const_iterator DefUseManager::UsersBegin(
|
|
|
|
const ir::Instruction* def) const {
|
|
|
|
return id_to_users_.lower_bound(
|
|
|
|
UserEntry(const_cast<ir::Instruction*>(def), nullptr));
|
2016-07-27 21:37:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
bool DefUseManager::UsersNotEnd(const IdToUsersMap::const_iterator& iter,
|
|
|
|
const IdToUsersMap::const_iterator& cached_end,
|
|
|
|
const ir::Instruction* inst) const {
|
|
|
|
return (iter != cached_end && iter->first == inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefUseManager::UsersNotEnd(const IdToUsersMap::const_iterator& iter,
|
|
|
|
const ir::Instruction* inst) const {
|
|
|
|
return UsersNotEnd(iter, id_to_users_.end(), inst);
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
bool DefUseManager::WhileEachUser(
|
2017-11-27 15:16:41 +00:00
|
|
|
const ir::Instruction* def,
|
2018-01-12 20:05:53 +00:00
|
|
|
const std::function<bool(ir::Instruction*)>& f) const {
|
2017-11-14 19:11:50 +00:00
|
|
|
// Ensure that |def| has been registered.
|
2018-01-26 12:07:10 +00:00
|
|
|
assert(def && (!def->HasResultId() || def == GetDef(def->result_id())) &&
|
2017-11-27 15:16:41 +00:00
|
|
|
"Definition is not registered.");
|
2018-01-26 12:07:10 +00:00
|
|
|
if (!def->HasResultId()) return true;
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
auto end = id_to_users_.end();
|
|
|
|
for (auto iter = UsersBegin(def); UsersNotEnd(iter, end, def); ++iter) {
|
2018-01-12 20:05:53 +00:00
|
|
|
if (!f(iter->second)) return false;
|
2017-11-14 19:11:50 +00:00
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefUseManager::WhileEachUser(
|
|
|
|
uint32_t id, const std::function<bool(ir::Instruction*)>& f) const {
|
|
|
|
return WhileEachUser(GetDef(id), f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefUseManager::ForEachUser(
|
|
|
|
const ir::Instruction* def,
|
|
|
|
const std::function<void(ir::Instruction*)>& f) const {
|
|
|
|
WhileEachUser(def, [&f](ir::Instruction* user) {
|
|
|
|
f(user);
|
|
|
|
return true;
|
|
|
|
});
|
2017-11-14 19:11:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
void DefUseManager::ForEachUser(
|
|
|
|
uint32_t id, const std::function<void(ir::Instruction*)>& f) const {
|
2017-11-14 19:11:50 +00:00
|
|
|
ForEachUser(GetDef(id), f);
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:05:53 +00:00
|
|
|
bool DefUseManager::WhileEachUse(
|
2017-11-27 15:16:41 +00:00
|
|
|
const ir::Instruction* def,
|
2018-01-12 20:05:53 +00:00
|
|
|
const std::function<bool(ir::Instruction*, uint32_t)>& f) const {
|
2017-11-14 19:11:50 +00:00
|
|
|
// Ensure that |def| has been registered.
|
2018-01-26 12:07:10 +00:00
|
|
|
assert(def && (!def->HasResultId() || def == GetDef(def->result_id())) &&
|
2017-11-27 15:16:41 +00:00
|
|
|
"Definition is not registered.");
|
2018-01-26 12:07:10 +00:00
|
|
|
if (!def->HasResultId()) return true;
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
auto end = id_to_users_.end();
|
|
|
|
for (auto iter = UsersBegin(def); UsersNotEnd(iter, end, def); ++iter) {
|
|
|
|
ir::Instruction* user = iter->second;
|
|
|
|
for (uint32_t idx = 0; idx != user->NumOperands(); ++idx) {
|
|
|
|
const ir::Operand& op = user->GetOperand(idx);
|
|
|
|
if (op.type != SPV_OPERAND_TYPE_RESULT_ID && spvIsIdType(op.type)) {
|
2018-01-12 20:05:53 +00:00
|
|
|
if (def->result_id() == op.words[0]) {
|
|
|
|
if (!f(user, idx)) return false;
|
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 20:05:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefUseManager::WhileEachUse(
|
|
|
|
uint32_t id,
|
|
|
|
const std::function<bool(ir::Instruction*, uint32_t)>& f) const {
|
|
|
|
return WhileEachUse(GetDef(id), f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefUseManager::ForEachUse(
|
|
|
|
const ir::Instruction* def,
|
|
|
|
const std::function<void(ir::Instruction*, uint32_t)>& f) const {
|
|
|
|
WhileEachUse(def, [&f](ir::Instruction* user, uint32_t index) {
|
|
|
|
f(user, index);
|
|
|
|
return true;
|
|
|
|
});
|
2017-11-14 19:11:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
void DefUseManager::ForEachUse(
|
|
|
|
uint32_t id,
|
|
|
|
const std::function<void(ir::Instruction*, uint32_t)>& f) const {
|
2017-11-14 19:11:50 +00:00
|
|
|
ForEachUse(GetDef(id), f);
|
2016-08-29 22:05:24 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 22:03:06 +00:00
|
|
|
uint32_t DefUseManager::NumUsers(const ir::Instruction* def) const {
|
|
|
|
uint32_t count = 0;
|
|
|
|
ForEachUser(def, [&count](ir::Instruction*) { ++count; });
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DefUseManager::NumUsers(uint32_t id) const {
|
|
|
|
return NumUsers(GetDef(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DefUseManager::NumUses(const ir::Instruction* def) const {
|
|
|
|
uint32_t count = 0;
|
|
|
|
ForEachUse(def, [&count](ir::Instruction*, uint32_t) { ++count; });
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DefUseManager::NumUses(uint32_t id) const {
|
|
|
|
return NumUses(GetDef(id));
|
|
|
|
}
|
|
|
|
|
2016-08-20 03:17:44 +00:00
|
|
|
std::vector<ir::Instruction*> DefUseManager::GetAnnotations(uint32_t id) const {
|
2016-08-29 22:05:24 +00:00
|
|
|
std::vector<ir::Instruction*> annos;
|
2017-11-14 19:11:50 +00:00
|
|
|
const ir::Instruction* def = GetDef(id);
|
|
|
|
if (!def) return annos;
|
|
|
|
|
|
|
|
ForEachUser(def, [&annos](ir::Instruction* user) {
|
|
|
|
if (ir::IsAnnotationInst(user->opcode())) {
|
|
|
|
annos.push_back(user);
|
2016-08-29 22:05:24 +00:00
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
});
|
2016-08-29 22:05:24 +00:00
|
|
|
return annos;
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:41:47 +00:00
|
|
|
void DefUseManager::AnalyzeDefUse(ir::Module* module) {
|
2016-08-16 17:06:03 +00:00
|
|
|
if (!module) return;
|
2017-11-14 19:11:50 +00:00
|
|
|
// Analyze all the defs before any uses to catch forward references.
|
2017-11-27 15:16:41 +00:00
|
|
|
module->ForEachInst(
|
|
|
|
std::bind(&DefUseManager::AnalyzeInstDef, this, std::placeholders::_1));
|
|
|
|
module->ForEachInst(
|
|
|
|
std::bind(&DefUseManager::AnalyzeInstUse, this, std::placeholders::_1));
|
2016-08-15 17:41:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 17:06:03 +00:00
|
|
|
void DefUseManager::ClearInst(ir::Instruction* inst) {
|
|
|
|
auto iter = inst_to_used_ids_.find(inst);
|
|
|
|
if (iter != inst_to_used_ids_.end()) {
|
|
|
|
EraseUseRecordsOfOperandIds(inst);
|
|
|
|
if (inst->result_id() != 0) {
|
2017-11-14 19:11:50 +00:00
|
|
|
// Remove all uses of this inst.
|
|
|
|
auto users_begin = UsersBegin(inst);
|
|
|
|
auto end = id_to_users_.end();
|
|
|
|
auto new_end = users_begin;
|
2017-11-27 15:16:41 +00:00
|
|
|
for (; UsersNotEnd(new_end, end, inst); ++new_end) {
|
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
id_to_users_.erase(users_begin, new_end);
|
2016-08-16 17:06:03 +00:00
|
|
|
id_to_def_.erase(inst->result_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 14:43:26 +00:00
|
|
|
|
2016-08-16 17:06:03 +00:00
|
|
|
void DefUseManager::EraseUseRecordsOfOperandIds(const ir::Instruction* inst) {
|
2016-08-12 14:43:26 +00:00
|
|
|
// Go through all ids used by this instruction, remove this instruction's
|
|
|
|
// uses of them.
|
2016-08-16 17:06:03 +00:00
|
|
|
auto iter = inst_to_used_ids_.find(inst);
|
|
|
|
if (iter != inst_to_used_ids_.end()) {
|
2017-11-14 19:11:50 +00:00
|
|
|
for (auto use_id : iter->second) {
|
2017-11-27 15:16:41 +00:00
|
|
|
id_to_users_.erase(
|
|
|
|
UserEntry(GetDef(use_id), const_cast<ir::Instruction*>(inst)));
|
2016-08-12 14:43:26 +00:00
|
|
|
}
|
2016-08-16 17:06:03 +00:00
|
|
|
inst_to_used_ids_.erase(inst);
|
2016-08-12 14:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:24:41 +00:00
|
|
|
bool operator==(const DefUseManager& lhs, const DefUseManager& rhs) {
|
|
|
|
if (lhs.id_to_def_ != rhs.id_to_def_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
if (lhs.id_to_users_ != rhs.id_to_users_) {
|
2018-03-06 16:20:28 +00:00
|
|
|
for (auto p : lhs.id_to_users_) {
|
|
|
|
if (rhs.id_to_users_.count(p) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto p : rhs.id_to_users_) {
|
|
|
|
if (lhs.id_to_users_.count(p) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-11-14 19:11:50 +00:00
|
|
|
return false;
|
2017-11-09 16:24:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lhs.inst_to_used_ids_ != lhs.inst_to_used_ids_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-27 21:37:04 +00:00
|
|
|
} // namespace analysis
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|