Path: blob/21.2-virgl/src/panfrost/bifrost/bi_opt_dce.c
4564 views
/*1* Copyright (C) 2018 Alyssa Rosenzweig2* Copyright (C) 2019-2020 Collabora, Ltd.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "compiler.h"25#include "util/u_memory.h"2627/* A simple liveness-based dead code elimination pass. */2829void30bi_opt_dead_code_eliminate(bi_context *ctx)31{32unsigned temp_count = bi_max_temp(ctx);3334bi_invalidate_liveness(ctx);35bi_compute_liveness(ctx);3637bi_foreach_block_rev(ctx, _block) {38bi_block *block = (bi_block *) _block;39uint16_t *live = rzalloc_array(_block, uint16_t, temp_count);4041pan_foreach_successor(_block, succ) {42for (unsigned i = 0; i < temp_count; ++i)43live[i] |= succ->live_in[i];44}4546bi_foreach_instr_in_block_safe_rev(block, ins) {47bool all_null = true;4849bi_foreach_dest(ins, d) {50unsigned index = bi_get_node(ins->dest[d]);5152if (index < temp_count && !(live[index] & bi_writemask(ins, d)))53ins->dest[d] = bi_null();5455all_null &= bi_is_null(ins->dest[d]);56}5758if (all_null && !bi_side_effects(ins->op))59bi_remove_instruction(ins);60else61bi_liveness_ins_update(live, ins, temp_count);62}6364ralloc_free(block->base.live_in);65block->base.live_in = live;66}67}6869/* Post-RA liveness-based dead code analysis to clean up results of bundling */7071uint64_t72bi_postra_liveness_ins(uint64_t live, bi_instr *ins)73{74bi_foreach_dest(ins, d) {75if (ins->dest[d].type == BI_INDEX_REGISTER) {76unsigned nr = bi_count_write_registers(ins, d);77unsigned reg = ins->dest[d].value;78live &= ~(BITFIELD64_MASK(nr) << reg);79}80}8182bi_foreach_src(ins, s) {83if (ins->src[s].type == BI_INDEX_REGISTER) {84unsigned nr = bi_count_read_registers(ins, s);85unsigned reg = ins->src[s].value;86live |= (BITFIELD64_MASK(nr) << reg);87}88}8990return live;91}9293static bool94bi_postra_liveness_block(bi_block *blk)95{96pan_foreach_successor((&blk->base), _succ) {97bi_block *succ = (bi_block *) _succ;98blk->reg_live_out |= succ->reg_live_in;99}100101uint64_t live = blk->reg_live_out;102103bi_foreach_instr_in_block_rev(blk, ins)104live = bi_postra_liveness_ins(live, ins);105106bool progress = blk->reg_live_in != live;107blk->reg_live_in = live;108return progress;109}110111/* Globally, liveness analysis uses a fixed-point algorithm based on a112* worklist. We initialize a work list with the exit block. We iterate the work113* list to compute live_in from live_out for each block on the work list,114* adding the predecessors of the block to the work list if we made progress.115*/116117void118bi_postra_liveness(bi_context *ctx)119{120struct set *work_list = _mesa_set_create(NULL,121_mesa_hash_pointer,122_mesa_key_pointer_equal);123124struct set *visited = _mesa_set_create(NULL,125_mesa_hash_pointer,126_mesa_key_pointer_equal);127128struct set_entry *cur;129cur = _mesa_set_add(work_list, pan_exit_block(&ctx->blocks));130131bi_foreach_block(ctx, _block) {132bi_block *block = (bi_block *) _block;133block->reg_live_out = block->reg_live_in = 0;134}135136do {137bi_block *blk = (struct bi_block *) cur->key;138_mesa_set_remove(work_list, cur);139140/* Update its liveness information */141bool progress = bi_postra_liveness_block(blk);142143/* If we made progress, we need to process the predecessors */144145if (progress || !_mesa_set_search(visited, blk)) {146pan_foreach_predecessor((&blk->base), pred)147_mesa_set_add(work_list, pred);148}149150_mesa_set_add(visited, blk);151} while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);152153_mesa_set_destroy(visited, NULL);154_mesa_set_destroy(work_list, NULL);155}156157void158bi_opt_dce_post_ra(bi_context *ctx)159{160bi_postra_liveness(ctx);161162bi_foreach_block_rev(ctx, _block) {163bi_block *block = (bi_block *) _block;164uint64_t live = block->reg_live_out;165166bi_foreach_instr_in_block_rev(block, ins) {167bi_foreach_dest(ins, d) {168if (ins->dest[d].type != BI_INDEX_REGISTER)169continue;170171unsigned nr = bi_count_write_registers(ins, d);172unsigned reg = ins->dest[d].value;173uint64_t mask = (BITFIELD64_MASK(nr) << reg);174bool cullable = (ins->op != BI_OPCODE_BLEND);175176if (!(live & mask) && cullable)177ins->dest[d] = bi_null();178}179180live = bi_postra_liveness_ins(live, ins);181}182}183}184185186