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 NOUVEAU_DEBUG 1
26 #include "nvc0_program.h"
29 nvc0_insn_can_load(struct nv_instruction
*nvi
, int s
,
30 struct nv_instruction
*ld
)
34 if (ld
->opcode
== NV_OP_MOV
&& ld
->src
[0]->value
->reg
.file
== NV_FILE_IMM
) {
35 if (s
> 1 || !(nvc0_op_info_table
[nvi
->opcode
].immediate
& (1 << s
)))
37 if (!(nvc0_op_info_table
[nvi
->opcode
].immediate
& 4))
38 if (ld
->src
[0]->value
->reg
.imm
.u32
& 0xfff)
41 if (!(nvc0_op_info_table
[nvi
->opcode
].memory
& (1 << s
)))
44 if (ld
->indirect
>= 0)
47 /* a few ops can use g[] sources directly, but we don't support g[] yet */
48 if (ld
->src
[0]->value
->reg
.file
== NV_FILE_MEM_L
||
49 ld
->src
[0]->value
->reg
.file
== NV_FILE_MEM_G
)
52 for (i
= 0; i
< 3 && nvi
->src
[i
]; ++i
)
53 if (nvi
->src
[i
]->value
->reg
.file
== NV_FILE_IMM
)
59 /* Return whether this instruction can be executed conditionally. */
61 nvc0_insn_is_predicateable(struct nv_instruction
*nvi
)
63 if (nvi
->predicate
>= 0) /* already predicated */
65 if (!nvc0_op_info_table
[nvi
->opcode
].predicate
&&
66 !nvc0_op_info_table
[nvi
->opcode
].pseudo
)
72 nvc0_insn_refcount(struct nv_instruction
*nvi
)
76 for (i
= 0; i
< 5 && nvi
->def
[i
]; ++i
) {
79 rc
+= nvi
->def
[i
]->refc
;
85 nvc0_pc_replace_value(struct nv_pc
*pc
,
86 struct nv_value
*old_val
,
87 struct nv_value
*new_val
)
91 if (old_val
== new_val
)
94 for (i
= 0, n
= 0; i
< pc
->num_refs
; ++i
) {
95 if (pc
->refs
[i
]->value
== old_val
) {
97 for (s
= 0; s
< 6 && pc
->refs
[i
]->insn
->src
[s
]; ++s
)
98 if (pc
->refs
[i
]->insn
->src
[s
] == pc
->refs
[i
])
101 nv_reference(pc
, pc
->refs
[i
]->insn
, s
, new_val
);
107 static INLINE boolean
108 is_gpr63(struct nv_value
*val
)
110 return (val
->reg
.file
== NV_FILE_GPR
&& val
->reg
.id
== 63);
114 nvc0_pc_find_constant(struct nv_ref
*ref
)
116 struct nv_value
*src
;
122 while (src
->insn
&& src
->insn
->opcode
== NV_OP_MOV
) {
123 assert(!src
->insn
->src
[0]->mod
);
124 src
= src
->insn
->src
[0]->value
;
126 if ((src
->reg
.file
== NV_FILE_IMM
) || is_gpr63(src
) ||
128 src
->insn
->opcode
== NV_OP_LD
&&
129 src
->insn
->src
[0]->value
->reg
.file
>= NV_FILE_MEM_C(0) &&
130 src
->insn
->src
[0]->value
->reg
.file
<= NV_FILE_MEM_C(15)))
136 nvc0_pc_find_immediate(struct nv_ref
*ref
)
138 struct nv_value
*src
= nvc0_pc_find_constant(ref
);
140 return (src
&& (src
->reg
.file
== NV_FILE_IMM
|| is_gpr63(src
))) ? src
: NULL
;
144 nv_pc_free_refs(struct nv_pc
*pc
)
147 for (i
= 0; i
< pc
->num_refs
; i
+= 64)
153 edge_name(ubyte type
)
156 case CFG_EDGE_FORWARD
: return "forward";
157 case CFG_EDGE_BACK
: return "back";
158 case CFG_EDGE_LOOP_ENTER
: return "loop";
159 case CFG_EDGE_LOOP_LEAVE
: return "break";
160 case CFG_EDGE_FAKE
: return "fake";
167 nvc0_pc_pass_in_order(struct nv_basic_block
*root
, nv_pc_pass_func f
,
170 struct nv_basic_block
*bb
[64], *bbb
[16], *b
;
181 for (j
= 1; j
>= 0; --j
) {
185 switch (b
->out_kind
[j
]) {
188 case CFG_EDGE_FORWARD
:
190 if (++b
->out
[j
]->priv
== b
->out
[j
]->num_in
)
193 case CFG_EDGE_LOOP_ENTER
:
196 case CFG_EDGE_LOOP_LEAVE
:
197 if (!b
->out
[j
]->priv
) {
198 bbb
[pp
++] = b
->out
[j
];
213 bb
[pp
- 1] = bbb
[pp
- 1];
219 nv_do_print_function(void *priv
, struct nv_basic_block
*b
)
221 struct nv_instruction
*i
;
223 debug_printf("=== BB %i ", b
->id
);
225 debug_printf("[%s -> %i] ", edge_name(b
->out_kind
[0]), b
->out
[0]->id
);
227 debug_printf("[%s -> %i] ", edge_name(b
->out_kind
[1]), b
->out
[1]->id
);
228 debug_printf("===\n");
233 for (; i
; i
= i
->next
)
234 nvc0_print_instruction(i
);
238 nvc0_print_function(struct nv_basic_block
*root
)
240 if (root
->subroutine
)
241 debug_printf("SUBROUTINE %i\n", root
->subroutine
);
243 debug_printf("MAIN\n");
245 nvc0_pc_pass_in_order(root
, nv_do_print_function
, root
);
249 nvc0_print_program(struct nv_pc
*pc
)
252 for (i
= 0; i
< pc
->num_subroutines
+ 1; ++i
)
254 nvc0_print_function(pc
->root
[i
]);
257 #if NOUVEAU_DEBUG > 1
259 nv_do_print_cfgraph(struct nv_pc
*pc
, FILE *f
, struct nv_basic_block
*b
)
263 b
->pass_seq
= pc
->pass_seq
;
265 fprintf(f
, "\t%i [shape=box]\n", b
->id
);
267 for (i
= 0; i
< 2; ++i
) {
270 switch (b
->out_kind
[i
]) {
271 case CFG_EDGE_FORWARD
:
272 fprintf(f
, "\t%i -> %i;\n", b
->id
, b
->out
[i
]->id
);
274 case CFG_EDGE_LOOP_ENTER
:
275 fprintf(f
, "\t%i -> %i [color=green];\n", b
->id
, b
->out
[i
]->id
);
277 case CFG_EDGE_LOOP_LEAVE
:
278 fprintf(f
, "\t%i -> %i [color=red];\n", b
->id
, b
->out
[i
]->id
);
281 fprintf(f
, "\t%i -> %i;\n", b
->id
, b
->out
[i
]->id
);
284 fprintf(f
, "\t%i -> %i [style=dotted];\n", b
->id
, b
->out
[i
]->id
);
290 if (b
->out
[i
]->pass_seq
< pc
->pass_seq
)
291 nv_do_print_cfgraph(pc
, f
, b
->out
[i
]);
295 /* Print the control flow graph of subroutine @subr (0 == MAIN) to a file. */
297 nv_print_cfgraph(struct nv_pc
*pc
, const char *filepath
, int subr
)
301 f
= fopen(filepath
, "a");
305 fprintf(f
, "digraph G {\n");
309 nv_do_print_cfgraph(pc
, f
, pc
->root
[subr
]);
318 nvc0_pc_print_binary(struct nv_pc
*pc
)
322 NOUVEAU_DBG("nvc0_pc_print_binary(%u ops)\n", pc
->emit_size
/ 8);
324 for (i
= 0; i
< pc
->emit_size
/ 4; i
+= 2) {
325 debug_printf("0x%08x ", pc
->emit
[i
+ 0]);
326 debug_printf("0x%08x ", pc
->emit
[i
+ 1]);
334 nvc0_emit_program(struct nv_pc
*pc
)
336 uint32_t *code
= pc
->emit
;
339 NOUVEAU_DBG("emitting program: size = %u\n", pc
->emit_size
);
342 for (n
= 0; n
< pc
->num_blocks
; ++n
) {
343 struct nv_instruction
*i
;
344 struct nv_basic_block
*b
= pc
->bb_list
[n
];
346 for (i
= b
->entry
; i
; i
= i
->next
) {
347 nvc0_emit_instruction(pc
, i
);
352 assert(pc
->emit
== &code
[pc
->emit_size
/ 4]);
354 pc
->emit
[0] = 0x00001de7;
355 pc
->emit
[1] = 0x80000000;
361 nvc0_pc_print_binary(pc
);
363 debug_printf("not printing binary\n");
369 nvc0_generate_code(struct nvc0_translation_info
*ti
)
375 pc
= CALLOC_STRUCT(nv_pc
);
379 pc
->is_fragprog
= ti
->prog
->type
== PIPE_SHADER_FRAGMENT
;
381 pc
->root
= CALLOC(ti
->num_subrs
+ 1, sizeof(pc
->root
[0]));
386 pc
->num_subroutines
= ti
->num_subrs
;
388 ret
= nvc0_tgsi_to_nc(pc
, ti
);
391 #if NOUVEAU_DEBUG > 1
392 nvc0_print_program(pc
);
395 pc
->opt_reload_elim
= ti
->require_stores
? FALSE
: TRUE
;
398 ret
= nvc0_pc_exec_pass0(pc
);
402 nvc0_print_program(pc
);
405 /* register allocation */
406 ret
= nvc0_pc_exec_pass1(pc
);
409 #if NOUVEAU_DEBUG > 1
410 nvc0_print_program(pc
);
411 nv_print_cfgraph(pc
, "nvc0_shader_cfgraph.dot", 0);
414 /* prepare for emission */
415 ret
= nvc0_pc_exec_pass2(pc
);
418 assert(!(pc
->emit_size
% 8));
420 pc
->emit
= CALLOC(pc
->emit_size
/ 4 + 2, 4);
425 ret
= nvc0_emit_program(pc
);
429 ti
->prog
->code
= pc
->emit
;
430 ti
->prog
->code_base
= 0;
431 ti
->prog
->code_size
= pc
->emit_size
;
432 ti
->prog
->parm_size
= 0;
434 ti
->prog
->max_gpr
= MAX2(4, pc
->max_reg
[NV_FILE_GPR
] + 1);
436 ti
->prog
->relocs
= pc
->reloc_entries
;
437 ti
->prog
->num_relocs
= pc
->num_relocs
;
439 NOUVEAU_DBG("SHADER TRANSLATION - %s\n", ret
? "failure" : "success");
444 for (i
= 0; i
< pc
->num_blocks
; ++i
)
445 FREE(pc
->bb_list
[i
]);
449 /* on success, these will be referenced by struct nvc0_program */
454 if (pc
->reloc_entries
)
455 FREE(pc
->reloc_entries
);
462 nvbb_insert_phi(struct nv_basic_block
*b
, struct nv_instruction
*i
)
469 assert(!b
->entry
->prev
&& b
->exit
);
477 if (b
->entry
->opcode
== NV_OP_PHI
) { /* insert after entry */
478 assert(b
->entry
== b
->exit
);
483 } else { /* insert before entry */
484 assert(b
->entry
->prev
&& b
->exit
);
486 i
->prev
= b
->entry
->prev
;
494 nvc0_insn_append(struct nv_basic_block
*b
, struct nv_instruction
*i
)
496 if (i
->opcode
== NV_OP_PHI
) {
497 nvbb_insert_phi(b
, i
);
506 if (i
->prev
&& i
->prev
->opcode
== NV_OP_PHI
)
511 b
->num_instructions
++;
513 if (i
->prev
&& i
->prev
->terminator
)
514 nvc0_insns_permute(i
->prev
, i
);
518 nvc0_insn_insert_after(struct nv_instruction
*at
, struct nv_instruction
*ni
)
521 nvc0_insn_append(at
->bb
, ni
);
529 ni
->bb
->num_instructions
++;
533 nvc0_insn_insert_before(struct nv_instruction
*at
, struct nv_instruction
*ni
)
535 nvc0_insn_insert_after(at
, ni
);
536 nvc0_insns_permute(at
, ni
);
540 nvc0_insn_delete(struct nv_instruction
*nvi
)
542 struct nv_basic_block
*b
= nvi
->bb
;
545 /* debug_printf("REM: "); nv_print_instruction(nvi); */
547 for (s
= 0; s
< 6 && nvi
->src
[s
]; ++s
)
548 nv_reference(NULL
, nvi
, s
, NULL
);
551 nvi
->next
->prev
= nvi
->prev
;
553 assert(nvi
== b
->exit
);
558 nvi
->prev
->next
= nvi
->next
;
560 if (nvi
== b
->entry
) {
561 /* PHIs don't get hooked to b->entry */
562 b
->entry
= nvi
->next
;
563 assert(!nvi
->prev
|| nvi
->prev
->opcode
== NV_OP_PHI
);
567 if (nvi
->opcode
!= NV_OP_PHI
)
568 NOUVEAU_DBG("NOTE: b->phi points to non-PHI instruction\n");
571 if (!nvi
->next
|| nvi
->next
->opcode
!= NV_OP_PHI
)
579 nvc0_insns_permute(struct nv_instruction
*i1
, struct nv_instruction
*i2
)
581 struct nv_basic_block
*b
= i1
->bb
;
583 assert(i1
->opcode
!= NV_OP_PHI
&&
584 i2
->opcode
!= NV_OP_PHI
);
585 assert(i1
->next
== i2
);
605 nvc0_bblock_attach(struct nv_basic_block
*parent
,
606 struct nv_basic_block
*b
, ubyte edge_kind
)
608 assert(b
->num_in
< 8);
610 if (parent
->out
[0]) {
611 assert(!parent
->out
[1]);
613 parent
->out_kind
[1] = edge_kind
;
616 parent
->out_kind
[0] = edge_kind
;
619 b
->in
[b
->num_in
] = parent
;
620 b
->in_kind
[b
->num_in
++] = edge_kind
;
623 /* NOTE: all BRKs are treated as conditional, so there are 2 outgoing BBs */
626 nvc0_bblock_dominated_by(struct nv_basic_block
*b
, struct nv_basic_block
*d
)
633 for (j
= 0; j
< b
->num_in
; ++j
)
634 if ((b
->in_kind
[j
] != CFG_EDGE_BACK
) &&
635 !nvc0_bblock_dominated_by(b
->in
[j
], d
))
638 return j
? TRUE
: FALSE
;
641 /* check if @bf (future) can be reached from @bp (past), stop at @bt */
643 nvc0_bblock_reachable_by(struct nv_basic_block
*bf
, struct nv_basic_block
*bp
,
644 struct nv_basic_block
*bt
)
646 struct nv_basic_block
*q
[NV_PC_MAX_BASIC_BLOCKS
], *b
;
660 assert(n
<= (1024 - 2));
662 for (i
= 0; i
< 2; ++i
) {
663 if (b
->out
[i
] && !IS_WALL_EDGE(b
->out_kind
[i
]) && !b
->out
[i
]->priv
) {
669 for (--n
; n
>= 0; --n
)
675 static struct nv_basic_block
*
676 nvbb_find_dom_frontier(struct nv_basic_block
*b
, struct nv_basic_block
*df
)
678 struct nv_basic_block
*out
;
681 if (!nvc0_bblock_dominated_by(df
, b
)) {
682 for (i
= 0; i
< df
->num_in
; ++i
) {
683 if (df
->in_kind
[i
] == CFG_EDGE_BACK
)
685 if (nvc0_bblock_dominated_by(df
->in
[i
], b
))
689 for (i
= 0; i
< 2 && df
->out
[i
]; ++i
) {
690 if (df
->out_kind
[i
] == CFG_EDGE_BACK
)
692 if ((out
= nvbb_find_dom_frontier(b
, df
->out
[i
])))
698 struct nv_basic_block
*
699 nvc0_bblock_dom_frontier(struct nv_basic_block
*b
)
701 struct nv_basic_block
*df
;
704 for (i
= 0; i
< 2 && b
->out
[i
]; ++i
)
705 if ((df
= nvbb_find_dom_frontier(b
, b
->out
[i
])))