1 /*
2  * Copyright © 2021 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 <vector>
28 
29 namespace aco {
30 namespace {
31 
32 struct idx_ctx {
33    std::vector<RegClass> temp_rc = {s1};
34    std::vector<uint32_t> renames;
35 };
36 
37 inline void
reindex_defs(idx_ctx & ctx,aco_ptr<Instruction> & instr)38 reindex_defs(idx_ctx& ctx, aco_ptr<Instruction>& instr)
39 {
40    for (Definition& def : instr->definitions) {
41       if (!def.isTemp())
42          continue;
43       uint32_t new_id = ctx.temp_rc.size();
44       RegClass rc = def.regClass();
45       ctx.renames[def.tempId()] = new_id;
46       ctx.temp_rc.emplace_back(rc);
47       def.setTemp(Temp(new_id, rc));
48    }
49 }
50 
51 inline void
reindex_ops(idx_ctx & ctx,aco_ptr<Instruction> & instr)52 reindex_ops(idx_ctx& ctx, aco_ptr<Instruction>& instr)
53 {
54    for (Operand& op : instr->operands) {
55       if (!op.isTemp())
56          continue;
57       uint32_t new_id = ctx.renames[op.tempId()];
58       assert(op.regClass() == ctx.temp_rc[new_id]);
59       op.setTemp(Temp(new_id, op.regClass()));
60    }
61 }
62 
63 void
reindex_program(idx_ctx & ctx,Program * program)64 reindex_program(idx_ctx& ctx, Program* program)
65 {
66    ctx.renames.resize(program->peekAllocationId());
67 
68    for (Block& block : program->blocks) {
69       auto it = block.instructions.begin();
70       /* for phis, only reindex the definitions */
71       while (is_phi(*it)) {
72          reindex_defs(ctx, *it++);
73       }
74       /* reindex all other instructions */
75       while (it != block.instructions.end()) {
76          reindex_defs(ctx, *it);
77          reindex_ops(ctx, *it);
78          ++it;
79       }
80    }
81    /* update the phi operands */
82    for (Block& block : program->blocks) {
83       auto it = block.instructions.begin();
84       while (is_phi(*it)) {
85          reindex_ops(ctx, *it++);
86       }
87    }
88 
89    /* update program members */
90    program->private_segment_buffer = Temp(ctx.renames[program->private_segment_buffer.id()],
91                                           program->private_segment_buffer.regClass());
92    program->scratch_offset =
93       Temp(ctx.renames[program->scratch_offset.id()], program->scratch_offset.regClass());
94    program->temp_rc = ctx.temp_rc;
95 }
96 
97 void
update_live_out(idx_ctx & ctx,std::vector<IDSet> & live_out)98 update_live_out(idx_ctx& ctx, std::vector<IDSet>& live_out)
99 {
100    for (IDSet& set : live_out) {
101       IDSet new_set;
102       for (uint32_t id : set)
103          new_set.insert(ctx.renames[id]);
104       set = new_set;
105    }
106 }
107 
108 } /* end namespace */
109 
110 void
reindex_ssa(Program * program)111 reindex_ssa(Program* program)
112 {
113    idx_ctx ctx;
114    reindex_program(ctx, program);
115 
116    program->allocationID = program->temp_rc.size();
117 }
118 
119 void
reindex_ssa(Program * program,std::vector<IDSet> & live_out)120 reindex_ssa(Program* program, std::vector<IDSet>& live_out)
121 {
122    idx_ctx ctx;
123    reindex_program(ctx, program);
124    update_live_out(ctx, live_out);
125 
126    program->allocationID = program->temp_rc.size();
127 }
128 
129 } // namespace aco
130