• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2019 Valve Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include "aco_ir.h"
26 
27 #include <algorithm>
28 #include <vector>
29 
30 /*
31  * Implements an analysis pass to determine the number of uses
32  * for each SSA-definition.
33  *
34  * This pass assumes that no loop header phis are dead code.
35  */
36 
37 namespace aco {
38 namespace {
39 
40 void
process_loop_header_phis(std::vector<uint16_t> & uses,Block & block)41 process_loop_header_phis(std::vector<uint16_t>& uses, Block& block)
42 {
43    for (aco_ptr<Instruction>& instr : block.instructions) {
44       if (!is_phi(instr))
45          return;
46       for (const Operand& op : instr->operands) {
47          if (op.isTemp())
48             uses[op.tempId()]++;
49       }
50    }
51 }
52 
53 void
process_block(std::vector<uint16_t> & uses,Block & block)54 process_block(std::vector<uint16_t>& uses, Block& block)
55 {
56    for (auto it = block.instructions.rbegin(); it != block.instructions.rend(); it++) {
57       aco_ptr<Instruction>& instr = *it;
58       if ((block.kind & block_kind_loop_header) && is_phi(instr))
59          break;
60 
61       if (!is_dead(uses, instr.get())) {
62          for (const Operand& op : instr->operands) {
63             if (op.isTemp())
64                uses[op.tempId()]++;
65          }
66       }
67    }
68 }
69 
70 } /* end namespace */
71 
72 std::vector<uint16_t>
dead_code_analysis(Program * program)73 dead_code_analysis(Program* program)
74 {
75    std::vector<uint16_t> uses(program->peekAllocationId());
76 
77    for (Block& block : program->blocks) {
78       if (block.kind & block_kind_loop_header)
79          process_loop_header_phis(uses, block);
80    }
81 
82    for (auto it = program->blocks.rbegin(); it != program->blocks.rend(); it++)
83       process_block(uses, *it);
84 
85    return uses;
86 }
87 
88 } // namespace aco
89