2 * Copyright 2010 Christoph Bumiller
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 /* #define NV50PC_DEBUG */
26 #include "nv50_program.h"
30 /* returns TRUE if operands 0 and 1 can be swapped */
32 nv_op_commutative(uint opcode
)
50 /* return operand to which the address register applies */
52 nv50_indirect_opnd(struct nv_instruction
*i
)
68 nv50_nvi_can_use_imm(struct nv_instruction
*nvi
, int s
)
70 if (nvi
->flags_src
|| nvi
->flags_def
)
73 switch (nvi
->opcode
) {
81 return (s
== 1) && (nvi
->src
[0]->value
->reg
.file
== NV_FILE_GPR
) &&
82 (nvi
->def
[0]->reg
.file
== NV_FILE_GPR
);
85 return (nvi
->def
[0]->reg
.file
== NV_FILE_GPR
);
92 nv50_nvi_can_load(struct nv_instruction
*nvi
, int s
, struct nv_value
*value
)
96 for (i
= 0; i
< 3 && nvi
->src
[i
]; ++i
)
97 if (nvi
->src
[i
]->value
->reg
.file
== NV_FILE_IMM
)
100 switch (nvi
->opcode
) {
113 if (s
== 0 && (value
->reg
.file
== NV_FILE_MEM_S
||
114 value
->reg
.file
== NV_FILE_MEM_P
))
116 if (value
->reg
.file
< NV_FILE_MEM_C(0) ||
117 value
->reg
.file
> NV_FILE_MEM_C(15))
120 ((s
== 2) && (nvi
->src
[1]->value
->reg
.file
== NV_FILE_GPR
));
123 return /* TRUE */ FALSE
; /* don't turn MOVs into loads */
129 /* Return whether this instruction can be executed conditionally. */
131 nv50_nvi_can_predicate(struct nv_instruction
*nvi
)
137 for (i
= 0; i
< 4 && nvi
->src
[i
]; ++i
)
138 if (nvi
->src
[i
]->value
->reg
.file
== NV_FILE_IMM
)
144 nv50_supported_src_mods(uint opcode
, int s
)
148 return NV_MOD_NEG
| NV_MOD_ABS
; /* obviously */
167 return NV_MOD_ABS
| NV_MOD_NEG
;
174 nv_nvi_refcount(struct nv_instruction
*nvi
)
178 rc
= nvi
->flags_def
? nvi
->flags_def
->refc
: 0;
180 for (i
= 0; i
< 4; ++i
) {
183 rc
+= nvi
->def
[i
]->refc
;
189 nvcg_replace_value(struct nv_pc
*pc
, struct nv_value
*old_val
,
190 struct nv_value
*new_val
)
194 if (old_val
== new_val
)
195 return old_val
->refc
;
197 for (i
= 0, n
= 0; i
< pc
->num_refs
; ++i
) {
198 if (pc
->refs
[i
]->value
== old_val
) {
200 nv_reference(pc
, &pc
->refs
[i
], new_val
);
207 nvcg_find_constant(struct nv_ref
*ref
)
209 struct nv_value
*src
;
215 while (src
->insn
&& src
->insn
->opcode
== NV_OP_MOV
) {
216 assert(!src
->insn
->src
[0]->mod
);
217 src
= src
->insn
->src
[0]->value
;
219 if ((src
->reg
.file
== NV_FILE_IMM
) ||
220 (src
->insn
&& src
->insn
->opcode
== NV_OP_LDA
&&
221 src
->insn
->src
[0]->value
->reg
.file
>= NV_FILE_MEM_C(0) &&
222 src
->insn
->src
[0]->value
->reg
.file
<= NV_FILE_MEM_C(15)))
228 nvcg_find_immediate(struct nv_ref
*ref
)
230 struct nv_value
*src
= nvcg_find_constant(ref
);
232 return (src
&& src
->reg
.file
== NV_FILE_IMM
) ? src
: NULL
;
236 nv_pc_free_refs(struct nv_pc
*pc
)
239 for (i
= 0; i
< pc
->num_refs
; i
+= 64)
245 edge_name(ubyte type
)
248 case CFG_EDGE_FORWARD
: return "forward";
249 case CFG_EDGE_BACK
: return "back";
250 case CFG_EDGE_LOOP_ENTER
: return "loop";
251 case CFG_EDGE_LOOP_LEAVE
: return "break";
252 case CFG_EDGE_FAKE
: return "fake";
259 nv_pc_pass_in_order(struct nv_basic_block
*root
, nv_pc_pass_func f
, void *priv
)
261 struct nv_basic_block
*bb
[64], *bbb
[16], *b
;
272 for (j
= 1; j
>= 0; --j
) {
276 switch (b
->out_kind
[j
]) {
279 case CFG_EDGE_FORWARD
:
281 if (++b
->out
[j
]->priv
== b
->out
[j
]->num_in
)
284 case CFG_EDGE_LOOP_ENTER
:
287 case CFG_EDGE_LOOP_LEAVE
:
288 bbb
[pp
++] = b
->out
[j
];
301 bb
[pp
- 1] = bbb
[pp
- 1];
307 nv_do_print_function(void *priv
, struct nv_basic_block
*b
)
309 struct nv_instruction
*i
= b
->phi
;
311 debug_printf("=== BB %i ", b
->id
);
313 debug_printf("[%s -> %i] ", edge_name(b
->out_kind
[0]), b
->out
[0]->id
);
315 debug_printf("[%s -> %i] ", edge_name(b
->out_kind
[1]), b
->out
[1]->id
);
316 debug_printf("===\n");
321 for (; i
; i
= i
->next
)
322 nv_print_instruction(i
);
326 nv_print_function(struct nv_basic_block
*root
)
328 if (root
->subroutine
)
329 debug_printf("SUBROUTINE %i\n", root
->subroutine
);
331 debug_printf("MAIN\n");
333 nv_pc_pass_in_order(root
, nv_do_print_function
, root
);
337 nv_print_program(struct nv_pc
*pc
)
340 for (i
= 0; i
< pc
->num_subroutines
+ 1; ++i
)
342 nv_print_function(pc
->root
[i
]);
347 nv_do_print_cfgraph(struct nv_pc
*pc
, FILE *f
, struct nv_basic_block
*b
)
351 b
->pass_seq
= pc
->pass_seq
;
353 fprintf(f
, "\t%i [shape=box]\n", b
->id
);
355 for (i
= 0; i
< 2; ++i
) {
358 switch (b
->out_kind
[i
]) {
359 case CFG_EDGE_FORWARD
:
360 fprintf(f
, "\t%i -> %i;\n", b
->id
, b
->out
[i
]->id
);
362 case CFG_EDGE_LOOP_ENTER
:
363 fprintf(f
, "\t%i -> %i [color=green];\n", b
->id
, b
->out
[i
]->id
);
365 case CFG_EDGE_LOOP_LEAVE
:
366 fprintf(f
, "\t%i -> %i [color=red];\n", b
->id
, b
->out
[i
]->id
);
369 fprintf(f
, "\t%i -> %i;\n", b
->id
, b
->out
[i
]->id
);
372 fprintf(f
, "\t%i -> %i [style=dotted];\n", b
->id
, b
->out
[i
]->id
);
378 if (b
->out
[i
]->pass_seq
< pc
->pass_seq
)
379 nv_do_print_cfgraph(pc
, f
, b
->out
[i
]);
383 /* Print the control flow graph of subroutine @subr (0 == MAIN) to a file. */
385 nv_print_cfgraph(struct nv_pc
*pc
, const char *filepath
, int subr
)
389 f
= fopen(filepath
, "a");
393 fprintf(f
, "digraph G {\n");
397 nv_do_print_cfgraph(pc
, f
, pc
->root
[subr
]);
406 nvcg_show_bincode(struct nv_pc
*pc
)
410 for (i
= 0; i
< pc
->bin_size
/ 4; ++i
) {
411 debug_printf("0x%08x ", pc
->emit
[i
]);
419 nv50_emit_program(struct nv_pc
*pc
)
421 uint32_t *code
= pc
->emit
;
424 NV50_DBGMSG("emitting program: size = %u\n", pc
->bin_size
);
426 for (n
= 0; n
< pc
->num_blocks
; ++n
) {
427 struct nv_instruction
*i
;
428 struct nv_basic_block
*b
= pc
->bb_list
[n
];
430 for (i
= b
->entry
; i
; i
= i
->next
) {
431 nv50_emit_instruction(pc
, i
);
433 pc
->bin_pos
+= 1 + (pc
->emit
[0] & 1);
434 pc
->emit
+= 1 + (pc
->emit
[0] & 1);
437 assert(pc
->emit
== &code
[pc
->bin_size
/ 4]);
439 /* XXX: we can do better than this ... */
440 if (!(pc
->emit
[-2] & 1) || (pc
->emit
[-2] & 2) || (pc
->emit
[-1] & 3)) {
441 pc
->emit
[0] = 0xf0000001;
442 pc
->emit
[1] = 0xe0000000;
447 code
[pc
->bin_size
/ 4 - 1] |= 1;
450 nvcg_show_bincode(pc
);
457 nv50_generate_code(struct nv50_translation_info
*ti
)
463 pc
= CALLOC_STRUCT(nv_pc
);
467 pc
->root
= CALLOC(ti
->subr_nr
+ 1, sizeof(pc
->root
[0]));
472 pc
->num_subroutines
= ti
->subr_nr
;
474 ret
= nv50_tgsi_to_nc(pc
, ti
);
478 nv_print_program(pc
);
481 pc
->opt_reload_elim
= ti
->store_to_memory
? FALSE
: TRUE
;
484 ret
= nv_pc_exec_pass0(pc
);
488 nv_print_program(pc
);
491 /* register allocation */
492 ret
= nv_pc_exec_pass1(pc
);
496 nv_print_program(pc
);
497 nv_print_cfgraph(pc
, "nv50_shader_cfgraph.dot", 0);
500 /* prepare for emission */
501 ret
= nv_pc_exec_pass2(pc
);
505 pc
->emit
= CALLOC(pc
->bin_size
/ 4 + 2, 4);
510 ret
= nv50_emit_program(pc
);
514 ti
->p
->code_size
= pc
->bin_size
;
515 ti
->p
->code
= pc
->emit
;
517 ti
->p
->immd_size
= pc
->immd_count
* 4;
518 ti
->p
->immd
= pc
->immd_buf
;
520 /* highest 16 bit reg to num of 32 bit regs */
521 ti
->p
->max_gpr
= (pc
->max_reg
[NV_FILE_GPR
] >> 1) + 1;
523 ti
->p
->fixups
= pc
->fixups
;
524 ti
->p
->num_fixups
= pc
->num_fixups
;
526 NV50_DBGMSG("SHADER TRANSLATION - %s\n", ret
? "failure" : "success");
531 for (i
= 0; i
< pc
->num_blocks
; ++i
)
532 FREE(pc
->bb_list
[i
]);
535 if (ret
) { /* on success, these will be referenced by nv50_program */
548 nvbb_insert_phi(struct nv_basic_block
*b
, struct nv_instruction
*i
)
555 assert(!b
->entry
->prev
&& b
->exit
);
563 if (b
->entry
->opcode
== NV_OP_PHI
) { /* insert after entry */
564 assert(b
->entry
== b
->exit
);
569 } else { /* insert before entry */
570 assert(b
->entry
->prev
&& b
->exit
);
572 i
->prev
= b
->entry
->prev
;
580 nvbb_insert_tail(struct nv_basic_block
*b
, struct nv_instruction
*i
)
582 if (i
->opcode
== NV_OP_PHI
) {
583 nvbb_insert_phi(b
, i
);
592 if (i
->prev
&& i
->prev
->opcode
== NV_OP_PHI
)
597 b
->num_instructions
++;
601 nvi_insert_after(struct nv_instruction
*at
, struct nv_instruction
*ni
)
604 nvbb_insert_tail(at
->bb
, ni
);
614 nv_nvi_delete(struct nv_instruction
*nvi
)
616 struct nv_basic_block
*b
= nvi
->bb
;
619 /* debug_printf("REM: "); nv_print_instruction(nvi); */
621 for (j
= 0; j
< 5; ++j
)
622 nv_reference(NULL
, &nvi
->src
[j
], NULL
);
623 nv_reference(NULL
, &nvi
->flags_src
, NULL
);
626 nvi
->next
->prev
= nvi
->prev
;
628 assert(nvi
== b
->exit
);
633 nvi
->prev
->next
= nvi
->next
;
635 if (nvi
== b
->entry
) {
636 /* PHIs don't get hooked to b->entry */
637 b
->entry
= nvi
->next
;
638 assert(!nvi
->prev
|| nvi
->prev
->opcode
== NV_OP_PHI
);
642 if (nvi
->opcode
!= NV_OP_PHI
)
643 NV50_DBGMSG("NOTE: b->phi points to non-PHI instruction\n");
646 if (!nvi
->next
|| nvi
->next
->opcode
!= NV_OP_PHI
)
654 nv_nvi_permute(struct nv_instruction
*i1
, struct nv_instruction
*i2
)
656 struct nv_basic_block
*b
= i1
->bb
;
658 assert(i1
->opcode
!= NV_OP_PHI
&&
659 i2
->opcode
!= NV_OP_PHI
);
660 assert(i1
->next
== i2
);
680 nvbb_attach_block(struct nv_basic_block
*parent
,
681 struct nv_basic_block
*b
, ubyte edge_kind
)
683 assert(b
->num_in
< 8);
685 if (parent
->out
[0]) {
686 assert(!parent
->out
[1]);
688 parent
->out_kind
[1] = edge_kind
;
691 parent
->out_kind
[0] = edge_kind
;
694 b
->in
[b
->num_in
] = parent
;
695 b
->in_kind
[b
->num_in
++] = edge_kind
;
698 /* NOTE: all BRKs are treated as conditional, so there are 2 outgoing BBs */
701 nvbb_dominated_by(struct nv_basic_block
*b
, struct nv_basic_block
*d
)
708 for (j
= 0; j
< b
->num_in
; ++j
)
709 if ((b
->in_kind
[j
] != CFG_EDGE_BACK
) && !nvbb_dominated_by(b
->in
[j
], d
))
712 return j
? TRUE
: FALSE
;
715 /* check if @bf (future) can be reached from @bp (past), stop at @bt */
717 nvbb_reachable_by(struct nv_basic_block
*bf
, struct nv_basic_block
*bp
,
718 struct nv_basic_block
*bt
)
720 struct nv_basic_block
*q
[NV_PC_MAX_BASIC_BLOCKS
], *b
;
734 assert(n
<= (1024 - 2));
736 for (i
= 0; i
< 2; ++i
) {
737 if (b
->out
[i
] && !IS_WALL_EDGE(b
->out_kind
[i
]) && !b
->out
[i
]->priv
) {
743 for (--n
; n
>= 0; --n
)
749 static struct nv_basic_block
*
750 nvbb_find_dom_frontier(struct nv_basic_block
*b
, struct nv_basic_block
*df
)
752 struct nv_basic_block
*out
;
755 if (!nvbb_dominated_by(df
, b
)) {
756 for (i
= 0; i
< df
->num_in
; ++i
) {
757 if (df
->in_kind
[i
] == CFG_EDGE_BACK
)
759 if (nvbb_dominated_by(df
->in
[i
], b
))
763 for (i
= 0; i
< 2 && df
->out
[i
]; ++i
) {
764 if (df
->out_kind
[i
] == CFG_EDGE_BACK
)
766 if ((out
= nvbb_find_dom_frontier(b
, df
->out
[i
])))
772 struct nv_basic_block
*
773 nvbb_dom_frontier(struct nv_basic_block
*b
)
775 struct nv_basic_block
*df
;
778 for (i
= 0; i
< 2 && b
->out
[i
]; ++i
)
779 if ((df
= nvbb_find_dom_frontier(b
, b
->out
[i
])))