Path: blob/21.2-virgl/src/intel/compiler/brw_fs_register_coalesce.cpp
7176 views
/*1* Copyright © 2012 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/** @file brw_fs_register_coalesce.cpp24*25* Implements register coalescing: Checks if the two registers involved in a26* raw move don't interfere, in which case they can both be stored in the same27* place and the MOV removed.28*29* To do this, all uses of the source of the MOV in the shader are replaced30* with the destination of the MOV. For example:31*32* add vgrf3:F, vgrf1:F, vgrf2:F33* mov vgrf4:F, vgrf3:F34* mul vgrf5:F, vgrf5:F, vgrf4:F35*36* becomes37*38* add vgrf4:F, vgrf1:F, vgrf2:F39* mul vgrf5:F, vgrf5:F, vgrf4:F40*/4142#include "brw_fs.h"43#include "brw_cfg.h"44#include "brw_fs_live_variables.h"4546using namespace brw;4748static bool49is_nop_mov(const fs_inst *inst)50{51if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) {52fs_reg dst = inst->dst;53for (int i = 0; i < inst->sources; i++) {54if (!dst.equals(inst->src[i])) {55return false;56}57dst.offset += (i < inst->header_size ? REG_SIZE :58inst->exec_size * dst.stride *59type_sz(inst->src[i].type));60}61return true;62} else if (inst->opcode == BRW_OPCODE_MOV) {63return inst->dst.equals(inst->src[0]);64}6566return false;67}6869static bool70is_coalesce_candidate(const fs_visitor *v, const fs_inst *inst)71{72if ((inst->opcode != BRW_OPCODE_MOV &&73inst->opcode != SHADER_OPCODE_LOAD_PAYLOAD) ||74inst->is_partial_write() ||75inst->saturate ||76inst->src[0].file != VGRF ||77inst->src[0].negate ||78inst->src[0].abs ||79!inst->src[0].is_contiguous() ||80inst->dst.file != VGRF ||81inst->dst.type != inst->src[0].type) {82return false;83}8485if (v->alloc.sizes[inst->src[0].nr] >86v->alloc.sizes[inst->dst.nr])87return false;8889if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) {90if (!is_coalescing_payload(v->alloc, inst)) {91return false;92}93}9495return true;96}9798static bool99can_coalesce_vars(const fs_live_variables &live, const cfg_t *cfg,100const bblock_t *block, const fs_inst *inst,101int dst_var, int src_var)102{103if (!live.vars_interfere(src_var, dst_var))104return true;105106int dst_start = live.start[dst_var];107int dst_end = live.end[dst_var];108int src_start = live.start[src_var];109int src_end = live.end[src_var];110111/* Variables interfere and one line range isn't a subset of the other. */112if ((dst_end > src_end && src_start < dst_start) ||113(src_end > dst_end && dst_start < src_start))114return false;115116/* Check for a write to either register in the intersection of their live117* ranges.118*/119int start_ip = MAX2(dst_start, src_start);120int end_ip = MIN2(dst_end, src_end);121122foreach_block(scan_block, cfg) {123if (scan_block->end_ip < start_ip)124continue;125126int scan_ip = scan_block->start_ip - 1;127128bool seen_src_write = false;129bool seen_copy = false;130foreach_inst_in_block(fs_inst, scan_inst, scan_block) {131scan_ip++;132133/* Ignore anything before the intersection of the live ranges */134if (scan_ip < start_ip)135continue;136137/* Ignore the copying instruction itself */138if (scan_inst == inst) {139seen_copy = true;140continue;141}142143if (scan_ip > end_ip)144return true; /* registers do not interfere */145146if (seen_src_write && !seen_copy) {147/* In order to satisfy the guarantee of register coalescing, we148* must ensure that the two registers always have the same value149* during the intersection of their live ranges. One way to do150* this is to simply ensure that neither is ever written apart151* from the one copy which syncs up the two registers. However,152* this can be overly conservative and only works in the case153* where the destination live range is entirely contained in the154* source live range.155*156* To handle the other case where the source is contained in the157* destination, we allow writes to the source register as long as158* they happen before the copy, in the same block as the copy, and159* the destination is never read between first such write and the160* copy. This effectively moves the write from the copy up.161*/162for (int j = 0; j < scan_inst->sources; j++) {163if (regions_overlap(scan_inst->src[j], scan_inst->size_read(j),164inst->dst, inst->size_written))165return false; /* registers interfere */166}167}168169/* The MOV being coalesced had better be the only instruction which170* writes to the coalesce destination in the intersection.171*/172if (regions_overlap(scan_inst->dst, scan_inst->size_written,173inst->dst, inst->size_written))174return false; /* registers interfere */175176/* See the big comment above */177if (regions_overlap(scan_inst->dst, scan_inst->size_written,178inst->src[0], inst->size_read(0))) {179if (seen_copy || scan_block != block)180return false;181seen_src_write = true;182}183}184}185186return true;187}188189bool190fs_visitor::register_coalesce()191{192bool progress = false;193fs_live_variables &live = live_analysis.require();194int src_size = 0;195int channels_remaining = 0;196unsigned src_reg = ~0u, dst_reg = ~0u;197int dst_reg_offset[MAX_VGRF_SIZE];198fs_inst *mov[MAX_VGRF_SIZE];199int dst_var[MAX_VGRF_SIZE];200int src_var[MAX_VGRF_SIZE];201202foreach_block_and_inst(block, fs_inst, inst, cfg) {203if (!is_coalesce_candidate(this, inst))204continue;205206if (is_nop_mov(inst)) {207inst->opcode = BRW_OPCODE_NOP;208progress = true;209continue;210}211212if (src_reg != inst->src[0].nr) {213src_reg = inst->src[0].nr;214215src_size = alloc.sizes[inst->src[0].nr];216assert(src_size <= MAX_VGRF_SIZE);217218channels_remaining = src_size;219memset(mov, 0, sizeof(mov));220221dst_reg = inst->dst.nr;222}223224if (dst_reg != inst->dst.nr)225continue;226227if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) {228for (int i = 0; i < src_size; i++) {229dst_reg_offset[i] = i;230}231mov[0] = inst;232channels_remaining -= regs_written(inst);233} else {234const int offset = inst->src[0].offset / REG_SIZE;235if (mov[offset]) {236/* This is the second time that this offset in the register has237* been set. This means, in particular, that inst->dst was238* live before this instruction and that the live ranges of239* inst->dst and inst->src[0] overlap and we can't coalesce the240* two variables. Let's ensure that doesn't happen.241*/242channels_remaining = -1;243continue;244}245for (unsigned i = 0; i < MAX2(inst->size_written / REG_SIZE, 1); i++)246dst_reg_offset[offset + i] = inst->dst.offset / REG_SIZE + i;247mov[offset] = inst;248channels_remaining -= regs_written(inst);249}250251if (channels_remaining)252continue;253254bool can_coalesce = true;255for (int i = 0; i < src_size; i++) {256if (dst_reg_offset[i] != dst_reg_offset[0] + i) {257/* Registers are out-of-order. */258can_coalesce = false;259src_reg = ~0u;260break;261}262263dst_var[i] = live.var_from_vgrf[dst_reg] + dst_reg_offset[i];264src_var[i] = live.var_from_vgrf[src_reg] + i;265266if (!can_coalesce_vars(live, cfg, block, inst, dst_var[i], src_var[i])) {267can_coalesce = false;268src_reg = ~0u;269break;270}271}272273if (!can_coalesce)274continue;275276progress = true;277278for (int i = 0; i < src_size; i++) {279if (!mov[i])280continue;281282if (mov[i]->conditional_mod == BRW_CONDITIONAL_NONE) {283mov[i]->opcode = BRW_OPCODE_NOP;284mov[i]->dst = reg_undef;285for (int j = 0; j < mov[i]->sources; j++) {286mov[i]->src[j] = reg_undef;287}288} else {289/* If we have a conditional modifier, rewrite the MOV to be a290* MOV.cmod from the coalesced register. Hopefully, cmod291* propagation will clean this up and move it to the instruction292* that writes the register. If not, this keeps things correct293* while still letting us coalesce.294*/295assert(mov[i]->opcode == BRW_OPCODE_MOV);296assert(mov[i]->sources == 1);297mov[i]->src[0] = mov[i]->dst;298mov[i]->dst = retype(brw_null_reg(), mov[i]->dst.type);299}300}301302foreach_block_and_inst(block, fs_inst, scan_inst, cfg) {303if (scan_inst->dst.file == VGRF &&304scan_inst->dst.nr == src_reg) {305scan_inst->dst.nr = dst_reg;306scan_inst->dst.offset = scan_inst->dst.offset % REG_SIZE +307dst_reg_offset[scan_inst->dst.offset / REG_SIZE] * REG_SIZE;308}309310for (int j = 0; j < scan_inst->sources; j++) {311if (scan_inst->src[j].file == VGRF &&312scan_inst->src[j].nr == src_reg) {313scan_inst->src[j].nr = dst_reg;314scan_inst->src[j].offset = scan_inst->src[j].offset % REG_SIZE +315dst_reg_offset[scan_inst->src[j].offset / REG_SIZE] * REG_SIZE;316}317}318}319320for (int i = 0; i < src_size; i++) {321live.start[dst_var[i]] = MIN2(live.start[dst_var[i]],322live.start[src_var[i]]);323live.end[dst_var[i]] = MAX2(live.end[dst_var[i]],324live.end[src_var[i]]);325}326src_reg = ~0u;327}328329if (progress) {330foreach_block_and_inst_safe (block, backend_instruction, inst, cfg) {331if (inst->opcode == BRW_OPCODE_NOP) {332inst->remove(block, true);333}334}335336cfg->adjust_block_ips();337338invalidate_analysis(DEPENDENCY_INSTRUCTIONS);339}340341return progress;342}343344345