1 /*
2 * Copyright © 2015 Broadcom
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 "util/macros.h"
25 #include "nir.h"
26 #include "nir_builder.h"
27
28 /** @file nir_lower_load_const_to_scalar.c
29 *
30 * Replaces vector nir_load_const instructions with a series of loads and a
31 * vec[234] to reconstruct the original vector (on the assumption that
32 * nir_lower_alu_to_scalar() will then be used to split it up).
33 *
34 * This gives NIR a chance to CSE more operations on a scalar shader, when the
35 * same value was used in different vector contant loads.
36 */
37
38 static bool
lower_load_const_instr_scalar(nir_load_const_instr * lower)39 lower_load_const_instr_scalar(nir_load_const_instr *lower)
40 {
41 if (lower->def.num_components == 1)
42 return false;
43
44 nir_builder b = nir_builder_at(nir_before_instr(&lower->instr));
45
46 /* Emit the individual loads. */
47 nir_def *loads[NIR_MAX_VEC_COMPONENTS];
48 for (unsigned i = 0; i < lower->def.num_components; i++) {
49 nir_load_const_instr *load_comp =
50 nir_load_const_instr_create(b.shader, 1, lower->def.bit_size);
51 load_comp->value[0] = lower->value[i];
52 nir_builder_instr_insert(&b, &load_comp->instr);
53 loads[i] = &load_comp->def;
54 }
55
56 /* Batch things back together into a vector. */
57 nir_def *vec = nir_vec(&b, loads, lower->def.num_components);
58
59 /* Replace the old load with a reference to our reconstructed vector. */
60 nir_def_replace(&lower->def, vec);
61 return true;
62 }
63
64 static bool
nir_lower_load_const_to_scalar_impl(nir_function_impl * impl)65 nir_lower_load_const_to_scalar_impl(nir_function_impl *impl)
66 {
67 bool progress = false;
68
69 nir_foreach_block(block, impl) {
70 nir_foreach_instr_safe(instr, block) {
71 if (instr->type == nir_instr_type_load_const)
72 progress |=
73 lower_load_const_instr_scalar(nir_instr_as_load_const(instr));
74 }
75 }
76
77 if (progress) {
78 nir_metadata_preserve(impl, nir_metadata_control_flow);
79 } else {
80 nir_metadata_preserve(impl, nir_metadata_all);
81 }
82
83 return progress;
84 }
85
86 bool
nir_lower_load_const_to_scalar(nir_shader * shader)87 nir_lower_load_const_to_scalar(nir_shader *shader)
88 {
89 bool progress = false;
90
91 nir_foreach_function_impl(impl, shader) {
92 progress |= nir_lower_load_const_to_scalar_impl(impl);
93 }
94
95 return progress;
96 }
97