101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2016 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#ifndef NIR_LOOP_ANALYZE_H
2501e04c3fSmrg#define NIR_LOOP_ANALYZE_H
2601e04c3fSmrg
2701e04c3fSmrg#include "nir.h"
2801e04c3fSmrg
2901e04c3fSmrg/* Returns true if nir_cf_node contains a jump other than the expected_jump
3001e04c3fSmrg * parameter.
3101e04c3fSmrg */
3201e04c3fSmrgstatic inline bool
3301e04c3fSmrgcontains_other_jump(nir_cf_node *node, nir_instr *expected_jump)
3401e04c3fSmrg{
3501e04c3fSmrg   switch (node->type) {
3601e04c3fSmrg   case nir_cf_node_block: {
3701e04c3fSmrg      nir_instr *lst_instr = nir_block_last_instr(nir_cf_node_as_block(node));
3801e04c3fSmrg
3901e04c3fSmrg      /* dead_cf should have eliminated any instruction after the first break
4001e04c3fSmrg       */
4101e04c3fSmrg      nir_foreach_instr(instr, nir_cf_node_as_block(node))
4201e04c3fSmrg         assert(instr->type != nir_instr_type_jump || instr == lst_instr);
4301e04c3fSmrg
4401e04c3fSmrg      if (lst_instr && lst_instr->type == nir_instr_type_jump &&
4501e04c3fSmrg          lst_instr != expected_jump)
4601e04c3fSmrg         return true;
4701e04c3fSmrg      else
4801e04c3fSmrg         return false;
4901e04c3fSmrg   }
5001e04c3fSmrg   case nir_cf_node_if: {
5101e04c3fSmrg      nir_if *if_stmt = nir_cf_node_as_if(node);
5201e04c3fSmrg
5301e04c3fSmrg      foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->then_list) {
5401e04c3fSmrg         if (contains_other_jump(node, expected_jump))
5501e04c3fSmrg            return true;
5601e04c3fSmrg      }
5701e04c3fSmrg
5801e04c3fSmrg      foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->else_list) {
5901e04c3fSmrg         if (contains_other_jump(node, expected_jump))
6001e04c3fSmrg            return true;
6101e04c3fSmrg      }
6201e04c3fSmrg
6301e04c3fSmrg      return false;
6401e04c3fSmrg   }
6501e04c3fSmrg   case nir_cf_node_loop:
667ec681f3Smrg      /* the jumps of nested loops are unrelated */
677ec681f3Smrg      return false;
6801e04c3fSmrg
6901e04c3fSmrg   default:
7001e04c3fSmrg      unreachable("Unhandled cf node type");
7101e04c3fSmrg   }
7201e04c3fSmrg}
7301e04c3fSmrg
7401e04c3fSmrg/* Here we define a trivial if as containing only a single break that must be
7501e04c3fSmrg * located at the end of either the then or else branch of the top level if,
7601e04c3fSmrg * there must be no other breaks or any other type of jump.  Or we pass NULL
7701e04c3fSmrg * to break_block the if must contains no jumps at all.
7801e04c3fSmrg */
7901e04c3fSmrgstatic inline bool
8001e04c3fSmrgnir_is_trivial_loop_if(nir_if *nif, nir_block *break_block)
8101e04c3fSmrg{
8201e04c3fSmrg   nir_instr *last_instr = NULL;
8301e04c3fSmrg
8401e04c3fSmrg   if (break_block) {
8501e04c3fSmrg      last_instr = nir_block_last_instr(break_block);
8601e04c3fSmrg      assert(last_instr && last_instr->type == nir_instr_type_jump &&
8701e04c3fSmrg             nir_instr_as_jump(last_instr)->type == nir_jump_break);
8801e04c3fSmrg   }
8901e04c3fSmrg
9001e04c3fSmrg   if (contains_other_jump(&nif->cf_node, last_instr))
9101e04c3fSmrg      return false;
9201e04c3fSmrg
9301e04c3fSmrg   return true;
9401e04c3fSmrg}
9501e04c3fSmrg
9601e04c3fSmrgstatic inline bool
977ec681f3Smrgnir_is_supported_terminator_condition(nir_ssa_scalar cond)
9801e04c3fSmrg{
997ec681f3Smrg   if (!nir_ssa_scalar_is_alu(cond))
10001e04c3fSmrg      return false;
10101e04c3fSmrg
1027ec681f3Smrg   nir_alu_instr *alu = nir_instr_as_alu(cond.def->parent_instr);
1037ec681f3Smrg   return nir_alu_instr_is_comparison(alu) &&
1047ec681f3Smrg          nir_op_infos[alu->op].num_inputs == 2;
10501e04c3fSmrg}
10601e04c3fSmrg
10701e04c3fSmrg#endif /* NIR_LOOP_ANALYZE_H */
108