1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2012 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci/** @file brw_fs_register_coalesce.cpp 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci * Implements register coalescing: Checks if the two registers involved in a 27bf215546Sopenharmony_ci * raw move don't interfere, in which case they can both be stored in the same 28bf215546Sopenharmony_ci * place and the MOV removed. 29bf215546Sopenharmony_ci * 30bf215546Sopenharmony_ci * To do this, all uses of the source of the MOV in the shader are replaced 31bf215546Sopenharmony_ci * with the destination of the MOV. For example: 32bf215546Sopenharmony_ci * 33bf215546Sopenharmony_ci * add vgrf3:F, vgrf1:F, vgrf2:F 34bf215546Sopenharmony_ci * mov vgrf4:F, vgrf3:F 35bf215546Sopenharmony_ci * mul vgrf5:F, vgrf5:F, vgrf4:F 36bf215546Sopenharmony_ci * 37bf215546Sopenharmony_ci * becomes 38bf215546Sopenharmony_ci * 39bf215546Sopenharmony_ci * add vgrf4:F, vgrf1:F, vgrf2:F 40bf215546Sopenharmony_ci * mul vgrf5:F, vgrf5:F, vgrf4:F 41bf215546Sopenharmony_ci */ 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_ci#include "brw_fs.h" 44bf215546Sopenharmony_ci#include "brw_cfg.h" 45bf215546Sopenharmony_ci#include "brw_fs_live_variables.h" 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ciusing namespace brw; 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_cistatic bool 50bf215546Sopenharmony_ciis_nop_mov(const fs_inst *inst) 51bf215546Sopenharmony_ci{ 52bf215546Sopenharmony_ci if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) { 53bf215546Sopenharmony_ci fs_reg dst = inst->dst; 54bf215546Sopenharmony_ci for (int i = 0; i < inst->sources; i++) { 55bf215546Sopenharmony_ci if (!dst.equals(inst->src[i])) { 56bf215546Sopenharmony_ci return false; 57bf215546Sopenharmony_ci } 58bf215546Sopenharmony_ci dst.offset += (i < inst->header_size ? REG_SIZE : 59bf215546Sopenharmony_ci inst->exec_size * dst.stride * 60bf215546Sopenharmony_ci type_sz(inst->src[i].type)); 61bf215546Sopenharmony_ci } 62bf215546Sopenharmony_ci return true; 63bf215546Sopenharmony_ci } else if (inst->opcode == BRW_OPCODE_MOV) { 64bf215546Sopenharmony_ci return inst->dst.equals(inst->src[0]); 65bf215546Sopenharmony_ci } 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_ci return false; 68bf215546Sopenharmony_ci} 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_cistatic bool 71bf215546Sopenharmony_ciis_coalesce_candidate(const fs_visitor *v, const fs_inst *inst) 72bf215546Sopenharmony_ci{ 73bf215546Sopenharmony_ci if ((inst->opcode != BRW_OPCODE_MOV && 74bf215546Sopenharmony_ci inst->opcode != SHADER_OPCODE_LOAD_PAYLOAD) || 75bf215546Sopenharmony_ci inst->is_partial_write() || 76bf215546Sopenharmony_ci inst->saturate || 77bf215546Sopenharmony_ci inst->src[0].file != VGRF || 78bf215546Sopenharmony_ci inst->src[0].negate || 79bf215546Sopenharmony_ci inst->src[0].abs || 80bf215546Sopenharmony_ci !inst->src[0].is_contiguous() || 81bf215546Sopenharmony_ci inst->dst.file != VGRF || 82bf215546Sopenharmony_ci inst->dst.type != inst->src[0].type) { 83bf215546Sopenharmony_ci return false; 84bf215546Sopenharmony_ci } 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci if (v->alloc.sizes[inst->src[0].nr] > 87bf215546Sopenharmony_ci v->alloc.sizes[inst->dst.nr]) 88bf215546Sopenharmony_ci return false; 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) { 91bf215546Sopenharmony_ci if (!is_coalescing_payload(v->alloc, inst)) { 92bf215546Sopenharmony_ci return false; 93bf215546Sopenharmony_ci } 94bf215546Sopenharmony_ci } 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci return true; 97bf215546Sopenharmony_ci} 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_cistatic bool 100bf215546Sopenharmony_cican_coalesce_vars(const fs_live_variables &live, const cfg_t *cfg, 101bf215546Sopenharmony_ci const bblock_t *block, const fs_inst *inst, 102bf215546Sopenharmony_ci int dst_var, int src_var) 103bf215546Sopenharmony_ci{ 104bf215546Sopenharmony_ci if (!live.vars_interfere(src_var, dst_var)) 105bf215546Sopenharmony_ci return true; 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci int dst_start = live.start[dst_var]; 108bf215546Sopenharmony_ci int dst_end = live.end[dst_var]; 109bf215546Sopenharmony_ci int src_start = live.start[src_var]; 110bf215546Sopenharmony_ci int src_end = live.end[src_var]; 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci /* Variables interfere and one line range isn't a subset of the other. */ 113bf215546Sopenharmony_ci if ((dst_end > src_end && src_start < dst_start) || 114bf215546Sopenharmony_ci (src_end > dst_end && dst_start < src_start)) 115bf215546Sopenharmony_ci return false; 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci /* Check for a write to either register in the intersection of their live 118bf215546Sopenharmony_ci * ranges. 119bf215546Sopenharmony_ci */ 120bf215546Sopenharmony_ci int start_ip = MAX2(dst_start, src_start); 121bf215546Sopenharmony_ci int end_ip = MIN2(dst_end, src_end); 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci foreach_block(scan_block, cfg) { 124bf215546Sopenharmony_ci if (scan_block->end_ip < start_ip) 125bf215546Sopenharmony_ci continue; 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci int scan_ip = scan_block->start_ip - 1; 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci bool seen_src_write = false; 130bf215546Sopenharmony_ci bool seen_copy = false; 131bf215546Sopenharmony_ci foreach_inst_in_block(fs_inst, scan_inst, scan_block) { 132bf215546Sopenharmony_ci scan_ip++; 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_ci /* Ignore anything before the intersection of the live ranges */ 135bf215546Sopenharmony_ci if (scan_ip < start_ip) 136bf215546Sopenharmony_ci continue; 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci /* Ignore the copying instruction itself */ 139bf215546Sopenharmony_ci if (scan_inst == inst) { 140bf215546Sopenharmony_ci seen_copy = true; 141bf215546Sopenharmony_ci continue; 142bf215546Sopenharmony_ci } 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci if (scan_ip > end_ip) 145bf215546Sopenharmony_ci return true; /* registers do not interfere */ 146bf215546Sopenharmony_ci 147bf215546Sopenharmony_ci if (seen_src_write && !seen_copy) { 148bf215546Sopenharmony_ci /* In order to satisfy the guarantee of register coalescing, we 149bf215546Sopenharmony_ci * must ensure that the two registers always have the same value 150bf215546Sopenharmony_ci * during the intersection of their live ranges. One way to do 151bf215546Sopenharmony_ci * this is to simply ensure that neither is ever written apart 152bf215546Sopenharmony_ci * from the one copy which syncs up the two registers. However, 153bf215546Sopenharmony_ci * this can be overly conservative and only works in the case 154bf215546Sopenharmony_ci * where the destination live range is entirely contained in the 155bf215546Sopenharmony_ci * source live range. 156bf215546Sopenharmony_ci * 157bf215546Sopenharmony_ci * To handle the other case where the source is contained in the 158bf215546Sopenharmony_ci * destination, we allow writes to the source register as long as 159bf215546Sopenharmony_ci * they happen before the copy, in the same block as the copy, and 160bf215546Sopenharmony_ci * the destination is never read between first such write and the 161bf215546Sopenharmony_ci * copy. This effectively moves the write from the copy up. 162bf215546Sopenharmony_ci */ 163bf215546Sopenharmony_ci for (int j = 0; j < scan_inst->sources; j++) { 164bf215546Sopenharmony_ci if (regions_overlap(scan_inst->src[j], scan_inst->size_read(j), 165bf215546Sopenharmony_ci inst->dst, inst->size_written)) 166bf215546Sopenharmony_ci return false; /* registers interfere */ 167bf215546Sopenharmony_ci } 168bf215546Sopenharmony_ci } 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ci /* The MOV being coalesced had better be the only instruction which 171bf215546Sopenharmony_ci * writes to the coalesce destination in the intersection. 172bf215546Sopenharmony_ci */ 173bf215546Sopenharmony_ci if (regions_overlap(scan_inst->dst, scan_inst->size_written, 174bf215546Sopenharmony_ci inst->dst, inst->size_written)) 175bf215546Sopenharmony_ci return false; /* registers interfere */ 176bf215546Sopenharmony_ci 177bf215546Sopenharmony_ci /* See the big comment above */ 178bf215546Sopenharmony_ci if (regions_overlap(scan_inst->dst, scan_inst->size_written, 179bf215546Sopenharmony_ci inst->src[0], inst->size_read(0))) { 180bf215546Sopenharmony_ci if (seen_copy || scan_block != block) 181bf215546Sopenharmony_ci return false; 182bf215546Sopenharmony_ci seen_src_write = true; 183bf215546Sopenharmony_ci } 184bf215546Sopenharmony_ci } 185bf215546Sopenharmony_ci } 186bf215546Sopenharmony_ci 187bf215546Sopenharmony_ci return true; 188bf215546Sopenharmony_ci} 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_cibool 191bf215546Sopenharmony_cifs_visitor::register_coalesce() 192bf215546Sopenharmony_ci{ 193bf215546Sopenharmony_ci bool progress = false; 194bf215546Sopenharmony_ci fs_live_variables &live = live_analysis.require(); 195bf215546Sopenharmony_ci int src_size = 0; 196bf215546Sopenharmony_ci int channels_remaining = 0; 197bf215546Sopenharmony_ci unsigned src_reg = ~0u, dst_reg = ~0u; 198bf215546Sopenharmony_ci int dst_reg_offset[MAX_VGRF_SIZE]; 199bf215546Sopenharmony_ci fs_inst *mov[MAX_VGRF_SIZE]; 200bf215546Sopenharmony_ci int dst_var[MAX_VGRF_SIZE]; 201bf215546Sopenharmony_ci int src_var[MAX_VGRF_SIZE]; 202bf215546Sopenharmony_ci 203bf215546Sopenharmony_ci foreach_block_and_inst(block, fs_inst, inst, cfg) { 204bf215546Sopenharmony_ci if (!is_coalesce_candidate(this, inst)) 205bf215546Sopenharmony_ci continue; 206bf215546Sopenharmony_ci 207bf215546Sopenharmony_ci if (is_nop_mov(inst)) { 208bf215546Sopenharmony_ci inst->opcode = BRW_OPCODE_NOP; 209bf215546Sopenharmony_ci progress = true; 210bf215546Sopenharmony_ci continue; 211bf215546Sopenharmony_ci } 212bf215546Sopenharmony_ci 213bf215546Sopenharmony_ci if (src_reg != inst->src[0].nr) { 214bf215546Sopenharmony_ci src_reg = inst->src[0].nr; 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_ci src_size = alloc.sizes[inst->src[0].nr]; 217bf215546Sopenharmony_ci assert(src_size <= MAX_VGRF_SIZE); 218bf215546Sopenharmony_ci 219bf215546Sopenharmony_ci channels_remaining = src_size; 220bf215546Sopenharmony_ci memset(mov, 0, sizeof(mov)); 221bf215546Sopenharmony_ci 222bf215546Sopenharmony_ci dst_reg = inst->dst.nr; 223bf215546Sopenharmony_ci } 224bf215546Sopenharmony_ci 225bf215546Sopenharmony_ci if (dst_reg != inst->dst.nr) 226bf215546Sopenharmony_ci continue; 227bf215546Sopenharmony_ci 228bf215546Sopenharmony_ci if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) { 229bf215546Sopenharmony_ci for (int i = 0; i < src_size; i++) { 230bf215546Sopenharmony_ci dst_reg_offset[i] = i; 231bf215546Sopenharmony_ci } 232bf215546Sopenharmony_ci mov[0] = inst; 233bf215546Sopenharmony_ci channels_remaining -= regs_written(inst); 234bf215546Sopenharmony_ci } else { 235bf215546Sopenharmony_ci const int offset = inst->src[0].offset / REG_SIZE; 236bf215546Sopenharmony_ci if (mov[offset]) { 237bf215546Sopenharmony_ci /* This is the second time that this offset in the register has 238bf215546Sopenharmony_ci * been set. This means, in particular, that inst->dst was 239bf215546Sopenharmony_ci * live before this instruction and that the live ranges of 240bf215546Sopenharmony_ci * inst->dst and inst->src[0] overlap and we can't coalesce the 241bf215546Sopenharmony_ci * two variables. Let's ensure that doesn't happen. 242bf215546Sopenharmony_ci */ 243bf215546Sopenharmony_ci channels_remaining = -1; 244bf215546Sopenharmony_ci continue; 245bf215546Sopenharmony_ci } 246bf215546Sopenharmony_ci for (unsigned i = 0; i < MAX2(inst->size_written / REG_SIZE, 1); i++) 247bf215546Sopenharmony_ci dst_reg_offset[offset + i] = inst->dst.offset / REG_SIZE + i; 248bf215546Sopenharmony_ci mov[offset] = inst; 249bf215546Sopenharmony_ci channels_remaining -= regs_written(inst); 250bf215546Sopenharmony_ci } 251bf215546Sopenharmony_ci 252bf215546Sopenharmony_ci if (channels_remaining) 253bf215546Sopenharmony_ci continue; 254bf215546Sopenharmony_ci 255bf215546Sopenharmony_ci bool can_coalesce = true; 256bf215546Sopenharmony_ci for (int i = 0; i < src_size; i++) { 257bf215546Sopenharmony_ci if (dst_reg_offset[i] != dst_reg_offset[0] + i) { 258bf215546Sopenharmony_ci /* Registers are out-of-order. */ 259bf215546Sopenharmony_ci can_coalesce = false; 260bf215546Sopenharmony_ci src_reg = ~0u; 261bf215546Sopenharmony_ci break; 262bf215546Sopenharmony_ci } 263bf215546Sopenharmony_ci 264bf215546Sopenharmony_ci dst_var[i] = live.var_from_vgrf[dst_reg] + dst_reg_offset[i]; 265bf215546Sopenharmony_ci src_var[i] = live.var_from_vgrf[src_reg] + i; 266bf215546Sopenharmony_ci 267bf215546Sopenharmony_ci if (!can_coalesce_vars(live, cfg, block, inst, dst_var[i], src_var[i])) { 268bf215546Sopenharmony_ci can_coalesce = false; 269bf215546Sopenharmony_ci src_reg = ~0u; 270bf215546Sopenharmony_ci break; 271bf215546Sopenharmony_ci } 272bf215546Sopenharmony_ci } 273bf215546Sopenharmony_ci 274bf215546Sopenharmony_ci if (!can_coalesce) 275bf215546Sopenharmony_ci continue; 276bf215546Sopenharmony_ci 277bf215546Sopenharmony_ci progress = true; 278bf215546Sopenharmony_ci 279bf215546Sopenharmony_ci for (int i = 0; i < src_size; i++) { 280bf215546Sopenharmony_ci if (!mov[i]) 281bf215546Sopenharmony_ci continue; 282bf215546Sopenharmony_ci 283bf215546Sopenharmony_ci if (mov[i]->conditional_mod == BRW_CONDITIONAL_NONE) { 284bf215546Sopenharmony_ci mov[i]->opcode = BRW_OPCODE_NOP; 285bf215546Sopenharmony_ci mov[i]->dst = reg_undef; 286bf215546Sopenharmony_ci for (int j = 0; j < mov[i]->sources; j++) { 287bf215546Sopenharmony_ci mov[i]->src[j] = reg_undef; 288bf215546Sopenharmony_ci } 289bf215546Sopenharmony_ci } else { 290bf215546Sopenharmony_ci /* If we have a conditional modifier, rewrite the MOV to be a 291bf215546Sopenharmony_ci * MOV.cmod from the coalesced register. Hopefully, cmod 292bf215546Sopenharmony_ci * propagation will clean this up and move it to the instruction 293bf215546Sopenharmony_ci * that writes the register. If not, this keeps things correct 294bf215546Sopenharmony_ci * while still letting us coalesce. 295bf215546Sopenharmony_ci */ 296bf215546Sopenharmony_ci assert(mov[i]->opcode == BRW_OPCODE_MOV); 297bf215546Sopenharmony_ci assert(mov[i]->sources == 1); 298bf215546Sopenharmony_ci mov[i]->src[0] = mov[i]->dst; 299bf215546Sopenharmony_ci mov[i]->dst = retype(brw_null_reg(), mov[i]->dst.type); 300bf215546Sopenharmony_ci } 301bf215546Sopenharmony_ci } 302bf215546Sopenharmony_ci 303bf215546Sopenharmony_ci foreach_block_and_inst(block, fs_inst, scan_inst, cfg) { 304bf215546Sopenharmony_ci if (scan_inst->dst.file == VGRF && 305bf215546Sopenharmony_ci scan_inst->dst.nr == src_reg) { 306bf215546Sopenharmony_ci scan_inst->dst.nr = dst_reg; 307bf215546Sopenharmony_ci scan_inst->dst.offset = scan_inst->dst.offset % REG_SIZE + 308bf215546Sopenharmony_ci dst_reg_offset[scan_inst->dst.offset / REG_SIZE] * REG_SIZE; 309bf215546Sopenharmony_ci } 310bf215546Sopenharmony_ci 311bf215546Sopenharmony_ci for (int j = 0; j < scan_inst->sources; j++) { 312bf215546Sopenharmony_ci if (scan_inst->src[j].file == VGRF && 313bf215546Sopenharmony_ci scan_inst->src[j].nr == src_reg) { 314bf215546Sopenharmony_ci scan_inst->src[j].nr = dst_reg; 315bf215546Sopenharmony_ci scan_inst->src[j].offset = scan_inst->src[j].offset % REG_SIZE + 316bf215546Sopenharmony_ci dst_reg_offset[scan_inst->src[j].offset / REG_SIZE] * REG_SIZE; 317bf215546Sopenharmony_ci } 318bf215546Sopenharmony_ci } 319bf215546Sopenharmony_ci } 320bf215546Sopenharmony_ci 321bf215546Sopenharmony_ci for (int i = 0; i < src_size; i++) { 322bf215546Sopenharmony_ci live.start[dst_var[i]] = MIN2(live.start[dst_var[i]], 323bf215546Sopenharmony_ci live.start[src_var[i]]); 324bf215546Sopenharmony_ci live.end[dst_var[i]] = MAX2(live.end[dst_var[i]], 325bf215546Sopenharmony_ci live.end[src_var[i]]); 326bf215546Sopenharmony_ci } 327bf215546Sopenharmony_ci src_reg = ~0u; 328bf215546Sopenharmony_ci } 329bf215546Sopenharmony_ci 330bf215546Sopenharmony_ci if (progress) { 331bf215546Sopenharmony_ci foreach_block_and_inst_safe (block, backend_instruction, inst, cfg) { 332bf215546Sopenharmony_ci if (inst->opcode == BRW_OPCODE_NOP) { 333bf215546Sopenharmony_ci inst->remove(block, true); 334bf215546Sopenharmony_ci } 335bf215546Sopenharmony_ci } 336bf215546Sopenharmony_ci 337bf215546Sopenharmony_ci cfg->adjust_block_ips(); 338bf215546Sopenharmony_ci 339bf215546Sopenharmony_ci invalidate_analysis(DEPENDENCY_INSTRUCTIONS); 340bf215546Sopenharmony_ci } 341bf215546Sopenharmony_ci 342bf215546Sopenharmony_ci return progress; 343bf215546Sopenharmony_ci} 344