Path: blob/21.2-virgl/src/intel/compiler/brw_dead_control_flow.cpp
4550 views
/*1* Copyright © 2013 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/** @file brw_dead_control_flow.cpp24*25* This file implements the dead control flow elimination optimization pass.26*/2728#include "brw_shader.h"29#include "brw_cfg.h"3031using namespace brw;3233/* Look for and eliminate dead control flow:34*35* - if/endif36* - else in else/endif37* - then in if/else/endif38*/39bool40dead_control_flow_eliminate(backend_shader *s)41{42bool progress = false;4344foreach_block_safe (block, s->cfg) {45bblock_t *prev_block = block->prev();4647if (!prev_block)48continue;4950backend_instruction *const inst = block->start();51backend_instruction *const prev_inst = prev_block->end();5253/* ENDIF instructions, by definition, can only be found at the start of54* basic blocks.55*/56if (inst->opcode == BRW_OPCODE_ENDIF &&57prev_inst->opcode == BRW_OPCODE_ELSE) {58bblock_t *const else_block = prev_block;59backend_instruction *const else_inst = prev_inst;6061else_inst->remove(else_block);62progress = true;63} else if (inst->opcode == BRW_OPCODE_ENDIF &&64prev_inst->opcode == BRW_OPCODE_IF) {65bblock_t *const endif_block = block;66bblock_t *const if_block = prev_block;67backend_instruction *const endif_inst = inst;68backend_instruction *const if_inst = prev_inst;6970bblock_t *earlier_block = NULL, *later_block = NULL;7172if (if_block->start_ip == if_block->end_ip) {73earlier_block = if_block->prev();74} else {75earlier_block = if_block;76}77if_inst->remove(if_block);7879if (endif_block->start_ip == endif_block->end_ip) {80later_block = endif_block->next();81} else {82later_block = endif_block;83}84endif_inst->remove(endif_block);8586assert((earlier_block == NULL) == (later_block == NULL));87if (earlier_block && earlier_block->can_combine_with(later_block)) {88earlier_block->combine_with(later_block);8990/* If ENDIF was in its own block, then we've now deleted it and91* merged the two surrounding blocks, the latter of which the92* __next block pointer was pointing to.93*/94if (endif_block != later_block) {95__next = earlier_block->next();96}97}9899progress = true;100} else if (inst->opcode == BRW_OPCODE_ELSE &&101prev_inst->opcode == BRW_OPCODE_IF) {102bblock_t *const else_block = block;103backend_instruction *const if_inst = prev_inst;104backend_instruction *const else_inst = inst;105106/* Since the else-branch is becoming the new then-branch, the107* condition has to be inverted.108*/109if_inst->predicate_inverse = !if_inst->predicate_inverse;110else_inst->remove(else_block);111112progress = true;113}114}115116if (progress)117s->invalidate_analysis(DEPENDENCY_BLOCKS | DEPENDENCY_INSTRUCTIONS);118119return progress;120}121122123