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
24 #include "nv50_program.h"
28 /* returns TRUE if operands 0 and 1 can be swapped */
30 nv_op_commutative(uint opcode
)
48 /* return operand to which the address register applies */
50 nv50_indirect_opnd(struct nv_instruction
*i
)
65 nv50_nvi_can_use_imm(struct nv_instruction
*nvi
, int s
)
67 if (nvi
->flags_src
|| nvi
->flags_def
)
70 switch (nvi
->opcode
) {
78 return (s
== 1) && (nvi
->def
[0]->reg
.file
== NV_FILE_GPR
);
81 return (nvi
->def
[0]->reg
.file
== NV_FILE_GPR
);
88 nv50_nvi_can_load(struct nv_instruction
*nvi
, int s
, struct nv_value
*value
)
90 switch (nvi
->opcode
) {
103 if (s
== 0 && (value
->reg
.file
== NV_FILE_MEM_S
||
104 value
->reg
.file
== NV_FILE_MEM_P
))
107 value
->reg
.file
>= NV_FILE_MEM_C(0) &&
108 value
->reg
.file
<= NV_FILE_MEM_C(15))
110 if (s
== 2 && nvi
->src
[1]->value
->reg
.file
== NV_FILE_GPR
)
122 nv50_supported_src_mods(uint opcode
, int s
)
126 return NV_MOD_NEG
| NV_MOD_ABS
; /* obviously */
145 return NV_MOD_ABS
| NV_MOD_NEG
;
152 nv_nvi_refcount(struct nv_instruction
*nvi
)
156 rc
= nvi
->flags_def
? nvi
->flags_def
->refc
: 0;
158 for (i
= 0; i
< 4; ++i
) {
161 rc
+= nvi
->def
[i
]->refc
;
167 nvcg_replace_value(struct nv_pc
*pc
, struct nv_value
*old_val
,
168 struct nv_value
*new_val
)
172 if (old_val
== new_val
)
173 return old_val
->refc
;
175 for (i
= 0, n
= 0; i
< pc
->num_refs
; ++i
) {
176 if (pc
->refs
[i
]->value
== old_val
) {
178 nv_reference(pc
, &pc
->refs
[i
], new_val
);
185 nv_pc_free_refs(struct nv_pc
*pc
)
188 for (i
= 0; i
< pc
->num_refs
; i
+= 64)
193 nv_print_program(struct nv_basic_block
*b
)
195 struct nv_instruction
*i
= b
->phi
;
199 debug_printf("=== BB %i ", b
->id
);
201 debug_printf("(--0> %i) ", b
->out
[0]->id
);
203 debug_printf("(--1> %i) ", b
->out
[1]->id
);
204 debug_printf("===\n");
208 for (; i
; i
= i
->next
)
209 nv_print_instruction(i
);
212 debug_printf("END\n\n");
215 if (!b
->out
[1] && ++(b
->out
[0]->priv
) != b
->out
[0]->num_in
)
219 nv_print_program(b
->out
[0]);
221 if (b
->out
[1] && b
->out
[1] != b
)
222 nv_print_program(b
->out
[1]);
226 nvcg_show_bincode(struct nv_pc
*pc
)
230 for (i
= 0; i
< pc
->bin_size
/ 4; ++i
)
231 debug_printf("0x%08x ", pc
->emit
[i
]);
236 nv50_emit_program(struct nv_pc
*pc
)
238 uint32_t *code
= pc
->emit
;
241 debug_printf("emitting program: size = %u\n", pc
->bin_size
);
243 for (n
= 0; n
< pc
->num_blocks
; ++n
) {
244 struct nv_instruction
*i
;
245 struct nv_basic_block
*b
= pc
->bb_list
[n
];
247 for (i
= b
->entry
; i
; i
= i
->next
) {
248 nv50_emit_instruction(pc
, i
);
250 pc
->bin_pos
+= 1 + (pc
->emit
[0] & 1);
251 pc
->emit
+= 1 + (pc
->emit
[0] & 1);
254 assert(pc
->emit
== &code
[pc
->bin_size
/ 4]);
256 /* XXX: we can do better than this ... */
257 if ((pc
->emit
[-1] & 3) == 3) {
258 pc
->emit
[0] = 0xf0000001;
259 pc
->emit
[1] = 0xe0000000;
264 code
[pc
->bin_size
/ 4 - 1] |= 1;
266 nvcg_show_bincode(pc
);
272 nv50_generate_code(struct nv50_translation_info
*ti
)
277 pc
= CALLOC_STRUCT(nv_pc
);
281 ret
= nv50_tgsi_to_nc(pc
, ti
);
286 ret
= nv_pc_exec_pass0(pc
);
290 /* register allocation */
291 ret
= nv_pc_exec_pass1(pc
);
295 /* prepare for emission */
296 ret
= nv_pc_exec_pass2(pc
);
300 pc
->emit
= CALLOC(pc
->bin_size
/ 4 + 2, 4);
305 ret
= nv50_emit_program(pc
);
309 ti
->p
->code_size
= pc
->bin_size
;
310 ti
->p
->code
= pc
->emit
;
312 ti
->p
->immd_size
= pc
->immd_count
* 4;
313 ti
->p
->immd
= pc
->immd_buf
;
315 ti
->p
->max_gpr
= (pc
->max_reg
[NV_FILE_GPR
] + 1) >> 1;
318 ti
->p
->fixups
= pc
->fixups
;
319 ti
->p
->num_fixups
= pc
->num_fixups
;
321 debug_printf("SHADER TRANSLATION - %s\n", ret
? "failure" : "success");
339 nvbb_insert_phi(struct nv_basic_block
*b
, struct nv_instruction
*i
)
346 assert(!b
->entry
->prev
&& b
->exit
);
354 if (b
->entry
->opcode
== NV_OP_PHI
) { /* insert after entry */
355 assert(b
->entry
== b
->exit
);
360 } else { /* insert before entry */
361 assert(b
->entry
->prev
&& b
->exit
);
363 i
->prev
= b
->entry
->prev
;
371 nvbb_insert_tail(struct nv_basic_block
*b
, struct nv_instruction
*i
)
373 if (i
->opcode
== NV_OP_PHI
) {
374 nvbb_insert_phi(b
, i
);
383 if (i
->prev
&& i
->prev
->opcode
== NV_OP_PHI
)
388 b
->num_instructions
++;
392 nv_nvi_delete(struct nv_instruction
*nvi
)
394 struct nv_basic_block
*b
= nvi
->bb
;
397 debug_printf("REM: "); nv_print_instruction(nvi
);
399 for (j
= 0; j
< 4; ++j
) {
402 --(nvi
->src
[j
]->value
->refc
);
407 nvi
->next
->prev
= nvi
->prev
;
409 assert(nvi
== b
->exit
);
414 nvi
->prev
->next
= nvi
->next
;
416 if (nvi
== b
->entry
) {
417 assert(nvi
->opcode
!= NV_OP_PHI
|| !nvi
->next
);
419 if (!nvi
->next
|| (nvi
->opcode
== NV_OP_PHI
))
420 b
->entry
= nvi
->prev
;
422 b
->entry
= nvi
->next
;
427 if (nvi
->opcode
!= NV_OP_PHI
)
428 debug_printf("WARN: b->phi points to non-PHI instruction\n");
430 if (!nvi
->next
|| nvi
->next
->opcode
!= NV_OP_PHI
)
438 nv_nvi_permute(struct nv_instruction
*i1
, struct nv_instruction
*i2
)
440 struct nv_basic_block
*b
= i1
->bb
;
442 assert(i1
->opcode
!= NV_OP_PHI
&&
443 i2
->opcode
!= NV_OP_PHI
);
444 assert(i1
->next
== i2
);
463 void nvbb_attach_block(struct nv_basic_block
*parent
, struct nv_basic_block
*b
)
465 if (parent
->out
[0]) {
466 assert(!parent
->out
[1]);
471 b
->in
[b
->num_in
++] = parent
;