17ec681f3Smrg/* 27ec681f3Smrg * Copyright © 2020 Google, Inc. 37ec681f3Smrg * 47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 57ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 67ec681f3Smrg * to deal in the Software without restriction, including without limitation 77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 97ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 107ec681f3Smrg * 117ec681f3Smrg * The above copyright notice and this permission notice (including the next 127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 137ec681f3Smrg * Software. 147ec681f3Smrg * 157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 217ec681f3Smrg * SOFTWARE. 227ec681f3Smrg */ 237ec681f3Smrg 247ec681f3Smrg#include "util/ralloc.h" 257ec681f3Smrg#include "util/u_dynarray.h" 267ec681f3Smrg 277ec681f3Smrg#include "ir3.h" 287ec681f3Smrg 297ec681f3Smrg/** 307ec681f3Smrg * A bit more extra cleanup after sched pass. In particular, prior to 317ec681f3Smrg * instruction scheduling, we can't easily eliminate unneeded mov's 327ec681f3Smrg * from "arrays", because we don't yet know if there is an intervening 337ec681f3Smrg * array-write scheduled before the use of the array-read. 347ec681f3Smrg * 357ec681f3Smrg * NOTE array is equivalent to nir "registers".. ie. it can be length of 367ec681f3Smrg * one. It is basically anything that is not SSA. 377ec681f3Smrg */ 387ec681f3Smrg 397ec681f3Smrg/** 407ec681f3Smrg * Check if any instruction before `use` and after `src` writes to the 417ec681f3Smrg * specified array. If `offset` is negative, it is a relative (a0.x) 427ec681f3Smrg * access and we care about all writes to the array (as we don't know 437ec681f3Smrg * which array element is read). Otherwise in the case of non-relative 447ec681f3Smrg * access, we only have to care about the write to the specified (>= 0) 457ec681f3Smrg * offset. In this case, we update `def` to point to the last write in 467ec681f3Smrg * between `use` and `src` to the same array, so that `use` points to 477ec681f3Smrg * the correct array write. 487ec681f3Smrg */ 497ec681f3Smrgstatic bool 507ec681f3Smrghas_conflicting_write(struct ir3_instruction *src, struct ir3_instruction *use, 517ec681f3Smrg struct ir3_register **def, unsigned id, int offset) 527ec681f3Smrg{ 537ec681f3Smrg assert(src->block == use->block); 547ec681f3Smrg bool last_write = true; 557ec681f3Smrg 567ec681f3Smrg /* NOTE that since src and use are in the same block, src by 577ec681f3Smrg * definition appears in the block's instr_list before use: 587ec681f3Smrg */ 597ec681f3Smrg foreach_instr_rev (instr, &use->node) { 607ec681f3Smrg if (instr == src) 617ec681f3Smrg break; 627ec681f3Smrg 637ec681f3Smrg /* if we are looking at a RELATIV read, we can't move 647ec681f3Smrg * it past an a0.x write: 657ec681f3Smrg */ 667ec681f3Smrg if ((offset < 0) && (dest_regs(instr) > 0) && 677ec681f3Smrg (instr->dsts[0]->num == regid(REG_A0, 0))) 687ec681f3Smrg return true; 697ec681f3Smrg 707ec681f3Smrg if (!writes_gpr(instr)) 717ec681f3Smrg continue; 727ec681f3Smrg 737ec681f3Smrg struct ir3_register *dst = instr->dsts[0]; 747ec681f3Smrg if (!(dst->flags & IR3_REG_ARRAY)) 757ec681f3Smrg continue; 767ec681f3Smrg 777ec681f3Smrg if (dst->array.id != id) 787ec681f3Smrg continue; 797ec681f3Smrg 807ec681f3Smrg /* 817ec681f3Smrg * At this point, we have narrowed down an instruction 827ec681f3Smrg * that writes to the same array.. check if it the write 837ec681f3Smrg * is to an array element that we care about: 847ec681f3Smrg */ 857ec681f3Smrg 867ec681f3Smrg /* is write to an unknown array element? */ 877ec681f3Smrg if (dst->flags & IR3_REG_RELATIV) 887ec681f3Smrg return true; 897ec681f3Smrg 907ec681f3Smrg /* is read from an unknown array element? */ 917ec681f3Smrg if (offset < 0) 927ec681f3Smrg return true; 937ec681f3Smrg 947ec681f3Smrg /* is write to same array element? */ 957ec681f3Smrg if (dst->array.offset == offset) 967ec681f3Smrg return true; 977ec681f3Smrg 987ec681f3Smrg if (last_write) 997ec681f3Smrg *def = dst; 1007ec681f3Smrg 1017ec681f3Smrg last_write = false; 1027ec681f3Smrg } 1037ec681f3Smrg 1047ec681f3Smrg return false; 1057ec681f3Smrg} 1067ec681f3Smrg 1077ec681f3Smrg/* Can we fold the mov src into use without invalid flags? */ 1087ec681f3Smrgstatic bool 1097ec681f3Smrgvalid_flags(struct ir3_instruction *use, struct ir3_instruction *mov) 1107ec681f3Smrg{ 1117ec681f3Smrg struct ir3_register *src = mov->srcs[0]; 1127ec681f3Smrg 1137ec681f3Smrg foreach_src_n (reg, n, use) { 1147ec681f3Smrg if (ssa(reg) != mov) 1157ec681f3Smrg continue; 1167ec681f3Smrg 1177ec681f3Smrg if (!ir3_valid_flags(use, n, reg->flags | src->flags)) 1187ec681f3Smrg return false; 1197ec681f3Smrg } 1207ec681f3Smrg 1217ec681f3Smrg return true; 1227ec681f3Smrg} 1237ec681f3Smrg 1247ec681f3Smrgstatic bool 1257ec681f3Smrginstr_cp_postsched(struct ir3_instruction *mov) 1267ec681f3Smrg{ 1277ec681f3Smrg struct ir3_register *src = mov->srcs[0]; 1287ec681f3Smrg 1297ec681f3Smrg /* only consider mov's from "arrays", other cases we have 1307ec681f3Smrg * already considered already: 1317ec681f3Smrg */ 1327ec681f3Smrg if (!(src->flags & IR3_REG_ARRAY)) 1337ec681f3Smrg return false; 1347ec681f3Smrg 1357ec681f3Smrg int offset = (src->flags & IR3_REG_RELATIV) ? -1 : src->array.offset; 1367ec681f3Smrg 1377ec681f3Smrg /* Once we move the array read directly into the consuming 1387ec681f3Smrg * instruction(s), we will also need to update instructions 1397ec681f3Smrg * that had a false-dep on the original mov to have deps 1407ec681f3Smrg * on the consuming instructions: 1417ec681f3Smrg */ 1427ec681f3Smrg struct util_dynarray newdeps; 1437ec681f3Smrg util_dynarray_init(&newdeps, mov->uses); 1447ec681f3Smrg 1457ec681f3Smrg foreach_ssa_use (use, mov) { 1467ec681f3Smrg if (use->block != mov->block) 1477ec681f3Smrg continue; 1487ec681f3Smrg 1497ec681f3Smrg if (is_meta(use)) 1507ec681f3Smrg continue; 1517ec681f3Smrg 1527ec681f3Smrg struct ir3_register *def = src->def; 1537ec681f3Smrg if (has_conflicting_write(mov, use, &def, src->array.id, offset)) 1547ec681f3Smrg continue; 1557ec681f3Smrg 1567ec681f3Smrg if (conflicts(mov->address, use->address)) 1577ec681f3Smrg continue; 1587ec681f3Smrg 1597ec681f3Smrg if (!valid_flags(use, mov)) 1607ec681f3Smrg continue; 1617ec681f3Smrg 1627ec681f3Smrg /* Ok, we've established that it is safe to remove this copy: */ 1637ec681f3Smrg 1647ec681f3Smrg bool removed = false; 1657ec681f3Smrg foreach_src_n (reg, n, use) { 1667ec681f3Smrg if (ssa(reg) != mov) 1677ec681f3Smrg continue; 1687ec681f3Smrg 1697ec681f3Smrg use->srcs[n] = ir3_reg_clone(mov->block->shader, src); 1707ec681f3Smrg 1717ec681f3Smrg /* preserve (abs)/etc modifiers: */ 1727ec681f3Smrg use->srcs[n]->flags |= reg->flags; 1737ec681f3Smrg 1747ec681f3Smrg /* If we're sinking the array read past any writes, make 1757ec681f3Smrg * sure to update it to point to the new previous write: 1767ec681f3Smrg */ 1777ec681f3Smrg use->srcs[n]->def = def; 1787ec681f3Smrg 1797ec681f3Smrg removed = true; 1807ec681f3Smrg } 1817ec681f3Smrg 1827ec681f3Smrg /* the use could have been only a false-dep, only add to the newdeps 1837ec681f3Smrg * array and update the address if we've actually updated a real src 1847ec681f3Smrg * reg for the use: 1857ec681f3Smrg */ 1867ec681f3Smrg if (removed) { 1877ec681f3Smrg if (src->flags & IR3_REG_RELATIV) 1887ec681f3Smrg ir3_instr_set_address(use, mov->address->def->instr); 1897ec681f3Smrg 1907ec681f3Smrg util_dynarray_append(&newdeps, struct ir3_instruction *, use); 1917ec681f3Smrg 1927ec681f3Smrg /* Remove the use from the src instruction: */ 1937ec681f3Smrg _mesa_set_remove_key(mov->uses, use); 1947ec681f3Smrg } 1957ec681f3Smrg } 1967ec681f3Smrg 1977ec681f3Smrg /* Once we have the complete set of instruction(s) that are are now 1987ec681f3Smrg * directly reading from the array, update any false-dep uses to 1997ec681f3Smrg * now depend on these instructions. The only remaining uses at 2007ec681f3Smrg * this point should be false-deps: 2017ec681f3Smrg */ 2027ec681f3Smrg foreach_ssa_use (use, mov) { 2037ec681f3Smrg util_dynarray_foreach (&newdeps, struct ir3_instruction *, instrp) { 2047ec681f3Smrg struct ir3_instruction *newdep = *instrp; 2057ec681f3Smrg ir3_instr_add_dep(use, newdep); 2067ec681f3Smrg } 2077ec681f3Smrg } 2087ec681f3Smrg 2097ec681f3Smrg return util_dynarray_num_elements(&newdeps, struct ir3_instruction **) > 0; 2107ec681f3Smrg} 2117ec681f3Smrg 2127ec681f3Smrgbool 2137ec681f3Smrgir3_cp_postsched(struct ir3 *ir) 2147ec681f3Smrg{ 2157ec681f3Smrg void *mem_ctx = ralloc_context(NULL); 2167ec681f3Smrg bool progress = false; 2177ec681f3Smrg 2187ec681f3Smrg ir3_find_ssa_uses(ir, mem_ctx, false); 2197ec681f3Smrg 2207ec681f3Smrg foreach_block (block, &ir->block_list) { 2217ec681f3Smrg foreach_instr_safe (instr, &block->instr_list) { 2227ec681f3Smrg if (is_same_type_mov(instr)) 2237ec681f3Smrg progress |= instr_cp_postsched(instr); 2247ec681f3Smrg } 2257ec681f3Smrg } 2267ec681f3Smrg 2277ec681f3Smrg ralloc_free(mem_ctx); 2287ec681f3Smrg 2297ec681f3Smrg return progress; 2307ec681f3Smrg} 231