1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2021 Valve 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 FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "ir3_ra.h" 25bf215546Sopenharmony_ci#include "ir3_shader.h" 26bf215546Sopenharmony_ci#include "ralloc.h" 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci/* A note on how phi node uses are handled: 29bf215546Sopenharmony_ci * 30bf215546Sopenharmony_ci * - Phi node sources are considered to happen after the end of the 31bf215546Sopenharmony_ci * predecessor block, so the live_out for that block contains phi sources. 32bf215546Sopenharmony_ci * - On the other hand, phi destinations are considered to happen at the start 33bf215546Sopenharmony_ci * of the block, so that live_in does *not* contain phi destinations. This 34bf215546Sopenharmony_ci * is mainly because phi destinations and live-through values have to be 35bf215546Sopenharmony_ci * treated very differently by RA at the beginning of a block. 36bf215546Sopenharmony_ci */ 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_cistatic bool 39bf215546Sopenharmony_cicompute_block_liveness(struct ir3_liveness *live, struct ir3_block *block, 40bf215546Sopenharmony_ci BITSET_WORD *tmp_live, unsigned bitset_words) 41bf215546Sopenharmony_ci{ 42bf215546Sopenharmony_ci memcpy(tmp_live, live->live_out[block->index], 43bf215546Sopenharmony_ci bitset_words * sizeof(BITSET_WORD)); 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci /* Process instructions */ 46bf215546Sopenharmony_ci foreach_instr_rev (instr, &block->instr_list) { 47bf215546Sopenharmony_ci ra_foreach_dst (dst, instr) { 48bf215546Sopenharmony_ci if (BITSET_TEST(tmp_live, dst->name)) 49bf215546Sopenharmony_ci dst->flags &= ~IR3_REG_UNUSED; 50bf215546Sopenharmony_ci else 51bf215546Sopenharmony_ci dst->flags |= IR3_REG_UNUSED; 52bf215546Sopenharmony_ci BITSET_CLEAR(tmp_live, dst->name); 53bf215546Sopenharmony_ci } 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci /* Phi node uses occur after the predecessor block */ 56bf215546Sopenharmony_ci if (instr->opc != OPC_META_PHI) { 57bf215546Sopenharmony_ci ra_foreach_src (src, instr) { 58bf215546Sopenharmony_ci if (BITSET_TEST(tmp_live, src->def->name)) 59bf215546Sopenharmony_ci src->flags &= ~IR3_REG_KILL; 60bf215546Sopenharmony_ci else 61bf215546Sopenharmony_ci src->flags |= IR3_REG_KILL; 62bf215546Sopenharmony_ci } 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci ra_foreach_src (src, instr) { 65bf215546Sopenharmony_ci if (BITSET_TEST(tmp_live, src->def->name)) 66bf215546Sopenharmony_ci src->flags &= ~IR3_REG_FIRST_KILL; 67bf215546Sopenharmony_ci else 68bf215546Sopenharmony_ci src->flags |= IR3_REG_FIRST_KILL; 69bf215546Sopenharmony_ci BITSET_SET(tmp_live, src->def->name); 70bf215546Sopenharmony_ci } 71bf215546Sopenharmony_ci } 72bf215546Sopenharmony_ci } 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci memcpy(live->live_in[block->index], tmp_live, 75bf215546Sopenharmony_ci bitset_words * sizeof(BITSET_WORD)); 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci bool progress = false; 78bf215546Sopenharmony_ci for (unsigned i = 0; i < block->predecessors_count; i++) { 79bf215546Sopenharmony_ci const struct ir3_block *pred = block->predecessors[i]; 80bf215546Sopenharmony_ci for (unsigned j = 0; j < bitset_words; j++) { 81bf215546Sopenharmony_ci if (tmp_live[j] & ~live->live_out[pred->index][j]) 82bf215546Sopenharmony_ci progress = true; 83bf215546Sopenharmony_ci live->live_out[pred->index][j] |= tmp_live[j]; 84bf215546Sopenharmony_ci } 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci /* Process phi sources. */ 87bf215546Sopenharmony_ci foreach_instr (phi, &block->instr_list) { 88bf215546Sopenharmony_ci if (phi->opc != OPC_META_PHI) 89bf215546Sopenharmony_ci break; 90bf215546Sopenharmony_ci if (!phi->srcs[i]->def) 91bf215546Sopenharmony_ci continue; 92bf215546Sopenharmony_ci unsigned name = phi->srcs[i]->def->name; 93bf215546Sopenharmony_ci if (!BITSET_TEST(live->live_out[pred->index], name)) { 94bf215546Sopenharmony_ci progress = true; 95bf215546Sopenharmony_ci BITSET_SET(live->live_out[pred->index], name); 96bf215546Sopenharmony_ci } 97bf215546Sopenharmony_ci } 98bf215546Sopenharmony_ci } 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci for (unsigned i = 0; i < block->physical_predecessors_count; i++) { 101bf215546Sopenharmony_ci const struct ir3_block *pred = block->physical_predecessors[i]; 102bf215546Sopenharmony_ci unsigned name; 103bf215546Sopenharmony_ci BITSET_FOREACH_SET (name, tmp_live, live->definitions_count) { 104bf215546Sopenharmony_ci struct ir3_register *reg = live->definitions[name]; 105bf215546Sopenharmony_ci if (!(reg->flags & IR3_REG_SHARED)) 106bf215546Sopenharmony_ci continue; 107bf215546Sopenharmony_ci if (!BITSET_TEST(live->live_out[pred->index], name)) { 108bf215546Sopenharmony_ci progress = true; 109bf215546Sopenharmony_ci BITSET_SET(live->live_out[pred->index], name); 110bf215546Sopenharmony_ci } 111bf215546Sopenharmony_ci } 112bf215546Sopenharmony_ci } 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci return progress; 115bf215546Sopenharmony_ci} 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_cistruct ir3_liveness * 118bf215546Sopenharmony_ciir3_calc_liveness(void *mem_ctx, struct ir3 *ir) 119bf215546Sopenharmony_ci{ 120bf215546Sopenharmony_ci struct ir3_liveness *live = rzalloc(mem_ctx, struct ir3_liveness); 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci /* Reserve name 0 to mean "doesn't have a name yet" to make the debug 123bf215546Sopenharmony_ci * output nicer. 124bf215546Sopenharmony_ci */ 125bf215546Sopenharmony_ci array_insert(live, live->definitions, NULL); 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci /* Build definition <-> name mapping */ 128bf215546Sopenharmony_ci unsigned block_count = 0; 129bf215546Sopenharmony_ci foreach_block (block, &ir->block_list) { 130bf215546Sopenharmony_ci block->index = block_count++; 131bf215546Sopenharmony_ci foreach_instr (instr, &block->instr_list) { 132bf215546Sopenharmony_ci ra_foreach_dst (dst, instr) { 133bf215546Sopenharmony_ci dst->name = live->definitions_count; 134bf215546Sopenharmony_ci array_insert(live, live->definitions, dst); 135bf215546Sopenharmony_ci } 136bf215546Sopenharmony_ci } 137bf215546Sopenharmony_ci } 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci live->block_count = block_count; 140bf215546Sopenharmony_ci 141bf215546Sopenharmony_ci unsigned bitset_words = BITSET_WORDS(live->definitions_count); 142bf215546Sopenharmony_ci BITSET_WORD *tmp_live = ralloc_array(live, BITSET_WORD, bitset_words); 143bf215546Sopenharmony_ci live->live_in = ralloc_array(live, BITSET_WORD *, block_count); 144bf215546Sopenharmony_ci live->live_out = ralloc_array(live, BITSET_WORD *, block_count); 145bf215546Sopenharmony_ci unsigned i = 0; 146bf215546Sopenharmony_ci foreach_block (block, &ir->block_list) { 147bf215546Sopenharmony_ci block->index = i++; 148bf215546Sopenharmony_ci live->live_in[block->index] = 149bf215546Sopenharmony_ci rzalloc_array(live, BITSET_WORD, bitset_words); 150bf215546Sopenharmony_ci live->live_out[block->index] = 151bf215546Sopenharmony_ci rzalloc_array(live, BITSET_WORD, bitset_words); 152bf215546Sopenharmony_ci } 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci bool progress = true; 155bf215546Sopenharmony_ci while (progress) { 156bf215546Sopenharmony_ci progress = false; 157bf215546Sopenharmony_ci foreach_block_rev (block, &ir->block_list) { 158bf215546Sopenharmony_ci progress |= 159bf215546Sopenharmony_ci compute_block_liveness(live, block, tmp_live, bitset_words); 160bf215546Sopenharmony_ci } 161bf215546Sopenharmony_ci } 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci return live; 164bf215546Sopenharmony_ci} 165bf215546Sopenharmony_ci 166bf215546Sopenharmony_ci/* Return true if "def" is live after "instr". It's assumed that "def" 167bf215546Sopenharmony_ci * dominates "instr". 168bf215546Sopenharmony_ci */ 169bf215546Sopenharmony_cibool 170bf215546Sopenharmony_ciir3_def_live_after(struct ir3_liveness *live, struct ir3_register *def, 171bf215546Sopenharmony_ci struct ir3_instruction *instr) 172bf215546Sopenharmony_ci{ 173bf215546Sopenharmony_ci /* If it's live out then it's definitely live at the instruction. */ 174bf215546Sopenharmony_ci if (BITSET_TEST(live->live_out[instr->block->index], def->name)) 175bf215546Sopenharmony_ci return true; 176bf215546Sopenharmony_ci 177bf215546Sopenharmony_ci /* If it's not live in and not defined in the same block then the live 178bf215546Sopenharmony_ci * range can't extend to the instruction. 179bf215546Sopenharmony_ci */ 180bf215546Sopenharmony_ci if (def->instr->block != instr->block && 181bf215546Sopenharmony_ci !BITSET_TEST(live->live_in[instr->block->index], def->name)) 182bf215546Sopenharmony_ci return false; 183bf215546Sopenharmony_ci 184bf215546Sopenharmony_ci /* Ok, now comes the tricky case, where "def" is killed somewhere in 185bf215546Sopenharmony_ci * "instr"'s block and we have to check if it's before or after. 186bf215546Sopenharmony_ci */ 187bf215546Sopenharmony_ci foreach_instr_rev (test_instr, &instr->block->instr_list) { 188bf215546Sopenharmony_ci if (test_instr == instr) 189bf215546Sopenharmony_ci break; 190bf215546Sopenharmony_ci 191bf215546Sopenharmony_ci for (unsigned i = 0; i < test_instr->srcs_count; i++) { 192bf215546Sopenharmony_ci if (test_instr->srcs[i]->def == def) 193bf215546Sopenharmony_ci return true; 194bf215546Sopenharmony_ci } 195bf215546Sopenharmony_ci } 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci return false; 198bf215546Sopenharmony_ci} 199