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 of
30 *  - if (cond) discard to discard_if(cond) and
31 *  - if (cond) demote to demote_if(cond)
32 *  - if (cond) terminate to terminate_if(cond)
33 */
34
35static bool
36nir_opt_conditional_discard_block(nir_builder *b, nir_block *block)
37{
38   if (nir_cf_node_is_first(&block->cf_node))
39      return false;
40
41   nir_cf_node *prev_node = nir_cf_node_prev(&block->cf_node);
42   if (prev_node->type != nir_cf_node_if)
43      return false;
44
45   nir_if *if_stmt = nir_cf_node_as_if(prev_node);
46   nir_block *then_block = nir_if_first_then_block(if_stmt);
47   nir_block *else_block = nir_if_first_else_block(if_stmt);
48
49   /* check there is only one else block and it is empty */
50   if (nir_if_last_else_block(if_stmt) != else_block)
51      return false;
52   if (!exec_list_is_empty(&else_block->instr_list))
53      return false;
54
55   /* check there is only one then block and it has only one instruction in it */
56   if (nir_if_last_then_block(if_stmt) != then_block)
57      return false;
58   if (exec_list_is_empty(&then_block->instr_list))
59      return false;
60   if (exec_list_length(&then_block->instr_list) > 1)
61      return false;
62   /*
63    * make sure no subsequent phi nodes point at this if.
64    */
65   nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&if_stmt->cf_node));
66   nir_foreach_instr_safe(instr, after) {
67      if (instr->type != nir_instr_type_phi)
68         break;
69      nir_phi_instr *phi = nir_instr_as_phi(instr);
70
71      nir_foreach_phi_src(phi_src, phi) {
72         if (phi_src->pred == then_block ||
73             phi_src->pred == else_block)
74            return false;
75      }
76   }
77
78   /* Get the first instruction in the then block and confirm it is
79    * a discard or a demote instruction.
80    */
81   nir_instr *instr = nir_block_first_instr(then_block);
82   if (instr->type != nir_instr_type_intrinsic)
83      return false;
84
85   nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
86   nir_intrinsic_op op = intrin->intrinsic;
87   assert(if_stmt->condition.is_ssa);
88   nir_ssa_def *cond = if_stmt->condition.ssa;
89   b->cursor = nir_before_cf_node(prev_node);
90
91   switch (intrin->intrinsic) {
92   case nir_intrinsic_discard:
93      op = nir_intrinsic_discard_if;
94      break;
95   case nir_intrinsic_demote:
96      op = nir_intrinsic_demote_if;
97      break;
98   case nir_intrinsic_terminate:
99      op = nir_intrinsic_terminate_if;
100      break;
101   case nir_intrinsic_discard_if:
102   case nir_intrinsic_demote_if:
103   case nir_intrinsic_terminate_if:
104      assert(intrin->src[0].is_ssa);
105      cond = nir_iand(b, cond, intrin->src[0].ssa);
106      break;
107   default:
108      return false;
109   }
110
111   nir_intrinsic_instr *discard_if =
112      nir_intrinsic_instr_create(b->shader, op);
113   discard_if->src[0] = nir_src_for_ssa(cond);
114
115   nir_instr_insert_before_cf(prev_node, &discard_if->instr);
116   nir_instr_remove(&intrin->instr);
117   nir_cf_node_remove(&if_stmt->cf_node);
118
119   return true;
120}
121
122bool
123nir_opt_conditional_discard(nir_shader *shader)
124{
125   bool progress = false;
126
127   nir_builder builder;
128
129   nir_foreach_function(function, shader) {
130      if (function->impl) {
131         nir_builder_init(&builder, function->impl);
132
133         bool impl_progress = false;
134         nir_foreach_block_safe(block, function->impl) {
135            if (nir_opt_conditional_discard_block(&builder, block))
136               impl_progress = true;
137         }
138
139         if (impl_progress) {
140            nir_metadata_preserve(function->impl, nir_metadata_none);
141            progress = true;
142         } else {
143            nir_metadata_preserve(function->impl, nir_metadata_all);
144         }
145      }
146   }
147   return progress;
148}
149