nir/dead_cf: delete code that's unreachable due to jumps
[mesa.git] / src / glsl / nir / nir_lower_load_const_to_scalar.c
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 void
39 lower_load_const_instr_scalar(nir_load_const_instr *lower)
40 {
41 if (lower->def.num_components == 1)
42 return;
43
44 nir_builder b;
45 nir_builder_init(&b, nir_cf_node_get_function(&lower->instr.block->cf_node));
46 b.cursor = nir_before_instr(&lower->instr);
47
48 /* Emit the individual loads. */
49 nir_ssa_def *loads[4];
50 for (unsigned i = 0; i < lower->def.num_components; i++) {
51 nir_load_const_instr *load_comp = nir_load_const_instr_create(b.shader, 1);
52 load_comp->value.u[0] = lower->value.u[i];
53 nir_builder_instr_insert(&b, &load_comp->instr);
54 loads[i] = &load_comp->def;
55 }
56
57 /* Batch things back together into a vector. */
58 nir_ssa_def *vec;
59 switch (lower->def.num_components) {
60 case 2:
61 vec = nir_vec2(&b, loads[0], loads[1]);
62 break;
63 case 3:
64 vec = nir_vec3(&b, loads[0], loads[1], loads[2]);
65 break;
66 case 4:
67 vec = nir_vec4(&b, loads[0], loads[1], loads[2], loads[3]);
68 break;
69 default:
70 unreachable("Unknown load_const component count.");
71 }
72
73 /* Replace the old load with a reference to our reconstructed vector. */
74 nir_ssa_def_rewrite_uses(&lower->def, nir_src_for_ssa(vec),
75 ralloc_parent(b.impl));
76 nir_instr_remove(&lower->instr);
77 }
78
79 static bool
80 lower_load_const_to_scalar_block(nir_block *block, void *data)
81 {
82 nir_foreach_instr_safe(block, instr) {
83 if (instr->type == nir_instr_type_load_const)
84 lower_load_const_instr_scalar(nir_instr_as_load_const(instr));
85 }
86
87 return true;
88 }
89
90 static void
91 nir_lower_load_const_to_scalar_impl(nir_function_impl *impl)
92 {
93 nir_foreach_block(impl, lower_load_const_to_scalar_block, NULL);
94 }
95
96 void
97 nir_lower_load_const_to_scalar(nir_shader *shader)
98 {
99 nir_foreach_overload(shader, overload) {
100 if (overload->impl)
101 nir_lower_load_const_to_scalar_impl(overload->impl);
102 }
103 }