nv50: reduce bb_reachable_by runtime from pot to linear
[mesa.git] / src / gallium / drivers / nv50 / nv50_pc.c
1 /*
2 * Copyright 2010 Christoph Bumiller
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 /* #define NV50PC_DEBUG */
24
25 #include "nv50_pc.h"
26 #include "nv50_program.h"
27
28 #include <stdio.h>
29
30 /* returns TRUE if operands 0 and 1 can be swapped */
31 boolean
32 nv_op_commutative(uint opcode)
33 {
34 switch (opcode) {
35 case NV_OP_ADD:
36 case NV_OP_MUL:
37 case NV_OP_MAD:
38 case NV_OP_AND:
39 case NV_OP_OR:
40 case NV_OP_XOR:
41 case NV_OP_MIN:
42 case NV_OP_MAX:
43 case NV_OP_SAD:
44 return TRUE;
45 default:
46 return FALSE;
47 }
48 }
49
50 /* return operand to which the address register applies */
51 int
52 nv50_indirect_opnd(struct nv_instruction *i)
53 {
54 if (!i->src[4])
55 return -1;
56
57 switch (i->opcode) {
58 case NV_OP_MOV:
59 case NV_OP_LDA:
60 return 0;
61 default:
62 return 1;
63 }
64 }
65
66 boolean
67 nv50_nvi_can_use_imm(struct nv_instruction *nvi, int s)
68 {
69 if (nvi->flags_src || nvi->flags_def)
70 return FALSE;
71
72 switch (nvi->opcode) {
73 case NV_OP_ADD:
74 case NV_OP_MUL:
75 case NV_OP_AND:
76 case NV_OP_OR:
77 case NV_OP_XOR:
78 case NV_OP_SHL:
79 case NV_OP_SHR:
80 return (s == 1) && (nvi->src[0]->value->reg.file == NV_FILE_GPR) &&
81 (nvi->def[0]->reg.file == NV_FILE_GPR);
82 case NV_OP_MOV:
83 assert(s == 0);
84 return (nvi->def[0]->reg.file == NV_FILE_GPR);
85 default:
86 return FALSE;
87 }
88 }
89
90 boolean
91 nv50_nvi_can_load(struct nv_instruction *nvi, int s, struct nv_value *value)
92 {
93 int i;
94
95 for (i = 0; i < 3 && nvi->src[i]; ++i)
96 if (nvi->src[i]->value->reg.file == NV_FILE_IMM)
97 return FALSE;
98
99 switch (nvi->opcode) {
100 case NV_OP_ABS:
101 case NV_OP_ADD:
102 case NV_OP_CEIL:
103 case NV_OP_FLOOR:
104 case NV_OP_TRUNC:
105 case NV_OP_CVT:
106 case NV_OP_MAD:
107 case NV_OP_MUL:
108 case NV_OP_SAT:
109 case NV_OP_SUB:
110 case NV_OP_MAX:
111 case NV_OP_MIN:
112 if (s == 0 && (value->reg.file == NV_FILE_MEM_S ||
113 value->reg.file == NV_FILE_MEM_P))
114 return TRUE;
115 if (value->reg.file < NV_FILE_MEM_C(0) ||
116 value->reg.file > NV_FILE_MEM_C(15))
117 return FALSE;
118 return (s == 1) ||
119 ((s == 2) && (nvi->src[1]->value->reg.file == NV_FILE_GPR));
120 case NV_OP_MOV:
121 assert(s == 0);
122 return /* TRUE */ FALSE; /* don't turn MOVs into loads */
123 default:
124 return FALSE;
125 }
126 }
127
128 /* Return whether this instruction can be executed conditionally. */
129 boolean
130 nv50_nvi_can_predicate(struct nv_instruction *nvi)
131 {
132 int i;
133
134 if (nvi->flags_src)
135 return FALSE;
136 for (i = 0; i < 4 && nvi->src[i]; ++i)
137 if (nvi->src[i]->value->reg.file == NV_FILE_IMM)
138 return FALSE;
139 return TRUE;
140 }
141
142 ubyte
143 nv50_supported_src_mods(uint opcode, int s)
144 {
145 switch (opcode) {
146 case NV_OP_ABS:
147 return NV_MOD_NEG | NV_MOD_ABS; /* obviously */
148 case NV_OP_ADD:
149 case NV_OP_MUL:
150 case NV_OP_MAD:
151 return NV_MOD_NEG;
152 case NV_OP_DFDX:
153 case NV_OP_DFDY:
154 assert(s == 0);
155 return NV_MOD_NEG;
156 case NV_OP_MAX:
157 case NV_OP_MIN:
158 return NV_MOD_ABS;
159 case NV_OP_CVT:
160 case NV_OP_LG2:
161 case NV_OP_NEG:
162 case NV_OP_PREEX2:
163 case NV_OP_PRESIN:
164 case NV_OP_RCP:
165 case NV_OP_RSQ:
166 return NV_MOD_ABS | NV_MOD_NEG;
167 default:
168 return 0;
169 }
170 }
171
172 int
173 nv_nvi_refcount(struct nv_instruction *nvi)
174 {
175 int i, rc;
176
177 rc = nvi->flags_def ? nvi->flags_def->refc : 0;
178
179 for (i = 0; i < 4; ++i) {
180 if (!nvi->def[i])
181 return rc;
182 rc += nvi->def[i]->refc;
183 }
184 return rc;
185 }
186
187 int
188 nvcg_replace_value(struct nv_pc *pc, struct nv_value *old_val,
189 struct nv_value *new_val)
190 {
191 int i, n;
192
193 if (old_val == new_val)
194 return old_val->refc;
195
196 for (i = 0, n = 0; i < pc->num_refs; ++i) {
197 if (pc->refs[i]->value == old_val) {
198 ++n;
199 nv_reference(pc, &pc->refs[i], new_val);
200 }
201 }
202 return n;
203 }
204
205 struct nv_value *
206 nvcg_find_constant(struct nv_ref *ref)
207 {
208 struct nv_value *src;
209
210 if (!ref)
211 return NULL;
212
213 src = ref->value;
214 while (src->insn && src->insn->opcode == NV_OP_MOV) {
215 assert(!src->insn->src[0]->mod);
216 src = src->insn->src[0]->value;
217 }
218 if ((src->reg.file == NV_FILE_IMM) ||
219 (src->insn && src->insn->opcode == NV_OP_LDA &&
220 src->insn->src[0]->value->reg.file >= NV_FILE_MEM_C(0) &&
221 src->insn->src[0]->value->reg.file <= NV_FILE_MEM_C(15)))
222 return src;
223 return NULL;
224 }
225
226 struct nv_value *
227 nvcg_find_immediate(struct nv_ref *ref)
228 {
229 struct nv_value *src = nvcg_find_constant(ref);
230
231 return (src && src->reg.file == NV_FILE_IMM) ? src : NULL;
232 }
233
234 static void
235 nv_pc_free_refs(struct nv_pc *pc)
236 {
237 int i;
238 for (i = 0; i < pc->num_refs; i += 64)
239 FREE(pc->refs[i]);
240 }
241
242 static const char *
243 edge_name(ubyte type)
244 {
245 switch (type) {
246 case CFG_EDGE_FORWARD: return "forward";
247 case CFG_EDGE_BACK: return "back";
248 case CFG_EDGE_LOOP_ENTER: return "loop";
249 case CFG_EDGE_LOOP_LEAVE: return "break";
250 case CFG_EDGE_FAKE: return "fake";
251 default:
252 return "?";
253 }
254 }
255
256 void
257 nv_pc_pass_in_order(struct nv_basic_block *root, nv_pc_pass_func f, void *priv)
258 {
259 struct nv_basic_block *bb[64], *bbb[16], *b;
260 int j, p, pp;
261
262 bb[0] = root;
263 p = 1;
264 pp = 0;
265
266 while (p > 0) {
267 b = bb[--p];
268 b->priv = 0;
269
270 for (j = 1; j >= 0; --j) {
271 if (!b->out[j])
272 continue;
273
274 switch (b->out_kind[j]) {
275 case CFG_EDGE_BACK:
276 continue;
277 case CFG_EDGE_FORWARD:
278 case CFG_EDGE_FAKE:
279 if (++b->out[j]->priv == b->out[j]->num_in)
280 bb[p++] = b->out[j];
281 break;
282 case CFG_EDGE_LOOP_ENTER:
283 bb[p++] = b->out[j];
284 break;
285 case CFG_EDGE_LOOP_LEAVE:
286 bbb[pp++] = b->out[j];
287 break;
288 default:
289 assert(0);
290 break;
291 }
292 }
293
294 f(priv, b);
295
296 if (!p) {
297 p = pp;
298 for (; pp > 0; --pp)
299 bb[pp - 1] = bbb[pp - 1];
300 }
301 }
302 }
303
304 static void
305 nv_do_print_function(void *priv, struct nv_basic_block *b)
306 {
307 struct nv_instruction *i = b->phi;
308
309 debug_printf("=== BB %i ", b->id);
310 if (b->out[0])
311 debug_printf("[%s -> %i] ", edge_name(b->out_kind[0]), b->out[0]->id);
312 if (b->out[1])
313 debug_printf("[%s -> %i] ", edge_name(b->out_kind[1]), b->out[1]->id);
314 debug_printf("===\n");
315
316 i = b->phi;
317 if (!i)
318 i = b->entry;
319 for (; i; i = i->next)
320 nv_print_instruction(i);
321 }
322
323 void
324 nv_print_function(struct nv_basic_block *root)
325 {
326 if (root->subroutine)
327 debug_printf("SUBROUTINE %i\n", root->subroutine);
328 else
329 debug_printf("MAIN\n");
330
331 nv_pc_pass_in_order(root, nv_do_print_function, root);
332 }
333
334 void
335 nv_print_program(struct nv_pc *pc)
336 {
337 int i;
338 for (i = 0; i < pc->num_subroutines + 1; ++i)
339 if (pc->root[i])
340 nv_print_function(pc->root[i]);
341 }
342
343 #ifdef NV50_PC_DEBUG
344 static void
345 nv_do_print_cfgraph(struct nv_pc *pc, FILE *f, struct nv_basic_block *b)
346 {
347 int i;
348
349 b->pass_seq = pc->pass_seq;
350
351 fprintf(f, "\t%i [shape=box]\n", b->id);
352
353 for (i = 0; i < 2; ++i) {
354 if (!b->out[i])
355 continue;
356 switch (b->out_kind[i]) {
357 case CFG_EDGE_FORWARD:
358 fprintf(f, "\t%i -> %i;\n", b->id, b->out[i]->id);
359 break;
360 case CFG_EDGE_LOOP_ENTER:
361 fprintf(f, "\t%i -> %i [color=green];\n", b->id, b->out[i]->id);
362 break;
363 case CFG_EDGE_LOOP_LEAVE:
364 fprintf(f, "\t%i -> %i [color=red];\n", b->id, b->out[i]->id);
365 break;
366 case CFG_EDGE_BACK:
367 fprintf(f, "\t%i -> %i;\n", b->id, b->out[i]->id);
368 continue;
369 case CFG_EDGE_FAKE:
370 fprintf(f, "\t%i -> %i [style=dotted];\n", b->id, b->out[i]->id);
371 break;
372 default:
373 assert(0);
374 break;
375 }
376 if (b->out[i]->pass_seq < pc->pass_seq)
377 nv_do_print_cfgraph(pc, f, b->out[i]);
378 }
379 }
380
381 /* Print the control flow graph of subroutine @subr (0 == MAIN) to a file. */
382 static void
383 nv_print_cfgraph(struct nv_pc *pc, const char *filepath, int subr)
384 {
385 FILE *f;
386
387 f = fopen(filepath, "a");
388 if (!f)
389 return;
390
391 fprintf(f, "digraph G {\n");
392
393 ++pc->pass_seq;
394
395 nv_do_print_cfgraph(pc, f, pc->root[subr]);
396
397 fprintf(f, "}\n");
398
399 fclose(f);
400 }
401 #endif
402
403 static INLINE void
404 nvcg_show_bincode(struct nv_pc *pc)
405 {
406 int i;
407
408 for (i = 0; i < pc->bin_size / 4; ++i)
409 debug_printf("0x%08x ", pc->emit[i]);
410 debug_printf("\n");
411 }
412
413 static int
414 nv50_emit_program(struct nv_pc *pc)
415 {
416 uint32_t *code = pc->emit;
417 int n;
418
419 NV50_DBGMSG("emitting program: size = %u\n", pc->bin_size);
420
421 for (n = 0; n < pc->num_blocks; ++n) {
422 struct nv_instruction *i;
423 struct nv_basic_block *b = pc->bb_list[n];
424
425 for (i = b->entry; i; i = i->next) {
426 nv50_emit_instruction(pc, i);
427
428 pc->bin_pos += 1 + (pc->emit[0] & 1);
429 pc->emit += 1 + (pc->emit[0] & 1);
430 }
431 }
432 assert(pc->emit == &code[pc->bin_size / 4]);
433
434 /* XXX: we can do better than this ... */
435 if (!(pc->emit[-2] & 1) || (pc->emit[-2] & 2) || (pc->emit[-1] & 3)) {
436 pc->emit[0] = 0xf0000001;
437 pc->emit[1] = 0xe0000000;
438 pc->bin_size += 8;
439 }
440
441 pc->emit = code;
442 code[pc->bin_size / 4 - 1] |= 1;
443
444 #ifdef NV50PC_DEBUG
445 nvcg_show_bincode(pc);
446 #endif
447
448 return 0;
449 }
450
451 int
452 nv50_generate_code(struct nv50_translation_info *ti)
453 {
454 struct nv_pc *pc;
455 int ret;
456 int i;
457
458 pc = CALLOC_STRUCT(nv_pc);
459 if (!pc)
460 return 1;
461
462 pc->root = CALLOC(ti->subr_nr + 1, sizeof(pc->root[0]));
463 if (!pc->root) {
464 FREE(pc);
465 return 1;
466 }
467 pc->num_subroutines = ti->subr_nr;
468
469 ret = nv50_tgsi_to_nc(pc, ti);
470 if (ret)
471 goto out;
472 #ifdef NV50PC_DEBUG
473 nv_print_program(pc);
474 #endif
475
476 pc->opt_reload_elim = ti->store_to_memory ? FALSE : TRUE;
477
478 /* optimization */
479 ret = nv_pc_exec_pass0(pc);
480 if (ret)
481 goto out;
482 #ifdef NV50PC_DEBUG
483 nv_print_program(pc);
484 #endif
485
486 /* register allocation */
487 ret = nv_pc_exec_pass1(pc);
488 if (ret)
489 goto out;
490 #ifdef NV50PC_DEBUG
491 nv_print_program(pc);
492 nv_print_cfgraph(pc, "nv50_shader_cfgraph.dot", 0);
493 #endif
494
495 /* prepare for emission */
496 ret = nv_pc_exec_pass2(pc);
497 if (ret)
498 goto out;
499
500 pc->emit = CALLOC(pc->bin_size / 4 + 2, 4);
501 if (!pc->emit) {
502 ret = 3;
503 goto out;
504 }
505 ret = nv50_emit_program(pc);
506 if (ret)
507 goto out;
508
509 ti->p->code_size = pc->bin_size;
510 ti->p->code = pc->emit;
511
512 ti->p->immd_size = pc->immd_count * 4;
513 ti->p->immd = pc->immd_buf;
514
515 /* highest 16 bit reg to num of 32 bit regs */
516 ti->p->max_gpr = (pc->max_reg[NV_FILE_GPR] >> 1) + 1;
517
518 ti->p->fixups = pc->fixups;
519 ti->p->num_fixups = pc->num_fixups;
520
521 NV50_DBGMSG("SHADER TRANSLATION - %s\n", ret ? "failure" : "success");
522
523 out:
524 nv_pc_free_refs(pc);
525
526 for (i = 0; i < pc->num_blocks; ++i)
527 FREE(pc->bb_list[i]);
528
529 if (ret) { /* on success, these will be referenced by nv50_program */
530 if (pc->emit)
531 FREE(pc->emit);
532 if (pc->immd_buf)
533 FREE(pc->immd_buf);
534 if (pc->fixups)
535 FREE(pc->fixups);
536 }
537 FREE(pc);
538 return ret;
539 }
540
541 static void
542 nvbb_insert_phi(struct nv_basic_block *b, struct nv_instruction *i)
543 {
544 if (!b->phi) {
545 i->prev = NULL;
546 b->phi = i;
547 i->next = b->entry;
548 if (b->entry) {
549 assert(!b->entry->prev && b->exit);
550 b->entry->prev = i;
551 } else {
552 b->entry = i;
553 b->exit = i;
554 }
555 } else {
556 assert(b->entry);
557 if (b->entry->opcode == NV_OP_PHI) { /* insert after entry */
558 assert(b->entry == b->exit);
559 b->entry->next = i;
560 i->prev = b->entry;
561 b->entry = i;
562 b->exit = i;
563 } else { /* insert before entry */
564 assert(b->entry->prev && b->exit);
565 i->next = b->entry;
566 i->prev = b->entry->prev;
567 b->entry->prev = i;
568 i->prev->next = i;
569 }
570 }
571 }
572
573 void
574 nvbb_insert_tail(struct nv_basic_block *b, struct nv_instruction *i)
575 {
576 if (i->opcode == NV_OP_PHI) {
577 nvbb_insert_phi(b, i);
578 } else {
579 i->prev = b->exit;
580 if (b->exit)
581 b->exit->next = i;
582 b->exit = i;
583 if (!b->entry)
584 b->entry = i;
585 else
586 if (i->prev && i->prev->opcode == NV_OP_PHI)
587 b->entry = i;
588 }
589
590 i->bb = b;
591 b->num_instructions++;
592 }
593
594 void
595 nvi_insert_after(struct nv_instruction *at, struct nv_instruction *ni)
596 {
597 if (!at->next) {
598 nvbb_insert_tail(at->bb, ni);
599 return;
600 }
601 ni->next = at->next;
602 ni->prev = at;
603 ni->next->prev = ni;
604 ni->prev->next = ni;
605 }
606
607 void
608 nv_nvi_delete(struct nv_instruction *nvi)
609 {
610 struct nv_basic_block *b = nvi->bb;
611 int j;
612
613 /* debug_printf("REM: "); nv_print_instruction(nvi); */
614
615 for (j = 0; j < 5; ++j)
616 nv_reference(NULL, &nvi->src[j], NULL);
617 nv_reference(NULL, &nvi->flags_src, NULL);
618
619 if (nvi->next)
620 nvi->next->prev = nvi->prev;
621 else {
622 assert(nvi == b->exit);
623 b->exit = nvi->prev;
624 }
625
626 if (nvi->prev)
627 nvi->prev->next = nvi->next;
628
629 if (nvi == b->entry) {
630 /* PHIs don't get hooked to b->entry */
631 b->entry = nvi->next;
632 assert(!nvi->prev || nvi->prev->opcode == NV_OP_PHI);
633 }
634
635 if (nvi == b->phi) {
636 if (nvi->opcode != NV_OP_PHI)
637 NV50_DBGMSG("NOTE: b->phi points to non-PHI instruction\n");
638
639 assert(!nvi->prev);
640 if (!nvi->next || nvi->next->opcode != NV_OP_PHI)
641 b->phi = NULL;
642 else
643 b->phi = nvi->next;
644 }
645 }
646
647 void
648 nv_nvi_permute(struct nv_instruction *i1, struct nv_instruction *i2)
649 {
650 struct nv_basic_block *b = i1->bb;
651
652 assert(i1->opcode != NV_OP_PHI &&
653 i2->opcode != NV_OP_PHI);
654 assert(i1->next == i2);
655
656 if (b->exit == i2)
657 b->exit = i1;
658
659 if (b->entry == i1)
660 b->entry = i2;
661
662 i2->prev = i1->prev;
663 i1->next = i2->next;
664 i2->next = i1;
665 i1->prev = i2;
666
667 if (i2->prev)
668 i2->prev->next = i2;
669 if (i1->next)
670 i1->next->prev = i1;
671 }
672
673 void
674 nvbb_attach_block(struct nv_basic_block *parent,
675 struct nv_basic_block *b, ubyte edge_kind)
676 {
677 assert(b->num_in < 8);
678
679 if (parent->out[0]) {
680 assert(!parent->out[1]);
681 parent->out[1] = b;
682 parent->out_kind[1] = edge_kind;
683 } else {
684 parent->out[0] = b;
685 parent->out_kind[0] = edge_kind;
686 }
687
688 b->in[b->num_in] = parent;
689 b->in_kind[b->num_in++] = edge_kind;
690 }
691
692 /* NOTE: all BRKs are treated as conditional, so there are 2 outgoing BBs */
693
694 boolean
695 nvbb_dominated_by(struct nv_basic_block *b, struct nv_basic_block *d)
696 {
697 int j;
698
699 if (b == d)
700 return TRUE;
701
702 for (j = 0; j < b->num_in; ++j)
703 if ((b->in_kind[j] != CFG_EDGE_BACK) && !nvbb_dominated_by(b->in[j], d))
704 return FALSE;
705
706 return j ? TRUE : FALSE;
707 }
708
709 /* check if @bf (future) can be reached from @bp (past), stop at @bt */
710 boolean
711 nvbb_reachable_by(struct nv_basic_block *bf, struct nv_basic_block *bp,
712 struct nv_basic_block *bt)
713 {
714 struct nv_basic_block *q[NV_PC_MAX_BASIC_BLOCKS], *b;
715 int i, p, n;
716
717 p = 0;
718 n = 1;
719 q[0] = bp;
720
721 while (p < n) {
722 b = q[p++];
723
724 if (b == bf)
725 break;
726 if (b == bt)
727 continue;
728 assert(n <= (1024 - 2));
729
730 for (i = 0; i < 2; ++i) {
731 if (b->out[i] && !IS_WALL_EDGE(b->out_kind[i]) && !b->out[i]->priv) {
732 q[n] = b->out[i];
733 q[n++]->priv = 1;
734 }
735 }
736 }
737 for (--n; n >= 0; --n)
738 q[n]->priv = 0;
739
740 return (b == bf);
741 }
742
743 static struct nv_basic_block *
744 nvbb_find_dom_frontier(struct nv_basic_block *b, struct nv_basic_block *df)
745 {
746 struct nv_basic_block *out;
747 int i;
748
749 if (!nvbb_dominated_by(df, b)) {
750 for (i = 0; i < df->num_in; ++i) {
751 if (df->in_kind[i] == CFG_EDGE_BACK)
752 continue;
753 if (nvbb_dominated_by(df->in[i], b))
754 return df;
755 }
756 }
757 for (i = 0; i < 2 && df->out[i]; ++i) {
758 if (df->out_kind[i] == CFG_EDGE_BACK)
759 continue;
760 if ((out = nvbb_find_dom_frontier(b, df->out[i])))
761 return out;
762 }
763 return NULL;
764 }
765
766 struct nv_basic_block *
767 nvbb_dom_frontier(struct nv_basic_block *b)
768 {
769 struct nv_basic_block *df;
770 int i;
771
772 for (i = 0; i < 2 && b->out[i]; ++i)
773 if ((df = nvbb_find_dom_frontier(b, b->out[i])))
774 return df;
775 return NULL;
776 }