2016-06-28 18:58:17 +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-06-28 18:58:17 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-28 18:58:17 +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-06-28 18:58:17 +00:00
|
|
|
|
2016-08-29 19:46:09 +00:00
|
|
|
#include "eliminate_dead_constant_pass.h"
|
2016-06-28 18:58:17 +00:00
|
|
|
|
2016-07-29 15:35:58 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
2016-08-29 19:46:09 +00:00
|
|
|
#include "def_use_manager.h"
|
2017-11-08 17:40:02 +00:00
|
|
|
#include "ir_context.h"
|
2016-08-20 03:17:44 +00:00
|
|
|
#include "log.h"
|
2016-07-29 15:35:58 +00:00
|
|
|
#include "reflect.h"
|
|
|
|
|
2016-06-28 18:58:17 +00:00
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
Pass::Status EliminateDeadConstantPass::Process(ir::IRContext* irContext) {
|
2016-07-29 15:35:58 +00:00
|
|
|
std::unordered_set<ir::Instruction*> working_list;
|
|
|
|
// Traverse all the instructions to get the initial set of dead constants as
|
|
|
|
// working list and count number of real uses for constants. Uses in
|
|
|
|
// annotation instructions do not count.
|
|
|
|
std::unordered_map<ir::Instruction*, size_t> use_counts;
|
2017-10-30 15:13:24 +00:00
|
|
|
std::vector<ir::Instruction*> constants = irContext->GetConstants();
|
2016-07-29 15:35:58 +00:00
|
|
|
for (auto* c : constants) {
|
|
|
|
uint32_t const_id = c->result_id();
|
|
|
|
size_t count = 0;
|
2017-11-14 19:11:50 +00:00
|
|
|
irContext->get_def_use_mgr()->ForEachUse(
|
|
|
|
const_id, [&count](ir::Instruction* user, uint32_t index) {
|
|
|
|
(void)index;
|
|
|
|
SpvOp op = user->opcode();
|
|
|
|
if (!(ir::IsAnnotationInst(op) || ir::IsDebug1Inst(op) ||
|
|
|
|
ir::IsDebug2Inst(op) || ir::IsDebug3Inst(op))) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
});
|
2016-07-29 15:35:58 +00:00
|
|
|
use_counts[c] = count;
|
|
|
|
if (!count) {
|
|
|
|
working_list.insert(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start from the constants with 0 uses, back trace through the def-use chain
|
|
|
|
// to find all dead constants.
|
|
|
|
std::unordered_set<ir::Instruction*> dead_consts;
|
|
|
|
while (!working_list.empty()) {
|
|
|
|
ir::Instruction* inst = *working_list.begin();
|
|
|
|
// Back propagate if the instruction contains IDs in its operands.
|
|
|
|
switch (inst->opcode()) {
|
|
|
|
case SpvOp::SpvOpConstantComposite:
|
|
|
|
case SpvOp::SpvOpSpecConstantComposite:
|
|
|
|
case SpvOp::SpvOpSpecConstantOp:
|
|
|
|
for (uint32_t i = 0; i < inst->NumInOperands(); i++) {
|
|
|
|
// SpecConstantOp instruction contains 'opcode' as its operand. Need
|
|
|
|
// to exclude such operands when decreasing uses.
|
|
|
|
if (inst->GetInOperand(i).type != SPV_OPERAND_TYPE_ID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint32_t operand_id = inst->GetSingleWordInOperand(i);
|
2017-11-08 17:40:02 +00:00
|
|
|
ir::Instruction* def_inst =
|
|
|
|
irContext->get_def_use_mgr()->GetDef(operand_id);
|
2016-07-29 15:35:58 +00:00
|
|
|
// If the use_count does not have any count for the def_inst,
|
|
|
|
// def_inst must not be a constant, and should be ignored here.
|
|
|
|
if (!use_counts.count(def_inst)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// The number of uses should never be less then 0, so it can not be
|
|
|
|
// less than 1 before it decreases.
|
2016-08-20 03:17:44 +00:00
|
|
|
SPIRV_ASSERT(consumer(), use_counts[def_inst] > 0);
|
2016-07-29 15:35:58 +00:00
|
|
|
--use_counts[def_inst];
|
|
|
|
if (!use_counts[def_inst]) {
|
|
|
|
working_list.insert(def_inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dead_consts.insert(inst);
|
|
|
|
working_list.erase(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn all dead instructions and uses of them to nop
|
|
|
|
for (auto* dc : dead_consts) {
|
2017-11-02 18:25:48 +00:00
|
|
|
irContext->KillDef(dc->result_id());
|
2016-07-29 15:35:58 +00:00
|
|
|
}
|
2016-09-12 16:39:44 +00:00
|
|
|
return dead_consts.empty() ? Status::SuccessWithoutChange
|
|
|
|
: Status::SuccessWithChange;
|
2016-07-29 15:35:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 18:58:17 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|