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 */
27 #define DESCEND_ARBITRARY(j, f) \
29 b->pass_seq = ctx->pc->pass_seq; \
31 for (j = 0; j < 2; ++j) \
32 if (b->out[j] && b->out[j]->pass_seq < ctx->pc->pass_seq) \
36 extern unsigned nv50_inst_min_size(struct nv_instruction
*);
43 values_equal(struct nv_value
*a
, struct nv_value
*b
)
46 return (a
->reg
.file
== b
->reg
.file
&& a
->join
->reg
.id
== b
->join
->reg
.id
);
50 inst_commutation_check(struct nv_instruction
*a
,
51 struct nv_instruction
*b
)
55 for (di
= 0; di
< 4; ++di
) {
58 for (si
= 0; si
< 5; ++si
) {
61 if (values_equal(a
->def
[di
], b
->src
[si
]->value
))
66 if (b
->flags_src
&& b
->flags_src
->value
== a
->flags_def
)
72 /* Check whether we can swap the order of the instructions,
73 * where a & b may be either the earlier or the later one.
76 inst_commutation_legal(struct nv_instruction
*a
,
77 struct nv_instruction
*b
)
79 return inst_commutation_check(a
, b
) && inst_commutation_check(b
, a
);
83 inst_cullable(struct nv_instruction
*nvi
)
85 if (nvi
->opcode
== NV_OP_STA
)
87 return (!(nvi
->is_terminator
|| nvi
->is_join
||
90 nv_nvi_refcount(nvi
)));
94 nvi_isnop(struct nv_instruction
*nvi
)
96 if (nvi
->opcode
== NV_OP_EXPORT
|| nvi
->opcode
== NV_OP_UNDEF
)
99 /* NOTE: 'fixed' now only means that it shouldn't be optimized away,
100 * but we can still remove it if it is a no-op move.
102 if (/* nvi->fixed || */
103 /* nvi->flags_src || */ /* cond. MOV to same register is still NOP */
105 nvi
->is_terminator
||
109 if (nvi
->def
[0] && nvi
->def
[0]->join
->reg
.id
< 0)
112 if (nvi
->opcode
!= NV_OP_MOV
&& nvi
->opcode
!= NV_OP_SELECT
)
115 if (nvi
->def
[0]->reg
.file
!= nvi
->src
[0]->value
->reg
.file
)
118 if (nvi
->src
[0]->value
->join
->reg
.id
< 0) {
119 NV50_DBGMSG("nvi_isnop: orphaned value detected\n");
123 if (nvi
->opcode
== NV_OP_SELECT
)
124 if (!values_equal(nvi
->def
[0], nvi
->src
[1]->value
))
127 return values_equal(nvi
->def
[0], nvi
->src
[0]->value
);
137 nv_pass_flatten(struct nv_pass
*ctx
, struct nv_basic_block
*b
);
140 nv_pc_pass_pre_emission(void *priv
, struct nv_basic_block
*b
)
142 struct nv_pc
*pc
= (struct nv_pc
*)priv
;
143 struct nv_basic_block
*in
;
144 struct nv_instruction
*nvi
, *next
;
148 for (j
= pc
->num_blocks
- 1; j
>= 0 && !pc
->bb_list
[j
]->bin_size
; --j
);
152 /* check for no-op branches (BRA $PC+8) */
153 if (in
->exit
&& in
->exit
->opcode
== NV_OP_BRA
&& in
->exit
->target
== b
) {
157 for (++j
; j
< pc
->num_blocks
; ++j
)
158 pc
->bb_list
[j
]->bin_pos
-= 8;
160 nv_nvi_delete(in
->exit
);
162 b
->bin_pos
= in
->bin_pos
+ in
->bin_size
;
165 pc
->bb_list
[pc
->num_blocks
++] = b
;
169 for (nvi
= b
->entry
; nvi
; nvi
= next
) {
175 for (nvi
= b
->entry
; nvi
; nvi
= next
) {
178 size
= nv50_inst_min_size(nvi
);
179 if (nvi
->next
&& size
< 8)
182 if ((n32
& 1) && nvi
->next
&&
183 nv50_inst_min_size(nvi
->next
) == 4 &&
184 inst_commutation_legal(nvi
, nvi
->next
)) {
186 nv_nvi_permute(nvi
, nvi
->next
);
191 b
->bin_size
+= n32
& 1;
193 nvi
->prev
->is_long
= 1;
196 b
->bin_size
+= 1 + nvi
->is_long
;
200 NV50_DBGMSG("block %p is now empty\n", b
);
202 if (!b
->exit
->is_long
) {
204 b
->exit
->is_long
= 1;
207 /* might have del'd a hole tail of instructions */
208 if (!b
->exit
->prev
->is_long
&& !(n32
& 1)) {
210 b
->exit
->prev
->is_long
= 1;
213 assert(!b
->entry
|| (b
->exit
&& b
->exit
->is_long
));
215 pc
->bin_size
+= b
->bin_size
*= 4;
219 nv_pc_pass2(struct nv_pc
*pc
, struct nv_basic_block
*root
)
227 nv_pass_flatten(&pass
, root
);
229 nv_pc_pass_in_order(root
, nv_pc_pass_pre_emission
, pc
);
235 nv_pc_exec_pass2(struct nv_pc
*pc
)
239 NV50_DBGMSG("preparing %u blocks for emission\n", pc
->num_blocks
);
241 pc
->bb_list
= CALLOC(pc
->num_blocks
, sizeof(pc
->bb_list
[0]));
245 for (i
= 0; i
< pc
->num_subroutines
+ 1; ++i
)
246 if (pc
->root
[i
] && (ret
= nv_pc_pass2(pc
, pc
->root
[i
])))
251 static INLINE boolean
252 is_cmem_load(struct nv_instruction
*nvi
)
254 return (nvi
->opcode
== NV_OP_LDA
&&
255 nvi
->src
[0]->value
->reg
.file
>= NV_FILE_MEM_C(0) &&
256 nvi
->src
[0]->value
->reg
.file
<= NV_FILE_MEM_C(15));
259 static INLINE boolean
260 is_smem_load(struct nv_instruction
*nvi
)
262 return (nvi
->opcode
== NV_OP_LDA
&&
263 (nvi
->src
[0]->value
->reg
.file
== NV_FILE_MEM_S
||
264 nvi
->src
[0]->value
->reg
.file
<= NV_FILE_MEM_P
));
267 static INLINE boolean
268 is_immd_move(struct nv_instruction
*nvi
)
270 return (nvi
->opcode
== NV_OP_MOV
&&
271 nvi
->src
[0]->value
->reg
.file
== NV_FILE_IMM
);
275 check_swap_src_0_1(struct nv_instruction
*nvi
)
277 static const ubyte cc_swapped
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
279 struct nv_ref
*src0
= nvi
->src
[0], *src1
= nvi
->src
[1];
281 if (!nv_op_commutative(nvi
->opcode
))
283 assert(src0
&& src1
);
285 if (src1
->value
->reg
.file
== NV_FILE_IMM
)
288 if (is_cmem_load(src0
->value
->insn
)) {
289 if (!is_cmem_load(src1
->value
->insn
)) {
292 /* debug_printf("swapping cmem load to 1\n"); */
295 if (is_smem_load(src1
->value
->insn
)) {
296 if (!is_smem_load(src0
->value
->insn
)) {
299 /* debug_printf("swapping smem load to 0\n"); */
303 if (nvi
->opcode
== NV_OP_SET
&& nvi
->src
[0] != src0
)
304 nvi
->set_cond
= cc_swapped
[nvi
->set_cond
];
308 nv_pass_fold_stores(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
310 struct nv_instruction
*nvi
, *sti
, *next
;
313 for (sti
= b
->entry
; sti
; sti
= next
) {
316 /* only handling MOV to $oX here */
317 if (!sti
->def
[0] || sti
->def
[0]->reg
.file
!= NV_FILE_OUT
)
319 if (sti
->opcode
!= NV_OP_MOV
&& sti
->opcode
!= NV_OP_STA
)
322 nvi
= sti
->src
[0]->value
->insn
;
323 if (!nvi
|| nvi
->opcode
== NV_OP_PHI
|| nv_is_vector_op(nvi
->opcode
))
325 assert(nvi
->def
[0] == sti
->src
[0]->value
);
327 if (nvi
->def
[0]->refc
> 1)
330 /* cannot write to $oX when using immediate */
331 for (j
= 0; j
< 4 && nvi
->src
[j
]; ++j
)
332 if (nvi
->src
[j
]->value
->reg
.file
== NV_FILE_IMM
)
334 if (j
< 4 && nvi
->src
[j
])
337 nvi
->def
[0] = sti
->def
[0];
338 nvi
->fixed
= sti
->fixed
;
342 DESCEND_ARBITRARY(j
, nv_pass_fold_stores
);
348 nv_pass_fold_loads(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
350 struct nv_instruction
*nvi
, *ld
;
353 for (nvi
= b
->entry
; nvi
; nvi
= nvi
->next
) {
354 check_swap_src_0_1(nvi
);
356 for (j
= 0; j
< 3; ++j
) {
359 ld
= nvi
->src
[j
]->value
->insn
;
363 if (is_immd_move(ld
) && nv50_nvi_can_use_imm(nvi
, j
)) {
364 nv_reference(ctx
->pc
, &nvi
->src
[j
], ld
->src
[0]->value
);
368 if (ld
->opcode
!= NV_OP_LDA
)
370 if (!nv50_nvi_can_load(nvi
, j
, ld
->src
[0]->value
))
373 if (j
== 0 && ld
->src
[4]) /* can't load shared mem */
376 /* fold it ! */ /* XXX: ref->insn */
377 nv_reference(ctx
->pc
, &nvi
->src
[j
], ld
->src
[0]->value
);
379 nv_reference(ctx
->pc
, &nvi
->src
[4], ld
->src
[4]->value
);
381 if (!nv_nvi_refcount(ld
))
385 DESCEND_ARBITRARY(j
, nv_pass_fold_loads
);
391 nv_pass_lower_mods(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
394 struct nv_instruction
*nvi
, *mi
, *next
;
397 for (nvi
= b
->entry
; nvi
; nvi
= next
) {
399 if (nvi
->opcode
== NV_OP_SUB
) {
400 nvi
->opcode
= NV_OP_ADD
;
401 nvi
->src
[1]->mod
^= NV_MOD_NEG
;
404 /* should not put any modifiers on NEG and ABS */
405 assert(nvi
->opcode
!= NV_MOD_NEG
|| !nvi
->src
[0]->mod
);
406 assert(nvi
->opcode
!= NV_MOD_ABS
|| !nvi
->src
[0]->mod
);
408 for (j
= 0; j
< 4; ++j
) {
412 mi
= nvi
->src
[j
]->value
->insn
;
415 if (mi
->def
[0]->refc
> 1)
418 if (mi
->opcode
== NV_OP_NEG
) mod
= NV_MOD_NEG
;
420 if (mi
->opcode
== NV_OP_ABS
) mod
= NV_MOD_ABS
;
424 if (nvi
->opcode
== NV_OP_ABS
)
425 mod
&= ~(NV_MOD_NEG
| NV_MOD_ABS
);
427 if (nvi
->opcode
== NV_OP_NEG
&& mod
== NV_MOD_NEG
) {
428 nvi
->opcode
= NV_OP_MOV
;
432 if (!(nv50_supported_src_mods(nvi
->opcode
, j
) & mod
))
435 nv_reference(ctx
->pc
, &nvi
->src
[j
], mi
->src
[0]->value
);
437 nvi
->src
[j
]->mod
^= mod
;
440 if (nvi
->opcode
== NV_OP_SAT
) {
441 mi
= nvi
->src
[0]->value
->insn
;
443 if ((mi
->opcode
== NV_OP_MAD
) && !mi
->flags_def
) {
445 mi
->def
[0] = nvi
->def
[0];
450 DESCEND_ARBITRARY(j
, nv_pass_lower_mods
);
455 #define SRC_IS_MUL(s) ((s)->insn && (s)->insn->opcode == NV_OP_MUL)
458 modifiers_apply(uint32_t *val
, ubyte type
, ubyte mod
)
460 if (mod
& NV_MOD_ABS
) {
461 if (type
== NV_TYPE_F32
)
464 if ((*val
) & (1 << 31))
467 if (mod
& NV_MOD_NEG
) {
468 if (type
== NV_TYPE_F32
)
476 modifiers_opcode(ubyte mod
)
479 case NV_MOD_NEG
: return NV_OP_NEG
;
480 case NV_MOD_ABS
: return NV_OP_ABS
;
489 constant_expression(struct nv_pc
*pc
, struct nv_instruction
*nvi
,
490 struct nv_value
*src0
, struct nv_value
*src1
)
492 struct nv_value
*val
;
502 type
= nvi
->def
[0]->reg
.type
;
505 u0
.u32
= src0
->reg
.imm
.u32
;
506 u1
.u32
= src1
->reg
.imm
.u32
;
508 modifiers_apply(&u0
.u32
, type
, nvi
->src
[0]->mod
);
509 modifiers_apply(&u1
.u32
, type
, nvi
->src
[1]->mod
);
511 switch (nvi
->opcode
) {
513 if (nvi
->src
[2]->value
->reg
.file
!= NV_FILE_GPR
)
518 case NV_TYPE_F32
: u
.f32
= u0
.f32
* u1
.f32
; break;
519 case NV_TYPE_U32
: u
.u32
= u0
.u32
* u1
.u32
; break;
520 case NV_TYPE_S32
: u
.s32
= u0
.s32
* u1
.s32
; break;
528 case NV_TYPE_F32
: u
.f32
= u0
.f32
+ u1
.f32
; break;
529 case NV_TYPE_U32
: u
.u32
= u0
.u32
+ u1
.u32
; break;
530 case NV_TYPE_S32
: u
.s32
= u0
.s32
+ u1
.s32
; break;
538 case NV_TYPE_F32
: u
.f32
= u0
.f32
- u1
.f32
; break;
539 case NV_TYPE_U32
: u
.u32
= u0
.u32
- u1
.u32
; break;
540 case NV_TYPE_S32
: u
.s32
= u0
.s32
- u1
.s32
; break;
550 nvi
->opcode
= NV_OP_MOV
;
552 val
= new_value(pc
, NV_FILE_IMM
, type
);
554 val
->reg
.imm
.u32
= u
.u32
;
556 nv_reference(pc
, &nvi
->src
[1], NULL
);
557 nv_reference(pc
, &nvi
->src
[0], val
);
559 if (nvi
->src
[2]) { /* from MAD */
560 nvi
->src
[1] = nvi
->src
[0];
561 nvi
->src
[0] = nvi
->src
[2];
563 nvi
->opcode
= NV_OP_ADD
;
568 constant_operand(struct nv_pc
*pc
,
569 struct nv_instruction
*nvi
, struct nv_value
*val
, int s
)
582 type
= nvi
->def
[0]->reg
.type
;
584 u
.u32
= val
->reg
.imm
.u32
;
585 modifiers_apply(&u
.u32
, type
, nvi
->src
[s
]->mod
);
587 switch (nvi
->opcode
) {
589 if ((type
== NV_TYPE_F32
&& u
.f32
== 1.0f
) ||
590 (NV_TYPE_ISINT(type
) && u
.u32
== 1)) {
591 if ((op
= modifiers_opcode(nvi
->src
[t
]->mod
)) == NV_OP_NOP
)
594 nv_reference(pc
, &nvi
->src
[s
], NULL
);
595 nvi
->src
[0] = nvi
->src
[t
];
598 if ((type
== NV_TYPE_F32
&& u
.f32
== 2.0f
) ||
599 (NV_TYPE_ISINT(type
) && u
.u32
== 2)) {
600 nvi
->opcode
= NV_OP_ADD
;
601 nv_reference(pc
, &nvi
->src
[s
], nvi
->src
[t
]->value
);
602 nvi
->src
[s
]->mod
= nvi
->src
[t
]->mod
;
604 if (type
== NV_TYPE_F32
&& u
.f32
== -1.0f
) {
605 if (nvi
->src
[t
]->mod
& NV_MOD_NEG
)
606 nvi
->opcode
= NV_OP_MOV
;
608 nvi
->opcode
= NV_OP_NEG
;
609 nv_reference(pc
, &nvi
->src
[s
], NULL
);
610 nvi
->src
[0] = nvi
->src
[t
];
613 if (type
== NV_TYPE_F32
&& u
.f32
== -2.0f
) {
614 nvi
->opcode
= NV_OP_ADD
;
615 nv_reference(pc
, &nvi
->src
[s
], nvi
->src
[t
]->value
);
616 nvi
->src
[s
]->mod
= (nvi
->src
[t
]->mod
^= NV_MOD_NEG
);
619 nvi
->opcode
= NV_OP_MOV
;
620 nv_reference(pc
, &nvi
->src
[t
], NULL
);
622 nvi
->src
[0] = nvi
->src
[1];
629 if ((op
= modifiers_opcode(nvi
->src
[t
]->mod
)) == NV_OP_NOP
)
632 nv_reference(pc
, &nvi
->src
[s
], NULL
);
633 nvi
->src
[0] = nvi
->src
[t
];
638 u
.f32
= 1.0f
/ u
.f32
;
639 (val
= new_value(pc
, NV_FILE_IMM
, NV_TYPE_F32
))->reg
.imm
.f32
= u
.f32
;
640 nvi
->opcode
= NV_OP_MOV
;
642 nv_reference(pc
, &nvi
->src
[0], val
);
645 u
.f32
= 1.0f
/ sqrtf(u
.f32
);
646 (val
= new_value(pc
, NV_FILE_IMM
, NV_TYPE_F32
))->reg
.imm
.f32
= u
.f32
;
647 nvi
->opcode
= NV_OP_MOV
;
649 nv_reference(pc
, &nvi
->src
[0], val
);
655 if (nvi
->opcode
== NV_OP_MOV
&& nvi
->flags_def
) {
656 struct nv_instruction
*cvt
= new_instruction_at(pc
, nvi
, NV_OP_CVT
);
658 nv_reference(pc
, &cvt
->src
[0], nvi
->def
[0]);
660 cvt
->flags_def
= nvi
->flags_def
;
661 nvi
->flags_def
= NULL
;
666 nv_pass_lower_arith(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
668 struct nv_instruction
*nvi
, *next
;
671 for (nvi
= b
->entry
; nvi
; nvi
= next
) {
672 struct nv_value
*src0
, *src1
, *src
;
677 src0
= nvcg_find_immediate(nvi
->src
[0]);
678 src1
= nvcg_find_immediate(nvi
->src
[1]);
681 constant_expression(ctx
->pc
, nvi
, src0
, src1
);
684 constant_operand(ctx
->pc
, nvi
, src0
, 0);
687 constant_operand(ctx
->pc
, nvi
, src1
, 1);
690 /* try to combine MUL, ADD into MAD */
691 if (nvi
->opcode
!= NV_OP_ADD
)
694 src0
= nvi
->src
[0]->value
;
695 src1
= nvi
->src
[1]->value
;
697 if (SRC_IS_MUL(src0
) && src0
->refc
== 1)
700 if (SRC_IS_MUL(src1
) && src1
->refc
== 1)
705 nvi
->opcode
= NV_OP_MAD
;
706 mod
= nvi
->src
[(src
== src0
) ? 0 : 1]->mod
;
707 nv_reference(ctx
->pc
, &nvi
->src
[(src
== src0
) ? 0 : 1], NULL
);
708 nvi
->src
[2] = nvi
->src
[(src
== src0
) ? 1 : 0];
710 assert(!(mod
& ~NV_MOD_NEG
));
711 nvi
->src
[0] = new_ref(ctx
->pc
, src
->insn
->src
[0]->value
);
712 nvi
->src
[1] = new_ref(ctx
->pc
, src
->insn
->src
[1]->value
);
713 nvi
->src
[0]->mod
= src
->insn
->src
[0]->mod
^ mod
;
714 nvi
->src
[1]->mod
= src
->insn
->src
[1]->mod
;
716 DESCEND_ARBITRARY(j
, nv_pass_lower_arith
);
721 /* TODO: redundant store elimination */
724 struct load_record
*next
;
726 struct nv_value
*value
;
729 #define LOAD_RECORD_POOL_SIZE 1024
731 struct nv_pass_reld_elim
{
734 struct load_record
*imm
;
735 struct load_record
*mem_s
;
736 struct load_record
*mem_v
;
737 struct load_record
*mem_c
[16];
738 struct load_record
*mem_l
;
740 struct load_record pool
[LOAD_RECORD_POOL_SIZE
];
744 /* TODO: properly handle loads from l[] memory in the presence of stores */
746 nv_pass_reload_elim(struct nv_pass_reld_elim
*ctx
, struct nv_basic_block
*b
)
748 struct load_record
**rec
, *it
;
749 struct nv_instruction
*ld
, *next
;
751 struct nv_value
*val
;
754 for (ld
= b
->entry
; ld
; ld
= next
) {
758 val
= ld
->src
[0]->value
;
761 if (ld
->opcode
== NV_OP_LINTERP
|| ld
->opcode
== NV_OP_PINTERP
) {
765 if (ld
->opcode
== NV_OP_LDA
) {
767 if (val
->reg
.file
>= NV_FILE_MEM_C(0) &&
768 val
->reg
.file
<= NV_FILE_MEM_C(15))
769 rec
= &ctx
->mem_c
[val
->reg
.file
- NV_FILE_MEM_C(0)];
771 if (val
->reg
.file
== NV_FILE_MEM_S
)
774 if (val
->reg
.file
== NV_FILE_MEM_L
)
777 if ((ld
->opcode
== NV_OP_MOV
) && (val
->reg
.file
== NV_FILE_IMM
)) {
778 data
= val
->reg
.imm
.u32
;
782 if (!rec
|| !ld
->def
[0]->refc
)
785 for (it
= *rec
; it
; it
= it
->next
)
786 if (it
->data
== data
)
790 if (ld
->def
[0]->reg
.id
>= 0)
791 it
->value
= ld
->def
[0];
794 nvcg_replace_value(ctx
->pc
, ld
->def
[0], it
->value
);
796 if (ctx
->alloc
== LOAD_RECORD_POOL_SIZE
)
798 it
= &ctx
->pool
[ctx
->alloc
++];
801 it
->value
= ld
->def
[0];
809 for (j
= 0; j
< 16; ++j
)
810 ctx
->mem_c
[j
] = NULL
;
814 DESCEND_ARBITRARY(j
, nv_pass_reload_elim
);
820 nv_pass_tex_mask(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
824 for (i
= 0; i
< ctx
->pc
->num_instructions
; ++i
) {
825 struct nv_instruction
*nvi
= &ctx
->pc
->instructions
[i
];
826 struct nv_value
*def
[4];
828 if (!nv_is_vector_op(nvi
->opcode
))
832 for (c
= 0; c
< 4; ++c
) {
833 if (nvi
->def
[c
]->refc
)
834 nvi
->tex_mask
|= 1 << c
;
835 def
[c
] = nvi
->def
[c
];
839 for (c
= 0; c
< 4; ++c
)
840 if (nvi
->tex_mask
& (1 << c
))
841 nvi
->def
[j
++] = def
[c
];
842 for (c
= 0; c
< 4; ++c
)
843 if (!(nvi
->tex_mask
& (1 << c
)))
844 nvi
->def
[j
++] = def
[c
];
856 nv_pass_dce(struct nv_pass_dce
*ctx
, struct nv_basic_block
*b
)
859 struct nv_instruction
*nvi
, *next
;
861 for (nvi
= b
->phi
? b
->phi
: b
->entry
; nvi
; nvi
= next
) {
864 if (inst_cullable(nvi
)) {
870 DESCEND_ARBITRARY(j
, nv_pass_dce
);
875 /* Register allocation inserted ELSE blocks for all IF/ENDIF without ELSE.
876 * Returns TRUE if @bb initiates an IF/ELSE/ENDIF clause, or is an IF with
877 * BREAK and dummy ELSE block.
879 static INLINE boolean
880 bb_is_if_else_endif(struct nv_basic_block
*bb
)
882 if (!bb
->out
[0] || !bb
->out
[1])
885 if (bb
->out
[0]->out_kind
[0] == CFG_EDGE_LOOP_LEAVE
) {
886 return (bb
->out
[0]->out
[1] == bb
->out
[1]->out
[0] &&
887 !bb
->out
[1]->out
[1]);
889 return (bb
->out
[0]->out
[0] == bb
->out
[1]->out
[0] &&
890 !bb
->out
[0]->out
[1] &&
891 !bb
->out
[1]->out
[1]);
895 /* predicate instructions and remove branch at the end */
897 predicate_instructions(struct nv_pc
*pc
, struct nv_basic_block
*b
,
898 struct nv_value
*p
, ubyte cc
)
900 struct nv_instruction
*nvi
;
904 for (nvi
= b
->entry
; nvi
->next
; nvi
= nvi
->next
) {
905 if (!nvi_isnop(nvi
)) {
907 nv_reference(pc
, &nvi
->flags_src
, p
);
911 if (nvi
->opcode
== NV_OP_BRA
)
914 if (!nvi_isnop(nvi
)) {
916 nv_reference(pc
, &nvi
->flags_src
, p
);
920 /* NOTE: Run this after register allocation, we can just cut out the cflow
921 * instructions and hook the predicates to the conditional OPs if they are
922 * not using immediates; better than inserting SELECT to join definitions.
924 * NOTE: Should adapt prior optimization to make this possible more often.
927 nv_pass_flatten(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
929 struct nv_instruction
*nvi
;
930 struct nv_value
*pred
;
934 if (bb_is_if_else_endif(b
)) {
936 NV50_DBGMSG("pass_flatten: IF/ELSE/ENDIF construct at BB:%i\n", b
->id
);
938 for (n0
= 0, nvi
= b
->out
[0]->entry
; nvi
; nvi
= nvi
->next
, ++n0
)
939 if (!nv50_nvi_can_predicate(nvi
))
942 for (n1
= 0, nvi
= b
->out
[1]->entry
; nvi
; nvi
= nvi
->next
, ++n1
)
943 if (!nv50_nvi_can_predicate(nvi
))
947 debug_printf("cannot predicate: "); nv_print_instruction(nvi
);
950 debug_printf("cannot predicate: "); nv_print_instruction(nvi
);
954 if (!nvi
&& n0
< 12 && n1
< 12) { /* 12 as arbitrary limit */
955 assert(b
->exit
&& b
->exit
->flags_src
);
956 pred
= b
->exit
->flags_src
->value
;
958 predicate_instructions(ctx
->pc
, b
->out
[0], pred
, NV_CC_NE
| NV_CC_U
);
959 predicate_instructions(ctx
->pc
, b
->out
[1], pred
, NV_CC_EQ
);
961 assert(b
->exit
&& b
->exit
->opcode
== NV_OP_BRA
);
962 nv_nvi_delete(b
->exit
);
964 if (b
->exit
&& b
->exit
->opcode
== NV_OP_JOINAT
)
965 nv_nvi_delete(b
->exit
);
967 i
= (b
->out
[0]->out_kind
[0] == CFG_EDGE_LOOP_LEAVE
) ? 1 : 0;
969 if ((nvi
= b
->out
[0]->out
[i
]->entry
)) {
971 if (nvi
->opcode
== NV_OP_JOIN
)
976 DESCEND_ARBITRARY(i
, nv_pass_flatten
);
981 /* local common subexpression elimination, stupid O(n^2) implementation */
983 nv_pass_cse(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
985 struct nv_instruction
*ir
, *ik
, *next
;
986 struct nv_instruction
*entry
= b
->phi
? b
->phi
: b
->entry
;
992 for (ir
= entry
; ir
; ir
= next
) {
994 for (ik
= entry
; ik
!= ir
; ik
= ik
->next
) {
995 if (ir
->opcode
!= ik
->opcode
|| ir
->fixed
)
998 if (!ir
->def
[0] || !ik
->def
[0] ||
999 ik
->opcode
== NV_OP_LDA
||
1000 ik
->opcode
== NV_OP_STA
||
1001 ik
->opcode
== NV_OP_MOV
||
1002 nv_is_vector_op(ik
->opcode
))
1003 continue; /* ignore loads, stores & moves */
1005 if (ik
->src
[4] || ir
->src
[4])
1006 continue; /* don't mess with address registers */
1008 if (ik
->flags_src
|| ir
->flags_src
||
1009 ik
->flags_def
|| ir
->flags_def
)
1010 continue; /* and also not with flags, for now */
1012 if (ik
->def
[0]->reg
.file
== NV_FILE_OUT
||
1013 ir
->def
[0]->reg
.file
== NV_FILE_OUT
||
1014 !values_equal(ik
->def
[0], ir
->def
[0]))
1017 for (s
= 0; s
< 3; ++s
) {
1018 struct nv_value
*a
, *b
;
1025 if (ik
->src
[s
]->mod
!= ir
->src
[s
]->mod
)
1027 a
= ik
->src
[s
]->value
;
1028 b
= ir
->src
[s
]->value
;
1031 if (a
->reg
.file
!= b
->reg
.file
||
1033 a
->reg
.id
!= b
->reg
.id
)
1039 nvcg_replace_value(ctx
->pc
, ir
->def
[0], ik
->def
[0]);
1046 DESCEND_ARBITRARY(s
, nv_pass_cse
);
1052 nv_pc_pass0(struct nv_pc
*pc
, struct nv_basic_block
*root
)
1054 struct nv_pass_reld_elim
*reldelim
;
1055 struct nv_pass pass
;
1056 struct nv_pass_dce dce
;
1062 /* Do this first, so we don't have to pay attention
1063 * to whether sources are supported memory loads.
1066 ret
= nv_pass_lower_arith(&pass
, root
);
1071 ret
= nv_pass_fold_loads(&pass
, root
);
1076 ret
= nv_pass_fold_stores(&pass
, root
);
1080 if (pc
->opt_reload_elim
) {
1081 reldelim
= CALLOC_STRUCT(nv_pass_reld_elim
);
1084 ret
= nv_pass_reload_elim(reldelim
, root
);
1091 ret
= nv_pass_cse(&pass
, root
);
1096 ret
= nv_pass_lower_mods(&pass
, root
);
1104 ret
= nv_pass_dce(&dce
, root
);
1107 } while (dce
.removed
);
1109 ret
= nv_pass_tex_mask(&pass
, root
);
1117 nv_pc_exec_pass0(struct nv_pc
*pc
)
1121 for (i
= 0; i
< pc
->num_subroutines
+ 1; ++i
)
1122 if (pc
->root
[i
] && (ret
= nv_pc_pass0(pc
, pc
->root
[i
])))