1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2016 Intel 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 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "nir.h" 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_cistatic bool 27bf215546Sopenharmony_ciinstr_is_continue(nir_instr *instr) 28bf215546Sopenharmony_ci{ 29bf215546Sopenharmony_ci if (instr->type != nir_instr_type_jump) 30bf215546Sopenharmony_ci return false; 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci return nir_instr_as_jump(instr)->type == nir_jump_continue; 33bf215546Sopenharmony_ci} 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_cistatic bool 36bf215546Sopenharmony_cilower_trivial_continues_block(nir_block *block, nir_loop *loop) 37bf215546Sopenharmony_ci{ 38bf215546Sopenharmony_ci bool progress = false; 39bf215546Sopenharmony_ci nir_instr *first_instr = nir_block_first_instr(block); 40bf215546Sopenharmony_ci if (!first_instr || instr_is_continue(first_instr)) { 41bf215546Sopenharmony_ci /* The block contains only a continue if anything */ 42bf215546Sopenharmony_ci nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_node); 43bf215546Sopenharmony_ci if (prev_node && prev_node->type == nir_cf_node_if) { 44bf215546Sopenharmony_ci nir_if *prev_if = nir_cf_node_as_if(prev_node); 45bf215546Sopenharmony_ci progress |= lower_trivial_continues_block( 46bf215546Sopenharmony_ci nir_if_last_then_block(prev_if), loop); 47bf215546Sopenharmony_ci progress |= lower_trivial_continues_block( 48bf215546Sopenharmony_ci nir_if_last_else_block(prev_if), loop); 49bf215546Sopenharmony_ci } 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ci /* The lower_phis_to_regs helper is smart enough to push phi sources as 52bf215546Sopenharmony_ci * far up if-ladders as it possibly can. In other words, the recursive 53bf215546Sopenharmony_ci * calls above shouldn't be adding instructions to this block since it 54bf215546Sopenharmony_ci * follows an if statement. 55bf215546Sopenharmony_ci */ 56bf215546Sopenharmony_ci first_instr = nir_block_first_instr(block); 57bf215546Sopenharmony_ci assert(!first_instr || instr_is_continue(first_instr)); 58bf215546Sopenharmony_ci } 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_ci nir_instr *last_instr = nir_block_last_instr(block); 61bf215546Sopenharmony_ci if (!last_instr || !instr_is_continue(last_instr)) 62bf215546Sopenharmony_ci return progress; 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci /* We're at the end of a block that goes straight through to the end of the 65bf215546Sopenharmony_ci * loop and continues to the top. We can exit normally instead of jump. 66bf215546Sopenharmony_ci */ 67bf215546Sopenharmony_ci nir_lower_phis_to_regs_block(nir_loop_first_block(loop)); 68bf215546Sopenharmony_ci nir_instr_remove(last_instr); 69bf215546Sopenharmony_ci return true; 70bf215546Sopenharmony_ci} 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_cistatic bool 73bf215546Sopenharmony_cilower_trivial_continues_list(struct exec_list *cf_list, 74bf215546Sopenharmony_ci bool list_ends_at_loop_tail, 75bf215546Sopenharmony_ci nir_loop *loop) 76bf215546Sopenharmony_ci{ 77bf215546Sopenharmony_ci bool progress = false; 78bf215546Sopenharmony_ci foreach_list_typed(nir_cf_node, cf_node, node, cf_list) { 79bf215546Sopenharmony_ci bool at_loop_tail = list_ends_at_loop_tail && 80bf215546Sopenharmony_ci &cf_node->node == exec_list_get_tail(cf_list); 81bf215546Sopenharmony_ci switch (cf_node->type) { 82bf215546Sopenharmony_ci case nir_cf_node_block: 83bf215546Sopenharmony_ci break; 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci case nir_cf_node_if: { 86bf215546Sopenharmony_ci nir_if *nif = nir_cf_node_as_if(cf_node); 87bf215546Sopenharmony_ci if (lower_trivial_continues_list(&nif->then_list, at_loop_tail, loop)) 88bf215546Sopenharmony_ci progress = true; 89bf215546Sopenharmony_ci if (lower_trivial_continues_list(&nif->else_list, at_loop_tail, loop)) 90bf215546Sopenharmony_ci progress = true; 91bf215546Sopenharmony_ci break; 92bf215546Sopenharmony_ci } 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ci case nir_cf_node_loop: { 95bf215546Sopenharmony_ci nir_loop *loop = nir_cf_node_as_loop(cf_node); 96bf215546Sopenharmony_ci if (lower_trivial_continues_list(&loop->body, true, loop)) 97bf215546Sopenharmony_ci progress = true; 98bf215546Sopenharmony_ci if (lower_trivial_continues_block(nir_loop_last_block(loop), loop)) 99bf215546Sopenharmony_ci progress = true; 100bf215546Sopenharmony_ci break; 101bf215546Sopenharmony_ci } 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci case nir_cf_node_function: 104bf215546Sopenharmony_ci unreachable("Invalid cf type"); 105bf215546Sopenharmony_ci } 106bf215546Sopenharmony_ci } 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci return progress; 109bf215546Sopenharmony_ci} 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci/** 112bf215546Sopenharmony_ci * This simple pass gets rid of any "trivial" continues, i.e. those that are 113bf215546Sopenharmony_ci * at the tail of a loop where we can just delete the continue instruction and 114bf215546Sopenharmony_ci * control-flow will naturally and harmlessly take us back to the top of the 115bf215546Sopenharmony_ci * loop. 116bf215546Sopenharmony_ci */ 117bf215546Sopenharmony_cibool 118bf215546Sopenharmony_cinir_opt_trivial_continues(nir_shader *shader) 119bf215546Sopenharmony_ci{ 120bf215546Sopenharmony_ci bool progress = false; 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci nir_foreach_function(function, shader) { 123bf215546Sopenharmony_ci if (function->impl == NULL) 124bf215546Sopenharmony_ci continue; 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci /* First we run the simple pass to get rid of pesky continues */ 127bf215546Sopenharmony_ci if (lower_trivial_continues_list(&function->impl->body, false, NULL)) { 128bf215546Sopenharmony_ci nir_metadata_preserve(function->impl, nir_metadata_none); 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci /* If that made progress, we're no longer really in SSA form. */ 131bf215546Sopenharmony_ci nir_lower_regs_to_ssa_impl(function->impl); 132bf215546Sopenharmony_ci progress = true; 133bf215546Sopenharmony_ci } else { 134bf215546Sopenharmony_ci nir_metadata_preserve(function->impl, nir_metadata_all); 135bf215546Sopenharmony_ci } 136bf215546Sopenharmony_ci } 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci return progress; 139bf215546Sopenharmony_ci} 140