Path: blob/21.2-virgl/src/amd/compiler/aco_reindex_ssa.cpp
4550 views
/*1* Copyright © 2021 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 <vector>2728namespace aco {29namespace {3031struct idx_ctx {32std::vector<RegClass> temp_rc = {s1};33std::vector<uint32_t> renames;34};3536inline void37reindex_defs(idx_ctx& ctx, aco_ptr<Instruction>& instr)38{39for (Definition& def : instr->definitions) {40if (!def.isTemp())41continue;42uint32_t new_id = ctx.temp_rc.size();43RegClass rc = def.regClass();44ctx.renames[def.tempId()] = new_id;45ctx.temp_rc.emplace_back(rc);46def.setTemp(Temp(new_id, rc));47}48}4950inline void51reindex_ops(idx_ctx& ctx, aco_ptr<Instruction>& instr)52{53for (Operand& op : instr->operands) {54if (!op.isTemp())55continue;56uint32_t new_id = ctx.renames[op.tempId()];57assert(op.regClass() == ctx.temp_rc[new_id]);58op.setTemp(Temp(new_id, op.regClass()));59}60}6162void63reindex_program(idx_ctx& ctx, Program* program)64{65ctx.renames.resize(program->peekAllocationId());6667for (Block& block : program->blocks) {68auto it = block.instructions.begin();69/* for phis, only reindex the definitions */70while (is_phi(*it)) {71reindex_defs(ctx, *it++);72}73/* reindex all other instructions */74while (it != block.instructions.end()) {75reindex_defs(ctx, *it);76reindex_ops(ctx, *it);77++it;78}79}80/* update the phi operands */81for (Block& block : program->blocks) {82auto it = block.instructions.begin();83while (is_phi(*it)) {84reindex_ops(ctx, *it++);85}86}8788/* update program members */89program->private_segment_buffer = Temp(ctx.renames[program->private_segment_buffer.id()],90program->private_segment_buffer.regClass());91program->scratch_offset =92Temp(ctx.renames[program->scratch_offset.id()], program->scratch_offset.regClass());93program->temp_rc = ctx.temp_rc;94}9596void97update_live_out(idx_ctx& ctx, std::vector<IDSet>& live_out)98{99for (IDSet& set : live_out) {100IDSet new_set;101for (uint32_t id : set)102new_set.insert(ctx.renames[id]);103set = new_set;104}105}106107} /* end namespace */108109void110reindex_ssa(Program* program)111{112idx_ctx ctx;113reindex_program(ctx, program);114115program->allocationID = program->temp_rc.size();116}117118void119reindex_ssa(Program* program, std::vector<IDSet>& live_out)120{121idx_ctx ctx;122reindex_program(ctx, program);123update_live_out(ctx, live_out);124125program->allocationID = program->temp_rc.size();126}127128} // namespace aco129130131