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
25 #define NOUVEAU_DEBUG 1
27 #include "pipe/p_shader_tokens.h"
28 #include "tgsi/tgsi_parse.h"
29 #include "tgsi/tgsi_util.h"
30 #include "tgsi/tgsi_dump.h"
31 #include "util/u_dynarray.h"
34 #include "nvc0_program.h"
36 /* Arbitrary internal limits. */
37 #define BLD_MAX_TEMPS 64
38 #define BLD_MAX_ADDRS 4
39 #define BLD_MAX_PREDS 4
40 #define BLD_MAX_IMMDS 128
41 #define BLD_MAX_OUTPS PIPE_MAX_SHADER_OUTPUTS
43 #define BLD_MAX_COND_NESTING 8
44 #define BLD_MAX_LOOP_NESTING 4
45 #define BLD_MAX_CALL_NESTING 2
47 /* This structure represents a TGSI register. */
49 struct nv_value
*current
;
50 /* collect all SSA values assigned to it */
51 struct util_dynarray vals
;
52 /* 1 bit per loop level, indicates if used/defd, reset when loop ends */
57 static INLINE
struct nv_value
**
58 bld_register_access(struct bld_register
*reg
, unsigned i
)
60 return util_dynarray_element(®
->vals
, struct nv_value
*, i
);
64 bld_register_add_val(struct bld_register
*reg
, struct nv_value
*val
)
66 util_dynarray_append(®
->vals
, struct nv_value
*, val
);
70 bld_register_del_val(struct bld_register
*reg
, struct nv_value
*val
)
74 for (i
= reg
->vals
.size
/ sizeof(struct nv_value
*); i
> 0; --i
)
75 if (*bld_register_access(reg
, i
- 1) == val
)
80 if (i
!= reg
->vals
.size
/ sizeof(struct nv_value
*))
81 *bld_register_access(reg
, i
- 1) = util_dynarray_pop(®
->vals
,
84 reg
->vals
.size
-= sizeof(struct nv_value
*);
90 struct nvc0_translation_info
*ti
;
93 struct nv_basic_block
*b
;
95 struct tgsi_parse_context parse
[BLD_MAX_CALL_NESTING
];
98 struct nv_basic_block
*cond_bb
[BLD_MAX_COND_NESTING
];
99 struct nv_basic_block
*join_bb
[BLD_MAX_COND_NESTING
];
100 struct nv_basic_block
*else_bb
[BLD_MAX_COND_NESTING
];
102 struct nv_basic_block
*loop_bb
[BLD_MAX_LOOP_NESTING
];
103 struct nv_basic_block
*brkt_bb
[BLD_MAX_LOOP_NESTING
];
106 ubyte out_kind
; /* CFG_EDGE_FORWARD, or FAKE in case of BREAK/CONT */
108 struct bld_register tvs
[BLD_MAX_TEMPS
][4]; /* TGSI_FILE_TEMPORARY */
109 struct bld_register avs
[BLD_MAX_ADDRS
][4]; /* TGSI_FILE_ADDRESS */
110 struct bld_register pvs
[BLD_MAX_PREDS
][4]; /* TGSI_FILE_PREDICATE */
111 struct bld_register ovs
[BLD_MAX_OUTPS
][4]; /* TGSI_FILE_OUTPUT, FP only */
113 uint32_t outputs_written
[(PIPE_MAX_SHADER_OUTPUTS
+ 7) / 8];
115 struct nv_value
*zero
;
116 struct nv_value
*frag_coord
[4];
119 struct nv_value
*saved_sysvals
[4];
120 struct nv_value
*saved_addr
[4][2];
121 struct nv_value
*saved_inputs
[PIPE_MAX_SHADER_INPUTS
][4];
122 struct nv_value
*saved_immd
[BLD_MAX_IMMDS
];
127 bld_register_file(struct bld_context
*bld
, struct bld_register
*reg
)
129 if (reg
< &bld
->avs
[0][0]) return NV_FILE_GPR
;
131 if (reg
< &bld
->pvs
[0][0]) return NV_FILE_GPR
;
133 if (reg
< &bld
->ovs
[0][0]) return NV_FILE_PRED
;
135 return NV_FILE_MEM_V
;
138 static INLINE
struct nv_value
*
139 bld_fetch(struct bld_context
*bld
, struct bld_register
*regs
, int i
, int c
)
141 regs
[i
* 4 + c
].loop_use
|= 1 << bld
->loop_lvl
;
142 return regs
[i
* 4 + c
].current
;
145 static struct nv_value
*
146 bld_loop_phi(struct bld_context
*, struct bld_register
*, struct nv_value
*);
148 /* If a variable is defined in a loop without prior use, we don't need
149 * a phi in the loop header to account for backwards flow.
151 * However, if this variable is then also used outside the loop, we do
152 * need a phi after all. But we must not use this phi's def inside the
153 * loop, so we can eliminate the phi if it is unused later.
156 bld_store(struct bld_context
*bld
,
157 struct bld_register
*regs
, int i
, int c
, struct nv_value
*val
)
159 const uint16_t m
= 1 << bld
->loop_lvl
;
160 struct bld_register
*reg
= ®s
[i
* 4 + c
];
162 if (bld
->loop_lvl
&& !(m
& (reg
->loop_def
| reg
->loop_use
)))
163 bld_loop_phi(bld
, reg
, val
);
166 bld_register_add_val(reg
, reg
->current
);
168 reg
->loop_def
|= 1 << bld
->loop_lvl
;
171 #define FETCH_TEMP(i, c) bld_fetch(bld, &bld->tvs[0][0], i, c)
172 #define STORE_TEMP(i, c, v) bld_store(bld, &bld->tvs[0][0], i, c, (v))
173 #define FETCH_ADDR(i, c) bld_fetch(bld, &bld->avs[0][0], i, c)
174 #define STORE_ADDR(i, c, v) bld_store(bld, &bld->avs[0][0], i, c, (v))
175 #define FETCH_PRED(i, c) bld_fetch(bld, &bld->pvs[0][0], i, c)
176 #define STORE_PRED(i, c, v) bld_store(bld, &bld->pvs[0][0], i, c, (v))
177 #define STORE_OUTP(i, c, v) \
179 bld_store(bld, &bld->ovs[0][0], i, c, (v)); \
180 bld->outputs_written[(i) / 8] |= 1 << (((i) * 4 + (c)) % 32); \
184 bld_clear_def_use(struct bld_register
*regs
, int n
, int lvl
)
187 const uint16_t mask
= ~(1 << lvl
);
189 for (i
= 0; i
< n
* 4; ++i
) {
190 regs
[i
].loop_def
&= mask
;
191 regs
[i
].loop_use
&= mask
;
196 bld_warn_uninitialized(struct bld_context
*bld
, int kind
,
197 struct bld_register
*reg
, struct nv_basic_block
*b
)
200 long i
= (reg
- &bld
->tvs
[0][0]) / 4;
201 long c
= (reg
- &bld
->tvs
[0][0]) & 3;
205 debug_printf("WARNING: TEMP[%li].%c %s used uninitialized in BB:%i\n",
206 i
, (int)('x' + c
), kind
? "may be" : "is", b
->id
);
210 static INLINE
struct nv_value
*
211 bld_def(struct nv_instruction
*i
, int c
, struct nv_value
*value
)
218 static INLINE
struct nv_value
*
219 find_by_bb(struct bld_register
*reg
, struct nv_basic_block
*b
)
223 if (reg
->current
&& reg
->current
->insn
->bb
== b
)
226 for (i
= 0; i
< reg
->vals
.size
/ sizeof(struct nv_value
*); ++i
)
227 if ((*bld_register_access(reg
, i
))->insn
->bb
== b
)
228 return *bld_register_access(reg
, i
);
232 /* Fetch value from register that was defined in the specified BB,
233 * or search for first definitions in all of its predecessors.
236 fetch_by_bb(struct bld_register
*reg
,
237 struct nv_value
**vals
, int *n
,
238 struct nv_basic_block
*b
)
241 struct nv_value
*val
;
243 assert(*n
< 16); /* MAX_COND_NESTING */
245 val
= find_by_bb(reg
, b
);
247 for (i
= 0; i
< *n
; ++i
)
253 for (i
= 0; i
< b
->num_in
; ++i
)
254 if (!IS_WALL_EDGE(b
->in_kind
[i
]))
255 fetch_by_bb(reg
, vals
, n
, b
->in
[i
]);
258 static INLINE
struct nv_value
*
259 bld_load_imm_u32(struct bld_context
*bld
, uint32_t u
);
261 static INLINE
struct nv_value
*
262 bld_undef(struct bld_context
*bld
, ubyte file
)
264 struct nv_instruction
*nvi
= new_instruction(bld
->pc
, NV_OP_UNDEF
);
266 return bld_def(nvi
, 0, new_value(bld
->pc
, file
, 4));
269 static struct nv_value
*
270 bld_phi(struct bld_context
*bld
, struct nv_basic_block
*b
,
271 struct bld_register
*reg
)
273 struct nv_basic_block
*in
;
274 struct nv_value
*vals
[16] = { NULL
};
275 struct nv_value
*val
;
276 struct nv_instruction
*phi
;
281 fetch_by_bb(reg
, vals
, &n
, b
);
284 bld_warn_uninitialized(bld
, 0, reg
, b
);
289 if (nvc0_bblock_dominated_by(b
, vals
[0]->insn
->bb
))
292 bld_warn_uninitialized(bld
, 1, reg
, b
);
294 /* back-tracking to insert missing value of other path */
297 if (in
->num_in
== 1) {
300 if (!nvc0_bblock_reachable_by(in
->in
[0], vals
[0]->insn
->bb
, b
))
303 if (!nvc0_bblock_reachable_by(in
->in
[1], vals
[0]->insn
->bb
, b
))
309 bld
->pc
->current_block
= in
;
311 /* should make this a no-op */
312 bld_register_add_val(reg
, bld_undef(bld
, vals
[0]->reg
.file
));
316 for (i
= 0; i
< n
; ++i
) {
317 /* if value dominates b, continue to the redefinitions */
318 if (nvc0_bblock_dominated_by(b
, vals
[i
]->insn
->bb
))
321 /* if value dominates any in-block, b should be the dom frontier */
322 for (j
= 0; j
< b
->num_in
; ++j
)
323 if (nvc0_bblock_dominated_by(b
->in
[j
], vals
[i
]->insn
->bb
))
325 /* otherwise, find the dominance frontier and put the phi there */
326 if (j
== b
->num_in
) {
327 in
= nvc0_bblock_dom_frontier(vals
[i
]->insn
->bb
);
328 val
= bld_phi(bld
, in
, reg
);
329 bld_register_add_val(reg
, val
);
335 bld
->pc
->current_block
= b
;
340 phi
= new_instruction(bld
->pc
, NV_OP_PHI
);
342 bld_def(phi
, 0, new_value(bld
->pc
, vals
[0]->reg
.file
, vals
[0]->reg
.size
));
343 for (i
= 0; i
< n
; ++i
)
344 nv_reference(bld
->pc
, phi
, i
, vals
[i
]);
349 /* Insert a phi function in the loop header.
350 * For nested loops, we need to insert phi functions in all the outer
351 * loop headers if they don't have one yet.
353 * @def: redefinition from inside loop, or NULL if to be replaced later
355 static struct nv_value
*
356 bld_loop_phi(struct bld_context
*bld
, struct bld_register
*reg
,
357 struct nv_value
*def
)
359 struct nv_instruction
*phi
;
360 struct nv_basic_block
*bb
= bld
->pc
->current_block
;
361 struct nv_value
*val
= NULL
;
363 if (bld
->loop_lvl
> 1) {
365 if (!((reg
->loop_def
| reg
->loop_use
) & (1 << bld
->loop_lvl
)))
366 val
= bld_loop_phi(bld
, reg
, NULL
);
371 val
= bld_phi(bld
, bld
->pc
->current_block
, reg
); /* old definition */
373 bld
->pc
->current_block
= bld
->loop_bb
[bld
->loop_lvl
- 1]->in
[0];
374 val
= bld_undef(bld
, bld_register_file(bld
, reg
));
377 bld
->pc
->current_block
= bld
->loop_bb
[bld
->loop_lvl
- 1];
379 phi
= new_instruction(bld
->pc
, NV_OP_PHI
);
381 bld_def(phi
, 0, new_value_like(bld
->pc
, val
));
385 bld_register_add_val(reg
, phi
->def
[0]);
387 phi
->target
= (struct nv_basic_block
*)reg
; /* cheat */
389 nv_reference(bld
->pc
, phi
, 0, val
);
390 nv_reference(bld
->pc
, phi
, 1, def
);
392 bld
->pc
->current_block
= bb
;
397 static INLINE
struct nv_value
*
398 bld_fetch_global(struct bld_context
*bld
, struct bld_register
*reg
)
400 const uint16_t m
= 1 << bld
->loop_lvl
;
401 const uint16_t use
= reg
->loop_use
;
405 /* If neither used nor def'd inside the loop, build a phi in foresight,
406 * so we don't have to replace stuff later on, which requires tracking.
408 if (bld
->loop_lvl
&& !((use
| reg
->loop_def
) & m
))
409 return bld_loop_phi(bld
, reg
, NULL
);
411 return bld_phi(bld
, bld
->pc
->current_block
, reg
);
414 static INLINE
struct nv_value
*
415 bld_imm_u32(struct bld_context
*bld
, uint32_t u
)
418 unsigned n
= bld
->num_immds
;
420 for (i
= 0; i
< n
; ++i
)
421 if (bld
->saved_immd
[i
]->reg
.imm
.u32
== u
)
422 return bld
->saved_immd
[i
];
424 assert(n
< BLD_MAX_IMMDS
);
427 bld
->saved_immd
[n
] = new_value(bld
->pc
, NV_FILE_IMM
, 4);
428 bld
->saved_immd
[n
]->reg
.imm
.u32
= u
;
429 return bld
->saved_immd
[n
];
433 bld_replace_value(struct nv_pc
*, struct nv_basic_block
*, struct nv_value
*,
436 /* Replace the source of the phi in the loop header by the last assignment,
437 * or eliminate the phi function if there is no assignment inside the loop.
439 * Redundancy situation 1 - (used) but (not redefined) value:
440 * %3 = phi %0, %3 = %3 is used
441 * %3 = phi %0, %4 = is new definition
443 * Redundancy situation 2 - (not used) but (redefined) value:
444 * %3 = phi %0, %2 = %2 is used, %3 could be used outside, deleted by DCE
447 bld_loop_end(struct bld_context
*bld
, struct nv_basic_block
*bb
)
449 struct nv_basic_block
*save
= bld
->pc
->current_block
;
450 struct nv_instruction
*phi
, *next
;
451 struct nv_value
*val
;
452 struct bld_register
*reg
;
455 for (phi
= bb
->phi
; phi
&& phi
->opcode
== NV_OP_PHI
; phi
= next
) {
458 reg
= (struct bld_register
*)phi
->target
;
461 for (s
= 1, n
= 0; n
< bb
->num_in
; ++n
) {
462 if (bb
->in_kind
[n
] != CFG_EDGE_BACK
)
466 bld
->pc
->current_block
= bb
->in
[n
];
467 val
= bld_fetch_global(bld
, reg
);
469 for (i
= 0; i
< 4; ++i
)
470 if (phi
->src
[i
] && phi
->src
[i
]->value
== val
)
473 nv_reference(bld
->pc
, phi
, s
++, val
);
475 bld
->pc
->current_block
= save
;
477 if (phi
->src
[0]->value
== phi
->def
[0] ||
478 phi
->src
[0]->value
== phi
->src
[1]->value
)
481 if (phi
->src
[1]->value
== phi
->def
[0])
487 /* eliminate the phi */
488 bld_register_del_val(reg
, phi
->def
[0]);
491 bld_replace_value(bld
->pc
, bb
, phi
->def
[0], phi
->src
[s
]->value
);
493 nvc0_insn_delete(phi
);
498 static INLINE
struct nv_value
*
499 bld_imm_f32(struct bld_context
*bld
, float f
)
501 return bld_imm_u32(bld
, fui(f
));
504 static struct nv_value
*
505 bld_insn_1(struct bld_context
*bld
, uint opcode
, struct nv_value
*src0
)
507 struct nv_instruction
*insn
= new_instruction(bld
->pc
, opcode
);
509 nv_reference(bld
->pc
, insn
, 0, src0
);
511 return bld_def(insn
, 0, new_value(bld
->pc
, NV_FILE_GPR
, src0
->reg
.size
));
514 static struct nv_value
*
515 bld_insn_2(struct bld_context
*bld
, uint opcode
,
516 struct nv_value
*src0
, struct nv_value
*src1
)
518 struct nv_instruction
*insn
= new_instruction(bld
->pc
, opcode
);
520 nv_reference(bld
->pc
, insn
, 0, src0
);
521 nv_reference(bld
->pc
, insn
, 1, src1
);
523 return bld_def(insn
, 0, new_value(bld
->pc
, NV_FILE_GPR
, src0
->reg
.size
));
526 static struct nv_value
*
527 bld_insn_3(struct bld_context
*bld
, uint opcode
,
528 struct nv_value
*src0
, struct nv_value
*src1
,
529 struct nv_value
*src2
)
531 struct nv_instruction
*insn
= new_instruction(bld
->pc
, opcode
);
533 nv_reference(bld
->pc
, insn
, 0, src0
);
534 nv_reference(bld
->pc
, insn
, 1, src1
);
535 nv_reference(bld
->pc
, insn
, 2, src2
);
537 return bld_def(insn
, 0, new_value(bld
->pc
, NV_FILE_GPR
, src0
->reg
.size
));
541 bld_src_predicate(struct bld_context
*bld
,
542 struct nv_instruction
*nvi
, int s
, struct nv_value
*val
)
545 nv_reference(bld
->pc
, nvi
, s
, val
);
549 bld_src_pointer(struct bld_context
*bld
,
550 struct nv_instruction
*nvi
, int s
, struct nv_value
*val
)
553 nv_reference(bld
->pc
, nvi
, s
, val
);
557 bld_lmem_store(struct bld_context
*bld
, struct nv_value
*ptr
, int ofst
,
558 struct nv_value
*val
)
560 struct nv_instruction
*insn
= new_instruction(bld
->pc
, NV_OP_ST
);
561 struct nv_value
*loc
;
563 loc
= new_value(bld
->pc
, NV_FILE_MEM_L
, nv_type_sizeof(NV_TYPE_U32
));
565 loc
->reg
.id
= ofst
* 4;
567 nv_reference(bld
->pc
, insn
, 0, loc
);
568 nv_reference(bld
->pc
, insn
, 1, ptr
);
569 nv_reference(bld
->pc
, insn
, 2, val
);
572 static struct nv_value
*
573 bld_lmem_load(struct bld_context
*bld
, struct nv_value
*ptr
, int ofst
)
575 struct nv_value
*loc
, *val
;
577 loc
= new_value(bld
->pc
, NV_FILE_MEM_L
, nv_type_sizeof(NV_TYPE_U32
));
579 loc
->reg
.address
= ofst
* 4;
581 val
= bld_insn_2(bld
, NV_OP_LD
, loc
, ptr
);
586 static struct nv_value
*
587 bld_pow(struct bld_context
*bld
, struct nv_value
*x
, struct nv_value
*e
)
589 struct nv_value
*val
;
591 val
= bld_insn_1(bld
, NV_OP_LG2
, x
);
592 val
= bld_insn_2(bld
, NV_OP_MUL_F32
, e
, val
);
594 val
= bld_insn_1(bld
, NV_OP_PREEX2
, val
);
595 val
= bld_insn_1(bld
, NV_OP_EX2
, val
);
600 static INLINE
struct nv_value
*
601 bld_load_imm_f32(struct bld_context
*bld
, float f
)
605 return bld_insn_1(bld
, NV_OP_MOV
, bld_imm_f32(bld
, f
));
608 static INLINE
struct nv_value
*
609 bld_load_imm_u32(struct bld_context
*bld
, uint32_t u
)
613 return bld_insn_1(bld
, NV_OP_MOV
, bld_imm_u32(bld
, u
));
616 static INLINE
struct nv_value
*
617 bld_setp(struct bld_context
*bld
, uint op
, uint8_t cc
,
618 struct nv_value
*src0
, struct nv_value
*src1
)
620 struct nv_value
*val
= bld_insn_2(bld
, op
, src0
, src1
);
622 val
->reg
.file
= NV_FILE_PRED
;
624 val
->insn
->set_cond
= cc
& 0xf;
628 static INLINE
struct nv_value
*
629 bld_cvt(struct bld_context
*bld
, uint8_t dt
, uint8_t st
, struct nv_value
*src
)
631 struct nv_value
*val
= bld_insn_1(bld
, NV_OP_CVT
, src
);
632 val
->insn
->ext
.cvt
.d
= dt
;
633 val
->insn
->ext
.cvt
.s
= st
;
638 bld_kil(struct bld_context
*bld
, struct nv_value
*src
)
640 struct nv_instruction
*nvi
;
642 src
= bld_setp(bld
, NV_OP_SET_F32
, NV_CC_LT
, src
, bld
->zero
);
644 nvi
= new_instruction(bld
->pc
, NV_OP_KIL
);
647 bld_src_predicate(bld
, nvi
, 0, src
);
651 bld_flow(struct bld_context
*bld
, uint opcode
,
652 struct nv_value
*src
, struct nv_basic_block
*target
,
655 struct nv_instruction
*nvi
;
658 new_instruction(bld
->pc
, NV_OP_JOINAT
)->fixed
= 1;
660 nvi
= new_instruction(bld
->pc
, opcode
);
661 nvi
->target
= target
;
664 bld_src_predicate(bld
, nvi
, 0, src
);
668 translate_setcc(unsigned opcode
)
671 case TGSI_OPCODE_SLT
: return NV_CC_LT
;
672 case TGSI_OPCODE_SGE
: return NV_CC_GE
;
673 case TGSI_OPCODE_SEQ
: return NV_CC_EQ
;
674 case TGSI_OPCODE_SGT
: return NV_CC_GT
;
675 case TGSI_OPCODE_SLE
: return NV_CC_LE
;
676 case TGSI_OPCODE_SNE
: return NV_CC_NE
| NV_CC_U
;
677 case TGSI_OPCODE_STR
: return NV_CC_TR
;
678 case TGSI_OPCODE_SFL
: return NV_CC_FL
;
680 case TGSI_OPCODE_ISLT
: return NV_CC_LT
;
681 case TGSI_OPCODE_ISGE
: return NV_CC_GE
;
682 case TGSI_OPCODE_USEQ
: return NV_CC_EQ
;
683 case TGSI_OPCODE_USGE
: return NV_CC_GE
;
684 case TGSI_OPCODE_USLT
: return NV_CC_LT
;
685 case TGSI_OPCODE_USNE
: return NV_CC_NE
;
693 translate_opcode(uint opcode
)
696 case TGSI_OPCODE_ABS
: return NV_OP_ABS_F32
;
697 case TGSI_OPCODE_ADD
: return NV_OP_ADD_F32
;
698 case TGSI_OPCODE_SUB
: return NV_OP_SUB_F32
;
699 case TGSI_OPCODE_UADD
: return NV_OP_ADD_B32
;
700 case TGSI_OPCODE_AND
: return NV_OP_AND
;
701 case TGSI_OPCODE_EX2
: return NV_OP_EX2
;
702 case TGSI_OPCODE_CEIL
: return NV_OP_CEIL
;
703 case TGSI_OPCODE_FLR
: return NV_OP_FLOOR
;
704 case TGSI_OPCODE_TRUNC
: return NV_OP_TRUNC
;
705 case TGSI_OPCODE_COS
: return NV_OP_COS
;
706 case TGSI_OPCODE_SIN
: return NV_OP_SIN
;
707 case TGSI_OPCODE_DDX
: return NV_OP_DFDX
;
708 case TGSI_OPCODE_DDY
: return NV_OP_DFDY
;
709 case TGSI_OPCODE_F2I
:
710 case TGSI_OPCODE_F2U
:
711 case TGSI_OPCODE_I2F
:
712 case TGSI_OPCODE_U2F
: return NV_OP_CVT
;
713 case TGSI_OPCODE_INEG
: return NV_OP_NEG_S32
;
714 case TGSI_OPCODE_LG2
: return NV_OP_LG2
;
715 case TGSI_OPCODE_ISHR
: return NV_OP_SAR
;
716 case TGSI_OPCODE_USHR
: return NV_OP_SHR
;
717 case TGSI_OPCODE_MAD
: return NV_OP_MAD_F32
;
718 case TGSI_OPCODE_MAX
: return NV_OP_MAX_F32
;
719 case TGSI_OPCODE_IMAX
: return NV_OP_MAX_S32
;
720 case TGSI_OPCODE_UMAX
: return NV_OP_MAX_U32
;
721 case TGSI_OPCODE_MIN
: return NV_OP_MIN_F32
;
722 case TGSI_OPCODE_IMIN
: return NV_OP_MIN_S32
;
723 case TGSI_OPCODE_UMIN
: return NV_OP_MIN_U32
;
724 case TGSI_OPCODE_MUL
: return NV_OP_MUL_F32
;
725 case TGSI_OPCODE_UMUL
: return NV_OP_MUL_B32
;
726 case TGSI_OPCODE_OR
: return NV_OP_OR
;
727 case TGSI_OPCODE_RCP
: return NV_OP_RCP
;
728 case TGSI_OPCODE_RSQ
: return NV_OP_RSQ
;
729 case TGSI_OPCODE_SAD
: return NV_OP_SAD
;
730 case TGSI_OPCODE_SHL
: return NV_OP_SHL
;
731 case TGSI_OPCODE_SLT
:
732 case TGSI_OPCODE_SGE
:
733 case TGSI_OPCODE_SEQ
:
734 case TGSI_OPCODE_SGT
:
735 case TGSI_OPCODE_SLE
:
736 case TGSI_OPCODE_SNE
: return NV_OP_FSET_F32
;
737 case TGSI_OPCODE_ISLT
:
738 case TGSI_OPCODE_ISGE
: return NV_OP_SET_S32
;
739 case TGSI_OPCODE_USEQ
:
740 case TGSI_OPCODE_USGE
:
741 case TGSI_OPCODE_USLT
:
742 case TGSI_OPCODE_USNE
: return NV_OP_SET_U32
;
743 case TGSI_OPCODE_TEX
: return NV_OP_TEX
;
744 case TGSI_OPCODE_TXP
: return NV_OP_TEX
;
745 case TGSI_OPCODE_TXB
: return NV_OP_TXB
;
746 case TGSI_OPCODE_TXL
: return NV_OP_TXL
;
747 case TGSI_OPCODE_XOR
: return NV_OP_XOR
;
755 infer_src_type(unsigned opcode
)
758 case TGSI_OPCODE_MOV
:
759 case TGSI_OPCODE_AND
:
761 case TGSI_OPCODE_XOR
:
762 case TGSI_OPCODE_SAD
:
763 case TGSI_OPCODE_U2F
:
764 case TGSI_OPCODE_UADD
:
765 case TGSI_OPCODE_UDIV
:
766 case TGSI_OPCODE_UMOD
:
767 case TGSI_OPCODE_UMAD
:
768 case TGSI_OPCODE_UMUL
:
769 case TGSI_OPCODE_UMAX
:
770 case TGSI_OPCODE_UMIN
:
771 case TGSI_OPCODE_USEQ
:
772 case TGSI_OPCODE_USGE
:
773 case TGSI_OPCODE_USLT
:
774 case TGSI_OPCODE_USNE
:
775 case TGSI_OPCODE_USHR
:
777 case TGSI_OPCODE_I2F
:
778 case TGSI_OPCODE_IDIV
:
779 case TGSI_OPCODE_IMAX
:
780 case TGSI_OPCODE_IMIN
:
781 case TGSI_OPCODE_INEG
:
782 case TGSI_OPCODE_ISGE
:
783 case TGSI_OPCODE_ISHR
:
784 case TGSI_OPCODE_ISLT
:
792 infer_dst_type(unsigned opcode
)
795 case TGSI_OPCODE_MOV
:
796 case TGSI_OPCODE_F2U
:
797 case TGSI_OPCODE_AND
:
799 case TGSI_OPCODE_XOR
:
800 case TGSI_OPCODE_SAD
:
801 case TGSI_OPCODE_UADD
:
802 case TGSI_OPCODE_UDIV
:
803 case TGSI_OPCODE_UMOD
:
804 case TGSI_OPCODE_UMAD
:
805 case TGSI_OPCODE_UMUL
:
806 case TGSI_OPCODE_UMAX
:
807 case TGSI_OPCODE_UMIN
:
808 case TGSI_OPCODE_USEQ
:
809 case TGSI_OPCODE_USGE
:
810 case TGSI_OPCODE_USLT
:
811 case TGSI_OPCODE_USNE
:
812 case TGSI_OPCODE_USHR
:
814 case TGSI_OPCODE_F2I
:
815 case TGSI_OPCODE_IDIV
:
816 case TGSI_OPCODE_IMAX
:
817 case TGSI_OPCODE_IMIN
:
818 case TGSI_OPCODE_INEG
:
819 case TGSI_OPCODE_ISGE
:
820 case TGSI_OPCODE_ISHR
:
821 case TGSI_OPCODE_ISLT
:
830 emit_store(struct bld_context
*bld
, const struct tgsi_full_instruction
*inst
,
831 unsigned chan
, struct nv_value
*res
)
833 const struct tgsi_full_dst_register
*reg
= &inst
->Dst
[0];
834 struct nv_instruction
*nvi
;
835 struct nv_value
*mem
;
836 struct nv_value
*ptr
= NULL
;
839 idx
= reg
->Register
.Index
;
842 if (reg
->Register
.Indirect
)
843 ptr
= FETCH_ADDR(reg
->Indirect
.Index
,
844 tgsi_util_get_src_register_swizzle(®
->Indirect
, 0));
846 switch (inst
->Instruction
.Saturate
) {
849 case TGSI_SAT_ZERO_ONE
:
850 res
= bld_insn_1(bld
, NV_OP_SAT
, res
);
852 case TGSI_SAT_MINUS_PLUS_ONE
:
853 res
= bld_insn_2(bld
, NV_OP_MAX_F32
, res
, bld_load_imm_f32(bld
, -1.0f
));
854 res
= bld_insn_2(bld
, NV_OP_MIN_F32
, res
, bld_load_imm_f32(bld
, 1.0f
));
858 switch (reg
->Register
.File
) {
859 case TGSI_FILE_OUTPUT
:
861 res
= bld_insn_1(bld
, NV_OP_MOV
, res
);
863 if (bld
->pc
->is_fragprog
) {
865 STORE_OUTP(idx
, chan
, res
);
867 nvi
= new_instruction(bld
->pc
, NV_OP_EXPORT
);
868 mem
= new_value(bld
->pc
, bld
->ti
->output_file
, res
->reg
.size
);
869 nv_reference(bld
->pc
, nvi
, 0, mem
);
870 nv_reference(bld
->pc
, nvi
, 1, res
);
872 mem
->reg
.address
= bld
->ti
->output_loc
[idx
][chan
];
874 mem
->reg
.address
= 0x80 + idx
* 16 + chan
* 4;
878 case TGSI_FILE_TEMPORARY
:
879 assert(idx
< BLD_MAX_TEMPS
);
881 res
= bld_insn_1(bld
, NV_OP_MOV
, res
);
883 assert(res
->reg
.file
== NV_FILE_GPR
);
884 assert(res
->insn
->bb
= bld
->pc
->current_block
);
886 if (bld
->ti
->require_stores
)
887 bld_lmem_store(bld
, ptr
, idx
* 4 + chan
, res
);
889 STORE_TEMP(idx
, chan
, res
);
891 case TGSI_FILE_ADDRESS
:
892 assert(idx
< BLD_MAX_ADDRS
);
893 STORE_ADDR(idx
, chan
, res
);
898 static INLINE
uint32_t
899 bld_is_output_written(struct bld_context
*bld
, int i
, int c
)
902 return bld
->outputs_written
[i
/ 8] & (0xf << ((i
* 4) % 32));
903 return bld
->outputs_written
[i
/ 8] & (1 << ((i
* 4 + c
) % 32));
907 bld_export_fp_outputs(struct bld_context
*bld
)
909 struct nv_value
*vals
[4];
910 struct nv_instruction
*nvi
;
913 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; ++i
) {
914 if (!bld_is_output_written(bld
, i
, -1))
916 for (n
= 0, c
= 0; c
< 4; ++c
) {
917 if (!bld_is_output_written(bld
, i
, c
))
919 vals
[n
] = bld_fetch_global(bld
, &bld
->ovs
[i
][c
]);
921 vals
[n
] = bld_insn_1(bld
, NV_OP_MOV
, vals
[n
]);
922 vals
[n
++]->reg
.id
= bld
->ti
->output_loc
[i
][c
];
926 (nvi
= new_instruction(bld
->pc
, NV_OP_EXPORT
))->fixed
= 1;
927 for (c
= 0; c
< n
; ++c
)
928 nv_reference(bld
->pc
, nvi
, c
, vals
[c
]);
933 bld_new_block(struct bld_context
*bld
, struct nv_basic_block
*b
)
937 bld
->pc
->current_block
= b
;
939 for (i
= 0; i
< 4; ++i
)
940 bld
->saved_addr
[i
][0] = NULL
;
941 for (i
= 0; i
< PIPE_MAX_SHADER_INPUTS
; ++i
)
942 for (c
= 0; c
< 4; ++c
)
943 bld
->saved_inputs
[i
][c
] = NULL
;
945 bld
->out_kind
= CFG_EDGE_FORWARD
;
948 static struct nv_value
*
949 bld_get_saved_input(struct bld_context
*bld
, unsigned i
, unsigned c
)
951 if (bld
->saved_inputs
[i
][c
])
952 return bld
->saved_inputs
[i
][c
];
956 static struct nv_value
*
957 bld_interp(struct bld_context
*bld
, unsigned mode
, struct nv_value
*val
)
959 unsigned cent
= mode
& NVC0_INTERP_CENTROID
;
961 mode
&= ~NVC0_INTERP_CENTROID
;
963 if (val
->reg
.address
== 0x3fc) {
964 /* gl_FrontFacing: 0/~0 to -1.0/+1.0 */
965 val
= bld_insn_1(bld
, NV_OP_LINTERP
, val
);
966 val
= bld_insn_2(bld
, NV_OP_SHL
, val
, bld_imm_u32(bld
, 31));
967 val
= bld_insn_2(bld
, NV_OP_XOR
, val
, bld_imm_f32(bld
, -1.0f
));
969 if (mode
== NVC0_INTERP_PERSPECTIVE
) {
970 val
= bld_insn_2(bld
, NV_OP_PINTERP
, val
, bld
->frag_coord
[3]);
972 val
= bld_insn_1(bld
, NV_OP_LINTERP
, val
);
975 val
->insn
->flat
= mode
== NVC0_INTERP_FLAT
? 1 : 0;
976 val
->insn
->centroid
= cent
? 1 : 0;
980 static struct nv_value
*
981 emit_fetch(struct bld_context
*bld
, const struct tgsi_full_instruction
*insn
,
982 const unsigned s
, const unsigned chan
)
984 const struct tgsi_full_src_register
*src
= &insn
->Src
[s
];
985 struct nv_value
*res
= NULL
;
986 struct nv_value
*ptr
= NULL
;
987 int idx
, ind_idx
, dim_idx
;
988 unsigned swz
, ind_swz
, sgn
;
990 idx
= src
->Register
.Index
;
991 swz
= tgsi_util_get_full_src_register_swizzle(src
, chan
);
993 if (src
->Register
.Indirect
) {
994 ind_idx
= src
->Indirect
.Index
;
995 ind_swz
= tgsi_util_get_src_register_swizzle(&src
->Indirect
, 0);
997 ptr
= FETCH_ADDR(ind_idx
, ind_swz
);
1000 if (src
->Register
.Dimension
)
1001 dim_idx
= src
->Dimension
.Index
;
1005 switch (src
->Register
.File
) {
1006 case TGSI_FILE_CONSTANT
:
1007 assert(dim_idx
< 14);
1008 res
= new_value(bld
->pc
, NV_FILE_MEM_C(dim_idx
), 4);
1009 res
->reg
.address
= idx
* 16 + swz
* 4;
1010 res
= bld_insn_1(bld
, NV_OP_LD
, res
);
1012 bld_src_pointer(bld
, res
->insn
, 1, ptr
);
1014 case TGSI_FILE_IMMEDIATE
: /* XXX: type for MOV TEMP[0], -IMM[0] */
1015 assert(idx
< bld
->ti
->immd32_nr
);
1016 res
= bld_load_imm_u32(bld
, bld
->ti
->immd32
[idx
* 4 + swz
]);
1018 case TGSI_FILE_INPUT
:
1019 assert(!src
->Register
.Dimension
);
1021 res
= bld_get_saved_input(bld
, idx
, swz
);
1025 res
= new_value(bld
->pc
, bld
->ti
->input_file
, 4);
1027 res
->reg
.address
= 0x80 + idx
* 16 + swz
* 4;
1029 res
->reg
.address
= bld
->ti
->input_loc
[idx
][swz
];
1031 if (bld
->pc
->is_fragprog
)
1032 res
= bld_interp(bld
, bld
->ti
->interp_mode
[idx
], res
);
1034 res
= bld_insn_1(bld
, NV_OP_VFETCH
, res
);
1037 bld_src_pointer(bld
, res
->insn
, res
->insn
->src
[1] ? 2 : 1, ptr
);
1039 bld
->saved_inputs
[idx
][swz
] = res
;
1041 case TGSI_FILE_TEMPORARY
:
1042 if (bld
->ti
->require_stores
)
1043 res
= bld_lmem_load(bld
, ptr
, idx
* 4 + swz
);
1045 res
= bld_fetch_global(bld
, &bld
->tvs
[idx
][swz
]);
1047 case TGSI_FILE_ADDRESS
:
1048 res
= bld_fetch_global(bld
, &bld
->avs
[idx
][swz
]);
1050 case TGSI_FILE_PREDICATE
:
1051 res
= bld_fetch_global(bld
, &bld
->pvs
[idx
][swz
]);
1054 NOUVEAU_ERR("illegal/unhandled src reg file: %d\n", src
->Register
.File
);
1059 return bld_undef(bld
, NV_FILE_GPR
);
1061 sgn
= tgsi_util_get_full_src_register_sign_mode(src
, chan
);
1064 case TGSI_UTIL_SIGN_KEEP
:
1066 case TGSI_UTIL_SIGN_CLEAR
:
1067 res
= bld_insn_1(bld
, NV_OP_ABS_F32
, res
);
1069 case TGSI_UTIL_SIGN_TOGGLE
:
1070 res
= bld_insn_1(bld
, NV_OP_NEG_F32
, res
);
1072 case TGSI_UTIL_SIGN_SET
:
1073 res
= bld_insn_1(bld
, NV_OP_ABS_F32
, res
);
1074 res
= bld_insn_1(bld
, NV_OP_NEG_F32
, res
);
1077 NOUVEAU_ERR("illegal/unhandled src reg sign mode\n");
1086 bld_lit(struct bld_context
*bld
, struct nv_value
*dst0
[4],
1087 const struct tgsi_full_instruction
*insn
)
1089 struct nv_value
*val0
= NULL
;
1090 unsigned mask
= insn
->Dst
[0].Register
.WriteMask
;
1092 if (mask
& ((1 << 0) | (1 << 3)))
1093 dst0
[3] = dst0
[0] = bld_load_imm_f32(bld
, 1.0f
);
1095 if (mask
& (3 << 1)) {
1096 val0
= bld_insn_2(bld
, NV_OP_MAX
, emit_fetch(bld
, insn
, 0, 0), bld
->zero
);
1097 if (mask
& (1 << 1))
1101 if (mask
& (1 << 2)) {
1102 struct nv_value
*val1
, *val3
, *src1
, *src3
, *pred
;
1103 struct nv_value
*pos128
= bld_load_imm_f32(bld
, 127.999999f
);
1104 struct nv_value
*neg128
= bld_load_imm_f32(bld
, -127.999999f
);
1106 src1
= emit_fetch(bld
, insn
, 0, 1);
1107 src3
= emit_fetch(bld
, insn
, 0, 3);
1109 pred
= bld_setp(bld
, NV_OP_SET_F32
, NV_CC_LE
, val0
, bld
->zero
);
1111 val1
= bld_insn_2(bld
, NV_OP_MAX_F32
, src1
, bld
->zero
);
1112 val3
= bld_insn_2(bld
, NV_OP_MAX_F32
, src3
, neg128
);
1113 val3
= bld_insn_2(bld
, NV_OP_MIN_F32
, val3
, pos128
);
1114 val3
= bld_pow(bld
, val1
, val3
);
1116 dst0
[2] = bld_insn_1(bld
, NV_OP_MOV
, bld
->zero
);
1117 bld_src_predicate(bld
, dst0
[2]->insn
, 1, pred
);
1119 dst0
[2] = bld_insn_2(bld
, NV_OP_SELECT
, val3
, dst0
[2]);
1124 get_tex_dim(const struct tgsi_full_instruction
*insn
, int *dim
, int *arg
)
1126 switch (insn
->Texture
.Texture
) {
1127 case TGSI_TEXTURE_1D
:
1130 case TGSI_TEXTURE_SHADOW1D
:
1134 case TGSI_TEXTURE_UNKNOWN
:
1135 case TGSI_TEXTURE_2D
:
1136 case TGSI_TEXTURE_RECT
:
1139 case TGSI_TEXTURE_SHADOW2D
:
1140 case TGSI_TEXTURE_SHADOWRECT
:
1144 case TGSI_TEXTURE_3D
:
1145 case TGSI_TEXTURE_CUBE
:
1154 static struct nv_value
*
1155 bld_clone(struct bld_context
*bld
, struct nv_instruction
*nvi
)
1157 struct nv_instruction
*dupi
= new_instruction(bld
->pc
, nvi
->opcode
);
1158 struct nv_instruction
*next
, *prev
;
1169 for (c
= 0; c
< 5 && nvi
->def
[c
]; ++c
)
1170 bld_def(dupi
, c
, new_value_like(bld
->pc
, nvi
->def
[c
]));
1172 for (c
= 0; c
< 6 && nvi
->src
[c
]; ++c
) {
1173 dupi
->src
[c
] = NULL
;
1174 nv_reference(bld
->pc
, dupi
, c
, nvi
->src
[c
]->value
);
1177 return dupi
->def
[0];
1180 /* NOTE: proj(t0) = (t0 / w) / (tc3 / w) = tc0 / tc2 handled by optimizer */
1182 load_proj_tex_coords(struct bld_context
*bld
,
1183 struct nv_value
*t
[4], int dim
, int arg
,
1184 const struct tgsi_full_instruction
*insn
)
1187 unsigned mask
= (1 << dim
) - 1;
1190 mask
|= 4; /* depth comparison value */
1192 t
[3] = emit_fetch(bld
, insn
, 0, 3);
1193 if (t
[3]->insn
->opcode
== NV_OP_PINTERP
) {
1194 t
[3] = bld_clone(bld
, t
[3]->insn
);
1195 t
[3]->insn
->opcode
= NV_OP_LINTERP
;
1196 nv_reference(bld
->pc
, t
[3]->insn
, 1, NULL
);
1198 t
[3] = bld_insn_1(bld
, NV_OP_RCP
, t
[3]);
1200 for (c
= 0; c
< 4; ++c
) {
1201 if (!(mask
& (1 << c
)))
1203 t
[c
] = emit_fetch(bld
, insn
, 0, c
);
1205 if (t
[c
]->insn
->opcode
!= NV_OP_PINTERP
)
1209 t
[c
] = bld_clone(bld
, t
[c
]->insn
);
1210 nv_reference(bld
->pc
, t
[c
]->insn
, 1, t
[3]);
1215 t
[3] = emit_fetch(bld
, insn
, 0, 3);
1216 t
[3] = bld_insn_1(bld
, NV_OP_RCP
, t
[3]);
1218 for (c
= 0; c
< 4; ++c
)
1219 if (mask
& (1 << c
))
1220 t
[c
] = bld_insn_2(bld
, NV_OP_MUL_F32
, t
[c
], t
[3]);
1223 /* For a quad of threads / top left, top right, bottom left, bottom right
1224 * pixels, do a different operation, and take src0 from a specific thread.
1231 #define QOP(a, b, c, d) \
1232 ((QOP_##a << 0) | (QOP_##b << 2) | (QOP_##c << 4) | (QOP_##d << 6))
1234 static INLINE
struct nv_value
*
1235 bld_quadop(struct bld_context
*bld
, ubyte qop
, struct nv_value
*src0
, int lane
,
1236 struct nv_value
*src1
, boolean wp
)
1238 struct nv_value
*val
= bld_insn_2(bld
, NV_OP_QUADOP
, src0
, src1
);
1239 val
->insn
->lanes
= lane
;
1240 val
->insn
->quadop
= qop
;
1242 assert(!"quadop predicate write");
1247 static struct nv_instruction
*
1248 emit_tex(struct bld_context
*bld
, uint opcode
,
1249 struct nv_value
*dst
[4], struct nv_value
*t_in
[4],
1250 int argc
, int tic
, int tsc
, int cube
)
1252 struct nv_value
*t
[4];
1253 struct nv_instruction
*nvi
;
1256 /* the inputs to a tex instruction must be separate values */
1257 for (c
= 0; c
< argc
; ++c
) {
1258 t
[c
] = bld_insn_1(bld
, NV_OP_MOV
, t_in
[c
]);
1259 t
[c
]->insn
->fixed
= 1;
1262 nvi
= new_instruction(bld
->pc
, opcode
);
1263 for (c
= 0; c
< 4; ++c
)
1264 dst
[c
] = bld_def(nvi
, c
, new_value(bld
->pc
, NV_FILE_GPR
, 4));
1265 for (c
= 0; c
< argc
; ++c
)
1266 nv_reference(bld
->pc
, nvi
, c
, t
[c
]);
1268 nvi
->ext
.tex
.t
= tic
;
1269 nvi
->ext
.tex
.s
= tsc
;
1270 nvi
->tex_mask
= 0xf;
1271 nvi
->tex_cube
= cube
;
1273 nvi
->tex_argc
= argc
;
1280 bld_is_constant(struct nv_value *val)
1282 if (val->reg.file == NV_FILE_IMM)
1284 return val->insn && nvCG_find_constant(val->insn->src[0]);
1289 bld_tex(struct bld_context
*bld
, struct nv_value
*dst0
[4],
1290 const struct tgsi_full_instruction
*insn
)
1292 struct nv_value
*t
[4], *s
[3];
1293 uint opcode
= translate_opcode(insn
->Instruction
.Opcode
);
1295 const int tic
= insn
->Src
[1].Register
.Index
;
1296 const int tsc
= tic
;
1297 const int cube
= (insn
->Texture
.Texture
== TGSI_TEXTURE_CUBE
) ? 1 : 0;
1299 get_tex_dim(insn
, &dim
, &arg
);
1301 if (!cube
&& insn
->Instruction
.Opcode
== TGSI_OPCODE_TXP
)
1302 load_proj_tex_coords(bld
, t
, dim
, arg
, insn
);
1304 for (c
= 0; c
< dim
; ++c
)
1305 t
[c
] = emit_fetch(bld
, insn
, 0, c
);
1307 t
[dim
] = emit_fetch(bld
, insn
, 0, 2);
1312 for (c
= 0; c
< 3; ++c
)
1313 s
[c
] = bld_insn_1(bld
, NV_OP_ABS_F32
, t
[c
]);
1315 s
[0] = bld_insn_2(bld
, NV_OP_MAX_F32
, s
[0], s
[1]);
1316 s
[0] = bld_insn_2(bld
, NV_OP_MAX_F32
, s
[0], s
[2]);
1317 s
[0] = bld_insn_1(bld
, NV_OP_RCP
, s
[0]);
1319 for (c
= 0; c
< 3; ++c
)
1320 t
[c
] = bld_insn_2(bld
, NV_OP_MUL_F32
, t
[c
], s
[0]);
1323 if (opcode
== NV_OP_TXB
|| opcode
== NV_OP_TXL
)
1324 t
[arg
++] = emit_fetch(bld
, insn
, 0, 3);
1325 emit_tex(bld
, opcode
, dst0
, t
, arg
, tic
, tsc
, cube
);
1328 static INLINE
struct nv_value
*
1329 bld_dot(struct bld_context
*bld
, const struct tgsi_full_instruction
*insn
,
1332 struct nv_value
*dotp
, *src0
, *src1
;
1335 src0
= emit_fetch(bld
, insn
, 0, 0);
1336 src1
= emit_fetch(bld
, insn
, 1, 0);
1337 dotp
= bld_insn_2(bld
, NV_OP_MUL_F32
, src0
, src1
);
1339 for (c
= 1; c
< n
; ++c
) {
1340 src0
= emit_fetch(bld
, insn
, 0, c
);
1341 src1
= emit_fetch(bld
, insn
, 1, c
);
1342 dotp
= bld_insn_3(bld
, NV_OP_MAD_F32
, src0
, src1
, dotp
);
1347 #define FOR_EACH_DST0_ENABLED_CHANNEL(chan, inst) \
1348 for (chan = 0; chan < 4; ++chan) \
1349 if ((inst)->Dst[0].Register.WriteMask & (1 << chan))
1352 bld_instruction(struct bld_context
*bld
,
1353 const struct tgsi_full_instruction
*insn
)
1355 struct nv_value
*src0
;
1356 struct nv_value
*src1
;
1357 struct nv_value
*src2
;
1358 struct nv_value
*dst0
[4] = { NULL
};
1359 struct nv_value
*temp
;
1361 uint opcode
= translate_opcode(insn
->Instruction
.Opcode
);
1362 uint8_t mask
= insn
->Dst
[0].Register
.WriteMask
;
1364 #ifdef NOUVEAU_DEBUG
1365 debug_printf("bld_instruction:"); tgsi_dump_instruction(insn
, 1);
1368 switch (insn
->Instruction
.Opcode
) {
1369 case TGSI_OPCODE_ADD
:
1370 case TGSI_OPCODE_MAX
:
1371 case TGSI_OPCODE_MIN
:
1372 case TGSI_OPCODE_MUL
:
1373 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1374 src0
= emit_fetch(bld
, insn
, 0, c
);
1375 src1
= emit_fetch(bld
, insn
, 1, c
);
1376 dst0
[c
] = bld_insn_2(bld
, opcode
, src0
, src1
);
1379 case TGSI_OPCODE_ARL
:
1380 src1
= bld_imm_u32(bld
, 4);
1381 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1382 src0
= emit_fetch(bld
, insn
, 0, c
);
1383 src0
= bld_insn_1(bld
, NV_OP_FLOOR
, src0
);
1384 src0
->insn
->ext
.cvt
.d
= NV_TYPE_S32
;
1385 src0
->insn
->ext
.cvt
.s
= NV_TYPE_F32
;
1386 dst0
[c
] = bld_insn_2(bld
, NV_OP_SHL
, src0
, src1
);
1389 case TGSI_OPCODE_CMP
:
1390 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1391 src0
= emit_fetch(bld
, insn
, 0, c
);
1392 src0
= bld_setp(bld
, NV_OP_SET_F32
, NV_CC_LT
, src0
, bld
->zero
);
1393 src1
= emit_fetch(bld
, insn
, 1, c
);
1394 src2
= emit_fetch(bld
, insn
, 2, c
);
1395 dst0
[c
] = bld_insn_3(bld
, NV_OP_SELP
, src1
, src2
, src0
);
1398 case TGSI_OPCODE_COS
:
1399 case TGSI_OPCODE_SIN
:
1400 src0
= emit_fetch(bld
, insn
, 0, 0);
1401 temp
= bld_insn_1(bld
, NV_OP_PRESIN
, src0
);
1402 if (insn
->Dst
[0].Register
.WriteMask
& 7)
1403 temp
= bld_insn_1(bld
, opcode
, temp
);
1404 for (c
= 0; c
< 3; ++c
)
1405 if (insn
->Dst
[0].Register
.WriteMask
& (1 << c
))
1407 if (!(insn
->Dst
[0].Register
.WriteMask
& (1 << 3)))
1409 src0
= emit_fetch(bld
, insn
, 0, 3);
1410 temp
= bld_insn_1(bld
, NV_OP_PRESIN
, src0
);
1411 dst0
[3] = bld_insn_1(bld
, opcode
, temp
);
1413 case TGSI_OPCODE_DP2
:
1414 temp
= bld_dot(bld
, insn
, 2);
1415 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1418 case TGSI_OPCODE_DP3
:
1419 temp
= bld_dot(bld
, insn
, 3);
1420 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1423 case TGSI_OPCODE_DP4
:
1424 temp
= bld_dot(bld
, insn
, 4);
1425 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1428 case TGSI_OPCODE_DPH
:
1429 src0
= bld_dot(bld
, insn
, 3);
1430 src1
= emit_fetch(bld
, insn
, 1, 3);
1431 temp
= bld_insn_2(bld
, NV_OP_ADD_F32
, src0
, src1
);
1432 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1435 case TGSI_OPCODE_DST
:
1436 if (insn
->Dst
[0].Register
.WriteMask
& 1)
1437 dst0
[0] = bld_imm_f32(bld
, 1.0f
);
1438 if (insn
->Dst
[0].Register
.WriteMask
& 2) {
1439 src0
= emit_fetch(bld
, insn
, 0, 1);
1440 src1
= emit_fetch(bld
, insn
, 1, 1);
1441 dst0
[1] = bld_insn_2(bld
, NV_OP_MUL_F32
, src0
, src1
);
1443 if (insn
->Dst
[0].Register
.WriteMask
& 4)
1444 dst0
[2] = emit_fetch(bld
, insn
, 0, 2);
1445 if (insn
->Dst
[0].Register
.WriteMask
& 8)
1446 dst0
[3] = emit_fetch(bld
, insn
, 1, 3);
1448 case TGSI_OPCODE_EXP
:
1449 src0
= emit_fetch(bld
, insn
, 0, 0);
1450 temp
= bld_insn_1(bld
, NV_OP_FLOOR
, src0
);
1452 if (insn
->Dst
[0].Register
.WriteMask
& 2)
1453 dst0
[1] = bld_insn_2(bld
, NV_OP_SUB_F32
, src0
, temp
);
1454 if (insn
->Dst
[0].Register
.WriteMask
& 1) {
1455 temp
= bld_insn_1(bld
, NV_OP_PREEX2
, temp
);
1456 dst0
[0] = bld_insn_1(bld
, NV_OP_EX2
, temp
);
1458 if (insn
->Dst
[0].Register
.WriteMask
& 4) {
1459 temp
= bld_insn_1(bld
, NV_OP_PREEX2
, src0
);
1460 dst0
[2] = bld_insn_1(bld
, NV_OP_EX2
, temp
);
1462 if (insn
->Dst
[0].Register
.WriteMask
& 8)
1463 dst0
[3] = bld_imm_f32(bld
, 1.0f
);
1465 case TGSI_OPCODE_EX2
:
1466 src0
= emit_fetch(bld
, insn
, 0, 0);
1467 temp
= bld_insn_1(bld
, NV_OP_PREEX2
, src0
);
1468 temp
= bld_insn_1(bld
, NV_OP_EX2
, temp
);
1469 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1472 case TGSI_OPCODE_FRC
:
1473 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1474 src0
= emit_fetch(bld
, insn
, 0, c
);
1475 dst0
[c
] = bld_insn_1(bld
, NV_OP_FLOOR
, src0
);
1476 dst0
[c
] = bld_insn_2(bld
, NV_OP_SUB_F32
, src0
, dst0
[c
]);
1479 case TGSI_OPCODE_KIL
:
1480 for (c
= 0; c
< 4; ++c
)
1481 bld_kil(bld
, emit_fetch(bld
, insn
, 0, c
));
1483 case TGSI_OPCODE_KILP
:
1484 (new_instruction(bld
->pc
, NV_OP_KIL
))->fixed
= 1;
1486 case TGSI_OPCODE_IF
:
1488 struct nv_basic_block
*b
= new_basic_block(bld
->pc
);
1490 assert(bld
->cond_lvl
< BLD_MAX_COND_NESTING
);
1492 nvc0_bblock_attach(bld
->pc
->current_block
, b
, CFG_EDGE_FORWARD
);
1494 bld
->join_bb
[bld
->cond_lvl
] = bld
->pc
->current_block
;
1495 bld
->cond_bb
[bld
->cond_lvl
] = bld
->pc
->current_block
;
1497 src1
= bld_setp(bld
, NV_OP_SET_U32
, NV_CC_EQ
,
1498 emit_fetch(bld
, insn
, 0, 0), bld
->zero
);
1500 bld_flow(bld
, NV_OP_BRA
, src1
, NULL
, (bld
->cond_lvl
== 0));
1503 bld_new_block(bld
, b
);
1506 case TGSI_OPCODE_ELSE
:
1508 struct nv_basic_block
*b
= new_basic_block(bld
->pc
);
1511 nvc0_bblock_attach(bld
->join_bb
[bld
->cond_lvl
], b
, CFG_EDGE_FORWARD
);
1513 bld
->cond_bb
[bld
->cond_lvl
]->exit
->target
= b
;
1514 bld
->cond_bb
[bld
->cond_lvl
] = bld
->pc
->current_block
;
1516 new_instruction(bld
->pc
, NV_OP_BRA
)->terminator
= 1;
1519 bld_new_block(bld
, b
);
1522 case TGSI_OPCODE_ENDIF
:
1524 struct nv_basic_block
*b
= new_basic_block(bld
->pc
);
1527 nvc0_bblock_attach(bld
->pc
->current_block
, b
, bld
->out_kind
);
1528 nvc0_bblock_attach(bld
->cond_bb
[bld
->cond_lvl
], b
, CFG_EDGE_FORWARD
);
1530 bld
->cond_bb
[bld
->cond_lvl
]->exit
->target
= b
;
1532 bld_new_block(bld
, b
);
1534 if (!bld
->cond_lvl
&& bld
->join_bb
[bld
->cond_lvl
]) {
1535 bld
->join_bb
[bld
->cond_lvl
]->exit
->prev
->target
= b
;
1536 new_instruction(bld
->pc
, NV_OP_JOIN
)->join
= 1;
1540 case TGSI_OPCODE_BGNLOOP
:
1542 struct nv_basic_block
*bl
= new_basic_block(bld
->pc
);
1543 struct nv_basic_block
*bb
= new_basic_block(bld
->pc
);
1545 assert(bld
->loop_lvl
< BLD_MAX_LOOP_NESTING
);
1547 bld
->loop_bb
[bld
->loop_lvl
] = bl
;
1548 bld
->brkt_bb
[bld
->loop_lvl
] = bb
;
1550 nvc0_bblock_attach(bld
->pc
->current_block
, bl
, CFG_EDGE_LOOP_ENTER
);
1552 bld_new_block(bld
, bld
->loop_bb
[bld
->loop_lvl
++]);
1554 if (bld
->loop_lvl
== bld
->pc
->loop_nesting_bound
)
1555 bld
->pc
->loop_nesting_bound
++;
1557 bld_clear_def_use(&bld
->tvs
[0][0], BLD_MAX_TEMPS
, bld
->loop_lvl
);
1558 bld_clear_def_use(&bld
->avs
[0][0], BLD_MAX_ADDRS
, bld
->loop_lvl
);
1559 bld_clear_def_use(&bld
->pvs
[0][0], BLD_MAX_PREDS
, bld
->loop_lvl
);
1562 case TGSI_OPCODE_BRK
:
1564 struct nv_basic_block
*bb
= bld
->brkt_bb
[bld
->loop_lvl
- 1];
1566 bld_flow(bld
, NV_OP_BRA
, NULL
, bb
, FALSE
);
1568 if (bld
->out_kind
== CFG_EDGE_FORWARD
) /* else we already had BRK/CONT */
1569 nvc0_bblock_attach(bld
->pc
->current_block
, bb
, CFG_EDGE_LOOP_LEAVE
);
1571 bld
->out_kind
= CFG_EDGE_FAKE
;
1574 case TGSI_OPCODE_CONT
:
1576 struct nv_basic_block
*bb
= bld
->loop_bb
[bld
->loop_lvl
- 1];
1578 bld_flow(bld
, NV_OP_BRA
, NULL
, bb
, FALSE
);
1580 nvc0_bblock_attach(bld
->pc
->current_block
, bb
, CFG_EDGE_BACK
);
1582 if ((bb
= bld
->join_bb
[bld
->cond_lvl
- 1])) {
1583 bld
->join_bb
[bld
->cond_lvl
- 1] = NULL
;
1584 nvc0_insn_delete(bb
->exit
->prev
);
1586 bld
->out_kind
= CFG_EDGE_FAKE
;
1589 case TGSI_OPCODE_ENDLOOP
:
1591 struct nv_basic_block
*bb
= bld
->loop_bb
[bld
->loop_lvl
- 1];
1593 bld_flow(bld
, NV_OP_BRA
, NULL
, bb
, FALSE
);
1595 nvc0_bblock_attach(bld
->pc
->current_block
, bb
, CFG_EDGE_BACK
);
1597 bld_loop_end(bld
, bb
); /* replace loop-side operand of the phis */
1599 bld_new_block(bld
, bld
->brkt_bb
[--bld
->loop_lvl
]);
1602 case TGSI_OPCODE_ABS
:
1603 case TGSI_OPCODE_CEIL
:
1604 case TGSI_OPCODE_FLR
:
1605 case TGSI_OPCODE_TRUNC
:
1606 case TGSI_OPCODE_DDX
:
1607 case TGSI_OPCODE_DDY
:
1608 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1609 src0
= emit_fetch(bld
, insn
, 0, c
);
1610 dst0
[c
] = bld_insn_1(bld
, opcode
, src0
);
1613 case TGSI_OPCODE_LIT
:
1614 bld_lit(bld
, dst0
, insn
);
1616 case TGSI_OPCODE_LRP
:
1617 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1618 src0
= emit_fetch(bld
, insn
, 0, c
);
1619 src1
= emit_fetch(bld
, insn
, 1, c
);
1620 src2
= emit_fetch(bld
, insn
, 2, c
);
1621 dst0
[c
] = bld_insn_2(bld
, NV_OP_SUB_F32
, src1
, src2
);
1622 dst0
[c
] = bld_insn_3(bld
, NV_OP_MAD_F32
, dst0
[c
], src0
, src2
);
1625 case TGSI_OPCODE_MOV
:
1626 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1627 dst0
[c
] = emit_fetch(bld
, insn
, 0, c
);
1629 case TGSI_OPCODE_MAD
:
1630 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1631 src0
= emit_fetch(bld
, insn
, 0, c
);
1632 src1
= emit_fetch(bld
, insn
, 1, c
);
1633 src2
= emit_fetch(bld
, insn
, 2, c
);
1634 dst0
[c
] = bld_insn_3(bld
, opcode
, src0
, src1
, src2
);
1637 case TGSI_OPCODE_POW
:
1638 src0
= emit_fetch(bld
, insn
, 0, 0);
1639 src1
= emit_fetch(bld
, insn
, 1, 0);
1640 temp
= bld_pow(bld
, src0
, src1
);
1641 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1644 case TGSI_OPCODE_LOG
:
1645 src0
= emit_fetch(bld
, insn
, 0, 0);
1646 src0
= bld_insn_1(bld
, NV_OP_ABS_F32
, src0
);
1647 temp
= bld_insn_1(bld
, NV_OP_LG2
, src0
);
1649 if (insn
->Dst
[0].Register
.WriteMask
& 3) {
1650 temp
= bld_insn_1(bld
, NV_OP_FLOOR
, temp
);
1653 if (insn
->Dst
[0].Register
.WriteMask
& 2) {
1654 temp
= bld_insn_1(bld
, NV_OP_PREEX2
, temp
);
1655 temp
= bld_insn_1(bld
, NV_OP_EX2
, temp
);
1656 temp
= bld_insn_1(bld
, NV_OP_RCP
, temp
);
1657 dst0
[1] = bld_insn_2(bld
, NV_OP_MUL_F32
, src0
, temp
);
1659 if (insn
->Dst
[0].Register
.WriteMask
& 8)
1660 dst0
[3] = bld_imm_f32(bld
, 1.0f
);
1662 case TGSI_OPCODE_RCP
:
1663 case TGSI_OPCODE_LG2
:
1664 src0
= emit_fetch(bld
, insn
, 0, 0);
1665 temp
= bld_insn_1(bld
, opcode
, src0
);
1666 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1669 case TGSI_OPCODE_RSQ
:
1670 src0
= emit_fetch(bld
, insn
, 0, 0);
1671 temp
= bld_insn_1(bld
, NV_OP_ABS_F32
, src0
);
1672 temp
= bld_insn_1(bld
, NV_OP_RSQ
, temp
);
1673 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
)
1676 case TGSI_OPCODE_SLT
:
1677 case TGSI_OPCODE_SGE
:
1678 case TGSI_OPCODE_SEQ
:
1679 case TGSI_OPCODE_SGT
:
1680 case TGSI_OPCODE_SLE
:
1681 case TGSI_OPCODE_SNE
:
1682 case TGSI_OPCODE_ISLT
:
1683 case TGSI_OPCODE_ISGE
:
1684 case TGSI_OPCODE_USEQ
:
1685 case TGSI_OPCODE_USGE
:
1686 case TGSI_OPCODE_USLT
:
1687 case TGSI_OPCODE_USNE
:
1688 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1689 src0
= emit_fetch(bld
, insn
, 0, c
);
1690 src1
= emit_fetch(bld
, insn
, 1, c
);
1691 dst0
[c
] = bld_insn_2(bld
, opcode
, src0
, src1
);
1692 dst0
[c
]->insn
->set_cond
= translate_setcc(insn
->Instruction
.Opcode
);
1695 case TGSI_OPCODE_SCS
:
1696 if (insn
->Dst
[0].Register
.WriteMask
& 0x3) {
1697 src0
= emit_fetch(bld
, insn
, 0, 0);
1698 temp
= bld_insn_1(bld
, NV_OP_PRESIN
, src0
);
1699 if (insn
->Dst
[0].Register
.WriteMask
& 0x1)
1700 dst0
[0] = bld_insn_1(bld
, NV_OP_COS
, temp
);
1701 if (insn
->Dst
[0].Register
.WriteMask
& 0x2)
1702 dst0
[1] = bld_insn_1(bld
, NV_OP_SIN
, temp
);
1704 if (insn
->Dst
[0].Register
.WriteMask
& 0x4)
1705 dst0
[2] = bld_imm_f32(bld
, 0.0f
);
1706 if (insn
->Dst
[0].Register
.WriteMask
& 0x8)
1707 dst0
[3] = bld_imm_f32(bld
, 1.0f
);
1709 case TGSI_OPCODE_SSG
:
1710 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) { /* XXX: set lt, set gt, sub */
1711 src0
= emit_fetch(bld
, insn
, 0, c
);
1712 src1
= bld_setp(bld
, NV_OP_SET_F32
, NV_CC_EQ
, src0
, bld
->zero
);
1713 temp
= bld_insn_2(bld
, NV_OP_AND
, src0
, bld_imm_u32(bld
, 0x80000000));
1714 temp
= bld_insn_2(bld
, NV_OP_OR
, temp
, bld_imm_f32(bld
, 1.0f
));
1715 dst0
[c
] = bld_insn_1(bld
, NV_OP_MOV
, temp
);
1716 bld_src_predicate(bld
, dst0
[c
]->insn
, 1, src1
);
1719 case TGSI_OPCODE_SUB
:
1720 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1721 src0
= emit_fetch(bld
, insn
, 0, c
);
1722 src1
= emit_fetch(bld
, insn
, 1, c
);
1723 dst0
[c
] = bld_insn_2(bld
, NV_OP_SUB_F32
, src0
, src1
);
1726 case TGSI_OPCODE_TEX
:
1727 case TGSI_OPCODE_TXB
:
1728 case TGSI_OPCODE_TXL
:
1729 case TGSI_OPCODE_TXP
:
1730 bld_tex(bld
, dst0
, insn
);
1732 case TGSI_OPCODE_XPD
:
1733 FOR_EACH_DST0_ENABLED_CHANNEL(c
, insn
) {
1735 dst0
[3] = bld_imm_f32(bld
, 1.0f
);
1738 src0
= emit_fetch(bld
, insn
, 1, (c
+ 1) % 3);
1739 src1
= emit_fetch(bld
, insn
, 0, (c
+ 2) % 3);
1740 dst0
[c
] = bld_insn_2(bld
, NV_OP_MUL_F32
, src0
, src1
);
1742 src0
= emit_fetch(bld
, insn
, 0, (c
+ 1) % 3);
1743 src1
= emit_fetch(bld
, insn
, 1, (c
+ 2) % 3);
1744 dst0
[c
] = bld_insn_3(bld
, NV_OP_MAD_F32
, src0
, src1
, dst0
[c
]);
1746 dst0
[c
]->insn
->src
[2]->mod
^= NV_MOD_NEG
;
1749 case TGSI_OPCODE_RET
:
1750 (new_instruction(bld
->pc
, NV_OP_RET
))->fixed
= 1;
1752 case TGSI_OPCODE_END
:
1753 /* VP outputs are exported in-place as scalars, optimization later */
1754 if (bld
->pc
->is_fragprog
)
1755 bld_export_fp_outputs(bld
);
1758 NOUVEAU_ERR("unhandled opcode %u\n", insn
->Instruction
.Opcode
);
1763 if (insn
->Dst
[0].Register
.File
== TGSI_FILE_OUTPUT
&&
1764 !bld
->pc
->is_fragprog
) {
1765 struct nv_instruction
*mi
= NULL
;
1768 for (c
= 0; c
< 4; ++c
)
1769 if ((mask
& (1 << c
)) &&
1770 ((dst0
[c
]->reg
.file
== NV_FILE_IMM
) ||
1771 (dst0
[c
]->reg
.id
== 63 && dst0
[c
]->reg
.file
== NV_FILE_GPR
)))
1772 dst0
[c
] = bld_insn_1(bld
, NV_OP_MOV
, dst0
[c
]);
1775 if ((mask
& 0x3) == 0x3) {
1778 mi
= bld_insn_2(bld
, NV_OP_BIND
, dst0
[0], dst0
[1])->insn
;
1780 if ((mask
& 0xc) == 0xc) {
1784 nv_reference(bld
->pc
, mi
, 2, dst0
[2]);
1785 nv_reference(bld
->pc
, mi
, 3, dst0
[3]);
1789 mi
= bld_insn_2(bld
, NV_OP_BIND
, dst0
[2], dst0
[3])->insn
;
1792 if (mi
&& (mask
& 0x4)) {
1795 nv_reference(bld
->pc
, mi
, 2, dst0
[2]);
1799 struct nv_instruction
*ex
= new_instruction(bld
->pc
, NV_OP_EXPORT
);
1802 nv_reference(bld
->pc
, ex
, 0, new_value(bld
->pc
, NV_FILE_MEM_V
, 4));
1803 nv_reference(bld
->pc
, ex
, 1, mi
->def
[0]);
1805 for (s
= 1; s
< size
/ 4; ++s
) {
1806 bld_def(mi
, s
, new_value(bld
->pc
, NV_FILE_GPR
, 4));
1807 nv_reference(bld
->pc
, ex
, s
+ 1, mi
->def
[s
]);
1811 ex
->src
[0]->value
->reg
.size
= size
;
1812 ex
->src
[0]->value
->reg
.address
=
1813 bld
->ti
->output_loc
[insn
->Dst
[0].Register
.Index
][c
];
1817 for (c
= 0; c
< 4; ++c
)
1818 if (mask
& (1 << c
))
1819 emit_store(bld
, insn
, c
, dst0
[c
]);
1823 bld_free_registers(struct bld_register
*base
, int n
)
1827 for (i
= 0; i
< n
; ++i
)
1828 for (c
= 0; c
< 4; ++c
)
1829 util_dynarray_fini(&base
[i
* 4 + c
].vals
);
1833 nvc0_tgsi_to_nc(struct nv_pc
*pc
, struct nvc0_translation_info
*ti
)
1835 struct bld_context
*bld
= CALLOC_STRUCT(bld_context
);
1838 pc
->root
[0] = pc
->current_block
= new_basic_block(pc
);
1843 pc
->loop_nesting_bound
= 1;
1845 bld
->zero
= new_value(pc
, NV_FILE_GPR
, 4);
1846 bld
->zero
->reg
.id
= 63;
1848 if (pc
->is_fragprog
) {
1849 struct nv_value
*mem
= new_value(pc
, NV_FILE_MEM_V
, 4);
1850 mem
->reg
.address
= 0x7c;
1852 bld
->frag_coord
[3] = bld_insn_1(bld
, NV_OP_LINTERP
, mem
);
1853 bld
->frag_coord
[3] = bld_insn_1(bld
, NV_OP_RCP
, bld
->frag_coord
[3]);
1856 for (ip
= 0; ip
< ti
->num_insns
; ++ip
)
1857 bld_instruction(bld
, &ti
->insns
[ip
]);
1859 bld_free_registers(&bld
->tvs
[0][0], BLD_MAX_TEMPS
);
1860 bld_free_registers(&bld
->avs
[0][0], BLD_MAX_ADDRS
);
1861 bld_free_registers(&bld
->pvs
[0][0], BLD_MAX_PREDS
);
1862 bld_free_registers(&bld
->ovs
[0][0], PIPE_MAX_SHADER_OUTPUTS
);
1868 /* If a variable is assigned in a loop, replace all references to the value
1869 * from outside the loop with a phi value.
1872 bld_replace_value(struct nv_pc
*pc
, struct nv_basic_block
*b
,
1873 struct nv_value
*old_val
,
1874 struct nv_value
*new_val
)
1876 struct nv_instruction
*nvi
;
1878 for (nvi
= b
->phi
? b
->phi
: b
->entry
; nvi
; nvi
= nvi
->next
) {
1880 for (s
= 0; s
< 6 && nvi
->src
[s
]; ++s
)
1881 if (nvi
->src
[s
]->value
== old_val
)
1882 nv_reference(pc
, nvi
, s
, new_val
);
1885 b
->pass_seq
= pc
->pass_seq
;
1887 if (b
->out
[0] && b
->out
[0]->pass_seq
< pc
->pass_seq
)
1888 bld_replace_value(pc
, b
->out
[0], old_val
, new_val
);
1890 if (b
->out
[1] && b
->out
[1]->pass_seq
< pc
->pass_seq
)
1891 bld_replace_value(pc
, b
->out
[1], old_val
, new_val
);