17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Valve Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg *
237ec681f3Smrg */
247ec681f3Smrg
257ec681f3Smrg#include "aco_builder.h"
267ec681f3Smrg#include "aco_ir.h"
277ec681f3Smrg
287ec681f3Smrg#include <algorithm>
297ec681f3Smrg#include <array>
307ec681f3Smrg#include <bitset>
317ec681f3Smrg#include <vector>
327ec681f3Smrg
337ec681f3Smrgnamespace aco {
347ec681f3Smrgnamespace {
357ec681f3Smrg
367ec681f3Smrgconstexpr const size_t max_reg_cnt = 512;
377ec681f3Smrg
387ec681f3Smrgstruct Idx {
397ec681f3Smrg   bool operator==(const Idx& other) const { return block == other.block && instr == other.instr; }
407ec681f3Smrg   bool operator!=(const Idx& other) const { return !operator==(other); }
417ec681f3Smrg
427ec681f3Smrg   bool found() const { return block != UINT32_MAX; }
437ec681f3Smrg
447ec681f3Smrg   uint32_t block;
457ec681f3Smrg   uint32_t instr;
467ec681f3Smrg};
477ec681f3Smrg
487ec681f3SmrgIdx not_written_in_block{UINT32_MAX, 0};
497ec681f3SmrgIdx clobbered{UINT32_MAX, 1};
507ec681f3SmrgIdx const_or_undef{UINT32_MAX, 2};
517ec681f3SmrgIdx written_by_multiple_instrs{UINT32_MAX, 3};
527ec681f3Smrg
537ec681f3Smrgstruct pr_opt_ctx {
547ec681f3Smrg   Program* program;
557ec681f3Smrg   Block* current_block;
567ec681f3Smrg   uint32_t current_instr_idx;
577ec681f3Smrg   std::vector<uint16_t> uses;
587ec681f3Smrg   std::vector<std::array<Idx, max_reg_cnt>> instr_idx_by_regs;
597ec681f3Smrg
607ec681f3Smrg   void reset_block(Block* block)
617ec681f3Smrg   {
627ec681f3Smrg      current_block = block;
637ec681f3Smrg      current_instr_idx = 0;
647ec681f3Smrg
657ec681f3Smrg      if ((block->kind & block_kind_loop_header) || block->linear_preds.empty()) {
667ec681f3Smrg         std::fill(instr_idx_by_regs[block->index].begin(), instr_idx_by_regs[block->index].end(),
677ec681f3Smrg                   not_written_in_block);
687ec681f3Smrg      } else {
697ec681f3Smrg         unsigned first_pred = block->linear_preds[0];
707ec681f3Smrg         for (unsigned i = 0; i < max_reg_cnt; i++) {
717ec681f3Smrg            bool all_same = std::all_of(
727ec681f3Smrg               std::next(block->linear_preds.begin()), block->linear_preds.end(),
737ec681f3Smrg               [&](unsigned pred)
747ec681f3Smrg               { return instr_idx_by_regs[pred][i] == instr_idx_by_regs[first_pred][i]; });
757ec681f3Smrg
767ec681f3Smrg            if (all_same)
777ec681f3Smrg               instr_idx_by_regs[block->index][i] = instr_idx_by_regs[first_pred][i];
787ec681f3Smrg            else
797ec681f3Smrg               instr_idx_by_regs[block->index][i] = not_written_in_block;
807ec681f3Smrg         }
817ec681f3Smrg      }
827ec681f3Smrg   }
837ec681f3Smrg
847ec681f3Smrg   Instruction* get(Idx idx) { return program->blocks[idx.block].instructions[idx.instr].get(); }
857ec681f3Smrg};
867ec681f3Smrg
877ec681f3Smrgvoid
887ec681f3Smrgsave_reg_writes(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)
897ec681f3Smrg{
907ec681f3Smrg   for (const Definition& def : instr->definitions) {
917ec681f3Smrg      assert(def.regClass().type() != RegType::sgpr || def.physReg().reg() <= 255);
927ec681f3Smrg      assert(def.regClass().type() != RegType::vgpr || def.physReg().reg() >= 256);
937ec681f3Smrg
947ec681f3Smrg      unsigned dw_size = DIV_ROUND_UP(def.bytes(), 4u);
957ec681f3Smrg      unsigned r = def.physReg().reg();
967ec681f3Smrg      Idx idx{ctx.current_block->index, ctx.current_instr_idx};
977ec681f3Smrg
987ec681f3Smrg      if (def.regClass().is_subdword())
997ec681f3Smrg         idx = clobbered;
1007ec681f3Smrg
1017ec681f3Smrg      assert((r + dw_size) <= max_reg_cnt);
1027ec681f3Smrg      assert(def.size() == dw_size || def.regClass().is_subdword());
1037ec681f3Smrg      std::fill(ctx.instr_idx_by_regs[ctx.current_block->index].begin() + r,
1047ec681f3Smrg                ctx.instr_idx_by_regs[ctx.current_block->index].begin() + r + dw_size, idx);
1057ec681f3Smrg   }
1067ec681f3Smrg}
1077ec681f3Smrg
1087ec681f3SmrgIdx
1097ec681f3Smrglast_writer_idx(pr_opt_ctx& ctx, PhysReg physReg, RegClass rc)
1107ec681f3Smrg{
1117ec681f3Smrg   /* Verify that all of the operand's registers are written by the same instruction. */
1127ec681f3Smrg   assert(physReg.reg() < max_reg_cnt);
1137ec681f3Smrg   Idx instr_idx = ctx.instr_idx_by_regs[ctx.current_block->index][physReg.reg()];
1147ec681f3Smrg   unsigned dw_size = DIV_ROUND_UP(rc.bytes(), 4u);
1157ec681f3Smrg   unsigned r = physReg.reg();
1167ec681f3Smrg   bool all_same =
1177ec681f3Smrg      std::all_of(ctx.instr_idx_by_regs[ctx.current_block->index].begin() + r,
1187ec681f3Smrg                  ctx.instr_idx_by_regs[ctx.current_block->index].begin() + r + dw_size,
1197ec681f3Smrg                  [instr_idx](Idx i) { return i == instr_idx; });
1207ec681f3Smrg
1217ec681f3Smrg   return all_same ? instr_idx : written_by_multiple_instrs;
1227ec681f3Smrg}
1237ec681f3Smrg
1247ec681f3SmrgIdx
1257ec681f3Smrglast_writer_idx(pr_opt_ctx& ctx, const Operand& op)
1267ec681f3Smrg{
1277ec681f3Smrg   if (op.isConstant() || op.isUndefined())
1287ec681f3Smrg      return const_or_undef;
1297ec681f3Smrg
1307ec681f3Smrg   assert(op.physReg().reg() < max_reg_cnt);
1317ec681f3Smrg   Idx instr_idx = ctx.instr_idx_by_regs[ctx.current_block->index][op.physReg().reg()];
1327ec681f3Smrg
1337ec681f3Smrg#ifndef NDEBUG
1347ec681f3Smrg   /* Debug mode:  */
1357ec681f3Smrg   instr_idx = last_writer_idx(ctx, op.physReg(), op.regClass());
1367ec681f3Smrg   assert(instr_idx != written_by_multiple_instrs);
1377ec681f3Smrg#endif
1387ec681f3Smrg
1397ec681f3Smrg   return instr_idx;
1407ec681f3Smrg}
1417ec681f3Smrg
1427ec681f3Smrgbool
1437ec681f3Smrgis_clobbered_since(pr_opt_ctx& ctx, PhysReg reg, RegClass rc, const Idx& idx)
1447ec681f3Smrg{
1457ec681f3Smrg   /* If we didn't find an instruction, assume that the register is clobbered. */
1467ec681f3Smrg   if (!idx.found())
1477ec681f3Smrg      return true;
1487ec681f3Smrg
1497ec681f3Smrg   /* TODO: We currently can't keep track of subdword registers. */
1507ec681f3Smrg   if (rc.is_subdword())
1517ec681f3Smrg      return true;
1527ec681f3Smrg
1537ec681f3Smrg   unsigned begin_reg = reg.reg();
1547ec681f3Smrg   unsigned end_reg = begin_reg + rc.size();
1557ec681f3Smrg   unsigned current_block_idx = ctx.current_block->index;
1567ec681f3Smrg
1577ec681f3Smrg   for (unsigned r = begin_reg; r < end_reg; ++r) {
1587ec681f3Smrg      Idx& i = ctx.instr_idx_by_regs[current_block_idx][r];
1597ec681f3Smrg      if (i == clobbered || i == written_by_multiple_instrs)
1607ec681f3Smrg         return true;
1617ec681f3Smrg      else if (i == not_written_in_block)
1627ec681f3Smrg         continue;
1637ec681f3Smrg
1647ec681f3Smrg      assert(i.found());
1657ec681f3Smrg
1667ec681f3Smrg      if (i.block > idx.block || (i.block == idx.block && i.instr > idx.instr))
1677ec681f3Smrg         return true;
1687ec681f3Smrg   }
1697ec681f3Smrg
1707ec681f3Smrg   return false;
1717ec681f3Smrg}
1727ec681f3Smrg
1737ec681f3Smrgtemplate <typename T>
1747ec681f3Smrgbool
1757ec681f3Smrgis_clobbered_since(pr_opt_ctx& ctx, const T& t, const Idx& idx)
1767ec681f3Smrg{
1777ec681f3Smrg   return is_clobbered_since(ctx, t.physReg(), t.regClass(), idx);
1787ec681f3Smrg}
1797ec681f3Smrg
1807ec681f3Smrgvoid
1817ec681f3Smrgtry_apply_branch_vcc(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)
1827ec681f3Smrg{
1837ec681f3Smrg   /* We are looking for the following pattern:
1847ec681f3Smrg    *
1857ec681f3Smrg    * vcc = ...                      ; last_vcc_wr
1867ec681f3Smrg    * sX, scc = s_and_bXX vcc, exec  ; op0_instr
1877ec681f3Smrg    * (...vcc and exec must not be clobbered inbetween...)
1887ec681f3Smrg    * s_cbranch_XX scc               ; instr
1897ec681f3Smrg    *
1907ec681f3Smrg    * If possible, the above is optimized into:
1917ec681f3Smrg    *
1927ec681f3Smrg    * vcc = ...                      ; last_vcc_wr
1937ec681f3Smrg    * s_cbranch_XX vcc               ; instr modified to use vcc
1947ec681f3Smrg    */
1957ec681f3Smrg
1967ec681f3Smrg   /* Don't try to optimize this on GFX6-7 because SMEM may corrupt the vccz bit. */
1977ec681f3Smrg   if (ctx.program->chip_class < GFX8)
1987ec681f3Smrg      return;
1997ec681f3Smrg
2007ec681f3Smrg   if (instr->format != Format::PSEUDO_BRANCH || instr->operands.size() == 0 ||
2017ec681f3Smrg       instr->operands[0].physReg() != scc)
2027ec681f3Smrg      return;
2037ec681f3Smrg
2047ec681f3Smrg   Idx op0_instr_idx = last_writer_idx(ctx, instr->operands[0]);
2057ec681f3Smrg   Idx last_vcc_wr_idx = last_writer_idx(ctx, vcc, ctx.program->lane_mask);
2067ec681f3Smrg
2077ec681f3Smrg   /* We need to make sure:
2087ec681f3Smrg    * - the instructions that wrote the operand register and VCC are both found
2097ec681f3Smrg    * - the operand register used by the branch, and VCC were both written in the current block
2107ec681f3Smrg    * - EXEC hasn't been clobbered since the last VCC write
2117ec681f3Smrg    * - VCC hasn't been clobbered since the operand register was written
2127ec681f3Smrg    *   (ie. the last VCC writer precedes the op0 writer)
2137ec681f3Smrg    */
2147ec681f3Smrg   if (!op0_instr_idx.found() || !last_vcc_wr_idx.found() ||
2157ec681f3Smrg       op0_instr_idx.block != ctx.current_block->index ||
2167ec681f3Smrg       last_vcc_wr_idx.block != ctx.current_block->index ||
2177ec681f3Smrg       is_clobbered_since(ctx, exec, ctx.program->lane_mask, last_vcc_wr_idx) ||
2187ec681f3Smrg       is_clobbered_since(ctx, vcc, ctx.program->lane_mask, op0_instr_idx))
2197ec681f3Smrg      return;
2207ec681f3Smrg
2217ec681f3Smrg   Instruction* op0_instr = ctx.get(op0_instr_idx);
2227ec681f3Smrg   Instruction* last_vcc_wr = ctx.get(last_vcc_wr_idx);
2237ec681f3Smrg
2247ec681f3Smrg   if ((op0_instr->opcode != aco_opcode::s_and_b64 /* wave64 */ &&
2257ec681f3Smrg        op0_instr->opcode != aco_opcode::s_and_b32 /* wave32 */) ||
2267ec681f3Smrg       op0_instr->operands[0].physReg() != vcc || op0_instr->operands[1].physReg() != exec ||
2277ec681f3Smrg       !last_vcc_wr->isVOPC())
2287ec681f3Smrg      return;
2297ec681f3Smrg
2307ec681f3Smrg   assert(last_vcc_wr->definitions[0].tempId() == op0_instr->operands[0].tempId());
2317ec681f3Smrg
2327ec681f3Smrg   /* Reduce the uses of the SCC def */
2337ec681f3Smrg   ctx.uses[instr->operands[0].tempId()]--;
2347ec681f3Smrg   /* Use VCC instead of SCC in the branch */
2357ec681f3Smrg   instr->operands[0] = op0_instr->operands[0];
2367ec681f3Smrg}
2377ec681f3Smrg
2387ec681f3Smrgvoid
2397ec681f3Smrgtry_optimize_scc_nocompare(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)
2407ec681f3Smrg{
2417ec681f3Smrg   /* We are looking for the following pattern:
2427ec681f3Smrg    *
2437ec681f3Smrg    * s_bfe_u32 s0, s3, 0x40018  ; outputs SGPR and SCC if the SGPR != 0
2447ec681f3Smrg    * s_cmp_eq_i32 s0, 0         ; comparison between the SGPR and 0
2457ec681f3Smrg    * s_cbranch_scc0 BB3         ; use the result of the comparison, eg. branch or cselect
2467ec681f3Smrg    *
2477ec681f3Smrg    * If possible, the above is optimized into:
2487ec681f3Smrg    *
2497ec681f3Smrg    * s_bfe_u32 s0, s3, 0x40018  ; original instruction
2507ec681f3Smrg    * s_cbranch_scc1 BB3         ; modified to use SCC directly rather than the SGPR with comparison
2517ec681f3Smrg    *
2527ec681f3Smrg    */
2537ec681f3Smrg
2547ec681f3Smrg   if (!instr->isSALU() && !instr->isBranch())
2557ec681f3Smrg      return;
2567ec681f3Smrg
2577ec681f3Smrg   if (instr->isSOPC() &&
2587ec681f3Smrg       (instr->opcode == aco_opcode::s_cmp_eq_u32 || instr->opcode == aco_opcode::s_cmp_eq_i32 ||
2597ec681f3Smrg        instr->opcode == aco_opcode::s_cmp_lg_u32 || instr->opcode == aco_opcode::s_cmp_lg_i32 ||
2607ec681f3Smrg        instr->opcode == aco_opcode::s_cmp_eq_u64 || instr->opcode == aco_opcode::s_cmp_lg_u64) &&
2617ec681f3Smrg       (instr->operands[0].constantEquals(0) || instr->operands[1].constantEquals(0)) &&
2627ec681f3Smrg       (instr->operands[0].isTemp() || instr->operands[1].isTemp())) {
2637ec681f3Smrg      /* Make sure the constant is always in operand 1 */
2647ec681f3Smrg      if (instr->operands[0].isConstant())
2657ec681f3Smrg         std::swap(instr->operands[0], instr->operands[1]);
2667ec681f3Smrg
2677ec681f3Smrg      if (ctx.uses[instr->operands[0].tempId()] > 1)
2687ec681f3Smrg         return;
2697ec681f3Smrg
2707ec681f3Smrg      /* Make sure both SCC and Operand 0 are written by the same instruction. */
2717ec681f3Smrg      Idx wr_idx = last_writer_idx(ctx, instr->operands[0]);
2727ec681f3Smrg      Idx sccwr_idx = last_writer_idx(ctx, scc, s1);
2737ec681f3Smrg      if (!wr_idx.found() || wr_idx != sccwr_idx)
2747ec681f3Smrg         return;
2757ec681f3Smrg
2767ec681f3Smrg      Instruction* wr_instr = ctx.get(wr_idx);
2777ec681f3Smrg      if (!wr_instr->isSALU() || wr_instr->definitions.size() < 2 ||
2787ec681f3Smrg          wr_instr->definitions[1].physReg() != scc)
2797ec681f3Smrg         return;
2807ec681f3Smrg
2817ec681f3Smrg      /* Look for instructions which set SCC := (D != 0) */
2827ec681f3Smrg      switch (wr_instr->opcode) {
2837ec681f3Smrg      case aco_opcode::s_bfe_i32:
2847ec681f3Smrg      case aco_opcode::s_bfe_i64:
2857ec681f3Smrg      case aco_opcode::s_bfe_u32:
2867ec681f3Smrg      case aco_opcode::s_bfe_u64:
2877ec681f3Smrg      case aco_opcode::s_and_b32:
2887ec681f3Smrg      case aco_opcode::s_and_b64:
2897ec681f3Smrg      case aco_opcode::s_andn2_b32:
2907ec681f3Smrg      case aco_opcode::s_andn2_b64:
2917ec681f3Smrg      case aco_opcode::s_or_b32:
2927ec681f3Smrg      case aco_opcode::s_or_b64:
2937ec681f3Smrg      case aco_opcode::s_orn2_b32:
2947ec681f3Smrg      case aco_opcode::s_orn2_b64:
2957ec681f3Smrg      case aco_opcode::s_xor_b32:
2967ec681f3Smrg      case aco_opcode::s_xor_b64:
2977ec681f3Smrg      case aco_opcode::s_not_b32:
2987ec681f3Smrg      case aco_opcode::s_not_b64:
2997ec681f3Smrg      case aco_opcode::s_nor_b32:
3007ec681f3Smrg      case aco_opcode::s_nor_b64:
3017ec681f3Smrg      case aco_opcode::s_xnor_b32:
3027ec681f3Smrg      case aco_opcode::s_xnor_b64:
3037ec681f3Smrg      case aco_opcode::s_nand_b32:
3047ec681f3Smrg      case aco_opcode::s_nand_b64:
3057ec681f3Smrg      case aco_opcode::s_lshl_b32:
3067ec681f3Smrg      case aco_opcode::s_lshl_b64:
3077ec681f3Smrg      case aco_opcode::s_lshr_b32:
3087ec681f3Smrg      case aco_opcode::s_lshr_b64:
3097ec681f3Smrg      case aco_opcode::s_ashr_i32:
3107ec681f3Smrg      case aco_opcode::s_ashr_i64:
3117ec681f3Smrg      case aco_opcode::s_abs_i32:
3127ec681f3Smrg      case aco_opcode::s_absdiff_i32: break;
3137ec681f3Smrg      default: return;
3147ec681f3Smrg      }
3157ec681f3Smrg
3167ec681f3Smrg      /* Use the SCC def from wr_instr */
3177ec681f3Smrg      ctx.uses[instr->operands[0].tempId()]--;
3187ec681f3Smrg      instr->operands[0] = Operand(wr_instr->definitions[1].getTemp(), scc);
3197ec681f3Smrg      ctx.uses[instr->operands[0].tempId()]++;
3207ec681f3Smrg
3217ec681f3Smrg      /* Set the opcode and operand to 32-bit */
3227ec681f3Smrg      instr->operands[1] = Operand::zero();
3237ec681f3Smrg      instr->opcode =
3247ec681f3Smrg         (instr->opcode == aco_opcode::s_cmp_eq_u32 || instr->opcode == aco_opcode::s_cmp_eq_i32 ||
3257ec681f3Smrg          instr->opcode == aco_opcode::s_cmp_eq_u64)
3267ec681f3Smrg            ? aco_opcode::s_cmp_eq_u32
3277ec681f3Smrg            : aco_opcode::s_cmp_lg_u32;
3287ec681f3Smrg   } else if ((instr->format == Format::PSEUDO_BRANCH && instr->operands.size() == 1 &&
3297ec681f3Smrg               instr->operands[0].physReg() == scc) ||
3307ec681f3Smrg              instr->opcode == aco_opcode::s_cselect_b32) {
3317ec681f3Smrg
3327ec681f3Smrg      /* For cselect, operand 2 is the SCC condition */
3337ec681f3Smrg      unsigned scc_op_idx = 0;
3347ec681f3Smrg      if (instr->opcode == aco_opcode::s_cselect_b32) {
3357ec681f3Smrg         scc_op_idx = 2;
3367ec681f3Smrg      }
3377ec681f3Smrg
3387ec681f3Smrg      Idx wr_idx = last_writer_idx(ctx, instr->operands[scc_op_idx]);
3397ec681f3Smrg      if (!wr_idx.found())
3407ec681f3Smrg         return;
3417ec681f3Smrg
3427ec681f3Smrg      Instruction* wr_instr = ctx.get(wr_idx);
3437ec681f3Smrg
3447ec681f3Smrg      /* Check if we found the pattern above. */
3457ec681f3Smrg      if (wr_instr->opcode != aco_opcode::s_cmp_eq_u32 &&
3467ec681f3Smrg          wr_instr->opcode != aco_opcode::s_cmp_lg_u32)
3477ec681f3Smrg         return;
3487ec681f3Smrg      if (wr_instr->operands[0].physReg() != scc)
3497ec681f3Smrg         return;
3507ec681f3Smrg      if (!wr_instr->operands[1].constantEquals(0))
3517ec681f3Smrg         return;
3527ec681f3Smrg
3537ec681f3Smrg      /* The optimization can be unsafe when there are other users. */
3547ec681f3Smrg      if (ctx.uses[instr->operands[scc_op_idx].tempId()] > 1)
3557ec681f3Smrg         return;
3567ec681f3Smrg
3577ec681f3Smrg      if (wr_instr->opcode == aco_opcode::s_cmp_eq_u32) {
3587ec681f3Smrg         /* Flip the meaning of the instruction to correctly use the SCC. */
3597ec681f3Smrg         if (instr->format == Format::PSEUDO_BRANCH)
3607ec681f3Smrg            instr->opcode = instr->opcode == aco_opcode::p_cbranch_z ? aco_opcode::p_cbranch_nz
3617ec681f3Smrg                                                                     : aco_opcode::p_cbranch_z;
3627ec681f3Smrg         else if (instr->opcode == aco_opcode::s_cselect_b32)
3637ec681f3Smrg            std::swap(instr->operands[0], instr->operands[1]);
3647ec681f3Smrg         else
3657ec681f3Smrg            unreachable(
3667ec681f3Smrg               "scc_nocompare optimization is only implemented for p_cbranch and s_cselect");
3677ec681f3Smrg      }
3687ec681f3Smrg
3697ec681f3Smrg      /* Use the SCC def from the original instruction, not the comparison */
3707ec681f3Smrg      ctx.uses[instr->operands[scc_op_idx].tempId()]--;
3717ec681f3Smrg      instr->operands[scc_op_idx] = wr_instr->operands[0];
3727ec681f3Smrg   }
3737ec681f3Smrg}
3747ec681f3Smrg
3757ec681f3Smrgvoid
3767ec681f3Smrgtry_combine_dpp(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)
3777ec681f3Smrg{
3787ec681f3Smrg   /* We are looking for the following pattern:
3797ec681f3Smrg    *
3807ec681f3Smrg    * v_mov_dpp vA, vB, ...      ; move instruction with DPP
3817ec681f3Smrg    * v_xxx vC, vA, ...          ; current instr that uses the result from the move
3827ec681f3Smrg    *
3837ec681f3Smrg    * If possible, the above is optimized into:
3847ec681f3Smrg    *
3857ec681f3Smrg    * v_xxx_dpp vC, vB, ...      ; current instr modified to use DPP directly
3867ec681f3Smrg    *
3877ec681f3Smrg    */
3887ec681f3Smrg
3897ec681f3Smrg   if (!instr->isVALU() || instr->isDPP() || !can_use_DPP(instr, false))
3907ec681f3Smrg      return;
3917ec681f3Smrg
3927ec681f3Smrg   for (unsigned i = 0; i < MIN2(2, instr->operands.size()); i++) {
3937ec681f3Smrg      Idx op_instr_idx = last_writer_idx(ctx, instr->operands[i]);
3947ec681f3Smrg      if (!op_instr_idx.found())
3957ec681f3Smrg         continue;
3967ec681f3Smrg
3977ec681f3Smrg      Instruction* mov = ctx.get(op_instr_idx);
3987ec681f3Smrg      if (mov->opcode != aco_opcode::v_mov_b32 || !mov->isDPP())
3997ec681f3Smrg         continue;
4007ec681f3Smrg
4017ec681f3Smrg      /* If we aren't going to remove the v_mov_b32, we have to ensure that it doesn't overwrite
4027ec681f3Smrg       * it's own operand before we use it.
4037ec681f3Smrg       */
4047ec681f3Smrg      if (mov->definitions[0].physReg() == mov->operands[0].physReg() &&
4057ec681f3Smrg          (!mov->definitions[0].tempId() || ctx.uses[mov->definitions[0].tempId()] > 1))
4067ec681f3Smrg         continue;
4077ec681f3Smrg
4087ec681f3Smrg      /* Don't propagate DPP if the source register is overwritten since the move. */
4097ec681f3Smrg      if (is_clobbered_since(ctx, mov->operands[0], op_instr_idx))
4107ec681f3Smrg         continue;
4117ec681f3Smrg
4127ec681f3Smrg      if (i && !can_swap_operands(instr, &instr->opcode))
4137ec681f3Smrg         continue;
4147ec681f3Smrg
4157ec681f3Smrg      /* anything else doesn't make sense in SSA */
4167ec681f3Smrg      assert(mov->dpp().row_mask == 0xf && mov->dpp().bank_mask == 0xf);
4177ec681f3Smrg
4187ec681f3Smrg      if (--ctx.uses[mov->definitions[0].tempId()])
4197ec681f3Smrg         ctx.uses[mov->operands[0].tempId()]++;
4207ec681f3Smrg
4217ec681f3Smrg      convert_to_DPP(instr);
4227ec681f3Smrg
4237ec681f3Smrg      DPP_instruction* dpp = &instr->dpp();
4247ec681f3Smrg      if (i) {
4257ec681f3Smrg         std::swap(dpp->operands[0], dpp->operands[1]);
4267ec681f3Smrg         std::swap(dpp->neg[0], dpp->neg[1]);
4277ec681f3Smrg         std::swap(dpp->abs[0], dpp->abs[1]);
4287ec681f3Smrg      }
4297ec681f3Smrg      dpp->operands[0] = mov->operands[0];
4307ec681f3Smrg      dpp->dpp_ctrl = mov->dpp().dpp_ctrl;
4317ec681f3Smrg      dpp->bound_ctrl = true;
4327ec681f3Smrg      dpp->neg[0] ^= mov->dpp().neg[0] && !dpp->abs[0];
4337ec681f3Smrg      dpp->abs[0] |= mov->dpp().abs[0];
4347ec681f3Smrg      return;
4357ec681f3Smrg   }
4367ec681f3Smrg}
4377ec681f3Smrg
4387ec681f3Smrgvoid
4397ec681f3Smrgprocess_instruction(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)
4407ec681f3Smrg{
4417ec681f3Smrg   try_apply_branch_vcc(ctx, instr);
4427ec681f3Smrg
4437ec681f3Smrg   try_optimize_scc_nocompare(ctx, instr);
4447ec681f3Smrg
4457ec681f3Smrg   try_combine_dpp(ctx, instr);
4467ec681f3Smrg
4477ec681f3Smrg   if (instr)
4487ec681f3Smrg      save_reg_writes(ctx, instr);
4497ec681f3Smrg
4507ec681f3Smrg   ctx.current_instr_idx++;
4517ec681f3Smrg}
4527ec681f3Smrg
4537ec681f3Smrg} // namespace
4547ec681f3Smrg
4557ec681f3Smrgvoid
4567ec681f3Smrgoptimize_postRA(Program* program)
4577ec681f3Smrg{
4587ec681f3Smrg   pr_opt_ctx ctx;
4597ec681f3Smrg   ctx.program = program;
4607ec681f3Smrg   ctx.uses = dead_code_analysis(program);
4617ec681f3Smrg   ctx.instr_idx_by_regs.resize(program->blocks.size());
4627ec681f3Smrg
4637ec681f3Smrg   /* Forward pass
4647ec681f3Smrg    * Goes through each instruction exactly once, and can transform
4657ec681f3Smrg    * instructions or adjust the use counts of temps.
4667ec681f3Smrg    */
4677ec681f3Smrg   for (auto& block : program->blocks) {
4687ec681f3Smrg      ctx.reset_block(&block);
4697ec681f3Smrg
4707ec681f3Smrg      for (aco_ptr<Instruction>& instr : block.instructions)
4717ec681f3Smrg         process_instruction(ctx, instr);
4727ec681f3Smrg   }
4737ec681f3Smrg
4747ec681f3Smrg   /* Cleanup pass
4757ec681f3Smrg    * Gets rid of instructions which are manually deleted or
4767ec681f3Smrg    * no longer have any uses.
4777ec681f3Smrg    */
4787ec681f3Smrg   for (auto& block : program->blocks) {
4797ec681f3Smrg      auto new_end = std::remove_if(block.instructions.begin(), block.instructions.end(),
4807ec681f3Smrg                                    [&ctx](const aco_ptr<Instruction>& instr)
4817ec681f3Smrg                                    { return !instr || is_dead(ctx.uses, instr.get()); });
4827ec681f3Smrg      block.instructions.resize(new_end - block.instructions.begin());
4837ec681f3Smrg   }
4847ec681f3Smrg}
4857ec681f3Smrg
4867ec681f3Smrg} // namespace aco
487