17ec681f3Smrg/* 27ec681f3Smrg * Copyright (C) 2020 Collabora, Ltd. 37ec681f3Smrg * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io> 47ec681f3Smrg * 57ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 67ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 77ec681f3Smrg * to deal in the Software without restriction, including without limitation 87ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 97ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 107ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 117ec681f3Smrg * 127ec681f3Smrg * The above copyright notice and this permission notice (including the next 137ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 147ec681f3Smrg * Software. 157ec681f3Smrg * 167ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 177ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 187ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 197ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 207ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 217ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 227ec681f3Smrg * SOFTWARE. 237ec681f3Smrg */ 247ec681f3Smrg 257ec681f3Smrg#include "compiler.h" 267ec681f3Smrg#include "util/u_memory.h" 277ec681f3Smrg#include "util/list.h" 287ec681f3Smrg#include "util/set.h" 297ec681f3Smrg 307ec681f3Smrg/* Liveness analysis is a backwards-may dataflow analysis pass. Within a block, 317ec681f3Smrg * we compute live_out from live_in. The intrablock pass is linear-time. It 327ec681f3Smrg * returns whether progress was made. */ 337ec681f3Smrg 347ec681f3Smrgvoid 357ec681f3Smrgbi_liveness_ins_update(uint8_t *live, bi_instr *ins, unsigned max) 367ec681f3Smrg{ 377ec681f3Smrg /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */ 387ec681f3Smrg 397ec681f3Smrg bi_foreach_dest(ins, d) { 407ec681f3Smrg unsigned node = bi_get_node(ins->dest[d]); 417ec681f3Smrg 427ec681f3Smrg if (node < max) 437ec681f3Smrg live[node] &= ~bi_writemask(ins, d); 447ec681f3Smrg } 457ec681f3Smrg 467ec681f3Smrg bi_foreach_src(ins, src) { 477ec681f3Smrg unsigned count = bi_count_read_registers(ins, src); 487ec681f3Smrg unsigned rmask = BITFIELD_MASK(count); 497ec681f3Smrg uint8_t mask = (rmask << ins->src[src].offset); 507ec681f3Smrg 517ec681f3Smrg unsigned node = bi_get_node(ins->src[src]); 527ec681f3Smrg if (node < max) 537ec681f3Smrg live[node] |= mask; 547ec681f3Smrg } 557ec681f3Smrg} 567ec681f3Smrg 577ec681f3Smrgstatic bool 587ec681f3Smrgliveness_block_update(bi_block *blk, unsigned temp_count) 597ec681f3Smrg{ 607ec681f3Smrg bool progress = false; 617ec681f3Smrg 627ec681f3Smrg /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */ 637ec681f3Smrg bi_foreach_successor(blk, succ) { 647ec681f3Smrg for (unsigned i = 0; i < temp_count; ++i) 657ec681f3Smrg blk->live_out[i] |= succ->live_in[i]; 667ec681f3Smrg } 677ec681f3Smrg 687ec681f3Smrg uint8_t *live = ralloc_array(blk, uint8_t, temp_count); 697ec681f3Smrg memcpy(live, blk->live_out, temp_count); 707ec681f3Smrg 717ec681f3Smrg bi_foreach_instr_in_block_rev(blk, ins) 727ec681f3Smrg bi_liveness_ins_update(live, (bi_instr *) ins, temp_count); 737ec681f3Smrg 747ec681f3Smrg /* To figure out progress, diff live_in */ 757ec681f3Smrg 767ec681f3Smrg for (unsigned i = 0; (i < temp_count) && !progress; ++i) 777ec681f3Smrg progress |= (blk->live_in[i] != live[i]); 787ec681f3Smrg 797ec681f3Smrg ralloc_free(blk->live_in); 807ec681f3Smrg blk->live_in = live; 817ec681f3Smrg 827ec681f3Smrg return progress; 837ec681f3Smrg} 847ec681f3Smrg 857ec681f3Smrg/* Globally, liveness analysis uses a fixed-point algorithm based on a 867ec681f3Smrg * worklist. We initialize a work list with the exit block. We iterate the work 877ec681f3Smrg * list to compute live_in from live_out for each block on the work list, 887ec681f3Smrg * adding the predecessors of the block to the work list if we made progress. 897ec681f3Smrg */ 907ec681f3Smrg 917ec681f3Smrgvoid 927ec681f3Smrgbi_compute_liveness(bi_context *ctx) 937ec681f3Smrg{ 947ec681f3Smrg if (ctx->has_liveness) 957ec681f3Smrg return; 967ec681f3Smrg 977ec681f3Smrg unsigned temp_count = bi_max_temp(ctx); 987ec681f3Smrg 997ec681f3Smrg /* Set of bi_block */ 1007ec681f3Smrg struct set *work_list = _mesa_set_create(NULL, 1017ec681f3Smrg _mesa_hash_pointer, 1027ec681f3Smrg _mesa_key_pointer_equal); 1037ec681f3Smrg 1047ec681f3Smrg struct set *visited = _mesa_set_create(NULL, 1057ec681f3Smrg _mesa_hash_pointer, 1067ec681f3Smrg _mesa_key_pointer_equal); 1077ec681f3Smrg 1087ec681f3Smrg list_for_each_entry(bi_block, block, &ctx->blocks, link) { 1097ec681f3Smrg if (block->live_in) 1107ec681f3Smrg ralloc_free(block->live_in); 1117ec681f3Smrg 1127ec681f3Smrg if (block->live_out) 1137ec681f3Smrg ralloc_free(block->live_out); 1147ec681f3Smrg 1157ec681f3Smrg block->live_in = rzalloc_array(block, uint8_t, temp_count); 1167ec681f3Smrg block->live_out = rzalloc_array(block, uint8_t, temp_count); 1177ec681f3Smrg } 1187ec681f3Smrg 1197ec681f3Smrg /* Initialize the work list with the exit block */ 1207ec681f3Smrg struct set_entry *cur; 1217ec681f3Smrg 1227ec681f3Smrg cur = _mesa_set_add(work_list, pan_exit_block(&ctx->blocks)); 1237ec681f3Smrg 1247ec681f3Smrg /* Iterate the work list */ 1257ec681f3Smrg 1267ec681f3Smrg do { 1277ec681f3Smrg /* Pop off a block */ 1287ec681f3Smrg bi_block *blk = (struct bi_block *) cur->key; 1297ec681f3Smrg _mesa_set_remove(work_list, cur); 1307ec681f3Smrg 1317ec681f3Smrg /* Update its liveness information */ 1327ec681f3Smrg bool progress = liveness_block_update(blk, temp_count); 1337ec681f3Smrg 1347ec681f3Smrg /* If we made progress, we need to process the predecessors */ 1357ec681f3Smrg 1367ec681f3Smrg if (progress || !_mesa_set_search(visited, blk)) { 1377ec681f3Smrg bi_foreach_predecessor(blk, pred) 1387ec681f3Smrg _mesa_set_add(work_list, pred); 1397ec681f3Smrg } 1407ec681f3Smrg 1417ec681f3Smrg _mesa_set_add(visited, blk); 1427ec681f3Smrg } while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL); 1437ec681f3Smrg 1447ec681f3Smrg _mesa_set_destroy(visited, NULL); 1457ec681f3Smrg _mesa_set_destroy(work_list, NULL); 1467ec681f3Smrg 1477ec681f3Smrg ctx->has_liveness = true; 1487ec681f3Smrg} 1497ec681f3Smrg 1507ec681f3Smrg/* Once liveness data is no longer valid, call this */ 1517ec681f3Smrg 1527ec681f3Smrgvoid 1537ec681f3Smrgbi_invalidate_liveness(bi_context *ctx) 1547ec681f3Smrg{ 1557ec681f3Smrg ctx->has_liveness = false; 1567ec681f3Smrg} 157