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#ifndef NIR_LOOP_ANALYZE_H 25bf215546Sopenharmony_ci#define NIR_LOOP_ANALYZE_H 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include "nir.h" 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci/* Returns true if nir_cf_node contains a jump other than the expected_jump 30bf215546Sopenharmony_ci * parameter. 31bf215546Sopenharmony_ci */ 32bf215546Sopenharmony_cistatic inline bool 33bf215546Sopenharmony_cicontains_other_jump(nir_cf_node *node, nir_instr *expected_jump) 34bf215546Sopenharmony_ci{ 35bf215546Sopenharmony_ci switch (node->type) { 36bf215546Sopenharmony_ci case nir_cf_node_block: { 37bf215546Sopenharmony_ci nir_instr *lst_instr = nir_block_last_instr(nir_cf_node_as_block(node)); 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci /* dead_cf should have eliminated any instruction after the first break 40bf215546Sopenharmony_ci */ 41bf215546Sopenharmony_ci nir_foreach_instr(instr, nir_cf_node_as_block(node)) 42bf215546Sopenharmony_ci assert(instr->type != nir_instr_type_jump || instr == lst_instr); 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci if (lst_instr && lst_instr->type == nir_instr_type_jump && 45bf215546Sopenharmony_ci lst_instr != expected_jump) 46bf215546Sopenharmony_ci return true; 47bf215546Sopenharmony_ci else 48bf215546Sopenharmony_ci return false; 49bf215546Sopenharmony_ci } 50bf215546Sopenharmony_ci case nir_cf_node_if: { 51bf215546Sopenharmony_ci nir_if *if_stmt = nir_cf_node_as_if(node); 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->then_list) { 54bf215546Sopenharmony_ci if (contains_other_jump(node, expected_jump)) 55bf215546Sopenharmony_ci return true; 56bf215546Sopenharmony_ci } 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->else_list) { 59bf215546Sopenharmony_ci if (contains_other_jump(node, expected_jump)) 60bf215546Sopenharmony_ci return true; 61bf215546Sopenharmony_ci } 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci return false; 64bf215546Sopenharmony_ci } 65bf215546Sopenharmony_ci case nir_cf_node_loop: 66bf215546Sopenharmony_ci /* the jumps of nested loops are unrelated */ 67bf215546Sopenharmony_ci return false; 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci default: 70bf215546Sopenharmony_ci unreachable("Unhandled cf node type"); 71bf215546Sopenharmony_ci } 72bf215546Sopenharmony_ci} 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci/* Here we define a trivial if as containing only a single break that must be 75bf215546Sopenharmony_ci * located at the end of either the then or else branch of the top level if, 76bf215546Sopenharmony_ci * there must be no other breaks or any other type of jump. Or we pass NULL 77bf215546Sopenharmony_ci * to break_block the if must contains no jumps at all. 78bf215546Sopenharmony_ci */ 79bf215546Sopenharmony_cistatic inline bool 80bf215546Sopenharmony_cinir_is_trivial_loop_if(nir_if *nif, nir_block *break_block) 81bf215546Sopenharmony_ci{ 82bf215546Sopenharmony_ci nir_instr *last_instr = NULL; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci if (break_block) { 85bf215546Sopenharmony_ci last_instr = nir_block_last_instr(break_block); 86bf215546Sopenharmony_ci assert(last_instr && last_instr->type == nir_instr_type_jump && 87bf215546Sopenharmony_ci nir_instr_as_jump(last_instr)->type == nir_jump_break); 88bf215546Sopenharmony_ci } 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci if (contains_other_jump(&nif->cf_node, last_instr)) 91bf215546Sopenharmony_ci return false; 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci return true; 94bf215546Sopenharmony_ci} 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_cistatic inline bool 97bf215546Sopenharmony_cinir_is_supported_terminator_condition(nir_ssa_scalar cond) 98bf215546Sopenharmony_ci{ 99bf215546Sopenharmony_ci if (!nir_ssa_scalar_is_alu(cond)) 100bf215546Sopenharmony_ci return false; 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci nir_alu_instr *alu = nir_instr_as_alu(cond.def->parent_instr); 103bf215546Sopenharmony_ci return nir_alu_instr_is_comparison(alu) && 104bf215546Sopenharmony_ci nir_op_infos[alu->op].num_inputs == 2; 105bf215546Sopenharmony_ci} 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci#endif /* NIR_LOOP_ANALYZE_H */ 108