17ec681f3Smrg
27ec681f3Smrgtemplate = """\
37ec681f3Smrg/*
47ec681f3Smrg * Copyright (c) 2019 Valve Corporation
57ec681f3Smrg *
67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
87ec681f3Smrg * to deal in the Software without restriction, including without limitation
97ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
107ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
117ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
127ec681f3Smrg *
137ec681f3Smrg * The above copyright notice and this permission notice (including the next
147ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
157ec681f3Smrg * Software.
167ec681f3Smrg *
177ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
187ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
197ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
207ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
217ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
227ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
237ec681f3Smrg * IN THE SOFTWARE.
247ec681f3Smrg *
257ec681f3Smrg * This file was generated by aco_builder_h.py
267ec681f3Smrg */
277ec681f3Smrg
287ec681f3Smrg#ifndef _ACO_BUILDER_
297ec681f3Smrg#define _ACO_BUILDER_
307ec681f3Smrg
317ec681f3Smrg#include "aco_ir.h"
327ec681f3Smrg
337ec681f3Smrgnamespace aco {
347ec681f3Smrgenum dpp_ctrl {
357ec681f3Smrg    _dpp_quad_perm = 0x000,
367ec681f3Smrg    _dpp_row_sl = 0x100,
377ec681f3Smrg    _dpp_row_sr = 0x110,
387ec681f3Smrg    _dpp_row_rr = 0x120,
397ec681f3Smrg    dpp_wf_sl1 = 0x130,
407ec681f3Smrg    dpp_wf_rl1 = 0x134,
417ec681f3Smrg    dpp_wf_sr1 = 0x138,
427ec681f3Smrg    dpp_wf_rr1 = 0x13C,
437ec681f3Smrg    dpp_row_mirror = 0x140,
447ec681f3Smrg    dpp_row_half_mirror = 0x141,
457ec681f3Smrg    dpp_row_bcast15 = 0x142,
467ec681f3Smrg    dpp_row_bcast31 = 0x143
477ec681f3Smrg};
487ec681f3Smrg
497ec681f3Smrginline dpp_ctrl
507ec681f3Smrgdpp_quad_perm(unsigned lane0, unsigned lane1, unsigned lane2, unsigned lane3)
517ec681f3Smrg{
527ec681f3Smrg    assert(lane0 < 4 && lane1 < 4 && lane2 < 4 && lane3 < 4);
537ec681f3Smrg    return (dpp_ctrl)(lane0 | (lane1 << 2) | (lane2 << 4) | (lane3 << 6));
547ec681f3Smrg}
557ec681f3Smrg
567ec681f3Smrginline dpp_ctrl
577ec681f3Smrgdpp_row_sl(unsigned amount)
587ec681f3Smrg{
597ec681f3Smrg    assert(amount > 0 && amount < 16);
607ec681f3Smrg    return (dpp_ctrl)(((unsigned) _dpp_row_sl) | amount);
617ec681f3Smrg}
627ec681f3Smrg
637ec681f3Smrginline dpp_ctrl
647ec681f3Smrgdpp_row_sr(unsigned amount)
657ec681f3Smrg{
667ec681f3Smrg    assert(amount > 0 && amount < 16);
677ec681f3Smrg    return (dpp_ctrl)(((unsigned) _dpp_row_sr) | amount);
687ec681f3Smrg}
697ec681f3Smrg
707ec681f3Smrginline dpp_ctrl
717ec681f3Smrgdpp_row_rr(unsigned amount)
727ec681f3Smrg{
737ec681f3Smrg    assert(amount > 0 && amount < 16);
747ec681f3Smrg    return (dpp_ctrl)(((unsigned) _dpp_row_rr) | amount);
757ec681f3Smrg}
767ec681f3Smrg
777ec681f3Smrginline unsigned
787ec681f3Smrgds_pattern_bitmode(unsigned and_mask, unsigned or_mask, unsigned xor_mask)
797ec681f3Smrg{
807ec681f3Smrg    assert(and_mask < 32 && or_mask < 32 && xor_mask < 32);
817ec681f3Smrg    return and_mask | (or_mask << 5) | (xor_mask << 10);
827ec681f3Smrg}
837ec681f3Smrg
847ec681f3Smrgaco_ptr<Instruction> create_s_mov(Definition dst, Operand src);
857ec681f3Smrg
867ec681f3Smrgenum sendmsg {
877ec681f3Smrg   sendmsg_none = 0,
887ec681f3Smrg   _sendmsg_gs = 2,
897ec681f3Smrg   _sendmsg_gs_done = 3,
907ec681f3Smrg   sendmsg_save_wave = 4,
917ec681f3Smrg   sendmsg_stall_wave_gen = 5,
927ec681f3Smrg   sendmsg_halt_waves = 6,
937ec681f3Smrg   sendmsg_ordered_ps_done = 7,
947ec681f3Smrg   sendmsg_early_prim_dealloc = 8,
957ec681f3Smrg   sendmsg_gs_alloc_req = 9,
967ec681f3Smrg   sendmsg_id_mask = 0xf,
977ec681f3Smrg};
987ec681f3Smrg
997ec681f3Smrginline sendmsg
1007ec681f3Smrgsendmsg_gs(bool cut, bool emit, unsigned stream)
1017ec681f3Smrg{
1027ec681f3Smrg    assert(stream < 4);
1037ec681f3Smrg    return (sendmsg)((unsigned)_sendmsg_gs | (cut << 4) | (emit << 5) | (stream << 8));
1047ec681f3Smrg}
1057ec681f3Smrg
1067ec681f3Smrginline sendmsg
1077ec681f3Smrgsendmsg_gs_done(bool cut, bool emit, unsigned stream)
1087ec681f3Smrg{
1097ec681f3Smrg    assert(stream < 4);
1107ec681f3Smrg    return (sendmsg)((unsigned)_sendmsg_gs_done | (cut << 4) | (emit << 5) | (stream << 8));
1117ec681f3Smrg}
1127ec681f3Smrg
1137ec681f3Smrgclass Builder {
1147ec681f3Smrgpublic:
1157ec681f3Smrg   struct Result {
1167ec681f3Smrg      Instruction *instr;
1177ec681f3Smrg
1187ec681f3Smrg      Result(Instruction *instr_) : instr(instr_) {}
1197ec681f3Smrg
1207ec681f3Smrg      operator Instruction *() const {
1217ec681f3Smrg         return instr;
1227ec681f3Smrg      }
1237ec681f3Smrg
1247ec681f3Smrg      operator Temp() const {
1257ec681f3Smrg         return instr->definitions[0].getTemp();
1267ec681f3Smrg      }
1277ec681f3Smrg
1287ec681f3Smrg      operator Operand() const {
1297ec681f3Smrg         return Operand((Temp)*this);
1307ec681f3Smrg      }
1317ec681f3Smrg
1327ec681f3Smrg      Definition& def(unsigned index) const {
1337ec681f3Smrg         return instr->definitions[index];
1347ec681f3Smrg      }
1357ec681f3Smrg
1367ec681f3Smrg      aco_ptr<Instruction> get_ptr() const {
1377ec681f3Smrg        return aco_ptr<Instruction>(instr);
1387ec681f3Smrg      }
1397ec681f3Smrg   };
1407ec681f3Smrg
1417ec681f3Smrg   struct Op {
1427ec681f3Smrg      Operand op;
1437ec681f3Smrg      Op(Temp tmp) : op(tmp) {}
1447ec681f3Smrg      Op(Operand op_) : op(op_) {}
1457ec681f3Smrg      Op(Result res) : op((Temp)res) {}
1467ec681f3Smrg   };
1477ec681f3Smrg
1487ec681f3Smrg   enum WaveSpecificOpcode {
1497ec681f3Smrg      s_cselect = (unsigned) aco_opcode::s_cselect_b64,
1507ec681f3Smrg      s_cmp_lg = (unsigned) aco_opcode::s_cmp_lg_u64,
1517ec681f3Smrg      s_and = (unsigned) aco_opcode::s_and_b64,
1527ec681f3Smrg      s_andn2 = (unsigned) aco_opcode::s_andn2_b64,
1537ec681f3Smrg      s_or = (unsigned) aco_opcode::s_or_b64,
1547ec681f3Smrg      s_orn2 = (unsigned) aco_opcode::s_orn2_b64,
1557ec681f3Smrg      s_not = (unsigned) aco_opcode::s_not_b64,
1567ec681f3Smrg      s_mov = (unsigned) aco_opcode::s_mov_b64,
1577ec681f3Smrg      s_wqm = (unsigned) aco_opcode::s_wqm_b64,
1587ec681f3Smrg      s_and_saveexec = (unsigned) aco_opcode::s_and_saveexec_b64,
1597ec681f3Smrg      s_or_saveexec = (unsigned) aco_opcode::s_or_saveexec_b64,
1607ec681f3Smrg      s_xnor = (unsigned) aco_opcode::s_xnor_b64,
1617ec681f3Smrg      s_xor = (unsigned) aco_opcode::s_xor_b64,
1627ec681f3Smrg      s_bcnt1_i32 = (unsigned) aco_opcode::s_bcnt1_i32_b64,
1637ec681f3Smrg      s_bitcmp1 = (unsigned) aco_opcode::s_bitcmp1_b64,
1647ec681f3Smrg      s_ff1_i32 = (unsigned) aco_opcode::s_ff1_i32_b64,
1657ec681f3Smrg      s_flbit_i32 = (unsigned) aco_opcode::s_flbit_i32_b64,
1667ec681f3Smrg      s_lshl = (unsigned) aco_opcode::s_lshl_b64,
1677ec681f3Smrg   };
1687ec681f3Smrg
1697ec681f3Smrg   Program *program;
1707ec681f3Smrg   bool use_iterator;
1717ec681f3Smrg   bool start; // only when use_iterator == false
1727ec681f3Smrg   RegClass lm;
1737ec681f3Smrg
1747ec681f3Smrg   std::vector<aco_ptr<Instruction>> *instructions;
1757ec681f3Smrg   std::vector<aco_ptr<Instruction>>::iterator it;
1767ec681f3Smrg   bool is_precise = false;
1777ec681f3Smrg   bool is_nuw = false;
1787ec681f3Smrg
1797ec681f3Smrg   Builder(Program *pgm) : program(pgm), use_iterator(false), start(false), lm(pgm ? pgm->lane_mask : s2), instructions(NULL) {}
1807ec681f3Smrg   Builder(Program *pgm, Block *block) : program(pgm), use_iterator(false), start(false), lm(pgm ? pgm->lane_mask : s2), instructions(&block->instructions) {}
1817ec681f3Smrg   Builder(Program *pgm, std::vector<aco_ptr<Instruction>> *instrs) : program(pgm), use_iterator(false), start(false), lm(pgm ? pgm->lane_mask : s2), instructions(instrs) {}
1827ec681f3Smrg
1837ec681f3Smrg   Builder precise() const {
1847ec681f3Smrg      Builder res = *this;
1857ec681f3Smrg      res.is_precise = true;
1867ec681f3Smrg      return res;
1877ec681f3Smrg   };
1887ec681f3Smrg
1897ec681f3Smrg   Builder nuw() const {
1907ec681f3Smrg      Builder res = *this;
1917ec681f3Smrg      res.is_nuw = true;
1927ec681f3Smrg      return res;
1937ec681f3Smrg   }
1947ec681f3Smrg
1957ec681f3Smrg   void moveEnd(Block *block) {
1967ec681f3Smrg      instructions = &block->instructions;
1977ec681f3Smrg   }
1987ec681f3Smrg
1997ec681f3Smrg   void reset() {
2007ec681f3Smrg      use_iterator = false;
2017ec681f3Smrg      start = false;
2027ec681f3Smrg      instructions = NULL;
2037ec681f3Smrg   }
2047ec681f3Smrg
2057ec681f3Smrg   void reset(Block *block) {
2067ec681f3Smrg      use_iterator = false;
2077ec681f3Smrg      start = false;
2087ec681f3Smrg      instructions = &block->instructions;
2097ec681f3Smrg   }
2107ec681f3Smrg
2117ec681f3Smrg   void reset(std::vector<aco_ptr<Instruction>> *instrs) {
2127ec681f3Smrg      use_iterator = false;
2137ec681f3Smrg      start = false;
2147ec681f3Smrg      instructions = instrs;
2157ec681f3Smrg   }
2167ec681f3Smrg
2177ec681f3Smrg   void reset(std::vector<aco_ptr<Instruction>> *instrs, std::vector<aco_ptr<Instruction>>::iterator instr_it) {
2187ec681f3Smrg      use_iterator = true;
2197ec681f3Smrg      start = false;
2207ec681f3Smrg      instructions = instrs;
2217ec681f3Smrg      it = instr_it;
2227ec681f3Smrg   }
2237ec681f3Smrg
2247ec681f3Smrg   Result insert(aco_ptr<Instruction> instr) {
2257ec681f3Smrg      Instruction *instr_ptr = instr.get();
2267ec681f3Smrg      if (instructions) {
2277ec681f3Smrg         if (use_iterator) {
2287ec681f3Smrg            it = instructions->emplace(it, std::move(instr));
2297ec681f3Smrg            it = std::next(it);
2307ec681f3Smrg         } else if (!start) {
2317ec681f3Smrg            instructions->emplace_back(std::move(instr));
2327ec681f3Smrg         } else {
2337ec681f3Smrg            instructions->emplace(instructions->begin(), std::move(instr));
2347ec681f3Smrg         }
2357ec681f3Smrg      }
2367ec681f3Smrg      return Result(instr_ptr);
2377ec681f3Smrg   }
2387ec681f3Smrg
2397ec681f3Smrg   Result insert(Instruction* instr) {
2407ec681f3Smrg      if (instructions) {
2417ec681f3Smrg         if (use_iterator) {
2427ec681f3Smrg            it = instructions->emplace(it, aco_ptr<Instruction>(instr));
2437ec681f3Smrg            it = std::next(it);
2447ec681f3Smrg         } else if (!start) {
2457ec681f3Smrg            instructions->emplace_back(aco_ptr<Instruction>(instr));
2467ec681f3Smrg         } else {
2477ec681f3Smrg            instructions->emplace(instructions->begin(), aco_ptr<Instruction>(instr));
2487ec681f3Smrg         }
2497ec681f3Smrg      }
2507ec681f3Smrg      return Result(instr);
2517ec681f3Smrg   }
2527ec681f3Smrg
2537ec681f3Smrg   Temp tmp(RegClass rc) {
2547ec681f3Smrg      return program->allocateTmp(rc);
2557ec681f3Smrg   }
2567ec681f3Smrg
2577ec681f3Smrg   Temp tmp(RegType type, unsigned size) {
2587ec681f3Smrg      return tmp(RegClass(type, size));
2597ec681f3Smrg   }
2607ec681f3Smrg
2617ec681f3Smrg   Definition def(RegClass rc) {
2627ec681f3Smrg      return Definition(program->allocateTmp(rc));
2637ec681f3Smrg   }
2647ec681f3Smrg
2657ec681f3Smrg   Definition def(RegType type, unsigned size) {
2667ec681f3Smrg      return def(RegClass(type, size));
2677ec681f3Smrg   }
2687ec681f3Smrg
2697ec681f3Smrg   Definition def(RegClass rc, PhysReg reg) {
2707ec681f3Smrg      return Definition(program->allocateId(rc), reg, rc);
2717ec681f3Smrg   }
2727ec681f3Smrg
2737ec681f3Smrg   inline aco_opcode w64or32(WaveSpecificOpcode opcode) const {
2747ec681f3Smrg      if (program->wave_size == 64)
2757ec681f3Smrg         return (aco_opcode) opcode;
2767ec681f3Smrg
2777ec681f3Smrg      switch (opcode) {
2787ec681f3Smrg      case s_cselect:
2797ec681f3Smrg         return aco_opcode::s_cselect_b32;
2807ec681f3Smrg      case s_cmp_lg:
2817ec681f3Smrg         return aco_opcode::s_cmp_lg_u32;
2827ec681f3Smrg      case s_and:
2837ec681f3Smrg         return aco_opcode::s_and_b32;
2847ec681f3Smrg      case s_andn2:
2857ec681f3Smrg         return aco_opcode::s_andn2_b32;
2867ec681f3Smrg      case s_or:
2877ec681f3Smrg         return aco_opcode::s_or_b32;
2887ec681f3Smrg      case s_orn2:
2897ec681f3Smrg         return aco_opcode::s_orn2_b32;
2907ec681f3Smrg      case s_not:
2917ec681f3Smrg         return aco_opcode::s_not_b32;
2927ec681f3Smrg      case s_mov:
2937ec681f3Smrg         return aco_opcode::s_mov_b32;
2947ec681f3Smrg      case s_wqm:
2957ec681f3Smrg         return aco_opcode::s_wqm_b32;
2967ec681f3Smrg      case s_and_saveexec:
2977ec681f3Smrg         return aco_opcode::s_and_saveexec_b32;
2987ec681f3Smrg      case s_or_saveexec:
2997ec681f3Smrg         return aco_opcode::s_or_saveexec_b32;
3007ec681f3Smrg      case s_xnor:
3017ec681f3Smrg         return aco_opcode::s_xnor_b32;
3027ec681f3Smrg      case s_xor:
3037ec681f3Smrg         return aco_opcode::s_xor_b32;
3047ec681f3Smrg      case s_bcnt1_i32:
3057ec681f3Smrg         return aco_opcode::s_bcnt1_i32_b32;
3067ec681f3Smrg      case s_bitcmp1:
3077ec681f3Smrg         return aco_opcode::s_bitcmp1_b32;
3087ec681f3Smrg      case s_ff1_i32:
3097ec681f3Smrg         return aco_opcode::s_ff1_i32_b32;
3107ec681f3Smrg      case s_flbit_i32:
3117ec681f3Smrg         return aco_opcode::s_flbit_i32_b32;
3127ec681f3Smrg      case s_lshl:
3137ec681f3Smrg         return aco_opcode::s_lshl_b32;
3147ec681f3Smrg      default:
3157ec681f3Smrg         unreachable("Unsupported wave specific opcode.");
3167ec681f3Smrg      }
3177ec681f3Smrg   }
3187ec681f3Smrg
3197ec681f3Smrg% for fixed in ['m0', 'vcc', 'exec', 'scc']:
3207ec681f3Smrg   Operand ${fixed}(Temp tmp) {
3217ec681f3Smrg       % if fixed == 'vcc' or fixed == 'exec':
3227ec681f3Smrg          //vcc_hi and exec_hi can still be used in wave32
3237ec681f3Smrg          assert(tmp.type() == RegType::sgpr && tmp.bytes() <= 8);
3247ec681f3Smrg       % endif
3257ec681f3Smrg       Operand op(tmp);
3267ec681f3Smrg       op.setFixed(aco::${fixed});
3277ec681f3Smrg       return op;
3287ec681f3Smrg   }
3297ec681f3Smrg
3307ec681f3Smrg   Definition ${fixed}(Definition def) {
3317ec681f3Smrg       % if fixed == 'vcc' or fixed == 'exec':
3327ec681f3Smrg          //vcc_hi and exec_hi can still be used in wave32
3337ec681f3Smrg          assert(def.regClass().type() == RegType::sgpr && def.bytes() <= 8);
3347ec681f3Smrg       % endif
3357ec681f3Smrg       def.setFixed(aco::${fixed});
3367ec681f3Smrg       return def;
3377ec681f3Smrg   }
3387ec681f3Smrg
3397ec681f3Smrg   Definition hint_${fixed}(Definition def) {
3407ec681f3Smrg       % if fixed == 'vcc' or fixed == 'exec':
3417ec681f3Smrg          //vcc_hi and exec_hi can still be used in wave32
3427ec681f3Smrg          assert(def.regClass().type() == RegType::sgpr && def.bytes() <= 8);
3437ec681f3Smrg       % endif
3447ec681f3Smrg       def.setHint(aco::${fixed});
3457ec681f3Smrg       return def;
3467ec681f3Smrg   }
3477ec681f3Smrg
3487ec681f3Smrg   Definition hint_${fixed}(RegClass rc) {
3497ec681f3Smrg       return hint_${fixed}(def(rc));
3507ec681f3Smrg   }
3517ec681f3Smrg
3527ec681f3Smrg% endfor
3537ec681f3Smrg
3547ec681f3Smrg   Operand set16bit(Operand op) {
3557ec681f3Smrg       op.set16bit(true);
3567ec681f3Smrg       return op;
3577ec681f3Smrg   }
3587ec681f3Smrg
3597ec681f3Smrg   Operand set24bit(Operand op) {
3607ec681f3Smrg       op.set24bit(true);
3617ec681f3Smrg       return op;
3627ec681f3Smrg   }
3637ec681f3Smrg
3647ec681f3Smrg   /* hand-written helpers */
3657ec681f3Smrg   Temp as_uniform(Op op)
3667ec681f3Smrg   {
3677ec681f3Smrg      assert(op.op.isTemp());
3687ec681f3Smrg      if (op.op.getTemp().type() == RegType::vgpr)
3697ec681f3Smrg         return pseudo(aco_opcode::p_as_uniform, def(RegType::sgpr, op.op.size()), op);
3707ec681f3Smrg      else
3717ec681f3Smrg         return op.op.getTemp();
3727ec681f3Smrg   }
3737ec681f3Smrg
3747ec681f3Smrg   Result v_mul_imm(Definition dst, Temp tmp, uint32_t imm, bool bits24=false)
3757ec681f3Smrg   {
3767ec681f3Smrg      assert(tmp.type() == RegType::vgpr);
3777ec681f3Smrg      bool has_lshl_add = program->chip_class >= GFX9;
3787ec681f3Smrg      /* v_mul_lo_u32 has 1.6x the latency of most VALU on GFX10 (8 vs 5 cycles),
3797ec681f3Smrg       * compared to 4x the latency on <GFX10. */
3807ec681f3Smrg      unsigned mul_cost = program->chip_class >= GFX10 ? 1 : (4 + Operand::c32(imm).isLiteral());
3817ec681f3Smrg      if (imm == 0) {
3827ec681f3Smrg         return copy(dst, Operand::zero());
3837ec681f3Smrg      } else if (imm == 1) {
3847ec681f3Smrg         return copy(dst, Operand(tmp));
3857ec681f3Smrg      } else if (util_is_power_of_two_or_zero(imm)) {
3867ec681f3Smrg         return vop2(aco_opcode::v_lshlrev_b32, dst, Operand::c32(ffs(imm) - 1u), tmp);
3877ec681f3Smrg      } else if (bits24) {
3887ec681f3Smrg        return vop2(aco_opcode::v_mul_u32_u24, dst, Operand::c32(imm), tmp);
3897ec681f3Smrg      } else if (util_is_power_of_two_nonzero(imm - 1u)) {
3907ec681f3Smrg         return vadd32(dst, vop2(aco_opcode::v_lshlrev_b32, def(v1), Operand::c32(ffs(imm - 1u) - 1u), tmp), tmp);
3917ec681f3Smrg      } else if (mul_cost > 2 && util_is_power_of_two_nonzero(imm + 1u)) {
3927ec681f3Smrg         return vsub32(dst, vop2(aco_opcode::v_lshlrev_b32, def(v1), Operand::c32(ffs(imm + 1u) - 1u), tmp), tmp);
3937ec681f3Smrg      }
3947ec681f3Smrg
3957ec681f3Smrg      unsigned instrs_required = util_bitcount(imm);
3967ec681f3Smrg      if (!has_lshl_add) {
3977ec681f3Smrg         instrs_required = util_bitcount(imm) - (imm & 0x1); /* shifts */
3987ec681f3Smrg         instrs_required += util_bitcount(imm) - 1; /* additions */
3997ec681f3Smrg      }
4007ec681f3Smrg      if (instrs_required < mul_cost) {
4017ec681f3Smrg         Result res(NULL);
4027ec681f3Smrg         Temp cur;
4037ec681f3Smrg         while (imm) {
4047ec681f3Smrg            unsigned shift = u_bit_scan(&imm);
4057ec681f3Smrg            Definition tmp_dst = imm ? def(v1) : dst;
4067ec681f3Smrg
4077ec681f3Smrg            if (shift && cur.id())
4087ec681f3Smrg               res = vadd32(Definition(tmp_dst), vop2(aco_opcode::v_lshlrev_b32, def(v1), Operand::c32(shift), tmp), cur);
4097ec681f3Smrg            else if (shift)
4107ec681f3Smrg               res = vop2(aco_opcode::v_lshlrev_b32, Definition(tmp_dst), Operand::c32(shift), tmp);
4117ec681f3Smrg            else if (cur.id())
4127ec681f3Smrg               res = vadd32(Definition(tmp_dst), tmp, cur);
4137ec681f3Smrg            else
4147ec681f3Smrg               tmp_dst = Definition(tmp);
4157ec681f3Smrg
4167ec681f3Smrg            cur = tmp_dst.getTemp();
4177ec681f3Smrg         }
4187ec681f3Smrg         return res;
4197ec681f3Smrg      }
4207ec681f3Smrg
4217ec681f3Smrg      Temp imm_tmp = copy(def(s1), Operand::c32(imm));
4227ec681f3Smrg      return vop3(aco_opcode::v_mul_lo_u32, dst, imm_tmp, tmp);
4237ec681f3Smrg   }
4247ec681f3Smrg
4257ec681f3Smrg   Result v_mul24_imm(Definition dst, Temp tmp, uint32_t imm)
4267ec681f3Smrg   {
4277ec681f3Smrg      return v_mul_imm(dst, tmp, imm, true);
4287ec681f3Smrg   }
4297ec681f3Smrg
4307ec681f3Smrg   Result copy(Definition dst, Op op) {
4317ec681f3Smrg      return pseudo(aco_opcode::p_parallelcopy, dst, op);
4327ec681f3Smrg   }
4337ec681f3Smrg
4347ec681f3Smrg   Result vadd32(Definition dst, Op a, Op b, bool carry_out=false, Op carry_in=Op(Operand(s2)), bool post_ra=false) {
4357ec681f3Smrg      if (b.op.isConstant() || b.op.regClass().type() != RegType::vgpr)
4367ec681f3Smrg         std::swap(a, b);
4377ec681f3Smrg      if (!post_ra && (!b.op.hasRegClass() || b.op.regClass().type() == RegType::sgpr))
4387ec681f3Smrg         b = copy(def(v1), b);
4397ec681f3Smrg
4407ec681f3Smrg      if (!carry_in.op.isUndefined())
4417ec681f3Smrg         return vop2(aco_opcode::v_addc_co_u32, Definition(dst), hint_vcc(def(lm)), a, b, carry_in);
4427ec681f3Smrg      else if (program->chip_class >= GFX10 && carry_out)
4437ec681f3Smrg         return vop3(aco_opcode::v_add_co_u32_e64, Definition(dst), def(lm), a, b);
4447ec681f3Smrg      else if (program->chip_class < GFX9 || carry_out)
4457ec681f3Smrg         return vop2(aco_opcode::v_add_co_u32, Definition(dst), hint_vcc(def(lm)), a, b);
4467ec681f3Smrg      else
4477ec681f3Smrg         return vop2(aco_opcode::v_add_u32, Definition(dst), a, b);
4487ec681f3Smrg   }
4497ec681f3Smrg
4507ec681f3Smrg   Result vsub32(Definition dst, Op a, Op b, bool carry_out=false, Op borrow=Op(Operand(s2)))
4517ec681f3Smrg   {
4527ec681f3Smrg      if (!borrow.op.isUndefined() || program->chip_class < GFX9)
4537ec681f3Smrg         carry_out = true;
4547ec681f3Smrg
4557ec681f3Smrg      bool reverse = !b.op.isTemp() || b.op.regClass().type() != RegType::vgpr;
4567ec681f3Smrg      if (reverse)
4577ec681f3Smrg         std::swap(a, b);
4587ec681f3Smrg      if (!b.op.hasRegClass() || b.op.regClass().type() == RegType::sgpr)
4597ec681f3Smrg         b = copy(def(v1), b);
4607ec681f3Smrg
4617ec681f3Smrg      aco_opcode op;
4627ec681f3Smrg      Temp carry;
4637ec681f3Smrg      if (carry_out) {
4647ec681f3Smrg         carry = tmp(s2);
4657ec681f3Smrg         if (borrow.op.isUndefined())
4667ec681f3Smrg            op = reverse ? aco_opcode::v_subrev_co_u32 : aco_opcode::v_sub_co_u32;
4677ec681f3Smrg         else
4687ec681f3Smrg            op = reverse ? aco_opcode::v_subbrev_co_u32 : aco_opcode::v_subb_co_u32;
4697ec681f3Smrg      } else {
4707ec681f3Smrg         op = reverse ? aco_opcode::v_subrev_u32 : aco_opcode::v_sub_u32;
4717ec681f3Smrg      }
4727ec681f3Smrg      bool vop3 = false;
4737ec681f3Smrg      if (program->chip_class >= GFX10 && op == aco_opcode::v_subrev_co_u32) {
4747ec681f3Smrg        vop3 = true;
4757ec681f3Smrg        op = aco_opcode::v_subrev_co_u32_e64;
4767ec681f3Smrg      } else if (program->chip_class >= GFX10 && op == aco_opcode::v_sub_co_u32) {
4777ec681f3Smrg        vop3 = true;
4787ec681f3Smrg        op = aco_opcode::v_sub_co_u32_e64;
4797ec681f3Smrg      }
4807ec681f3Smrg
4817ec681f3Smrg      int num_ops = borrow.op.isUndefined() ? 2 : 3;
4827ec681f3Smrg      int num_defs = carry_out ? 2 : 1;
4837ec681f3Smrg      aco_ptr<Instruction> sub;
4847ec681f3Smrg      if (vop3)
4857ec681f3Smrg        sub.reset(create_instruction<VOP3_instruction>(op, Format::VOP3, num_ops, num_defs));
4867ec681f3Smrg      else
4877ec681f3Smrg        sub.reset(create_instruction<VOP2_instruction>(op, Format::VOP2, num_ops, num_defs));
4887ec681f3Smrg      sub->operands[0] = a.op;
4897ec681f3Smrg      sub->operands[1] = b.op;
4907ec681f3Smrg      if (!borrow.op.isUndefined())
4917ec681f3Smrg         sub->operands[2] = borrow.op;
4927ec681f3Smrg      sub->definitions[0] = dst;
4937ec681f3Smrg      if (carry_out) {
4947ec681f3Smrg         sub->definitions[1] = Definition(carry);
4957ec681f3Smrg         sub->definitions[1].setHint(aco::vcc);
4967ec681f3Smrg      }
4977ec681f3Smrg      return insert(std::move(sub));
4987ec681f3Smrg   }
4997ec681f3Smrg
5007ec681f3Smrg   Result readlane(Definition dst, Op vsrc, Op lane)
5017ec681f3Smrg   {
5027ec681f3Smrg      if (program->chip_class >= GFX8)
5037ec681f3Smrg         return vop3(aco_opcode::v_readlane_b32_e64, dst, vsrc, lane);
5047ec681f3Smrg      else
5057ec681f3Smrg         return vop2(aco_opcode::v_readlane_b32, dst, vsrc, lane);
5067ec681f3Smrg   }
5077ec681f3Smrg   Result writelane(Definition dst, Op val, Op lane, Op vsrc) {
5087ec681f3Smrg      if (program->chip_class >= GFX8)
5097ec681f3Smrg         return vop3(aco_opcode::v_writelane_b32_e64, dst, val, lane, vsrc);
5107ec681f3Smrg      else
5117ec681f3Smrg         return vop2(aco_opcode::v_writelane_b32, dst, val, lane, vsrc);
5127ec681f3Smrg   }
5137ec681f3Smrg<%
5147ec681f3Smrgimport itertools
5157ec681f3Smrgformats = [("pseudo", [Format.PSEUDO], 'Pseudo_instruction', list(itertools.product(range(5), range(6))) + [(8, 1), (1, 8)]),
5167ec681f3Smrg           ("sop1", [Format.SOP1], 'SOP1_instruction', [(0, 1), (1, 0), (1, 1), (2, 1), (3, 2)]),
5177ec681f3Smrg           ("sop2", [Format.SOP2], 'SOP2_instruction', itertools.product([1, 2], [2, 3])),
5187ec681f3Smrg           ("sopk", [Format.SOPK], 'SOPK_instruction', itertools.product([0, 1, 2], [0, 1])),
5197ec681f3Smrg           ("sopp", [Format.SOPP], 'SOPP_instruction', itertools.product([0, 1], [0, 1])),
5207ec681f3Smrg           ("sopc", [Format.SOPC], 'SOPC_instruction', [(1, 2)]),
5217ec681f3Smrg           ("smem", [Format.SMEM], 'SMEM_instruction', [(0, 4), (0, 3), (1, 0), (1, 3), (1, 2), (0, 0)]),
5227ec681f3Smrg           ("ds", [Format.DS], 'DS_instruction', [(1, 1), (1, 2), (0, 3), (0, 4)]),
5237ec681f3Smrg           ("mubuf", [Format.MUBUF], 'MUBUF_instruction', [(0, 4), (1, 3)]),
5247ec681f3Smrg           ("mtbuf", [Format.MTBUF], 'MTBUF_instruction', [(0, 4), (1, 3)]),
5257ec681f3Smrg           ("mimg", [Format.MIMG], 'MIMG_instruction', itertools.product([0, 1], [3, 4, 5, 6, 7])),
5267ec681f3Smrg           ("exp", [Format.EXP], 'Export_instruction', [(0, 4)]),
5277ec681f3Smrg           ("branch", [Format.PSEUDO_BRANCH], 'Pseudo_branch_instruction', itertools.product([1], [0, 1])),
5287ec681f3Smrg           ("barrier", [Format.PSEUDO_BARRIER], 'Pseudo_barrier_instruction', [(0, 0)]),
5297ec681f3Smrg           ("reduction", [Format.PSEUDO_REDUCTION], 'Pseudo_reduction_instruction', [(3, 2)]),
5307ec681f3Smrg           ("vop1", [Format.VOP1], 'VOP1_instruction', [(0, 0), (1, 1), (2, 2)]),
5317ec681f3Smrg           ("vop1_sdwa", [Format.VOP1, Format.SDWA], 'SDWA_instruction', [(1, 1)]),
5327ec681f3Smrg           ("vop2", [Format.VOP2], 'VOP2_instruction', itertools.product([1, 2], [2, 3])),
5337ec681f3Smrg           ("vop2_sdwa", [Format.VOP2, Format.SDWA], 'SDWA_instruction', itertools.product([1, 2], [2, 3])),
5347ec681f3Smrg           ("vopc", [Format.VOPC], 'VOPC_instruction', itertools.product([1, 2], [2])),
5357ec681f3Smrg           ("vopc_sdwa", [Format.VOPC, Format.SDWA], 'SDWA_instruction', itertools.product([1, 2], [2])),
5367ec681f3Smrg           ("vop3", [Format.VOP3], 'VOP3_instruction', [(1, 3), (1, 2), (1, 1), (2, 2)]),
5377ec681f3Smrg           ("vop3p", [Format.VOP3P], 'VOP3P_instruction', [(1, 2), (1, 3)]),
5387ec681f3Smrg           ("vintrp", [Format.VINTRP], 'Interp_instruction', [(1, 2), (1, 3)]),
5397ec681f3Smrg           ("vop1_dpp", [Format.VOP1, Format.DPP], 'DPP_instruction', [(1, 1)]),
5407ec681f3Smrg           ("vop2_dpp", [Format.VOP2, Format.DPP], 'DPP_instruction', itertools.product([1, 2], [2, 3])),
5417ec681f3Smrg           ("vopc_dpp", [Format.VOPC, Format.DPP], 'DPP_instruction', itertools.product([1, 2], [2])),
5427ec681f3Smrg           ("vop1_e64", [Format.VOP1, Format.VOP3], 'VOP3_instruction', itertools.product([1], [1])),
5437ec681f3Smrg           ("vop2_e64", [Format.VOP2, Format.VOP3], 'VOP3_instruction', itertools.product([1, 2], [2, 3])),
5447ec681f3Smrg           ("vopc_e64", [Format.VOPC, Format.VOP3], 'VOP3_instruction', itertools.product([1, 2], [2])),
5457ec681f3Smrg           ("flat", [Format.FLAT], 'FLAT_instruction', [(0, 3), (1, 2)]),
5467ec681f3Smrg           ("global", [Format.GLOBAL], 'FLAT_instruction', [(0, 3), (1, 2)])]
5477ec681f3Smrgformats = [(f if len(f) == 5 else f + ('',)) for f in formats]
5487ec681f3Smrg%>\\
5497ec681f3Smrg% for name, formats, struct, shapes, extra_field_setup in formats:
5507ec681f3Smrg    % for num_definitions, num_operands in shapes:
5517ec681f3Smrg        <%
5527ec681f3Smrg        args = ['aco_opcode opcode']
5537ec681f3Smrg        for i in range(num_definitions):
5547ec681f3Smrg            args.append('Definition def%d' % i)
5557ec681f3Smrg        for i in range(num_operands):
5567ec681f3Smrg            args.append('Op op%d' % i)
5577ec681f3Smrg        for f in formats:
5587ec681f3Smrg            args += f.get_builder_field_decls()
5597ec681f3Smrg        %>\\
5607ec681f3Smrg
5617ec681f3Smrg   Result ${name}(${', '.join(args)})
5627ec681f3Smrg   {
5637ec681f3Smrg      ${struct} *instr = create_instruction<${struct}>(opcode, (Format)(${'|'.join('(int)Format::%s' % f.name for f in formats)}), ${num_operands}, ${num_definitions});
5647ec681f3Smrg        % for i in range(num_definitions):
5657ec681f3Smrg            instr->definitions[${i}] = def${i};
5667ec681f3Smrg            instr->definitions[${i}].setPrecise(is_precise);
5677ec681f3Smrg            instr->definitions[${i}].setNUW(is_nuw);
5687ec681f3Smrg        % endfor
5697ec681f3Smrg        % for i in range(num_operands):
5707ec681f3Smrg            instr->operands[${i}] = op${i}.op;
5717ec681f3Smrg        % endfor
5727ec681f3Smrg        % for f in formats:
5737ec681f3Smrg            % for dest, field_name in zip(f.get_builder_field_dests(), f.get_builder_field_names()):
5747ec681f3Smrg      instr->${dest} = ${field_name};
5757ec681f3Smrg            % endfor
5767ec681f3Smrg            ${f.get_builder_initialization(num_operands)}
5777ec681f3Smrg        % endfor
5787ec681f3Smrg       ${extra_field_setup}
5797ec681f3Smrg      return insert(instr);
5807ec681f3Smrg   }
5817ec681f3Smrg
5827ec681f3Smrg    % if name == 'sop1' or name == 'sop2' or name == 'sopc':
5837ec681f3Smrg        <%
5847ec681f3Smrg        args[0] = 'WaveSpecificOpcode opcode'
5857ec681f3Smrg        params = []
5867ec681f3Smrg        for i in range(num_definitions):
5877ec681f3Smrg            params.append('def%d' % i)
5887ec681f3Smrg        for i in range(num_operands):
5897ec681f3Smrg            params.append('op%d' % i)
5907ec681f3Smrg        %>\\
5917ec681f3Smrg
5927ec681f3Smrg   inline Result ${name}(${', '.join(args)})
5937ec681f3Smrg   {
5947ec681f3Smrg       return ${name}(w64or32(opcode), ${', '.join(params)});
5957ec681f3Smrg   }
5967ec681f3Smrg
5977ec681f3Smrg    % endif
5987ec681f3Smrg    % endfor
5997ec681f3Smrg% endfor
6007ec681f3Smrg};
6017ec681f3Smrg
6027ec681f3Smrg} // namespace aco
6037ec681f3Smrg
6047ec681f3Smrg#endif /* _ACO_BUILDER_ */"""
6057ec681f3Smrg
6067ec681f3Smrgfrom aco_opcodes import opcodes, Format
6077ec681f3Smrgfrom mako.template import Template
6087ec681f3Smrg
6097ec681f3Smrgprint(Template(template).render(opcodes=opcodes, Format=Format))
610