Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_opt_copy_propagation.c
4570 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 vc4_opt_copy_propagation.c25*26* This implements simple copy propagation for QIR 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 "vc4_qir.h"3536static bool37is_copy_mov(struct qinst *inst)38{39if (!inst)40return false;4142if (inst->op != QOP_MOV &&43inst->op != QOP_FMOV &&44inst->op != QOP_MMOV) {45return false;46}4748if (inst->dst.file != QFILE_TEMP)49return false;5051if (inst->src[0].file != QFILE_TEMP &&52inst->src[0].file != QFILE_UNIF) {53return false;54}5556if (inst->dst.pack || inst->cond != QPU_COND_ALWAYS)57return false;5859return true;6061}6263static bool64try_copy_prop(struct vc4_compile *c, struct qinst *inst, struct qinst **movs)65{66bool debug = false;67bool progress = false;6869for (int i = 0; i < qir_get_nsrc(inst); i++) {70if (inst->src[i].file != QFILE_TEMP)71continue;7273/* We have two ways of finding MOVs we can copy propagate74* from. One is if it's an SSA def: then we can reuse it from75* any block in the program, as long as its source is also an76* SSA def. Alternatively, if it's in the "movs" array77* tracked within the block, then we know the sources for it78* haven't been changed since we saw the instruction within79* our block.80*/81struct qinst *mov = movs[inst->src[i].index];82if (!mov) {83if (!is_copy_mov(c->defs[inst->src[i].index]))84continue;85mov = c->defs[inst->src[i].index];8687if (mov->src[0].file == QFILE_TEMP &&88!c->defs[mov->src[0].index])89continue;90}9192/* Mul rotation's source needs to be in an r0-r3 accumulator,93* so no uniforms or regfile-a/r4 unpacking allowed.94*/95if (inst->op == QOP_ROT_MUL &&96(mov->src[0].file != QFILE_TEMP ||97mov->src[0].pack))98continue;99100uint8_t unpack;101if (mov->src[0].pack) {102/* Make sure that the meaning of the unpack103* would be the same between the two104* instructions.105*/106if (qir_is_float_input(inst) !=107qir_is_float_input(mov)) {108continue;109}110111/* There's only one unpack field, so make sure112* this instruction doesn't already use it.113*/114bool already_has_unpack = false;115for (int j = 0; j < qir_get_nsrc(inst); j++) {116if (inst->src[j].pack)117already_has_unpack = true;118}119if (already_has_unpack)120continue;121122/* A destination pack requires the PM bit to123* be set to a specific value already, which124* may be different from ours.125*/126if (inst->dst.pack)127continue;128129unpack = mov->src[0].pack;130} else {131unpack = inst->src[i].pack;132}133134if (debug) {135fprintf(stderr, "Copy propagate: ");136qir_dump_inst(c, inst);137fprintf(stderr, "\n");138}139140inst->src[i] = mov->src[0];141inst->src[i].pack = unpack;142143if (debug) {144fprintf(stderr, "to: ");145qir_dump_inst(c, inst);146fprintf(stderr, "\n");147}148149progress = true;150}151152return progress;153}154155static void156apply_kills(struct vc4_compile *c, struct qinst **movs, struct qinst *inst)157{158if (inst->dst.file != QFILE_TEMP)159return;160161for (int i = 0; i < c->num_temps; i++) {162if (movs[i] &&163(movs[i]->dst.index == inst->dst.index ||164(movs[i]->src[0].file == QFILE_TEMP &&165movs[i]->src[0].index == inst->dst.index))) {166movs[i] = NULL;167}168}169}170171bool172qir_opt_copy_propagation(struct vc4_compile *c)173{174bool progress = false;175struct qinst **movs;176177movs = ralloc_array(c, struct qinst *, c->num_temps);178if (!movs)179return false;180181qir_for_each_block(block, c) {182/* The MOVs array tracks only available movs within the183* block.184*/185memset(movs, 0, sizeof(struct qinst *) * c->num_temps);186187qir_for_each_inst(inst, block) {188progress = try_copy_prop(c, inst, movs) || progress;189190apply_kills(c, movs, inst);191192if (is_copy_mov(inst))193movs[inst->dst.index] = inst;194}195}196197ralloc_free(movs);198199return progress;200}201202203