Path: blob/21.2-virgl/src/amd/compiler/aco_dead_code_analysis.cpp
4550 views
/*1* Copyright © 2019 Valve Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22*/2324#include "aco_ir.h"2526#include <algorithm>27#include <vector>2829/*30* Implements an analysis pass to determine the number of uses31* for each SSA-definition.32*/3334namespace aco {35namespace {3637struct dce_ctx {38int current_block;39std::vector<uint16_t> uses;40std::vector<std::vector<bool>> live;4142dce_ctx(Program* program)43: current_block(program->blocks.size() - 1), uses(program->peekAllocationId())44{45live.reserve(program->blocks.size());46for (Block& block : program->blocks)47live.emplace_back(block.instructions.size());48}49};5051void52process_block(dce_ctx& ctx, Block& block)53{54std::vector<bool>& live = ctx.live[block.index];55assert(live.size() == block.instructions.size());56bool process_predecessors = false;57for (int idx = block.instructions.size() - 1; idx >= 0; idx--) {58if (live[idx])59continue;6061aco_ptr<Instruction>& instr = block.instructions[idx];62if (!is_dead(ctx.uses, instr.get())) {63for (const Operand& op : instr->operands) {64if (op.isTemp()) {65if (ctx.uses[op.tempId()] == 0)66process_predecessors = true;67ctx.uses[op.tempId()]++;68}69}70live[idx] = true;71}72}7374if (process_predecessors) {75for (unsigned pred_idx : block.linear_preds)76ctx.current_block = std::max(ctx.current_block, (int)pred_idx);77}78}7980} /* end namespace */8182bool83is_dead(const std::vector<uint16_t>& uses, Instruction* instr)84{85if (instr->definitions.empty() || instr->isBranch())86return false;87if (std::any_of(instr->definitions.begin(), instr->definitions.end(),88[&uses](const Definition& def) { return !def.isTemp() || uses[def.tempId()]; }))89return false;90return !(get_sync_info(instr).semantics & (semantic_volatile | semantic_acqrel));91}9293std::vector<uint16_t>94dead_code_analysis(Program* program)95{9697dce_ctx ctx(program);9899while (ctx.current_block >= 0) {100unsigned next_block = ctx.current_block--;101process_block(ctx, program->blocks[next_block]);102}103104/* add one use to exec to prevent startpgm from being removed */105aco_ptr<Instruction>& startpgm = program->blocks[0].instructions[0];106assert(startpgm->opcode == aco_opcode::p_startpgm);107ctx.uses[startpgm->definitions.back().tempId()]++;108109return ctx.uses;110}111112} // namespace aco113114115