17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2018 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 "util/memstream.h"
287ec681f3Smrg
297ec681f3Smrg#include <array>
307ec681f3Smrg#include <map>
317ec681f3Smrg#include <set>
327ec681f3Smrg#include <vector>
337ec681f3Smrg
347ec681f3Smrgnamespace aco {
357ec681f3Smrg
367ec681f3Smrgstatic void
377ec681f3Smrgaco_log(Program* program, enum radv_compiler_debug_level level, const char* prefix,
387ec681f3Smrg        const char* file, unsigned line, const char* fmt, va_list args)
397ec681f3Smrg{
407ec681f3Smrg   char* msg;
417ec681f3Smrg
427ec681f3Smrg   if (program->debug.shorten_messages) {
437ec681f3Smrg      msg = ralloc_vasprintf(NULL, fmt, args);
447ec681f3Smrg   } else {
457ec681f3Smrg      msg = ralloc_strdup(NULL, prefix);
467ec681f3Smrg      ralloc_asprintf_append(&msg, "    In file %s:%u\n", file, line);
477ec681f3Smrg      ralloc_asprintf_append(&msg, "    ");
487ec681f3Smrg      ralloc_vasprintf_append(&msg, fmt, args);
497ec681f3Smrg   }
507ec681f3Smrg
517ec681f3Smrg   if (program->debug.func)
527ec681f3Smrg      program->debug.func(program->debug.private_data, level, msg);
537ec681f3Smrg
547ec681f3Smrg   fprintf(program->debug.output, "%s\n", msg);
557ec681f3Smrg
567ec681f3Smrg   ralloc_free(msg);
577ec681f3Smrg}
587ec681f3Smrg
597ec681f3Smrgvoid
607ec681f3Smrg_aco_perfwarn(Program* program, const char* file, unsigned line, const char* fmt, ...)
617ec681f3Smrg{
627ec681f3Smrg   va_list args;
637ec681f3Smrg
647ec681f3Smrg   va_start(args, fmt);
657ec681f3Smrg   aco_log(program, RADV_COMPILER_DEBUG_LEVEL_PERFWARN, "ACO PERFWARN:\n", file, line, fmt, args);
667ec681f3Smrg   va_end(args);
677ec681f3Smrg}
687ec681f3Smrg
697ec681f3Smrgvoid
707ec681f3Smrg_aco_err(Program* program, const char* file, unsigned line, const char* fmt, ...)
717ec681f3Smrg{
727ec681f3Smrg   va_list args;
737ec681f3Smrg
747ec681f3Smrg   va_start(args, fmt);
757ec681f3Smrg   aco_log(program, RADV_COMPILER_DEBUG_LEVEL_ERROR, "ACO ERROR:\n", file, line, fmt, args);
767ec681f3Smrg   va_end(args);
777ec681f3Smrg}
787ec681f3Smrg
797ec681f3Smrgbool
807ec681f3Smrgvalidate_ir(Program* program)
817ec681f3Smrg{
827ec681f3Smrg   bool is_valid = true;
837ec681f3Smrg   auto check = [&program, &is_valid](bool success, const char* msg,
847ec681f3Smrg                                      aco::Instruction* instr) -> void
857ec681f3Smrg   {
867ec681f3Smrg      if (!success) {
877ec681f3Smrg         char* out;
887ec681f3Smrg         size_t outsize;
897ec681f3Smrg         struct u_memstream mem;
907ec681f3Smrg         u_memstream_open(&mem, &out, &outsize);
917ec681f3Smrg         FILE* const memf = u_memstream_get(&mem);
927ec681f3Smrg
937ec681f3Smrg         fprintf(memf, "%s: ", msg);
947ec681f3Smrg         aco_print_instr(instr, memf);
957ec681f3Smrg         u_memstream_close(&mem);
967ec681f3Smrg
977ec681f3Smrg         aco_err(program, "%s", out);
987ec681f3Smrg         free(out);
997ec681f3Smrg
1007ec681f3Smrg         is_valid = false;
1017ec681f3Smrg      }
1027ec681f3Smrg   };
1037ec681f3Smrg
1047ec681f3Smrg   auto check_block = [&program, &is_valid](bool success, const char* msg,
1057ec681f3Smrg                                            aco::Block* block) -> void
1067ec681f3Smrg   {
1077ec681f3Smrg      if (!success) {
1087ec681f3Smrg         aco_err(program, "%s: BB%u", msg, block->index);
1097ec681f3Smrg         is_valid = false;
1107ec681f3Smrg      }
1117ec681f3Smrg   };
1127ec681f3Smrg
1137ec681f3Smrg   for (Block& block : program->blocks) {
1147ec681f3Smrg      for (aco_ptr<Instruction>& instr : block.instructions) {
1157ec681f3Smrg
1167ec681f3Smrg         /* check base format */
1177ec681f3Smrg         Format base_format = instr->format;
1187ec681f3Smrg         base_format = (Format)((uint32_t)base_format & ~(uint32_t)Format::SDWA);
1197ec681f3Smrg         base_format = (Format)((uint32_t)base_format & ~(uint32_t)Format::DPP);
1207ec681f3Smrg         if ((uint32_t)base_format & (uint32_t)Format::VOP1)
1217ec681f3Smrg            base_format = Format::VOP1;
1227ec681f3Smrg         else if ((uint32_t)base_format & (uint32_t)Format::VOP2)
1237ec681f3Smrg            base_format = Format::VOP2;
1247ec681f3Smrg         else if ((uint32_t)base_format & (uint32_t)Format::VOPC)
1257ec681f3Smrg            base_format = Format::VOPC;
1267ec681f3Smrg         else if ((uint32_t)base_format & (uint32_t)Format::VINTRP) {
1277ec681f3Smrg            if (instr->opcode == aco_opcode::v_interp_p1ll_f16 ||
1287ec681f3Smrg                instr->opcode == aco_opcode::v_interp_p1lv_f16 ||
1297ec681f3Smrg                instr->opcode == aco_opcode::v_interp_p2_legacy_f16 ||
1307ec681f3Smrg                instr->opcode == aco_opcode::v_interp_p2_f16) {
1317ec681f3Smrg               /* v_interp_*_fp16 are considered VINTRP by the compiler but
1327ec681f3Smrg                * they are emitted as VOP3.
1337ec681f3Smrg                */
1347ec681f3Smrg               base_format = Format::VOP3;
1357ec681f3Smrg            } else {
1367ec681f3Smrg               base_format = Format::VINTRP;
1377ec681f3Smrg            }
1387ec681f3Smrg         }
1397ec681f3Smrg         check(base_format == instr_info.format[(int)instr->opcode],
1407ec681f3Smrg               "Wrong base format for instruction", instr.get());
1417ec681f3Smrg
1427ec681f3Smrg         /* check VOP3 modifiers */
1437ec681f3Smrg         if (instr->isVOP3() && instr->format != Format::VOP3) {
1447ec681f3Smrg            check(base_format == Format::VOP2 || base_format == Format::VOP1 ||
1457ec681f3Smrg                     base_format == Format::VOPC || base_format == Format::VINTRP,
1467ec681f3Smrg                  "Format cannot have VOP3/VOP3B applied", instr.get());
1477ec681f3Smrg         }
1487ec681f3Smrg
1497ec681f3Smrg         /* check SDWA */
1507ec681f3Smrg         if (instr->isSDWA()) {
1517ec681f3Smrg            check(base_format == Format::VOP2 || base_format == Format::VOP1 ||
1527ec681f3Smrg                     base_format == Format::VOPC,
1537ec681f3Smrg                  "Format cannot have SDWA applied", instr.get());
1547ec681f3Smrg
1557ec681f3Smrg            check(program->chip_class >= GFX8, "SDWA is GFX8+ only", instr.get());
1567ec681f3Smrg
1577ec681f3Smrg            SDWA_instruction& sdwa = instr->sdwa();
1587ec681f3Smrg            check(sdwa.omod == 0 || program->chip_class >= GFX9,
1597ec681f3Smrg                  "SDWA omod only supported on GFX9+", instr.get());
1607ec681f3Smrg            if (base_format == Format::VOPC) {
1617ec681f3Smrg               check(sdwa.clamp == false || program->chip_class == GFX8,
1627ec681f3Smrg                     "SDWA VOPC clamp only supported on GFX8", instr.get());
1637ec681f3Smrg               check((instr->definitions[0].isFixed() && instr->definitions[0].physReg() == vcc) ||
1647ec681f3Smrg                        program->chip_class >= GFX9,
1657ec681f3Smrg                     "SDWA+VOPC definition must be fixed to vcc on GFX8", instr.get());
1667ec681f3Smrg            } else {
1677ec681f3Smrg               const Definition& def = instr->definitions[0];
1687ec681f3Smrg               check(def.bytes() <= 4, "SDWA definitions must not be larger than 4 bytes",
1697ec681f3Smrg                     instr.get());
1707ec681f3Smrg               check(def.bytes() >= sdwa.dst_sel.size() + sdwa.dst_sel.offset(),
1717ec681f3Smrg                     "SDWA definition selection size must be at most definition size", instr.get());
1727ec681f3Smrg               check(
1737ec681f3Smrg                  sdwa.dst_sel.size() == 1 || sdwa.dst_sel.size() == 2 || sdwa.dst_sel.size() == 4,
1747ec681f3Smrg                  "SDWA definition selection size must be 1, 2 or 4 bytes", instr.get());
1757ec681f3Smrg               check(sdwa.dst_sel.offset() % sdwa.dst_sel.size() == 0, "Invalid selection offset",
1767ec681f3Smrg                     instr.get());
1777ec681f3Smrg               check(def.bytes() == 4 || def.bytes() == sdwa.dst_sel.size(),
1787ec681f3Smrg                     "SDWA dst_sel size must be definition size for subdword definitions",
1797ec681f3Smrg                     instr.get());
1807ec681f3Smrg               check(def.bytes() == 4 || sdwa.dst_sel.offset() == 0,
1817ec681f3Smrg                     "SDWA dst_sel offset must be 0 for subdword definitions", instr.get());
1827ec681f3Smrg            }
1837ec681f3Smrg
1847ec681f3Smrg            for (unsigned i = 0; i < std::min<unsigned>(2, instr->operands.size()); i++) {
1857ec681f3Smrg               const Operand& op = instr->operands[i];
1867ec681f3Smrg               check(op.bytes() <= 4, "SDWA operands must not be larger than 4 bytes", instr.get());
1877ec681f3Smrg               check(op.bytes() >= sdwa.sel[i].size() + sdwa.sel[i].offset(),
1887ec681f3Smrg                     "SDWA operand selection size must be at most operand size", instr.get());
1897ec681f3Smrg               check(sdwa.sel[i].size() == 1 || sdwa.sel[i].size() == 2 || sdwa.sel[i].size() == 4,
1907ec681f3Smrg                     "SDWA operand selection size must be 1, 2 or 4 bytes", instr.get());
1917ec681f3Smrg               check(sdwa.sel[i].offset() % sdwa.sel[i].size() == 0, "Invalid selection offset",
1927ec681f3Smrg                     instr.get());
1937ec681f3Smrg            }
1947ec681f3Smrg            if (instr->operands.size() >= 3) {
1957ec681f3Smrg               check(instr->operands[2].isFixed() && instr->operands[2].physReg() == vcc,
1967ec681f3Smrg                     "3rd operand must be fixed to vcc with SDWA", instr.get());
1977ec681f3Smrg            }
1987ec681f3Smrg            if (instr->definitions.size() >= 2) {
1997ec681f3Smrg               check(instr->definitions[1].isFixed() && instr->definitions[1].physReg() == vcc,
2007ec681f3Smrg                     "2nd definition must be fixed to vcc with SDWA", instr.get());
2017ec681f3Smrg            }
2027ec681f3Smrg
2037ec681f3Smrg            const bool sdwa_opcodes =
2047ec681f3Smrg               instr->opcode != aco_opcode::v_fmac_f32 && instr->opcode != aco_opcode::v_fmac_f16 &&
2057ec681f3Smrg               instr->opcode != aco_opcode::v_fmamk_f32 &&
2067ec681f3Smrg               instr->opcode != aco_opcode::v_fmaak_f32 &&
2077ec681f3Smrg               instr->opcode != aco_opcode::v_fmamk_f16 &&
2087ec681f3Smrg               instr->opcode != aco_opcode::v_fmaak_f16 &&
2097ec681f3Smrg               instr->opcode != aco_opcode::v_madmk_f32 &&
2107ec681f3Smrg               instr->opcode != aco_opcode::v_madak_f32 &&
2117ec681f3Smrg               instr->opcode != aco_opcode::v_madmk_f16 &&
2127ec681f3Smrg               instr->opcode != aco_opcode::v_madak_f16 &&
2137ec681f3Smrg               instr->opcode != aco_opcode::v_readfirstlane_b32 &&
2147ec681f3Smrg               instr->opcode != aco_opcode::v_clrexcp && instr->opcode != aco_opcode::v_swap_b32;
2157ec681f3Smrg
2167ec681f3Smrg            const bool feature_mac =
2177ec681f3Smrg               program->chip_class == GFX8 &&
2187ec681f3Smrg               (instr->opcode == aco_opcode::v_mac_f32 && instr->opcode == aco_opcode::v_mac_f16);
2197ec681f3Smrg
2207ec681f3Smrg            check(sdwa_opcodes || feature_mac, "SDWA can't be used with this opcode", instr.get());
2217ec681f3Smrg         }
2227ec681f3Smrg
2237ec681f3Smrg         /* check opsel */
2247ec681f3Smrg         if (instr->isVOP3()) {
2257ec681f3Smrg            VOP3_instruction& vop3 = instr->vop3();
2267ec681f3Smrg            check(vop3.opsel == 0 || program->chip_class >= GFX9,
2277ec681f3Smrg                  "Opsel is only supported on GFX9+", instr.get());
2287ec681f3Smrg
2297ec681f3Smrg            for (unsigned i = 0; i < 3; i++) {
2307ec681f3Smrg               if (i >= instr->operands.size() ||
2317ec681f3Smrg                   (instr->operands[i].hasRegClass() &&
2327ec681f3Smrg                    instr->operands[i].regClass().is_subdword() && !instr->operands[i].isFixed()))
2337ec681f3Smrg                  check((vop3.opsel & (1 << i)) == 0, "Unexpected opsel for operand", instr.get());
2347ec681f3Smrg            }
2357ec681f3Smrg            if (instr->definitions[0].regClass().is_subdword() && !instr->definitions[0].isFixed())
2367ec681f3Smrg               check((vop3.opsel & (1 << 3)) == 0, "Unexpected opsel for sub-dword definition",
2377ec681f3Smrg                     instr.get());
2387ec681f3Smrg         } else if (instr->isVOP3P()) {
2397ec681f3Smrg            VOP3P_instruction& vop3p = instr->vop3p();
2407ec681f3Smrg            for (unsigned i = 0; i < instr->operands.size(); i++) {
2417ec681f3Smrg               if (instr->operands[i].hasRegClass() &&
2427ec681f3Smrg                   instr->operands[i].regClass().is_subdword() && !instr->operands[i].isFixed())
2437ec681f3Smrg                  check((vop3p.opsel_lo & (1 << i)) == 0 && (vop3p.opsel_hi & (1 << i)) == 0,
2447ec681f3Smrg                        "Unexpected opsel for subdword operand", instr.get());
2457ec681f3Smrg            }
2467ec681f3Smrg            check(instr->definitions[0].regClass() == v1, "VOP3P must have v1 definition",
2477ec681f3Smrg                  instr.get());
2487ec681f3Smrg         }
2497ec681f3Smrg
2507ec681f3Smrg         /* check for undefs */
2517ec681f3Smrg         for (unsigned i = 0; i < instr->operands.size(); i++) {
2527ec681f3Smrg            if (instr->operands[i].isUndefined()) {
2537ec681f3Smrg               bool flat = instr->isFlatLike();
2547ec681f3Smrg               bool can_be_undef = is_phi(instr) || instr->isEXP() || instr->isReduction() ||
2557ec681f3Smrg                                   instr->opcode == aco_opcode::p_create_vector ||
2567ec681f3Smrg                                   (flat && i == 1) || (instr->isMIMG() && (i == 1 || i == 2)) ||
2577ec681f3Smrg                                   ((instr->isMUBUF() || instr->isMTBUF()) && i == 1);
2587ec681f3Smrg               check(can_be_undef, "Undefs can only be used in certain operands", instr.get());
2597ec681f3Smrg            } else {
2607ec681f3Smrg               check(instr->operands[i].isFixed() || instr->operands[i].isTemp() ||
2617ec681f3Smrg                        instr->operands[i].isConstant(),
2627ec681f3Smrg                     "Uninitialized Operand", instr.get());
2637ec681f3Smrg            }
2647ec681f3Smrg         }
2657ec681f3Smrg
2667ec681f3Smrg         /* check subdword definitions */
2677ec681f3Smrg         for (unsigned i = 0; i < instr->definitions.size(); i++) {
2687ec681f3Smrg            if (instr->definitions[i].regClass().is_subdword())
2697ec681f3Smrg               check(instr->isPseudo() || instr->definitions[i].bytes() <= 4,
2707ec681f3Smrg                     "Only Pseudo instructions can write subdword registers larger than 4 bytes",
2717ec681f3Smrg                     instr.get());
2727ec681f3Smrg         }
2737ec681f3Smrg
2747ec681f3Smrg         if (instr->isSALU() || instr->isVALU()) {
2757ec681f3Smrg            /* check literals */
2767ec681f3Smrg            Operand literal(s1);
2777ec681f3Smrg            for (unsigned i = 0; i < instr->operands.size(); i++) {
2787ec681f3Smrg               Operand op = instr->operands[i];
2797ec681f3Smrg               if (!op.isLiteral())
2807ec681f3Smrg                  continue;
2817ec681f3Smrg
2827ec681f3Smrg               check(!instr->isDPP() && !instr->isSDWA() &&
2837ec681f3Smrg                        (!instr->isVOP3() || program->chip_class >= GFX10) &&
2847ec681f3Smrg                        (!instr->isVOP3P() || program->chip_class >= GFX10),
2857ec681f3Smrg                     "Literal applied on wrong instruction format", instr.get());
2867ec681f3Smrg
2877ec681f3Smrg               check(literal.isUndefined() || (literal.size() == op.size() &&
2887ec681f3Smrg                                               literal.constantValue() == op.constantValue()),
2897ec681f3Smrg                     "Only 1 Literal allowed", instr.get());
2907ec681f3Smrg               literal = op;
2917ec681f3Smrg               check(instr->isSALU() || instr->isVOP3() || instr->isVOP3P() || i == 0 || i == 2,
2927ec681f3Smrg                     "Wrong source position for Literal argument", instr.get());
2937ec681f3Smrg            }
2947ec681f3Smrg
2957ec681f3Smrg            /* check num sgprs for VALU */
2967ec681f3Smrg            if (instr->isVALU()) {
2977ec681f3Smrg               bool is_shift64 = instr->opcode == aco_opcode::v_lshlrev_b64 ||
2987ec681f3Smrg                                 instr->opcode == aco_opcode::v_lshrrev_b64 ||
2997ec681f3Smrg                                 instr->opcode == aco_opcode::v_ashrrev_i64;
3007ec681f3Smrg               unsigned const_bus_limit = 1;
3017ec681f3Smrg               if (program->chip_class >= GFX10 && !is_shift64)
3027ec681f3Smrg                  const_bus_limit = 2;
3037ec681f3Smrg
3047ec681f3Smrg               uint32_t scalar_mask = instr->isVOP3() || instr->isVOP3P() ? 0x7 : 0x5;
3057ec681f3Smrg               if (instr->isSDWA())
3067ec681f3Smrg                  scalar_mask = program->chip_class >= GFX9 ? 0x7 : 0x4;
3077ec681f3Smrg               else if (instr->isDPP())
3087ec681f3Smrg                  scalar_mask = 0x4;
3097ec681f3Smrg
3107ec681f3Smrg               if (instr->isVOPC() || instr->opcode == aco_opcode::v_readfirstlane_b32 ||
3117ec681f3Smrg                   instr->opcode == aco_opcode::v_readlane_b32 ||
3127ec681f3Smrg                   instr->opcode == aco_opcode::v_readlane_b32_e64) {
3137ec681f3Smrg                  check(instr->definitions[0].getTemp().type() == RegType::sgpr,
3147ec681f3Smrg                        "Wrong Definition type for VALU instruction", instr.get());
3157ec681f3Smrg               } else {
3167ec681f3Smrg                  check(instr->definitions[0].getTemp().type() == RegType::vgpr,
3177ec681f3Smrg                        "Wrong Definition type for VALU instruction", instr.get());
3187ec681f3Smrg               }
3197ec681f3Smrg
3207ec681f3Smrg               unsigned num_sgprs = 0;
3217ec681f3Smrg               unsigned sgpr[] = {0, 0};
3227ec681f3Smrg               for (unsigned i = 0; i < instr->operands.size(); i++) {
3237ec681f3Smrg                  Operand op = instr->operands[i];
3247ec681f3Smrg                  if (instr->opcode == aco_opcode::v_readfirstlane_b32 ||
3257ec681f3Smrg                      instr->opcode == aco_opcode::v_readlane_b32 ||
3267ec681f3Smrg                      instr->opcode == aco_opcode::v_readlane_b32_e64) {
3277ec681f3Smrg                     check(i != 1 || (op.isTemp() && op.regClass().type() == RegType::sgpr) ||
3287ec681f3Smrg                              op.isConstant(),
3297ec681f3Smrg                           "Must be a SGPR or a constant", instr.get());
3307ec681f3Smrg                     check(i == 1 || (op.isTemp() && op.regClass().type() == RegType::vgpr &&
3317ec681f3Smrg                                      op.bytes() <= 4),
3327ec681f3Smrg                           "Wrong Operand type for VALU instruction", instr.get());
3337ec681f3Smrg                     continue;
3347ec681f3Smrg                  }
3357ec681f3Smrg                  if (instr->opcode == aco_opcode::v_permlane16_b32 ||
3367ec681f3Smrg                      instr->opcode == aco_opcode::v_permlanex16_b32) {
3377ec681f3Smrg                     check(i != 0 || (op.isTemp() && op.regClass().type() == RegType::vgpr),
3387ec681f3Smrg                           "Operand 0 of v_permlane must be VGPR", instr.get());
3397ec681f3Smrg                     check(i == 0 || (op.isTemp() && op.regClass().type() == RegType::sgpr) ||
3407ec681f3Smrg                              op.isConstant(),
3417ec681f3Smrg                           "Lane select operands of v_permlane must be SGPR or constant",
3427ec681f3Smrg                           instr.get());
3437ec681f3Smrg                  }
3447ec681f3Smrg
3457ec681f3Smrg                  if (instr->opcode == aco_opcode::v_writelane_b32 ||
3467ec681f3Smrg                      instr->opcode == aco_opcode::v_writelane_b32_e64) {
3477ec681f3Smrg                     check(i != 2 || (op.isTemp() && op.regClass().type() == RegType::vgpr &&
3487ec681f3Smrg                                      op.bytes() <= 4),
3497ec681f3Smrg                           "Wrong Operand type for VALU instruction", instr.get());
3507ec681f3Smrg                     check(i == 2 || (op.isTemp() && op.regClass().type() == RegType::sgpr) ||
3517ec681f3Smrg                              op.isConstant(),
3527ec681f3Smrg                           "Must be a SGPR or a constant", instr.get());
3537ec681f3Smrg                     continue;
3547ec681f3Smrg                  }
3557ec681f3Smrg                  if (op.isTemp() && instr->operands[i].regClass().type() == RegType::sgpr) {
3567ec681f3Smrg                     check(scalar_mask & (1 << i), "Wrong source position for SGPR argument",
3577ec681f3Smrg                           instr.get());
3587ec681f3Smrg
3597ec681f3Smrg                     if (op.tempId() != sgpr[0] && op.tempId() != sgpr[1]) {
3607ec681f3Smrg                        if (num_sgprs < 2)
3617ec681f3Smrg                           sgpr[num_sgprs++] = op.tempId();
3627ec681f3Smrg                     }
3637ec681f3Smrg                  }
3647ec681f3Smrg
3657ec681f3Smrg                  if (op.isConstant() && !op.isLiteral())
3667ec681f3Smrg                     check(scalar_mask & (1 << i), "Wrong source position for constant argument",
3677ec681f3Smrg                           instr.get());
3687ec681f3Smrg               }
3697ec681f3Smrg               check(num_sgprs + (literal.isUndefined() ? 0 : 1) <= const_bus_limit,
3707ec681f3Smrg                     "Too many SGPRs/literals", instr.get());
3717ec681f3Smrg            }
3727ec681f3Smrg
3737ec681f3Smrg            if (instr->isSOP1() || instr->isSOP2()) {
3747ec681f3Smrg               check(instr->definitions[0].getTemp().type() == RegType::sgpr,
3757ec681f3Smrg                     "Wrong Definition type for SALU instruction", instr.get());
3767ec681f3Smrg               for (const Operand& op : instr->operands) {
3777ec681f3Smrg                  check(op.isConstant() || op.regClass().type() <= RegType::sgpr,
3787ec681f3Smrg                        "Wrong Operand type for SALU instruction", instr.get());
3797ec681f3Smrg               }
3807ec681f3Smrg            }
3817ec681f3Smrg         }
3827ec681f3Smrg
3837ec681f3Smrg         switch (instr->format) {
3847ec681f3Smrg         case Format::PSEUDO: {
3857ec681f3Smrg            if (instr->opcode == aco_opcode::p_create_vector) {
3867ec681f3Smrg               unsigned size = 0;
3877ec681f3Smrg               for (const Operand& op : instr->operands) {
3887ec681f3Smrg                  check(op.bytes() < 4 || size % 4 == 0, "Operand is not aligned", instr.get());
3897ec681f3Smrg                  size += op.bytes();
3907ec681f3Smrg               }
3917ec681f3Smrg               check(size == instr->definitions[0].bytes(),
3927ec681f3Smrg                     "Definition size does not match operand sizes", instr.get());
3937ec681f3Smrg               if (instr->definitions[0].getTemp().type() == RegType::sgpr) {
3947ec681f3Smrg                  for (const Operand& op : instr->operands) {
3957ec681f3Smrg                     check(op.isConstant() || op.regClass().type() == RegType::sgpr,
3967ec681f3Smrg                           "Wrong Operand type for scalar vector", instr.get());
3977ec681f3Smrg                  }
3987ec681f3Smrg               }
3997ec681f3Smrg            } else if (instr->opcode == aco_opcode::p_extract_vector) {
4007ec681f3Smrg               check((instr->operands[0].isTemp()) && instr->operands[1].isConstant(),
4017ec681f3Smrg                     "Wrong Operand types", instr.get());
4027ec681f3Smrg               check((instr->operands[1].constantValue() + 1) * instr->definitions[0].bytes() <=
4037ec681f3Smrg                        instr->operands[0].bytes(),
4047ec681f3Smrg                     "Index out of range", instr.get());
4057ec681f3Smrg               check(instr->definitions[0].getTemp().type() == RegType::vgpr ||
4067ec681f3Smrg                        instr->operands[0].regClass().type() == RegType::sgpr,
4077ec681f3Smrg                     "Cannot extract SGPR value from VGPR vector", instr.get());
4087ec681f3Smrg               check(program->chip_class >= GFX9 ||
4097ec681f3Smrg                        !instr->definitions[0].regClass().is_subdword() ||
4107ec681f3Smrg                        instr->operands[0].regClass().type() == RegType::vgpr,
4117ec681f3Smrg                     "Cannot extract subdword from SGPR before GFX9+", instr.get());
4127ec681f3Smrg            } else if (instr->opcode == aco_opcode::p_split_vector) {
4137ec681f3Smrg               check(instr->operands[0].isTemp(), "Operand must be a temporary", instr.get());
4147ec681f3Smrg               unsigned size = 0;
4157ec681f3Smrg               for (const Definition& def : instr->definitions) {
4167ec681f3Smrg                  size += def.bytes();
4177ec681f3Smrg               }
4187ec681f3Smrg               check(size == instr->operands[0].bytes(),
4197ec681f3Smrg                     "Operand size does not match definition sizes", instr.get());
4207ec681f3Smrg               if (instr->operands[0].getTemp().type() == RegType::vgpr) {
4217ec681f3Smrg                  for (const Definition& def : instr->definitions)
4227ec681f3Smrg                     check(def.regClass().type() == RegType::vgpr,
4237ec681f3Smrg                           "Wrong Definition type for VGPR split_vector", instr.get());
4247ec681f3Smrg               } else {
4257ec681f3Smrg                  for (const Definition& def : instr->definitions)
4267ec681f3Smrg                     check(program->chip_class >= GFX9 || !def.regClass().is_subdword(),
4277ec681f3Smrg                           "Cannot split SGPR into subdword VGPRs before GFX9+", instr.get());
4287ec681f3Smrg               }
4297ec681f3Smrg            } else if (instr->opcode == aco_opcode::p_parallelcopy) {
4307ec681f3Smrg               check(instr->definitions.size() == instr->operands.size(),
4317ec681f3Smrg                     "Number of Operands does not match number of Definitions", instr.get());
4327ec681f3Smrg               for (unsigned i = 0; i < instr->operands.size(); i++) {
4337ec681f3Smrg                  check(instr->definitions[i].bytes() == instr->operands[i].bytes(),
4347ec681f3Smrg                        "Operand and Definition size must match", instr.get());
4357ec681f3Smrg                  if (instr->operands[i].isTemp()) {
4367ec681f3Smrg                     check((instr->definitions[i].getTemp().type() ==
4377ec681f3Smrg                            instr->operands[i].regClass().type()) ||
4387ec681f3Smrg                              (instr->definitions[i].getTemp().type() == RegType::vgpr &&
4397ec681f3Smrg                               instr->operands[i].regClass().type() == RegType::sgpr),
4407ec681f3Smrg                           "Operand and Definition types do not match", instr.get());
4417ec681f3Smrg                     check(instr->definitions[i].regClass().is_linear_vgpr() ==
4427ec681f3Smrg                              instr->operands[i].regClass().is_linear_vgpr(),
4437ec681f3Smrg                           "Operand and Definition types do not match", instr.get());
4447ec681f3Smrg                  } else {
4457ec681f3Smrg                     check(!instr->definitions[i].regClass().is_linear_vgpr(),
4467ec681f3Smrg                           "Can only copy linear VGPRs into linear VGPRs, not constant/undef",
4477ec681f3Smrg                           instr.get());
4487ec681f3Smrg                  }
4497ec681f3Smrg               }
4507ec681f3Smrg            } else if (instr->opcode == aco_opcode::p_phi) {
4517ec681f3Smrg               check(instr->operands.size() == block.logical_preds.size(),
4527ec681f3Smrg                     "Number of Operands does not match number of predecessors", instr.get());
4537ec681f3Smrg               check(instr->definitions[0].getTemp().type() == RegType::vgpr,
4547ec681f3Smrg                     "Logical Phi Definition must be vgpr", instr.get());
4557ec681f3Smrg               for (const Operand& op : instr->operands)
4567ec681f3Smrg                  check(instr->definitions[0].size() == op.size(),
4577ec681f3Smrg                        "Operand sizes must match Definition size", instr.get());
4587ec681f3Smrg            } else if (instr->opcode == aco_opcode::p_linear_phi) {
4597ec681f3Smrg               for (const Operand& op : instr->operands) {
4607ec681f3Smrg                  check(!op.isTemp() || op.getTemp().is_linear(), "Wrong Operand type",
4617ec681f3Smrg                        instr.get());
4627ec681f3Smrg                  check(instr->definitions[0].size() == op.size(),
4637ec681f3Smrg                        "Operand sizes must match Definition size", instr.get());
4647ec681f3Smrg               }
4657ec681f3Smrg               check(instr->operands.size() == block.linear_preds.size(),
4667ec681f3Smrg                     "Number of Operands does not match number of predecessors", instr.get());
4677ec681f3Smrg            } else if (instr->opcode == aco_opcode::p_extract ||
4687ec681f3Smrg                       instr->opcode == aco_opcode::p_insert) {
4697ec681f3Smrg               check(instr->operands[0].isTemp(), "Data operand must be temporary", instr.get());
4707ec681f3Smrg               check(instr->operands[1].isConstant(), "Index must be constant", instr.get());
4717ec681f3Smrg               if (instr->opcode == aco_opcode::p_extract)
4727ec681f3Smrg                  check(instr->operands[3].isConstant(), "Sign-extend flag must be constant",
4737ec681f3Smrg                        instr.get());
4747ec681f3Smrg
4757ec681f3Smrg               check(instr->definitions[0].getTemp().type() != RegType::sgpr ||
4767ec681f3Smrg                        instr->operands[0].getTemp().type() == RegType::sgpr,
4777ec681f3Smrg                     "Can't extract/insert VGPR to SGPR", instr.get());
4787ec681f3Smrg
4797ec681f3Smrg               if (instr->opcode == aco_opcode::p_insert)
4807ec681f3Smrg                  check(instr->operands[0].bytes() == instr->definitions[0].bytes(),
4817ec681f3Smrg                        "Sizes of p_insert data operand and definition must match", instr.get());
4827ec681f3Smrg
4837ec681f3Smrg               if (instr->definitions[0].getTemp().type() == RegType::sgpr)
4847ec681f3Smrg                  check(instr->definitions.size() >= 2 && instr->definitions[1].isFixed() &&
4857ec681f3Smrg                           instr->definitions[1].physReg() == scc,
4867ec681f3Smrg                        "SGPR extract/insert needs an SCC definition", instr.get());
4877ec681f3Smrg
4887ec681f3Smrg               unsigned data_bits = instr->operands[0].getTemp().bytes() * 8u;
4897ec681f3Smrg               unsigned op_bits = instr->operands[2].constantValue();
4907ec681f3Smrg
4917ec681f3Smrg               if (instr->opcode == aco_opcode::p_insert) {
4927ec681f3Smrg                  check(op_bits == 8 || op_bits == 16, "Size must be 8 or 16", instr.get());
4937ec681f3Smrg                  check(op_bits < data_bits, "Size must be smaller than source", instr.get());
4947ec681f3Smrg               } else if (instr->opcode == aco_opcode::p_extract) {
4957ec681f3Smrg                  check(op_bits == 8 || op_bits == 16 || op_bits == 32,
4967ec681f3Smrg                        "Size must be 8 or 16 or 32", instr.get());
4977ec681f3Smrg                  check(data_bits >= op_bits, "Can't extract more bits than what the data has.",
4987ec681f3Smrg                        instr.get());
4997ec681f3Smrg               }
5007ec681f3Smrg
5017ec681f3Smrg               unsigned comp = data_bits / MAX2(op_bits, 1);
5027ec681f3Smrg               check(instr->operands[1].constantValue() < comp, "Index must be in-bounds",
5037ec681f3Smrg                     instr.get());
5047ec681f3Smrg            }
5057ec681f3Smrg            break;
5067ec681f3Smrg         }
5077ec681f3Smrg         case Format::PSEUDO_REDUCTION: {
5087ec681f3Smrg            for (const Operand& op : instr->operands)
5097ec681f3Smrg               check(op.regClass().type() == RegType::vgpr,
5107ec681f3Smrg                     "All operands of PSEUDO_REDUCTION instructions must be in VGPRs.",
5117ec681f3Smrg                     instr.get());
5127ec681f3Smrg
5137ec681f3Smrg            if (instr->opcode == aco_opcode::p_reduce &&
5147ec681f3Smrg                instr->reduction().cluster_size == program->wave_size)
5157ec681f3Smrg               check(instr->definitions[0].regClass().type() == RegType::sgpr ||
5167ec681f3Smrg                        program->wave_size == 32,
5177ec681f3Smrg                     "The result of unclustered reductions must go into an SGPR.", instr.get());
5187ec681f3Smrg            else
5197ec681f3Smrg               check(instr->definitions[0].regClass().type() == RegType::vgpr,
5207ec681f3Smrg                     "The result of scans and clustered reductions must go into a VGPR.",
5217ec681f3Smrg                     instr.get());
5227ec681f3Smrg
5237ec681f3Smrg            break;
5247ec681f3Smrg         }
5257ec681f3Smrg         case Format::SMEM: {
5267ec681f3Smrg            if (instr->operands.size() >= 1)
5277ec681f3Smrg               check((instr->operands[0].isFixed() && !instr->operands[0].isConstant()) ||
5287ec681f3Smrg                        (instr->operands[0].isTemp() &&
5297ec681f3Smrg                         instr->operands[0].regClass().type() == RegType::sgpr),
5307ec681f3Smrg                     "SMEM operands must be sgpr", instr.get());
5317ec681f3Smrg            if (instr->operands.size() >= 2)
5327ec681f3Smrg               check(instr->operands[1].isConstant() ||
5337ec681f3Smrg                        (instr->operands[1].isTemp() &&
5347ec681f3Smrg                         instr->operands[1].regClass().type() == RegType::sgpr),
5357ec681f3Smrg                     "SMEM offset must be constant or sgpr", instr.get());
5367ec681f3Smrg            if (!instr->definitions.empty())
5377ec681f3Smrg               check(instr->definitions[0].getTemp().type() == RegType::sgpr,
5387ec681f3Smrg                     "SMEM result must be sgpr", instr.get());
5397ec681f3Smrg            break;
5407ec681f3Smrg         }
5417ec681f3Smrg         case Format::MTBUF:
5427ec681f3Smrg         case Format::MUBUF: {
5437ec681f3Smrg            check(instr->operands.size() > 1, "VMEM instructions must have at least one operand",
5447ec681f3Smrg                  instr.get());
5457ec681f3Smrg            check(instr->operands[1].hasRegClass() &&
5467ec681f3Smrg                     instr->operands[1].regClass().type() == RegType::vgpr,
5477ec681f3Smrg                  "VADDR must be in vgpr for VMEM instructions", instr.get());
5487ec681f3Smrg            check(
5497ec681f3Smrg               instr->operands[0].isTemp() && instr->operands[0].regClass().type() == RegType::sgpr,
5507ec681f3Smrg               "VMEM resource constant must be sgpr", instr.get());
5517ec681f3Smrg            check(instr->operands.size() < 4 ||
5527ec681f3Smrg                     (instr->operands[3].isTemp() &&
5537ec681f3Smrg                      instr->operands[3].regClass().type() == RegType::vgpr),
5547ec681f3Smrg                  "VMEM write data must be vgpr", instr.get());
5557ec681f3Smrg            break;
5567ec681f3Smrg         }
5577ec681f3Smrg         case Format::MIMG: {
5587ec681f3Smrg            check(instr->operands.size() >= 4, "MIMG instructions must have at least 4 operands",
5597ec681f3Smrg                  instr.get());
5607ec681f3Smrg            check(instr->operands[0].hasRegClass() &&
5617ec681f3Smrg                     (instr->operands[0].regClass() == s4 || instr->operands[0].regClass() == s8),
5627ec681f3Smrg                  "MIMG operands[0] (resource constant) must be in 4 or 8 SGPRs", instr.get());
5637ec681f3Smrg            if (instr->operands[1].hasRegClass())
5647ec681f3Smrg               check(instr->operands[1].regClass() == s4,
5657ec681f3Smrg                     "MIMG operands[1] (sampler constant) must be 4 SGPRs", instr.get());
5667ec681f3Smrg            if (!instr->operands[2].isUndefined()) {
5677ec681f3Smrg               bool is_cmpswap = instr->opcode == aco_opcode::image_atomic_cmpswap ||
5687ec681f3Smrg                                 instr->opcode == aco_opcode::image_atomic_fcmpswap;
5697ec681f3Smrg               check(instr->definitions.empty() ||
5707ec681f3Smrg                        (instr->definitions[0].regClass() == instr->operands[2].regClass() ||
5717ec681f3Smrg                         is_cmpswap),
5727ec681f3Smrg                     "MIMG operands[2] (VDATA) must be the same as definitions[0] for atomics and "
5737ec681f3Smrg                     "TFE/LWE loads",
5747ec681f3Smrg                     instr.get());
5757ec681f3Smrg            }
5767ec681f3Smrg            check(instr->operands.size() == 4 || program->chip_class >= GFX10,
5777ec681f3Smrg                  "NSA is only supported on GFX10+", instr.get());
5787ec681f3Smrg            for (unsigned i = 3; i < instr->operands.size(); i++) {
5797ec681f3Smrg               if (instr->operands.size() == 4) {
5807ec681f3Smrg                  check(instr->operands[i].hasRegClass() &&
5817ec681f3Smrg                           instr->operands[i].regClass().type() == RegType::vgpr,
5827ec681f3Smrg                        "MIMG operands[3] (VADDR) must be VGPR", instr.get());
5837ec681f3Smrg               } else {
5847ec681f3Smrg                  check(instr->operands[i].regClass() == v1, "MIMG VADDR must be v1 if NSA is used",
5857ec681f3Smrg                        instr.get());
5867ec681f3Smrg               }
5877ec681f3Smrg            }
5887ec681f3Smrg            check(instr->definitions.empty() ||
5897ec681f3Smrg                     (instr->definitions[0].isTemp() &&
5907ec681f3Smrg                      instr->definitions[0].regClass().type() == RegType::vgpr),
5917ec681f3Smrg                  "MIMG definitions[0] (VDATA) must be VGPR", instr.get());
5927ec681f3Smrg            break;
5937ec681f3Smrg         }
5947ec681f3Smrg         case Format::DS: {
5957ec681f3Smrg            for (const Operand& op : instr->operands) {
5967ec681f3Smrg               check((op.isTemp() && op.regClass().type() == RegType::vgpr) || op.physReg() == m0,
5977ec681f3Smrg                     "Only VGPRs are valid DS instruction operands", instr.get());
5987ec681f3Smrg            }
5997ec681f3Smrg            if (!instr->definitions.empty())
6007ec681f3Smrg               check(instr->definitions[0].getTemp().type() == RegType::vgpr,
6017ec681f3Smrg                     "DS instruction must return VGPR", instr.get());
6027ec681f3Smrg            break;
6037ec681f3Smrg         }
6047ec681f3Smrg         case Format::EXP: {
6057ec681f3Smrg            for (unsigned i = 0; i < 4; i++)
6067ec681f3Smrg               check(instr->operands[i].hasRegClass() &&
6077ec681f3Smrg                        instr->operands[i].regClass().type() == RegType::vgpr,
6087ec681f3Smrg                     "Only VGPRs are valid Export arguments", instr.get());
6097ec681f3Smrg            break;
6107ec681f3Smrg         }
6117ec681f3Smrg         case Format::FLAT:
6127ec681f3Smrg            check(instr->operands[1].isUndefined(), "Flat instructions don't support SADDR",
6137ec681f3Smrg                  instr.get());
6147ec681f3Smrg            FALLTHROUGH;
6157ec681f3Smrg         case Format::GLOBAL:
6167ec681f3Smrg         case Format::SCRATCH: {
6177ec681f3Smrg            check(
6187ec681f3Smrg               instr->operands[0].isTemp() && instr->operands[0].regClass().type() == RegType::vgpr,
6197ec681f3Smrg               "FLAT/GLOBAL/SCRATCH address must be vgpr", instr.get());
6207ec681f3Smrg            check(instr->operands[1].hasRegClass() &&
6217ec681f3Smrg                     instr->operands[1].regClass().type() == RegType::sgpr,
6227ec681f3Smrg                  "FLAT/GLOBAL/SCRATCH sgpr address must be undefined or sgpr", instr.get());
6237ec681f3Smrg            if (!instr->definitions.empty())
6247ec681f3Smrg               check(instr->definitions[0].getTemp().type() == RegType::vgpr,
6257ec681f3Smrg                     "FLAT/GLOBAL/SCRATCH result must be vgpr", instr.get());
6267ec681f3Smrg            else
6277ec681f3Smrg               check(instr->operands[2].regClass().type() == RegType::vgpr,
6287ec681f3Smrg                     "FLAT/GLOBAL/SCRATCH data must be vgpr", instr.get());
6297ec681f3Smrg            break;
6307ec681f3Smrg         }
6317ec681f3Smrg         default: break;
6327ec681f3Smrg         }
6337ec681f3Smrg      }
6347ec681f3Smrg   }
6357ec681f3Smrg
6367ec681f3Smrg   /* validate CFG */
6377ec681f3Smrg   for (unsigned i = 0; i < program->blocks.size(); i++) {
6387ec681f3Smrg      Block& block = program->blocks[i];
6397ec681f3Smrg      check_block(block.index == i, "block.index must match actual index", &block);
6407ec681f3Smrg
6417ec681f3Smrg      /* predecessors/successors should be sorted */
6427ec681f3Smrg      for (unsigned j = 0; j + 1 < block.linear_preds.size(); j++)
6437ec681f3Smrg         check_block(block.linear_preds[j] < block.linear_preds[j + 1],
6447ec681f3Smrg                     "linear predecessors must be sorted", &block);
6457ec681f3Smrg      for (unsigned j = 0; j + 1 < block.logical_preds.size(); j++)
6467ec681f3Smrg         check_block(block.logical_preds[j] < block.logical_preds[j + 1],
6477ec681f3Smrg                     "logical predecessors must be sorted", &block);
6487ec681f3Smrg      for (unsigned j = 0; j + 1 < block.linear_succs.size(); j++)
6497ec681f3Smrg         check_block(block.linear_succs[j] < block.linear_succs[j + 1],
6507ec681f3Smrg                     "linear successors must be sorted", &block);
6517ec681f3Smrg      for (unsigned j = 0; j + 1 < block.logical_succs.size(); j++)
6527ec681f3Smrg         check_block(block.logical_succs[j] < block.logical_succs[j + 1],
6537ec681f3Smrg                     "logical successors must be sorted", &block);
6547ec681f3Smrg
6557ec681f3Smrg      /* critical edges are not allowed */
6567ec681f3Smrg      if (block.linear_preds.size() > 1) {
6577ec681f3Smrg         for (unsigned pred : block.linear_preds)
6587ec681f3Smrg            check_block(program->blocks[pred].linear_succs.size() == 1,
6597ec681f3Smrg                        "linear critical edges are not allowed", &program->blocks[pred]);
6607ec681f3Smrg         for (unsigned pred : block.logical_preds)
6617ec681f3Smrg            check_block(program->blocks[pred].logical_succs.size() == 1,
6627ec681f3Smrg                        "logical critical edges are not allowed", &program->blocks[pred]);
6637ec681f3Smrg      }
6647ec681f3Smrg   }
6657ec681f3Smrg
6667ec681f3Smrg   return is_valid;
6677ec681f3Smrg}
6687ec681f3Smrg
6697ec681f3Smrg/* RA validation */
6707ec681f3Smrgnamespace {
6717ec681f3Smrg
6727ec681f3Smrgstruct Location {
6737ec681f3Smrg   Location() : block(NULL), instr(NULL) {}
6747ec681f3Smrg
6757ec681f3Smrg   Block* block;
6767ec681f3Smrg   Instruction* instr; // NULL if it's the block's live-in
6777ec681f3Smrg};
6787ec681f3Smrg
6797ec681f3Smrgstruct Assignment {
6807ec681f3Smrg   Location defloc;
6817ec681f3Smrg   Location firstloc;
6827ec681f3Smrg   PhysReg reg;
6837ec681f3Smrg};
6847ec681f3Smrg
6857ec681f3Smrgbool
6867ec681f3Smrgra_fail(Program* program, Location loc, Location loc2, const char* fmt, ...)
6877ec681f3Smrg{
6887ec681f3Smrg   va_list args;
6897ec681f3Smrg   va_start(args, fmt);
6907ec681f3Smrg   char msg[1024];
6917ec681f3Smrg   vsprintf(msg, fmt, args);
6927ec681f3Smrg   va_end(args);
6937ec681f3Smrg
6947ec681f3Smrg   char* out;
6957ec681f3Smrg   size_t outsize;
6967ec681f3Smrg   struct u_memstream mem;
6977ec681f3Smrg   u_memstream_open(&mem, &out, &outsize);
6987ec681f3Smrg   FILE* const memf = u_memstream_get(&mem);
6997ec681f3Smrg
7007ec681f3Smrg   fprintf(memf, "RA error found at instruction in BB%d:\n", loc.block->index);
7017ec681f3Smrg   if (loc.instr) {
7027ec681f3Smrg      aco_print_instr(loc.instr, memf);
7037ec681f3Smrg      fprintf(memf, "\n%s", msg);
7047ec681f3Smrg   } else {
7057ec681f3Smrg      fprintf(memf, "%s", msg);
7067ec681f3Smrg   }
7077ec681f3Smrg   if (loc2.block) {
7087ec681f3Smrg      fprintf(memf, " in BB%d:\n", loc2.block->index);
7097ec681f3Smrg      aco_print_instr(loc2.instr, memf);
7107ec681f3Smrg   }
7117ec681f3Smrg   fprintf(memf, "\n\n");
7127ec681f3Smrg   u_memstream_close(&mem);
7137ec681f3Smrg
7147ec681f3Smrg   aco_err(program, "%s", out);
7157ec681f3Smrg   free(out);
7167ec681f3Smrg
7177ec681f3Smrg   return true;
7187ec681f3Smrg}
7197ec681f3Smrg
7207ec681f3Smrgbool
7217ec681f3Smrgvalidate_subdword_operand(chip_class chip, const aco_ptr<Instruction>& instr, unsigned index)
7227ec681f3Smrg{
7237ec681f3Smrg   Operand op = instr->operands[index];
7247ec681f3Smrg   unsigned byte = op.physReg().byte();
7257ec681f3Smrg
7267ec681f3Smrg   if (instr->opcode == aco_opcode::p_as_uniform)
7277ec681f3Smrg      return byte == 0;
7287ec681f3Smrg   if (instr->isPseudo() && chip >= GFX8)
7297ec681f3Smrg      return true;
7307ec681f3Smrg   if (instr->isSDWA())
7317ec681f3Smrg      return byte + instr->sdwa().sel[index].offset() + instr->sdwa().sel[index].size() <= 4 &&
7327ec681f3Smrg             byte % instr->sdwa().sel[index].size() == 0;
7337ec681f3Smrg   if (instr->isVOP3P())
7347ec681f3Smrg      return ((instr->vop3p().opsel_lo >> index) & 1) == (byte >> 1) &&
7357ec681f3Smrg             ((instr->vop3p().opsel_hi >> index) & 1) == (byte >> 1);
7367ec681f3Smrg   if (byte == 2 && can_use_opsel(chip, instr->opcode, index, 1))
7377ec681f3Smrg      return true;
7387ec681f3Smrg
7397ec681f3Smrg   switch (instr->opcode) {
7407ec681f3Smrg   case aco_opcode::v_cvt_f32_ubyte1:
7417ec681f3Smrg      if (byte == 1)
7427ec681f3Smrg         return true;
7437ec681f3Smrg      break;
7447ec681f3Smrg   case aco_opcode::v_cvt_f32_ubyte2:
7457ec681f3Smrg      if (byte == 2)
7467ec681f3Smrg         return true;
7477ec681f3Smrg      break;
7487ec681f3Smrg   case aco_opcode::v_cvt_f32_ubyte3:
7497ec681f3Smrg      if (byte == 3)
7507ec681f3Smrg         return true;
7517ec681f3Smrg      break;
7527ec681f3Smrg   case aco_opcode::ds_write_b8_d16_hi:
7537ec681f3Smrg   case aco_opcode::ds_write_b16_d16_hi:
7547ec681f3Smrg      if (byte == 2 && index == 1)
7557ec681f3Smrg         return true;
7567ec681f3Smrg      break;
7577ec681f3Smrg   case aco_opcode::buffer_store_byte_d16_hi:
7587ec681f3Smrg   case aco_opcode::buffer_store_short_d16_hi:
7597ec681f3Smrg      if (byte == 2 && index == 3)
7607ec681f3Smrg         return true;
7617ec681f3Smrg      break;
7627ec681f3Smrg   case aco_opcode::flat_store_byte_d16_hi:
7637ec681f3Smrg   case aco_opcode::flat_store_short_d16_hi:
7647ec681f3Smrg   case aco_opcode::scratch_store_byte_d16_hi:
7657ec681f3Smrg   case aco_opcode::scratch_store_short_d16_hi:
7667ec681f3Smrg   case aco_opcode::global_store_byte_d16_hi:
7677ec681f3Smrg   case aco_opcode::global_store_short_d16_hi:
7687ec681f3Smrg      if (byte == 2 && index == 2)
7697ec681f3Smrg         return true;
7707ec681f3Smrg      break;
7717ec681f3Smrg   default: break;
7727ec681f3Smrg   }
7737ec681f3Smrg
7747ec681f3Smrg   return byte == 0;
7757ec681f3Smrg}
7767ec681f3Smrg
7777ec681f3Smrgbool
7787ec681f3Smrgvalidate_subdword_definition(chip_class chip, const aco_ptr<Instruction>& instr)
7797ec681f3Smrg{
7807ec681f3Smrg   Definition def = instr->definitions[0];
7817ec681f3Smrg   unsigned byte = def.physReg().byte();
7827ec681f3Smrg
7837ec681f3Smrg   if (instr->isPseudo() && chip >= GFX8)
7847ec681f3Smrg      return true;
7857ec681f3Smrg   if (instr->isSDWA())
7867ec681f3Smrg      return byte + instr->sdwa().dst_sel.offset() + instr->sdwa().dst_sel.size() <= 4 &&
7877ec681f3Smrg             byte % instr->sdwa().dst_sel.size() == 0;
7887ec681f3Smrg   if (byte == 2 && can_use_opsel(chip, instr->opcode, -1, 1))
7897ec681f3Smrg      return true;
7907ec681f3Smrg
7917ec681f3Smrg   switch (instr->opcode) {
7927ec681f3Smrg   case aco_opcode::buffer_load_ubyte_d16_hi:
7937ec681f3Smrg   case aco_opcode::buffer_load_short_d16_hi:
7947ec681f3Smrg   case aco_opcode::flat_load_ubyte_d16_hi:
7957ec681f3Smrg   case aco_opcode::flat_load_short_d16_hi:
7967ec681f3Smrg   case aco_opcode::scratch_load_ubyte_d16_hi:
7977ec681f3Smrg   case aco_opcode::scratch_load_short_d16_hi:
7987ec681f3Smrg   case aco_opcode::global_load_ubyte_d16_hi:
7997ec681f3Smrg   case aco_opcode::global_load_short_d16_hi:
8007ec681f3Smrg   case aco_opcode::ds_read_u8_d16_hi:
8017ec681f3Smrg   case aco_opcode::ds_read_u16_d16_hi: return byte == 2;
8027ec681f3Smrg   default: break;
8037ec681f3Smrg   }
8047ec681f3Smrg
8057ec681f3Smrg   return byte == 0;
8067ec681f3Smrg}
8077ec681f3Smrg
8087ec681f3Smrgunsigned
8097ec681f3Smrgget_subdword_bytes_written(Program* program, const aco_ptr<Instruction>& instr, unsigned index)
8107ec681f3Smrg{
8117ec681f3Smrg   chip_class chip = program->chip_class;
8127ec681f3Smrg   Definition def = instr->definitions[index];
8137ec681f3Smrg
8147ec681f3Smrg   if (instr->isPseudo())
8157ec681f3Smrg      return chip >= GFX8 ? def.bytes() : def.size() * 4u;
8167ec681f3Smrg   if (instr->isVALU()) {
8177ec681f3Smrg      assert(def.bytes() <= 2);
8187ec681f3Smrg      if (instr->isSDWA())
8197ec681f3Smrg         return instr->sdwa().dst_sel.size();
8207ec681f3Smrg
8217ec681f3Smrg      if (instr_is_16bit(chip, instr->opcode))
8227ec681f3Smrg         return 2;
8237ec681f3Smrg
8247ec681f3Smrg      return 4;
8257ec681f3Smrg   }
8267ec681f3Smrg
8277ec681f3Smrg   switch (instr->opcode) {
8287ec681f3Smrg   case aco_opcode::buffer_load_ubyte_d16:
8297ec681f3Smrg   case aco_opcode::buffer_load_short_d16:
8307ec681f3Smrg   case aco_opcode::flat_load_ubyte_d16:
8317ec681f3Smrg   case aco_opcode::flat_load_short_d16:
8327ec681f3Smrg   case aco_opcode::scratch_load_ubyte_d16:
8337ec681f3Smrg   case aco_opcode::scratch_load_short_d16:
8347ec681f3Smrg   case aco_opcode::global_load_ubyte_d16:
8357ec681f3Smrg   case aco_opcode::global_load_short_d16:
8367ec681f3Smrg   case aco_opcode::ds_read_u8_d16:
8377ec681f3Smrg   case aco_opcode::ds_read_u16_d16:
8387ec681f3Smrg   case aco_opcode::buffer_load_ubyte_d16_hi:
8397ec681f3Smrg   case aco_opcode::buffer_load_short_d16_hi:
8407ec681f3Smrg   case aco_opcode::flat_load_ubyte_d16_hi:
8417ec681f3Smrg   case aco_opcode::flat_load_short_d16_hi:
8427ec681f3Smrg   case aco_opcode::scratch_load_ubyte_d16_hi:
8437ec681f3Smrg   case aco_opcode::scratch_load_short_d16_hi:
8447ec681f3Smrg   case aco_opcode::global_load_ubyte_d16_hi:
8457ec681f3Smrg   case aco_opcode::global_load_short_d16_hi:
8467ec681f3Smrg   case aco_opcode::ds_read_u8_d16_hi:
8477ec681f3Smrg   case aco_opcode::ds_read_u16_d16_hi: return program->dev.sram_ecc_enabled ? 4 : 2;
8487ec681f3Smrg   default: return def.size() * 4;
8497ec681f3Smrg   }
8507ec681f3Smrg}
8517ec681f3Smrg
8527ec681f3Smrg} /* end namespace */
8537ec681f3Smrg
8547ec681f3Smrgbool
8557ec681f3Smrgvalidate_ra(Program* program)
8567ec681f3Smrg{
8577ec681f3Smrg   if (!(debug_flags & DEBUG_VALIDATE_RA))
8587ec681f3Smrg      return false;
8597ec681f3Smrg
8607ec681f3Smrg   bool err = false;
8617ec681f3Smrg   aco::live live_vars = aco::live_var_analysis(program);
8627ec681f3Smrg   std::vector<std::vector<Temp>> phi_sgpr_ops(program->blocks.size());
8637ec681f3Smrg   uint16_t sgpr_limit = get_addr_sgpr_from_waves(program, program->num_waves);
8647ec681f3Smrg
8657ec681f3Smrg   std::map<unsigned, Assignment> assignments;
8667ec681f3Smrg   for (Block& block : program->blocks) {
8677ec681f3Smrg      Location loc;
8687ec681f3Smrg      loc.block = &block;
8697ec681f3Smrg      for (aco_ptr<Instruction>& instr : block.instructions) {
8707ec681f3Smrg         if (instr->opcode == aco_opcode::p_phi) {
8717ec681f3Smrg            for (unsigned i = 0; i < instr->operands.size(); i++) {
8727ec681f3Smrg               if (instr->operands[i].isTemp() &&
8737ec681f3Smrg                   instr->operands[i].getTemp().type() == RegType::sgpr &&
8747ec681f3Smrg                   instr->operands[i].isFirstKill())
8757ec681f3Smrg                  phi_sgpr_ops[block.logical_preds[i]].emplace_back(instr->operands[i].getTemp());
8767ec681f3Smrg            }
8777ec681f3Smrg         }
8787ec681f3Smrg
8797ec681f3Smrg         loc.instr = instr.get();
8807ec681f3Smrg         for (unsigned i = 0; i < instr->operands.size(); i++) {
8817ec681f3Smrg            Operand& op = instr->operands[i];
8827ec681f3Smrg            if (!op.isTemp())
8837ec681f3Smrg               continue;
8847ec681f3Smrg            if (!op.isFixed())
8857ec681f3Smrg               err |= ra_fail(program, loc, Location(), "Operand %d is not assigned a register", i);
8867ec681f3Smrg            if (assignments.count(op.tempId()) && assignments[op.tempId()].reg != op.physReg())
8877ec681f3Smrg               err |=
8887ec681f3Smrg                  ra_fail(program, loc, assignments.at(op.tempId()).firstloc,
8897ec681f3Smrg                          "Operand %d has an inconsistent register assignment with instruction", i);
8907ec681f3Smrg            if ((op.getTemp().type() == RegType::vgpr &&
8917ec681f3Smrg                 op.physReg().reg_b + op.bytes() > (256 + program->config->num_vgprs) * 4) ||
8927ec681f3Smrg                (op.getTemp().type() == RegType::sgpr &&
8937ec681f3Smrg                 op.physReg() + op.size() > program->config->num_sgprs &&
8947ec681f3Smrg                 op.physReg() < sgpr_limit))
8957ec681f3Smrg               err |= ra_fail(program, loc, assignments.at(op.tempId()).firstloc,
8967ec681f3Smrg                              "Operand %d has an out-of-bounds register assignment", i);
8977ec681f3Smrg            if (op.physReg() == vcc && !program->needs_vcc)
8987ec681f3Smrg               err |= ra_fail(program, loc, Location(),
8997ec681f3Smrg                              "Operand %d fixed to vcc but needs_vcc=false", i);
9007ec681f3Smrg            if (op.regClass().is_subdword() &&
9017ec681f3Smrg                !validate_subdword_operand(program->chip_class, instr, i))
9027ec681f3Smrg               err |= ra_fail(program, loc, Location(), "Operand %d not aligned correctly", i);
9037ec681f3Smrg            if (!assignments[op.tempId()].firstloc.block)
9047ec681f3Smrg               assignments[op.tempId()].firstloc = loc;
9057ec681f3Smrg            if (!assignments[op.tempId()].defloc.block)
9067ec681f3Smrg               assignments[op.tempId()].reg = op.physReg();
9077ec681f3Smrg         }
9087ec681f3Smrg
9097ec681f3Smrg         for (unsigned i = 0; i < instr->definitions.size(); i++) {
9107ec681f3Smrg            Definition& def = instr->definitions[i];
9117ec681f3Smrg            if (!def.isTemp())
9127ec681f3Smrg               continue;
9137ec681f3Smrg            if (!def.isFixed())
9147ec681f3Smrg               err |=
9157ec681f3Smrg                  ra_fail(program, loc, Location(), "Definition %d is not assigned a register", i);
9167ec681f3Smrg            if (assignments[def.tempId()].defloc.block)
9177ec681f3Smrg               err |= ra_fail(program, loc, assignments.at(def.tempId()).defloc,
9187ec681f3Smrg                              "Temporary %%%d also defined by instruction", def.tempId());
9197ec681f3Smrg            if ((def.getTemp().type() == RegType::vgpr &&
9207ec681f3Smrg                 def.physReg().reg_b + def.bytes() > (256 + program->config->num_vgprs) * 4) ||
9217ec681f3Smrg                (def.getTemp().type() == RegType::sgpr &&
9227ec681f3Smrg                 def.physReg() + def.size() > program->config->num_sgprs &&
9237ec681f3Smrg                 def.physReg() < sgpr_limit))
9247ec681f3Smrg               err |= ra_fail(program, loc, assignments.at(def.tempId()).firstloc,
9257ec681f3Smrg                              "Definition %d has an out-of-bounds register assignment", i);
9267ec681f3Smrg            if (def.physReg() == vcc && !program->needs_vcc)
9277ec681f3Smrg               err |= ra_fail(program, loc, Location(),
9287ec681f3Smrg                              "Definition %d fixed to vcc but needs_vcc=false", i);
9297ec681f3Smrg            if (def.regClass().is_subdword() &&
9307ec681f3Smrg                !validate_subdword_definition(program->chip_class, instr))
9317ec681f3Smrg               err |= ra_fail(program, loc, Location(), "Definition %d not aligned correctly", i);
9327ec681f3Smrg            if (!assignments[def.tempId()].firstloc.block)
9337ec681f3Smrg               assignments[def.tempId()].firstloc = loc;
9347ec681f3Smrg            assignments[def.tempId()].defloc = loc;
9357ec681f3Smrg            assignments[def.tempId()].reg = def.physReg();
9367ec681f3Smrg         }
9377ec681f3Smrg      }
9387ec681f3Smrg   }
9397ec681f3Smrg
9407ec681f3Smrg   for (Block& block : program->blocks) {
9417ec681f3Smrg      Location loc;
9427ec681f3Smrg      loc.block = &block;
9437ec681f3Smrg
9447ec681f3Smrg      std::array<unsigned, 2048> regs; /* register file in bytes */
9457ec681f3Smrg      regs.fill(0);
9467ec681f3Smrg
9477ec681f3Smrg      std::set<Temp> live;
9487ec681f3Smrg      for (unsigned id : live_vars.live_out[block.index])
9497ec681f3Smrg         live.insert(Temp(id, program->temp_rc[id]));
9507ec681f3Smrg      /* remove killed p_phi sgpr operands */
9517ec681f3Smrg      for (Temp tmp : phi_sgpr_ops[block.index])
9527ec681f3Smrg         live.erase(tmp);
9537ec681f3Smrg
9547ec681f3Smrg      /* check live out */
9557ec681f3Smrg      for (Temp tmp : live) {
9567ec681f3Smrg         PhysReg reg = assignments.at(tmp.id()).reg;
9577ec681f3Smrg         for (unsigned i = 0; i < tmp.bytes(); i++) {
9587ec681f3Smrg            if (regs[reg.reg_b + i]) {
9597ec681f3Smrg               err |= ra_fail(program, loc, Location(),
9607ec681f3Smrg                              "Assignment of element %d of %%%d already taken by %%%d in live-out",
9617ec681f3Smrg                              i, tmp.id(), regs[reg.reg_b + i]);
9627ec681f3Smrg            }
9637ec681f3Smrg            regs[reg.reg_b + i] = tmp.id();
9647ec681f3Smrg         }
9657ec681f3Smrg      }
9667ec681f3Smrg      regs.fill(0);
9677ec681f3Smrg
9687ec681f3Smrg      for (auto it = block.instructions.rbegin(); it != block.instructions.rend(); ++it) {
9697ec681f3Smrg         aco_ptr<Instruction>& instr = *it;
9707ec681f3Smrg
9717ec681f3Smrg         /* check killed p_phi sgpr operands */
9727ec681f3Smrg         if (instr->opcode == aco_opcode::p_logical_end) {
9737ec681f3Smrg            for (Temp tmp : phi_sgpr_ops[block.index]) {
9747ec681f3Smrg               PhysReg reg = assignments.at(tmp.id()).reg;
9757ec681f3Smrg               for (unsigned i = 0; i < tmp.bytes(); i++) {
9767ec681f3Smrg                  if (regs[reg.reg_b + i])
9777ec681f3Smrg                     err |= ra_fail(
9787ec681f3Smrg                        program, loc, Location(),
9797ec681f3Smrg                        "Assignment of element %d of %%%d already taken by %%%d in live-out", i,
9807ec681f3Smrg                        tmp.id(), regs[reg.reg_b + i]);
9817ec681f3Smrg               }
9827ec681f3Smrg               live.emplace(tmp);
9837ec681f3Smrg            }
9847ec681f3Smrg         }
9857ec681f3Smrg
9867ec681f3Smrg         for (const Definition& def : instr->definitions) {
9877ec681f3Smrg            if (!def.isTemp())
9887ec681f3Smrg               continue;
9897ec681f3Smrg            live.erase(def.getTemp());
9907ec681f3Smrg         }
9917ec681f3Smrg
9927ec681f3Smrg         /* don't count phi operands as live-in, since they are actually
9937ec681f3Smrg          * killed when they are copied at the predecessor */
9947ec681f3Smrg         if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
9957ec681f3Smrg            for (const Operand& op : instr->operands) {
9967ec681f3Smrg               if (!op.isTemp())
9977ec681f3Smrg                  continue;
9987ec681f3Smrg               live.insert(op.getTemp());
9997ec681f3Smrg            }
10007ec681f3Smrg         }
10017ec681f3Smrg      }
10027ec681f3Smrg
10037ec681f3Smrg      for (Temp tmp : live) {
10047ec681f3Smrg         PhysReg reg = assignments.at(tmp.id()).reg;
10057ec681f3Smrg         for (unsigned i = 0; i < tmp.bytes(); i++)
10067ec681f3Smrg            regs[reg.reg_b + i] = tmp.id();
10077ec681f3Smrg      }
10087ec681f3Smrg
10097ec681f3Smrg      for (aco_ptr<Instruction>& instr : block.instructions) {
10107ec681f3Smrg         loc.instr = instr.get();
10117ec681f3Smrg
10127ec681f3Smrg         /* remove killed p_phi operands from regs */
10137ec681f3Smrg         if (instr->opcode == aco_opcode::p_logical_end) {
10147ec681f3Smrg            for (Temp tmp : phi_sgpr_ops[block.index]) {
10157ec681f3Smrg               PhysReg reg = assignments.at(tmp.id()).reg;
10167ec681f3Smrg               for (unsigned i = 0; i < tmp.bytes(); i++)
10177ec681f3Smrg                  regs[reg.reg_b + i] = 0;
10187ec681f3Smrg            }
10197ec681f3Smrg         }
10207ec681f3Smrg
10217ec681f3Smrg         if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
10227ec681f3Smrg            for (const Operand& op : instr->operands) {
10237ec681f3Smrg               if (!op.isTemp())
10247ec681f3Smrg                  continue;
10257ec681f3Smrg               if (op.isFirstKillBeforeDef()) {
10267ec681f3Smrg                  for (unsigned j = 0; j < op.getTemp().bytes(); j++)
10277ec681f3Smrg                     regs[op.physReg().reg_b + j] = 0;
10287ec681f3Smrg               }
10297ec681f3Smrg            }
10307ec681f3Smrg         }
10317ec681f3Smrg
10327ec681f3Smrg         for (unsigned i = 0; i < instr->definitions.size(); i++) {
10337ec681f3Smrg            Definition& def = instr->definitions[i];
10347ec681f3Smrg            if (!def.isTemp())
10357ec681f3Smrg               continue;
10367ec681f3Smrg            Temp tmp = def.getTemp();
10377ec681f3Smrg            PhysReg reg = assignments.at(tmp.id()).reg;
10387ec681f3Smrg            for (unsigned j = 0; j < tmp.bytes(); j++) {
10397ec681f3Smrg               if (regs[reg.reg_b + j])
10407ec681f3Smrg                  err |= ra_fail(
10417ec681f3Smrg                     program, loc, assignments.at(regs[reg.reg_b + j]).defloc,
10427ec681f3Smrg                     "Assignment of element %d of %%%d already taken by %%%d from instruction", i,
10437ec681f3Smrg                     tmp.id(), regs[reg.reg_b + j]);
10447ec681f3Smrg               regs[reg.reg_b + j] = tmp.id();
10457ec681f3Smrg            }
10467ec681f3Smrg            if (def.regClass().is_subdword() && def.bytes() < 4) {
10477ec681f3Smrg               unsigned written = get_subdword_bytes_written(program, instr, i);
10487ec681f3Smrg               /* If written=4, the instruction still might write the upper half. In that case, it's
10497ec681f3Smrg                * the lower half that isn't preserved */
10507ec681f3Smrg               for (unsigned j = reg.byte() & ~(written - 1); j < written; j++) {
10517ec681f3Smrg                  unsigned written_reg = reg.reg() * 4u + j;
10527ec681f3Smrg                  if (regs[written_reg] && regs[written_reg] != def.tempId())
10537ec681f3Smrg                     err |= ra_fail(program, loc, assignments.at(regs[written_reg]).defloc,
10547ec681f3Smrg                                    "Assignment of element %d of %%%d overwrites the full register "
10557ec681f3Smrg                                    "taken by %%%d from instruction",
10567ec681f3Smrg                                    i, tmp.id(), regs[written_reg]);
10577ec681f3Smrg               }
10587ec681f3Smrg            }
10597ec681f3Smrg         }
10607ec681f3Smrg
10617ec681f3Smrg         for (const Definition& def : instr->definitions) {
10627ec681f3Smrg            if (!def.isTemp())
10637ec681f3Smrg               continue;
10647ec681f3Smrg            if (def.isKill()) {
10657ec681f3Smrg               for (unsigned j = 0; j < def.getTemp().bytes(); j++)
10667ec681f3Smrg                  regs[def.physReg().reg_b + j] = 0;
10677ec681f3Smrg            }
10687ec681f3Smrg         }
10697ec681f3Smrg
10707ec681f3Smrg         if (instr->opcode != aco_opcode::p_phi && instr->opcode != aco_opcode::p_linear_phi) {
10717ec681f3Smrg            for (const Operand& op : instr->operands) {
10727ec681f3Smrg               if (!op.isTemp())
10737ec681f3Smrg                  continue;
10747ec681f3Smrg               if (op.isLateKill() && op.isFirstKill()) {
10757ec681f3Smrg                  for (unsigned j = 0; j < op.getTemp().bytes(); j++)
10767ec681f3Smrg                     regs[op.physReg().reg_b + j] = 0;
10777ec681f3Smrg               }
10787ec681f3Smrg            }
10797ec681f3Smrg         }
10807ec681f3Smrg      }
10817ec681f3Smrg   }
10827ec681f3Smrg
10837ec681f3Smrg   return err;
10847ec681f3Smrg}
10857ec681f3Smrg} // namespace aco
1086