Path: blob/21.2-virgl/src/intel/compiler/brw_fs_saturate_propagation.cpp
4550 views
/*1* Copyright © 2013 Intel 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*/2223#include "brw_fs.h"24#include "brw_fs_live_variables.h"25#include "brw_cfg.h"2627using namespace brw;2829/** @file brw_fs_saturate_propagation.cpp30*31* Implements a pass that propagates the SAT modifier from a MOV.SAT into the32* instruction that produced the source of the MOV.SAT, thereby allowing the33* MOV's src and dst to be coalesced and the MOV removed.34*35* For instance,36*37* ADD tmp, src0, src138* MOV.SAT dst, tmp39*40* would be transformed into41*42* ADD.SAT tmp, src0, src143* MOV dst, tmp44*/4546static bool47opt_saturate_propagation_local(const fs_live_variables &live, bblock_t *block)48{49bool progress = false;50int ip = block->end_ip + 1;5152foreach_inst_in_block_reverse(fs_inst, inst, block) {53ip--;5455if (inst->opcode != BRW_OPCODE_MOV ||56!inst->saturate ||57inst->dst.file != VGRF ||58inst->dst.type != inst->src[0].type ||59inst->src[0].file != VGRF ||60inst->src[0].abs)61continue;6263int src_var = live.var_from_reg(inst->src[0]);64int src_end_ip = live.end[src_var];6566bool interfered = false;67foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {68if (scan_inst->exec_size == inst->exec_size &&69regions_overlap(scan_inst->dst, scan_inst->size_written,70inst->src[0], inst->size_read(0))) {71if (scan_inst->is_partial_write() ||72(scan_inst->dst.type != inst->dst.type &&73!scan_inst->can_change_types()))74break;7576if (scan_inst->saturate) {77inst->saturate = false;78progress = true;79} else if (src_end_ip == ip || inst->dst.equals(inst->src[0])) {80if (scan_inst->can_do_saturate()) {81if (scan_inst->dst.type != inst->dst.type) {82scan_inst->dst.type = inst->dst.type;83for (int i = 0; i < scan_inst->sources; i++) {84scan_inst->src[i].type = inst->dst.type;85}86}8788if (inst->src[0].negate) {89if (scan_inst->opcode == BRW_OPCODE_MUL) {90scan_inst->src[0].negate = !scan_inst->src[0].negate;91inst->src[0].negate = false;92} else if (scan_inst->opcode == BRW_OPCODE_MAD) {93for (int i = 0; i < 2; i++) {94if (scan_inst->src[i].file == IMM) {95brw_negate_immediate(scan_inst->src[i].type,96&scan_inst->src[i].as_brw_reg());97} else {98scan_inst->src[i].negate = !scan_inst->src[i].negate;99}100}101inst->src[0].negate = false;102} else if (scan_inst->opcode == BRW_OPCODE_ADD) {103if (scan_inst->src[1].file == IMM) {104if (!brw_negate_immediate(scan_inst->src[1].type,105&scan_inst->src[1].as_brw_reg())) {106break;107}108} else {109scan_inst->src[1].negate = !scan_inst->src[1].negate;110}111scan_inst->src[0].negate = !scan_inst->src[0].negate;112inst->src[0].negate = false;113} else {114break;115}116}117118scan_inst->saturate = true;119inst->saturate = false;120progress = true;121}122}123break;124}125for (int i = 0; i < scan_inst->sources; i++) {126if (scan_inst->src[i].file == VGRF &&127scan_inst->src[i].nr == inst->src[0].nr &&128scan_inst->src[i].offset / REG_SIZE ==129inst->src[0].offset / REG_SIZE) {130if (scan_inst->opcode != BRW_OPCODE_MOV ||131!scan_inst->saturate ||132scan_inst->src[0].abs ||133scan_inst->src[0].negate ||134scan_inst->src[0].abs != inst->src[0].abs ||135scan_inst->src[0].negate != inst->src[0].negate) {136interfered = true;137break;138}139}140}141142if (interfered)143break;144}145}146147return progress;148}149150bool151fs_visitor::opt_saturate_propagation()152{153const fs_live_variables &live = live_analysis.require();154bool progress = false;155156foreach_block (block, cfg) {157progress = opt_saturate_propagation_local(live, block) || progress;158}159160/* Live intervals are still valid. */161162return progress;163}164165166