Path: blob/21.2-virgl/src/broadcom/compiler/vir_opt_redundant_flags.c
4564 views
/*1* Copyright © 2019 Broadcom2*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*/2223/**24* @file v3d_opt_redundant_flags.c25*26* This eliminates the APF/MPF flags for redundant flags updates. These are27* often produced by our channel masking in nonuniform control flow.28*/2930#include "v3d_compiler.h"3132static bool debug;3334static void35vir_dce_pf(struct v3d_compile *c, struct qinst *inst)36{37if (debug) {38fprintf(stderr,39"Removing flags write from: ");40vir_dump_inst(c, inst);41fprintf(stderr, "\n");42}4344assert(inst->qpu.type == V3D_QPU_INSTR_TYPE_ALU);4546inst->qpu.flags.apf = V3D_QPU_PF_NONE;47inst->qpu.flags.mpf = V3D_QPU_PF_NONE;48}4950static bool51vir_sources_modified(struct qinst *srcs, struct qinst *write)52{53for (int i = 0; i < vir_get_nsrc(srcs); i++) {54if (write->dst.file == QFILE_TEMP &&55srcs->src[i].file == QFILE_TEMP &&56srcs->src[i].index == write->dst.index) {57return true;58}5960/* assume magic regs may be modified by basically anything. */61if (srcs->src[i].file != QFILE_TEMP &&62srcs->src[i].file != QFILE_SMALL_IMM)63return true;64}6566return false;67}6869static bool70vir_instr_flags_op_equal(struct qinst *a, struct qinst *b)71{72for (int i = 0; i < vir_get_nsrc(a); i++) {73if (a->src[i].file != b->src[i].file ||74a->src[i].index != b->src[i].index) {75return false;76}77}7879if (a->qpu.flags.apf != b->qpu.flags.apf ||80a->qpu.flags.mpf != b->qpu.flags.mpf ||81a->qpu.alu.add.op != b->qpu.alu.add.op ||82a->qpu.alu.mul.op != b->qpu.alu.mul.op ||83a->qpu.alu.add.a_unpack != b->qpu.alu.add.a_unpack ||84a->qpu.alu.add.b_unpack != b->qpu.alu.add.b_unpack ||85a->qpu.alu.add.output_pack != b->qpu.alu.add.output_pack ||86a->qpu.alu.mul.a_unpack != b->qpu.alu.mul.a_unpack ||87a->qpu.alu.mul.b_unpack != b->qpu.alu.mul.b_unpack ||88a->qpu.alu.mul.output_pack != b->qpu.alu.mul.output_pack) {89return false;90}9192return true;93}9495static bool96vir_opt_redundant_flags_block(struct v3d_compile *c, struct qblock *block)97{98struct qinst *last_flags = NULL;99bool progress = false;100101vir_for_each_inst(inst, block) {102if (inst->qpu.type != V3D_QPU_INSTR_TYPE_ALU ||103inst->qpu.flags.auf != V3D_QPU_UF_NONE ||104inst->qpu.flags.muf != V3D_QPU_UF_NONE) {105last_flags = NULL;106continue;107}108109/* Flags aren't preserved across a thrsw. */110if (inst->qpu.sig.thrsw)111last_flags = NULL;112113if (inst->qpu.flags.apf != V3D_QPU_PF_NONE ||114inst->qpu.flags.mpf != V3D_QPU_PF_NONE) {115if (last_flags &&116vir_instr_flags_op_equal(inst, last_flags)) {117vir_dce_pf(c, inst);118progress = true;119} else {120last_flags = inst;121}122}123124if (last_flags && vir_sources_modified(last_flags, inst)) {125last_flags = NULL;126}127}128129return progress;130}131132bool133vir_opt_redundant_flags(struct v3d_compile *c)134{135bool progress = false;136137vir_for_each_block(block, c) {138progress = vir_opt_redundant_flags_block(c, block) || progress;139}140141return progress;142}143144145