Rename nir_lower_constant_initializers to nir_lower_variable_initalizers
[mesa.git] / src / compiler / nir / nir_lower_bool_to_float.c
1 /*
2 * Copyright © 2018 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 static bool
28 assert_ssa_def_is_not_1bit(nir_ssa_def *def, UNUSED void *unused)
29 {
30 assert(def->bit_size > 1);
31 return true;
32 }
33
34 static bool
35 rewrite_1bit_ssa_def_to_32bit(nir_ssa_def *def, void *_progress)
36 {
37 bool *progress = _progress;
38 if (def->bit_size == 1) {
39 def->bit_size = 32;
40 *progress = true;
41 }
42 return true;
43 }
44
45 static bool
46 lower_alu_instr(nir_builder *b, nir_alu_instr *alu)
47 {
48 const nir_op_info *op_info = &nir_op_infos[alu->op];
49
50 b->cursor = nir_before_instr(&alu->instr);
51
52 /* Replacement SSA value */
53 nir_ssa_def *rep = NULL;
54 switch (alu->op) {
55 case nir_op_mov:
56 case nir_op_vec2:
57 case nir_op_vec3:
58 case nir_op_vec4:
59 case nir_op_vec8:
60 case nir_op_vec16:
61 /* These we expect to have booleans but the opcode doesn't change */
62 break;
63
64 case nir_op_b2f32: alu->op = nir_op_mov; break;
65 case nir_op_b2i32: alu->op = nir_op_mov; break;
66 case nir_op_f2b1:
67 case nir_op_i2b1:
68 rep = nir_sne(b, nir_ssa_for_alu_src(b, alu, 0),
69 nir_imm_float(b, 0));
70 break;
71
72 case nir_op_flt: alu->op = nir_op_slt; break;
73 case nir_op_fge: alu->op = nir_op_sge; break;
74 case nir_op_feq: alu->op = nir_op_seq; break;
75 case nir_op_fne: alu->op = nir_op_sne; break;
76 case nir_op_ilt: alu->op = nir_op_slt; break;
77 case nir_op_ige: alu->op = nir_op_sge; break;
78 case nir_op_ieq: alu->op = nir_op_seq; break;
79 case nir_op_ine: alu->op = nir_op_sne; break;
80 case nir_op_ult: alu->op = nir_op_slt; break;
81 case nir_op_uge: alu->op = nir_op_sge; break;
82
83 case nir_op_ball_fequal2: alu->op = nir_op_fall_equal2; break;
84 case nir_op_ball_fequal3: alu->op = nir_op_fall_equal3; break;
85 case nir_op_ball_fequal4: alu->op = nir_op_fall_equal4; break;
86 case nir_op_bany_fnequal2: alu->op = nir_op_fany_nequal2; break;
87 case nir_op_bany_fnequal3: alu->op = nir_op_fany_nequal3; break;
88 case nir_op_bany_fnequal4: alu->op = nir_op_fany_nequal4; break;
89 case nir_op_ball_iequal2: alu->op = nir_op_fall_equal2; break;
90 case nir_op_ball_iequal3: alu->op = nir_op_fall_equal3; break;
91 case nir_op_ball_iequal4: alu->op = nir_op_fall_equal4; break;
92 case nir_op_bany_inequal2: alu->op = nir_op_fany_nequal2; break;
93 case nir_op_bany_inequal3: alu->op = nir_op_fany_nequal3; break;
94 case nir_op_bany_inequal4: alu->op = nir_op_fany_nequal4; break;
95
96 case nir_op_bcsel: alu->op = nir_op_fcsel; break;
97
98 case nir_op_iand: alu->op = nir_op_fmul; break;
99 case nir_op_ixor: alu->op = nir_op_sne; break;
100 case nir_op_ior: alu->op = nir_op_fmax; break;
101
102 case nir_op_inot:
103 rep = nir_seq(b, nir_ssa_for_alu_src(b, alu, 0),
104 nir_imm_float(b, 0));
105 break;
106
107 default:
108 assert(alu->dest.dest.ssa.bit_size > 1);
109 for (unsigned i = 0; i < op_info->num_inputs; i++)
110 assert(alu->src[i].src.ssa->bit_size > 1);
111 return false;
112 }
113
114 if (rep) {
115 /* We've emitted a replacement instruction */
116 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(rep));
117 nir_instr_remove(&alu->instr);
118 } else {
119 if (alu->dest.dest.ssa.bit_size == 1)
120 alu->dest.dest.ssa.bit_size = 32;
121 }
122
123 return true;
124 }
125
126 static bool
127 nir_lower_bool_to_float_impl(nir_function_impl *impl)
128 {
129 bool progress = false;
130
131 nir_builder b;
132 nir_builder_init(&b, impl);
133
134 nir_foreach_block(block, impl) {
135 nir_foreach_instr_safe(instr, block) {
136 switch (instr->type) {
137 case nir_instr_type_alu:
138 progress |= lower_alu_instr(&b, nir_instr_as_alu(instr));
139 break;
140
141 case nir_instr_type_load_const: {
142 nir_load_const_instr *load = nir_instr_as_load_const(instr);
143 if (load->def.bit_size == 1) {
144 nir_const_value *value = load->value;
145 for (unsigned i = 0; i < load->def.num_components; i++)
146 load->value[i].f32 = value[i].b ? 1.0 : 0.0;
147 load->def.bit_size = 32;
148 progress = true;
149 }
150 break;
151 }
152
153 case nir_instr_type_intrinsic:
154 case nir_instr_type_ssa_undef:
155 case nir_instr_type_phi:
156 case nir_instr_type_tex:
157 nir_foreach_ssa_def(instr, rewrite_1bit_ssa_def_to_32bit,
158 &progress);
159 break;
160
161 default:
162 nir_foreach_ssa_def(instr, assert_ssa_def_is_not_1bit, NULL);
163 }
164 }
165 }
166
167 if (progress) {
168 nir_metadata_preserve(impl, nir_metadata_block_index |
169 nir_metadata_dominance);
170 }
171
172 return progress;
173 }
174
175 bool
176 nir_lower_bool_to_float(nir_shader *shader)
177 {
178 bool progress = false;
179
180 nir_foreach_function(function, shader) {
181 if (function->impl && nir_lower_bool_to_float_impl(function->impl))
182 progress = true;
183 }
184
185 return progress;
186 }