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 (is_cspace_load(src0
->value
->insn
)) {
249 if (!is_cspace_load(src1
->value
->insn
)) {
254 if (is_immd32_load(src0
->value
->insn
)) {
255 if (!is_cspace_load(src1
->value
->insn
) &&
256 !is_immd32_load(src1
->value
->insn
)) {
262 if (nvi
->src
[0] != src0
&& NV_BASEOP(nvi
->opcode
) == NV_OP_SET
)
263 nvi
->set_cond
= (nvi
->set_cond
& ~7) | cc_swapped
[nvi
->set_cond
& 7];
267 nvi_set_indirect_load(struct nv_pc
*pc
,
268 struct nv_instruction
*nvi
, struct nv_value
*val
)
270 for (nvi
->indirect
= 0; nvi
->indirect
< 6 && nvi
->src
[nvi
->indirect
];
272 assert(nvi
->indirect
< 6);
273 nv_reference(pc
, nvi
, nvi
->indirect
, val
);
277 nvc0_pass_fold_loads(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
279 struct nv_instruction
*nvi
, *ld
;
282 for (nvi
= b
->entry
; nvi
; nvi
= nvi
->next
) {
283 check_swap_src_0_1(nvi
);
285 for (s
= 0; s
< 3 && nvi
->src
[s
]; ++s
) {
286 ld
= nvi
->src
[s
]->value
->insn
;
287 if (!ld
|| (ld
->opcode
!= NV_OP_LD
&& ld
->opcode
!= NV_OP_MOV
))
289 if (!nvc0_insn_can_load(nvi
, s
, ld
))
293 nv_reference(ctx
->pc
, nvi
, s
, ld
->src
[0]->value
);
294 if (ld
->indirect
>= 0)
295 nvi_set_indirect_load(ctx
->pc
, nvi
, ld
->src
[ld
->indirect
]->value
);
297 if (!nvc0_insn_refcount(ld
))
298 nvc0_insn_delete(ld
);
301 DESCEND_ARBITRARY(s
, nvc0_pass_fold_loads
);
306 /* NOTE: Assumes loads have not yet been folded. */
308 nv_pass_lower_mods(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
310 struct nv_instruction
*nvi
, *mi
, *next
;
314 for (nvi
= b
->entry
; nvi
; nvi
= next
) {
316 if (nvi
->opcode
== NV_OP_SUB
) {
317 nvi
->src
[1]->mod
^= NV_MOD_NEG
;
318 nvi
->opcode
= NV_OP_ADD
;
321 for (j
= 0; j
< 3 && nvi
->src
[j
]; ++j
) {
322 mi
= nvi
->src
[j
]->value
->insn
;
325 if (mi
->def
[0]->refc
> 1 || mi
->predicate
>= 0)
328 if (NV_BASEOP(mi
->opcode
) == NV_OP_NEG
) mod
= NV_MOD_NEG
;
330 if (NV_BASEOP(mi
->opcode
) == NV_OP_ABS
) mod
= NV_MOD_ABS
;
333 assert(!(mod
& mi
->src
[0]->mod
& NV_MOD_NEG
));
335 mod
|= mi
->src
[0]->mod
;
337 if ((nvi
->opcode
== NV_OP_ABS
) || (nvi
->src
[j
]->mod
& NV_MOD_ABS
)) {
338 /* abs neg [abs] = abs */
339 mod
&= ~(NV_MOD_NEG
| NV_MOD_ABS
);
341 if ((nvi
->opcode
== NV_OP_NEG
) && (mod
& NV_MOD_NEG
)) {
342 /* neg as opcode and modifier on same insn cannot occur */
343 /* neg neg abs = abs, neg neg = identity */
345 if (mod
& NV_MOD_ABS
)
346 nvi
->opcode
= NV_OP_ABS
;
348 nvi
->opcode
= NV_OP_MOV
;
352 if ((nv_op_supported_src_mods(nvi
->opcode
) & mod
) != mod
)
355 nv_reference(ctx
->pc
, nvi
, j
, mi
->src
[0]->value
);
357 nvi
->src
[j
]->mod
^= mod
;
360 if (nvi
->opcode
== NV_OP_SAT
) {
361 mi
= nvi
->src
[0]->value
->insn
;
363 if (mi
->def
[0]->refc
> 1 ||
364 (mi
->opcode
!= NV_OP_ADD
&&
365 mi
->opcode
!= NV_OP_MUL
&&
366 mi
->opcode
!= NV_OP_MAD
))
369 mi
->def
[0] = nvi
->def
[0];
370 mi
->def
[0]->insn
= mi
;
371 nvc0_insn_delete(nvi
);
374 DESCEND_ARBITRARY(j
, nv_pass_lower_mods
);
379 #define SRC_IS_MUL(s) ((s)->insn && (s)->insn->opcode == NV_OP_MUL)
382 apply_modifiers(uint32_t *val
, uint8_t type
, uint8_t mod
)
384 if (mod
& NV_MOD_ABS
) {
385 if (type
== NV_TYPE_F32
)
388 if ((*val
) & (1 << 31))
391 if (mod
& NV_MOD_NEG
) {
392 if (type
== NV_TYPE_F32
)
397 if (mod
& NV_MOD_SAT
) {
404 if (type
== NV_TYPE_F32
) {
405 u
.f
= CLAMP(u
.f
, -1.0f
, 1.0f
);
407 if (type
== NV_TYPE_U16
) {
408 u
.u
= MIN2(u
.u
, 0xffff);
410 if (type
== NV_TYPE_S16
) {
411 u
.i
= CLAMP(u
.i
, -32768, 32767);
415 if (mod
& NV_MOD_NOT
)
420 constant_expression(struct nv_pc
*pc
, struct nv_instruction
*nvi
,
421 struct nv_value
*src0
, struct nv_value
*src1
)
423 struct nv_value
*val
;
433 type
= NV_OPTYPE(nvi
->opcode
);
436 u0
.u32
= src0
->reg
.imm
.u32
;
437 u1
.u32
= src1
->reg
.imm
.u32
;
439 apply_modifiers(&u0
.u32
, type
, nvi
->src
[0]->mod
);
440 apply_modifiers(&u1
.u32
, type
, nvi
->src
[1]->mod
);
442 switch (nvi
->opcode
) {
444 if (nvi
->src
[2]->value
->reg
.file
!= NV_FILE_GPR
)
448 u
.f32
= u0
.f32
* u1
.f32
;
451 u
.u32
= u0
.u32
* u1
.u32
;
454 u
.f32
= u0
.f32
+ u1
.f32
;
457 u
.u32
= u0
.u32
+ u1
.u32
;
460 u
.f32
= u0
.f32
- u1
.f32
;
464 u.u32 = u0.u32 - u1.u32;
471 val
= new_value(pc
, NV_FILE_IMM
, nv_type_sizeof(type
));
472 val
->reg
.imm
.u32
= u
.u32
;
474 nv_reference(pc
, nvi
, 1, NULL
);
475 nv_reference(pc
, nvi
, 0, val
);
477 if (nvi
->opcode
== NV_OP_MAD_F32
) {
478 nvi
->src
[1] = nvi
->src
[0];
479 nvi
->src
[0] = nvi
->src
[2];
481 nvi
->opcode
= NV_OP_ADD_F32
;
483 if (val
->reg
.imm
.u32
== 0) {
485 nvi
->opcode
= NV_OP_MOV
;
488 nvi
->opcode
= NV_OP_MOV
;
493 constant_operand(struct nv_pc
*pc
,
494 struct nv_instruction
*nvi
, struct nv_value
*val
, int s
)
508 type
= NV_OPTYPE(nvi
->opcode
);
510 u
.u32
= val
->reg
.imm
.u32
;
511 apply_modifiers(&u
.u32
, type
, nvi
->src
[s
]->mod
);
513 if (u
.u32
== 0 && NV_BASEOP(nvi
->opcode
) == NV_OP_MUL
) {
514 nvi
->opcode
= NV_OP_MOV
;
515 nv_reference(pc
, nvi
, t
, NULL
);
517 nvi
->src
[0] = nvi
->src
[1];
523 switch (nvi
->opcode
) {
525 if (u
.f32
== 1.0f
|| u
.f32
== -1.0f
) {
527 nvi
->src
[t
]->mod
^= NV_MOD_NEG
;
528 switch (nvi
->src
[t
]->mod
) {
529 case 0: op
= nvi
->saturate
? NV_OP_SAT
: NV_OP_MOV
; break;
530 case NV_MOD_NEG
: op
= NV_OP_NEG_F32
; break;
531 case NV_MOD_ABS
: op
= NV_OP_ABS_F32
; break;
536 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
537 nv_reference(pc
, nvi
, 1, NULL
);
538 nvi
->src
[0]->mod
= 0;
540 if (u
.f32
== 2.0f
|| u
.f32
== -2.0f
) {
542 nvi
->src
[t
]->mod
^= NV_MOD_NEG
;
543 nvi
->opcode
= NV_OP_ADD_F32
;
544 nv_reference(pc
, nvi
, s
, nvi
->src
[t
]->value
);
545 nvi
->src
[s
]->mod
= nvi
->src
[t
]->mod
;
549 switch (nvi
->src
[t
]->mod
) {
550 case 0: op
= nvi
->saturate
? NV_OP_SAT
: NV_OP_MOV
; break;
551 case NV_MOD_NEG
: op
= NV_OP_NEG_F32
; break;
552 case NV_MOD_ABS
: op
= NV_OP_ABS_F32
; break;
553 case NV_MOD_NEG
| NV_MOD_ABS
:
555 nvi
->ext
.cvt
.s
= nvi
->ext
.cvt
.d
= type
;
561 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
562 nv_reference(pc
, nvi
, 1, NULL
);
563 if (nvi
->opcode
!= NV_OP_CVT
)
564 nvi
->src
[0]->mod
= 0;
568 assert(nvi
->src
[t
]->mod
== 0);
569 nvi
->opcode
= nvi
->saturate
? NV_OP_CVT
: NV_OP_MOV
;
570 nvi
->ext
.cvt
.s
= nvi
->ext
.cvt
.d
= type
;
571 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
572 nv_reference(pc
, nvi
, 1, NULL
);
576 /* multiplication by 0 already handled above */
577 assert(nvi
->src
[s
]->mod
== 0);
578 shift
= ffs(u
.s32
) - 1;
580 nvi
->opcode
= NV_OP_MOV
;
581 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
582 nv_reference(pc
, nvi
, 1, NULL
);
584 if (u
.s32
> 0 && u
.s32
== (1 << shift
)) {
585 nvi
->opcode
= NV_OP_SHL
;
586 (val
= new_value(pc
, NV_FILE_IMM
, NV_TYPE_U32
))->reg
.imm
.s32
= shift
;
587 nv_reference(pc
, nvi
, 0, nvi
->src
[t
]->value
);
588 nv_reference(pc
, nvi
, 1, val
);
593 u
.f32
= 1.0f
/ u
.f32
;
594 (val
= new_value(pc
, NV_FILE_IMM
, NV_TYPE_F32
))->reg
.imm
.f32
= u
.f32
;
595 nvi
->opcode
= NV_OP_MOV
;
597 nv_reference(pc
, nvi
, 0, val
);
600 u
.f32
= 1.0f
/ sqrtf(u
.f32
);
601 (val
= new_value(pc
, NV_FILE_IMM
, NV_TYPE_F32
))->reg
.imm
.f32
= u
.f32
;
602 nvi
->opcode
= NV_OP_MOV
;
604 nv_reference(pc
, nvi
, 0, val
);
612 handle_min_max(struct nv_pass
*ctx
, struct nv_instruction
*nvi
)
614 struct nv_value
*src0
= nvi
->src
[0]->value
;
615 struct nv_value
*src1
= nvi
->src
[1]->value
;
617 if (src0
!= src1
|| (nvi
->src
[0]->mod
| nvi
->src
[1]->mod
))
619 if (src0
->reg
.file
!= NV_FILE_GPR
)
621 nvc0_pc_replace_value(ctx
->pc
, nvi
->def
[0], src0
);
622 nvc0_insn_delete(nvi
);
625 /* check if we can MUL + ADD -> MAD/FMA */
627 handle_add_mul(struct nv_pass
*ctx
, struct nv_instruction
*nvi
)
629 struct nv_value
*src0
= nvi
->src
[0]->value
;
630 struct nv_value
*src1
= nvi
->src
[1]->value
;
631 struct nv_value
*src
;
635 if (SRC_IS_MUL(src0
) && src0
->refc
== 1) s
= 0;
637 if (SRC_IS_MUL(src1
) && src1
->refc
== 1) s
= 1;
641 if ((src0
->insn
&& src0
->insn
->bb
!= nvi
->bb
) ||
642 (src1
->insn
&& src1
->insn
->bb
!= nvi
->bb
))
645 /* check for immediates from prior constant folding */
646 if (src0
->reg
.file
!= NV_FILE_GPR
|| src1
->reg
.file
!= NV_FILE_GPR
)
648 src
= nvi
->src
[s
]->value
;
650 mod
[0] = nvi
->src
[0]->mod
;
651 mod
[1] = nvi
->src
[1]->mod
;
652 mod
[2] = src
->insn
->src
[0]->mod
;
653 mod
[3] = src
->insn
->src
[1]->mod
;
655 if ((mod
[0] | mod
[1] | mod
[2] | mod
[3]) & ~NV_MOD_NEG
)
658 nvi
->opcode
= NV_OP_MAD_F32
;
660 nv_reference(ctx
->pc
, nvi
, s
, NULL
);
661 nvi
->src
[2] = nvi
->src
[!s
];
664 nv_reference(ctx
->pc
, nvi
, 0, src
->insn
->src
[0]->value
);
665 nvi
->src
[0]->mod
= mod
[2] ^ mod
[s
];
666 nv_reference(ctx
->pc
, nvi
, 1, src
->insn
->src
[1]->value
);
667 nvi
->src
[1]->mod
= mod
[3];
671 nv_pass_algebraic_opt(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
673 struct nv_instruction
*nvi
, *next
;
676 for (nvi
= b
->entry
; nvi
; nvi
= next
) {
677 struct nv_value
*src0
, *src1
;
678 uint baseop
= NV_BASEOP(nvi
->opcode
);
682 src0
= nvc0_pc_find_immediate(nvi
->src
[0]);
683 src1
= nvc0_pc_find_immediate(nvi
->src
[1]);
686 constant_expression(ctx
->pc
, nvi
, src0
, src1
);
689 constant_operand(ctx
->pc
, nvi
, src0
, 0);
692 constant_operand(ctx
->pc
, nvi
, src1
, 1);
695 if (baseop
== NV_OP_MIN
|| baseop
== NV_OP_MAX
)
696 handle_min_max(ctx
, nvi
);
698 if (nvi
->opcode
== NV_OP_ADD_F32
)
699 handle_add_mul(ctx
, nvi
);
701 DESCEND_ARBITRARY(j
, nv_pass_algebraic_opt
);
706 /* TODO: redundant store elimination */
709 struct mem_record
*next
;
710 struct nv_instruction
*insn
;
716 #define MEM_RECORD_POOL_SIZE 1024
718 struct pass_reld_elim
{
721 struct mem_record
*imm
;
722 struct mem_record
*mem_v
;
723 struct mem_record
*mem_a
;
724 struct mem_record
*mem_c
[16];
725 struct mem_record
*mem_l
;
727 struct mem_record pool
[MEM_RECORD_POOL_SIZE
];
731 /* Extend the load operation in @rec to also cover the data loaded by @ld.
732 * The two loads may not overlap but reference adjacent memory locations.
735 combine_load(struct mem_record
*rec
, struct nv_instruction
*ld
)
737 struct nv_instruction
*fv
= rec
->insn
;
738 struct nv_value
*mem
= ld
->src
[0]->value
;
739 uint32_t size
= rec
->size
+ mem
->reg
.size
;
741 int d
= rec
->size
/ 4;
743 assert(rec
->size
< 16);
744 if (rec
->ofst
> mem
->reg
.address
) {
745 if ((size
== 8 && mem
->reg
.address
& 3) ||
746 (size
> 8 && mem
->reg
.address
& 7))
748 rec
->ofst
= mem
->reg
.address
;
749 for (j
= 0; j
< d
; ++j
)
750 fv
->def
[mem
->reg
.size
/ 4 + j
] = fv
->def
[j
];
753 if ((size
== 8 && rec
->ofst
& 3) ||
754 (size
> 8 && rec
->ofst
& 7)) {
758 for (j
= 0; j
< mem
->reg
.size
/ 4; ++j
) {
759 fv
->def
[d
] = ld
->def
[j
];
760 fv
->def
[d
++]->insn
= fv
;
763 fv
->src
[0]->value
->reg
.address
= rec
->ofst
;
764 fv
->src
[0]->value
->reg
.size
= rec
->size
= size
;
766 nvc0_insn_delete(ld
);
770 combine_export(struct mem_record
*rec
, struct nv_instruction
*ex
)
776 add_mem_record(struct pass_reld_elim
*ctx
, struct mem_record
**rec
,
777 uint32_t base
, uint32_t ofst
, struct nv_instruction
*nvi
)
779 struct mem_record
*it
= &ctx
->pool
[ctx
->alloc
++];
786 it
->size
= nvi
->src
[0]->value
->reg
.size
;
789 /* vectorize and reuse loads from memory or of immediates */
791 nv_pass_mem_opt(struct pass_reld_elim
*ctx
, struct nv_basic_block
*b
)
793 struct mem_record
**rec
, *it
;
794 struct nv_instruction
*ld
, *next
;
795 struct nv_value
*mem
;
799 for (ld
= b
->entry
; ld
; ld
= next
) {
802 if (is_cspace_load(ld
)) {
803 mem
= ld
->src
[0]->value
;
804 rec
= &ctx
->mem_c
[ld
->src
[0]->value
->reg
.file
- NV_FILE_MEM_C(0)];
806 if (ld
->opcode
== NV_OP_VFETCH
) {
807 mem
= ld
->src
[0]->value
;
810 if (ld
->opcode
== NV_OP_EXPORT
) {
811 mem
= ld
->src
[0]->value
;
812 if (mem
->reg
.file
!= NV_FILE_MEM_V
)
818 if (ld
->def
[0] && ld
->def
[0]->refc
== 0)
820 ofst
= mem
->reg
.address
;
821 base
= (ld
->indirect
>= 0) ? ld
->src
[ld
->indirect
]->value
->n
: 0;
823 for (it
= *rec
; it
; it
= it
->next
) {
824 if (it
->base
== base
&&
825 ((it
->ofst
>> 4) == (ofst
>> 4)) &&
826 ((it
->ofst
+ it
->size
== ofst
) ||
827 (it
->ofst
- mem
->reg
.size
== ofst
))) {
828 /* only NV_OP_VFETCH can load exactly 12 bytes */
829 if (ld
->opcode
== NV_OP_LD
&& it
->size
+ mem
->reg
.size
== 12)
831 if (it
->ofst
< ofst
) {
832 if ((it
->ofst
& 0xf) == 4)
835 if ((ofst
& 0xf) == 4)
841 switch (ld
->opcode
) {
842 case NV_OP_EXPORT
: combine_export(it
, ld
); break;
844 combine_load(it
, ld
);
848 if (ctx
->alloc
< MEM_RECORD_POOL_SIZE
) {
849 add_mem_record(ctx
, rec
, base
, ofst
, ld
);
854 ctx
->mem_a
= ctx
->mem_v
= ctx
->mem_l
= NULL
;
855 for (s
= 0; s
< 16; ++s
)
856 ctx
->mem_c
[s
] = NULL
;
858 DESCEND_ARBITRARY(s
, nv_pass_mem_opt
);
863 eliminate_store(struct mem_record
*rec
, struct nv_instruction
*st
)
867 /* elimination of redundant stores */
869 pass_store_elim(struct pass_reld_elim
*ctx
, struct nv_basic_block
*b
)
871 struct mem_record
**rec
, *it
;
872 struct nv_instruction
*st
, *next
;
873 struct nv_value
*mem
;
874 uint32_t base
, ofst
, size
;
877 for (st
= b
->entry
; st
; st
= next
) {
880 if (st
->opcode
== NV_OP_ST
) {
881 mem
= st
->src
[0]->value
;
884 if (st
->opcode
== NV_OP_EXPORT
) {
885 mem
= st
->src
[0]->value
;
886 if (mem
->reg
.file
!= NV_FILE_MEM_V
)
890 if (st
->opcode
== NV_OP_ST
) {
893 ofst
= mem
->reg
.address
;
894 base
= (st
->indirect
>= 0) ? st
->src
[st
->indirect
]->value
->n
: 0;
895 size
= mem
->reg
.size
;
897 for (it
= *rec
; it
; it
= it
->next
) {
898 if (it
->base
== base
&&
899 (it
->ofst
<= ofst
&& (it
->ofst
+ size
) > ofst
))
903 eliminate_store(it
, st
);
905 add_mem_record(ctx
, rec
, base
, ofst
, st
);
908 DESCEND_ARBITRARY(s
, nv_pass_mem_opt
);
912 /* TODO: properly handle loads from l[] memory in the presence of stores */
914 nv_pass_reload_elim(struct pass_reld_elim
*ctx
, struct nv_basic_block
*b
)
917 struct load_record
**rec
, *it
;
918 struct nv_instruction
*ld
, *next
;
920 struct nv_value
*val
;
923 for (ld
= b
->entry
; ld
; ld
= next
) {
927 val
= ld
->src
[0]->value
;
930 if (ld
->opcode
== NV_OP_LINTERP
|| ld
->opcode
== NV_OP_PINTERP
) {
931 data
[0] = val
->reg
.id
;
935 if (ld
->opcode
== NV_OP_LDA
) {
936 data
[0] = val
->reg
.id
;
937 data
[1] = ld
->src
[4] ? ld
->src
[4]->value
->n
: ~0ULL;
938 if (val
->reg
.file
>= NV_FILE_MEM_C(0) &&
939 val
->reg
.file
<= NV_FILE_MEM_C(15))
940 rec
= &ctx
->mem_c
[val
->reg
.file
- NV_FILE_MEM_C(0)];
942 if (val
->reg
.file
== NV_FILE_MEM_S
)
945 if (val
->reg
.file
== NV_FILE_MEM_L
)
948 if ((ld
->opcode
== NV_OP_MOV
) && (val
->reg
.file
== NV_FILE_IMM
)) {
949 data
[0] = val
->reg
.imm
.u32
;
954 if (!rec
|| !ld
->def
[0]->refc
)
957 for (it
= *rec
; it
; it
= it
->next
)
958 if (it
->data
[0] == data
[0] && it
->data
[1] == data
[1])
962 if (ld
->def
[0]->reg
.id
>= 0)
963 it
->value
= ld
->def
[0];
966 nvc0_pc_replace_value(ctx
->pc
, ld
->def
[0], it
->value
);
968 if (ctx
->alloc
== LOAD_RECORD_POOL_SIZE
)
970 it
= &ctx
->pool
[ctx
->alloc
++];
972 it
->data
[0] = data
[0];
973 it
->data
[1] = data
[1];
974 it
->value
= ld
->def
[0];
982 for (j
= 0; j
< 16; ++j
)
983 ctx
->mem_c
[j
] = NULL
;
987 DESCEND_ARBITRARY(j
, nv_pass_reload_elim
);
993 nv_pass_tex_mask(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
997 for (i
= 0; i
< ctx
->pc
->num_instructions
; ++i
) {
998 struct nv_instruction
*nvi
= &ctx
->pc
->instructions
[i
];
999 struct nv_value
*def
[4];
1001 if (!nv_is_texture_op(nvi
->opcode
))
1005 for (c
= 0; c
< 4; ++c
) {
1006 if (nvi
->def
[c
]->refc
)
1007 nvi
->tex_mask
|= 1 << c
;
1008 def
[c
] = nvi
->def
[c
];
1012 for (c
= 0; c
< 4; ++c
)
1013 if (nvi
->tex_mask
& (1 << c
))
1014 nvi
->def
[j
++] = def
[c
];
1015 for (c
= 0; c
< 4; ++c
)
1016 if (!(nvi
->tex_mask
& (1 << c
)))
1017 nvi
->def
[j
++] = def
[c
];
1023 struct nv_pass_dce
{
1029 nv_pass_dce(struct nv_pass_dce
*ctx
, struct nv_basic_block
*b
)
1032 struct nv_instruction
*nvi
, *next
;
1034 for (nvi
= b
->phi
? b
->phi
: b
->entry
; nvi
; nvi
= next
) {
1037 if (inst_removable(nvi
)) {
1038 nvc0_insn_delete(nvi
);
1042 DESCEND_ARBITRARY(j
, nv_pass_dce
);
1047 /* Register allocation inserted ELSE blocks for all IF/ENDIF without ELSE.
1048 * Returns TRUE if @bb initiates an IF/ELSE/ENDIF clause, or is an IF with
1049 * BREAK and dummy ELSE block.
1051 static INLINE boolean
1052 bb_is_if_else_endif(struct nv_basic_block
*bb
)
1054 if (!bb
->out
[0] || !bb
->out
[1])
1057 if (bb
->out
[0]->out_kind
[0] == CFG_EDGE_LOOP_LEAVE
) {
1058 return (bb
->out
[0]->out
[1] == bb
->out
[1]->out
[0] &&
1059 !bb
->out
[1]->out
[1]);
1061 return (bb
->out
[0]->out
[0] == bb
->out
[1]->out
[0] &&
1062 !bb
->out
[0]->out
[1] &&
1063 !bb
->out
[1]->out
[1]);
1067 /* Predicate instructions and delete any branch at the end if it is
1068 * not a break from a loop.
1071 predicate_instructions(struct nv_pc
*pc
, struct nv_basic_block
*b
,
1072 struct nv_value
*pred
, uint8_t cc
)
1074 struct nv_instruction
*nvi
, *prev
;
1079 for (nvi
= b
->entry
; nvi
; nvi
= nvi
->next
) {
1081 if (inst_is_noop(nvi
))
1083 for (s
= 0; nvi
->src
[s
]; ++s
);
1087 nv_reference(pc
, nvi
, nvi
->predicate
, pred
);
1089 if (prev
->opcode
== NV_OP_BRA
&&
1090 b
->out_kind
[0] != CFG_EDGE_LOOP_LEAVE
&&
1091 b
->out_kind
[1] != CFG_EDGE_LOOP_LEAVE
)
1092 nvc0_insn_delete(prev
);
1095 static INLINE boolean
1096 may_predicate_insn(struct nv_instruction
*nvi
, struct nv_value
*pred
)
1098 if (nvi
->def
[0] && values_equal(nvi
->def
[0], pred
))
1100 return nvc0_insn_is_predicateable(nvi
);
1103 /* Transform IF/ELSE/ENDIF constructs into predicated instructions
1107 nv_pass_flatten(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
1109 struct nv_instruction
*nvi
;
1110 struct nv_value
*pred
;
1112 int n0
, n1
; /* instruction counts of outgoing blocks */
1114 if (bb_is_if_else_endif(b
)) {
1115 assert(b
->exit
&& b
->exit
->opcode
== NV_OP_BRA
);
1117 assert(b
->exit
->predicate
>= 0);
1118 pred
= b
->exit
->src
[b
->exit
->predicate
]->value
;
1121 for (nvi
= b
->out
[0]->entry
; nvi
; nvi
= nvi
->next
, ++n0
)
1122 if (!may_predicate_insn(nvi
, pred
))
1125 /* we're after register allocation, so there always is an ELSE block */
1126 for (nvi
= b
->out
[1]->entry
; nvi
; nvi
= nvi
->next
, ++n1
)
1127 if (!may_predicate_insn(nvi
, pred
))
1131 /* 12 is an arbitrary limit */
1132 if (!nvi
&& n0
< 12 && n1
< 12) {
1133 predicate_instructions(ctx
->pc
, b
->out
[0], pred
, !b
->exit
->cc
);
1134 predicate_instructions(ctx
->pc
, b
->out
[1], pred
, b
->exit
->cc
);
1136 nvc0_insn_delete(b
->exit
); /* delete the branch */
1138 /* and a potential joinat before it */
1139 if (b
->exit
&& b
->exit
->opcode
== NV_OP_JOINAT
)
1140 nvc0_insn_delete(b
->exit
);
1142 /* remove join operations at the end of the conditional */
1143 k
= (b
->out
[0]->out_kind
[0] == CFG_EDGE_LOOP_LEAVE
) ? 1 : 0;
1144 if ((nvi
= b
->out
[0]->out
[k
]->entry
)) {
1146 if (nvi
->opcode
== NV_OP_JOIN
)
1147 nvc0_insn_delete(nvi
);
1151 DESCEND_ARBITRARY(k
, nv_pass_flatten
);
1156 /* Tests instructions for equality, but independently of sources. */
1158 is_operation_equal(struct nv_instruction
*a
, struct nv_instruction
*b
)
1160 if (a
->opcode
!= b
->opcode
)
1162 if (nv_is_texture_op(a
->opcode
)) {
1163 if (a
->ext
.tex
.t
!= b
->ext
.tex
.t
||
1164 a
->ext
.tex
.s
!= b
->ext
.tex
.s
)
1166 if (a
->tex_dim
!= b
->tex_dim
||
1167 a
->tex_array
!= b
->tex_array
||
1168 a
->tex_cube
!= b
->tex_cube
||
1169 a
->tex_shadow
!= b
->tex_shadow
||
1170 a
->tex_live
!= b
->tex_live
)
1173 if (a
->opcode
== NV_OP_CVT
) {
1174 if (a
->ext
.cvt
.s
!= b
->ext
.cvt
.s
||
1175 a
->ext
.cvt
.d
!= b
->ext
.cvt
.d
)
1178 if (NV_BASEOP(a
->opcode
) == NV_OP_SET
||
1179 NV_BASEOP(a
->opcode
) == NV_OP_SLCT
) {
1180 if (a
->set_cond
!= b
->set_cond
)
1183 if (a
->opcode
== NV_OP_LINTERP
||
1184 a
->opcode
== NV_OP_PINTERP
) {
1185 if (a
->centroid
!= b
->centroid
||
1191 if (a
->lanes
!= b
->lanes
||
1192 a
->patch
!= b
->patch
||
1193 a
->saturate
!= b
->saturate
)
1195 if (a
->opcode
== NV_OP_QUADOP
) /* beware quadon ! */
1200 /* local common subexpression elimination, stupid O(n^2) implementation */
1202 nv_pass_cse(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
1204 struct nv_instruction
*ir
, *ik
, *next
;
1205 struct nv_instruction
*entry
= b
->phi
? b
->phi
: b
->entry
;
1211 for (ir
= entry
; ir
; ir
= next
) {
1215 for (ik
= entry
; ik
!= ir
; ik
= ik
->next
) {
1216 if (!is_operation_equal(ir
, ik
))
1218 if (!ir
->def
[0] || !ik
->def
[0])
1221 if (ik
->indirect
!= ir
->indirect
|| ik
->predicate
!= ir
->predicate
)
1224 for (d
= 0; d
< 4; ++d
) {
1225 if ((ir
->def
[d
] ? 1 : 0) != (ik
->def
[d
] ? 1 : 0))
1228 if (!values_equal(ik
->def
[0], ir
->def
[0]))
1238 for (s
= 0; s
< 5; ++s
) {
1239 struct nv_value
*a
, *b
;
1241 if ((ir
->src
[s
] ? 1 : 0) != (ik
->src
[s
] ? 1 : 0))
1248 if (ik
->src
[s
]->mod
!= ir
->src
[s
]->mod
)
1250 a
= ik
->src
[s
]->value
;
1251 b
= ir
->src
[s
]->value
;
1254 if (a
->reg
.file
!= b
->reg
.file
||
1255 a
->reg
.id
< 0 || /* this excludes memory loads/stores */
1256 a
->reg
.id
!= b
->reg
.id
)
1260 nvc0_insn_delete(ir
);
1261 for (d
= 0; d
< 4 && ir
->def
[d
]; ++d
)
1262 nvc0_pc_replace_value(ctx
->pc
, ir
->def
[d
], ik
->def
[d
]);
1270 DESCEND_ARBITRARY(s
, nv_pass_cse
);
1275 /* Make sure all sources of an NV_OP_BIND are distinct, they need to occupy
1276 * neighbouring registers. CSE might have messed this up.
1277 * Just generate a MOV for each source to avoid conflicts if they're used in
1278 * multiple NV_OP_BIND at different positions.
1281 nv_pass_fix_bind(struct nv_pass
*ctx
, struct nv_basic_block
*b
)
1283 struct nv_value
*val
;
1284 struct nv_instruction
*bnd
, *nvi
, *next
;
1287 for (bnd
= b
->entry
; bnd
; bnd
= next
) {
1289 if (bnd
->opcode
!= NV_OP_BIND
)
1291 for (s
= 0; s
< 4 && bnd
->src
[s
]; ++s
) {
1292 val
= bnd
->src
[s
]->value
;
1294 nvi
= nv_alloc_instruction(ctx
->pc
, NV_OP_MOV
);
1295 nvi
->def
[0] = new_value_like(ctx
->pc
, val
);
1296 nvi
->def
[0]->insn
= nvi
;
1297 nv_reference(ctx
->pc
, nvi
, 0, val
);
1298 nv_reference(ctx
->pc
, bnd
, s
, nvi
->def
[0]);
1300 nvc0_insn_insert_before(bnd
, nvi
);
1303 DESCEND_ARBITRARY(s
, nv_pass_fix_bind
);
1309 nv_pc_pass0(struct nv_pc
*pc
, struct nv_basic_block
*root
)
1311 struct pass_reld_elim
*reldelim
;
1312 struct nv_pass pass
;
1313 struct nv_pass_dce dce
;
1319 /* Do CSE so we can just compare values by pointer in subsequent passes. */
1321 ret
= nv_pass_cse(&pass
, root
);
1325 /* Do this first, so we don't have to pay attention
1326 * to whether sources are supported memory loads.
1329 ret
= nv_pass_algebraic_opt(&pass
, root
);
1334 ret
= nv_pass_lower_mods(&pass
, root
);
1339 ret
= nvc0_pass_fold_loads(&pass
, root
);
1343 if (pc
->opt_reload_elim
) {
1344 reldelim
= CALLOC_STRUCT(pass_reld_elim
);
1348 ret
= nv_pass_reload_elim(reldelim
, root
);
1353 memset(reldelim
, 0, sizeof(struct pass_reld_elim
));
1357 /* May run DCE before load-combining since that pass will clean up
1364 ret
= nv_pass_dce(&dce
, root
);
1367 } while (dce
.removed
);
1369 if (pc
->opt_reload_elim
) {
1371 ret
= nv_pass_mem_opt(reldelim
, root
);
1373 memset(reldelim
, 0, sizeof(struct pass_reld_elim
));
1377 ret
= nv_pass_mem_opt(reldelim
, root
);
1384 ret
= nv_pass_tex_mask(&pass
, root
);
1389 ret
= nv_pass_fix_bind(&pass
, root
);
1395 nvc0_pc_exec_pass0(struct nv_pc
*pc
)
1399 for (i
= 0; i
< pc
->num_subroutines
+ 1; ++i
)
1400 if (pc
->root
[i
] && (ret
= nv_pc_pass0(pc
, pc
->root
[i
])))