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 "nvc0_program.h"
26 #define DESCEND_ARBITRARY(j, f) \
28 b->pass_seq = ctx->pc->pass_seq; \
30 for (j = 0; j < 2; ++j) \
31 if (b->out[j] && b->out[j]->pass_seq < ctx->pc->pass_seq) \
36 registers_interfere(struct nv_value
*a
, struct nv_value
*b
)
38 if (a
->reg
.file
!= b
->reg
.file
)
40 if (NV_IS_MEMORY_FILE(a
->reg
.file
) || NV_IS_MEMORY_FILE(b
->reg
.file
))
43 assert(a
->join
->reg
.id
>= 0 && b
->join
->reg
.id
>= 0);
45 if (a
->join
->reg
.id
< b
->join
->reg
.id
) {
46 return (a
->join
->reg
.id
+ a
->reg
.size
>= b
->join
->reg
.id
);
48 if (a
->join
->reg
.id
> b
->join
->reg
.id
) {
49 return (b
->join
->reg
.id
+ b
->reg
.size
>= a
->join
->reg
.id
);
56 values_equal(struct nv_value
*a
, struct nv_value
*b
)
58 if (a
->reg
.file
!= b
->reg
.file
|| a
->reg
.size
!= b
->reg
.size
)
60 if (NV_IS_MEMORY_FILE(a
->reg
.file
))
61 return a
->reg
.address
== b
->reg
.address
;
63 return a
->join
->reg
.id
== b
->join
->reg
.id
;
68 inst_commutation_check(struct nv_instruction
*a
, struct nv_instruction
*b
)
72 for (di
= 0; di
< 4 && a
->def
[di
]; ++di
)
73 for (si
= 0; si
< 5 && b
->src
[si
]; ++si
)
74 if (registers_interfere(a
->def
[di
], b
->src
[si
]->value
))
80 /* Check whether we can swap the order of the instructions,
81 * where a & b may be either the earlier or the later one.
84 inst_commutation_legal(struct nv_instruction
*a
, struct nv_instruction
*b
)
86 return inst_commutation_check(a
, b
) && inst_commutation_check(b
, a
);
91 inst_removable(struct nv_instruction
*nvi
)
93 if (nvi
->opcode
== NV_OP_ST
)
95 return (!(nvi
->terminator
||
99 nvc0_insn_refcount(nvi
)));
102 /* Check if we do not actually have to emit this instruction. */
103 static INLINE boolean
104 inst_is_noop(struct nv_instruction
*nvi
)
106 if (nvi
->opcode
== NV_OP_UNDEF
|| nvi
->opcode
== NV_OP_BIND
)
108 if (nvi
->terminator
|| nvi
->join
)
110 if (nvi
->def
[0] && nvi
->def
[0]->join
->reg
.id
< 0)
112 if (nvi
->opcode
!= NV_OP_MOV
&& nvi
->opcode
!= NV_OP_SELECT
)
114 if (nvi
->def
[0]->reg
.file
!= nvi
->src
[0]->value
->reg
.file
)
117 if (nvi
->src
[0]->value
->join
->reg
.id
< 0) {
118 NOUVEAU_DBG("inst_is_noop: orphaned value detected\n");
122 if (nvi
->opcode
== NV_OP_SELECT
)
123 if (!values_equal(nvi
->def
[0], nvi
->src
[1]->value
))
125 return values_equal(nvi
->def
[0], nvi
->src
[0]->value
);
135 nv_pass_flatten(struct nv_pass
*ctx
, struct nv_basic_block
*b
);
138 nv_pc_pass_pre_emission(void *priv
, struct nv_basic_block
*b
)
140 struct nv_pc
*pc
= (struct nv_pc
*)priv
;
141 struct nv_basic_block
*in
;
142 struct nv_instruction
*nvi
, *next
;
145 for (j
= pc
->num_blocks
- 1; j
>= 0 && !pc
->bb_list
[j
]->emit_size
; --j
);
150 /* check for no-op branches (BRA $PC+8) */
151 if (in
->exit
&& in
->exit
->opcode
== NV_OP_BRA
&& in
->exit
->target
== b
) {
155 for (++j
; j
< pc
->num_blocks
; ++j
)
156 pc
->bb_list
[j
]->emit_pos
-= 8;
158 nvc0_insn_delete(in
->exit
);
160 b
->emit_pos
= in
->emit_pos
+ in
->emit_size
;
163 pc
->bb_list
[pc
->num_blocks
++] = b
;
167 for (nvi
= b
->entry
; nvi
; nvi
= next
) {
169 if (inst_is_noop(nvi
) ||
170 (pc
->is_fragprog
&& nvi
->opcode
== NV_OP_EXPORT
)) {
171 nvc0_insn_delete(nvi
);
175 pc
->emit_size
+= b
->emit_size
;
179 debug_printf("BB:%i is now empty\n", b
->id
);
181 debug_printf("BB:%i size = %u\n", b
->id
, b
->emit_size
);
186 nv_pc_pass2(struct nv_pc
*pc
, struct nv_basic_block
*root
)
193 nv_pass_flatten(&pass
, root
);
195 nvc0_pc_pass_in_order(root
, nv_pc_pass_pre_emission
, pc
);
201 nvc0_pc_exec_pass2(struct nv_pc
*pc
)
205 NOUVEAU_DBG("preparing %u blocks for emission\n", pc
->num_blocks
);
207 pc
->num_blocks
= 0; /* will reorder bb_list */
209 for (i
= 0; i
< pc
->num_subroutines
+ 1; ++i
)
210 if (pc
->root
[i
] && (ret
= nv_pc_pass2(pc
, pc
->root
[i
])))
215 static INLINE boolean
216 is_cspace_load(struct nv_instruction
*nvi
)
220 assert(nvi
->indirect
!= 0);
221 return (nvi
->opcode
== NV_OP_LD
&&
222 nvi
->src
[0]->value
->reg
.file
>= NV_FILE_MEM_C(0) &&
223 nvi
->src
[0]->value
->reg
.file
<= NV_FILE_MEM_C(15));
226 static INLINE boolean
227 is_immd32_load(struct nv_instruction
*nvi
)
231 return (nvi
->opcode
== NV_OP_MOV
&&
232 nvi
->src
[0]->value
->reg
.file
== NV_FILE_IMM
&&
233 nvi
->src
[0]->value
->reg
.size
== 4);
237 check_swap_src_0_1(struct nv_instruction
*nvi
)
239 static const uint8_t cc_swapped
[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
241 struct nv_ref
*src0
= nvi
->src
[0];
242 struct nv_ref
*src1
= nvi
->src
[1];
244 if (!nv_op_commutative(nvi
->opcode
) && NV_BASEOP(nvi
->opcode
) != NV_OP_SET
)
246 assert(src0
&& src1
&& src0
->value
&& src1
->value
);
248 if (src1
->value
->reg
.file
!= NV_FILE_GPR
)
251 if (is_cspace_load(src0
->value
->insn
)) {
252 if (!is_cspace_load(src1
->value
->insn
)) {
257 if (is_immd32_load(src0
->value
->insn
)) {
258 if (!is_cspace_load(src1
->value
->insn
) &&
259 !is_immd32_load(src1
->value
->insn
)) {
265 if (nvi
->src
[0] != src0
&& NV_BASEOP(nvi
->opcode
) == NV_OP_SET
)
266 nvi
->set_cond
= (nvi
->set_cond
& ~7) | cc_swapped
[nvi
->set_cond
& 7];
270 nvi_set_indirect_load(struct nv_pc
*pc
,
271 struct nv_instruction
*nvi
, struct nv_value
*val
)
273 for (nvi
->indirect
= 0; nvi
->indirect
< 6 && nvi
->src
[nvi
->indirect
];
275 assert(nvi
->indirect
< 6);
276 nv_reference(pc
, nvi
, nvi
->indirect
, val
);
280 nvc0_pass_fold_loads(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
282 struct nv_instruction
*nvi
, *ld
;
285 for (nvi
= b
->entry
; nvi
; nvi
= nvi
->next
) {
286 check_swap_src_0_1(nvi
);
288 for (s
= 0; s
< 3 && nvi
->src
[s
]; ++s
) {
289 ld
= nvi
->src
[s
]->value
->insn
;
290 if (!ld
|| (ld
->opcode
!= NV_OP_LD
&& ld
->opcode
!= NV_OP_MOV
))
292 if (!nvc0_insn_can_load(nvi
, s
, ld
))
296 nv_reference(ctx
->pc
, nvi
, s
, ld
->src
[0]->value
);
297 if (ld
->indirect
>= 0)
298 nvi_set_indirect_load(ctx
->pc
, nvi
, ld
->src
[ld
->indirect
]->value
);
300 if (!nvc0_insn_refcount(ld
))
301 nvc0_insn_delete(ld
);
304 DESCEND_ARBITRARY(s
, nvc0_pass_fold_loads
);
309 /* NOTE: Assumes loads have not yet been folded. */
311 nv_pass_lower_mods(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
313 struct nv_instruction
*nvi
, *mi
, *next
;
317 for (nvi
= b
->entry
; nvi
; nvi
= next
) {
319 if (nvi
->opcode
== NV_OP_SUB
) {
320 nvi
->src
[1]->mod
^= NV_MOD_NEG
;
321 nvi
->opcode
= NV_OP_ADD
;
324 for (j
= 0; j
< 3 && nvi
->src
[j
]; ++j
) {
325 mi
= nvi
->src
[j
]->value
->insn
;
328 if (mi
->def
[0]->refc
> 1 || mi
->predicate
>= 0)
331 if (NV_BASEOP(mi
->opcode
) == NV_OP_NEG
) mod
= NV_MOD_NEG
;
333 if (NV_BASEOP(mi
->opcode
) == NV_OP_ABS
) mod
= NV_MOD_ABS
;
336 assert(!(mod
& mi
->src
[0]->mod
& NV_MOD_NEG
));
338 mod
|= mi
->src
[0]->mod
;
340 if ((nvi
->opcode
== NV_OP_ABS
) || (nvi
->src
[j
]->mod
& NV_MOD_ABS
)) {
341 /* abs neg [abs] = abs */
342 mod
&= ~(NV_MOD_NEG
| NV_MOD_ABS
);
344 if ((nvi
->opcode
== NV_OP_NEG
) && (mod
& NV_MOD_NEG
)) {
345 /* neg as opcode and modifier on same insn cannot occur */
346 /* neg neg abs = abs, neg neg = identity */
348 if (mod
& NV_MOD_ABS
)
349 nvi
->opcode
= NV_OP_ABS
;
351 nvi
->opcode
= NV_OP_MOV
;
355 if ((nv_op_supported_src_mods(nvi
->opcode
) & mod
) != mod
)
358 nv_reference(ctx
->pc
, nvi
, j
, mi
->src
[0]->value
);
360 nvi
->src
[j
]->mod
^= mod
;
363 if (nvi
->opcode
== NV_OP_SAT
) {
364 mi
= nvi
->src
[0]->value
->insn
;
366 if (mi
->def
[0]->refc
> 1 ||
367 (mi
->opcode
!= NV_OP_ADD
&&
368 mi
->opcode
!= NV_OP_MUL
&&
369 mi
->opcode
!= NV_OP_MAD
))
372 mi
->def
[0] = nvi
->def
[0];
373 mi
->def
[0]->insn
= mi
;
374 nvc0_insn_delete(nvi
);
377 DESCEND_ARBITRARY(j
, nv_pass_lower_mods
);
382 #define SRC_IS_MUL(s) ((s)->insn && (s)->insn->opcode == NV_OP_MUL)
385 apply_modifiers(uint32_t *val
, uint8_t type
, uint8_t mod
)
387 if (mod
& NV_MOD_ABS
) {
388 if (type
== NV_TYPE_F32
)
391 if ((*val
) & (1 << 31))
394 if (mod
& NV_MOD_NEG
) {
395 if (type
== NV_TYPE_F32
)
400 if (mod
& NV_MOD_SAT
) {
407 if (type
== NV_TYPE_F32
) {
408 u
.f
= CLAMP(u
.f
, -1.0f
, 1.0f
);
410 if (type
== NV_TYPE_U16
) {
411 u
.u
= MIN2(u
.u
, 0xffff);
413 if (type
== NV_TYPE_S16
) {
414 u
.i
= CLAMP(u
.i
, -32768, 32767);
418 if (mod
& NV_MOD_NOT
)
423 constant_expression(struct nv_pc
*pc
, struct nv_instruction
*nvi
,
424 struct nv_value
*src0
, struct nv_value
*src1
)
426 struct nv_value
*val
;
436 type
= NV_OPTYPE(nvi
->opcode
);
439 u0
.u32
= src0
->reg
.imm
.u32
;
440 u1
.u32
= src1
->reg
.imm
.u32
;
442 apply_modifiers(&u0
.u32
, type
, nvi
->src
[0]->mod
);
443 apply_modifiers(&u1
.u32
, type
, nvi
->src
[1]->mod
);
445 switch (nvi
->opcode
) {
447 if (nvi
->src
[2]->value
->reg
.file
!= NV_FILE_GPR
)
451 u
.f32
= u0
.f32
* u1
.f32
;
454 u
.u32
= u0
.u32
* u1
.u32
;
457 u
.f32
= u0
.f32
+ u1
.f32
;
460 u
.u32
= u0
.u32
+ u1
.u32
;
463 u
.f32
= u0
.f32
- u1
.f32
;
467 u.u32 = u0.u32 - u1.u32;
474 val
= new_value(pc
, NV_FILE_IMM
, nv_type_sizeof(type
));
475 val
->reg
.imm
.u32
= u
.u32
;
477 nv_reference(pc
, nvi
, 1, NULL
);
478 nv_reference(pc
, nvi
, 0, val
);
480 if (nvi
->opcode
== NV_OP_MAD_F32
) {
481 nvi
->src
[1] = nvi
->src
[0];
482 nvi
->src
[0] = nvi
->src
[2];
484 nvi
->opcode
= NV_OP_ADD_F32
;
486 if (val
->reg
.imm
.u32
== 0) {
488 nvi
->opcode
= NV_OP_MOV
;
491 nvi
->opcode
= NV_OP_MOV
;
496 constant_operand(struct nv_pc
*pc
,
497 struct nv_instruction
*nvi
, struct nv_value
*val
, int s
)
511 type
= NV_OPTYPE(nvi
->opcode
);
513 u
.u32
= val
->reg
.imm
.u32
;
514 apply_modifiers(&u
.u32
, type
, nvi
->src
[s
]->mod
);
516 if (u
.u32
== 0 && NV_BASEOP(nvi
->opcode
) == NV_OP_MUL
) {
517 nvi
->opcode
= NV_OP_MOV
;
518 nv_reference(pc
, nvi
, t
, NULL
);
520 nvi
->src
[0] = nvi
->src
[1];
526 switch (nvi
->opcode
) {
528 if (u
.f32
== 1.0f
|| u
.f32
== -1.0f
) {
530 nvi
->src
[t
]->mod
^= NV_MOD_NEG
;
531 switch (nvi
->src
[t
]->mod
) {
532 case 0: op
= nvi
->saturate
? NV_OP_SAT
: NV_OP_MOV
; break;
533 case NV_MOD_NEG
: op
= NV_OP_NEG_F32
; break;
534 case NV_MOD_ABS
: op
= NV_OP_ABS_F32
; break;
539 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
540 nv_reference(pc
, nvi
, 1, NULL
);
541 nvi
->src
[0]->mod
= 0;
543 if (u
.f32
== 2.0f
|| u
.f32
== -2.0f
) {
545 nvi
->src
[t
]->mod
^= NV_MOD_NEG
;
546 nvi
->opcode
= NV_OP_ADD_F32
;
547 nv_reference(pc
, nvi
, s
, nvi
->src
[t
]->value
);
548 nvi
->src
[s
]->mod
= nvi
->src
[t
]->mod
;
553 switch (nvi
->src
[t
]->mod
) {
554 case 0: op
= nvi
->saturate
? NV_OP_SAT
: NV_OP_MOV
; break;
555 case NV_MOD_NEG
: op
= NV_OP_NEG_F32
; break;
556 case NV_MOD_ABS
: op
= NV_OP_ABS_F32
; break;
557 case NV_MOD_NEG
| NV_MOD_ABS
:
559 nvi
->ext
.cvt
.s
= nvi
->ext
.cvt
.d
= type
;
565 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
566 nv_reference(pc
, nvi
, 1, NULL
);
567 if (nvi
->opcode
!= NV_OP_CVT
)
568 nvi
->src
[0]->mod
= 0;
573 assert(nvi
->src
[t
]->mod
== 0);
574 nvi
->opcode
= nvi
->saturate
? NV_OP_CVT
: NV_OP_MOV
;
575 nvi
->ext
.cvt
.s
= nvi
->ext
.cvt
.d
= type
;
576 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
577 nv_reference(pc
, nvi
, 1, NULL
);
581 /* multiplication by 0 already handled above */
582 assert(nvi
->src
[s
]->mod
== 0);
583 shift
= ffs(u
.s32
) - 1;
585 nvi
->opcode
= NV_OP_MOV
;
586 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
587 nv_reference(pc
, nvi
, 1, NULL
);
589 if (u
.s32
> 0 && u
.s32
== (1 << shift
)) {
590 nvi
->opcode
= NV_OP_SHL
;
591 (val
= new_value(pc
, NV_FILE_IMM
, NV_TYPE_U32
))->reg
.imm
.s32
= shift
;
592 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
593 nv_reference(pc
, nvi
, 1, val
);
598 u
.f32
= 1.0f
/ u
.f32
;
599 (val
= new_value(pc
, NV_FILE_IMM
, NV_TYPE_F32
))->reg
.imm
.f32
= u
.f32
;
600 nvi
->opcode
= NV_OP_MOV
;
602 nv_reference(pc
, nvi
, 0, val
);
605 u
.f32
= 1.0f
/ sqrtf(u
.f32
);
606 (val
= new_value(pc
, NV_FILE_IMM
, NV_TYPE_F32
))->reg
.imm
.f32
= u
.f32
;
607 nvi
->opcode
= NV_OP_MOV
;
609 nv_reference(pc
, nvi
, 0, val
);
617 handle_min_max(struct nv_pass
*ctx
, struct nv_instruction
*nvi
)
619 struct nv_value
*src0
= nvi
->src
[0]->value
;
620 struct nv_value
*src1
= nvi
->src
[1]->value
;
622 if (src0
!= src1
|| (nvi
->src
[0]->mod
| nvi
->src
[1]->mod
))
624 if (src0
->reg
.file
!= NV_FILE_GPR
)
626 nvc0_pc_replace_value(ctx
->pc
, nvi
->def
[0], src0
);
627 nvc0_insn_delete(nvi
);
630 /* check if we can MUL + ADD -> MAD/FMA */
632 handle_add_mul(struct nv_pass
*ctx
, struct nv_instruction
*nvi
)
634 struct nv_value
*src0
= nvi
->src
[0]->value
;
635 struct nv_value
*src1
= nvi
->src
[1]->value
;
636 struct nv_value
*src
;
640 if (SRC_IS_MUL(src0
) && src0
->refc
== 1) s
= 0;
642 if (SRC_IS_MUL(src1
) && src1
->refc
== 1) s
= 1;
646 if ((src0
->insn
&& src0
->insn
->bb
!= nvi
->bb
) ||
647 (src1
->insn
&& src1
->insn
->bb
!= nvi
->bb
))
650 /* check for immediates from prior constant folding */
651 if (src0
->reg
.file
!= NV_FILE_GPR
|| src1
->reg
.file
!= NV_FILE_GPR
)
653 src
= nvi
->src
[s
]->value
;
655 mod
[0] = nvi
->src
[0]->mod
;
656 mod
[1] = nvi
->src
[1]->mod
;
657 mod
[2] = src
->insn
->src
[0]->mod
;
658 mod
[3] = src
->insn
->src
[1]->mod
;
660 if ((mod
[0] | mod
[1] | mod
[2] | mod
[3]) & ~NV_MOD_NEG
)
663 nvi
->opcode
= NV_OP_MAD_F32
;
665 nv_reference(ctx
->pc
, nvi
, s
, NULL
);
666 nvi
->src
[2] = nvi
->src
[!s
];
669 nv_reference(ctx
->pc
, nvi
, 0, src
->insn
->src
[0]->value
);
670 nvi
->src
[0]->mod
= mod
[2] ^ mod
[s
];
671 nv_reference(ctx
->pc
, nvi
, 1, src
->insn
->src
[1]->value
);
672 nvi
->src
[1]->mod
= mod
[3];
676 nv_pass_algebraic_opt(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
678 struct nv_instruction
*nvi
, *next
;
681 for (nvi
= b
->entry
; nvi
; nvi
= next
) {
682 struct nv_value
*src0
, *src1
;
683 uint baseop
= NV_BASEOP(nvi
->opcode
);
687 src0
= nvc0_pc_find_immediate(nvi
->src
[0]);
688 src1
= nvc0_pc_find_immediate(nvi
->src
[1]);
691 constant_expression(ctx
->pc
, nvi
, src0
, src1
);
694 constant_operand(ctx
->pc
, nvi
, src0
, 0);
697 constant_operand(ctx
->pc
, nvi
, src1
, 1);
700 if (baseop
== NV_OP_MIN
|| baseop
== NV_OP_MAX
)
701 handle_min_max(ctx
, nvi
);
703 if (nvi
->opcode
== NV_OP_ADD_F32
)
704 handle_add_mul(ctx
, nvi
);
706 DESCEND_ARBITRARY(j
, nv_pass_algebraic_opt
);
711 /* TODO: redundant store elimination */
714 struct mem_record
*next
;
715 struct nv_instruction
*insn
;
721 #define MEM_RECORD_POOL_SIZE 1024
723 struct pass_reld_elim
{
726 struct mem_record
*imm
;
727 struct mem_record
*mem_v
;
728 struct mem_record
*mem_a
;
729 struct mem_record
*mem_c
[16];
730 struct mem_record
*mem_l
;
732 struct mem_record pool
[MEM_RECORD_POOL_SIZE
];
736 /* Extend the load operation in @rec to also cover the data loaded by @ld.
737 * The two loads may not overlap but reference adjacent memory locations.
740 combine_load(struct nv_pc
*pc
, struct mem_record
*rec
,
741 struct nv_instruction
*ld
)
743 struct nv_instruction
*fv
= rec
->insn
;
744 struct nv_value
*mem
= ld
->src
[0]->value
;
745 uint32_t size
= rec
->size
+ mem
->reg
.size
;
747 int d
= rec
->size
/ 4;
749 assert(rec
->size
< 16);
750 if (rec
->ofst
> mem
->reg
.address
) {
751 if ((size
== 8 && mem
->reg
.address
& 3) ||
752 (size
> 8 && mem
->reg
.address
& 7))
754 rec
->ofst
= mem
->reg
.address
;
755 for (j
= 0; j
< d
; ++j
)
756 fv
->def
[mem
->reg
.size
/ 4 + j
] = fv
->def
[j
];
759 if ((size
== 8 && rec
->ofst
& 3) ||
760 (size
> 8 && rec
->ofst
& 7)) {
764 for (j
= 0; j
< mem
->reg
.size
/ 4; ++j
) {
765 fv
->def
[d
] = ld
->def
[j
];
766 fv
->def
[d
++]->insn
= fv
;
769 if (fv
->src
[0]->value
->refc
> 1)
770 nv_reference(pc
, fv
, 0, new_value_like(pc
, fv
->src
[0]->value
));
771 fv
->src
[0]->value
->reg
.address
= rec
->ofst
;
772 fv
->src
[0]->value
->reg
.size
= rec
->size
= size
;
774 nvc0_insn_delete(ld
);
778 combine_export(struct mem_record
*rec
, struct nv_instruction
*ex
)
784 add_mem_record(struct pass_reld_elim
*ctx
, struct mem_record
**rec
,
785 uint32_t base
, uint32_t ofst
, struct nv_instruction
*nvi
)
787 struct mem_record
*it
= &ctx
->pool
[ctx
->alloc
++];
794 it
->size
= nvi
->src
[0]->value
->reg
.size
;
797 /* vectorize and reuse loads from memory or of immediates */
799 nv_pass_mem_opt(struct pass_reld_elim
*ctx
, struct nv_basic_block
*b
)
801 struct mem_record
**rec
, *it
;
802 struct nv_instruction
*ld
, *next
;
803 struct nv_value
*mem
;
807 for (ld
= b
->entry
; ld
; ld
= next
) {
810 if (is_cspace_load(ld
)) {
811 mem
= ld
->src
[0]->value
;
812 rec
= &ctx
->mem_c
[ld
->src
[0]->value
->reg
.file
- NV_FILE_MEM_C(0)];
814 if (ld
->opcode
== NV_OP_VFETCH
) {
815 mem
= ld
->src
[0]->value
;
818 if (ld
->opcode
== NV_OP_EXPORT
) {
819 mem
= ld
->src
[0]->value
;
820 if (mem
->reg
.file
!= NV_FILE_MEM_V
)
826 if (ld
->def
[0] && ld
->def
[0]->refc
== 0)
828 ofst
= mem
->reg
.address
;
829 base
= (ld
->indirect
>= 0) ? ld
->src
[ld
->indirect
]->value
->n
: 0;
831 for (it
= *rec
; it
; it
= it
->next
) {
832 if (it
->base
== base
&&
833 ((it
->ofst
>> 4) == (ofst
>> 4)) &&
834 ((it
->ofst
+ it
->size
== ofst
) ||
835 (it
->ofst
- mem
->reg
.size
== ofst
))) {
836 /* only NV_OP_VFETCH can load exactly 12 bytes */
837 if (ld
->opcode
== NV_OP_LD
&& it
->size
+ mem
->reg
.size
== 12)
839 if (it
->ofst
< ofst
) {
840 if ((it
->ofst
& 0xf) == 4)
843 if ((ofst
& 0xf) == 4)
849 switch (ld
->opcode
) {
850 case NV_OP_EXPORT
: combine_export(it
, ld
); break;
852 combine_load(ctx
->pc
, it
, ld
);
856 if (ctx
->alloc
< MEM_RECORD_POOL_SIZE
) {
857 add_mem_record(ctx
, rec
, base
, ofst
, ld
);
862 ctx
->mem_a
= ctx
->mem_v
= ctx
->mem_l
= NULL
;
863 for (s
= 0; s
< 16; ++s
)
864 ctx
->mem_c
[s
] = NULL
;
866 DESCEND_ARBITRARY(s
, nv_pass_mem_opt
);
871 eliminate_store(struct mem_record
*rec
, struct nv_instruction
*st
)
875 /* elimination of redundant stores */
877 pass_store_elim(struct pass_reld_elim
*ctx
, struct nv_basic_block
*b
)
879 struct mem_record
**rec
, *it
;
880 struct nv_instruction
*st
, *next
;
881 struct nv_value
*mem
;
882 uint32_t base
, ofst
, size
;
885 for (st
= b
->entry
; st
; st
= next
) {
888 if (st
->opcode
== NV_OP_ST
) {
889 mem
= st
->src
[0]->value
;
892 if (st
->opcode
== NV_OP_EXPORT
) {
893 mem
= st
->src
[0]->value
;
894 if (mem
->reg
.file
!= NV_FILE_MEM_V
)
898 if (st
->opcode
== NV_OP_ST
) {
901 ofst
= mem
->reg
.address
;
902 base
= (st
->indirect
>= 0) ? st
->src
[st
->indirect
]->value
->n
: 0;
903 size
= mem
->reg
.size
;
905 for (it
= *rec
; it
; it
= it
->next
) {
906 if (it
->base
== base
&&
907 (it
->ofst
<= ofst
&& (it
->ofst
+ size
) > ofst
))
911 eliminate_store(it
, st
);
913 add_mem_record(ctx
, rec
, base
, ofst
, st
);
916 DESCEND_ARBITRARY(s
, nv_pass_mem_opt
);
920 /* TODO: properly handle loads from l[] memory in the presence of stores */
922 nv_pass_reload_elim(struct pass_reld_elim
*ctx
, struct nv_basic_block
*b
)
925 struct load_record
**rec
, *it
;
926 struct nv_instruction
*ld
, *next
;
928 struct nv_value
*val
;
931 for (ld
= b
->entry
; ld
; ld
= next
) {
935 val
= ld
->src
[0]->value
;
938 if (ld
->opcode
== NV_OP_LINTERP
|| ld
->opcode
== NV_OP_PINTERP
) {
939 data
[0] = val
->reg
.id
;
943 if (ld
->opcode
== NV_OP_LDA
) {
944 data
[0] = val
->reg
.id
;
945 data
[1] = ld
->src
[4] ? ld
->src
[4]->value
->n
: ~0ULL;
946 if (val
->reg
.file
>= NV_FILE_MEM_C(0) &&
947 val
->reg
.file
<= NV_FILE_MEM_C(15))
948 rec
= &ctx
->mem_c
[val
->reg
.file
- NV_FILE_MEM_C(0)];
950 if (val
->reg
.file
== NV_FILE_MEM_S
)
953 if (val
->reg
.file
== NV_FILE_MEM_L
)
956 if ((ld
->opcode
== NV_OP_MOV
) && (val
->reg
.file
== NV_FILE_IMM
)) {
957 data
[0] = val
->reg
.imm
.u32
;
962 if (!rec
|| !ld
->def
[0]->refc
)
965 for (it
= *rec
; it
; it
= it
->next
)
966 if (it
->data
[0] == data
[0] && it
->data
[1] == data
[1])
970 if (ld
->def
[0]->reg
.id
>= 0)
971 it
->value
= ld
->def
[0];
974 nvc0_pc_replace_value(ctx
->pc
, ld
->def
[0], it
->value
);
976 if (ctx
->alloc
== LOAD_RECORD_POOL_SIZE
)
978 it
= &ctx
->pool
[ctx
->alloc
++];
980 it
->data
[0] = data
[0];
981 it
->data
[1] = data
[1];
982 it
->value
= ld
->def
[0];
990 for (j
= 0; j
< 16; ++j
)
991 ctx
->mem_c
[j
] = NULL
;
995 DESCEND_ARBITRARY(j
, nv_pass_reload_elim
);
1001 nv_pass_tex_mask(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
1005 for (i
= 0; i
< ctx
->pc
->num_instructions
; ++i
) {
1006 struct nv_instruction
*nvi
= &ctx
->pc
->instructions
[i
];
1007 struct nv_value
*def
[4];
1009 if (!nv_is_texture_op(nvi
->opcode
))
1013 for (c
= 0; c
< 4; ++c
) {
1014 if (nvi
->def
[c
]->refc
)
1015 nvi
->tex_mask
|= 1 << c
;
1016 def
[c
] = nvi
->def
[c
];
1020 for (c
= 0; c
< 4; ++c
)
1021 if (nvi
->tex_mask
& (1 << c
))
1022 nvi
->def
[j
++] = def
[c
];
1023 for (c
= 0; c
< 4; ++c
)
1024 if (!(nvi
->tex_mask
& (1 << c
)))
1025 nvi
->def
[j
++] = def
[c
];
1031 struct nv_pass_dce
{
1037 nv_pass_dce(struct nv_pass_dce
*ctx
, struct nv_basic_block
*b
)
1040 struct nv_instruction
*nvi
, *next
;
1042 for (nvi
= b
->phi
? b
->phi
: b
->entry
; nvi
; nvi
= next
) {
1045 if (inst_removable(nvi
)) {
1046 nvc0_insn_delete(nvi
);
1050 DESCEND_ARBITRARY(j
, nv_pass_dce
);
1055 /* Register allocation inserted ELSE blocks for all IF/ENDIF without ELSE.
1056 * Returns TRUE if @bb initiates an IF/ELSE/ENDIF clause, or is an IF with
1057 * BREAK and dummy ELSE block.
1059 static INLINE boolean
1060 bb_is_if_else_endif(struct nv_basic_block
*bb
)
1062 if (!bb
->out
[0] || !bb
->out
[1])
1065 if (bb
->out
[0]->out_kind
[0] == CFG_EDGE_LOOP_LEAVE
) {
1066 return (bb
->out
[0]->out
[1] == bb
->out
[1]->out
[0] &&
1067 !bb
->out
[1]->out
[1]);
1069 return (bb
->out
[0]->out
[0] == bb
->out
[1]->out
[0] &&
1070 !bb
->out
[0]->out
[1] &&
1071 !bb
->out
[1]->out
[1]);
1075 /* Predicate instructions and delete any branch at the end if it is
1076 * not a break from a loop.
1079 predicate_instructions(struct nv_pc
*pc
, struct nv_basic_block
*b
,
1080 struct nv_value
*pred
, uint8_t cc
)
1082 struct nv_instruction
*nvi
, *prev
;
1087 for (nvi
= b
->entry
; nvi
; nvi
= nvi
->next
) {
1089 if (inst_is_noop(nvi
))
1091 for (s
= 0; nvi
->src
[s
]; ++s
);
1095 nv_reference(pc
, nvi
, nvi
->predicate
, pred
);
1097 if (prev
->opcode
== NV_OP_BRA
&&
1098 b
->out_kind
[0] != CFG_EDGE_LOOP_LEAVE
&&
1099 b
->out_kind
[1] != CFG_EDGE_LOOP_LEAVE
)
1100 nvc0_insn_delete(prev
);
1103 static INLINE boolean
1104 may_predicate_insn(struct nv_instruction
*nvi
, struct nv_value
*pred
)
1106 if (nvi
->def
[0] && values_equal(nvi
->def
[0], pred
))
1108 return nvc0_insn_is_predicateable(nvi
);
1111 /* Transform IF/ELSE/ENDIF constructs into predicated instructions
1115 nv_pass_flatten(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
1117 struct nv_instruction
*nvi
;
1118 struct nv_value
*pred
;
1120 int n0
, n1
; /* instruction counts of outgoing blocks */
1122 if (bb_is_if_else_endif(b
)) {
1123 assert(b
->exit
&& b
->exit
->opcode
== NV_OP_BRA
);
1125 assert(b
->exit
->predicate
>= 0);
1126 pred
= b
->exit
->src
[b
->exit
->predicate
]->value
;
1129 for (nvi
= b
->out
[0]->entry
; nvi
; nvi
= nvi
->next
, ++n0
)
1130 if (!may_predicate_insn(nvi
, pred
))
1133 /* we're after register allocation, so there always is an ELSE block */
1134 for (nvi
= b
->out
[1]->entry
; nvi
; nvi
= nvi
->next
, ++n1
)
1135 if (!may_predicate_insn(nvi
, pred
))
1139 /* 12 is an arbitrary limit */
1140 if (!nvi
&& n0
< 12 && n1
< 12) {
1141 predicate_instructions(ctx
->pc
, b
->out
[0], pred
, !b
->exit
->cc
);
1142 predicate_instructions(ctx
->pc
, b
->out
[1], pred
, b
->exit
->cc
);
1144 nvc0_insn_delete(b
->exit
); /* delete the branch */
1146 /* and a potential joinat before it */
1147 if (b
->exit
&& b
->exit
->opcode
== NV_OP_JOINAT
)
1148 nvc0_insn_delete(b
->exit
);
1150 /* remove join operations at the end of the conditional */
1151 k
= (b
->out
[0]->out_kind
[0] == CFG_EDGE_LOOP_LEAVE
) ? 1 : 0;
1152 if ((nvi
= b
->out
[0]->out
[k
]->entry
)) {
1154 if (nvi
->opcode
== NV_OP_JOIN
)
1155 nvc0_insn_delete(nvi
);
1159 DESCEND_ARBITRARY(k
, nv_pass_flatten
);
1164 /* Tests instructions for equality, but independently of sources. */
1166 is_operation_equal(struct nv_instruction
*a
, struct nv_instruction
*b
)
1168 if (a
->opcode
!= b
->opcode
)
1170 if (nv_is_texture_op(a
->opcode
)) {
1171 if (a
->ext
.tex
.t
!= b
->ext
.tex
.t
||
1172 a
->ext
.tex
.s
!= b
->ext
.tex
.s
)
1174 if (a
->tex_dim
!= b
->tex_dim
||
1175 a
->tex_array
!= b
->tex_array
||
1176 a
->tex_cube
!= b
->tex_cube
||
1177 a
->tex_shadow
!= b
->tex_shadow
||
1178 a
->tex_live
!= b
->tex_live
)
1181 if (a
->opcode
== NV_OP_CVT
) {
1182 if (a
->ext
.cvt
.s
!= b
->ext
.cvt
.s
||
1183 a
->ext
.cvt
.d
!= b
->ext
.cvt
.d
)
1186 if (NV_BASEOP(a
->opcode
) == NV_OP_SET
||
1187 NV_BASEOP(a
->opcode
) == NV_OP_SLCT
) {
1188 if (a
->set_cond
!= b
->set_cond
)
1191 if (a
->opcode
== NV_OP_LINTERP
||
1192 a
->opcode
== NV_OP_PINTERP
) {
1193 if (a
->centroid
!= b
->centroid
||
1199 if (a
->lanes
!= b
->lanes
||
1200 a
->patch
!= b
->patch
||
1201 a
->saturate
!= b
->saturate
)
1203 if (a
->opcode
== NV_OP_QUADOP
) /* beware quadon ! */
1208 /* local common subexpression elimination, stupid O(n^2) implementation */
1210 nv_pass_cse(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
1212 struct nv_instruction
*ir
, *ik
, *next
;
1213 struct nv_instruction
*entry
= b
->phi
? b
->phi
: b
->entry
;
1219 for (ir
= entry
; ir
; ir
= next
) {
1223 for (ik
= entry
; ik
!= ir
; ik
= ik
->next
) {
1224 if (!is_operation_equal(ir
, ik
))
1226 if (!ir
->def
[0] || !ik
->def
[0])
1229 if (ik
->indirect
!= ir
->indirect
|| ik
->predicate
!= ir
->predicate
)
1232 for (d
= 0; d
< 4; ++d
) {
1233 if ((ir
->def
[d
] ? 1 : 0) != (ik
->def
[d
] ? 1 : 0))
1236 if (!values_equal(ik
->def
[0], ir
->def
[0]))
1246 for (s
= 0; s
< 5; ++s
) {
1247 struct nv_value
*a
, *b
;
1249 if ((ir
->src
[s
] ? 1 : 0) != (ik
->src
[s
] ? 1 : 0))
1256 if (ik
->src
[s
]->mod
!= ir
->src
[s
]->mod
)
1258 a
= ik
->src
[s
]->value
;
1259 b
= ir
->src
[s
]->value
;
1262 if (a
->reg
.file
!= b
->reg
.file
||
1263 a
->reg
.id
< 0 || /* this excludes memory loads/stores */
1264 a
->reg
.id
!= b
->reg
.id
)
1268 nvc0_insn_delete(ir
);
1269 for (d
= 0; d
< 4 && ir
->def
[d
]; ++d
)
1270 nvc0_pc_replace_value(ctx
->pc
, ir
->def
[d
], ik
->def
[d
]);
1278 DESCEND_ARBITRARY(s
, nv_pass_cse
);
1283 /* Make sure all sources of an NV_OP_BIND are distinct, they need to occupy
1284 * neighbouring registers. CSE might have messed this up.
1285 * Just generate a MOV for each source to avoid conflicts if they're used in
1286 * multiple NV_OP_BIND at different positions.
1289 nv_pass_fix_bind(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
1291 struct nv_value
*val
;
1292 struct nv_instruction
*bnd
, *nvi
, *next
;
1295 for (bnd
= b
->entry
; bnd
; bnd
= next
) {
1297 if (bnd
->opcode
!= NV_OP_BIND
)
1299 for (s
= 0; s
< 4 && bnd
->src
[s
]; ++s
) {
1300 val
= bnd
->src
[s
]->value
;
1302 nvi
= nv_alloc_instruction(ctx
->pc
, NV_OP_MOV
);
1303 nvi
->def
[0] = new_value_like(ctx
->pc
, val
);
1304 nvi
->def
[0]->insn
= nvi
;
1305 nv_reference(ctx
->pc
, nvi
, 0, val
);
1306 nv_reference(ctx
->pc
, bnd
, s
, nvi
->def
[0]);
1308 nvc0_insn_insert_before(bnd
, nvi
);
1311 DESCEND_ARBITRARY(s
, nv_pass_fix_bind
);
1317 nv_pc_pass0(struct nv_pc
*pc
, struct nv_basic_block
*root
)
1319 struct pass_reld_elim
*reldelim
;
1320 struct nv_pass pass
;
1321 struct nv_pass_dce dce
;
1327 /* Do CSE so we can just compare values by pointer in subsequent passes. */
1329 ret
= nv_pass_cse(&pass
, root
);
1333 /* Do this first, so we don't have to pay attention
1334 * to whether sources are supported memory loads.
1337 ret
= nv_pass_algebraic_opt(&pass
, root
);
1342 ret
= nv_pass_lower_mods(&pass
, root
);
1347 ret
= nvc0_pass_fold_loads(&pass
, root
);
1351 if (pc
->opt_reload_elim
) {
1352 reldelim
= CALLOC_STRUCT(pass_reld_elim
);
1356 ret
= nv_pass_reload_elim(reldelim
, root
);
1361 memset(reldelim
, 0, sizeof(struct pass_reld_elim
));
1365 /* May run DCE before load-combining since that pass will clean up
1372 ret
= nv_pass_dce(&dce
, root
);
1375 } while (dce
.removed
);
1377 if (pc
->opt_reload_elim
) {
1379 ret
= nv_pass_mem_opt(reldelim
, root
);
1381 memset(reldelim
, 0, sizeof(struct pass_reld_elim
));
1385 ret
= nv_pass_mem_opt(reldelim
, root
);
1392 ret
= nv_pass_tex_mask(&pass
, root
);
1397 ret
= nv_pass_fix_bind(&pass
, root
);
1403 nvc0_pc_exec_pass0(struct nv_pc
*pc
)
1407 for (i
= 0; i
< pc
->num_subroutines
+ 1; ++i
)
1408 if (pc
->root
[i
] && (ret
= nv_pc_pass0(pc
, pc
->root
[i
])))