Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_opt_constant_folding.c
4570 views
/*1* Copyright © 2015 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_constant_folding.c25*26* Simple constant folding pass to clean up operations on only constants,27* which we might have generated within vc4_program.c.28*/2930#include "vc4_qir.h"31#include "util/u_math.h"3233static bool debug;3435static void36dump_from(struct vc4_compile *c, struct qinst *inst)37{38if (!debug)39return;4041fprintf(stderr, "optimizing: ");42qir_dump_inst(c, inst);43fprintf(stderr, "\n");44}4546static void47dump_to(struct vc4_compile *c, struct qinst *inst)48{49if (!debug)50return;5152fprintf(stderr, "to: ");53qir_dump_inst(c, inst);54fprintf(stderr, "\n");55}5657static bool58constant_fold(struct vc4_compile *c, struct qinst *inst)59{60int nsrc = qir_get_nsrc(inst);61uint32_t ui[nsrc];6263for (int i = 0; i < nsrc; i++) {64struct qreg reg = inst->src[i];65if (reg.file == QFILE_UNIF &&66c->uniform_contents[reg.index] == QUNIFORM_CONSTANT) {67ui[i] = c->uniform_data[reg.index];68} else if (reg.file == QFILE_SMALL_IMM) {69ui[i] = reg.index;70} else {71return false;72}73}7475uint32_t result = 0;76switch (inst->op) {77case QOP_SHR:78result = ui[0] >> ui[1];79break;8081default:82return false;83}8485dump_from(c, inst);8687inst->src[0] = qir_uniform_ui(c, result);88for (int i = 1; i < nsrc; i++)89inst->src[i] = c->undef;90inst->op = QOP_MOV;9192dump_to(c, inst);93return true;94}9596bool97qir_opt_constant_folding(struct vc4_compile *c)98{99bool progress = false;100101qir_for_each_inst_inorder(inst, c) {102if (constant_fold(c, inst))103progress = true;104}105106return progress;107}108109110