1/*
2 * Copyright © 2016 Red Hat
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "nir.h"
25#include "nir_builder.h"
26
27/** @file nir_opt_conditional_discard.c
28 *
29 * Handles optimization of lowering if (cond) discard to discard_if(cond).
30 */
31
32static bool
33nir_opt_conditional_discard_block(nir_builder *b, nir_block *block)
34{
35   if (nir_cf_node_is_first(&block->cf_node))
36      return false;
37
38   nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_node);
39   if (prev_node->type != nir_cf_node_if)
40      return false;
41
42   nir_if *if_stmt = nir_cf_node_as_if(prev_node);
43   nir_block *then_block = nir_if_first_then_block(if_stmt);
44   nir_block *else_block = nir_if_first_else_block(if_stmt);
45
46   /* check there is only one else block and it is empty */
47   if (nir_if_last_else_block(if_stmt) != else_block)
48      return false;
49   if (!exec_list_is_empty(&else_block->instr_list))
50      return false;
51
52   /* check there is only one then block and it has only one instruction in it */
53   if (nir_if_last_then_block(if_stmt) != then_block)
54      return false;
55   if (exec_list_is_empty(&then_block->instr_list))
56      return false;
57   if (exec_list_length(&then_block->instr_list) > 1)
58      return false;
59   /*
60    * make sure no subsequent phi nodes point at this if.
61    */
62   nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&if_stmt->cf_node));
63   nir_foreach_instr_safe(instr, after) {
64      if (instr->type != nir_instr_type_phi)
65         break;
66      nir_phi_instr *phi = nir_instr_as_phi(instr);
67
68      nir_foreach_phi_src(phi_src, phi) {
69         if (phi_src->pred == then_block ||
70             phi_src->pred == else_block)
71            return false;
72      }
73   }
74
75   /* Get the first instruction in the then block and confirm it is
76    * a discard or a discard_if
77    */
78   nir_instr *instr = nir_block_first_instr(then_block);
79   if (instr->type != nir_instr_type_intrinsic)
80      return false;
81
82   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
83   if (intrin->intrinsic != nir_intrinsic_discard &&
84       intrin->intrinsic != nir_intrinsic_discard_if)
85      return false;
86
87   nir_src cond;
88
89   b->cursor = nir_before_cf_node(prev_node);
90   if (intrin->intrinsic == nir_intrinsic_discard)
91      cond = if_stmt->condition;
92   else
93      cond = nir_src_for_ssa(nir_iand(b,
94                                      nir_ssa_for_src(b, if_stmt->condition, 1),
95                                      nir_ssa_for_src(b, intrin->src[0], 1)));
96
97   nir_intrinsic_instr *discard_if =
98      nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard_if);
99   nir_src_copy(&discard_if->src[0], &cond, discard_if);
100
101   nir_instr_insert_before_cf(prev_node, &discard_if->instr);
102   nir_instr_remove(&intrin->instr);
103   nir_cf_node_remove(&if_stmt->cf_node);
104
105   return true;
106}
107
108bool
109nir_opt_conditional_discard(nir_shader *shader)
110{
111   bool progress = false;
112
113   nir_builder builder;
114
115   nir_foreach_function(function, shader) {
116      if (function->impl) {
117         nir_builder_init(&builder, function->impl);
118         nir_foreach_block_safe(block, function->impl) {
119            progress |= nir_opt_conditional_discard_block(&builder, block);
120         }
121      }
122   }
123   return progress;
124}
125