17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Valve Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg */
247ec681f3Smrg
257ec681f3Smrg#include "aco_ir.h"
267ec681f3Smrg
277ec681f3Smrg#include <vector>
287ec681f3Smrg
297ec681f3Smrgnamespace aco {
307ec681f3Smrgnamespace {
317ec681f3Smrg
327ec681f3Smrgstruct idx_ctx {
337ec681f3Smrg   std::vector<RegClass> temp_rc = {s1};
347ec681f3Smrg   std::vector<uint32_t> renames;
357ec681f3Smrg};
367ec681f3Smrg
377ec681f3Smrginline void
387ec681f3Smrgreindex_defs(idx_ctx& ctx, aco_ptr<Instruction>& instr)
397ec681f3Smrg{
407ec681f3Smrg   for (Definition& def : instr->definitions) {
417ec681f3Smrg      if (!def.isTemp())
427ec681f3Smrg         continue;
437ec681f3Smrg      uint32_t new_id = ctx.temp_rc.size();
447ec681f3Smrg      RegClass rc = def.regClass();
457ec681f3Smrg      ctx.renames[def.tempId()] = new_id;
467ec681f3Smrg      ctx.temp_rc.emplace_back(rc);
477ec681f3Smrg      def.setTemp(Temp(new_id, rc));
487ec681f3Smrg   }
497ec681f3Smrg}
507ec681f3Smrg
517ec681f3Smrginline void
527ec681f3Smrgreindex_ops(idx_ctx& ctx, aco_ptr<Instruction>& instr)
537ec681f3Smrg{
547ec681f3Smrg   for (Operand& op : instr->operands) {
557ec681f3Smrg      if (!op.isTemp())
567ec681f3Smrg         continue;
577ec681f3Smrg      uint32_t new_id = ctx.renames[op.tempId()];
587ec681f3Smrg      assert(op.regClass() == ctx.temp_rc[new_id]);
597ec681f3Smrg      op.setTemp(Temp(new_id, op.regClass()));
607ec681f3Smrg   }
617ec681f3Smrg}
627ec681f3Smrg
637ec681f3Smrgvoid
647ec681f3Smrgreindex_program(idx_ctx& ctx, Program* program)
657ec681f3Smrg{
667ec681f3Smrg   ctx.renames.resize(program->peekAllocationId());
677ec681f3Smrg
687ec681f3Smrg   for (Block& block : program->blocks) {
697ec681f3Smrg      auto it = block.instructions.begin();
707ec681f3Smrg      /* for phis, only reindex the definitions */
717ec681f3Smrg      while (is_phi(*it)) {
727ec681f3Smrg         reindex_defs(ctx, *it++);
737ec681f3Smrg      }
747ec681f3Smrg      /* reindex all other instructions */
757ec681f3Smrg      while (it != block.instructions.end()) {
767ec681f3Smrg         reindex_defs(ctx, *it);
777ec681f3Smrg         reindex_ops(ctx, *it);
787ec681f3Smrg         ++it;
797ec681f3Smrg      }
807ec681f3Smrg   }
817ec681f3Smrg   /* update the phi operands */
827ec681f3Smrg   for (Block& block : program->blocks) {
837ec681f3Smrg      auto it = block.instructions.begin();
847ec681f3Smrg      while (is_phi(*it)) {
857ec681f3Smrg         reindex_ops(ctx, *it++);
867ec681f3Smrg      }
877ec681f3Smrg   }
887ec681f3Smrg
897ec681f3Smrg   /* update program members */
907ec681f3Smrg   program->private_segment_buffer = Temp(ctx.renames[program->private_segment_buffer.id()],
917ec681f3Smrg                                          program->private_segment_buffer.regClass());
927ec681f3Smrg   program->scratch_offset =
937ec681f3Smrg      Temp(ctx.renames[program->scratch_offset.id()], program->scratch_offset.regClass());
947ec681f3Smrg   program->temp_rc = ctx.temp_rc;
957ec681f3Smrg}
967ec681f3Smrg
977ec681f3Smrgvoid
987ec681f3Smrgupdate_live_out(idx_ctx& ctx, std::vector<IDSet>& live_out)
997ec681f3Smrg{
1007ec681f3Smrg   for (IDSet& set : live_out) {
1017ec681f3Smrg      IDSet new_set;
1027ec681f3Smrg      for (uint32_t id : set)
1037ec681f3Smrg         new_set.insert(ctx.renames[id]);
1047ec681f3Smrg      set = new_set;
1057ec681f3Smrg   }
1067ec681f3Smrg}
1077ec681f3Smrg
1087ec681f3Smrg} /* end namespace */
1097ec681f3Smrg
1107ec681f3Smrgvoid
1117ec681f3Smrgreindex_ssa(Program* program)
1127ec681f3Smrg{
1137ec681f3Smrg   idx_ctx ctx;
1147ec681f3Smrg   reindex_program(ctx, program);
1157ec681f3Smrg
1167ec681f3Smrg   program->allocationID = program->temp_rc.size();
1177ec681f3Smrg}
1187ec681f3Smrg
1197ec681f3Smrgvoid
1207ec681f3Smrgreindex_ssa(Program* program, std::vector<IDSet>& live_out)
1217ec681f3Smrg{
1227ec681f3Smrg   idx_ctx ctx;
1237ec681f3Smrg   reindex_program(ctx, program);
1247ec681f3Smrg   update_live_out(ctx, live_out);
1257ec681f3Smrg
1267ec681f3Smrg   program->allocationID = program->temp_rc.size();
1277ec681f3Smrg}
1287ec681f3Smrg
1297ec681f3Smrg} // namespace aco
130