58e8829e1c4d7e50849d902c50c601d5c1927f28
[mesa.git] / src / glsl / nir / nir_to_ssa.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 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "malloc.h"
30 #include <unistd.h>
31
32 /*
33 * Implements the classic to-SSA algorithm described by Cytron et. al. in
34 * "Efficiently Computing Static Single Assignment Form and the Control
35 * Dependence Graph."
36 */
37
38 /* inserts a phi node of the form reg = phi(reg, reg, reg, ...) */
39
40 static void
41 insert_trivial_phi(nir_register *reg, nir_block *block, void *mem_ctx)
42 {
43 nir_phi_instr *instr = nir_phi_instr_create(mem_ctx);
44
45 instr->dest.reg.reg = reg;
46 struct set_entry *entry;
47 set_foreach(block->predecessors, entry) {
48 nir_block *pred = (nir_block *) entry->key;
49
50 nir_phi_src *src = ralloc(mem_ctx, nir_phi_src);
51 src->pred = pred;
52 src->src.is_ssa = false;
53 src->src.reg.base_offset = 0;
54 src->src.reg.indirect = NULL;
55 src->src.reg.reg = reg;
56 exec_list_push_tail(&instr->srcs, &src->node);
57 }
58
59 nir_instr_insert_before_block(block, &instr->instr);
60 }
61
62 static void
63 insert_phi_nodes(nir_function_impl *impl)
64 {
65 void *mem_ctx = ralloc_parent(impl);
66
67 unsigned *work = calloc(impl->num_blocks, sizeof(unsigned));
68 unsigned *has_already = calloc(impl->num_blocks, sizeof(unsigned));
69
70 /*
71 * Since the work flags already prevent us from inserting a node that has
72 * ever been inserted into W, we don't need to use a set to represent W.
73 * Also, since no block can ever be inserted into W more than once, we know
74 * that the maximum size of W is the number of basic blocks in the
75 * function. So all we need to handle W is an array and a pointer to the
76 * next element to be inserted and the next element to be removed.
77 */
78 nir_block **W = malloc(impl->num_blocks * sizeof(nir_block *));
79 unsigned w_start, w_end;
80
81 unsigned iter_count = 0;
82
83 nir_index_blocks(impl);
84
85 foreach_list_typed(nir_register, reg, node, &impl->registers) {
86 if (reg->num_array_elems != 0)
87 continue;
88
89 w_start = w_end = 0;
90 iter_count++;
91
92 struct set_entry *entry;
93 set_foreach(reg->defs, entry) {
94 nir_instr *def = (nir_instr *) entry->key;
95 if (work[def->block->index] < iter_count)
96 W[w_end++] = def->block;
97 work[def->block->index] = iter_count;
98 }
99
100 while (w_start != w_end) {
101 nir_block *cur = W[w_start++];
102 set_foreach(cur->dom_frontier, entry) {
103 nir_block *next = (nir_block *) entry->key;
104
105 /*
106 * If there's more than one return statement, then the end block
107 * can be a join point for some definitions. However, there are
108 * no instructions in the end block, so nothing would use those
109 * phi nodes. Of course, we couldn't place those phi nodes
110 * anyways due to the restriction of having no instructions in the
111 * end block...
112 */
113 if (next == impl->end_block)
114 continue;
115
116 if (has_already[next->index] < iter_count) {
117 insert_trivial_phi(reg, next, mem_ctx);
118 has_already[next->index] = iter_count;
119 if (work[next->index] < iter_count) {
120 work[next->index] = iter_count;
121 W[w_end++] = next;
122 }
123 }
124 }
125 }
126 }
127
128 free(work);
129 free(has_already);
130 free(W);
131 }
132
133 typedef struct {
134 nir_ssa_def **stack;
135 int index;
136 unsigned num_defs; /** < used to add indices to debug names */
137 #ifdef DEBUG
138 unsigned stack_size;
139 #endif
140 } reg_state;
141
142 typedef struct {
143 reg_state *states;
144 void *mem_ctx;
145 nir_instr *parent_instr;
146 nir_if *parent_if;
147 nir_function_impl *impl;
148
149 /* map from SSA value -> original register */
150 struct hash_table *ssa_map;
151 } rewrite_state;
152
153 static nir_ssa_def *get_ssa_src(nir_register *reg, rewrite_state *state)
154 {
155 unsigned index = reg->index;
156
157 if (state->states[index].index == -1) {
158 /*
159 * We're using an undefined register, create a new undefined SSA value
160 * to preserve the information that this source is undefined
161 */
162 nir_ssa_undef_instr *instr =
163 nir_ssa_undef_instr_create(state->mem_ctx, reg->num_components);
164
165 /*
166 * We could just insert the undefined instruction before the instruction
167 * we're rewriting, but we could be rewriting a phi source in which case
168 * we can't do that, so do the next easiest thing - insert it at the
169 * beginning of the program. In the end, it doesn't really matter where
170 * the undefined instructions are because they're going to be ignored
171 * in the backend.
172 */
173 nir_instr_insert_before_cf_list(&state->impl->body, &instr->instr);
174 return &instr->def;
175 }
176
177 return state->states[index].stack[state->states[index].index];
178 }
179
180 static bool
181 rewrite_use(nir_src *src, void *_state)
182 {
183 rewrite_state *state = (rewrite_state *) _state;
184
185 if (src->is_ssa)
186 return true;
187
188 unsigned index = src->reg.reg->index;
189
190 if (state->states[index].stack == NULL)
191 return true;
192
193 src->is_ssa = true;
194 src->ssa = get_ssa_src(src->reg.reg, state);
195
196 if (state->parent_instr)
197 _mesa_set_add(src->ssa->uses, _mesa_hash_pointer(state->parent_instr),
198 state->parent_instr);
199 else
200 _mesa_set_add(src->ssa->if_uses, _mesa_hash_pointer(state->parent_if),
201 state->parent_if);
202 return true;
203 }
204
205 static bool
206 rewrite_def_forwards(nir_dest *dest, void *_state)
207 {
208 rewrite_state *state = (rewrite_state *) _state;
209
210 if (dest->is_ssa)
211 return true;
212
213 nir_register *reg = dest->reg.reg;
214 unsigned index = reg->index;
215
216 if (state->states[index].stack == NULL)
217 return true;
218
219 dest->is_ssa = true;
220
221 char *name = NULL;
222 if (dest->reg.reg->name)
223 name = ralloc_asprintf(state->mem_ctx, "%s_%u", dest->reg.reg->name,
224 state->states[index].num_defs);
225
226 nir_ssa_def_init(state->parent_instr, &dest->ssa,
227 reg->num_components, name);
228
229 /* push our SSA destination on the stack */
230 state->states[index].index++;
231 assert(state->states[index].index < state->states[index].stack_size);
232 state->states[index].stack[state->states[index].index] = &dest->ssa;
233 state->states[index].num_defs++;
234
235 _mesa_hash_table_insert(state->ssa_map, &dest->ssa, reg);
236
237 return true;
238 }
239
240 static void
241 rewrite_alu_instr_forward(nir_alu_instr *instr, rewrite_state *state)
242 {
243 state->parent_instr = &instr->instr;
244
245 nir_foreach_src(&instr->instr, rewrite_use, state);
246
247 nir_register *reg = instr->dest.dest.reg.reg;
248 unsigned index = reg->index;
249
250 if (state->states[index].stack == NULL)
251 return;
252
253 unsigned write_mask = instr->dest.write_mask;
254 if (write_mask != (1 << instr->dest.dest.reg.reg->num_components) - 1) {
255 /*
256 * Calculate the number of components the final instruction, which for
257 * per-component things is the number of output components of the
258 * instruction and non-per-component things is the number of enabled
259 * channels in the write mask.
260 */
261 unsigned num_components;
262 if (nir_op_infos[instr->op].output_size == 0) {
263 unsigned temp = (write_mask & 0x5) + ((write_mask >> 1) & 0x5);
264 num_components = (temp & 0x3) + ((temp >> 2) & 0x3);
265 } else {
266 num_components = nir_op_infos[instr->op].output_size;
267 }
268
269 char *name = NULL;
270 if (instr->dest.dest.reg.reg->name)
271 name = ralloc_asprintf(state->mem_ctx, "%s_%u",
272 reg->name, state->states[index].num_defs);
273
274 instr->dest.write_mask = (1 << num_components) - 1;
275 instr->dest.dest.is_ssa = true;
276 nir_ssa_def_init(&instr->instr, &instr->dest.dest.ssa,
277 num_components, name);
278
279 if (nir_op_infos[instr->op].output_size == 0) {
280 /*
281 * When we change the output writemask, we need to change the
282 * swizzles for per-component inputs too
283 */
284 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
285 if (nir_op_infos[instr->op].input_sizes[i] != 0)
286 continue;
287
288 unsigned new_swizzle[4] = {0, 0, 0, 0};
289
290 /*
291 * We keep two indices:
292 * 1. The index of the original (non-SSA) component
293 * 2. The index of the post-SSA, compacted, component
294 *
295 * We need to map the swizzle component at index 1 to the swizzle
296 * component at index 2.
297 */
298
299 unsigned ssa_index = 0;
300 for (unsigned index = 0; index < 4; index++) {
301 if (!((write_mask >> index) & 1))
302 continue;
303
304 new_swizzle[ssa_index] = instr->src[i].swizzle[index];
305 ssa_index++;
306 }
307
308 for (unsigned j = 0; j < 4; j++)
309 instr->src[i].swizzle[j] = new_swizzle[j];
310 }
311 }
312
313 nir_op op;
314 switch (reg->num_components) {
315 case 2: op = nir_op_vec2; break;
316 case 3: op = nir_op_vec3; break;
317 case 4: op = nir_op_vec4; break;
318 default: assert(0); break;
319 }
320
321 nir_alu_instr *vec = nir_alu_instr_create(state->mem_ctx, op);
322
323 vec->dest.dest.reg.reg = reg;
324 vec->dest.write_mask = (1 << reg->num_components) - 1;
325
326 nir_ssa_def *old_src = get_ssa_src(reg, state);
327 nir_ssa_def *new_src = &instr->dest.dest.ssa;
328
329 unsigned ssa_index = 0;
330 for (unsigned i = 0; i < reg->num_components; i++) {
331 vec->src[i].src.is_ssa = true;
332 if ((write_mask >> i) & 1) {
333 vec->src[i].src.ssa = new_src;
334 if (nir_op_infos[instr->op].output_size == 0)
335 vec->src[i].swizzle[0] = ssa_index;
336 else
337 vec->src[i].swizzle[0] = i;
338 ssa_index++;
339 } else {
340 vec->src[i].src.ssa = old_src;
341 vec->src[i].swizzle[0] = i;
342 }
343 }
344
345 nir_instr_insert_after(&instr->instr, &vec->instr);
346
347 state->parent_instr = &vec->instr;
348 rewrite_def_forwards(&vec->dest.dest, state);
349 } else {
350 rewrite_def_forwards(&instr->dest.dest, state);
351 }
352 }
353
354 static void
355 rewrite_phi_instr(nir_phi_instr *instr, rewrite_state *state)
356 {
357 state->parent_instr = &instr->instr;
358 rewrite_def_forwards(&instr->dest, state);
359 }
360
361 static void
362 rewrite_instr_forward(nir_instr *instr, rewrite_state *state)
363 {
364 if (instr->type == nir_instr_type_alu) {
365 rewrite_alu_instr_forward(nir_instr_as_alu(instr), state);
366 return;
367 }
368
369 if (instr->type == nir_instr_type_phi) {
370 rewrite_phi_instr(nir_instr_as_phi(instr), state);
371 return;
372 }
373
374 state->parent_instr = instr;
375
376 nir_foreach_src(instr, rewrite_use, state);
377 nir_foreach_dest(instr, rewrite_def_forwards, state);
378 }
379
380 static void
381 rewrite_phi_sources(nir_block *block, nir_block *pred, rewrite_state *state)
382 {
383 nir_foreach_instr(block, instr) {
384 if (instr->type != nir_instr_type_phi)
385 break;
386
387 nir_phi_instr *phi_instr = nir_instr_as_phi(instr);
388
389 state->parent_instr = instr;
390
391 foreach_list_typed(nir_phi_src, src, node, &phi_instr->srcs) {
392 if (src->pred == pred) {
393 rewrite_use(&src->src, state);
394 break;
395 }
396 }
397 }
398 }
399
400 static bool
401 rewrite_def_backwards(nir_dest *dest, void *_state)
402 {
403 rewrite_state *state = (rewrite_state *) _state;
404
405 if (!dest->is_ssa)
406 return true;
407
408 struct hash_entry *entry =
409 _mesa_hash_table_search(state->ssa_map, &dest->ssa);
410
411 if (!entry)
412 return true;
413
414 nir_register *reg = (nir_register *) entry->data;
415 unsigned index = reg->index;
416
417 state->states[index].index--;
418 assert(state->states[index].index >= -1);
419
420 return true;
421 }
422
423 static void
424 rewrite_instr_backwards(nir_instr *instr, rewrite_state *state)
425 {
426 nir_foreach_dest(instr, rewrite_def_backwards, state);
427 }
428
429 static void
430 rewrite_block(nir_block *block, rewrite_state *state)
431 {
432 /* This will skip over any instructions after the current one, which is
433 * what we want because those instructions (vector gather, conditional
434 * select) will already be in SSA form.
435 */
436 nir_foreach_instr_safe(block, instr) {
437 rewrite_instr_forward(instr, state);
438 }
439
440 if (block != state->impl->end_block &&
441 !nir_cf_node_is_last(&block->cf_node) &&
442 nir_cf_node_next(&block->cf_node)->type == nir_cf_node_if) {
443 nir_if *if_stmt = nir_cf_node_as_if(nir_cf_node_next(&block->cf_node));
444 state->parent_instr = NULL;
445 state->parent_if = if_stmt;
446 rewrite_use(&if_stmt->condition, state);
447 }
448
449 if (block->successors[0])
450 rewrite_phi_sources(block->successors[0], block, state);
451 if (block->successors[1])
452 rewrite_phi_sources(block->successors[1], block, state);
453
454 for (unsigned i = 0; i < block->num_dom_children; i++)
455 rewrite_block(block->dom_children[i], state);
456
457 nir_foreach_instr_reverse(block, instr) {
458 rewrite_instr_backwards(instr, state);
459 }
460 }
461
462 static void
463 remove_unused_regs(nir_function_impl *impl, rewrite_state *state)
464 {
465 foreach_list_typed_safe(nir_register, reg, node, &impl->registers) {
466 if (state->states[reg->index].stack != NULL)
467 exec_node_remove(&reg->node);
468 }
469 }
470
471 static void
472 init_rewrite_state(nir_function_impl *impl, rewrite_state *state)
473 {
474 state->impl = impl;
475 state->mem_ctx = ralloc_parent(impl);
476 state->ssa_map = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
477 _mesa_key_pointer_equal);
478 state->states = ralloc_array(NULL, reg_state, impl->reg_alloc);
479
480 foreach_list_typed(nir_register, reg, node, &impl->registers) {
481 assert(reg->index < impl->reg_alloc);
482 if (reg->num_array_elems > 0) {
483 state->states[reg->index].stack = NULL;
484 } else {
485 /*
486 * Calculate a conservative estimate of the stack size based on the
487 * number of definitions there are. Note that this function *must* be
488 * called after phi nodes are inserted so we can count phi node
489 * definitions too.
490 */
491 unsigned stack_size = reg->defs->entries;
492
493 state->states[reg->index].stack = ralloc_array(state->states,
494 nir_ssa_def *,
495 stack_size);
496 #ifdef DEBUG
497 state->states[reg->index].stack_size = stack_size;
498 #endif
499 state->states[reg->index].index = -1;
500 state->states[reg->index].num_defs = 0;
501 }
502 }
503 }
504
505 static void
506 destroy_rewrite_state(rewrite_state *state)
507 {
508 _mesa_hash_table_destroy(state->ssa_map, NULL);
509 ralloc_free(state->states);
510 }
511
512 void
513 nir_convert_to_ssa_impl(nir_function_impl *impl)
514 {
515 nir_metadata_require(impl, nir_metadata_dominance);
516
517 insert_phi_nodes(impl);
518
519 rewrite_state state;
520 init_rewrite_state(impl, &state);
521
522 rewrite_block(impl->start_block, &state);
523
524 remove_unused_regs(impl, &state);
525
526 nir_metadata_preserve(impl, nir_metadata_block_index |
527 nir_metadata_dominance);
528
529 destroy_rewrite_state(&state);
530 }
531
532 void
533 nir_convert_to_ssa(nir_shader *shader)
534 {
535 nir_foreach_overload(shader, overload) {
536 if (overload->impl)
537 nir_convert_to_ssa_impl(overload->impl);
538 }
539 }