1 /*
2  * Copyright © 2017 Intel Corporation
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 /**
28  * \file nir_opt_intrinsics.c
29  */
30 
31 static bool
src_is_single_use_shuffle(nir_src src,nir_ssa_def ** data,nir_ssa_def ** index)32 src_is_single_use_shuffle(nir_src src, nir_ssa_def **data, nir_ssa_def **index)
33 {
34    nir_intrinsic_instr *shuffle = nir_src_as_intrinsic(src);
35    if (shuffle == NULL || shuffle->intrinsic != nir_intrinsic_shuffle)
36       return false;
37 
38    /* This is only called when src is part of an ALU op so requiring no if
39     * uses is reasonable.  If we ever want to use this from an if statement,
40     * we can change it then.
41     */
42    if (!list_is_empty(&shuffle->dest.ssa.if_uses) ||
43        !list_is_singular(&shuffle->dest.ssa.uses))
44       return false;
45 
46    assert(shuffle->src[0].is_ssa);
47    assert(shuffle->src[1].is_ssa);
48 
49    *data = shuffle->src[0].ssa;
50    *index = shuffle->src[1].ssa;
51 
52    return true;
53 }
54 
55 static nir_ssa_def *
try_opt_bcsel_of_shuffle(nir_builder * b,nir_alu_instr * alu)56 try_opt_bcsel_of_shuffle(nir_builder *b, nir_alu_instr *alu)
57 {
58    assert(alu->op == nir_op_bcsel);
59 
60    if (!nir_alu_src_is_trivial_ssa(alu, 0))
61       return NULL;
62 
63    nir_ssa_def *data1, *index1;
64    if (!nir_alu_src_is_trivial_ssa(alu, 1) ||
65        !src_is_single_use_shuffle(alu->src[1].src, &data1, &index1))
66       return NULL;
67 
68    nir_ssa_def *data2, *index2;
69    if (!nir_alu_src_is_trivial_ssa(alu, 2) ||
70        !src_is_single_use_shuffle(alu->src[2].src, &data2, &index2))
71       return NULL;
72 
73    if (data1 != data2)
74       return NULL;
75 
76    nir_ssa_def *index = nir_bcsel(b, alu->src[0].src.ssa, index1, index2);
77    nir_intrinsic_instr *shuffle =
78       nir_intrinsic_instr_create(b->shader, nir_intrinsic_shuffle);
79    shuffle->src[0] = nir_src_for_ssa(index);
80    shuffle->src[1] = nir_src_for_ssa(data1);
81    shuffle->num_components = alu->dest.dest.ssa.num_components;
82    nir_ssa_dest_init(&shuffle->instr, &shuffle->dest,
83                      alu->dest.dest.ssa.num_components,
84                      alu->dest.dest.ssa.bit_size, NULL);
85    nir_builder_instr_insert(b, &shuffle->instr);
86 
87    return &shuffle->dest.ssa;
88 }
89 
90 static bool
opt_intrinsics_alu(nir_builder * b,nir_alu_instr * alu)91 opt_intrinsics_alu(nir_builder *b, nir_alu_instr *alu)
92 {
93    nir_ssa_def *replacement = NULL;
94 
95    switch (alu->op) {
96    case nir_op_bcsel:
97       replacement = try_opt_bcsel_of_shuffle(b, alu);
98       break;
99 
100    default:
101       break;
102    }
103 
104    if (replacement) {
105       nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa,
106                                nir_src_for_ssa(replacement));
107       nir_instr_remove(&alu->instr);
108       return true;
109    } else {
110       return false;
111    }
112 }
113 
114 static bool
opt_intrinsics_intrin(nir_builder * b,nir_intrinsic_instr * intrin,const struct nir_shader_compiler_options * options)115 opt_intrinsics_intrin(nir_builder *b, nir_intrinsic_instr *intrin,
116                       const struct nir_shader_compiler_options *options)
117 {
118    switch (intrin->intrinsic) {
119    case nir_intrinsic_load_sample_mask_in: {
120       /* Transform:
121        *   gl_SampleMaskIn == 0 ---> gl_HelperInvocation
122        *   gl_SampleMaskIn != 0 ---> !gl_HelperInvocation
123        */
124       if (!options->optimize_sample_mask_in)
125          return false;
126 
127       bool progress = false;
128       nir_foreach_use_safe(use_src, &intrin->dest.ssa) {
129          if (use_src->parent_instr->type == nir_instr_type_alu) {
130             nir_alu_instr *alu = nir_instr_as_alu(use_src->parent_instr);
131 
132             if (alu->op == nir_op_ieq ||
133                 alu->op == nir_op_ine) {
134                /* Check for 0 in either operand. */
135                nir_const_value *const_val =
136                    nir_src_as_const_value(alu->src[0].src);
137                if (!const_val)
138                   const_val = nir_src_as_const_value(alu->src[1].src);
139                if (!const_val || const_val->i32 != 0)
140                   continue;
141 
142                nir_ssa_def *new_expr = nir_load_helper_invocation(b, 1);
143 
144                if (alu->op == nir_op_ine)
145                   new_expr = nir_inot(b, new_expr);
146 
147                nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa,
148                                         nir_src_for_ssa(new_expr));
149                nir_instr_remove(&alu->instr);
150                progress = true;
151             }
152          }
153       }
154       return progress;
155    }
156 
157    default:
158       return false;
159    }
160 }
161 
162 static bool
opt_intrinsics_impl(nir_function_impl * impl,const struct nir_shader_compiler_options * options)163 opt_intrinsics_impl(nir_function_impl *impl,
164                     const struct nir_shader_compiler_options *options)
165 {
166    nir_builder b;
167    nir_builder_init(&b, impl);
168    bool progress = false;
169 
170    nir_foreach_block(block, impl) {
171       nir_foreach_instr_safe(instr, block) {
172          b.cursor = nir_before_instr(instr);
173 
174          switch (instr->type) {
175          case nir_instr_type_alu:
176             if (opt_intrinsics_alu(&b, nir_instr_as_alu(instr)))
177                progress = true;
178             break;
179 
180          case nir_instr_type_intrinsic:
181             if (opt_intrinsics_intrin(&b, nir_instr_as_intrinsic(instr),
182                                       options))
183                progress = true;
184             break;
185 
186          default:
187             break;
188          }
189       }
190    }
191 
192    return progress;
193 }
194 
195 bool
nir_opt_intrinsics(nir_shader * shader)196 nir_opt_intrinsics(nir_shader *shader)
197 {
198    bool progress = false;
199 
200    nir_foreach_function(function, shader) {
201       if (!function->impl)
202          continue;
203 
204       if (opt_intrinsics_impl(function->impl, shader->options)) {
205          progress = true;
206          nir_metadata_preserve(function->impl, nir_metadata_block_index |
207                                                nir_metadata_dominance);
208       } else {
209          nir_metadata_preserve(function->impl, nir_metadata_all);
210       }
211    }
212 
213    return progress;
214 }
215