Merge remote-tracking branch 'mareko/r300g-draw-instanced' into pipe-video
[mesa.git] / src / gallium / drivers / nv50 / nv50_tgsi_to_nc.c
1 /*
2 * Copyright 2010 Christoph Bumiller
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 */
22
23 #include <unistd.h>
24
25 #include "nv50_context.h"
26 #include "nv50_pc.h"
27
28 #include "pipe/p_shader_tokens.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "tgsi/tgsi_util.h"
31
32 #include "tgsi/tgsi_dump.h"
33
34 #define BLD_MAX_TEMPS 64
35 #define BLD_MAX_ADDRS 4
36 #define BLD_MAX_PREDS 4
37 #define BLD_MAX_IMMDS 128
38
39 #define BLD_MAX_COND_NESTING 8
40 #define BLD_MAX_LOOP_NESTING 4
41 #define BLD_MAX_CALL_NESTING 2
42
43 /* collects all values assigned to the same TGSI register */
44 struct bld_value_stack {
45 struct nv_value *top;
46 struct nv_value **body;
47 unsigned size;
48 uint16_t loop_use; /* 1 bit per loop level, indicates if used/defd */
49 uint16_t loop_def;
50 };
51
52 static INLINE void
53 bld_vals_push_val(struct bld_value_stack *stk, struct nv_value *val)
54 {
55 assert(!stk->size || (stk->body[stk->size - 1] != val));
56
57 if (!(stk->size % 8)) {
58 unsigned old_sz = (stk->size + 0) * sizeof(struct nv_value *);
59 unsigned new_sz = (stk->size + 8) * sizeof(struct nv_value *);
60 stk->body = (struct nv_value **)REALLOC(stk->body, old_sz, new_sz);
61 }
62 stk->body[stk->size++] = val;
63 }
64
65 static INLINE boolean
66 bld_vals_del_val(struct bld_value_stack *stk, struct nv_value *val)
67 {
68 unsigned i;
69
70 for (i = stk->size; i > 0; --i)
71 if (stk->body[i - 1] == val)
72 break;
73 if (!i)
74 return FALSE;
75
76 if (i != stk->size)
77 stk->body[i - 1] = stk->body[stk->size - 1];
78
79 --stk->size; /* XXX: old size in REALLOC */
80 return TRUE;
81 }
82
83 static INLINE void
84 bld_vals_push(struct bld_value_stack *stk)
85 {
86 bld_vals_push_val(stk, stk->top);
87 stk->top = NULL;
88 }
89
90 static INLINE void
91 bld_push_values(struct bld_value_stack *stacks, int n)
92 {
93 int i, c;
94
95 for (i = 0; i < n; ++i)
96 for (c = 0; c < 4; ++c)
97 if (stacks[i * 4 + c].top)
98 bld_vals_push(&stacks[i * 4 + c]);
99 }
100
101 struct bld_context {
102 struct nv50_translation_info *ti;
103
104 struct nv_pc *pc;
105 struct nv_basic_block *b;
106
107 struct tgsi_parse_context parse[BLD_MAX_CALL_NESTING];
108 int call_lvl;
109
110 struct nv_basic_block *cond_bb[BLD_MAX_COND_NESTING];
111 struct nv_basic_block *join_bb[BLD_MAX_COND_NESTING];
112 struct nv_basic_block *else_bb[BLD_MAX_COND_NESTING];
113 int cond_lvl;
114 struct nv_basic_block *loop_bb[BLD_MAX_LOOP_NESTING];
115 struct nv_basic_block *brkt_bb[BLD_MAX_LOOP_NESTING];
116 int loop_lvl;
117
118 ubyte out_kind; /* CFG_EDGE_FORWARD, or FAKE in case of BREAK/CONT */
119
120 struct bld_value_stack tvs[BLD_MAX_TEMPS][4]; /* TGSI_FILE_TEMPORARY */
121 struct bld_value_stack avs[BLD_MAX_ADDRS][4]; /* TGSI_FILE_ADDRESS */
122 struct bld_value_stack pvs[BLD_MAX_PREDS][4]; /* TGSI_FILE_PREDICATE */
123 struct bld_value_stack ovs[PIPE_MAX_SHADER_OUTPUTS][4];
124
125 uint32_t outputs_written[(PIPE_MAX_SHADER_OUTPUTS + 7) / 8];
126
127 struct nv_value *frgcrd[4];
128 struct nv_value *sysval[4];
129
130 /* wipe on new BB */
131 struct nv_value *saved_addr[4][2];
132 struct nv_value *saved_inputs[128];
133 struct nv_value *saved_immd[BLD_MAX_IMMDS];
134 uint num_immds;
135 };
136
137 static INLINE ubyte
138 bld_stack_file(struct bld_context *bld, struct bld_value_stack *stk)
139 {
140 if (stk < &bld->avs[0][0])
141 return NV_FILE_GPR;
142 else
143 if (stk < &bld->pvs[0][0])
144 return NV_FILE_ADDR;
145 else
146 if (stk < &bld->ovs[0][0])
147 return NV_FILE_FLAGS;
148 else
149 return NV_FILE_OUT;
150 }
151
152 static INLINE struct nv_value *
153 bld_fetch(struct bld_context *bld, struct bld_value_stack *stk, int i, int c)
154 {
155 stk[i * 4 + c].loop_use |= 1 << bld->loop_lvl;
156
157 return stk[i * 4 + c].top;
158 }
159
160 static struct nv_value *
161 bld_loop_phi(struct bld_context *, struct bld_value_stack *, struct nv_value *);
162
163 /* If a variable is defined in a loop without prior use, we don't need
164 * a phi in the loop header to account for backwards flow.
165 *
166 * However, if this variable is then also used outside the loop, we do
167 * need a phi after all. But we must not use this phi's def inside the
168 * loop, so we can eliminate the phi if it is unused later.
169 */
170 static INLINE void
171 bld_store(struct bld_context *bld, struct bld_value_stack *stk, int i, int c,
172 struct nv_value *val)
173 {
174 const uint16_t m = 1 << bld->loop_lvl;
175
176 stk = &stk[i * 4 + c];
177
178 if (bld->loop_lvl && !(m & (stk->loop_def | stk->loop_use)))
179 bld_loop_phi(bld, stk, val);
180
181 stk->top = val;
182 stk->loop_def |= 1 << bld->loop_lvl;
183 }
184
185 static INLINE void
186 bld_clear_def_use(struct bld_value_stack *stk, int n, int lvl)
187 {
188 int i;
189 const uint16_t mask = ~(1 << lvl);
190
191 for (i = 0; i < n * 4; ++i) {
192 stk[i].loop_def &= mask;
193 stk[i].loop_use &= mask;
194 }
195 }
196
197 #define FETCH_TEMP(i, c) bld_fetch(bld, &bld->tvs[0][0], i, c)
198 #define STORE_TEMP(i, c, v) bld_store(bld, &bld->tvs[0][0], i, c, (v))
199 #define FETCH_ADDR(i, c) bld_fetch(bld, &bld->avs[0][0], i, c)
200 #define STORE_ADDR(i, c, v) bld_store(bld, &bld->avs[0][0], i, c, (v))
201 #define FETCH_PRED(i, c) bld_fetch(bld, &bld->pvs[0][0], i, c)
202 #define STORE_PRED(i, c, v) bld_store(bld, &bld->pvs[0][0], i, c, (v))
203
204 #define STORE_OUTR(i, c, v) \
205 do { \
206 bld->ovs[i][c].top = (v); \
207 bld->outputs_written[(i) / 8] |= 1 << (((i) * 4 + (c)) % 32); \
208 } while (0)
209
210 static INLINE void
211 bld_warn_uninitialized(struct bld_context *bld, int kind,
212 struct bld_value_stack *stk, struct nv_basic_block *b)
213 {
214 #if NV50_DEBUG & NV50_DEBUG_PROG_IR
215 long i = (stk - &bld->tvs[0][0]) / 4;
216 long c = (stk - &bld->tvs[0][0]) & 3;
217
218 if (c == 3)
219 c = -1;
220
221 debug_printf("WARNING: TEMP[%li].%c %s used uninitialized in BB:%i\n",
222 i, (int)('x' + c), kind ? "may be" : "is", b->id);
223 #endif
224 }
225
226 static INLINE struct nv_value *
227 bld_def(struct nv_instruction *i, int c, struct nv_value *value)
228 {
229 i->def[c] = value;
230 value->insn = i;
231 return value;
232 }
233
234 static INLINE struct nv_value *
235 find_by_bb(struct bld_value_stack *stack, struct nv_basic_block *b)
236 {
237 int i;
238
239 if (stack->top && stack->top->insn->bb == b)
240 return stack->top;
241
242 for (i = stack->size - 1; i >= 0; --i)
243 if (stack->body[i]->insn->bb == b)
244 return stack->body[i];
245 return NULL;
246 }
247
248 /* fetch value from stack that was defined in the specified basic block,
249 * or search for first definitions in all of its predecessors
250 */
251 static void
252 fetch_by_bb(struct bld_value_stack *stack,
253 struct nv_value **vals, int *n,
254 struct nv_basic_block *b)
255 {
256 int i;
257 struct nv_value *val;
258
259 assert(*n < 16); /* MAX_COND_NESTING */
260
261 val = find_by_bb(stack, b);
262 if (val) {
263 for (i = 0; i < *n; ++i)
264 if (vals[i] == val)
265 return;
266 vals[(*n)++] = val;
267 return;
268 }
269 for (i = 0; i < b->num_in; ++i)
270 if (!IS_WALL_EDGE(b->in_kind[i]))
271 fetch_by_bb(stack, vals, n, b->in[i]);
272 }
273
274 static INLINE boolean
275 nvbb_is_terminated(struct nv_basic_block *bb)
276 {
277 return bb->exit && bb->exit->is_terminator;
278 }
279
280 static INLINE struct nv_value *
281 bld_load_imm_u32(struct bld_context *bld, uint32_t u);
282
283 static INLINE struct nv_value *
284 bld_undef(struct bld_context *bld, ubyte file)
285 {
286 struct nv_instruction *nvi = new_instruction(bld->pc, NV_OP_UNDEF);
287
288 return bld_def(nvi, 0, new_value(bld->pc, file, NV_TYPE_U32));
289 }
290
291 static struct nv_value *
292 bld_phi(struct bld_context *bld, struct nv_basic_block *b,
293 struct bld_value_stack *stack)
294 {
295 struct nv_basic_block *in;
296 struct nv_value *vals[16] = { 0 };
297 struct nv_value *val;
298 struct nv_instruction *phi;
299 int i, j, n;
300
301 do {
302 i = n = 0;
303 fetch_by_bb(stack, vals, &n, b);
304
305 if (!n) {
306 bld_warn_uninitialized(bld, 0, stack, b);
307 return NULL;
308 }
309
310 if (n == 1) {
311 if (nvbb_dominated_by(b, vals[0]->insn->bb))
312 break;
313
314 bld_warn_uninitialized(bld, 1, stack, b);
315
316 /* back-tracking to insert missing value of other path */
317 in = b;
318 while (in->in[0]) {
319 if (in->num_in == 1) {
320 in = in->in[0];
321 } else {
322 if (!nvbb_reachable_by(in->in[0], vals[0]->insn->bb, b))
323 in = in->in[0];
324 else
325 if (!nvbb_reachable_by(in->in[1], vals[0]->insn->bb, b))
326 in = in->in[1];
327 else
328 in = in->in[0];
329 }
330 }
331 bld->pc->current_block = in;
332
333 /* should make this a no-op */
334 bld_vals_push_val(stack, bld_undef(bld, vals[0]->reg.file));
335 continue;
336 }
337
338 for (i = 0; i < n; ++i) {
339 /* if value dominates b, continue to the redefinitions */
340 if (nvbb_dominated_by(b, vals[i]->insn->bb))
341 continue;
342
343 /* if value dominates any in-block, b should be the dom frontier */
344 for (j = 0; j < b->num_in; ++j)
345 if (nvbb_dominated_by(b->in[j], vals[i]->insn->bb))
346 break;
347 /* otherwise, find the dominance frontier and put the phi there */
348 if (j == b->num_in) {
349 in = nvbb_dom_frontier(vals[i]->insn->bb);
350 val = bld_phi(bld, in, stack);
351 bld_vals_push_val(stack, val);
352 break;
353 }
354 }
355 } while(i < n);
356
357 bld->pc->current_block = b;
358
359 if (n == 1)
360 return vals[0];
361
362 phi = new_instruction(bld->pc, NV_OP_PHI);
363
364 bld_def(phi, 0, new_value(bld->pc, vals[0]->reg.file, vals[0]->reg.type));
365 for (i = 0; i < n; ++i)
366 phi->src[i] = new_ref(bld->pc, vals[i]);
367
368 return phi->def[0];
369 }
370
371 /* Insert a phi function in the loop header.
372 * For nested loops, we need to insert phi functions in all the outer
373 * loop headers if they don't have one yet.
374 *
375 * @def: redefinition from inside loop, or NULL if to be replaced later
376 */
377 static struct nv_value *
378 bld_loop_phi(struct bld_context *bld, struct bld_value_stack *stack,
379 struct nv_value *def)
380 {
381 struct nv_instruction *phi;
382 struct nv_basic_block *bb = bld->pc->current_block;
383 struct nv_value *val = NULL;
384
385 if (bld->loop_lvl > 1) {
386 --bld->loop_lvl;
387 if (!((stack->loop_def | stack->loop_use) & (1 << bld->loop_lvl)))
388 val = bld_loop_phi(bld, stack, NULL);
389 ++bld->loop_lvl;
390 }
391
392 if (!val)
393 val = bld_phi(bld, bld->pc->current_block, stack); /* old definition */
394 if (!val) {
395 bld->pc->current_block = bld->loop_bb[bld->loop_lvl - 1]->in[0];
396 val = bld_undef(bld, bld_stack_file(bld, stack));
397 }
398
399 bld->pc->current_block = bld->loop_bb[bld->loop_lvl - 1];
400
401 phi = new_instruction(bld->pc, NV_OP_PHI);
402
403 bld_def(phi, 0, new_value_like(bld->pc, val));
404 if (!def)
405 def = phi->def[0];
406
407 bld_vals_push_val(stack, phi->def[0]);
408
409 phi->target = (struct nv_basic_block *)stack; /* cheat */
410
411 nv_reference(bld->pc, &phi->src[0], val);
412 nv_reference(bld->pc, &phi->src[1], def);
413
414 bld->pc->current_block = bb;
415
416 return phi->def[0];
417 }
418
419 static INLINE struct nv_value *
420 bld_fetch_global(struct bld_context *bld, struct bld_value_stack *stack)
421 {
422 const uint16_t m = 1 << bld->loop_lvl;
423 const uint16_t use = stack->loop_use;
424
425 stack->loop_use |= m;
426
427 /* If neither used nor def'd inside the loop, build a phi in foresight,
428 * so we don't have to replace stuff later on, which requires tracking.
429 */
430 if (bld->loop_lvl && !((use | stack->loop_def) & m))
431 return bld_loop_phi(bld, stack, NULL);
432
433 return bld_phi(bld, bld->pc->current_block, stack);
434 }
435
436 static INLINE struct nv_value *
437 bld_imm_u32(struct bld_context *bld, uint32_t u)
438 {
439 int i;
440 unsigned n = bld->num_immds;
441
442 for (i = 0; i < n; ++i)
443 if (bld->saved_immd[i]->reg.imm.u32 == u)
444 return bld->saved_immd[i];
445 assert(n < BLD_MAX_IMMDS);
446
447 bld->num_immds++;
448
449 bld->saved_immd[n] = new_value(bld->pc, NV_FILE_IMM, NV_TYPE_U32);
450 bld->saved_immd[n]->reg.imm.u32 = u;
451 return bld->saved_immd[n];
452 }
453
454 static void
455 bld_replace_value(struct nv_pc *, struct nv_basic_block *, struct nv_value *,
456 struct nv_value *);
457
458 /* Replace the source of the phi in the loop header by the last assignment,
459 * or eliminate the phi function if there is no assignment inside the loop.
460 *
461 * Redundancy situation 1 - (used) but (not redefined) value:
462 * %3 = phi %0, %3 = %3 is used
463 * %3 = phi %0, %4 = is new definition
464 *
465 * Redundancy situation 2 - (not used) but (redefined) value:
466 * %3 = phi %0, %2 = %2 is used, %3 could be used outside, deleted by DCE
467 */
468 static void
469 bld_loop_end(struct bld_context *bld, struct nv_basic_block *bb)
470 {
471 struct nv_basic_block *save = bld->pc->current_block;
472 struct nv_instruction *phi, *next;
473 struct nv_value *val;
474 struct bld_value_stack *stk;
475 int i, s, n;
476
477 for (phi = bb->phi; phi && phi->opcode == NV_OP_PHI; phi = next) {
478 next = phi->next;
479
480 stk = (struct bld_value_stack *)phi->target;
481 phi->target = NULL;
482
483 /* start with s == 1, src[0] is from outside the loop */
484 for (s = 1, n = 0; n < bb->num_in; ++n) {
485 if (bb->in_kind[n] != CFG_EDGE_BACK)
486 continue;
487
488 assert(s < 4);
489 bld->pc->current_block = bb->in[n];
490 val = bld_fetch_global(bld, stk);
491
492 for (i = 0; i < 4; ++i)
493 if (phi->src[i] && phi->src[i]->value == val)
494 break;
495 if (i == 4) {
496 /* skip values we do not want to replace */
497 for (; phi->src[s] && phi->src[s]->value != phi->def[0]; ++s);
498 nv_reference(bld->pc, &phi->src[s++], val);
499 }
500 }
501 bld->pc->current_block = save;
502
503 if (phi->src[0]->value == phi->def[0] ||
504 phi->src[0]->value == phi->src[1]->value)
505 s = 1;
506 else
507 if (phi->src[1]->value == phi->def[0])
508 s = 0;
509 else
510 continue;
511
512 if (s >= 0) {
513 /* eliminate the phi */
514 bld_vals_del_val(stk, phi->def[0]);
515
516 ++bld->pc->pass_seq;
517 bld_replace_value(bld->pc, bb, phi->def[0], phi->src[s]->value);
518
519 nv_nvi_delete(phi);
520 }
521 }
522 }
523
524 static INLINE struct nv_value *
525 bld_imm_f32(struct bld_context *bld, float f)
526 {
527 return bld_imm_u32(bld, fui(f));
528 }
529
530 #define SET_TYPE(v, t) ((v)->reg.type = (v)->reg.as_type = (t))
531
532 static struct nv_value *
533 bld_insn_1(struct bld_context *bld, uint opcode, struct nv_value *src0)
534 {
535 struct nv_instruction *insn = new_instruction(bld->pc, opcode);
536
537 nv_reference(bld->pc, &insn->src[0], src0);
538
539 return bld_def(insn, 0, new_value(bld->pc, NV_FILE_GPR, src0->reg.as_type));
540 }
541
542 static struct nv_value *
543 bld_insn_2(struct bld_context *bld, uint opcode,
544 struct nv_value *src0, struct nv_value *src1)
545 {
546 struct nv_instruction *insn = new_instruction(bld->pc, opcode);
547
548 nv_reference(bld->pc, &insn->src[0], src0);
549 nv_reference(bld->pc, &insn->src[1], src1);
550
551 return bld_def(insn, 0, new_value(bld->pc, NV_FILE_GPR, src0->reg.as_type));
552 }
553
554 static struct nv_value *
555 bld_insn_3(struct bld_context *bld, uint opcode,
556 struct nv_value *src0, struct nv_value *src1,
557 struct nv_value *src2)
558 {
559 struct nv_instruction *insn = new_instruction(bld->pc, opcode);
560
561 nv_reference(bld->pc, &insn->src[0], src0);
562 nv_reference(bld->pc, &insn->src[1], src1);
563 nv_reference(bld->pc, &insn->src[2], src2);
564
565 return bld_def(insn, 0, new_value(bld->pc, NV_FILE_GPR, src0->reg.as_type));
566 }
567
568 static struct nv_value *
569 bld_duplicate_insn(struct bld_context *bld, struct nv_instruction *nvi)
570 {
571 struct nv_instruction *dupi = new_instruction(bld->pc, nvi->opcode);
572 int c;
573
574 if (nvi->def[0])
575 bld_def(dupi, 0, new_value_like(bld->pc, nvi->def[0]));
576
577 if (nvi->flags_def) {
578 dupi->flags_def = new_value_like(bld->pc, nvi->flags_def);
579 dupi->flags_def->insn = dupi;
580 }
581
582 for (c = 0; c < 5; ++c)
583 if (nvi->src[c])
584 nv_reference(bld->pc, &dupi->src[c], nvi->src[c]->value);
585 if (nvi->flags_src)
586 nv_reference(bld->pc, &dupi->flags_src, nvi->flags_src->value);
587
588 dupi->cc = nvi->cc;
589 dupi->saturate = nvi->saturate;
590 dupi->centroid = nvi->centroid;
591 dupi->flat = nvi->flat;
592
593 return dupi->def[0];
594 }
595
596 static void
597 bld_lmem_store(struct bld_context *bld, struct nv_value *ptr, int ofst,
598 struct nv_value *val)
599 {
600 struct nv_instruction *insn = new_instruction(bld->pc, NV_OP_STA);
601 struct nv_value *loc;
602
603 loc = new_value(bld->pc, NV_FILE_MEM_L, NV_TYPE_U32);
604
605 loc->reg.id = ofst * 4;
606
607 nv_reference(bld->pc, &insn->src[0], loc);
608 nv_reference(bld->pc, &insn->src[1], val);
609 nv_reference(bld->pc, &insn->src[4], ptr);
610 }
611
612 static struct nv_value *
613 bld_lmem_load(struct bld_context *bld, struct nv_value *ptr, int ofst)
614 {
615 struct nv_value *loc, *val;
616
617 loc = new_value(bld->pc, NV_FILE_MEM_L, NV_TYPE_U32);
618
619 loc->reg.id = ofst * 4;
620
621 val = bld_insn_1(bld, NV_OP_LDA, loc);
622
623 nv_reference(bld->pc, &val->insn->src[4], ptr);
624
625 return val;
626 }
627
628 #define BLD_INSN_1_EX(d, op, dt, s0, s0t) \
629 do { \
630 (d) = bld_insn_1(bld, (NV_OP_##op), (s0)); \
631 SET_TYPE(d, NV_TYPE_##dt); \
632 (d)->insn->src[0]->typecast = NV_TYPE_##s0t; \
633 } while(0)
634
635 #define BLD_INSN_2_EX(d, op, dt, s0, s0t, s1, s1t) \
636 do { \
637 (d) = bld_insn_2(bld, (NV_OP_##op), (s0), (s1)); \
638 SET_TYPE(d, NV_TYPE_##dt); \
639 (d)->insn->src[0]->typecast = NV_TYPE_##s0t; \
640 (d)->insn->src[1]->typecast = NV_TYPE_##s1t; \
641 } while(0)
642
643 static struct nv_value *
644 bld_pow(struct bld_context *bld, struct nv_value *x, struct nv_value *e)
645 {
646 struct nv_value *val;
647
648 BLD_INSN_1_EX(val, LG2, F32, x, F32);
649 BLD_INSN_2_EX(val, MUL, F32, e, F32, val, F32);
650 val = bld_insn_1(bld, NV_OP_PREEX2, val);
651 val = bld_insn_1(bld, NV_OP_EX2, val);
652
653 return val;
654 }
655
656 static INLINE struct nv_value *
657 bld_load_imm_f32(struct bld_context *bld, float f)
658 {
659 struct nv_value *imm = bld_insn_1(bld, NV_OP_MOV, bld_imm_f32(bld, f));
660
661 SET_TYPE(imm, NV_TYPE_F32);
662 return imm;
663 }
664
665 static INLINE struct nv_value *
666 bld_load_imm_u32(struct bld_context *bld, uint32_t u)
667 {
668 return bld_insn_1(bld, NV_OP_MOV, bld_imm_u32(bld, u));
669 }
670
671 static struct nv_value *
672 bld_get_address(struct bld_context *bld, int id, struct nv_value *indirect)
673 {
674 int i;
675 struct nv_instruction *nvi;
676 struct nv_value *val;
677
678 for (i = 0; i < 4; ++i) {
679 if (!bld->saved_addr[i][0])
680 break;
681 if (bld->saved_addr[i][1] == indirect) {
682 nvi = bld->saved_addr[i][0]->insn;
683 if (nvi->src[0]->value->reg.imm.u32 == id)
684 return bld->saved_addr[i][0];
685 }
686 }
687 i &= 3;
688
689 val = bld_imm_u32(bld, id);
690 if (indirect)
691 val = bld_insn_2(bld, NV_OP_ADD, indirect, val);
692 else
693 val = bld_insn_1(bld, NV_OP_MOV, val);
694
695 bld->saved_addr[i][0] = val;
696 bld->saved_addr[i][0]->reg.file = NV_FILE_ADDR;
697 bld->saved_addr[i][0]->reg.type = NV_TYPE_U16;
698 bld->saved_addr[i][1] = indirect;
699 return bld->saved_addr[i][0];
700 }
701
702
703 static struct nv_value *
704 bld_predicate(struct bld_context *bld, struct nv_value *src, boolean bool_only)
705 {
706 struct nv_instruction *s0i, *nvi = src->insn;
707
708 if (!nvi) {
709 nvi = bld_insn_1(bld,
710 (src->reg.file == NV_FILE_IMM) ? NV_OP_MOV : NV_OP_LDA,
711 src)->insn;
712 src = nvi->def[0];
713 } else
714 if (bool_only) {
715 while (nvi->opcode == NV_OP_ABS || nvi->opcode == NV_OP_NEG ||
716 nvi->opcode == NV_OP_CVT) {
717 s0i = nvi->src[0]->value->insn;
718 if (!s0i || !nv50_op_can_write_flags(s0i->opcode))
719 break;
720 nvi = s0i;
721 assert(!nvi->flags_src);
722 }
723 }
724
725 if (!nv50_op_can_write_flags(nvi->opcode) ||
726 nvi->bb != bld->pc->current_block) {
727 nvi = new_instruction(bld->pc, NV_OP_CVT);
728 nv_reference(bld->pc, &nvi->src[0], src);
729 }
730
731 if (!nvi->flags_def) {
732 nvi->flags_def = new_value(bld->pc, NV_FILE_FLAGS, NV_TYPE_U16);
733 nvi->flags_def->insn = nvi;
734 }
735 return nvi->flags_def;
736 }
737
738 static void
739 bld_kil(struct bld_context *bld, struct nv_value *src)
740 {
741 struct nv_instruction *nvi;
742
743 src = bld_predicate(bld, src, FALSE);
744 nvi = new_instruction(bld->pc, NV_OP_KIL);
745 nvi->fixed = 1;
746 nvi->flags_src = new_ref(bld->pc, src);
747 nvi->cc = NV_CC_LT;
748 }
749
750 static void
751 bld_flow(struct bld_context *bld, uint opcode, ubyte cc,
752 struct nv_value *src, struct nv_basic_block *target,
753 boolean plan_reconverge)
754 {
755 struct nv_instruction *nvi;
756
757 if (plan_reconverge)
758 new_instruction(bld->pc, NV_OP_JOINAT)->fixed = 1;
759
760 nvi = new_instruction(bld->pc, opcode);
761 nvi->is_terminator = 1;
762 nvi->cc = cc;
763 nvi->target = target;
764 if (src)
765 nvi->flags_src = new_ref(bld->pc, src);
766 }
767
768 static ubyte
769 translate_setcc(unsigned opcode)
770 {
771 switch (opcode) {
772 case TGSI_OPCODE_SLT: return NV_CC_LT;
773 case TGSI_OPCODE_SGE: return NV_CC_GE;
774 case TGSI_OPCODE_SEQ: return NV_CC_EQ;
775 case TGSI_OPCODE_SGT: return NV_CC_GT;
776 case TGSI_OPCODE_SLE: return NV_CC_LE;
777 case TGSI_OPCODE_SNE: return NV_CC_NE | NV_CC_U;
778 case TGSI_OPCODE_STR: return NV_CC_TR;
779 case TGSI_OPCODE_SFL: return NV_CC_FL;
780
781 case TGSI_OPCODE_ISLT: return NV_CC_LT;
782 case TGSI_OPCODE_ISGE: return NV_CC_GE;
783 case TGSI_OPCODE_USEQ: return NV_CC_EQ;
784 case TGSI_OPCODE_USGE: return NV_CC_GE;
785 case TGSI_OPCODE_USLT: return NV_CC_LT;
786 case TGSI_OPCODE_USNE: return NV_CC_NE;
787 default:
788 assert(0);
789 return NV_CC_FL;
790 }
791 }
792
793 static uint
794 translate_opcode(uint opcode)
795 {
796 switch (opcode) {
797 case TGSI_OPCODE_ABS: return NV_OP_ABS;
798 case TGSI_OPCODE_ADD:
799 case TGSI_OPCODE_SUB:
800 case TGSI_OPCODE_UADD: return NV_OP_ADD;
801 case TGSI_OPCODE_AND: return NV_OP_AND;
802 case TGSI_OPCODE_EX2: return NV_OP_EX2;
803 case TGSI_OPCODE_CEIL: return NV_OP_CEIL;
804 case TGSI_OPCODE_FLR: return NV_OP_FLOOR;
805 case TGSI_OPCODE_TRUNC: return NV_OP_TRUNC;
806 case TGSI_OPCODE_COS: return NV_OP_COS;
807 case TGSI_OPCODE_SIN: return NV_OP_SIN;
808 case TGSI_OPCODE_DDX: return NV_OP_DFDX;
809 case TGSI_OPCODE_DDY: return NV_OP_DFDY;
810 case TGSI_OPCODE_F2I:
811 case TGSI_OPCODE_F2U:
812 case TGSI_OPCODE_I2F:
813 case TGSI_OPCODE_U2F: return NV_OP_CVT;
814 case TGSI_OPCODE_INEG: return NV_OP_NEG;
815 case TGSI_OPCODE_LG2: return NV_OP_LG2;
816 case TGSI_OPCODE_ISHR:
817 case TGSI_OPCODE_USHR: return NV_OP_SHR;
818 case TGSI_OPCODE_MAD:
819 case TGSI_OPCODE_UMAD: return NV_OP_MAD;
820 case TGSI_OPCODE_MAX:
821 case TGSI_OPCODE_IMAX:
822 case TGSI_OPCODE_UMAX: return NV_OP_MAX;
823 case TGSI_OPCODE_MIN:
824 case TGSI_OPCODE_IMIN:
825 case TGSI_OPCODE_UMIN: return NV_OP_MIN;
826 case TGSI_OPCODE_MUL:
827 case TGSI_OPCODE_UMUL: return NV_OP_MUL;
828 case TGSI_OPCODE_OR: return NV_OP_OR;
829 case TGSI_OPCODE_RCP: return NV_OP_RCP;
830 case TGSI_OPCODE_RSQ: return NV_OP_RSQ;
831 case TGSI_OPCODE_SAD: return NV_OP_SAD;
832 case TGSI_OPCODE_SHL: return NV_OP_SHL;
833 case TGSI_OPCODE_SLT:
834 case TGSI_OPCODE_SGE:
835 case TGSI_OPCODE_SEQ:
836 case TGSI_OPCODE_SGT:
837 case TGSI_OPCODE_SLE:
838 case TGSI_OPCODE_SNE:
839 case TGSI_OPCODE_ISLT:
840 case TGSI_OPCODE_ISGE:
841 case TGSI_OPCODE_USEQ:
842 case TGSI_OPCODE_USGE:
843 case TGSI_OPCODE_USLT:
844 case TGSI_OPCODE_USNE: return NV_OP_SET;
845 case TGSI_OPCODE_TEX: return NV_OP_TEX;
846 case TGSI_OPCODE_TXP: return NV_OP_TEX;
847 case TGSI_OPCODE_TXB: return NV_OP_TXB;
848 case TGSI_OPCODE_TXL: return NV_OP_TXL;
849 case TGSI_OPCODE_XOR: return NV_OP_XOR;
850 default:
851 return NV_OP_NOP;
852 }
853 }
854
855 static ubyte
856 infer_src_type(unsigned opcode)
857 {
858 switch (opcode) {
859 case TGSI_OPCODE_MOV:
860 case TGSI_OPCODE_AND:
861 case TGSI_OPCODE_OR:
862 case TGSI_OPCODE_XOR:
863 case TGSI_OPCODE_SAD:
864 case TGSI_OPCODE_U2F:
865 case TGSI_OPCODE_UADD:
866 case TGSI_OPCODE_UDIV:
867 case TGSI_OPCODE_UMOD:
868 case TGSI_OPCODE_UMAD:
869 case TGSI_OPCODE_UMUL:
870 case TGSI_OPCODE_UMAX:
871 case TGSI_OPCODE_UMIN:
872 case TGSI_OPCODE_USEQ:
873 case TGSI_OPCODE_USGE:
874 case TGSI_OPCODE_USLT:
875 case TGSI_OPCODE_USNE:
876 case TGSI_OPCODE_USHR:
877 return NV_TYPE_U32;
878 case TGSI_OPCODE_I2F:
879 case TGSI_OPCODE_IDIV:
880 case TGSI_OPCODE_IMAX:
881 case TGSI_OPCODE_IMIN:
882 case TGSI_OPCODE_INEG:
883 case TGSI_OPCODE_ISGE:
884 case TGSI_OPCODE_ISHR:
885 case TGSI_OPCODE_ISLT:
886 return NV_TYPE_S32;
887 default:
888 return NV_TYPE_F32;
889 }
890 }
891
892 static ubyte
893 infer_dst_type(unsigned opcode)
894 {
895 switch (opcode) {
896 case TGSI_OPCODE_MOV:
897 case TGSI_OPCODE_F2U:
898 case TGSI_OPCODE_AND:
899 case TGSI_OPCODE_OR:
900 case TGSI_OPCODE_XOR:
901 case TGSI_OPCODE_SAD:
902 case TGSI_OPCODE_UADD:
903 case TGSI_OPCODE_UDIV:
904 case TGSI_OPCODE_UMOD:
905 case TGSI_OPCODE_UMAD:
906 case TGSI_OPCODE_UMUL:
907 case TGSI_OPCODE_UMAX:
908 case TGSI_OPCODE_UMIN:
909 case TGSI_OPCODE_USEQ:
910 case TGSI_OPCODE_USGE:
911 case TGSI_OPCODE_USLT:
912 case TGSI_OPCODE_USNE:
913 case TGSI_OPCODE_USHR:
914 return NV_TYPE_U32;
915 case TGSI_OPCODE_F2I:
916 case TGSI_OPCODE_IDIV:
917 case TGSI_OPCODE_IMAX:
918 case TGSI_OPCODE_IMIN:
919 case TGSI_OPCODE_INEG:
920 case TGSI_OPCODE_ISGE:
921 case TGSI_OPCODE_ISHR:
922 case TGSI_OPCODE_ISLT:
923 return NV_TYPE_S32;
924 default:
925 return NV_TYPE_F32;
926 }
927 }
928
929 static void
930 emit_store(struct bld_context *bld, const struct tgsi_full_instruction *inst,
931 unsigned chan, struct nv_value *value)
932 {
933 struct nv_value *ptr;
934 const struct tgsi_full_dst_register *reg = &inst->Dst[0];
935
936 if (reg->Register.Indirect) {
937 ptr = FETCH_ADDR(reg->Indirect.Index,
938 tgsi_util_get_src_register_swizzle(&reg->Indirect, 0));
939 } else {
940 ptr = NULL;
941 }
942
943 assert(chan < 4);
944
945 if (inst->Instruction.Opcode != TGSI_OPCODE_MOV)
946 value->reg.type = infer_dst_type(inst->Instruction.Opcode);
947
948 switch (inst->Instruction.Saturate) {
949 case TGSI_SAT_NONE:
950 break;
951 case TGSI_SAT_ZERO_ONE:
952 BLD_INSN_1_EX(value, SAT, F32, value, F32);
953 break;
954 case TGSI_SAT_MINUS_PLUS_ONE:
955 value->reg.as_type = NV_TYPE_F32;
956 value = bld_insn_2(bld, NV_OP_MAX, value, bld_load_imm_f32(bld, -1.0f));
957 value = bld_insn_2(bld, NV_OP_MIN, value, bld_load_imm_f32(bld, 1.0f));
958 break;
959 }
960
961 switch (reg->Register.File) {
962 case TGSI_FILE_OUTPUT:
963 if (!value->insn && (bld->ti->output_file == NV_FILE_OUT))
964 value = bld_insn_1(bld, NV_OP_MOV, value);
965 value = bld_insn_1(bld, NV_OP_MOV, value);
966 value->reg.file = bld->ti->output_file;
967
968 if (bld->ti->p->type == PIPE_SHADER_FRAGMENT) {
969 STORE_OUTR(reg->Register.Index, chan, value);
970 } else {
971 value->insn->fixed = 1;
972 value->reg.id = bld->ti->output_map[reg->Register.Index][chan];
973 }
974 break;
975 case TGSI_FILE_TEMPORARY:
976 assert(reg->Register.Index < BLD_MAX_TEMPS);
977 if (!value->insn || (value->insn->bb != bld->pc->current_block))
978 value = bld_insn_1(bld, NV_OP_MOV, value);
979 value->reg.file = NV_FILE_GPR;
980
981 if (bld->ti->store_to_memory)
982 bld_lmem_store(bld, ptr, reg->Register.Index * 4 + chan, value);
983 else
984 STORE_TEMP(reg->Register.Index, chan, value);
985 break;
986 case TGSI_FILE_ADDRESS:
987 assert(reg->Register.Index < BLD_MAX_ADDRS);
988 value->reg.file = NV_FILE_ADDR;
989 value->reg.type = NV_TYPE_U16;
990 STORE_ADDR(reg->Register.Index, chan, value);
991 break;
992 }
993 }
994
995 static INLINE uint32_t
996 bld_is_output_written(struct bld_context *bld, int i, int c)
997 {
998 if (c < 0)
999 return bld->outputs_written[i / 8] & (0xf << ((i * 4) % 32));
1000 return bld->outputs_written[i / 8] & (1 << ((i * 4 + c) % 32));
1001 }
1002
1003 static void
1004 bld_export_outputs(struct bld_context *bld)
1005 {
1006 struct nv_value *vals[4];
1007 struct nv_instruction *nvi;
1008 int i, c, n;
1009
1010 bld_push_values(&bld->ovs[0][0], PIPE_MAX_SHADER_OUTPUTS);
1011
1012 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; ++i) {
1013 if (!bld_is_output_written(bld, i, -1))
1014 continue;
1015 for (n = 0, c = 0; c < 4; ++c) {
1016 if (!bld_is_output_written(bld, i, c))
1017 continue;
1018 vals[n] = bld_fetch_global(bld, &bld->ovs[i][c]);
1019 assert(vals[n]);
1020 vals[n] = bld_insn_1(bld, NV_OP_MOV, vals[n]);
1021 vals[n++]->reg.id = bld->ti->output_map[i][c];
1022 }
1023 assert(n);
1024
1025 (nvi = new_instruction(bld->pc, NV_OP_EXPORT))->fixed = 1;
1026
1027 for (c = 0; c < n; ++c)
1028 nvi->src[c] = new_ref(bld->pc, vals[c]);
1029 }
1030 }
1031
1032 static void
1033 bld_new_block(struct bld_context *bld, struct nv_basic_block *b)
1034 {
1035 int i;
1036
1037 bld_push_values(&bld->tvs[0][0], BLD_MAX_TEMPS);
1038 bld_push_values(&bld->avs[0][0], BLD_MAX_ADDRS);
1039 bld_push_values(&bld->pvs[0][0], BLD_MAX_PREDS);
1040 bld_push_values(&bld->ovs[0][0], PIPE_MAX_SHADER_OUTPUTS);
1041
1042 bld->pc->current_block = b;
1043
1044 for (i = 0; i < 4; ++i)
1045 bld->saved_addr[i][0] = NULL;
1046
1047 for (i = 0; i < 128; ++i)
1048 bld->saved_inputs[i] = NULL;
1049
1050 bld->out_kind = CFG_EDGE_FORWARD;
1051 }
1052
1053 static struct nv_value *
1054 bld_saved_input(struct bld_context *bld, unsigned i, unsigned c)
1055 {
1056 unsigned idx = bld->ti->input_map[i][c];
1057
1058 if (bld->ti->p->type != PIPE_SHADER_FRAGMENT)
1059 return NULL;
1060 if (bld->saved_inputs[idx])
1061 return bld->saved_inputs[idx];
1062 return NULL;
1063 }
1064
1065 static struct nv_value *
1066 bld_interpolate(struct bld_context *bld, unsigned mode, struct nv_value *val)
1067 {
1068 if (val->reg.id == 255) {
1069 /* gl_FrontFacing: 0/~0 to -1.0/+1.0 */
1070 val = bld_insn_1(bld, NV_OP_LINTERP, val);
1071 val = bld_insn_2(bld, NV_OP_SHL, val, bld_imm_u32(bld, 31));
1072 val->insn->src[0]->typecast = NV_TYPE_U32;
1073 val = bld_insn_2(bld, NV_OP_XOR, val, bld_imm_f32(bld, -1.0f));
1074 val->insn->src[0]->typecast = NV_TYPE_U32;
1075 } else
1076 if (mode & (NV50_INTERP_LINEAR | NV50_INTERP_FLAT))
1077 val = bld_insn_1(bld, NV_OP_LINTERP, val);
1078 else
1079 val = bld_insn_2(bld, NV_OP_PINTERP, val, bld->frgcrd[3]);
1080
1081 val->insn->flat = (mode & NV50_INTERP_FLAT) ? 1 : 0;
1082 val->insn->centroid = (mode & NV50_INTERP_CENTROID) ? 1 : 0;
1083 return val;
1084 }
1085
1086 static struct nv_value *
1087 emit_fetch(struct bld_context *bld, const struct tgsi_full_instruction *insn,
1088 const unsigned s, const unsigned chan)
1089 {
1090 const struct tgsi_full_src_register *src = &insn->Src[s];
1091 struct nv_value *res;
1092 struct nv_value *ptr = NULL;
1093 unsigned idx, swz, dim_idx, ind_idx, ind_swz, sgn;
1094 ubyte type = infer_src_type(insn->Instruction.Opcode);
1095
1096 idx = src->Register.Index;
1097 swz = tgsi_util_get_full_src_register_swizzle(src, chan);
1098 dim_idx = -1;
1099 ind_idx = -1;
1100 ind_swz = 0;
1101
1102 if (src->Register.Indirect) {
1103 ind_idx = src->Indirect.Index;
1104 ind_swz = tgsi_util_get_src_register_swizzle(&src->Indirect, 0);
1105
1106 ptr = FETCH_ADDR(ind_idx, ind_swz);
1107 }
1108 if (idx >= (128 / 4) && src->Register.File == TGSI_FILE_CONSTANT)
1109 ptr = bld_get_address(bld, (idx * 16) & ~0x1ff, ptr);
1110
1111 switch (src->Register.File) {
1112 case TGSI_FILE_CONSTANT:
1113 dim_idx = src->Dimension.Index;
1114 assert(dim_idx < 15);
1115
1116 res = new_value(bld->pc, NV_FILE_MEM_C(dim_idx), type);
1117 SET_TYPE(res, type);
1118 res->reg.id = (idx * 4 + swz) & 127;
1119 res = bld_insn_1(bld, NV_OP_LDA, res);
1120
1121 if (ptr)
1122 res->insn->src[4] = new_ref(bld->pc, ptr);
1123 break;
1124 case TGSI_FILE_IMMEDIATE:
1125 assert(idx < bld->ti->immd32_nr);
1126 res = bld_load_imm_u32(bld, bld->ti->immd32[idx * 4 + swz]);
1127
1128 switch (bld->ti->immd32_ty[idx]) {
1129 case TGSI_IMM_FLOAT32: SET_TYPE(res, NV_TYPE_F32); break;
1130 case TGSI_IMM_UINT32: SET_TYPE(res, NV_TYPE_U32); break;
1131 case TGSI_IMM_INT32: SET_TYPE(res, NV_TYPE_S32); break;
1132 default:
1133 SET_TYPE(res, type);
1134 break;
1135 }
1136 break;
1137 case TGSI_FILE_INPUT:
1138 res = bld_saved_input(bld, idx, swz);
1139 if (res && (insn->Instruction.Opcode != TGSI_OPCODE_TXP))
1140 break;
1141
1142 res = new_value(bld->pc, bld->ti->input_file, type);
1143 res->reg.id = bld->ti->input_map[idx][swz];
1144
1145 if (res->reg.file == NV_FILE_MEM_V) {
1146 res = bld_interpolate(bld, bld->ti->interp_mode[idx], res);
1147 } else {
1148 assert(src->Dimension.Dimension == 0);
1149 res = bld_insn_1(bld, NV_OP_LDA, res);
1150 assert(res->reg.type == type);
1151 }
1152 bld->saved_inputs[bld->ti->input_map[idx][swz]] = res;
1153 break;
1154 case TGSI_FILE_TEMPORARY:
1155 if (bld->ti->store_to_memory)
1156 res = bld_lmem_load(bld, ptr, idx * 4 + swz);
1157 else
1158 res = bld_fetch_global(bld, &bld->tvs[idx][swz]);
1159 break;
1160 case TGSI_FILE_ADDRESS:
1161 res = bld_fetch_global(bld, &bld->avs[idx][swz]);
1162 break;
1163 case TGSI_FILE_PREDICATE:
1164 res = bld_fetch_global(bld, &bld->pvs[idx][swz]);
1165 break;
1166 case TGSI_FILE_SYSTEM_VALUE:
1167 res = new_value(bld->pc, bld->ti->input_file, NV_TYPE_U32);
1168 res->reg.id = bld->ti->sysval_map[idx];
1169 res = bld_insn_1(bld, NV_OP_LDA, res);
1170 res = bld_insn_1(bld, NV_OP_CVT, res);
1171 res->reg.type = NV_TYPE_F32;
1172 break;
1173 default:
1174 NOUVEAU_ERR("illegal/unhandled src reg file: %d\n", src->Register.File);
1175 abort();
1176 break;
1177 }
1178 if (!res)
1179 return bld_undef(bld, NV_FILE_GPR);
1180
1181 sgn = tgsi_util_get_full_src_register_sign_mode(src, chan);
1182
1183 if (insn->Instruction.Opcode != TGSI_OPCODE_MOV)
1184 res->reg.as_type = type;
1185 else
1186 if (sgn != TGSI_UTIL_SIGN_KEEP) /* apparently "MOV A, -B" assumes float */
1187 res->reg.as_type = NV_TYPE_F32;
1188
1189 switch (sgn) {
1190 case TGSI_UTIL_SIGN_KEEP:
1191 break;
1192 case TGSI_UTIL_SIGN_CLEAR:
1193 res = bld_insn_1(bld, NV_OP_ABS, res);
1194 break;
1195 case TGSI_UTIL_SIGN_TOGGLE:
1196 res = bld_insn_1(bld, NV_OP_NEG, res);
1197 break;
1198 case TGSI_UTIL_SIGN_SET:
1199 res = bld_insn_1(bld, NV_OP_ABS, res);
1200 res = bld_insn_1(bld, NV_OP_NEG, res);
1201 break;
1202 default:
1203 NOUVEAU_ERR("illegal/unhandled src reg sign mode\n");
1204 abort();
1205 break;
1206 }
1207
1208 return res;
1209 }
1210
1211 static void
1212 bld_lit(struct bld_context *bld, struct nv_value *dst0[4],
1213 const struct tgsi_full_instruction *insn)
1214 {
1215 struct nv_value *val0 = NULL;
1216 struct nv_value *zero = NULL;
1217 unsigned mask = insn->Dst[0].Register.WriteMask;
1218
1219 if (mask & ((1 << 0) | (1 << 3)))
1220 dst0[3] = dst0[0] = bld_load_imm_f32(bld, 1.0f);
1221
1222 if (mask & (3 << 1)) {
1223 zero = bld_load_imm_f32(bld, 0.0f);
1224 val0 = bld_insn_2(bld, NV_OP_MAX, emit_fetch(bld, insn, 0, 0), zero);
1225
1226 if (mask & (1 << 1))
1227 dst0[1] = val0;
1228 }
1229
1230 if (mask & (1 << 2)) {
1231 struct nv_value *val1, *val3, *src1, *src3;
1232 struct nv_value *pos128 = bld_load_imm_f32(bld, 127.999999f);
1233 struct nv_value *neg128 = bld_load_imm_f32(bld, -127.999999f);
1234
1235 src1 = emit_fetch(bld, insn, 0, 1);
1236 src3 = emit_fetch(bld, insn, 0, 3);
1237
1238 val0->insn->flags_def = new_value(bld->pc, NV_FILE_FLAGS, NV_TYPE_U16);
1239 val0->insn->flags_def->insn = val0->insn;
1240
1241 val1 = bld_insn_2(bld, NV_OP_MAX, src1, zero);
1242 val3 = bld_insn_2(bld, NV_OP_MAX, src3, neg128);
1243 val3 = bld_insn_2(bld, NV_OP_MIN, val3, pos128);
1244 val3 = bld_pow(bld, val1, val3);
1245
1246 dst0[2] = bld_insn_1(bld, NV_OP_MOV, zero);
1247 dst0[2]->insn->cc = NV_CC_LE;
1248 dst0[2]->insn->flags_src = new_ref(bld->pc, val0->insn->flags_def);
1249
1250 dst0[2] = bld_insn_2(bld, NV_OP_SELECT, val3, dst0[2]);
1251 }
1252 }
1253
1254 static INLINE void
1255 get_tex_dim(const struct tgsi_full_instruction *insn, int *dim, int *arg)
1256 {
1257 switch (insn->Texture.Texture) {
1258 case TGSI_TEXTURE_1D:
1259 *arg = *dim = 1;
1260 break;
1261 case TGSI_TEXTURE_SHADOW1D:
1262 *dim = 1;
1263 *arg = 2;
1264 break;
1265 case TGSI_TEXTURE_UNKNOWN:
1266 case TGSI_TEXTURE_2D:
1267 case TGSI_TEXTURE_RECT:
1268 *arg = *dim = 2;
1269 break;
1270 case TGSI_TEXTURE_SHADOW2D:
1271 case TGSI_TEXTURE_SHADOWRECT:
1272 *dim = 2;
1273 *arg = 3;
1274 break;
1275 case TGSI_TEXTURE_3D:
1276 case TGSI_TEXTURE_CUBE:
1277 *dim = *arg = 3;
1278 break;
1279 default:
1280 assert(0);
1281 break;
1282 }
1283 }
1284
1285 static void
1286 load_proj_tex_coords(struct bld_context *bld,
1287 struct nv_value *t[4], int dim, int arg,
1288 const struct tgsi_full_instruction *insn)
1289 {
1290 int c, mask;
1291
1292 mask = (1 << dim) - 1;
1293 if (arg != dim)
1294 mask |= 4; /* depth comparison value */
1295
1296 t[3] = emit_fetch(bld, insn, 0, 3);
1297
1298 if (t[3]->insn->opcode == NV_OP_PINTERP) {
1299 t[3] = bld_duplicate_insn(bld, t[3]->insn);
1300 t[3]->insn->opcode = NV_OP_LINTERP;
1301 nv_reference(bld->pc, &t[3]->insn->src[1], NULL);
1302 }
1303
1304 t[3] = bld_insn_1(bld, NV_OP_RCP, t[3]);
1305
1306 for (c = 0; c < 4; ++c) {
1307 if (!(mask & (1 << c)))
1308 continue;
1309 t[c] = emit_fetch(bld, insn, 0, c);
1310
1311 if (t[c]->insn->opcode != NV_OP_LINTERP &&
1312 t[c]->insn->opcode != NV_OP_PINTERP)
1313 continue;
1314 t[c] = bld_duplicate_insn(bld, t[c]->insn);
1315 t[c]->insn->opcode = NV_OP_PINTERP;
1316 nv_reference(bld->pc, &t[c]->insn->src[1], t[3]);
1317
1318 mask &= ~(1 << c);
1319 }
1320
1321 for (c = 0; mask; ++c, mask >>= 1) {
1322 if (!(mask & 1))
1323 continue;
1324 t[c] = bld_insn_2(bld, NV_OP_MUL, t[c], t[3]);
1325 }
1326 }
1327
1328 /* For a quad of threads / top left, top right, bottom left, bottom right
1329 * pixels, do a different operation, and take src0 from a specific thread.
1330 */
1331 #define QOP_ADD 0
1332 #define QOP_SUBR 1
1333 #define QOP_SUB 2
1334 #define QOP_MOV1 3
1335
1336 #define QOP(a, b, c, d) \
1337 ((QOP_##a << 0) | (QOP_##b << 2) | (QOP_##c << 4) | (QOP_##d << 6))
1338
1339 static INLINE struct nv_value *
1340 bld_quadop(struct bld_context *bld, ubyte qop, struct nv_value *src0, int lane,
1341 struct nv_value *src1, boolean wp)
1342 {
1343 struct nv_value *val = bld_insn_2(bld, NV_OP_QUADOP, src0, src1);
1344 val->insn->lanes = lane;
1345 val->insn->quadop = qop;
1346 if (wp) {
1347 val->insn->flags_def = new_value(bld->pc, NV_FILE_FLAGS, NV_TYPE_U16);
1348 val->insn->flags_def->insn = val->insn;
1349 }
1350 return val;
1351 }
1352
1353 static INLINE struct nv_value *
1354 bld_cmov(struct bld_context *bld,
1355 struct nv_value *src, ubyte cc, struct nv_value *cr)
1356 {
1357 src = bld_insn_1(bld, NV_OP_MOV, src);
1358
1359 src->insn->cc = cc;
1360 src->insn->flags_src = new_ref(bld->pc, cr);
1361
1362 return src;
1363 }
1364
1365 static struct nv_instruction *
1366 emit_tex(struct bld_context *bld, uint opcode,
1367 struct nv_value *dst[4], struct nv_value *t_in[4],
1368 int argc, int tic, int tsc, int cube)
1369 {
1370 struct nv_value *t[4];
1371 struct nv_instruction *nvi;
1372 int c;
1373
1374 /* the inputs to a tex instruction must be separate values */
1375 for (c = 0; c < argc; ++c) {
1376 t[c] = bld_insn_1(bld, NV_OP_MOV, t_in[c]);
1377 SET_TYPE(t[c], NV_TYPE_F32);
1378 t[c]->insn->fixed = 1;
1379 }
1380
1381 nvi = new_instruction(bld->pc, opcode);
1382
1383 for (c = 0; c < 4; ++c)
1384 dst[c] = bld_def(nvi, c, new_value(bld->pc, NV_FILE_GPR, NV_TYPE_F32));
1385
1386 for (c = 0; c < argc; ++c)
1387 nvi->src[c] = new_ref(bld->pc, t[c]);
1388
1389 nvi->tex_t = tic;
1390 nvi->tex_s = tsc;
1391 nvi->tex_mask = 0xf;
1392 nvi->tex_cube = cube;
1393 nvi->tex_live = 0;
1394 nvi->tex_argc = argc;
1395
1396 return nvi;
1397 }
1398
1399 static void
1400 bld_texlod_sequence(struct bld_context *bld,
1401 struct nv_value *dst[4], struct nv_value *t[4], int arg,
1402 int tic, int tsc, int cube)
1403 {
1404 emit_tex(bld, NV_OP_TXL, dst, t, arg, tic, tsc, cube); /* TODO */
1405 }
1406
1407
1408 /* The lanes of a quad are grouped by the bit in the condition register
1409 * they have set, which is selected by differing bias values.
1410 * Move the input values for TEX into a new register set for each group
1411 * and execute TEX only for a specific group.
1412 * We always need to use 4 new registers for the inputs/outputs because
1413 * the implicitly calculated derivatives must be correct.
1414 */
1415 static void
1416 bld_texbias_sequence(struct bld_context *bld,
1417 struct nv_value *dst[4], struct nv_value *t[4], int arg,
1418 int tic, int tsc, int cube)
1419 {
1420 struct nv_instruction *sel, *tex;
1421 struct nv_value *bit[4], *cr[4], *res[4][4], *val;
1422 int l, c;
1423
1424 const ubyte cc[4] = { NV_CC_EQ, NV_CC_S, NV_CC_C, NV_CC_O };
1425
1426 for (l = 0; l < 4; ++l) {
1427 bit[l] = bld_load_imm_u32(bld, 1 << l);
1428
1429 val = bld_quadop(bld, QOP(SUBR, SUBR, SUBR, SUBR),
1430 t[arg - 1], l, t[arg - 1], TRUE);
1431
1432 cr[l] = bld_cmov(bld, bit[l], NV_CC_EQ, val->insn->flags_def);
1433
1434 cr[l]->reg.file = NV_FILE_FLAGS;
1435 SET_TYPE(cr[l], NV_TYPE_U16);
1436 }
1437
1438 sel = new_instruction(bld->pc, NV_OP_SELECT);
1439
1440 for (l = 0; l < 4; ++l)
1441 sel->src[l] = new_ref(bld->pc, cr[l]);
1442
1443 bld_def(sel, 0, new_value(bld->pc, NV_FILE_FLAGS, NV_TYPE_U16));
1444
1445 for (l = 0; l < 4; ++l) {
1446 tex = emit_tex(bld, NV_OP_TXB, dst, t, arg, tic, tsc, cube);
1447
1448 tex->cc = cc[l];
1449 tex->flags_src = new_ref(bld->pc, sel->def[0]);
1450
1451 for (c = 0; c < 4; ++c)
1452 res[l][c] = tex->def[c];
1453 }
1454
1455 for (l = 0; l < 4; ++l)
1456 for (c = 0; c < 4; ++c)
1457 res[l][c] = bld_cmov(bld, res[l][c], cc[l], sel->def[0]);
1458
1459 for (c = 0; c < 4; ++c) {
1460 sel = new_instruction(bld->pc, NV_OP_SELECT);
1461
1462 for (l = 0; l < 4; ++l)
1463 sel->src[l] = new_ref(bld->pc, res[l][c]);
1464
1465 bld_def(sel, 0, (dst[c] = new_value(bld->pc, NV_FILE_GPR, NV_TYPE_F32)));
1466 }
1467 }
1468
1469 static boolean
1470 bld_is_constant(struct nv_value *val)
1471 {
1472 if (val->reg.file == NV_FILE_IMM)
1473 return TRUE;
1474 return val->insn && nvcg_find_constant(val->insn->src[0]);
1475 }
1476
1477 static void
1478 bld_tex(struct bld_context *bld, struct nv_value *dst0[4],
1479 const struct tgsi_full_instruction *insn)
1480 {
1481 struct nv_value *t[4], *s[3];
1482 uint opcode = translate_opcode(insn->Instruction.Opcode);
1483 int arg, dim, c;
1484 const int tic = insn->Src[1].Register.Index;
1485 const int tsc = tic;
1486 const int cube = (insn->Texture.Texture == TGSI_TEXTURE_CUBE) ? 1 : 0;
1487
1488 get_tex_dim(insn, &dim, &arg);
1489
1490 if (!cube && insn->Instruction.Opcode == TGSI_OPCODE_TXP)
1491 load_proj_tex_coords(bld, t, dim, arg, insn);
1492 else {
1493 for (c = 0; c < dim; ++c)
1494 t[c] = emit_fetch(bld, insn, 0, c);
1495 if (arg != dim)
1496 t[dim] = emit_fetch(bld, insn, 0, 2);
1497 }
1498
1499 if (cube) {
1500 assert(dim >= 3);
1501 for (c = 0; c < 3; ++c)
1502 s[c] = bld_insn_1(bld, NV_OP_ABS, t[c]);
1503
1504 s[0] = bld_insn_2(bld, NV_OP_MAX, s[0], s[1]);
1505 s[0] = bld_insn_2(bld, NV_OP_MAX, s[0], s[2]);
1506 s[0] = bld_insn_1(bld, NV_OP_RCP, s[0]);
1507
1508 for (c = 0; c < 3; ++c)
1509 t[c] = bld_insn_2(bld, NV_OP_MUL, t[c], s[0]);
1510 }
1511
1512 if (opcode == NV_OP_TXB || opcode == NV_OP_TXL) {
1513 t[arg++] = emit_fetch(bld, insn, 0, 3);
1514
1515 if ((bld->ti->p->type == PIPE_SHADER_FRAGMENT) &&
1516 !bld_is_constant(t[arg - 1])) {
1517 if (opcode == NV_OP_TXB)
1518 bld_texbias_sequence(bld, dst0, t, arg, tic, tsc, cube);
1519 else
1520 bld_texlod_sequence(bld, dst0, t, arg, tic, tsc, cube);
1521 return;
1522 }
1523 }
1524
1525 emit_tex(bld, opcode, dst0, t, arg, tic, tsc, cube);
1526 }
1527
1528 static INLINE struct nv_value *
1529 bld_dot(struct bld_context *bld, const struct tgsi_full_instruction *insn,
1530 int n)
1531 {
1532 struct nv_value *dotp, *src0, *src1;
1533 int c;
1534
1535 src0 = emit_fetch(bld, insn, 0, 0);
1536 src1 = emit_fetch(bld, insn, 1, 0);
1537 dotp = bld_insn_2(bld, NV_OP_MUL, src0, src1);
1538
1539 for (c = 1; c < n; ++c) {
1540 src0 = emit_fetch(bld, insn, 0, c);
1541 src1 = emit_fetch(bld, insn, 1, c);
1542 dotp = bld_insn_3(bld, NV_OP_MAD, src0, src1, dotp);
1543 }
1544 return dotp;
1545 }
1546
1547 #define FOR_EACH_DST0_ENABLED_CHANNEL(chan, inst) \
1548 for (chan = 0; chan < 4; ++chan) \
1549 if ((inst)->Dst[0].Register.WriteMask & (1 << chan))
1550
1551 static void
1552 bld_instruction(struct bld_context *bld,
1553 const struct tgsi_full_instruction *insn)
1554 {
1555 struct nv50_program *prog = bld->ti->p;
1556 const struct tgsi_full_dst_register *dreg = &insn->Dst[0];
1557 struct nv_value *src0;
1558 struct nv_value *src1;
1559 struct nv_value *src2;
1560 struct nv_value *dst0[4] = { 0 };
1561 struct nv_value *temp;
1562 int c;
1563 uint opcode = translate_opcode(insn->Instruction.Opcode);
1564
1565 #if NV50_DEBUG & NV50_DEBUG_PROG_IR
1566 debug_printf("bld_instruction:"); tgsi_dump_instruction(insn, 1);
1567 #endif
1568
1569 switch (insn->Instruction.Opcode) {
1570 case TGSI_OPCODE_ADD:
1571 case TGSI_OPCODE_MAX:
1572 case TGSI_OPCODE_MIN:
1573 case TGSI_OPCODE_MUL:
1574 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1575 src0 = emit_fetch(bld, insn, 0, c);
1576 src1 = emit_fetch(bld, insn, 1, c);
1577 dst0[c] = bld_insn_2(bld, opcode, src0, src1);
1578 }
1579 break;
1580 case TGSI_OPCODE_ARL:
1581 src1 = bld_imm_u32(bld, 4);
1582 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1583 src0 = emit_fetch(bld, insn, 0, c);
1584 temp = bld_insn_1(bld, NV_OP_FLOOR, src0);
1585 SET_TYPE(temp, NV_TYPE_S32);
1586 dst0[c] = bld_insn_2(bld, NV_OP_SHL, temp, src1);
1587 }
1588 break;
1589 case TGSI_OPCODE_CMP:
1590 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1591 src0 = emit_fetch(bld, insn, 0, c);
1592 src1 = emit_fetch(bld, insn, 1, c);
1593 src2 = emit_fetch(bld, insn, 2, c);
1594 src0 = bld_predicate(bld, src0, FALSE);
1595
1596 src1 = bld_insn_1(bld, NV_OP_MOV, src1);
1597 src1->insn->flags_src = new_ref(bld->pc, src0);
1598 src1->insn->cc = NV_CC_LT;
1599
1600 src2 = bld_insn_1(bld, NV_OP_MOV, src2);
1601 src2->insn->flags_src = new_ref(bld->pc, src0);
1602 src2->insn->cc = NV_CC_GE;
1603
1604 dst0[c] = bld_insn_2(bld, NV_OP_SELECT, src1, src2);
1605 }
1606 break;
1607 case TGSI_OPCODE_COS:
1608 case TGSI_OPCODE_SIN:
1609 src0 = emit_fetch(bld, insn, 0, 0);
1610 temp = bld_insn_1(bld, NV_OP_PRESIN, src0);
1611 if (insn->Dst[0].Register.WriteMask & 7)
1612 temp = bld_insn_1(bld, opcode, temp);
1613 for (c = 0; c < 3; ++c)
1614 if (insn->Dst[0].Register.WriteMask & (1 << c))
1615 dst0[c] = temp;
1616 if (!(insn->Dst[0].Register.WriteMask & (1 << 3)))
1617 break;
1618 src0 = emit_fetch(bld, insn, 0, 3);
1619 temp = bld_insn_1(bld, NV_OP_PRESIN, src0);
1620 dst0[3] = bld_insn_1(bld, opcode, temp);
1621 break;
1622 case TGSI_OPCODE_DP2:
1623 temp = bld_dot(bld, insn, 2);
1624 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1625 dst0[c] = temp;
1626 break;
1627 case TGSI_OPCODE_DP3:
1628 temp = bld_dot(bld, insn, 3);
1629 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1630 dst0[c] = temp;
1631 break;
1632 case TGSI_OPCODE_DP4:
1633 temp = bld_dot(bld, insn, 4);
1634 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1635 dst0[c] = temp;
1636 break;
1637 case TGSI_OPCODE_DPH:
1638 src0 = bld_dot(bld, insn, 3);
1639 src1 = emit_fetch(bld, insn, 1, 3);
1640 temp = bld_insn_2(bld, NV_OP_ADD, src0, src1);
1641 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1642 dst0[c] = temp;
1643 break;
1644 case TGSI_OPCODE_DST:
1645 if (insn->Dst[0].Register.WriteMask & 1)
1646 dst0[0] = bld_imm_f32(bld, 1.0f);
1647 if (insn->Dst[0].Register.WriteMask & 2) {
1648 src0 = emit_fetch(bld, insn, 0, 1);
1649 src1 = emit_fetch(bld, insn, 1, 1);
1650 dst0[1] = bld_insn_2(bld, NV_OP_MUL, src0, src1);
1651 }
1652 if (insn->Dst[0].Register.WriteMask & 4)
1653 dst0[2] = emit_fetch(bld, insn, 0, 2);
1654 if (insn->Dst[0].Register.WriteMask & 8)
1655 dst0[3] = emit_fetch(bld, insn, 1, 3);
1656 break;
1657 case TGSI_OPCODE_EXP:
1658 src0 = emit_fetch(bld, insn, 0, 0);
1659 temp = bld_insn_1(bld, NV_OP_FLOOR, src0);
1660
1661 if (insn->Dst[0].Register.WriteMask & 2)
1662 dst0[1] = bld_insn_2(bld, NV_OP_SUB, src0, temp);
1663 if (insn->Dst[0].Register.WriteMask & 1) {
1664 temp = bld_insn_1(bld, NV_OP_PREEX2, temp);
1665 dst0[0] = bld_insn_1(bld, NV_OP_EX2, temp);
1666 }
1667 if (insn->Dst[0].Register.WriteMask & 4) {
1668 temp = bld_insn_1(bld, NV_OP_PREEX2, src0);
1669 dst0[2] = bld_insn_1(bld, NV_OP_EX2, temp);
1670 }
1671 if (insn->Dst[0].Register.WriteMask & 8)
1672 dst0[3] = bld_imm_f32(bld, 1.0f);
1673 break;
1674 case TGSI_OPCODE_EX2:
1675 src0 = emit_fetch(bld, insn, 0, 0);
1676 temp = bld_insn_1(bld, NV_OP_PREEX2, src0);
1677 temp = bld_insn_1(bld, NV_OP_EX2, temp);
1678 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1679 dst0[c] = temp;
1680 break;
1681 case TGSI_OPCODE_FRC:
1682 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1683 src0 = emit_fetch(bld, insn, 0, c);
1684 dst0[c] = bld_insn_1(bld, NV_OP_FLOOR, src0);
1685 dst0[c] = bld_insn_2(bld, NV_OP_SUB, src0, dst0[c]);
1686 }
1687 break;
1688 case TGSI_OPCODE_KIL:
1689 for (c = 0; c < 4; ++c) {
1690 src0 = emit_fetch(bld, insn, 0, c);
1691 bld_kil(bld, src0);
1692 }
1693 break;
1694 case TGSI_OPCODE_KILP:
1695 (new_instruction(bld->pc, NV_OP_KIL))->fixed = 1;
1696 break;
1697 case TGSI_OPCODE_IF:
1698 {
1699 struct nv_basic_block *b = new_basic_block(bld->pc);
1700
1701 assert(bld->cond_lvl < BLD_MAX_COND_NESTING);
1702
1703 nvbb_attach_block(bld->pc->current_block, b, CFG_EDGE_FORWARD);
1704
1705 bld->join_bb[bld->cond_lvl] = bld->pc->current_block;
1706 bld->cond_bb[bld->cond_lvl] = bld->pc->current_block;
1707
1708 src1 = bld_predicate(bld, emit_fetch(bld, insn, 0, 0), TRUE);
1709
1710 bld_flow(bld, NV_OP_BRA, NV_CC_EQ, src1, NULL, (bld->cond_lvl == 0));
1711
1712 ++bld->cond_lvl;
1713 bld_new_block(bld, b);
1714 }
1715 break;
1716 case TGSI_OPCODE_ELSE:
1717 {
1718 struct nv_basic_block *b = new_basic_block(bld->pc);
1719
1720 --bld->cond_lvl;
1721 nvbb_attach_block(bld->join_bb[bld->cond_lvl], b, CFG_EDGE_FORWARD);
1722
1723 bld->cond_bb[bld->cond_lvl]->exit->target = b;
1724 bld->cond_bb[bld->cond_lvl] = bld->pc->current_block;
1725
1726 new_instruction(bld->pc, NV_OP_BRA)->is_terminator = 1;
1727
1728 ++bld->cond_lvl;
1729 bld_new_block(bld, b);
1730 }
1731 break;
1732 case TGSI_OPCODE_ENDIF:
1733 {
1734 struct nv_basic_block *b = new_basic_block(bld->pc);
1735
1736 if (!nvbb_is_terminated(bld->pc->current_block))
1737 bld_flow(bld, NV_OP_BRA, NV_CC_TR, NULL, b, FALSE);
1738
1739 --bld->cond_lvl;
1740 nvbb_attach_block(bld->pc->current_block, b, bld->out_kind);
1741 nvbb_attach_block(bld->cond_bb[bld->cond_lvl], b, CFG_EDGE_FORWARD);
1742
1743 bld->cond_bb[bld->cond_lvl]->exit->target = b;
1744
1745 bld_new_block(bld, b);
1746
1747 if (!bld->cond_lvl && bld->join_bb[bld->cond_lvl]) {
1748 bld->join_bb[bld->cond_lvl]->exit->prev->target = b;
1749 new_instruction(bld->pc, NV_OP_JOIN)->is_join = TRUE;
1750 }
1751 }
1752 break;
1753 case TGSI_OPCODE_BGNLOOP:
1754 {
1755 struct nv_basic_block *bl = new_basic_block(bld->pc);
1756 struct nv_basic_block *bb = new_basic_block(bld->pc);
1757
1758 assert(bld->loop_lvl < BLD_MAX_LOOP_NESTING);
1759
1760 bld->loop_bb[bld->loop_lvl] = bl;
1761 bld->brkt_bb[bld->loop_lvl] = bb;
1762
1763 bld_flow(bld, NV_OP_BREAKADDR, NV_CC_TR, NULL, bb, FALSE);
1764
1765 nvbb_attach_block(bld->pc->current_block, bl, CFG_EDGE_LOOP_ENTER);
1766
1767 bld_new_block(bld, bld->loop_bb[bld->loop_lvl++]);
1768
1769 if (bld->loop_lvl == bld->pc->loop_nesting_bound)
1770 bld->pc->loop_nesting_bound++;
1771
1772 bld_clear_def_use(&bld->tvs[0][0], BLD_MAX_TEMPS, bld->loop_lvl);
1773 bld_clear_def_use(&bld->avs[0][0], BLD_MAX_ADDRS, bld->loop_lvl);
1774 bld_clear_def_use(&bld->pvs[0][0], BLD_MAX_PREDS, bld->loop_lvl);
1775 }
1776 break;
1777 case TGSI_OPCODE_BRK:
1778 {
1779 struct nv_basic_block *bb = bld->brkt_bb[bld->loop_lvl - 1];
1780
1781 bld_flow(bld, NV_OP_BREAK, NV_CC_TR, NULL, bb, FALSE);
1782
1783 if (bld->out_kind == CFG_EDGE_FORWARD) /* else we already had BRK/CONT */
1784 nvbb_attach_block(bld->pc->current_block, bb, CFG_EDGE_LOOP_LEAVE);
1785
1786 bld->out_kind = CFG_EDGE_FAKE;
1787 }
1788 break;
1789 case TGSI_OPCODE_CONT:
1790 {
1791 struct nv_basic_block *bb = bld->loop_bb[bld->loop_lvl - 1];
1792
1793 bld_flow(bld, NV_OP_BRA, NV_CC_TR, NULL, bb, FALSE);
1794
1795 nvbb_attach_block(bld->pc->current_block, bb, CFG_EDGE_BACK);
1796
1797 if ((bb = bld->join_bb[bld->cond_lvl - 1])) {
1798 bld->join_bb[bld->cond_lvl - 1] = NULL;
1799 nv_nvi_delete(bb->exit->prev);
1800 }
1801 bld->out_kind = CFG_EDGE_FAKE;
1802 }
1803 break;
1804 case TGSI_OPCODE_ENDLOOP:
1805 {
1806 struct nv_basic_block *bb = bld->loop_bb[bld->loop_lvl - 1];
1807
1808 if (!nvbb_is_terminated(bld->pc->current_block))
1809 bld_flow(bld, NV_OP_BRA, NV_CC_TR, NULL, bb, FALSE);
1810
1811 nvbb_attach_block(bld->pc->current_block, bb, CFG_EDGE_BACK);
1812
1813 bld_loop_end(bld, bb); /* replace loop-side operand of the phis */
1814
1815 bld_new_block(bld, bld->brkt_bb[--bld->loop_lvl]);
1816 }
1817 break;
1818 case TGSI_OPCODE_ABS:
1819 case TGSI_OPCODE_CEIL:
1820 case TGSI_OPCODE_FLR:
1821 case TGSI_OPCODE_TRUNC:
1822 case TGSI_OPCODE_DDX:
1823 case TGSI_OPCODE_DDY:
1824 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1825 src0 = emit_fetch(bld, insn, 0, c);
1826 dst0[c] = bld_insn_1(bld, opcode, src0);
1827 }
1828 break;
1829 case TGSI_OPCODE_LIT:
1830 bld_lit(bld, dst0, insn);
1831 break;
1832 case TGSI_OPCODE_LRP:
1833 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1834 src0 = emit_fetch(bld, insn, 0, c);
1835 src1 = emit_fetch(bld, insn, 1, c);
1836 src2 = emit_fetch(bld, insn, 2, c);
1837 dst0[c] = bld_insn_2(bld, NV_OP_SUB, src1, src2);
1838 dst0[c] = bld_insn_3(bld, NV_OP_MAD, dst0[c], src0, src2);
1839 }
1840 break;
1841 case TGSI_OPCODE_MOV:
1842 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1843 dst0[c] = emit_fetch(bld, insn, 0, c);
1844 break;
1845 case TGSI_OPCODE_MAD:
1846 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1847 src0 = emit_fetch(bld, insn, 0, c);
1848 src1 = emit_fetch(bld, insn, 1, c);
1849 src2 = emit_fetch(bld, insn, 2, c);
1850 dst0[c] = bld_insn_3(bld, opcode, src0, src1, src2);
1851 }
1852 break;
1853 case TGSI_OPCODE_POW:
1854 src0 = emit_fetch(bld, insn, 0, 0);
1855 src1 = emit_fetch(bld, insn, 1, 0);
1856 temp = bld_pow(bld, src0, src1);
1857 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1858 dst0[c] = temp;
1859 break;
1860 case TGSI_OPCODE_LOG:
1861 src0 = emit_fetch(bld, insn, 0, 0);
1862 src0 = bld_insn_1(bld, NV_OP_ABS, src0);
1863 temp = bld_insn_1(bld, NV_OP_LG2, src0);
1864 dst0[2] = temp;
1865 if (insn->Dst[0].Register.WriteMask & 3) {
1866 temp = bld_insn_1(bld, NV_OP_FLOOR, temp);
1867 dst0[0] = temp;
1868 }
1869 if (insn->Dst[0].Register.WriteMask & 2) {
1870 temp = bld_insn_1(bld, NV_OP_PREEX2, temp);
1871 temp = bld_insn_1(bld, NV_OP_EX2, temp);
1872 temp = bld_insn_1(bld, NV_OP_RCP, temp);
1873 dst0[1] = bld_insn_2(bld, NV_OP_MUL, src0, temp);
1874 }
1875 if (insn->Dst[0].Register.WriteMask & 8)
1876 dst0[3] = bld_imm_f32(bld, 1.0f);
1877 break;
1878 case TGSI_OPCODE_RCP:
1879 case TGSI_OPCODE_LG2:
1880 src0 = emit_fetch(bld, insn, 0, 0);
1881 temp = bld_insn_1(bld, opcode, src0);
1882 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1883 dst0[c] = temp;
1884 break;
1885 case TGSI_OPCODE_RSQ:
1886 src0 = emit_fetch(bld, insn, 0, 0);
1887 temp = bld_insn_1(bld, NV_OP_ABS, src0);
1888 temp = bld_insn_1(bld, NV_OP_RSQ, temp);
1889 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1890 dst0[c] = temp;
1891 break;
1892 case TGSI_OPCODE_SLT:
1893 case TGSI_OPCODE_SGE:
1894 case TGSI_OPCODE_SEQ:
1895 case TGSI_OPCODE_SGT:
1896 case TGSI_OPCODE_SLE:
1897 case TGSI_OPCODE_SNE:
1898 case TGSI_OPCODE_ISLT:
1899 case TGSI_OPCODE_ISGE:
1900 case TGSI_OPCODE_USEQ:
1901 case TGSI_OPCODE_USGE:
1902 case TGSI_OPCODE_USLT:
1903 case TGSI_OPCODE_USNE:
1904 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1905 src0 = emit_fetch(bld, insn, 0, c);
1906 src1 = emit_fetch(bld, insn, 1, c);
1907 dst0[c] = bld_insn_2(bld, NV_OP_SET, src0, src1);
1908 dst0[c]->insn->set_cond = translate_setcc(insn->Instruction.Opcode);
1909 SET_TYPE(dst0[c], infer_dst_type(insn->Instruction.Opcode));
1910
1911 dst0[c]->insn->src[0]->typecast =
1912 dst0[c]->insn->src[1]->typecast =
1913 infer_src_type(insn->Instruction.Opcode);
1914
1915 if (dst0[c]->reg.type != NV_TYPE_F32)
1916 break;
1917 dst0[c]->reg.as_type = NV_TYPE_S32;
1918 dst0[c] = bld_insn_1(bld, NV_OP_ABS, dst0[c]);
1919 dst0[c] = bld_insn_1(bld, NV_OP_CVT, dst0[c]);
1920 SET_TYPE(dst0[c], NV_TYPE_F32);
1921 }
1922 break;
1923 case TGSI_OPCODE_SCS:
1924 if (insn->Dst[0].Register.WriteMask & 0x3) {
1925 src0 = emit_fetch(bld, insn, 0, 0);
1926 temp = bld_insn_1(bld, NV_OP_PRESIN, src0);
1927 if (insn->Dst[0].Register.WriteMask & 0x1)
1928 dst0[0] = bld_insn_1(bld, NV_OP_COS, temp);
1929 if (insn->Dst[0].Register.WriteMask & 0x2)
1930 dst0[1] = bld_insn_1(bld, NV_OP_SIN, temp);
1931 }
1932 if (insn->Dst[0].Register.WriteMask & 0x4)
1933 dst0[2] = bld_imm_f32(bld, 0.0f);
1934 if (insn->Dst[0].Register.WriteMask & 0x8)
1935 dst0[3] = bld_imm_f32(bld, 1.0f);
1936 break;
1937 case TGSI_OPCODE_SSG:
1938 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1939 src0 = emit_fetch(bld, insn, 0, c);
1940 src1 = bld_predicate(bld, src0, FALSE);
1941 temp = bld_insn_2(bld, NV_OP_AND, src0, bld_imm_u32(bld, 0x80000000));
1942 temp = bld_insn_2(bld, NV_OP_OR, temp, bld_imm_f32(bld, 1.0f));
1943 dst0[c] = bld_insn_2(bld, NV_OP_XOR, temp, temp);
1944 dst0[c]->insn->cc = NV_CC_EQ;
1945 nv_reference(bld->pc, &dst0[c]->insn->flags_src, src1);
1946 dst0[c] = bld_insn_2(bld, NV_OP_SELECT, dst0[c], temp);
1947 }
1948 break;
1949 case TGSI_OPCODE_SUB:
1950 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1951 src0 = emit_fetch(bld, insn, 0, c);
1952 src1 = emit_fetch(bld, insn, 1, c);
1953 dst0[c] = bld_insn_2(bld, NV_OP_ADD, src0, src1);
1954 dst0[c]->insn->src[1]->mod ^= NV_MOD_NEG;
1955 }
1956 break;
1957 case TGSI_OPCODE_TEX:
1958 case TGSI_OPCODE_TXB:
1959 case TGSI_OPCODE_TXL:
1960 case TGSI_OPCODE_TXP:
1961 bld_tex(bld, dst0, insn);
1962 break;
1963 case TGSI_OPCODE_XPD:
1964 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn) {
1965 if (c == 3) {
1966 dst0[3] = bld_imm_f32(bld, 1.0f);
1967 break;
1968 }
1969 src0 = emit_fetch(bld, insn, 1, (c + 1) % 3);
1970 src1 = emit_fetch(bld, insn, 0, (c + 2) % 3);
1971 dst0[c] = bld_insn_2(bld, NV_OP_MUL, src0, src1);
1972
1973 src0 = emit_fetch(bld, insn, 0, (c + 1) % 3);
1974 src1 = emit_fetch(bld, insn, 1, (c + 2) % 3);
1975 dst0[c] = bld_insn_3(bld, NV_OP_MAD, src0, src1, dst0[c]);
1976
1977 dst0[c]->insn->src[2]->mod ^= NV_MOD_NEG;
1978 }
1979 break;
1980 case TGSI_OPCODE_RET:
1981 (new_instruction(bld->pc, NV_OP_RET))->fixed = 1;
1982 break;
1983 case TGSI_OPCODE_END:
1984 if (bld->ti->p->type == PIPE_SHADER_FRAGMENT)
1985 bld_export_outputs(bld);
1986 break;
1987 default:
1988 NOUVEAU_ERR("unhandled opcode %u\n", insn->Instruction.Opcode);
1989 abort();
1990 break;
1991 }
1992
1993 FOR_EACH_DST0_ENABLED_CHANNEL(c, insn)
1994 emit_store(bld, insn, c, dst0[c]);
1995
1996 if (prog->type == PIPE_SHADER_VERTEX && prog->vp.clpd_nr &&
1997 dreg->Register.File == TGSI_FILE_OUTPUT && !dreg->Register.Indirect &&
1998 prog->out[dreg->Register.Index].sn == TGSI_SEMANTIC_POSITION) {
1999
2000 int p;
2001 for (p = 0; p < prog->vp.clpd_nr; p++) {
2002 struct nv_value *clipd = NULL;
2003
2004 for (c = 0; c < 4; c++) {
2005 temp = new_value(bld->pc, NV_FILE_MEM_C(15), NV_TYPE_F32);
2006 temp->reg.id = p * 4 + c;
2007 temp = bld_insn_1(bld, NV_OP_LDA, temp);
2008
2009 clipd = clipd ?
2010 bld_insn_3(bld, NV_OP_MAD, dst0[c], temp, clipd) :
2011 bld_insn_2(bld, NV_OP_MUL, dst0[c], temp);
2012 }
2013
2014 temp = bld_insn_1(bld, NV_OP_MOV, clipd);
2015 temp->reg.file = NV_FILE_OUT;
2016 temp->reg.id = bld->ti->p->vp.clpd + p;
2017 temp->insn->fixed = 1;
2018 }
2019 }
2020 }
2021
2022 static INLINE void
2023 bld_free_value_trackers(struct bld_value_stack *base, int n)
2024 {
2025 int i, c;
2026
2027 for (i = 0; i < n; ++i)
2028 for (c = 0; c < 4; ++c)
2029 if (base[i * 4 + c].body)
2030 FREE(base[i * 4 + c].body);
2031 }
2032
2033 int
2034 nv50_tgsi_to_nc(struct nv_pc *pc, struct nv50_translation_info *ti)
2035 {
2036 struct bld_context *bld = CALLOC_STRUCT(bld_context);
2037 int c;
2038 unsigned ip;
2039
2040 pc->root[0] = pc->current_block = new_basic_block(pc);
2041
2042 bld->pc = pc;
2043 bld->ti = ti;
2044
2045 pc->loop_nesting_bound = 1;
2046
2047 c = util_bitcount(bld->ti->p->fp.interp >> 24);
2048 if (c && ti->p->type == PIPE_SHADER_FRAGMENT) {
2049 bld->frgcrd[3] = new_value(pc, NV_FILE_MEM_V, NV_TYPE_F32);
2050 bld->frgcrd[3]->reg.id = c - 1;
2051 bld->frgcrd[3] = bld_insn_1(bld, NV_OP_LINTERP, bld->frgcrd[3]);
2052 bld->frgcrd[3] = bld_insn_1(bld, NV_OP_RCP, bld->frgcrd[3]);
2053 }
2054
2055 for (ip = 0; ip < ti->inst_nr; ++ip)
2056 bld_instruction(bld, &ti->insns[ip]);
2057
2058 bld_free_value_trackers(&bld->tvs[0][0], BLD_MAX_TEMPS);
2059 bld_free_value_trackers(&bld->avs[0][0], BLD_MAX_ADDRS);
2060 bld_free_value_trackers(&bld->pvs[0][0], BLD_MAX_PREDS);
2061
2062 bld_free_value_trackers(&bld->ovs[0][0], PIPE_MAX_SHADER_OUTPUTS);
2063
2064 FREE(bld);
2065 return 0;
2066 }
2067
2068 /* If a variable is assigned in a loop, replace all references to the value
2069 * from outside the loop with a phi value.
2070 */
2071 static void
2072 bld_replace_value(struct nv_pc *pc, struct nv_basic_block *b,
2073 struct nv_value *old_val,
2074 struct nv_value *new_val)
2075 {
2076 struct nv_instruction *nvi;
2077
2078 for (nvi = b->phi ? b->phi : b->entry; nvi; nvi = nvi->next) {
2079 int s;
2080 for (s = 0; s < 5; ++s) {
2081 if (!nvi->src[s])
2082 continue;
2083 if (nvi->src[s]->value == old_val)
2084 nv_reference(pc, &nvi->src[s], new_val);
2085 }
2086 if (nvi->flags_src && nvi->flags_src->value == old_val)
2087 nv_reference(pc, &nvi->flags_src, new_val);
2088 }
2089
2090 b->pass_seq = pc->pass_seq;
2091
2092 if (b->out[0] && b->out[0]->pass_seq < pc->pass_seq)
2093 bld_replace_value(pc, b->out[0], old_val, new_val);
2094
2095 if (b->out[1] && b->out[1]->pass_seq < pc->pass_seq)
2096 bld_replace_value(pc, b->out[1], old_val, new_val);
2097 }