1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2016 Intel Corporation 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21b8e80941Smrg * IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg#ifndef NIR_LOOP_ANALYZE_H 25b8e80941Smrg#define NIR_LOOP_ANALYZE_H 26b8e80941Smrg 27b8e80941Smrg#include "nir.h" 28b8e80941Smrg 29b8e80941Smrg/* Returns true if nir_cf_node contains a jump other than the expected_jump 30b8e80941Smrg * parameter. 31b8e80941Smrg */ 32b8e80941Smrgstatic inline bool 33b8e80941Smrgcontains_other_jump(nir_cf_node *node, nir_instr *expected_jump) 34b8e80941Smrg{ 35b8e80941Smrg switch (node->type) { 36b8e80941Smrg case nir_cf_node_block: { 37b8e80941Smrg nir_instr *lst_instr = nir_block_last_instr(nir_cf_node_as_block(node)); 38b8e80941Smrg 39b8e80941Smrg /* dead_cf should have eliminated any instruction after the first break 40b8e80941Smrg */ 41b8e80941Smrg nir_foreach_instr(instr, nir_cf_node_as_block(node)) 42b8e80941Smrg assert(instr->type != nir_instr_type_jump || instr == lst_instr); 43b8e80941Smrg 44b8e80941Smrg if (lst_instr && lst_instr->type == nir_instr_type_jump && 45b8e80941Smrg lst_instr != expected_jump) 46b8e80941Smrg return true; 47b8e80941Smrg else 48b8e80941Smrg return false; 49b8e80941Smrg } 50b8e80941Smrg case nir_cf_node_if: { 51b8e80941Smrg nir_if *if_stmt = nir_cf_node_as_if(node); 52b8e80941Smrg 53b8e80941Smrg foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->then_list) { 54b8e80941Smrg if (contains_other_jump(node, expected_jump)) 55b8e80941Smrg return true; 56b8e80941Smrg } 57b8e80941Smrg 58b8e80941Smrg foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->else_list) { 59b8e80941Smrg if (contains_other_jump(node, expected_jump)) 60b8e80941Smrg return true; 61b8e80941Smrg } 62b8e80941Smrg 63b8e80941Smrg return false; 64b8e80941Smrg } 65b8e80941Smrg case nir_cf_node_loop: 66b8e80941Smrg return true; 67b8e80941Smrg 68b8e80941Smrg default: 69b8e80941Smrg unreachable("Unhandled cf node type"); 70b8e80941Smrg } 71b8e80941Smrg} 72b8e80941Smrg 73b8e80941Smrg/* Here we define a trivial if as containing only a single break that must be 74b8e80941Smrg * located at the end of either the then or else branch of the top level if, 75b8e80941Smrg * there must be no other breaks or any other type of jump. Or we pass NULL 76b8e80941Smrg * to break_block the if must contains no jumps at all. 77b8e80941Smrg */ 78b8e80941Smrgstatic inline bool 79b8e80941Smrgnir_is_trivial_loop_if(nir_if *nif, nir_block *break_block) 80b8e80941Smrg{ 81b8e80941Smrg nir_instr *last_instr = NULL; 82b8e80941Smrg 83b8e80941Smrg if (break_block) { 84b8e80941Smrg last_instr = nir_block_last_instr(break_block); 85b8e80941Smrg assert(last_instr && last_instr->type == nir_instr_type_jump && 86b8e80941Smrg nir_instr_as_jump(last_instr)->type == nir_jump_break); 87b8e80941Smrg } 88b8e80941Smrg 89b8e80941Smrg if (contains_other_jump(&nif->cf_node, last_instr)) 90b8e80941Smrg return false; 91b8e80941Smrg 92b8e80941Smrg return true; 93b8e80941Smrg} 94b8e80941Smrg 95b8e80941Smrgstatic inline bool 96b8e80941Smrgnir_block_ends_in_break(nir_block *block) 97b8e80941Smrg{ 98b8e80941Smrg if (exec_list_is_empty(&block->instr_list)) 99b8e80941Smrg return false; 100b8e80941Smrg 101b8e80941Smrg nir_instr *instr = nir_block_last_instr(block); 102b8e80941Smrg return instr->type == nir_instr_type_jump && 103b8e80941Smrg nir_instr_as_jump(instr)->type == nir_jump_break; 104b8e80941Smrg} 105b8e80941Smrg 106b8e80941Smrg#endif /* NIR_LOOP_ANALYZE_H */ 107