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_builder.h"
267ec681f3Smrg#include "aco_ir.h"
277ec681f3Smrg
287ec681f3Smrg#include "common/ac_shader_util.h"
297ec681f3Smrg#include "common/sid.h"
307ec681f3Smrg
317ec681f3Smrg#include <array>
327ec681f3Smrg
337ec681f3Smrgnamespace aco {
347ec681f3Smrg
357ec681f3Smrgconst std::array<const char*, num_reduce_ops> reduce_ops = []()
367ec681f3Smrg{
377ec681f3Smrg   std::array<const char*, num_reduce_ops> ret{};
387ec681f3Smrg   ret[iadd8] = "iadd8";
397ec681f3Smrg   ret[iadd16] = "iadd16";
407ec681f3Smrg   ret[iadd32] = "iadd32";
417ec681f3Smrg   ret[iadd64] = "iadd64";
427ec681f3Smrg   ret[imul8] = "imul8";
437ec681f3Smrg   ret[imul16] = "imul16";
447ec681f3Smrg   ret[imul32] = "imul32";
457ec681f3Smrg   ret[imul64] = "imul64";
467ec681f3Smrg   ret[fadd16] = "fadd16";
477ec681f3Smrg   ret[fadd32] = "fadd32";
487ec681f3Smrg   ret[fadd64] = "fadd64";
497ec681f3Smrg   ret[fmul16] = "fmul16";
507ec681f3Smrg   ret[fmul32] = "fmul32";
517ec681f3Smrg   ret[fmul64] = "fmul64";
527ec681f3Smrg   ret[imin8] = "imin8";
537ec681f3Smrg   ret[imin16] = "imin16";
547ec681f3Smrg   ret[imin32] = "imin32";
557ec681f3Smrg   ret[imin64] = "imin64";
567ec681f3Smrg   ret[imax8] = "imax8";
577ec681f3Smrg   ret[imax16] = "imax16";
587ec681f3Smrg   ret[imax32] = "imax32";
597ec681f3Smrg   ret[imax64] = "imax64";
607ec681f3Smrg   ret[umin8] = "umin8";
617ec681f3Smrg   ret[umin16] = "umin16";
627ec681f3Smrg   ret[umin32] = "umin32";
637ec681f3Smrg   ret[umin64] = "umin64";
647ec681f3Smrg   ret[umax8] = "umax8";
657ec681f3Smrg   ret[umax16] = "umax16";
667ec681f3Smrg   ret[umax32] = "umax32";
677ec681f3Smrg   ret[umax64] = "umax64";
687ec681f3Smrg   ret[fmin16] = "fmin16";
697ec681f3Smrg   ret[fmin32] = "fmin32";
707ec681f3Smrg   ret[fmin64] = "fmin64";
717ec681f3Smrg   ret[fmax16] = "fmax16";
727ec681f3Smrg   ret[fmax32] = "fmax32";
737ec681f3Smrg   ret[fmax64] = "fmax64";
747ec681f3Smrg   ret[iand8] = "iand8";
757ec681f3Smrg   ret[iand16] = "iand16";
767ec681f3Smrg   ret[iand32] = "iand32";
777ec681f3Smrg   ret[iand64] = "iand64";
787ec681f3Smrg   ret[ior8] = "ior8";
797ec681f3Smrg   ret[ior16] = "ior16";
807ec681f3Smrg   ret[ior32] = "ior32";
817ec681f3Smrg   ret[ior64] = "ior64";
827ec681f3Smrg   ret[ixor8] = "ixor8";
837ec681f3Smrg   ret[ixor16] = "ixor16";
847ec681f3Smrg   ret[ixor32] = "ixor32";
857ec681f3Smrg   ret[ixor64] = "ixor64";
867ec681f3Smrg   return ret;
877ec681f3Smrg}();
887ec681f3Smrg
897ec681f3Smrgstatic void
907ec681f3Smrgprint_reg_class(const RegClass rc, FILE* output)
917ec681f3Smrg{
927ec681f3Smrg   if (rc.is_subdword()) {
937ec681f3Smrg      fprintf(output, " v%ub: ", rc.bytes());
947ec681f3Smrg   } else if (rc.type() == RegType::sgpr) {
957ec681f3Smrg      fprintf(output, " s%u: ", rc.size());
967ec681f3Smrg   } else if (rc.is_linear()) {
977ec681f3Smrg      fprintf(output, " lv%u: ", rc.size());
987ec681f3Smrg   } else {
997ec681f3Smrg      fprintf(output, " v%u: ", rc.size());
1007ec681f3Smrg   }
1017ec681f3Smrg}
1027ec681f3Smrg
1037ec681f3Smrgvoid
1047ec681f3Smrgprint_physReg(PhysReg reg, unsigned bytes, FILE* output, unsigned flags)
1057ec681f3Smrg{
1067ec681f3Smrg   if (reg == 124) {
1077ec681f3Smrg      fprintf(output, "m0");
1087ec681f3Smrg   } else if (reg == 106) {
1097ec681f3Smrg      fprintf(output, "vcc");
1107ec681f3Smrg   } else if (reg == 253) {
1117ec681f3Smrg      fprintf(output, "scc");
1127ec681f3Smrg   } else if (reg == 126) {
1137ec681f3Smrg      fprintf(output, "exec");
1147ec681f3Smrg   } else {
1157ec681f3Smrg      bool is_vgpr = reg / 256;
1167ec681f3Smrg      unsigned r = reg % 256;
1177ec681f3Smrg      unsigned size = DIV_ROUND_UP(bytes, 4);
1187ec681f3Smrg      if (size == 1 && (flags & print_no_ssa)) {
1197ec681f3Smrg         fprintf(output, "%c%d", is_vgpr ? 'v' : 's', r);
1207ec681f3Smrg      } else {
1217ec681f3Smrg         fprintf(output, "%c[%d", is_vgpr ? 'v' : 's', r);
1227ec681f3Smrg         if (size > 1)
1237ec681f3Smrg            fprintf(output, "-%d]", r + size - 1);
1247ec681f3Smrg         else
1257ec681f3Smrg            fprintf(output, "]");
1267ec681f3Smrg      }
1277ec681f3Smrg      if (reg.byte() || bytes % 4)
1287ec681f3Smrg         fprintf(output, "[%d:%d]", reg.byte() * 8, (reg.byte() + bytes) * 8);
1297ec681f3Smrg   }
1307ec681f3Smrg}
1317ec681f3Smrg
1327ec681f3Smrgstatic void
1337ec681f3Smrgprint_constant(uint8_t reg, FILE* output)
1347ec681f3Smrg{
1357ec681f3Smrg   if (reg >= 128 && reg <= 192) {
1367ec681f3Smrg      fprintf(output, "%d", reg - 128);
1377ec681f3Smrg      return;
1387ec681f3Smrg   } else if (reg >= 192 && reg <= 208) {
1397ec681f3Smrg      fprintf(output, "%d", 192 - reg);
1407ec681f3Smrg      return;
1417ec681f3Smrg   }
1427ec681f3Smrg
1437ec681f3Smrg   switch (reg) {
1447ec681f3Smrg   case 240: fprintf(output, "0.5"); break;
1457ec681f3Smrg   case 241: fprintf(output, "-0.5"); break;
1467ec681f3Smrg   case 242: fprintf(output, "1.0"); break;
1477ec681f3Smrg   case 243: fprintf(output, "-1.0"); break;
1487ec681f3Smrg   case 244: fprintf(output, "2.0"); break;
1497ec681f3Smrg   case 245: fprintf(output, "-2.0"); break;
1507ec681f3Smrg   case 246: fprintf(output, "4.0"); break;
1517ec681f3Smrg   case 247: fprintf(output, "-4.0"); break;
1527ec681f3Smrg   case 248: fprintf(output, "1/(2*PI)"); break;
1537ec681f3Smrg   }
1547ec681f3Smrg}
1557ec681f3Smrg
1567ec681f3Smrgvoid
1577ec681f3Smrgaco_print_operand(const Operand* operand, FILE* output, unsigned flags)
1587ec681f3Smrg{
1597ec681f3Smrg   if (operand->isLiteral() || (operand->isConstant() && operand->bytes() == 1)) {
1607ec681f3Smrg      if (operand->bytes() == 1)
1617ec681f3Smrg         fprintf(output, "0x%.2x", operand->constantValue());
1627ec681f3Smrg      else if (operand->bytes() == 2)
1637ec681f3Smrg         fprintf(output, "0x%.4x", operand->constantValue());
1647ec681f3Smrg      else
1657ec681f3Smrg         fprintf(output, "0x%x", operand->constantValue());
1667ec681f3Smrg   } else if (operand->isConstant()) {
1677ec681f3Smrg      print_constant(operand->physReg().reg(), output);
1687ec681f3Smrg   } else if (operand->isUndefined()) {
1697ec681f3Smrg      print_reg_class(operand->regClass(), output);
1707ec681f3Smrg      fprintf(output, "undef");
1717ec681f3Smrg   } else {
1727ec681f3Smrg      if (operand->isLateKill())
1737ec681f3Smrg         fprintf(output, "(latekill)");
1747ec681f3Smrg      if (operand->is16bit())
1757ec681f3Smrg         fprintf(output, "(is16bit)");
1767ec681f3Smrg      if (operand->is24bit())
1777ec681f3Smrg         fprintf(output, "(is24bit)");
1787ec681f3Smrg      if ((flags & print_kill) && operand->isKill())
1797ec681f3Smrg         fprintf(output, "(kill)");
1807ec681f3Smrg
1817ec681f3Smrg      if (!(flags & print_no_ssa))
1827ec681f3Smrg         fprintf(output, "%%%d%s", operand->tempId(), operand->isFixed() ? ":" : "");
1837ec681f3Smrg
1847ec681f3Smrg      if (operand->isFixed())
1857ec681f3Smrg         print_physReg(operand->physReg(), operand->bytes(), output, flags);
1867ec681f3Smrg   }
1877ec681f3Smrg}
1887ec681f3Smrg
1897ec681f3Smrgstatic void
1907ec681f3Smrgprint_definition(const Definition* definition, FILE* output, unsigned flags)
1917ec681f3Smrg{
1927ec681f3Smrg   if (!(flags & print_no_ssa))
1937ec681f3Smrg      print_reg_class(definition->regClass(), output);
1947ec681f3Smrg   if (definition->isPrecise())
1957ec681f3Smrg      fprintf(output, "(precise)");
1967ec681f3Smrg   if (definition->isNUW())
1977ec681f3Smrg      fprintf(output, "(nuw)");
1987ec681f3Smrg   if (definition->isNoCSE())
1997ec681f3Smrg      fprintf(output, "(noCSE)");
2007ec681f3Smrg   if ((flags & print_kill) && definition->isKill())
2017ec681f3Smrg      fprintf(output, "(kill)");
2027ec681f3Smrg   if (!(flags & print_no_ssa))
2037ec681f3Smrg      fprintf(output, "%%%d%s", definition->tempId(), definition->isFixed() ? ":" : "");
2047ec681f3Smrg
2057ec681f3Smrg   if (definition->isFixed())
2067ec681f3Smrg      print_physReg(definition->physReg(), definition->bytes(), output, flags);
2077ec681f3Smrg}
2087ec681f3Smrg
2097ec681f3Smrgstatic void
2107ec681f3Smrgprint_storage(storage_class storage, FILE* output)
2117ec681f3Smrg{
2127ec681f3Smrg   fprintf(output, " storage:");
2137ec681f3Smrg   int printed = 0;
2147ec681f3Smrg   if (storage & storage_buffer)
2157ec681f3Smrg      printed += fprintf(output, "%sbuffer", printed ? "," : "");
2167ec681f3Smrg   if (storage & storage_atomic_counter)
2177ec681f3Smrg      printed += fprintf(output, "%satomic_counter", printed ? "," : "");
2187ec681f3Smrg   if (storage & storage_image)
2197ec681f3Smrg      printed += fprintf(output, "%simage", printed ? "," : "");
2207ec681f3Smrg   if (storage & storage_shared)
2217ec681f3Smrg      printed += fprintf(output, "%sshared", printed ? "," : "");
2227ec681f3Smrg   if (storage & storage_vmem_output)
2237ec681f3Smrg      printed += fprintf(output, "%svmem_output", printed ? "," : "");
2247ec681f3Smrg   if (storage & storage_scratch)
2257ec681f3Smrg      printed += fprintf(output, "%sscratch", printed ? "," : "");
2267ec681f3Smrg   if (storage & storage_vgpr_spill)
2277ec681f3Smrg      printed += fprintf(output, "%svgpr_spill", printed ? "," : "");
2287ec681f3Smrg}
2297ec681f3Smrg
2307ec681f3Smrgstatic void
2317ec681f3Smrgprint_semantics(memory_semantics sem, FILE* output)
2327ec681f3Smrg{
2337ec681f3Smrg   fprintf(output, " semantics:");
2347ec681f3Smrg   int printed = 0;
2357ec681f3Smrg   if (sem & semantic_acquire)
2367ec681f3Smrg      printed += fprintf(output, "%sacquire", printed ? "," : "");
2377ec681f3Smrg   if (sem & semantic_release)
2387ec681f3Smrg      printed += fprintf(output, "%srelease", printed ? "," : "");
2397ec681f3Smrg   if (sem & semantic_volatile)
2407ec681f3Smrg      printed += fprintf(output, "%svolatile", printed ? "," : "");
2417ec681f3Smrg   if (sem & semantic_private)
2427ec681f3Smrg      printed += fprintf(output, "%sprivate", printed ? "," : "");
2437ec681f3Smrg   if (sem & semantic_can_reorder)
2447ec681f3Smrg      printed += fprintf(output, "%sreorder", printed ? "," : "");
2457ec681f3Smrg   if (sem & semantic_atomic)
2467ec681f3Smrg      printed += fprintf(output, "%satomic", printed ? "," : "");
2477ec681f3Smrg   if (sem & semantic_rmw)
2487ec681f3Smrg      printed += fprintf(output, "%srmw", printed ? "," : "");
2497ec681f3Smrg}
2507ec681f3Smrg
2517ec681f3Smrgstatic void
2527ec681f3Smrgprint_scope(sync_scope scope, FILE* output, const char* prefix = "scope")
2537ec681f3Smrg{
2547ec681f3Smrg   fprintf(output, " %s:", prefix);
2557ec681f3Smrg   switch (scope) {
2567ec681f3Smrg   case scope_invocation: fprintf(output, "invocation"); break;
2577ec681f3Smrg   case scope_subgroup: fprintf(output, "subgroup"); break;
2587ec681f3Smrg   case scope_workgroup: fprintf(output, "workgroup"); break;
2597ec681f3Smrg   case scope_queuefamily: fprintf(output, "queuefamily"); break;
2607ec681f3Smrg   case scope_device: fprintf(output, "device"); break;
2617ec681f3Smrg   }
2627ec681f3Smrg}
2637ec681f3Smrg
2647ec681f3Smrgstatic void
2657ec681f3Smrgprint_sync(memory_sync_info sync, FILE* output)
2667ec681f3Smrg{
2677ec681f3Smrg   print_storage(sync.storage, output);
2687ec681f3Smrg   print_semantics(sync.semantics, output);
2697ec681f3Smrg   print_scope(sync.scope, output);
2707ec681f3Smrg}
2717ec681f3Smrg
2727ec681f3Smrgstatic void
2737ec681f3Smrgprint_instr_format_specific(const Instruction* instr, FILE* output)
2747ec681f3Smrg{
2757ec681f3Smrg   switch (instr->format) {
2767ec681f3Smrg   case Format::SOPK: {
2777ec681f3Smrg      const SOPK_instruction& sopk = instr->sopk();
2787ec681f3Smrg      fprintf(output, " imm:%d", sopk.imm & 0x8000 ? (sopk.imm - 65536) : sopk.imm);
2797ec681f3Smrg      break;
2807ec681f3Smrg   }
2817ec681f3Smrg   case Format::SOPP: {
2827ec681f3Smrg      uint16_t imm = instr->sopp().imm;
2837ec681f3Smrg      switch (instr->opcode) {
2847ec681f3Smrg      case aco_opcode::s_waitcnt: {
2857ec681f3Smrg         /* we usually should check the chip class for vmcnt/lgkm, but
2867ec681f3Smrg          * insert_waitcnt() should fill it in regardless. */
2877ec681f3Smrg         unsigned vmcnt = (imm & 0xF) | ((imm & (0x3 << 14)) >> 10);
2887ec681f3Smrg         if (vmcnt != 63)
2897ec681f3Smrg            fprintf(output, " vmcnt(%d)", vmcnt);
2907ec681f3Smrg         if (((imm >> 4) & 0x7) < 0x7)
2917ec681f3Smrg            fprintf(output, " expcnt(%d)", (imm >> 4) & 0x7);
2927ec681f3Smrg         if (((imm >> 8) & 0x3F) < 0x3F)
2937ec681f3Smrg            fprintf(output, " lgkmcnt(%d)", (imm >> 8) & 0x3F);
2947ec681f3Smrg         break;
2957ec681f3Smrg      }
2967ec681f3Smrg      case aco_opcode::s_endpgm:
2977ec681f3Smrg      case aco_opcode::s_endpgm_saved:
2987ec681f3Smrg      case aco_opcode::s_endpgm_ordered_ps_done:
2997ec681f3Smrg      case aco_opcode::s_wakeup:
3007ec681f3Smrg      case aco_opcode::s_barrier:
3017ec681f3Smrg      case aco_opcode::s_icache_inv:
3027ec681f3Smrg      case aco_opcode::s_ttracedata:
3037ec681f3Smrg      case aco_opcode::s_set_gpr_idx_off: {
3047ec681f3Smrg         break;
3057ec681f3Smrg      }
3067ec681f3Smrg      case aco_opcode::s_sendmsg: {
3077ec681f3Smrg         unsigned id = imm & sendmsg_id_mask;
3087ec681f3Smrg         switch (id) {
3097ec681f3Smrg         case sendmsg_none: fprintf(output, " sendmsg(MSG_NONE)"); break;
3107ec681f3Smrg         case _sendmsg_gs:
3117ec681f3Smrg            fprintf(output, " sendmsg(gs%s%s, %u)", imm & 0x10 ? ", cut" : "",
3127ec681f3Smrg                    imm & 0x20 ? ", emit" : "", imm >> 8);
3137ec681f3Smrg            break;
3147ec681f3Smrg         case _sendmsg_gs_done:
3157ec681f3Smrg            fprintf(output, " sendmsg(gs_done%s%s, %u)", imm & 0x10 ? ", cut" : "",
3167ec681f3Smrg                    imm & 0x20 ? ", emit" : "", imm >> 8);
3177ec681f3Smrg            break;
3187ec681f3Smrg         case sendmsg_save_wave: fprintf(output, " sendmsg(save_wave)"); break;
3197ec681f3Smrg         case sendmsg_stall_wave_gen: fprintf(output, " sendmsg(stall_wave_gen)"); break;
3207ec681f3Smrg         case sendmsg_halt_waves: fprintf(output, " sendmsg(halt_waves)"); break;
3217ec681f3Smrg         case sendmsg_ordered_ps_done: fprintf(output, " sendmsg(ordered_ps_done)"); break;
3227ec681f3Smrg         case sendmsg_early_prim_dealloc: fprintf(output, " sendmsg(early_prim_dealloc)"); break;
3237ec681f3Smrg         case sendmsg_gs_alloc_req: fprintf(output, " sendmsg(gs_alloc_req)"); break;
3247ec681f3Smrg         }
3257ec681f3Smrg         break;
3267ec681f3Smrg      }
3277ec681f3Smrg      default: {
3287ec681f3Smrg         if (imm)
3297ec681f3Smrg            fprintf(output, " imm:%u", imm);
3307ec681f3Smrg         break;
3317ec681f3Smrg      }
3327ec681f3Smrg      }
3337ec681f3Smrg      if (instr->sopp().block != -1)
3347ec681f3Smrg         fprintf(output, " block:BB%d", instr->sopp().block);
3357ec681f3Smrg      break;
3367ec681f3Smrg   }
3377ec681f3Smrg   case Format::SMEM: {
3387ec681f3Smrg      const SMEM_instruction& smem = instr->smem();
3397ec681f3Smrg      if (smem.glc)
3407ec681f3Smrg         fprintf(output, " glc");
3417ec681f3Smrg      if (smem.dlc)
3427ec681f3Smrg         fprintf(output, " dlc");
3437ec681f3Smrg      if (smem.nv)
3447ec681f3Smrg         fprintf(output, " nv");
3457ec681f3Smrg      print_sync(smem.sync, output);
3467ec681f3Smrg      break;
3477ec681f3Smrg   }
3487ec681f3Smrg   case Format::VINTRP: {
3497ec681f3Smrg      const Interp_instruction& vintrp = instr->vintrp();
3507ec681f3Smrg      fprintf(output, " attr%d.%c", vintrp.attribute, "xyzw"[vintrp.component]);
3517ec681f3Smrg      break;
3527ec681f3Smrg   }
3537ec681f3Smrg   case Format::DS: {
3547ec681f3Smrg      const DS_instruction& ds = instr->ds();
3557ec681f3Smrg      if (ds.offset0)
3567ec681f3Smrg         fprintf(output, " offset0:%u", ds.offset0);
3577ec681f3Smrg      if (ds.offset1)
3587ec681f3Smrg         fprintf(output, " offset1:%u", ds.offset1);
3597ec681f3Smrg      if (ds.gds)
3607ec681f3Smrg         fprintf(output, " gds");
3617ec681f3Smrg      print_sync(ds.sync, output);
3627ec681f3Smrg      break;
3637ec681f3Smrg   }
3647ec681f3Smrg   case Format::MUBUF: {
3657ec681f3Smrg      const MUBUF_instruction& mubuf = instr->mubuf();
3667ec681f3Smrg      if (mubuf.offset)
3677ec681f3Smrg         fprintf(output, " offset:%u", mubuf.offset);
3687ec681f3Smrg      if (mubuf.offen)
3697ec681f3Smrg         fprintf(output, " offen");
3707ec681f3Smrg      if (mubuf.idxen)
3717ec681f3Smrg         fprintf(output, " idxen");
3727ec681f3Smrg      if (mubuf.addr64)
3737ec681f3Smrg         fprintf(output, " addr64");
3747ec681f3Smrg      if (mubuf.glc)
3757ec681f3Smrg         fprintf(output, " glc");
3767ec681f3Smrg      if (mubuf.dlc)
3777ec681f3Smrg         fprintf(output, " dlc");
3787ec681f3Smrg      if (mubuf.slc)
3797ec681f3Smrg         fprintf(output, " slc");
3807ec681f3Smrg      if (mubuf.tfe)
3817ec681f3Smrg         fprintf(output, " tfe");
3827ec681f3Smrg      if (mubuf.lds)
3837ec681f3Smrg         fprintf(output, " lds");
3847ec681f3Smrg      if (mubuf.disable_wqm)
3857ec681f3Smrg         fprintf(output, " disable_wqm");
3867ec681f3Smrg      print_sync(mubuf.sync, output);
3877ec681f3Smrg      break;
3887ec681f3Smrg   }
3897ec681f3Smrg   case Format::MIMG: {
3907ec681f3Smrg      const MIMG_instruction& mimg = instr->mimg();
3917ec681f3Smrg      unsigned identity_dmask =
3927ec681f3Smrg         !instr->definitions.empty() ? (1 << instr->definitions[0].size()) - 1 : 0xf;
3937ec681f3Smrg      if ((mimg.dmask & identity_dmask) != identity_dmask)
3947ec681f3Smrg         fprintf(output, " dmask:%s%s%s%s", mimg.dmask & 0x1 ? "x" : "",
3957ec681f3Smrg                 mimg.dmask & 0x2 ? "y" : "", mimg.dmask & 0x4 ? "z" : "",
3967ec681f3Smrg                 mimg.dmask & 0x8 ? "w" : "");
3977ec681f3Smrg      switch (mimg.dim) {
3987ec681f3Smrg      case ac_image_1d: fprintf(output, " 1d"); break;
3997ec681f3Smrg      case ac_image_2d: fprintf(output, " 2d"); break;
4007ec681f3Smrg      case ac_image_3d: fprintf(output, " 3d"); break;
4017ec681f3Smrg      case ac_image_cube: fprintf(output, " cube"); break;
4027ec681f3Smrg      case ac_image_1darray: fprintf(output, " 1darray"); break;
4037ec681f3Smrg      case ac_image_2darray: fprintf(output, " 2darray"); break;
4047ec681f3Smrg      case ac_image_2dmsaa: fprintf(output, " 2dmsaa"); break;
4057ec681f3Smrg      case ac_image_2darraymsaa: fprintf(output, " 2darraymsaa"); break;
4067ec681f3Smrg      }
4077ec681f3Smrg      if (mimg.unrm)
4087ec681f3Smrg         fprintf(output, " unrm");
4097ec681f3Smrg      if (mimg.glc)
4107ec681f3Smrg         fprintf(output, " glc");
4117ec681f3Smrg      if (mimg.dlc)
4127ec681f3Smrg         fprintf(output, " dlc");
4137ec681f3Smrg      if (mimg.slc)
4147ec681f3Smrg         fprintf(output, " slc");
4157ec681f3Smrg      if (mimg.tfe)
4167ec681f3Smrg         fprintf(output, " tfe");
4177ec681f3Smrg      if (mimg.da)
4187ec681f3Smrg         fprintf(output, " da");
4197ec681f3Smrg      if (mimg.lwe)
4207ec681f3Smrg         fprintf(output, " lwe");
4217ec681f3Smrg      if (mimg.r128 || mimg.a16)
4227ec681f3Smrg         fprintf(output, " r128/a16");
4237ec681f3Smrg      if (mimg.d16)
4247ec681f3Smrg         fprintf(output, " d16");
4257ec681f3Smrg      if (mimg.disable_wqm)
4267ec681f3Smrg         fprintf(output, " disable_wqm");
4277ec681f3Smrg      print_sync(mimg.sync, output);
4287ec681f3Smrg      break;
4297ec681f3Smrg   }
4307ec681f3Smrg   case Format::EXP: {
4317ec681f3Smrg      const Export_instruction& exp = instr->exp();
4327ec681f3Smrg      unsigned identity_mask = exp.compressed ? 0x5 : 0xf;
4337ec681f3Smrg      if ((exp.enabled_mask & identity_mask) != identity_mask)
4347ec681f3Smrg         fprintf(output, " en:%c%c%c%c", exp.enabled_mask & 0x1 ? 'r' : '*',
4357ec681f3Smrg                 exp.enabled_mask & 0x2 ? 'g' : '*', exp.enabled_mask & 0x4 ? 'b' : '*',
4367ec681f3Smrg                 exp.enabled_mask & 0x8 ? 'a' : '*');
4377ec681f3Smrg      if (exp.compressed)
4387ec681f3Smrg         fprintf(output, " compr");
4397ec681f3Smrg      if (exp.done)
4407ec681f3Smrg         fprintf(output, " done");
4417ec681f3Smrg      if (exp.valid_mask)
4427ec681f3Smrg         fprintf(output, " vm");
4437ec681f3Smrg
4447ec681f3Smrg      if (exp.dest <= V_008DFC_SQ_EXP_MRT + 7)
4457ec681f3Smrg         fprintf(output, " mrt%d", exp.dest - V_008DFC_SQ_EXP_MRT);
4467ec681f3Smrg      else if (exp.dest == V_008DFC_SQ_EXP_MRTZ)
4477ec681f3Smrg         fprintf(output, " mrtz");
4487ec681f3Smrg      else if (exp.dest == V_008DFC_SQ_EXP_NULL)
4497ec681f3Smrg         fprintf(output, " null");
4507ec681f3Smrg      else if (exp.dest >= V_008DFC_SQ_EXP_POS && exp.dest <= V_008DFC_SQ_EXP_POS + 3)
4517ec681f3Smrg         fprintf(output, " pos%d", exp.dest - V_008DFC_SQ_EXP_POS);
4527ec681f3Smrg      else if (exp.dest >= V_008DFC_SQ_EXP_PARAM && exp.dest <= V_008DFC_SQ_EXP_PARAM + 31)
4537ec681f3Smrg         fprintf(output, " param%d", exp.dest - V_008DFC_SQ_EXP_PARAM);
4547ec681f3Smrg      break;
4557ec681f3Smrg   }
4567ec681f3Smrg   case Format::PSEUDO_BRANCH: {
4577ec681f3Smrg      const Pseudo_branch_instruction& branch = instr->branch();
4587ec681f3Smrg      /* Note: BB0 cannot be a branch target */
4597ec681f3Smrg      if (branch.target[0] != 0)
4607ec681f3Smrg         fprintf(output, " BB%d", branch.target[0]);
4617ec681f3Smrg      if (branch.target[1] != 0)
4627ec681f3Smrg         fprintf(output, ", BB%d", branch.target[1]);
4637ec681f3Smrg      break;
4647ec681f3Smrg   }
4657ec681f3Smrg   case Format::PSEUDO_REDUCTION: {
4667ec681f3Smrg      const Pseudo_reduction_instruction& reduce = instr->reduction();
4677ec681f3Smrg      fprintf(output, " op:%s", reduce_ops[reduce.reduce_op]);
4687ec681f3Smrg      if (reduce.cluster_size)
4697ec681f3Smrg         fprintf(output, " cluster_size:%u", reduce.cluster_size);
4707ec681f3Smrg      break;
4717ec681f3Smrg   }
4727ec681f3Smrg   case Format::PSEUDO_BARRIER: {
4737ec681f3Smrg      const Pseudo_barrier_instruction& barrier = instr->barrier();
4747ec681f3Smrg      print_sync(barrier.sync, output);
4757ec681f3Smrg      print_scope(barrier.exec_scope, output, "exec_scope");
4767ec681f3Smrg      break;
4777ec681f3Smrg   }
4787ec681f3Smrg   case Format::FLAT:
4797ec681f3Smrg   case Format::GLOBAL:
4807ec681f3Smrg   case Format::SCRATCH: {
4817ec681f3Smrg      const FLAT_instruction& flat = instr->flatlike();
4827ec681f3Smrg      if (flat.offset)
4837ec681f3Smrg         fprintf(output, " offset:%u", flat.offset);
4847ec681f3Smrg      if (flat.glc)
4857ec681f3Smrg         fprintf(output, " glc");
4867ec681f3Smrg      if (flat.dlc)
4877ec681f3Smrg         fprintf(output, " dlc");
4887ec681f3Smrg      if (flat.slc)
4897ec681f3Smrg         fprintf(output, " slc");
4907ec681f3Smrg      if (flat.lds)
4917ec681f3Smrg         fprintf(output, " lds");
4927ec681f3Smrg      if (flat.nv)
4937ec681f3Smrg         fprintf(output, " nv");
4947ec681f3Smrg      if (flat.disable_wqm)
4957ec681f3Smrg         fprintf(output, " disable_wqm");
4967ec681f3Smrg      print_sync(flat.sync, output);
4977ec681f3Smrg      break;
4987ec681f3Smrg   }
4997ec681f3Smrg   case Format::MTBUF: {
5007ec681f3Smrg      const MTBUF_instruction& mtbuf = instr->mtbuf();
5017ec681f3Smrg      fprintf(output, " dfmt:");
5027ec681f3Smrg      switch (mtbuf.dfmt) {
5037ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_8: fprintf(output, "8"); break;
5047ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_16: fprintf(output, "16"); break;
5057ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_8_8: fprintf(output, "8_8"); break;
5067ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_32: fprintf(output, "32"); break;
5077ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_16_16: fprintf(output, "16_16"); break;
5087ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_10_11_11: fprintf(output, "10_11_11"); break;
5097ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_11_11_10: fprintf(output, "11_11_10"); break;
5107ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_10_10_10_2: fprintf(output, "10_10_10_2"); break;
5117ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_2_10_10_10: fprintf(output, "2_10_10_10"); break;
5127ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_8_8_8_8: fprintf(output, "8_8_8_8"); break;
5137ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_32_32: fprintf(output, "32_32"); break;
5147ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_16_16_16_16: fprintf(output, "16_16_16_16"); break;
5157ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_32_32_32: fprintf(output, "32_32_32"); break;
5167ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_32_32_32_32: fprintf(output, "32_32_32_32"); break;
5177ec681f3Smrg      case V_008F0C_BUF_DATA_FORMAT_RESERVED_15: fprintf(output, "reserved15"); break;
5187ec681f3Smrg      }
5197ec681f3Smrg      fprintf(output, " nfmt:");
5207ec681f3Smrg      switch (mtbuf.nfmt) {
5217ec681f3Smrg      case V_008F0C_BUF_NUM_FORMAT_UNORM: fprintf(output, "unorm"); break;
5227ec681f3Smrg      case V_008F0C_BUF_NUM_FORMAT_SNORM: fprintf(output, "snorm"); break;
5237ec681f3Smrg      case V_008F0C_BUF_NUM_FORMAT_USCALED: fprintf(output, "uscaled"); break;
5247ec681f3Smrg      case V_008F0C_BUF_NUM_FORMAT_SSCALED: fprintf(output, "sscaled"); break;
5257ec681f3Smrg      case V_008F0C_BUF_NUM_FORMAT_UINT: fprintf(output, "uint"); break;
5267ec681f3Smrg      case V_008F0C_BUF_NUM_FORMAT_SINT: fprintf(output, "sint"); break;
5277ec681f3Smrg      case V_008F0C_BUF_NUM_FORMAT_SNORM_OGL: fprintf(output, "snorm"); break;
5287ec681f3Smrg      case V_008F0C_BUF_NUM_FORMAT_FLOAT: fprintf(output, "float"); break;
5297ec681f3Smrg      }
5307ec681f3Smrg      if (mtbuf.offset)
5317ec681f3Smrg         fprintf(output, " offset:%u", mtbuf.offset);
5327ec681f3Smrg      if (mtbuf.offen)
5337ec681f3Smrg         fprintf(output, " offen");
5347ec681f3Smrg      if (mtbuf.idxen)
5357ec681f3Smrg         fprintf(output, " idxen");
5367ec681f3Smrg      if (mtbuf.glc)
5377ec681f3Smrg         fprintf(output, " glc");
5387ec681f3Smrg      if (mtbuf.dlc)
5397ec681f3Smrg         fprintf(output, " dlc");
5407ec681f3Smrg      if (mtbuf.slc)
5417ec681f3Smrg         fprintf(output, " slc");
5427ec681f3Smrg      if (mtbuf.tfe)
5437ec681f3Smrg         fprintf(output, " tfe");
5447ec681f3Smrg      if (mtbuf.disable_wqm)
5457ec681f3Smrg         fprintf(output, " disable_wqm");
5467ec681f3Smrg      print_sync(mtbuf.sync, output);
5477ec681f3Smrg      break;
5487ec681f3Smrg   }
5497ec681f3Smrg   case Format::VOP3P: {
5507ec681f3Smrg      if (instr->vop3p().clamp)
5517ec681f3Smrg         fprintf(output, " clamp");
5527ec681f3Smrg      break;
5537ec681f3Smrg   }
5547ec681f3Smrg   default: {
5557ec681f3Smrg      break;
5567ec681f3Smrg   }
5577ec681f3Smrg   }
5587ec681f3Smrg   if (instr->isVOP3()) {
5597ec681f3Smrg      const VOP3_instruction& vop3 = instr->vop3();
5607ec681f3Smrg      switch (vop3.omod) {
5617ec681f3Smrg      case 1: fprintf(output, " *2"); break;
5627ec681f3Smrg      case 2: fprintf(output, " *4"); break;
5637ec681f3Smrg      case 3: fprintf(output, " *0.5"); break;
5647ec681f3Smrg      }
5657ec681f3Smrg      if (vop3.clamp)
5667ec681f3Smrg         fprintf(output, " clamp");
5677ec681f3Smrg      if (vop3.opsel & (1 << 3))
5687ec681f3Smrg         fprintf(output, " opsel_hi");
5697ec681f3Smrg   } else if (instr->isDPP()) {
5707ec681f3Smrg      const DPP_instruction& dpp = instr->dpp();
5717ec681f3Smrg      if (dpp.dpp_ctrl <= 0xff) {
5727ec681f3Smrg         fprintf(output, " quad_perm:[%d,%d,%d,%d]", dpp.dpp_ctrl & 0x3, (dpp.dpp_ctrl >> 2) & 0x3,
5737ec681f3Smrg                 (dpp.dpp_ctrl >> 4) & 0x3, (dpp.dpp_ctrl >> 6) & 0x3);
5747ec681f3Smrg      } else if (dpp.dpp_ctrl >= 0x101 && dpp.dpp_ctrl <= 0x10f) {
5757ec681f3Smrg         fprintf(output, " row_shl:%d", dpp.dpp_ctrl & 0xf);
5767ec681f3Smrg      } else if (dpp.dpp_ctrl >= 0x111 && dpp.dpp_ctrl <= 0x11f) {
5777ec681f3Smrg         fprintf(output, " row_shr:%d", dpp.dpp_ctrl & 0xf);
5787ec681f3Smrg      } else if (dpp.dpp_ctrl >= 0x121 && dpp.dpp_ctrl <= 0x12f) {
5797ec681f3Smrg         fprintf(output, " row_ror:%d", dpp.dpp_ctrl & 0xf);
5807ec681f3Smrg      } else if (dpp.dpp_ctrl == dpp_wf_sl1) {
5817ec681f3Smrg         fprintf(output, " wave_shl:1");
5827ec681f3Smrg      } else if (dpp.dpp_ctrl == dpp_wf_rl1) {
5837ec681f3Smrg         fprintf(output, " wave_rol:1");
5847ec681f3Smrg      } else if (dpp.dpp_ctrl == dpp_wf_sr1) {
5857ec681f3Smrg         fprintf(output, " wave_shr:1");
5867ec681f3Smrg      } else if (dpp.dpp_ctrl == dpp_wf_rr1) {
5877ec681f3Smrg         fprintf(output, " wave_ror:1");
5887ec681f3Smrg      } else if (dpp.dpp_ctrl == dpp_row_mirror) {
5897ec681f3Smrg         fprintf(output, " row_mirror");
5907ec681f3Smrg      } else if (dpp.dpp_ctrl == dpp_row_half_mirror) {
5917ec681f3Smrg         fprintf(output, " row_half_mirror");
5927ec681f3Smrg      } else if (dpp.dpp_ctrl == dpp_row_bcast15) {
5937ec681f3Smrg         fprintf(output, " row_bcast:15");
5947ec681f3Smrg      } else if (dpp.dpp_ctrl == dpp_row_bcast31) {
5957ec681f3Smrg         fprintf(output, " row_bcast:31");
5967ec681f3Smrg      } else {
5977ec681f3Smrg         fprintf(output, " dpp_ctrl:0x%.3x", dpp.dpp_ctrl);
5987ec681f3Smrg      }
5997ec681f3Smrg      if (dpp.row_mask != 0xf)
6007ec681f3Smrg         fprintf(output, " row_mask:0x%.1x", dpp.row_mask);
6017ec681f3Smrg      if (dpp.bank_mask != 0xf)
6027ec681f3Smrg         fprintf(output, " bank_mask:0x%.1x", dpp.bank_mask);
6037ec681f3Smrg      if (dpp.bound_ctrl)
6047ec681f3Smrg         fprintf(output, " bound_ctrl:1");
6057ec681f3Smrg   } else if (instr->isSDWA()) {
6067ec681f3Smrg      const SDWA_instruction& sdwa = instr->sdwa();
6077ec681f3Smrg      switch (sdwa.omod) {
6087ec681f3Smrg      case 1: fprintf(output, " *2"); break;
6097ec681f3Smrg      case 2: fprintf(output, " *4"); break;
6107ec681f3Smrg      case 3: fprintf(output, " *0.5"); break;
6117ec681f3Smrg      }
6127ec681f3Smrg      if (sdwa.clamp)
6137ec681f3Smrg         fprintf(output, " clamp");
6147ec681f3Smrg      if (!instr->isVOPC()) {
6157ec681f3Smrg         char sext = sdwa.dst_sel.sign_extend() ? 's' : 'u';
6167ec681f3Smrg         unsigned offset = sdwa.dst_sel.offset();
6177ec681f3Smrg         if (instr->definitions[0].isFixed())
6187ec681f3Smrg            offset += instr->definitions[0].physReg().byte();
6197ec681f3Smrg         switch (sdwa.dst_sel.size()) {
6207ec681f3Smrg         case 1: fprintf(output, " dst_sel:%cbyte%u", sext, offset); break;
6217ec681f3Smrg         case 2: fprintf(output, " dst_sel:%cword%u", sext, offset >> 1); break;
6227ec681f3Smrg         case 4: fprintf(output, " dst_sel:dword"); break;
6237ec681f3Smrg         default: break;
6247ec681f3Smrg         }
6257ec681f3Smrg         if (instr->definitions[0].bytes() < 4)
6267ec681f3Smrg            fprintf(output, " dst_preserve");
6277ec681f3Smrg      }
6287ec681f3Smrg      for (unsigned i = 0; i < std::min<unsigned>(2, instr->operands.size()); i++) {
6297ec681f3Smrg         char sext = sdwa.sel[i].sign_extend() ? 's' : 'u';
6307ec681f3Smrg         unsigned offset = sdwa.sel[i].offset();
6317ec681f3Smrg         if (instr->operands[i].isFixed())
6327ec681f3Smrg            offset += instr->operands[i].physReg().byte();
6337ec681f3Smrg         switch (sdwa.sel[i].size()) {
6347ec681f3Smrg         case 1: fprintf(output, " src%d_sel:%cbyte%u", i, sext, offset); break;
6357ec681f3Smrg         case 2: fprintf(output, " src%d_sel:%cword%u", i, sext, offset >> 1); break;
6367ec681f3Smrg         case 4: fprintf(output, " src%d_sel:dword", i); break;
6377ec681f3Smrg         default: break;
6387ec681f3Smrg         }
6397ec681f3Smrg      }
6407ec681f3Smrg   }
6417ec681f3Smrg}
6427ec681f3Smrg
6437ec681f3Smrgvoid
6447ec681f3Smrgaco_print_instr(const Instruction* instr, FILE* output, unsigned flags)
6457ec681f3Smrg{
6467ec681f3Smrg   if (!instr->definitions.empty()) {
6477ec681f3Smrg      for (unsigned i = 0; i < instr->definitions.size(); ++i) {
6487ec681f3Smrg         print_definition(&instr->definitions[i], output, flags);
6497ec681f3Smrg         if (i + 1 != instr->definitions.size())
6507ec681f3Smrg            fprintf(output, ", ");
6517ec681f3Smrg      }
6527ec681f3Smrg      fprintf(output, " = ");
6537ec681f3Smrg   }
6547ec681f3Smrg   fprintf(output, "%s", instr_info.name[(int)instr->opcode]);
6557ec681f3Smrg   if (instr->operands.size()) {
6567ec681f3Smrg      bool* const abs = (bool*)alloca(instr->operands.size() * sizeof(bool));
6577ec681f3Smrg      bool* const neg = (bool*)alloca(instr->operands.size() * sizeof(bool));
6587ec681f3Smrg      bool* const opsel = (bool*)alloca(instr->operands.size() * sizeof(bool));
6597ec681f3Smrg      for (unsigned i = 0; i < instr->operands.size(); ++i) {
6607ec681f3Smrg         abs[i] = false;
6617ec681f3Smrg         neg[i] = false;
6627ec681f3Smrg         opsel[i] = false;
6637ec681f3Smrg      }
6647ec681f3Smrg      if (instr->isVOP3()) {
6657ec681f3Smrg         const VOP3_instruction& vop3 = instr->vop3();
6667ec681f3Smrg         for (unsigned i = 0; i < 3; ++i) {
6677ec681f3Smrg            abs[i] = vop3.abs[i];
6687ec681f3Smrg            neg[i] = vop3.neg[i];
6697ec681f3Smrg            opsel[i] = vop3.opsel & (1 << i);
6707ec681f3Smrg         }
6717ec681f3Smrg      } else if (instr->isDPP()) {
6727ec681f3Smrg         const DPP_instruction& dpp = instr->dpp();
6737ec681f3Smrg         for (unsigned i = 0; i < 2; ++i) {
6747ec681f3Smrg            abs[i] = dpp.abs[i];
6757ec681f3Smrg            neg[i] = dpp.neg[i];
6767ec681f3Smrg            opsel[i] = false;
6777ec681f3Smrg         }
6787ec681f3Smrg      } else if (instr->isSDWA()) {
6797ec681f3Smrg         const SDWA_instruction& sdwa = instr->sdwa();
6807ec681f3Smrg         for (unsigned i = 0; i < 2; ++i) {
6817ec681f3Smrg            abs[i] = sdwa.abs[i];
6827ec681f3Smrg            neg[i] = sdwa.neg[i];
6837ec681f3Smrg            opsel[i] = false;
6847ec681f3Smrg         }
6857ec681f3Smrg      }
6867ec681f3Smrg      for (unsigned i = 0; i < instr->operands.size(); ++i) {
6877ec681f3Smrg         if (i)
6887ec681f3Smrg            fprintf(output, ", ");
6897ec681f3Smrg         else
6907ec681f3Smrg            fprintf(output, " ");
6917ec681f3Smrg
6927ec681f3Smrg         if (neg[i])
6937ec681f3Smrg            fprintf(output, "-");
6947ec681f3Smrg         if (abs[i])
6957ec681f3Smrg            fprintf(output, "|");
6967ec681f3Smrg         if (opsel[i])
6977ec681f3Smrg            fprintf(output, "hi(");
6987ec681f3Smrg         aco_print_operand(&instr->operands[i], output, flags);
6997ec681f3Smrg         if (opsel[i])
7007ec681f3Smrg            fprintf(output, ")");
7017ec681f3Smrg         if (abs[i])
7027ec681f3Smrg            fprintf(output, "|");
7037ec681f3Smrg
7047ec681f3Smrg         if (instr->isVOP3P()) {
7057ec681f3Smrg            const VOP3P_instruction& vop3 = instr->vop3p();
7067ec681f3Smrg            if ((vop3.opsel_lo & (1 << i)) || !(vop3.opsel_hi & (1 << i))) {
7077ec681f3Smrg               fprintf(output, ".%c%c", vop3.opsel_lo & (1 << i) ? 'y' : 'x',
7087ec681f3Smrg                       vop3.opsel_hi & (1 << i) ? 'y' : 'x');
7097ec681f3Smrg            }
7107ec681f3Smrg            if (vop3.neg_lo[i] && vop3.neg_hi[i])
7117ec681f3Smrg               fprintf(output, "*[-1,-1]");
7127ec681f3Smrg            else if (vop3.neg_lo[i])
7137ec681f3Smrg               fprintf(output, "*[-1,1]");
7147ec681f3Smrg            else if (vop3.neg_hi[i])
7157ec681f3Smrg               fprintf(output, "*[1,-1]");
7167ec681f3Smrg         }
7177ec681f3Smrg      }
7187ec681f3Smrg   }
7197ec681f3Smrg   print_instr_format_specific(instr, output);
7207ec681f3Smrg}
7217ec681f3Smrg
7227ec681f3Smrgstatic void
7237ec681f3Smrgprint_block_kind(uint16_t kind, FILE* output)
7247ec681f3Smrg{
7257ec681f3Smrg   if (kind & block_kind_uniform)
7267ec681f3Smrg      fprintf(output, "uniform, ");
7277ec681f3Smrg   if (kind & block_kind_top_level)
7287ec681f3Smrg      fprintf(output, "top-level, ");
7297ec681f3Smrg   if (kind & block_kind_loop_preheader)
7307ec681f3Smrg      fprintf(output, "loop-preheader, ");
7317ec681f3Smrg   if (kind & block_kind_loop_header)
7327ec681f3Smrg      fprintf(output, "loop-header, ");
7337ec681f3Smrg   if (kind & block_kind_loop_exit)
7347ec681f3Smrg      fprintf(output, "loop-exit, ");
7357ec681f3Smrg   if (kind & block_kind_continue)
7367ec681f3Smrg      fprintf(output, "continue, ");
7377ec681f3Smrg   if (kind & block_kind_break)
7387ec681f3Smrg      fprintf(output, "break, ");
7397ec681f3Smrg   if (kind & block_kind_continue_or_break)
7407ec681f3Smrg      fprintf(output, "continue_or_break, ");
7417ec681f3Smrg   if (kind & block_kind_discard)
7427ec681f3Smrg      fprintf(output, "discard, ");
7437ec681f3Smrg   if (kind & block_kind_branch)
7447ec681f3Smrg      fprintf(output, "branch, ");
7457ec681f3Smrg   if (kind & block_kind_merge)
7467ec681f3Smrg      fprintf(output, "merge, ");
7477ec681f3Smrg   if (kind & block_kind_invert)
7487ec681f3Smrg      fprintf(output, "invert, ");
7497ec681f3Smrg   if (kind & block_kind_uses_discard_if)
7507ec681f3Smrg      fprintf(output, "discard_if, ");
7517ec681f3Smrg   if (kind & block_kind_needs_lowering)
7527ec681f3Smrg      fprintf(output, "needs_lowering, ");
7537ec681f3Smrg   if (kind & block_kind_uses_demote)
7547ec681f3Smrg      fprintf(output, "uses_demote, ");
7557ec681f3Smrg   if (kind & block_kind_export_end)
7567ec681f3Smrg      fprintf(output, "export_end, ");
7577ec681f3Smrg}
7587ec681f3Smrg
7597ec681f3Smrgstatic void
7607ec681f3Smrgprint_stage(Stage stage, FILE* output)
7617ec681f3Smrg{
7627ec681f3Smrg   fprintf(output, "ACO shader stage: ");
7637ec681f3Smrg
7647ec681f3Smrg   if (stage == compute_cs)
7657ec681f3Smrg      fprintf(output, "compute_cs");
7667ec681f3Smrg   else if (stage == fragment_fs)
7677ec681f3Smrg      fprintf(output, "fragment_fs");
7687ec681f3Smrg   else if (stage == gs_copy_vs)
7697ec681f3Smrg      fprintf(output, "gs_copy_vs");
7707ec681f3Smrg   else if (stage == vertex_ls)
7717ec681f3Smrg      fprintf(output, "vertex_ls");
7727ec681f3Smrg   else if (stage == vertex_es)
7737ec681f3Smrg      fprintf(output, "vertex_es");
7747ec681f3Smrg   else if (stage == vertex_vs)
7757ec681f3Smrg      fprintf(output, "vertex_vs");
7767ec681f3Smrg   else if (stage == tess_control_hs)
7777ec681f3Smrg      fprintf(output, "tess_control_hs");
7787ec681f3Smrg   else if (stage == vertex_tess_control_hs)
7797ec681f3Smrg      fprintf(output, "vertex_tess_control_hs");
7807ec681f3Smrg   else if (stage == tess_eval_es)
7817ec681f3Smrg      fprintf(output, "tess_eval_es");
7827ec681f3Smrg   else if (stage == tess_eval_vs)
7837ec681f3Smrg      fprintf(output, "tess_eval_vs");
7847ec681f3Smrg   else if (stage == geometry_gs)
7857ec681f3Smrg      fprintf(output, "geometry_gs");
7867ec681f3Smrg   else if (stage == vertex_geometry_gs)
7877ec681f3Smrg      fprintf(output, "vertex_geometry_gs");
7887ec681f3Smrg   else if (stage == tess_eval_geometry_gs)
7897ec681f3Smrg      fprintf(output, "tess_eval_geometry_gs");
7907ec681f3Smrg   else if (stage == vertex_ngg)
7917ec681f3Smrg      fprintf(output, "vertex_ngg");
7927ec681f3Smrg   else if (stage == tess_eval_ngg)
7937ec681f3Smrg      fprintf(output, "tess_eval_ngg");
7947ec681f3Smrg   else if (stage == vertex_geometry_ngg)
7957ec681f3Smrg      fprintf(output, "vertex_geometry_ngg");
7967ec681f3Smrg   else if (stage == tess_eval_geometry_ngg)
7977ec681f3Smrg      fprintf(output, "tess_eval_geometry_ngg");
7987ec681f3Smrg   else
7997ec681f3Smrg      fprintf(output, "unknown");
8007ec681f3Smrg
8017ec681f3Smrg   fprintf(output, "\n");
8027ec681f3Smrg}
8037ec681f3Smrg
8047ec681f3Smrgvoid
8057ec681f3Smrgaco_print_block(const Block* block, FILE* output, unsigned flags, const live& live_vars)
8067ec681f3Smrg{
8077ec681f3Smrg   fprintf(output, "BB%d\n", block->index);
8087ec681f3Smrg   fprintf(output, "/* logical preds: ");
8097ec681f3Smrg   for (unsigned pred : block->logical_preds)
8107ec681f3Smrg      fprintf(output, "BB%d, ", pred);
8117ec681f3Smrg   fprintf(output, "/ linear preds: ");
8127ec681f3Smrg   for (unsigned pred : block->linear_preds)
8137ec681f3Smrg      fprintf(output, "BB%d, ", pred);
8147ec681f3Smrg   fprintf(output, "/ kind: ");
8157ec681f3Smrg   print_block_kind(block->kind, output);
8167ec681f3Smrg   fprintf(output, "*/\n");
8177ec681f3Smrg
8187ec681f3Smrg   if (flags & print_live_vars) {
8197ec681f3Smrg      fprintf(output, "\tlive out:");
8207ec681f3Smrg      for (unsigned id : live_vars.live_out[block->index])
8217ec681f3Smrg         fprintf(output, " %%%d", id);
8227ec681f3Smrg      fprintf(output, "\n");
8237ec681f3Smrg
8247ec681f3Smrg      RegisterDemand demand = block->register_demand;
8257ec681f3Smrg      fprintf(output, "\tdemand: %u vgpr, %u sgpr\n", demand.vgpr, demand.sgpr);
8267ec681f3Smrg   }
8277ec681f3Smrg
8287ec681f3Smrg   unsigned index = 0;
8297ec681f3Smrg   for (auto const& instr : block->instructions) {
8307ec681f3Smrg      fprintf(output, "\t");
8317ec681f3Smrg      if (flags & print_live_vars) {
8327ec681f3Smrg         RegisterDemand demand = live_vars.register_demand[block->index][index];
8337ec681f3Smrg         fprintf(output, "(%3u vgpr, %3u sgpr)   ", demand.vgpr, demand.sgpr);
8347ec681f3Smrg      }
8357ec681f3Smrg      if (flags & print_perf_info)
8367ec681f3Smrg         fprintf(output, "(%3u clk)   ", instr->pass_flags);
8377ec681f3Smrg
8387ec681f3Smrg      aco_print_instr(instr.get(), output, flags);
8397ec681f3Smrg      fprintf(output, "\n");
8407ec681f3Smrg      index++;
8417ec681f3Smrg   }
8427ec681f3Smrg}
8437ec681f3Smrg
8447ec681f3Smrgvoid
8457ec681f3Smrgaco_print_program(const Program* program, FILE* output, const live& live_vars, unsigned flags)
8467ec681f3Smrg{
8477ec681f3Smrg   switch (program->progress) {
8487ec681f3Smrg   case CompilationProgress::after_isel: fprintf(output, "After Instruction Selection:\n"); break;
8497ec681f3Smrg   case CompilationProgress::after_spilling:
8507ec681f3Smrg      fprintf(output, "After Spilling:\n");
8517ec681f3Smrg      flags |= print_kill;
8527ec681f3Smrg      break;
8537ec681f3Smrg   case CompilationProgress::after_ra: fprintf(output, "After RA:\n"); break;
8547ec681f3Smrg   }
8557ec681f3Smrg
8567ec681f3Smrg   print_stage(program->stage, output);
8577ec681f3Smrg
8587ec681f3Smrg   for (Block const& block : program->blocks)
8597ec681f3Smrg      aco_print_block(&block, output, flags, live_vars);
8607ec681f3Smrg
8617ec681f3Smrg   if (program->constant_data.size()) {
8627ec681f3Smrg      fprintf(output, "\n/* constant data */\n");
8637ec681f3Smrg      for (unsigned i = 0; i < program->constant_data.size(); i += 32) {
8647ec681f3Smrg         fprintf(output, "[%06d] ", i);
8657ec681f3Smrg         unsigned line_size = std::min<size_t>(program->constant_data.size() - i, 32);
8667ec681f3Smrg         for (unsigned j = 0; j < line_size; j += 4) {
8677ec681f3Smrg            unsigned size = std::min<size_t>(program->constant_data.size() - (i + j), 4);
8687ec681f3Smrg            uint32_t v = 0;
8697ec681f3Smrg            memcpy(&v, &program->constant_data[i + j], size);
8707ec681f3Smrg            fprintf(output, " %08x", v);
8717ec681f3Smrg         }
8727ec681f3Smrg         fprintf(output, "\n");
8737ec681f3Smrg      }
8747ec681f3Smrg   }
8757ec681f3Smrg
8767ec681f3Smrg   fprintf(output, "\n");
8777ec681f3Smrg}
8787ec681f3Smrg
8797ec681f3Smrgvoid
8807ec681f3Smrgaco_print_program(const Program* program, FILE* output, unsigned flags)
8817ec681f3Smrg{
8827ec681f3Smrg   aco_print_program(program, output, live(), flags);
8837ec681f3Smrg}
8847ec681f3Smrg
8857ec681f3Smrg} // namespace aco
886