Path: blob/21.2-virgl/src/broadcom/compiler/vir_opt_copy_propagate.c
4564 views
/*1* Copyright © 2014 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_copy_propagation.c25*26* This implements simple copy propagation for VIR without control flow.27*28* For each temp, it keeps a qreg of which source it was MOVed from, if it29* was. If we see that used later, we can just reuse the source value, since30* we know we don't have control flow, and we have SSA for our values so31* there's no killing to worry about.32*/3334#include "v3d_compiler.h"3536static bool37is_copy_mov(struct qinst *inst)38{39if (!inst)40return false;4142if (inst->qpu.type != V3D_QPU_INSTR_TYPE_ALU ||43(inst->qpu.alu.mul.op != V3D_QPU_M_FMOV &&44inst->qpu.alu.mul.op != V3D_QPU_M_MOV)) {45return false;46}4748if (inst->dst.file != QFILE_TEMP)49return false;5051if (inst->src[0].file != QFILE_TEMP)52return false;5354if (inst->qpu.alu.add.output_pack != V3D_QPU_PACK_NONE ||55inst->qpu.alu.mul.output_pack != V3D_QPU_PACK_NONE) {56return false;57}5859if (inst->qpu.flags.ac != V3D_QPU_COND_NONE ||60inst->qpu.flags.mc != V3D_QPU_COND_NONE) {61return false;62}6364switch (inst->src[0].file) {65case QFILE_MAGIC:66/* No copy propagating from R3/R4/R5 -- the MOVs from those67* are there to register allocate values produced into R3/4/568* to other regs (though hopefully r3/4/5).69*/70switch (inst->src[0].index) {71case V3D_QPU_WADDR_R3:72case V3D_QPU_WADDR_R4:73case V3D_QPU_WADDR_R5:74return false;75default:76break;77}78break;7980case QFILE_REG:81switch (inst->src[0].index) {82case 0:83case 1:84case 2:85/* MOVs from rf0/1/2 are only to track the live86* intervals for W/centroid W/Z.87*/88return false;89}90break;9192default:93break;94}9596return true;97}9899static bool100vir_has_unpack(struct qinst *inst, int chan)101{102assert(chan == 0 || chan == 1);103104if (vir_is_add(inst)) {105if (chan == 0)106return inst->qpu.alu.add.a_unpack != V3D_QPU_UNPACK_NONE;107else108return inst->qpu.alu.add.b_unpack != V3D_QPU_UNPACK_NONE;109} else {110if (chan == 0)111return inst->qpu.alu.mul.a_unpack != V3D_QPU_UNPACK_NONE;112else113return inst->qpu.alu.mul.b_unpack != V3D_QPU_UNPACK_NONE;114}115}116117static bool118try_copy_prop(struct v3d_compile *c, struct qinst *inst, struct qinst **movs)119{120bool debug = false;121bool progress = false;122123for (int i = 0; i < vir_get_nsrc(inst); i++) {124if (inst->src[i].file != QFILE_TEMP)125continue;126127/* We have two ways of finding MOVs we can copy propagate128* from. One is if it's an SSA def: then we can reuse it from129* any block in the program, as long as its source is also an130* SSA def. Alternatively, if it's in the "movs" array131* tracked within the block, then we know the sources for it132* haven't been changed since we saw the instruction within133* our block.134*/135struct qinst *mov = movs[inst->src[i].index];136if (!mov) {137if (!is_copy_mov(c->defs[inst->src[i].index]))138continue;139mov = c->defs[inst->src[i].index];140141if (mov->src[0].file == QFILE_TEMP &&142!c->defs[mov->src[0].index])143continue;144}145146if (vir_has_unpack(mov, 0)) {147/* Make sure that the meaning of the unpack148* would be the same between the two149* instructions.150*/151if (v3d_qpu_unpacks_f32(&inst->qpu) !=152v3d_qpu_unpacks_f32(&mov->qpu) ||153v3d_qpu_unpacks_f16(&inst->qpu) !=154v3d_qpu_unpacks_f16(&mov->qpu)) {155continue;156}157158/* No composing the unpacks. */159if (vir_has_unpack(inst, i))160continue;161162/* these ops can't represent abs. */163if (mov->qpu.alu.mul.a_unpack == V3D_QPU_UNPACK_ABS) {164switch (inst->qpu.alu.add.op) {165case V3D_QPU_A_VFPACK:166case V3D_QPU_A_FROUND:167case V3D_QPU_A_FTRUNC:168case V3D_QPU_A_FFLOOR:169case V3D_QPU_A_FCEIL:170case V3D_QPU_A_FDX:171case V3D_QPU_A_FDY:172case V3D_QPU_A_FTOIN:173case V3D_QPU_A_FTOIZ:174case V3D_QPU_A_FTOUZ:175case V3D_QPU_A_FTOC:176continue;177default:178break;179}180}181}182183if (debug) {184fprintf(stderr, "Copy propagate: ");185vir_dump_inst(c, inst);186fprintf(stderr, "\n");187}188189inst->src[i] = mov->src[0];190if (vir_has_unpack(mov, 0)) {191enum v3d_qpu_input_unpack unpack = mov->qpu.alu.mul.a_unpack;192193vir_set_unpack(inst, i, unpack);194}195196if (debug) {197fprintf(stderr, "to: ");198vir_dump_inst(c, inst);199fprintf(stderr, "\n");200}201202progress = true;203}204205return progress;206}207208static void209apply_kills(struct v3d_compile *c, struct qinst **movs, struct qinst *inst)210{211if (inst->dst.file != QFILE_TEMP)212return;213214for (int i = 0; i < c->num_temps; i++) {215if (movs[i] &&216(movs[i]->dst.index == inst->dst.index ||217(movs[i]->src[0].file == QFILE_TEMP &&218movs[i]->src[0].index == inst->dst.index))) {219movs[i] = NULL;220}221}222}223224bool225vir_opt_copy_propagate(struct v3d_compile *c)226{227bool progress = false;228struct qinst **movs;229230movs = ralloc_array(c, struct qinst *, c->num_temps);231if (!movs)232return false;233234vir_for_each_block(block, c) {235/* The MOVs array tracks only available movs within the236* block.237*/238memset(movs, 0, sizeof(struct qinst *) * c->num_temps);239240vir_for_each_inst(inst, block) {241progress = try_copy_prop(c, inst, movs) || progress;242243apply_kills(c, movs, inst);244245if (is_copy_mov(inst))246movs[inst->dst.index] = inst;247}248}249250ralloc_free(movs);251252return progress;253}254255256