glsl: Use array deref for access to vector components
[mesa.git] / src / glsl / nir / nir_opt_peephole_ffma.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 small peephole optimization that looks for a multiply that
32 * is only ever used in an add and replaces both with an fma.
33 */
34
35 struct peephole_ffma_state {
36 void *mem_ctx;
37 nir_function_impl *impl;
38 bool progress;
39 };
40
41 static inline bool
42 are_all_uses_fadd(nir_ssa_def *def)
43 {
44 if (!list_empty(&def->if_uses))
45 return false;
46
47 nir_foreach_use(def, use_src) {
48 nir_instr *use_instr = use_src->parent_instr;
49
50 if (use_instr->type != nir_instr_type_alu)
51 return false;
52
53 nir_alu_instr *use_alu = nir_instr_as_alu(use_instr);
54 switch (use_alu->op) {
55 case nir_op_fadd:
56 break; /* This one's ok */
57
58 case nir_op_imov:
59 case nir_op_fmov:
60 case nir_op_fneg:
61 case nir_op_fabs:
62 assert(use_alu->dest.dest.is_ssa);
63 if (!are_all_uses_fadd(&use_alu->dest.dest.ssa))
64 return false;
65 break;
66
67 default:
68 return false;
69 }
70 }
71
72 return true;
73 }
74
75 static nir_alu_instr *
76 get_mul_for_src(nir_alu_src *src, int num_components,
77 uint8_t swizzle[4], bool *negate, bool *abs)
78 {
79 uint8_t swizzle_tmp[4];
80 assert(src->src.is_ssa && !src->abs && !src->negate);
81
82 nir_instr *instr = src->src.ssa->parent_instr;
83 if (instr->type != nir_instr_type_alu)
84 return NULL;
85
86 nir_alu_instr *alu = nir_instr_as_alu(instr);
87 switch (alu->op) {
88 case nir_op_imov:
89 case nir_op_fmov:
90 alu = get_mul_for_src(&alu->src[0], num_components, swizzle, negate, abs);
91 break;
92
93 case nir_op_fneg:
94 alu = get_mul_for_src(&alu->src[0], num_components, swizzle, negate, abs);
95 *negate = !*negate;
96 break;
97
98 case nir_op_fabs:
99 alu = get_mul_for_src(&alu->src[0], num_components, swizzle, negate, abs);
100 *negate = false;
101 *abs = true;
102 break;
103
104 case nir_op_fmul:
105 /* Only absorb a fmul into a ffma if the fmul is is only used in fadd
106 * operations. This prevents us from being too aggressive with our
107 * fusing which can actually lead to more instructions.
108 */
109 if (!are_all_uses_fadd(&alu->dest.dest.ssa))
110 return NULL;
111 break;
112
113 default:
114 return NULL;
115 }
116
117 if (!alu)
118 return NULL;
119
120 /* Copy swizzle data before overwriting it to avoid setting a wrong swizzle.
121 *
122 * Example:
123 * Former swizzle[] = xyzw
124 * src->swizzle[] = zyxx
125 *
126 * Expected output swizzle = zyxx
127 * If we reuse swizzle in the loop, then output swizzle would be zyzz.
128 */
129 memcpy(swizzle_tmp, swizzle, 4*sizeof(uint8_t));
130 for (int i = 0; i < num_components; i++)
131 swizzle[i] = swizzle_tmp[src->swizzle[i]];
132
133 return alu;
134 }
135
136 static bool
137 nir_opt_peephole_ffma_block(nir_block *block, void *void_state)
138 {
139 struct peephole_ffma_state *state = void_state;
140
141 nir_foreach_instr_safe(block, instr) {
142 if (instr->type != nir_instr_type_alu)
143 continue;
144
145 nir_alu_instr *add = nir_instr_as_alu(instr);
146 if (add->op != nir_op_fadd)
147 continue;
148
149 /* TODO: Maybe bail if this expression is considered "precise"? */
150
151 assert(add->src[0].src.is_ssa && add->src[1].src.is_ssa);
152
153 /* This, is the case a + a. We would rather handle this with an
154 * algebraic reduction than fuse it. Also, we want to only fuse
155 * things where the multiply is used only once and, in this case,
156 * it would be used twice by the same instruction.
157 */
158 if (add->src[0].src.ssa == add->src[1].src.ssa)
159 continue;
160
161 nir_alu_instr *mul;
162 uint8_t add_mul_src, swizzle[4];
163 bool negate, abs;
164 for (add_mul_src = 0; add_mul_src < 2; add_mul_src++) {
165 for (unsigned i = 0; i < 4; i++)
166 swizzle[i] = i;
167
168 negate = false;
169 abs = false;
170
171 mul = get_mul_for_src(&add->src[add_mul_src],
172 add->dest.dest.ssa.num_components,
173 swizzle, &negate, &abs);
174
175 if (mul != NULL)
176 break;
177 }
178
179 if (mul == NULL)
180 continue;
181
182 nir_ssa_def *mul_src[2];
183 mul_src[0] = mul->src[0].src.ssa;
184 mul_src[1] = mul->src[1].src.ssa;
185
186 if (abs) {
187 for (unsigned i = 0; i < 2; i++) {
188 nir_alu_instr *abs = nir_alu_instr_create(state->mem_ctx,
189 nir_op_fabs);
190 abs->src[0].src = nir_src_for_ssa(mul_src[i]);
191 nir_ssa_dest_init(&abs->instr, &abs->dest.dest,
192 mul_src[i]->num_components, NULL);
193 abs->dest.write_mask = (1 << mul_src[i]->num_components) - 1;
194 nir_instr_insert_before(&add->instr, &abs->instr);
195 mul_src[i] = &abs->dest.dest.ssa;
196 }
197 }
198
199 if (negate) {
200 nir_alu_instr *neg = nir_alu_instr_create(state->mem_ctx,
201 nir_op_fneg);
202 neg->src[0].src = nir_src_for_ssa(mul_src[0]);
203 nir_ssa_dest_init(&neg->instr, &neg->dest.dest,
204 mul_src[0]->num_components, NULL);
205 neg->dest.write_mask = (1 << mul_src[0]->num_components) - 1;
206 nir_instr_insert_before(&add->instr, &neg->instr);
207 mul_src[0] = &neg->dest.dest.ssa;
208 }
209
210 nir_alu_instr *ffma = nir_alu_instr_create(state->mem_ctx, nir_op_ffma);
211 ffma->dest.saturate = add->dest.saturate;
212 ffma->dest.write_mask = add->dest.write_mask;
213
214 for (unsigned i = 0; i < 2; i++) {
215 ffma->src[i].src = nir_src_for_ssa(mul_src[i]);
216 for (unsigned j = 0; j < add->dest.dest.ssa.num_components; j++)
217 ffma->src[i].swizzle[j] = mul->src[i].swizzle[swizzle[j]];
218 }
219 nir_alu_src_copy(&ffma->src[2], &add->src[1 - add_mul_src], ffma);
220
221 assert(add->dest.dest.is_ssa);
222
223 nir_ssa_dest_init(&ffma->instr, &ffma->dest.dest,
224 add->dest.dest.ssa.num_components,
225 add->dest.dest.ssa.name);
226 nir_ssa_def_rewrite_uses(&add->dest.dest.ssa,
227 nir_src_for_ssa(&ffma->dest.dest.ssa));
228
229 nir_instr_insert_before(&add->instr, &ffma->instr);
230 assert(list_empty(&add->dest.dest.ssa.uses));
231 nir_instr_remove(&add->instr);
232
233 state->progress = true;
234 }
235
236 return true;
237 }
238
239 static bool
240 nir_opt_peephole_ffma_impl(nir_function_impl *impl)
241 {
242 struct peephole_ffma_state state;
243
244 state.mem_ctx = ralloc_parent(impl);
245 state.impl = impl;
246 state.progress = false;
247
248 nir_foreach_block(impl, nir_opt_peephole_ffma_block, &state);
249
250 if (state.progress)
251 nir_metadata_preserve(impl, nir_metadata_block_index |
252 nir_metadata_dominance);
253
254 return state.progress;
255 }
256
257 bool
258 nir_opt_peephole_ffma(nir_shader *shader)
259 {
260 bool progress = false;
261
262 nir_foreach_overload(shader, overload) {
263 if (overload->impl)
264 progress |= nir_opt_peephole_ffma_impl(overload->impl);
265 }
266
267 return progress;
268 }