xref: /aosp_15_r20/external/mesa3d/src/amd/compiler/aco_dead_code_analysis.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2019 Valve Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #include "aco_ir.h"
8 
9 #include <algorithm>
10 #include <vector>
11 
12 /*
13  * Implements an analysis pass to determine the number of uses
14  * for each SSA-definition.
15  *
16  * This pass assumes that no loop header phis are dead code.
17  */
18 
19 namespace aco {
20 namespace {
21 
22 void
process_loop_header_phis(std::vector<uint16_t> & uses,Block & block)23 process_loop_header_phis(std::vector<uint16_t>& uses, Block& block)
24 {
25    for (aco_ptr<Instruction>& instr : block.instructions) {
26       if (!is_phi(instr))
27          return;
28       for (const Operand& op : instr->operands) {
29          if (op.isTemp())
30             uses[op.tempId()]++;
31       }
32    }
33 }
34 
35 void
process_block(std::vector<uint16_t> & uses,Block & block)36 process_block(std::vector<uint16_t>& uses, Block& block)
37 {
38    for (auto it = block.instructions.rbegin(); it != block.instructions.rend(); it++) {
39       aco_ptr<Instruction>& instr = *it;
40       if ((block.kind & block_kind_loop_header) && is_phi(instr))
41          break;
42 
43       if (!is_dead(uses, instr.get())) {
44          for (const Operand& op : instr->operands) {
45             if (op.isTemp())
46                uses[op.tempId()]++;
47          }
48       }
49    }
50 }
51 
52 } /* end namespace */
53 
54 std::vector<uint16_t>
dead_code_analysis(Program * program)55 dead_code_analysis(Program* program)
56 {
57    std::vector<uint16_t> uses(program->peekAllocationId());
58 
59    for (Block& block : program->blocks) {
60       if (block.kind & block_kind_loop_header)
61          process_loop_header_phis(uses, block);
62    }
63 
64    for (auto it = program->blocks.rbegin(); it != program->blocks.rend(); it++)
65       process_block(uses, *it);
66 
67    return uses;
68 }
69 
70 } // namespace aco
71