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->num_blocks = 0; /* will reorder bb_list */
243 for (i = 0; i < pc->num_subroutines + 1; ++i)
244 if (pc->root[i] && (ret = nv_pc_pass2(pc, pc->root[i])))
249 static INLINE boolean
250 is_cmem_load(struct nv_instruction *nvi)
252 return (nvi->opcode == NV_OP_LDA &&
253 nvi->src[0]->value->reg.file >= NV_FILE_MEM_C(0) &&
254 nvi->src[0]->value->reg.file <= NV_FILE_MEM_C(15));
257 static INLINE boolean
258 is_smem_load(struct nv_instruction *nvi)
260 return (nvi->opcode == NV_OP_LDA &&
261 (nvi->src[0]->value->reg.file == NV_FILE_MEM_S ||
262 nvi->src[0]->value->reg.file <= NV_FILE_MEM_P));
265 static INLINE boolean
266 is_immd_move(struct nv_instruction *nvi)
268 return (nvi->opcode == NV_OP_MOV &&
269 nvi->src[0]->value->reg.file == NV_FILE_IMM);
273 check_swap_src_0_1(struct nv_instruction *nvi)
275 static const ubyte cc_swapped[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
277 struct nv_ref *src0 = nvi->src[0], *src1 = nvi->src[1];
279 if (!nv_op_commutative(nvi->opcode))
281 assert(src0 && src1);
283 if (src1->value->reg.file == NV_FILE_IMM)
286 if (is_cmem_load(src0->value->insn)) {
287 if (!is_cmem_load(src1->value->insn)) {
290 /* debug_printf("swapping cmem load to 1\n"); */
293 if (is_smem_load(src1->value->insn)) {
294 if (!is_smem_load(src0->value->insn)) {
297 /* debug_printf("swapping smem load to 0\n"); */
301 if (nvi->opcode == NV_OP_SET && nvi->src[0] != src0)
302 nvi->set_cond = (nvi->set_cond & ~7) | cc_swapped[nvi->set_cond & 7];
306 nv_pass_fold_stores(struct nv_pass *ctx, struct nv_basic_block *b)
308 struct nv_instruction *nvi, *sti, *next;
311 for (sti = b->entry; sti; sti = next) {
314 /* only handling MOV to $oX here */
315 if (!sti->def[0] || sti->def[0]->reg.file != NV_FILE_OUT)
317 if (sti->opcode != NV_OP_MOV && sti->opcode != NV_OP_STA)
320 nvi = sti->src[0]->value->insn;
321 if (!nvi || nvi->opcode == NV_OP_PHI || nv_is_vector_op(nvi->opcode))
323 assert(nvi->def[0] == sti->src[0]->value);
325 if (nvi->opcode == NV_OP_SELECT)
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 ||
333 nvi->src[j]->value->reg.file == NV_FILE_MEM_L)
335 if (j < 4 && nvi->src[j])
338 nvi->def[0] = sti->def[0];
339 nvi->def[0]->insn = nvi;
340 nvi->fixed = sti->fixed;
344 DESCEND_ARBITRARY(j, nv_pass_fold_stores);
350 nv_pass_fold_loads(struct nv_pass *ctx, struct nv_basic_block *b)
352 struct nv_instruction *nvi, *ld;
355 for (nvi = b->entry; nvi; nvi = nvi->next) {
356 check_swap_src_0_1(nvi);
358 for (j = 0; j < 3; ++j) {
361 ld = nvi->src[j]->value->insn;
365 if (is_immd_move(ld) && nv50_nvi_can_use_imm(nvi, j)) {
366 nv_reference(ctx->pc, &nvi->src[j], ld->src[0]->value);
370 if (ld->opcode != NV_OP_LDA)
372 if (!nv50_nvi_can_load(nvi, j, ld->src[0]->value))
375 if (j == 0 && ld->src[4]) /* can't load shared mem */
379 nv_reference(ctx->pc, &nvi->src[j], ld->src[0]->value);
381 nv_reference(ctx->pc, &nvi->src[4], ld->src[4]->value);
383 if (!nv_nvi_refcount(ld))
387 DESCEND_ARBITRARY(j, nv_pass_fold_loads);
392 /* NOTE: Assumes loads have not yet been folded. */
394 nv_pass_lower_mods(struct nv_pass *ctx, struct nv_basic_block *b)
397 struct nv_instruction *nvi, *mi, *next;
400 for (nvi = b->entry; nvi; nvi = next) {
402 if (nvi->opcode == NV_OP_SUB) {
403 nvi->opcode = NV_OP_ADD;
404 nvi->src[1]->mod ^= NV_MOD_NEG;
407 for (j = 0; j < 4 && nvi->src[j]; ++j) {
408 mi = nvi->src[j]->value->insn;
411 if (mi->def[0]->refc > 1)
414 if (mi->opcode == NV_OP_NEG) mod = NV_MOD_NEG;
416 if (mi->opcode == NV_OP_ABS) mod = NV_MOD_ABS;
419 assert(!(mod & mi->src[0]->mod & NV_MOD_NEG));
421 mod |= mi->src[0]->mod;
423 if (mi->flags_def || mi->flags_src)
426 if ((nvi->opcode == NV_OP_ABS) || (nvi->src[j]->mod & NV_MOD_ABS)) {
427 /* abs neg [abs] = abs */
428 mod &= ~(NV_MOD_NEG | NV_MOD_ABS);
430 if ((nvi->opcode == NV_OP_NEG) && (mod & NV_MOD_NEG)) {
431 /* neg as opcode and modifier on same insn cannot occur */
432 /* neg neg abs = abs, neg neg = identity */
434 if (mod & NV_MOD_ABS)
435 nvi->opcode = NV_OP_ABS;
438 nvi->opcode = NV_OP_CVT;
440 nvi->opcode = NV_OP_MOV;
444 if ((nv50_supported_src_mods(nvi->opcode, j) & mod) != mod)
447 nv_reference(ctx->pc, &nvi->src[j], mi->src[0]->value);
449 nvi->src[j]->mod ^= mod;
452 if (nvi->opcode == NV_OP_SAT) {
453 mi = nvi->src[0]->value->insn;
455 if (mi->opcode != NV_OP_ADD && mi->opcode != NV_OP_MAD)
457 if (mi->flags_def || mi->def[0]->refc > 1)
461 mi->def[0] = nvi->def[0];
462 mi->def[0]->insn = mi;
466 DESCEND_ARBITRARY(j, nv_pass_lower_mods);
471 #define SRC_IS_MUL(s) ((s)->insn && (s)->insn->opcode == NV_OP_MUL)
474 modifiers_apply(uint32_t *val, ubyte type, ubyte mod)
476 if (mod & NV_MOD_ABS) {
477 if (type == NV_TYPE_F32)
480 if ((*val) & (1 << 31))
483 if (mod & NV_MOD_NEG) {
484 if (type == NV_TYPE_F32)
492 modifiers_opcode(ubyte mod)
495 case NV_MOD_NEG: return NV_OP_NEG;
496 case NV_MOD_ABS: return NV_OP_ABS;
505 constant_expression(struct nv_pc *pc, struct nv_instruction *nvi,
506 struct nv_value *src0, struct nv_value *src1)
508 struct nv_value *val;
518 type = nvi->def[0]->reg.type;
521 u0.u32 = src0->reg.imm.u32;
522 u1.u32 = src1->reg.imm.u32;
524 modifiers_apply(&u0.u32, type, nvi->src[0]->mod);
525 modifiers_apply(&u1.u32, type, nvi->src[1]->mod);
527 switch (nvi->opcode) {
529 if (nvi->src[2]->value->reg.file != NV_FILE_GPR)
534 case NV_TYPE_F32: u.f32 = u0.f32 * u1.f32; break;
535 case NV_TYPE_U32: u.u32 = u0.u32 * u1.u32; break;
536 case NV_TYPE_S32: u.s32 = u0.s32 * u1.s32; break;
544 case NV_TYPE_F32: u.f32 = u0.f32 + u1.f32; break;
545 case NV_TYPE_U32: u.u32 = u0.u32 + u1.u32; break;
546 case NV_TYPE_S32: u.s32 = u0.s32 + u1.s32; break;
554 case NV_TYPE_F32: u.f32 = u0.f32 - u1.f32; break;
555 case NV_TYPE_U32: u.u32 = u0.u32 - u1.u32; break;
556 case NV_TYPE_S32: u.s32 = u0.s32 - u1.s32; break;
566 nvi->opcode = NV_OP_MOV;
568 val = new_value(pc, NV_FILE_IMM, type);
570 val->reg.imm.u32 = u.u32;
572 nv_reference(pc, &nvi->src[1], NULL);
573 nv_reference(pc, &nvi->src[0], val);
575 if (nvi->src[2]) { /* from MAD */
576 nvi->src[1] = nvi->src[0];
577 nvi->src[0] = nvi->src[2];
579 nvi->opcode = NV_OP_ADD;
581 if (val->reg.imm.u32 == 0) {
583 nvi->opcode = NV_OP_MOV;
589 constant_operand(struct nv_pc *pc,
590 struct nv_instruction *nvi, struct nv_value *val, int s)
603 type = nvi->def[0]->reg.type;
605 u.u32 = val->reg.imm.u32;
606 modifiers_apply(&u.u32, type, nvi->src[s]->mod);
608 switch (nvi->opcode) {
610 if ((type == NV_TYPE_F32 && u.f32 == 1.0f) ||
611 (NV_TYPE_ISINT(type) && u.u32 == 1)) {
612 if ((op = modifiers_opcode(nvi->src[t]->mod)) == NV_OP_NOP)
615 nv_reference(pc, &nvi->src[s], NULL);
616 nvi->src[0] = nvi->src[t];
619 if ((type == NV_TYPE_F32 && u.f32 == 2.0f) ||
620 (NV_TYPE_ISINT(type) && u.u32 == 2)) {
621 nvi->opcode = NV_OP_ADD;
622 nv_reference(pc, &nvi->src[s], nvi->src[t]->value);
623 nvi->src[s]->mod = nvi->src[t]->mod;
625 if (type == NV_TYPE_F32 && u.f32 == -1.0f) {
626 if (nvi->src[t]->mod & NV_MOD_NEG)
627 nvi->opcode = NV_OP_MOV;
629 nvi->opcode = NV_OP_NEG;
630 nv_reference(pc, &nvi->src[s], NULL);
631 nvi->src[0] = nvi->src[t];
634 if (type == NV_TYPE_F32 && u.f32 == -2.0f) {
635 nvi->opcode = NV_OP_ADD;
636 nv_reference(pc, &nvi->src[s], nvi->src[t]->value);
637 nvi->src[s]->mod = (nvi->src[t]->mod ^= NV_MOD_NEG);
640 nvi->opcode = NV_OP_MOV;
641 nv_reference(pc, &nvi->src[t], NULL);
643 nvi->src[0] = nvi->src[1];
650 if ((op = modifiers_opcode(nvi->src[t]->mod)) == NV_OP_NOP)
653 nv_reference(pc, &nvi->src[s], NULL);
654 nvi->src[0] = nvi->src[t];
659 u.f32 = 1.0f / u.f32;
660 (val = new_value(pc, NV_FILE_IMM, NV_TYPE_F32))->reg.imm.f32 = u.f32;
661 nvi->opcode = NV_OP_MOV;
663 nv_reference(pc, &nvi->src[0], val);
666 u.f32 = 1.0f / sqrtf(u.f32);
667 (val = new_value(pc, NV_FILE_IMM, NV_TYPE_F32))->reg.imm.f32 = u.f32;
668 nvi->opcode = NV_OP_MOV;
670 nv_reference(pc, &nvi->src[0], val);
676 if (nvi->opcode == NV_OP_MOV && nvi->flags_def) {
677 struct nv_instruction *cvt = new_instruction_at(pc, nvi, NV_OP_CVT);
679 nv_reference(pc, &cvt->src[0], nvi->def[0]);
681 cvt->flags_def = nvi->flags_def;
682 nvi->flags_def = NULL;
687 nv_pass_lower_arith(struct nv_pass *ctx, struct nv_basic_block *b)
689 struct nv_instruction *nvi, *next;
692 for (nvi = b->entry; nvi; nvi = next) {
693 struct nv_value *src0, *src1, *src;
698 src0 = nvcg_find_immediate(nvi->src[0]);
699 src1 = nvcg_find_immediate(nvi->src[1]);
702 constant_expression(ctx->pc, nvi, src0, src1);
705 constant_operand(ctx->pc, nvi, src0, 0);
708 constant_operand(ctx->pc, nvi, src1, 1);
711 /* try to combine MUL, ADD into MAD */
712 if (nvi->opcode != NV_OP_ADD)
715 src0 = nvi->src[0]->value;
716 src1 = nvi->src[1]->value;
718 if (SRC_IS_MUL(src0) && src0->refc == 1)
721 if (SRC_IS_MUL(src1) && src1->refc == 1)
726 /* could have an immediate from above constant_* */
727 if (src0->reg.file != NV_FILE_GPR || src1->reg.file != NV_FILE_GPR)
730 nvi->opcode = NV_OP_MAD;
731 mod = nvi->src[(src == src0) ? 0 : 1]->mod;
732 nv_reference(ctx->pc, &nvi->src[(src == src0) ? 0 : 1], NULL);
733 nvi->src[2] = nvi->src[(src == src0) ? 1 : 0];
735 assert(!(mod & ~NV_MOD_NEG));
736 nvi->src[0] = new_ref(ctx->pc, src->insn->src[0]->value);
737 nvi->src[1] = new_ref(ctx->pc, src->insn->src[1]->value);
738 nvi->src[0]->mod = src->insn->src[0]->mod ^ mod;
739 nvi->src[1]->mod = src->insn->src[1]->mod;
741 DESCEND_ARBITRARY(j, nv_pass_lower_arith);
746 /* TODO: redundant store elimination */
749 struct load_record *next;
751 struct nv_value *value;
754 #define LOAD_RECORD_POOL_SIZE 1024
756 struct nv_pass_reld_elim {
759 struct load_record *imm;
760 struct load_record *mem_s;
761 struct load_record *mem_v;
762 struct load_record *mem_c[16];
763 struct load_record *mem_l;
765 struct load_record pool[LOAD_RECORD_POOL_SIZE];
769 /* TODO: properly handle loads from l[] memory in the presence of stores */
771 nv_pass_reload_elim(struct nv_pass_reld_elim *ctx, struct nv_basic_block *b)
773 struct load_record **rec, *it;
774 struct nv_instruction *ld, *next;
776 struct nv_value *val;
779 for (ld = b->entry; ld; ld = next) {
783 val = ld->src[0]->value;
786 if (ld->opcode == NV_OP_LINTERP || ld->opcode == NV_OP_PINTERP) {
787 data[0] = val->reg.id;
791 if (ld->opcode == NV_OP_LDA) {
792 data[0] = val->reg.id;
793 data[1] = ld->src[4] ? ld->src[4]->value->n : ~0ULL;
794 if (val->reg.file >= NV_FILE_MEM_C(0) &&
795 val->reg.file <= NV_FILE_MEM_C(15))
796 rec = &ctx->mem_c[val->reg.file - NV_FILE_MEM_C(0)];
798 if (val->reg.file == NV_FILE_MEM_S)
801 if (val->reg.file == NV_FILE_MEM_L)
804 if ((ld->opcode == NV_OP_MOV) && (val->reg.file == NV_FILE_IMM)) {
805 data[0] = val->reg.imm.u32;
810 if (!rec || !ld->def[0]->refc)
813 for (it = *rec; it; it = it->next)
814 if (it->data[0] == data[0] && it->data[1] == data[1])
818 if (ld->def[0]->reg.id >= 0)
819 it->value = ld->def[0];
822 nvcg_replace_value(ctx->pc, ld->def[0], it->value);
824 if (ctx->alloc == LOAD_RECORD_POOL_SIZE)
826 it = &ctx->pool[ctx->alloc++];
828 it->data[0] = data[0];
829 it->data[1] = data[1];
830 it->value = ld->def[0];
838 for (j = 0; j < 16; ++j)
839 ctx->mem_c[j] = NULL;
843 DESCEND_ARBITRARY(j, nv_pass_reload_elim);
849 nv_pass_tex_mask(struct nv_pass *ctx, struct nv_basic_block *b)
853 for (i = 0; i < ctx->pc->num_instructions; ++i) {
854 struct nv_instruction *nvi = &ctx->pc->instructions[i];
855 struct nv_value *def[4];
857 if (!nv_is_vector_op(nvi->opcode))
861 for (c = 0; c < 4; ++c) {
862 if (nvi->def[c]->refc)
863 nvi->tex_mask |= 1 << c;
864 def[c] = nvi->def[c];
868 for (c = 0; c < 4; ++c)
869 if (nvi->tex_mask & (1 << c))
870 nvi->def[j++] = def[c];
871 for (c = 0; c < 4; ++c)
872 if (!(nvi->tex_mask & (1 << c)))
873 nvi->def[j++] = def[c];
885 nv_pass_dce(struct nv_pass_dce *ctx, struct nv_basic_block *b)
888 struct nv_instruction *nvi, *next;
890 for (nvi = b->phi ? b->phi : b->entry; nvi; nvi = next) {
893 if (inst_cullable(nvi)) {
899 DESCEND_ARBITRARY(j, nv_pass_dce);
904 /* Register allocation inserted ELSE blocks for all IF/ENDIF without ELSE.
905 * Returns TRUE if @bb initiates an IF/ELSE/ENDIF clause, or is an IF with
906 * BREAK and dummy ELSE block.
908 static INLINE boolean
909 bb_is_if_else_endif(struct nv_basic_block *bb)
911 if (!bb->out[0] || !bb->out[1])
914 if (bb->out[0]->out_kind[0] == CFG_EDGE_LOOP_LEAVE) {
915 return (bb->out[0]->out[1] == bb->out[1]->out[0] &&
916 !bb->out[1]->out[1]);
918 return (bb->out[0]->out[0] == bb->out[1]->out[0] &&
919 !bb->out[0]->out[1] &&
920 !bb->out[1]->out[1]);
924 /* predicate instructions and remove branch at the end */
926 predicate_instructions(struct nv_pc *pc, struct nv_basic_block *b,
927 struct nv_value *p, ubyte cc)
929 struct nv_instruction *nvi;
933 for (nvi = b->entry; nvi->next; nvi = nvi->next) {
934 if (!nvi_isnop(nvi)) {
936 nv_reference(pc, &nvi->flags_src, p);
940 if (nvi->opcode == NV_OP_BRA)
943 if (!nvi_isnop(nvi)) {
945 nv_reference(pc, &nvi->flags_src, p);
949 /* NOTE: Run this after register allocation, we can just cut out the cflow
950 * instructions and hook the predicates to the conditional OPs if they are
951 * not using immediates; better than inserting SELECT to join definitions.
953 * NOTE: Should adapt prior optimization to make this possible more often.
956 nv_pass_flatten(struct nv_pass *ctx, struct nv_basic_block *b)
958 struct nv_instruction *nvi;
959 struct nv_value *pred;
963 if (bb_is_if_else_endif(b)) {
965 NV50_DBGMSG("pass_flatten: IF/ELSE/ENDIF construct at BB:%i\n", b->id);
967 for (n0 = 0, nvi = b->out[0]->entry; nvi; nvi = nvi->next, ++n0)
968 if (!nv50_nvi_can_predicate(nvi))
971 for (n1 = 0, nvi = b->out[1]->entry; nvi; nvi = nvi->next, ++n1)
972 if (!nv50_nvi_can_predicate(nvi))
976 debug_printf("cannot predicate: "); nv_print_instruction(nvi);
979 debug_printf("cannot predicate: "); nv_print_instruction(nvi);
983 if (!nvi && n0 < 12 && n1 < 12) { /* 12 as arbitrary limit */
984 assert(b->exit && b->exit->flags_src);
985 pred = b->exit->flags_src->value;
987 predicate_instructions(ctx->pc, b->out[0], pred, NV_CC_NE | NV_CC_U);
988 predicate_instructions(ctx->pc, b->out[1], pred, NV_CC_EQ);
990 assert(b->exit && b->exit->opcode == NV_OP_BRA);
991 nv_nvi_delete(b->exit);
993 if (b->exit && b->exit->opcode == NV_OP_JOINAT)
994 nv_nvi_delete(b->exit);
996 i = (b->out[0]->out_kind[0] == CFG_EDGE_LOOP_LEAVE) ? 1 : 0;
998 if ((nvi = b->out[0]->out[i]->entry)) {
1000 if (nvi->opcode == NV_OP_JOIN)
1005 DESCEND_ARBITRARY(i, nv_pass_flatten);
1010 /* local common subexpression elimination, stupid O(n^2) implementation */
1012 nv_pass_cse(struct nv_pass *ctx, struct nv_basic_block *b)
1014 struct nv_instruction *ir, *ik, *next;
1015 struct nv_instruction *entry = b->phi ? b->phi : b->entry;
1021 for (ir = entry; ir; ir = next) {
1023 for (ik = entry; ik != ir; ik = ik->next) {
1024 if (ir->opcode != ik->opcode || ir->fixed)
1027 if (!ir->def[0] || !ik->def[0] ||
1028 ik->opcode == NV_OP_LDA ||
1029 ik->opcode == NV_OP_STA ||
1030 ik->opcode == NV_OP_MOV ||
1031 nv_is_vector_op(ik->opcode))
1032 continue; /* ignore loads, stores & moves */
1034 if (ik->src[4] || ir->src[4])
1035 continue; /* don't mess with address registers */
1037 if (ik->flags_src || ir->flags_src ||
1038 ik->flags_def || ir->flags_def)
1039 continue; /* and also not with flags, for now */
1041 if (ik->def[0]->reg.file == NV_FILE_OUT ||
1042 ir->def[0]->reg.file == NV_FILE_OUT ||
1043 !values_equal(ik->def[0], ir->def[0]))
1046 for (s = 0; s < 3; ++s) {
1047 struct nv_value *a, *b;
1054 if (ik->src[s]->mod != ir->src[s]->mod)
1056 a = ik->src[s]->value;
1057 b = ir->src[s]->value;
1060 if (a->reg.file != b->reg.file ||
1062 a->reg.id != b->reg.id)
1068 nvcg_replace_value(ctx->pc, ir->def[0], ik->def[0]);
1075 DESCEND_ARBITRARY(s, nv_pass_cse);
1081 nv_pc_pass0(struct nv_pc *pc, struct nv_basic_block *root)
1083 struct nv_pass_reld_elim *reldelim;
1084 struct nv_pass pass;
1085 struct nv_pass_dce dce;
1091 /* Do this first, so we don't have to pay attention
1092 * to whether sources are supported memory loads.
1095 ret = nv_pass_lower_arith(&pass, root);
1100 ret = nv_pass_lower_mods(&pass, root);
1105 ret = nv_pass_fold_loads(&pass, root);
1110 ret = nv_pass_fold_stores(&pass, root);
1114 if (pc->opt_reload_elim) {
1115 reldelim = CALLOC_STRUCT(nv_pass_reld_elim);
1118 ret = nv_pass_reload_elim(reldelim, root);
1125 ret = nv_pass_cse(&pass, root);
1133 ret = nv_pass_dce(&dce, root);
1136 } while (dce.removed);
1138 ret = nv_pass_tex_mask(&pass, root);
1146 nv_pc_exec_pass0(struct nv_pc *pc)
1150 for (i = 0; i < pc->num_subroutines + 1; ++i)
1151 if (pc->root[i] && (ret = nv_pc_pass0(pc, pc->root[i])))