nir: Add a lower_vec_to_movs pass
[mesa.git] / src / glsl / nir / nir_lower_vec_to_movs.c
1 /*
2 * Copyright © 2014 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 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir.h"
29
30 /*
31 * Implements a simple pass that lowers vecN instructions to a series of
32 * moves with partial writes.
33 */
34
35 static bool
36 lower_vec_to_movs_block(nir_block *block, void *mem_ctx)
37 {
38 nir_foreach_instr_safe(block, instr) {
39 if (instr->type != nir_instr_type_alu)
40 continue;
41
42 nir_alu_instr *vec = (nir_alu_instr *)instr;
43
44 switch (vec->op) {
45 case nir_op_vec2:
46 case nir_op_vec3:
47 case nir_op_vec4:
48 break;
49 default:
50 continue; /* The loop */
51 }
52
53 for (unsigned i = 0, src_idx = 0; i < 4; i++) {
54 if (!(vec->dest.write_mask & (1 << i)))
55 continue;
56
57 assert(src_idx < nir_op_infos[vec->op].num_inputs);
58
59 nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov);
60 mov->src[0].src = nir_src_copy(vec->src[src_idx].src, mem_ctx);
61 mov->src[0].negate = vec->src[src_idx].negate;
62 mov->src[0].abs = vec->src[src_idx].abs;
63
64 /* We only care about the one swizzle */
65 mov->src[0].swizzle[i] = vec->src[src_idx].swizzle[0];
66
67 mov->dest.dest = nir_dest_copy(vec->dest.dest, mem_ctx);
68 mov->dest.saturate = vec->dest.saturate;
69 mov->dest.write_mask = (1u << i);
70
71 nir_instr_insert_before(&vec->instr, &mov->instr);
72
73 src_idx++;
74 }
75
76 nir_instr_remove(&vec->instr);
77 ralloc_free(vec);
78 }
79
80 return true;
81 }
82
83 static void
84 nir_lower_vec_to_movs_impl(nir_function_impl *impl)
85 {
86 nir_foreach_block(impl, lower_vec_to_movs_block, ralloc_parent(impl));
87 }
88
89 void
90 nir_lower_vec_to_movs(nir_shader *shader)
91 {
92 nir_foreach_overload(shader, overload) {
93 if (overload->impl)
94 nir_lower_vec_to_movs_impl(overload->impl);
95 }
96 }