17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2019 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 <map>
307ec681f3Smrg#include <vector>
317ec681f3Smrg
327ec681f3Smrgnamespace aco {
337ec681f3Smrg
347ec681f3Smrgenum class pred_defined : uint8_t {
357ec681f3Smrg   undef = 0,
367ec681f3Smrg   const_1 = 1,
377ec681f3Smrg   const_0 = 2,
387ec681f3Smrg   temp = 3,
397ec681f3Smrg   zero = 4, /* all disabled lanes are zero'd out */
407ec681f3Smrg};
417ec681f3SmrgMESA_DEFINE_CPP_ENUM_BITFIELD_OPERATORS(pred_defined);
427ec681f3Smrg
437ec681f3Smrgstruct ssa_state {
447ec681f3Smrg   bool checked_preds_for_uniform;
457ec681f3Smrg   bool all_preds_uniform;
467ec681f3Smrg   unsigned loop_nest_depth;
477ec681f3Smrg
487ec681f3Smrg   std::vector<pred_defined> any_pred_defined;
497ec681f3Smrg   std::vector<bool> visited;
507ec681f3Smrg   std::vector<Operand> outputs; /* the output per block */
517ec681f3Smrg};
527ec681f3Smrg
537ec681f3SmrgOperand
547ec681f3Smrgget_ssa(Program* program, unsigned block_idx, ssa_state* state, bool input)
557ec681f3Smrg{
567ec681f3Smrg   if (!input) {
577ec681f3Smrg      if (state->visited[block_idx])
587ec681f3Smrg         return state->outputs[block_idx];
597ec681f3Smrg
607ec681f3Smrg      /* otherwise, output == input */
617ec681f3Smrg      Operand output = get_ssa(program, block_idx, state, true);
627ec681f3Smrg      state->visited[block_idx] = true;
637ec681f3Smrg      state->outputs[block_idx] = output;
647ec681f3Smrg      return output;
657ec681f3Smrg   }
667ec681f3Smrg
677ec681f3Smrg   /* retrieve the Operand by checking the predecessors */
687ec681f3Smrg   if (state->any_pred_defined[block_idx] == pred_defined::undef)
697ec681f3Smrg      return Operand(program->lane_mask);
707ec681f3Smrg
717ec681f3Smrg   Block& block = program->blocks[block_idx];
727ec681f3Smrg   size_t pred = block.linear_preds.size();
737ec681f3Smrg   Operand op;
747ec681f3Smrg   if (block.loop_nest_depth < state->loop_nest_depth) {
757ec681f3Smrg      /* loop-carried value for loop exit phis */
767ec681f3Smrg      op = Operand::zero(program->lane_mask.bytes());
777ec681f3Smrg   } else if (block.loop_nest_depth > state->loop_nest_depth || pred == 1 ||
787ec681f3Smrg              block.kind & block_kind_loop_exit) {
797ec681f3Smrg      op = get_ssa(program, block.linear_preds[0], state, false);
807ec681f3Smrg   } else {
817ec681f3Smrg      assert(pred > 1);
827ec681f3Smrg      bool previously_visited = state->visited[block_idx];
837ec681f3Smrg      /* potential recursion: anchor at loop header */
847ec681f3Smrg      if (block.kind & block_kind_loop_header) {
857ec681f3Smrg         assert(!previously_visited);
867ec681f3Smrg         previously_visited = true;
877ec681f3Smrg         state->visited[block_idx] = true;
887ec681f3Smrg         state->outputs[block_idx] = Operand(Temp(program->allocateTmp(program->lane_mask)));
897ec681f3Smrg      }
907ec681f3Smrg
917ec681f3Smrg      /* collect predecessor output operands */
927ec681f3Smrg      std::vector<Operand> ops(pred);
937ec681f3Smrg      for (unsigned i = 0; i < pred; i++)
947ec681f3Smrg         ops[i] = get_ssa(program, block.linear_preds[i], state, false);
957ec681f3Smrg
967ec681f3Smrg      /* check triviality */
977ec681f3Smrg      if (std::all_of(ops.begin() + 1, ops.end(), [&](Operand same) { return same == ops[0]; }))
987ec681f3Smrg         return ops[0];
997ec681f3Smrg
1007ec681f3Smrg      /* Return if this was handled in a recursive call by a loop header phi */
1017ec681f3Smrg      if (!previously_visited && state->visited[block_idx])
1027ec681f3Smrg         return state->outputs[block_idx];
1037ec681f3Smrg
1047ec681f3Smrg      if (block.kind & block_kind_loop_header)
1057ec681f3Smrg         op = state->outputs[block_idx];
1067ec681f3Smrg      else
1077ec681f3Smrg         op = Operand(Temp(program->allocateTmp(program->lane_mask)));
1087ec681f3Smrg
1097ec681f3Smrg      /* create phi */
1107ec681f3Smrg      aco_ptr<Pseudo_instruction> phi{
1117ec681f3Smrg         create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, pred, 1)};
1127ec681f3Smrg      for (unsigned i = 0; i < pred; i++)
1137ec681f3Smrg         phi->operands[i] = ops[i];
1147ec681f3Smrg      phi->definitions[0] = Definition(op.getTemp());
1157ec681f3Smrg      block.instructions.emplace(block.instructions.begin(), std::move(phi));
1167ec681f3Smrg   }
1177ec681f3Smrg
1187ec681f3Smrg   assert(op.size() == program->lane_mask.size());
1197ec681f3Smrg   return op;
1207ec681f3Smrg}
1217ec681f3Smrg
1227ec681f3Smrgvoid
1237ec681f3Smrginsert_before_logical_end(Block* block, aco_ptr<Instruction> instr)
1247ec681f3Smrg{
1257ec681f3Smrg   auto IsLogicalEnd = [](const aco_ptr<Instruction>& inst) -> bool
1267ec681f3Smrg   { return inst->opcode == aco_opcode::p_logical_end; };
1277ec681f3Smrg   auto it = std::find_if(block->instructions.crbegin(), block->instructions.crend(), IsLogicalEnd);
1287ec681f3Smrg
1297ec681f3Smrg   if (it == block->instructions.crend()) {
1307ec681f3Smrg      assert(block->instructions.back()->isBranch());
1317ec681f3Smrg      block->instructions.insert(std::prev(block->instructions.end()), std::move(instr));
1327ec681f3Smrg   } else {
1337ec681f3Smrg      block->instructions.insert(std::prev(it.base()), std::move(instr));
1347ec681f3Smrg   }
1357ec681f3Smrg}
1367ec681f3Smrg
1377ec681f3Smrgvoid
1387ec681f3Smrgbuild_merge_code(Program* program, ssa_state* state, Block* block, Operand cur)
1397ec681f3Smrg{
1407ec681f3Smrg   unsigned block_idx = block->index;
1417ec681f3Smrg   Definition dst = Definition(state->outputs[block_idx].getTemp());
1427ec681f3Smrg   Operand prev = get_ssa(program, block_idx, state, true);
1437ec681f3Smrg   if (cur.isUndefined())
1447ec681f3Smrg      cur = Operand::zero(program->lane_mask.bytes());
1457ec681f3Smrg
1467ec681f3Smrg   Builder bld(program);
1477ec681f3Smrg   auto IsLogicalEnd = [](const aco_ptr<Instruction>& instr) -> bool
1487ec681f3Smrg   { return instr->opcode == aco_opcode::p_logical_end; };
1497ec681f3Smrg   auto it = std::find_if(block->instructions.rbegin(), block->instructions.rend(), IsLogicalEnd);
1507ec681f3Smrg   assert(it != block->instructions.rend());
1517ec681f3Smrg   bld.reset(&block->instructions, std::prev(it.base()));
1527ec681f3Smrg
1537ec681f3Smrg   pred_defined defined = state->any_pred_defined[block_idx];
1547ec681f3Smrg   if (defined == pred_defined::undef) {
1557ec681f3Smrg      return;
1567ec681f3Smrg   } else if (defined == pred_defined::const_0) {
1577ec681f3Smrg      bld.sop2(Builder::s_and, dst, bld.def(s1, scc), cur, Operand(exec, bld.lm));
1587ec681f3Smrg      return;
1597ec681f3Smrg   } else if (defined == pred_defined::const_1) {
1607ec681f3Smrg      bld.sop2(Builder::s_orn2, dst, bld.def(s1, scc), cur, Operand(exec, bld.lm));
1617ec681f3Smrg      return;
1627ec681f3Smrg   }
1637ec681f3Smrg
1647ec681f3Smrg   assert(prev.isTemp());
1657ec681f3Smrg   /* simpler sequence in case prev has only zeros in disabled lanes */
1667ec681f3Smrg   if ((defined & pred_defined::zero) == pred_defined::zero) {
1677ec681f3Smrg      if (cur.isConstant()) {
1687ec681f3Smrg         if (!cur.constantValue()) {
1697ec681f3Smrg            bld.copy(dst, prev);
1707ec681f3Smrg            return;
1717ec681f3Smrg         }
1727ec681f3Smrg         cur = Operand(exec, bld.lm);
1737ec681f3Smrg      } else {
1747ec681f3Smrg         cur =
1757ec681f3Smrg            bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cur, Operand(exec, bld.lm));
1767ec681f3Smrg      }
1777ec681f3Smrg      bld.sop2(Builder::s_or, dst, bld.def(s1, scc), prev, cur);
1787ec681f3Smrg      return;
1797ec681f3Smrg   }
1807ec681f3Smrg
1817ec681f3Smrg   if (cur.isConstant()) {
1827ec681f3Smrg      if (cur.constantValue())
1837ec681f3Smrg         bld.sop2(Builder::s_or, dst, bld.def(s1, scc), prev, Operand(exec, bld.lm));
1847ec681f3Smrg      else
1857ec681f3Smrg         bld.sop2(Builder::s_andn2, dst, bld.def(s1, scc), prev, Operand(exec, bld.lm));
1867ec681f3Smrg      return;
1877ec681f3Smrg   }
1887ec681f3Smrg   prev =
1897ec681f3Smrg      bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc), prev, Operand(exec, bld.lm));
1907ec681f3Smrg   cur = bld.sop2(Builder::s_and, bld.def(bld.lm), bld.def(s1, scc), cur, Operand(exec, bld.lm));
1917ec681f3Smrg   bld.sop2(Builder::s_or, dst, bld.def(s1, scc), prev, cur);
1927ec681f3Smrg   return;
1937ec681f3Smrg}
1947ec681f3Smrg
1957ec681f3Smrgvoid
1967ec681f3Smrginit_any_pred_defined(Program* program, ssa_state* state, Block* block, aco_ptr<Instruction>& phi)
1977ec681f3Smrg{
1987ec681f3Smrg   std::fill(state->any_pred_defined.begin(), state->any_pred_defined.end(), pred_defined::undef);
1997ec681f3Smrg   for (unsigned i = 0; i < block->logical_preds.size(); i++) {
2007ec681f3Smrg      if (phi->operands[i].isUndefined())
2017ec681f3Smrg         continue;
2027ec681f3Smrg      pred_defined defined = pred_defined::temp;
2037ec681f3Smrg      if (phi->operands[i].isConstant())
2047ec681f3Smrg         defined = phi->operands[i].constantValue() ? pred_defined::const_1 : pred_defined::const_0;
2057ec681f3Smrg      for (unsigned succ : program->blocks[block->logical_preds[i]].linear_succs)
2067ec681f3Smrg         state->any_pred_defined[succ] |= defined;
2077ec681f3Smrg   }
2087ec681f3Smrg
2097ec681f3Smrg   unsigned start = block->logical_preds[0];
2107ec681f3Smrg   unsigned end = block->index;
2117ec681f3Smrg
2127ec681f3Smrg   /* for loop exit phis, start at the loop header */
2137ec681f3Smrg   if (block->kind & block_kind_loop_exit) {
2147ec681f3Smrg      while (program->blocks[start - 1].loop_nest_depth >= state->loop_nest_depth)
2157ec681f3Smrg         start--;
2167ec681f3Smrg      /* If the loop-header has a back-edge, we need to insert a phi.
2177ec681f3Smrg       * This will contain a defined value */
2187ec681f3Smrg      if (program->blocks[start].linear_preds.size() > 1)
2197ec681f3Smrg         state->any_pred_defined[start] = pred_defined::temp;
2207ec681f3Smrg   }
2217ec681f3Smrg   /* for loop header phis, end at the loop exit */
2227ec681f3Smrg   if (block->kind & block_kind_loop_header) {
2237ec681f3Smrg      while (program->blocks[end].loop_nest_depth >= state->loop_nest_depth)
2247ec681f3Smrg         end++;
2257ec681f3Smrg      /* don't propagate the incoming value */
2267ec681f3Smrg      state->any_pred_defined[block->index] = pred_defined::undef;
2277ec681f3Smrg   }
2287ec681f3Smrg
2297ec681f3Smrg   /* add dominating zero: this allows to emit simpler merge sequences
2307ec681f3Smrg    * if we can ensure that all disabled lanes are always zero on incoming values */
2317ec681f3Smrg   // TODO: find more occasions where pred_defined::zero is beneficial (e.g. with 2+ temp merges)
2327ec681f3Smrg   if (block->kind & block_kind_loop_exit) {
2337ec681f3Smrg      /* zero the loop-carried variable */
2347ec681f3Smrg      if (program->blocks[start].linear_preds.size() > 1) {
2357ec681f3Smrg         state->any_pred_defined[start] |= pred_defined::zero;
2367ec681f3Smrg         // TODO: emit this zero explicitly
2377ec681f3Smrg         state->any_pred_defined[start - 1] = pred_defined::const_0;
2387ec681f3Smrg      }
2397ec681f3Smrg   }
2407ec681f3Smrg
2417ec681f3Smrg   for (unsigned j = start; j < end; j++) {
2427ec681f3Smrg      if (state->any_pred_defined[j] == pred_defined::undef)
2437ec681f3Smrg         continue;
2447ec681f3Smrg      for (unsigned succ : program->blocks[j].linear_succs)
2457ec681f3Smrg         state->any_pred_defined[succ] |= state->any_pred_defined[j];
2467ec681f3Smrg   }
2477ec681f3Smrg
2487ec681f3Smrg   state->any_pred_defined[block->index] = pred_defined::undef;
2497ec681f3Smrg}
2507ec681f3Smrg
2517ec681f3Smrgvoid
2527ec681f3Smrglower_divergent_bool_phi(Program* program, ssa_state* state, Block* block,
2537ec681f3Smrg                         aco_ptr<Instruction>& phi)
2547ec681f3Smrg{
2557ec681f3Smrg   Builder bld(program);
2567ec681f3Smrg
2577ec681f3Smrg   if (!state->checked_preds_for_uniform) {
2587ec681f3Smrg      state->all_preds_uniform = !(block->kind & block_kind_merge) &&
2597ec681f3Smrg                                 block->linear_preds.size() == block->logical_preds.size();
2607ec681f3Smrg      for (unsigned pred : block->logical_preds)
2617ec681f3Smrg         state->all_preds_uniform =
2627ec681f3Smrg            state->all_preds_uniform && (program->blocks[pred].kind & block_kind_uniform);
2637ec681f3Smrg      state->checked_preds_for_uniform = true;
2647ec681f3Smrg   }
2657ec681f3Smrg
2667ec681f3Smrg   if (state->all_preds_uniform) {
2677ec681f3Smrg      phi->opcode = aco_opcode::p_linear_phi;
2687ec681f3Smrg      return;
2697ec681f3Smrg   }
2707ec681f3Smrg
2717ec681f3Smrg   /* do this here to avoid resizing in case of no boolean phis */
2727ec681f3Smrg   state->visited.resize(program->blocks.size());
2737ec681f3Smrg   state->outputs.resize(program->blocks.size());
2747ec681f3Smrg   state->any_pred_defined.resize(program->blocks.size());
2757ec681f3Smrg   state->loop_nest_depth = block->loop_nest_depth;
2767ec681f3Smrg   if (block->kind & block_kind_loop_exit)
2777ec681f3Smrg      state->loop_nest_depth += 1;
2787ec681f3Smrg   std::fill(state->visited.begin(), state->visited.end(), false);
2797ec681f3Smrg   init_any_pred_defined(program, state, block, phi);
2807ec681f3Smrg
2817ec681f3Smrg   for (unsigned i = 0; i < phi->operands.size(); i++) {
2827ec681f3Smrg      unsigned pred = block->logical_preds[i];
2837ec681f3Smrg      if (state->any_pred_defined[pred] != pred_defined::undef)
2847ec681f3Smrg         state->outputs[pred] = Operand(bld.tmp(bld.lm));
2857ec681f3Smrg      else
2867ec681f3Smrg         state->outputs[pred] = phi->operands[i];
2877ec681f3Smrg      assert(state->outputs[pred].size() == bld.lm.size());
2887ec681f3Smrg      state->visited[pred] = true;
2897ec681f3Smrg   }
2907ec681f3Smrg
2917ec681f3Smrg   for (unsigned i = 0; i < phi->operands.size(); i++)
2927ec681f3Smrg      build_merge_code(program, state, &program->blocks[block->logical_preds[i]], phi->operands[i]);
2937ec681f3Smrg
2947ec681f3Smrg   unsigned num_preds = block->linear_preds.size();
2957ec681f3Smrg   if (phi->operands.size() != num_preds) {
2967ec681f3Smrg      Pseudo_instruction* new_phi{create_instruction<Pseudo_instruction>(
2977ec681f3Smrg         aco_opcode::p_linear_phi, Format::PSEUDO, num_preds, 1)};
2987ec681f3Smrg      new_phi->definitions[0] = phi->definitions[0];
2997ec681f3Smrg      phi.reset(new_phi);
3007ec681f3Smrg   } else {
3017ec681f3Smrg      phi->opcode = aco_opcode::p_linear_phi;
3027ec681f3Smrg   }
3037ec681f3Smrg   assert(phi->operands.size() == num_preds);
3047ec681f3Smrg
3057ec681f3Smrg   for (unsigned i = 0; i < num_preds; i++)
3067ec681f3Smrg      phi->operands[i] = get_ssa(program, block->linear_preds[i], state, false);
3077ec681f3Smrg
3087ec681f3Smrg   return;
3097ec681f3Smrg}
3107ec681f3Smrg
3117ec681f3Smrgvoid
3127ec681f3Smrglower_subdword_phis(Program* program, Block* block, aco_ptr<Instruction>& phi)
3137ec681f3Smrg{
3147ec681f3Smrg   Builder bld(program);
3157ec681f3Smrg   for (unsigned i = 0; i < phi->operands.size(); i++) {
3167ec681f3Smrg      if (phi->operands[i].isUndefined())
3177ec681f3Smrg         continue;
3187ec681f3Smrg      if (phi->operands[i].regClass() == phi->definitions[0].regClass())
3197ec681f3Smrg         continue;
3207ec681f3Smrg
3217ec681f3Smrg      assert(phi->operands[i].isTemp());
3227ec681f3Smrg      Block* pred = &program->blocks[block->logical_preds[i]];
3237ec681f3Smrg      Temp phi_src = phi->operands[i].getTemp();
3247ec681f3Smrg
3257ec681f3Smrg      assert(phi_src.regClass().type() == RegType::sgpr);
3267ec681f3Smrg      Temp tmp = bld.tmp(RegClass(RegType::vgpr, phi_src.size()));
3277ec681f3Smrg      insert_before_logical_end(pred, bld.copy(Definition(tmp), phi_src).get_ptr());
3287ec681f3Smrg      Temp new_phi_src = bld.tmp(phi->definitions[0].regClass());
3297ec681f3Smrg      insert_before_logical_end(pred, bld.pseudo(aco_opcode::p_extract_vector,
3307ec681f3Smrg                                                 Definition(new_phi_src), tmp, Operand::zero())
3317ec681f3Smrg                                         .get_ptr());
3327ec681f3Smrg
3337ec681f3Smrg      phi->operands[i].setTemp(new_phi_src);
3347ec681f3Smrg   }
3357ec681f3Smrg   return;
3367ec681f3Smrg}
3377ec681f3Smrg
3387ec681f3Smrgvoid
3397ec681f3Smrglower_phis(Program* program)
3407ec681f3Smrg{
3417ec681f3Smrg   ssa_state state;
3427ec681f3Smrg
3437ec681f3Smrg   for (Block& block : program->blocks) {
3447ec681f3Smrg      state.checked_preds_for_uniform = false;
3457ec681f3Smrg      for (aco_ptr<Instruction>& phi : block.instructions) {
3467ec681f3Smrg         if (phi->opcode == aco_opcode::p_phi) {
3477ec681f3Smrg            assert(program->wave_size == 64 ? phi->definitions[0].regClass() != s1
3487ec681f3Smrg                                            : phi->definitions[0].regClass() != s2);
3497ec681f3Smrg            if (phi->definitions[0].regClass() == program->lane_mask)
3507ec681f3Smrg               lower_divergent_bool_phi(program, &state, &block, phi);
3517ec681f3Smrg            else if (phi->definitions[0].regClass().is_subdword())
3527ec681f3Smrg               lower_subdword_phis(program, &block, phi);
3537ec681f3Smrg         } else if (!is_phi(phi)) {
3547ec681f3Smrg            break;
3557ec681f3Smrg         }
3567ec681f3Smrg      }
3577ec681f3Smrg   }
3587ec681f3Smrg}
3597ec681f3Smrg
3607ec681f3Smrg} // namespace aco
361