Path: blob/21.2-virgl/src/amd/compiler/aco_optimizer_postRA.cpp
4550 views
/*1* Copyright © 2021 Valve Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22*/2324#include "aco_ir.h"2526#include <algorithm>27#include <array>28#include <bitset>29#include <vector>3031namespace aco {32namespace {3334constexpr const size_t max_reg_cnt = 512;3536enum {37not_written_in_block = -1,38clobbered = -2,39const_or_undef = -3,40written_by_multiple_instrs = -4,41};4243struct pr_opt_ctx {44Program* program;45Block* current_block;46int current_instr_idx;47std::vector<uint16_t> uses;48std::array<int, max_reg_cnt * 4u> instr_idx_by_regs;4950void reset_block(Block* block)51{52current_block = block;53current_instr_idx = -1;54std::fill(instr_idx_by_regs.begin(), instr_idx_by_regs.end(), not_written_in_block);55}56};5758void59save_reg_writes(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)60{61for (const Definition& def : instr->definitions) {62assert(def.regClass().type() != RegType::sgpr || def.physReg().reg() <= 255);63assert(def.regClass().type() != RegType::vgpr || def.physReg().reg() >= 256);6465unsigned dw_size = DIV_ROUND_UP(def.bytes(), 4u);66unsigned r = def.physReg().reg();67int idx = ctx.current_instr_idx;6869if (def.regClass().is_subdword())70idx = clobbered;7172assert(def.size() == dw_size || def.regClass().is_subdword());73std::fill(&ctx.instr_idx_by_regs[r], &ctx.instr_idx_by_regs[r + dw_size], idx);74}75}7677int78last_writer_idx(pr_opt_ctx& ctx, PhysReg physReg, RegClass rc)79{80/* Verify that all of the operand's registers are written by the same instruction. */81int instr_idx = ctx.instr_idx_by_regs[physReg.reg()];82unsigned dw_size = DIV_ROUND_UP(rc.bytes(), 4u);83unsigned r = physReg.reg();84bool all_same = std::all_of(&ctx.instr_idx_by_regs[r], &ctx.instr_idx_by_regs[r + dw_size],85[instr_idx](int i) { return i == instr_idx; });8687return all_same ? instr_idx : written_by_multiple_instrs;88}8990int91last_writer_idx(pr_opt_ctx& ctx, const Operand& op)92{93if (op.isConstant() || op.isUndefined())94return const_or_undef;9596int instr_idx = ctx.instr_idx_by_regs[op.physReg().reg()];9798#ifndef NDEBUG99/* Debug mode: */100instr_idx = last_writer_idx(ctx, op.physReg(), op.regClass());101assert(instr_idx != written_by_multiple_instrs);102#endif103104return instr_idx;105}106107void108try_apply_branch_vcc(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)109{110/* We are looking for the following pattern:111*112* vcc = ... ; last_vcc_wr113* sX, scc = s_and_bXX vcc, exec ; op0_instr114* (...vcc and exec must not be clobbered inbetween...)115* s_cbranch_XX scc ; instr116*117* If possible, the above is optimized into:118*119* vcc = ... ; last_vcc_wr120* s_cbranch_XX vcc ; instr modified to use vcc121*/122123/* Don't try to optimize this on GFX6-7 because SMEM may corrupt the vccz bit. */124if (ctx.program->chip_class < GFX8)125return;126127if (instr->format != Format::PSEUDO_BRANCH || instr->operands.size() == 0 ||128instr->operands[0].physReg() != scc)129return;130131int op0_instr_idx = last_writer_idx(ctx, instr->operands[0]);132int last_vcc_wr_idx = last_writer_idx(ctx, vcc, ctx.program->lane_mask);133int last_exec_wr_idx = last_writer_idx(ctx, exec, ctx.program->lane_mask);134135/* We need to make sure:136* - the operand register used by the branch, and VCC were both written in the current block137* - VCC was NOT written after the operand register138* - EXEC is sane and was NOT written after the operand register139*/140if (op0_instr_idx < 0 || last_vcc_wr_idx < 0 || last_vcc_wr_idx > op0_instr_idx ||141last_exec_wr_idx > last_vcc_wr_idx || last_exec_wr_idx < not_written_in_block)142return;143144aco_ptr<Instruction>& op0_instr = ctx.current_block->instructions[op0_instr_idx];145aco_ptr<Instruction>& last_vcc_wr = ctx.current_block->instructions[last_vcc_wr_idx];146147if ((op0_instr->opcode != aco_opcode::s_and_b64 /* wave64 */ &&148op0_instr->opcode != aco_opcode::s_and_b32 /* wave32 */) ||149op0_instr->operands[0].physReg() != vcc || op0_instr->operands[1].physReg() != exec ||150!last_vcc_wr->isVOPC())151return;152153assert(last_vcc_wr->definitions[0].tempId() == op0_instr->operands[0].tempId());154155/* Reduce the uses of the SCC def */156ctx.uses[instr->operands[0].tempId()]--;157/* Use VCC instead of SCC in the branch */158instr->operands[0] = op0_instr->operands[0];159}160161void162try_optimize_scc_nocompare(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)163{164/* We are looking for the following pattern:165*166* s_bfe_u32 s0, s3, 0x40018 ; outputs SGPR and SCC if the SGPR != 0167* s_cmp_eq_i32 s0, 0 ; comparison between the SGPR and 0168* s_cbranch_scc0 BB3 ; use the result of the comparison, eg. branch or cselect169*170* If possible, the above is optimized into:171*172* s_bfe_u32 s0, s3, 0x40018 ; original instruction173* s_cbranch_scc1 BB3 ; modified to use SCC directly rather than the SGPR with comparison174*175*/176177if (!instr->isSALU() && !instr->isBranch())178return;179180if (instr->isSOPC() &&181(instr->opcode == aco_opcode::s_cmp_eq_u32 || instr->opcode == aco_opcode::s_cmp_eq_i32 ||182instr->opcode == aco_opcode::s_cmp_lg_u32 || instr->opcode == aco_opcode::s_cmp_lg_i32 ||183instr->opcode == aco_opcode::s_cmp_eq_u64 || instr->opcode == aco_opcode::s_cmp_lg_u64) &&184(instr->operands[0].constantEquals(0) || instr->operands[1].constantEquals(0)) &&185(instr->operands[0].isTemp() || instr->operands[1].isTemp())) {186/* Make sure the constant is always in operand 1 */187if (instr->operands[0].isConstant())188std::swap(instr->operands[0], instr->operands[1]);189190if (ctx.uses[instr->operands[0].tempId()] > 1)191return;192193/* Make sure both SCC and Operand 0 are written by the same instruction. */194int wr_idx = last_writer_idx(ctx, instr->operands[0]);195int sccwr_idx = last_writer_idx(ctx, scc, s1);196if (wr_idx < 0 || wr_idx != sccwr_idx)197return;198199aco_ptr<Instruction>& wr_instr = ctx.current_block->instructions[wr_idx];200if (!wr_instr->isSALU() || wr_instr->definitions.size() < 2 ||201wr_instr->definitions[1].physReg() != scc)202return;203204/* Look for instructions which set SCC := (D != 0) */205switch (wr_instr->opcode) {206case aco_opcode::s_bfe_i32:207case aco_opcode::s_bfe_i64:208case aco_opcode::s_bfe_u32:209case aco_opcode::s_bfe_u64:210case aco_opcode::s_and_b32:211case aco_opcode::s_and_b64:212case aco_opcode::s_andn2_b32:213case aco_opcode::s_andn2_b64:214case aco_opcode::s_or_b32:215case aco_opcode::s_or_b64:216case aco_opcode::s_orn2_b32:217case aco_opcode::s_orn2_b64:218case aco_opcode::s_xor_b32:219case aco_opcode::s_xor_b64:220case aco_opcode::s_not_b32:221case aco_opcode::s_not_b64:222case aco_opcode::s_nor_b32:223case aco_opcode::s_nor_b64:224case aco_opcode::s_xnor_b32:225case aco_opcode::s_xnor_b64:226case aco_opcode::s_nand_b32:227case aco_opcode::s_nand_b64:228case aco_opcode::s_lshl_b32:229case aco_opcode::s_lshl_b64:230case aco_opcode::s_lshr_b32:231case aco_opcode::s_lshr_b64:232case aco_opcode::s_ashr_i32:233case aco_opcode::s_ashr_i64:234case aco_opcode::s_abs_i32:235case aco_opcode::s_absdiff_i32: break;236default: return;237}238239/* Use the SCC def from wr_instr */240ctx.uses[instr->operands[0].tempId()]--;241instr->operands[0] = Operand(wr_instr->definitions[1].getTemp(), scc);242ctx.uses[instr->operands[0].tempId()]++;243244/* Set the opcode and operand to 32-bit */245instr->operands[1] = Operand::zero();246instr->opcode =247(instr->opcode == aco_opcode::s_cmp_eq_u32 || instr->opcode == aco_opcode::s_cmp_eq_i32 ||248instr->opcode == aco_opcode::s_cmp_eq_u64)249? aco_opcode::s_cmp_eq_u32250: aco_opcode::s_cmp_lg_u32;251} else if ((instr->format == Format::PSEUDO_BRANCH && instr->operands.size() == 1 &&252instr->operands[0].physReg() == scc) ||253instr->opcode == aco_opcode::s_cselect_b32) {254255/* For cselect, operand 2 is the SCC condition */256unsigned scc_op_idx = 0;257if (instr->opcode == aco_opcode::s_cselect_b32) {258scc_op_idx = 2;259}260261int wr_idx = last_writer_idx(ctx, instr->operands[scc_op_idx]);262if (wr_idx < 0)263return;264265aco_ptr<Instruction>& wr_instr = ctx.current_block->instructions[wr_idx];266267/* Check if we found the pattern above. */268if (wr_instr->opcode != aco_opcode::s_cmp_eq_u32 &&269wr_instr->opcode != aco_opcode::s_cmp_lg_u32)270return;271if (wr_instr->operands[0].physReg() != scc)272return;273if (!wr_instr->operands[1].constantEquals(0))274return;275276/* The optimization can be unsafe when there are other users. */277if (ctx.uses[instr->operands[scc_op_idx].tempId()] > 1)278return;279280if (wr_instr->opcode == aco_opcode::s_cmp_eq_u32) {281/* Flip the meaning of the instruction to correctly use the SCC. */282if (instr->format == Format::PSEUDO_BRANCH)283instr->opcode = instr->opcode == aco_opcode::p_cbranch_z ? aco_opcode::p_cbranch_nz284: aco_opcode::p_cbranch_z;285else if (instr->opcode == aco_opcode::s_cselect_b32)286std::swap(instr->operands[0], instr->operands[1]);287else288unreachable(289"scc_nocompare optimization is only implemented for p_cbranch and s_cselect");290}291292/* Use the SCC def from the original instruction, not the comparison */293ctx.uses[instr->operands[scc_op_idx].tempId()]--;294instr->operands[scc_op_idx] = wr_instr->operands[0];295}296}297298void299process_instruction(pr_opt_ctx& ctx, aco_ptr<Instruction>& instr)300{301ctx.current_instr_idx++;302303try_apply_branch_vcc(ctx, instr);304305try_optimize_scc_nocompare(ctx, instr);306307if (instr)308save_reg_writes(ctx, instr);309}310311} // namespace312313void314optimize_postRA(Program* program)315{316pr_opt_ctx ctx;317ctx.program = program;318ctx.uses = dead_code_analysis(program);319320/* Forward pass321* Goes through each instruction exactly once, and can transform322* instructions or adjust the use counts of temps.323*/324for (auto& block : program->blocks) {325ctx.reset_block(&block);326327for (aco_ptr<Instruction>& instr : block.instructions)328process_instruction(ctx, instr);329}330331/* Cleanup pass332* Gets rid of instructions which are manually deleted or333* no longer have any uses.334*/335for (auto& block : program->blocks) {336auto new_end = std::remove_if(block.instructions.begin(), block.instructions.end(),337[&ctx](const aco_ptr<Instruction>& instr)338{ return !instr || is_dead(ctx.uses, instr.get()); });339block.instructions.resize(new_end - block.instructions.begin());340}341}342343} // namespace aco344345346