nv50: attempt at making more complicated loops work
[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 (s == 1 &&
116 value->reg.file >= NV_FILE_MEM_C(0) &&
117 value->reg.file <= NV_FILE_MEM_C(15))
118 return TRUE;
119 if (s == 2 && nvi->src[1]->value->reg.file == NV_FILE_GPR)
120 return TRUE;
121 return FALSE;
122 case NV_OP_MOV:
123 assert(s == 0);
124 return TRUE;
125 default:
126 return FALSE;
127 }
128 }
129
130 /* Return whether this instruction can be executed conditionally. */
131 boolean
132 nv50_nvi_can_predicate(struct nv_instruction *nvi)
133 {
134 int i;
135
136 if (nvi->flags_src)
137 return FALSE;
138 for (i = 0; i < 4 && nvi->src[i]; ++i)
139 if (nvi->src[i]->value->reg.file == NV_FILE_IMM)
140 return FALSE;
141 return TRUE;
142 }
143
144 ubyte
145 nv50_supported_src_mods(uint opcode, int s)
146 {
147 switch (opcode) {
148 case NV_OP_ABS:
149 return NV_MOD_NEG | NV_MOD_ABS; /* obviously */
150 case NV_OP_ADD:
151 case NV_OP_MUL:
152 case NV_OP_MAD:
153 return NV_MOD_NEG;
154 case NV_OP_DFDX:
155 case NV_OP_DFDY:
156 assert(s == 0);
157 return NV_MOD_NEG;
158 case NV_OP_MAX:
159 case NV_OP_MIN:
160 return NV_MOD_ABS;
161 case NV_OP_CVT:
162 case NV_OP_LG2:
163 case NV_OP_NEG:
164 case NV_OP_PREEX2:
165 case NV_OP_PRESIN:
166 case NV_OP_RCP:
167 case NV_OP_RSQ:
168 return NV_MOD_ABS | NV_MOD_NEG;
169 default:
170 return 0;
171 }
172 }
173
174 int
175 nv_nvi_refcount(struct nv_instruction *nvi)
176 {
177 int i, rc;
178
179 rc = nvi->flags_def ? nvi->flags_def->refc : 0;
180
181 for (i = 0; i < 4; ++i) {
182 if (!nvi->def[i])
183 return rc;
184 rc += nvi->def[i]->refc;
185 }
186 return rc;
187 }
188
189 int
190 nvcg_replace_value(struct nv_pc *pc, struct nv_value *old_val,
191 struct nv_value *new_val)
192 {
193 int i, n;
194
195 if (old_val == new_val)
196 return old_val->refc;
197
198 for (i = 0, n = 0; i < pc->num_refs; ++i) {
199 if (pc->refs[i]->value == old_val) {
200 ++n;
201 nv_reference(pc, &pc->refs[i], new_val);
202 }
203 }
204 return n;
205 }
206
207 static void
208 nv_pc_free_refs(struct nv_pc *pc)
209 {
210 int i;
211 for (i = 0; i < pc->num_refs; i += 64)
212 FREE(pc->refs[i]);
213 }
214
215 static const char *
216 edge_name(ubyte type)
217 {
218 switch (type) {
219 case CFG_EDGE_FORWARD: return "forward";
220 case CFG_EDGE_BACK: return "back";
221 case CFG_EDGE_LOOP_ENTER: return "loop";
222 case CFG_EDGE_LOOP_LEAVE: return "break";
223 case CFG_EDGE_FAKE: return "fake";
224 default:
225 return "?";
226 }
227 }
228
229 void
230 nv_pc_pass_in_order(struct nv_basic_block *root, nv_pc_pass_func f, void *priv)
231 {
232 struct nv_basic_block *bb[64], *bbb[16], *b;
233 int j, p, pp;
234
235 bb[0] = root;
236 p = 1;
237 pp = 0;
238
239 while (p > 0) {
240 b = bb[--p];
241 b->priv = 0;
242
243 for (j = 1; j >= 0; --j) {
244 if (!b->out[j])
245 continue;
246
247 switch (b->out_kind[j]) {
248 case CFG_EDGE_BACK:
249 continue;
250 case CFG_EDGE_FORWARD:
251 case CFG_EDGE_FAKE:
252 if (++b->out[j]->priv == b->out[j]->num_in)
253 bb[p++] = b->out[j];
254 break;
255 case CFG_EDGE_LOOP_ENTER:
256 bb[p++] = b->out[j];
257 break;
258 case CFG_EDGE_LOOP_LEAVE:
259 bbb[pp++] = b->out[j];
260 break;
261 default:
262 assert(0);
263 break;
264 }
265 }
266
267 f(priv, b);
268
269 if (!p) {
270 p = pp;
271 for (; pp > 0; --pp)
272 bb[pp - 1] = bbb[pp - 1];
273 }
274 }
275 }
276
277 static void
278 nv_do_print_program(void *priv, struct nv_basic_block *b)
279 {
280 struct nv_instruction *i = b->phi;
281
282 debug_printf("=== BB %i ", b->id);
283 if (b->out[0])
284 debug_printf("[%s -> %i] ", edge_name(b->out_kind[0]), b->out[0]->id);
285 if (b->out[1])
286 debug_printf("[%s -> %i] ", edge_name(b->out_kind[1]), b->out[1]->id);
287 debug_printf("===\n");
288
289 i = b->phi;
290 if (!i)
291 i = b->entry;
292 for (; i; i = i->next)
293 nv_print_instruction(i);
294 }
295
296 void
297 nv_print_program(struct nv_basic_block *root)
298 {
299 nv_pc_pass_in_order(root, nv_do_print_program, root);
300
301 debug_printf("END\n\n");
302 }
303
304 static INLINE void
305 nvcg_show_bincode(struct nv_pc *pc)
306 {
307 int i;
308
309 for (i = 0; i < pc->bin_size / 4; ++i)
310 debug_printf("0x%08x ", pc->emit[i]);
311 debug_printf("\n");
312 }
313
314 static int
315 nv50_emit_program(struct nv_pc *pc)
316 {
317 uint32_t *code = pc->emit;
318 int n;
319
320 NV50_DBGMSG("emitting program: size = %u\n", pc->bin_size);
321
322 for (n = 0; n < pc->num_blocks; ++n) {
323 struct nv_instruction *i;
324 struct nv_basic_block *b = pc->bb_list[n];
325
326 for (i = b->entry; i; i = i->next) {
327 nv50_emit_instruction(pc, i);
328
329 pc->bin_pos += 1 + (pc->emit[0] & 1);
330 pc->emit += 1 + (pc->emit[0] & 1);
331 }
332 }
333 assert(pc->emit == &code[pc->bin_size / 4]);
334
335 /* XXX: we can do better than this ... */
336 if (!(pc->emit[-2] & 1) || (pc->emit[-2] & 2) || (pc->emit[-1] & 3)) {
337 pc->emit[0] = 0xf0000001;
338 pc->emit[1] = 0xe0000000;
339 pc->bin_size += 8;
340 }
341
342 pc->emit = code;
343 code[pc->bin_size / 4 - 1] |= 1;
344
345 #ifdef NV50PC_DEBUG
346 nvcg_show_bincode(pc);
347 #endif
348
349 return 0;
350 }
351
352 int
353 nv50_generate_code(struct nv50_translation_info *ti)
354 {
355 struct nv_pc *pc;
356 int ret;
357
358 pc = CALLOC_STRUCT(nv_pc);
359 if (!pc)
360 return 1;
361
362 ret = nv50_tgsi_to_nc(pc, ti);
363 if (ret)
364 goto out;
365 #ifdef NV50PC_DEBUG
366 nv_print_program(pc->root);
367 #endif
368
369 /* optimization */
370 ret = nv_pc_exec_pass0(pc);
371 if (ret)
372 goto out;
373 #ifdef NV50PC_DEBUG
374 nv_print_program(pc->root);
375 #endif
376
377 /* register allocation */
378 ret = nv_pc_exec_pass1(pc);
379 if (ret)
380 goto out;
381 #ifdef NV50PC_DEBUG
382 nv_print_program(pc->root);
383 #endif
384
385 /* prepare for emission */
386 ret = nv_pc_exec_pass2(pc);
387 if (ret)
388 goto out;
389
390 pc->emit = CALLOC(pc->bin_size / 4 + 2, 4);
391 if (!pc->emit) {
392 ret = 3;
393 goto out;
394 }
395 ret = nv50_emit_program(pc);
396 if (ret)
397 goto out;
398
399 ti->p->code_size = pc->bin_size;
400 ti->p->code = pc->emit;
401
402 ti->p->immd_size = pc->immd_count * 4;
403 ti->p->immd = pc->immd_buf;
404
405 /* highest 16 bit reg to num of 32 bit regs */
406 ti->p->max_gpr = (pc->max_reg[NV_FILE_GPR] >> 1) + 1;
407
408 ti->p->fixups = pc->fixups;
409 ti->p->num_fixups = pc->num_fixups;
410
411 NV50_DBGMSG("SHADER TRANSLATION - %s\n", ret ? "failure" : "success");
412
413 out:
414 nv_pc_free_refs(pc);
415 if (ret) {
416 if (pc->emit)
417 free(pc->emit);
418 if (pc->immd_buf)
419 free(pc->immd_buf);
420 if (pc->fixups)
421 free(pc->fixups);
422 }
423 free(pc);
424
425 return ret;
426 }
427
428 static void
429 nvbb_insert_phi(struct nv_basic_block *b, struct nv_instruction *i)
430 {
431 if (!b->phi) {
432 i->prev = NULL;
433 b->phi = i;
434 i->next = b->entry;
435 if (b->entry) {
436 assert(!b->entry->prev && b->exit);
437 b->entry->prev = i;
438 } else {
439 b->entry = i;
440 b->exit = i;
441 }
442 } else {
443 assert(b->entry);
444 if (b->entry->opcode == NV_OP_PHI) { /* insert after entry */
445 assert(b->entry == b->exit);
446 b->entry->next = i;
447 i->prev = b->entry;
448 b->entry = i;
449 b->exit = i;
450 } else { /* insert before entry */
451 assert(b->entry->prev && b->exit);
452 i->next = b->entry;
453 i->prev = b->entry->prev;
454 b->entry->prev = i;
455 i->prev->next = i;
456 }
457 }
458 }
459
460 void
461 nvbb_insert_tail(struct nv_basic_block *b, struct nv_instruction *i)
462 {
463 if (i->opcode == NV_OP_PHI) {
464 nvbb_insert_phi(b, i);
465 } else {
466 i->prev = b->exit;
467 if (b->exit)
468 b->exit->next = i;
469 b->exit = i;
470 if (!b->entry)
471 b->entry = i;
472 else
473 if (i->prev && i->prev->opcode == NV_OP_PHI)
474 b->entry = i;
475 }
476
477 i->bb = b;
478 b->num_instructions++;
479 }
480
481 void
482 nv_nvi_delete(struct nv_instruction *nvi)
483 {
484 struct nv_basic_block *b = nvi->bb;
485 int j;
486
487 /* debug_printf("REM: "); nv_print_instruction(nvi); */
488
489 for (j = 0; j < 5; ++j)
490 nv_reference(NULL, &nvi->src[j], NULL);
491 nv_reference(NULL, &nvi->flags_src, NULL);
492
493 if (nvi->next)
494 nvi->next->prev = nvi->prev;
495 else {
496 assert(nvi == b->exit);
497 b->exit = nvi->prev;
498 }
499
500 if (nvi->prev)
501 nvi->prev->next = nvi->next;
502
503 if (nvi == b->entry) {
504 /* PHIs don't get hooked to b->entry */
505 b->entry = nvi->next;
506 assert(!nvi->prev || nvi->prev->opcode == NV_OP_PHI);
507 }
508
509 if (nvi == b->phi) {
510 if (nvi->opcode != NV_OP_PHI)
511 NV50_DBGMSG("NOTE: b->phi points to non-PHI instruction\n");
512
513 assert(!nvi->prev);
514 if (!nvi->next || nvi->next->opcode != NV_OP_PHI)
515 b->phi = NULL;
516 else
517 b->phi = nvi->next;
518 }
519 }
520
521 void
522 nv_nvi_permute(struct nv_instruction *i1, struct nv_instruction *i2)
523 {
524 struct nv_basic_block *b = i1->bb;
525
526 assert(i1->opcode != NV_OP_PHI &&
527 i2->opcode != NV_OP_PHI);
528 assert(i1->next == i2);
529
530 if (b->exit == i2)
531 b->exit = i1;
532
533 if (b->entry == i1)
534 b->entry = i2;
535
536 i2->prev = i1->prev;
537 i1->next = i2->next;
538 i2->next = i1;
539 i1->prev = i2;
540
541 if (i2->prev)
542 i2->prev->next = i2;
543 if (i1->next)
544 i1->next->prev = i1;
545 }
546
547 void
548 nvbb_attach_block(struct nv_basic_block *parent,
549 struct nv_basic_block *b, ubyte edge_kind)
550 {
551 assert(b->num_in < 8);
552
553 if (parent->out[0]) {
554 assert(!parent->out[1]);
555 parent->out[1] = b;
556 parent->out_kind[1] = edge_kind;
557 } else {
558 parent->out[0] = b;
559 parent->out_kind[0] = edge_kind;
560 }
561
562 b->in[b->num_in] = parent;
563 b->in_kind[b->num_in++] = edge_kind;
564 }
565
566 /* NOTE: all BRKs are treated as conditional, so there are 2 outgoing BBs */
567
568 boolean
569 nvbb_dominated_by(struct nv_basic_block *b, struct nv_basic_block *d)
570 {
571 int j;
572
573 if (b == d)
574 return TRUE;
575
576 for (j = 0; j < b->num_in; ++j)
577 if ((b->in_kind[j] != CFG_EDGE_BACK) && !nvbb_dominated_by(b->in[j], d))
578 return FALSE;
579
580 return j ? TRUE : FALSE;
581 }
582
583 /* check if bf (future) can be reached from bp (past) */
584 boolean
585 nvbb_reachable_by(struct nv_basic_block *bf, struct nv_basic_block *bp,
586 struct nv_basic_block *bt)
587 {
588 if (bf == bp)
589 return TRUE;
590 if (bp == bt)
591 return FALSE;
592
593 if (bp->out[0] && !IS_WALL_EDGE(bp->out_kind[0]) &&
594 nvbb_reachable_by(bf, bp->out[0], bt))
595 return TRUE;
596 if (bp->out[1] && !IS_WALL_EDGE(bp->out_kind[1]) &&
597 nvbb_reachable_by(bf, bp->out[1], bt))
598 return TRUE;
599 return FALSE;
600 }
601
602 static struct nv_basic_block *
603 nvbb_find_dom_frontier(struct nv_basic_block *b, struct nv_basic_block *df)
604 {
605 int i;
606
607 if (!nvbb_dominated_by(df, b)) {
608 for (i = 0; i < df->num_in; ++i) {
609 if (df->in_kind[i] == CFG_EDGE_BACK)
610 continue;
611 if (nvbb_dominated_by(df->in[i], b))
612 return df;
613 }
614 }
615 for (i = 0; i < 2 && b->out[i]; ++i) {
616 if (b->out_kind[i] == CFG_EDGE_BACK)
617 continue;
618 if ((df = nvbb_find_dom_frontier(b, b->out[i])))
619 return df;
620 }
621 return NULL;
622 }
623
624 struct nv_basic_block *
625 nvbb_dom_frontier(struct nv_basic_block *b)
626 {
627 struct nv_basic_block *df;
628 int i;
629
630 for (i = 0; i < 2 && b->out[i]; ++i)
631 if ((df = nvbb_find_dom_frontier(b, b->out[i])))
632 return df;
633 return NULL;
634 }