1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019 Valve Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "aco_ir.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <algorithm>
28bf215546Sopenharmony_ci#include <vector>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci/*
31bf215546Sopenharmony_ci * Implements an analysis pass to determine the number of uses
32bf215546Sopenharmony_ci * for each SSA-definition.
33bf215546Sopenharmony_ci */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cinamespace aco {
36bf215546Sopenharmony_cinamespace {
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistruct dce_ctx {
39bf215546Sopenharmony_ci   int current_block;
40bf215546Sopenharmony_ci   std::vector<uint16_t> uses;
41bf215546Sopenharmony_ci   std::vector<std::vector<bool>> live;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   dce_ctx(Program* program)
44bf215546Sopenharmony_ci       : current_block(program->blocks.size() - 1), uses(program->peekAllocationId())
45bf215546Sopenharmony_ci   {
46bf215546Sopenharmony_ci      live.reserve(program->blocks.size());
47bf215546Sopenharmony_ci      for (Block& block : program->blocks)
48bf215546Sopenharmony_ci         live.emplace_back(block.instructions.size());
49bf215546Sopenharmony_ci   }
50bf215546Sopenharmony_ci};
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_civoid
53bf215546Sopenharmony_ciprocess_block(dce_ctx& ctx, Block& block)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci   std::vector<bool>& live = ctx.live[block.index];
56bf215546Sopenharmony_ci   assert(live.size() == block.instructions.size());
57bf215546Sopenharmony_ci   bool process_predecessors = false;
58bf215546Sopenharmony_ci   for (int idx = block.instructions.size() - 1; idx >= 0; idx--) {
59bf215546Sopenharmony_ci      if (live[idx])
60bf215546Sopenharmony_ci         continue;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci      aco_ptr<Instruction>& instr = block.instructions[idx];
63bf215546Sopenharmony_ci      if (!is_dead(ctx.uses, instr.get())) {
64bf215546Sopenharmony_ci         for (const Operand& op : instr->operands) {
65bf215546Sopenharmony_ci            if (op.isTemp()) {
66bf215546Sopenharmony_ci               if (ctx.uses[op.tempId()] == 0)
67bf215546Sopenharmony_ci                  process_predecessors = true;
68bf215546Sopenharmony_ci               ctx.uses[op.tempId()]++;
69bf215546Sopenharmony_ci            }
70bf215546Sopenharmony_ci         }
71bf215546Sopenharmony_ci         live[idx] = true;
72bf215546Sopenharmony_ci      }
73bf215546Sopenharmony_ci   }
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   if (process_predecessors) {
76bf215546Sopenharmony_ci      for (unsigned pred_idx : block.linear_preds)
77bf215546Sopenharmony_ci         ctx.current_block = std::max(ctx.current_block, (int)pred_idx);
78bf215546Sopenharmony_ci   }
79bf215546Sopenharmony_ci}
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci} /* end namespace */
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cibool
84bf215546Sopenharmony_ciis_dead(const std::vector<uint16_t>& uses, Instruction* instr)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   if (instr->definitions.empty() || instr->isBranch() ||
87bf215546Sopenharmony_ci       instr->opcode == aco_opcode::p_init_scratch)
88bf215546Sopenharmony_ci      return false;
89bf215546Sopenharmony_ci   if (std::any_of(instr->definitions.begin(), instr->definitions.end(),
90bf215546Sopenharmony_ci                   [&uses](const Definition& def) { return !def.isTemp() || uses[def.tempId()]; }))
91bf215546Sopenharmony_ci      return false;
92bf215546Sopenharmony_ci   return !(get_sync_info(instr).semantics & (semantic_volatile | semantic_acqrel));
93bf215546Sopenharmony_ci}
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_cistd::vector<uint16_t>
96bf215546Sopenharmony_cidead_code_analysis(Program* program)
97bf215546Sopenharmony_ci{
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   dce_ctx ctx(program);
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci   while (ctx.current_block >= 0) {
102bf215546Sopenharmony_ci      unsigned next_block = ctx.current_block--;
103bf215546Sopenharmony_ci      process_block(ctx, program->blocks[next_block]);
104bf215546Sopenharmony_ci   }
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   /* add one use to exec to prevent startpgm from being removed */
107bf215546Sopenharmony_ci   aco_ptr<Instruction>& startpgm = program->blocks[0].instructions[0];
108bf215546Sopenharmony_ci   assert(startpgm->opcode == aco_opcode::p_startpgm);
109bf215546Sopenharmony_ci   ctx.uses[startpgm->definitions.back().tempId()]++;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   return ctx.uses;
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci} // namespace aco
115