nvc0: remove unused 'buf' parameter in pipe_buffer_unmap
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_pc.h
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 #ifndef __NVC0_COMPILER_H__
24 #define __NVC0_COMPILER_H__
25
26 #include <stdio.h>
27
28 #ifndef NOUVEAU_DBG
29 #ifdef NOUVEAU_DEBUG
30 # define NOUVEAU_DBG(args...) debug_printf(args);
31 #else
32 # define NOUVEAU_DBG(args...)
33 #endif
34 #endif
35
36 #ifndef NOUVEAU_ERR
37 #define NOUVEAU_ERR(fmt, args...) \
38 fprintf(stderr, "%s:%d - "fmt, __FUNCTION__, __LINE__, ##args);
39 #endif
40
41 #include "pipe/p_defines.h"
42 #include "util/u_inlines.h"
43 #include "util/u_memory.h"
44 #include "util/u_double_list.h"
45
46 /* pseudo opcodes */
47 #define NV_OP_UNDEF 0
48 #define NV_OP_BIND 1
49 #define NV_OP_MERGE 2
50 #define NV_OP_PHI 3
51 #define NV_OP_SELECT 4
52 #define NV_OP_NOP 5
53
54 /**
55 * BIND forces source operand i into the same register as destination operand i,
56 * and the operands will be assigned consecutive registers (needed for TEX)
57 * SELECT forces its multiple source operands and its destination operand into
58 * one and the same register.
59 */
60
61 /* base opcodes */
62 #define NV_OP_LD 6
63 #define NV_OP_ST 7
64 #define NV_OP_MOV 8
65 #define NV_OP_AND 9
66 #define NV_OP_OR 10
67 #define NV_OP_XOR 11
68 #define NV_OP_SHL 12
69 #define NV_OP_SHR 13
70 #define NV_OP_NOT 14
71 #define NV_OP_SET 15
72 #define NV_OP_ADD 16
73 #define NV_OP_SUB 17
74 #define NV_OP_MUL 18
75 #define NV_OP_MAD 19
76 #define NV_OP_ABS 20
77 #define NV_OP_NEG 21
78 #define NV_OP_MAX 22
79 #define NV_OP_MIN 23
80 #define NV_OP_CVT 24
81 #define NV_OP_CEIL 25
82 #define NV_OP_FLOOR 26
83 #define NV_OP_TRUNC 27
84 #define NV_OP_SAD 28
85
86 /* shader opcodes */
87 #define NV_OP_VFETCH 29
88 #define NV_OP_PFETCH 30
89 #define NV_OP_EXPORT 31
90 #define NV_OP_LINTERP 32
91 #define NV_OP_PINTERP 33
92 #define NV_OP_EMIT 34
93 #define NV_OP_RESTART 35
94 #define NV_OP_TEX 36
95 #define NV_OP_TXB 37
96 #define NV_OP_TXL 38
97 #define NV_OP_TXF 39
98 #define NV_OP_TXQ 40
99 #define NV_OP_QUADOP 41
100 #define NV_OP_DFDX 42
101 #define NV_OP_DFDY 43
102 #define NV_OP_KIL 44
103
104 /* control flow opcodes */
105 #define NV_OP_BRA 45
106 #define NV_OP_CALL 46
107 #define NV_OP_RET 47
108 #define NV_OP_EXIT 48
109 #define NV_OP_BREAK 49
110 #define NV_OP_BREAKADDR 50
111 #define NV_OP_JOINAT 51
112 #define NV_OP_JOIN 52
113
114 /* typed opcodes */
115 #define NV_OP_ADD_F32 NV_OP_ADD
116 #define NV_OP_ADD_B32 53
117 #define NV_OP_MUL_F32 NV_OP_MUL
118 #define NV_OP_MUL_B32 54
119 #define NV_OP_ABS_F32 NV_OP_ABS
120 #define NV_OP_ABS_S32 55
121 #define NV_OP_NEG_F32 NV_OP_NEG
122 #define NV_OP_NEG_S32 56
123 #define NV_OP_MAX_F32 NV_OP_MAX
124 #define NV_OP_MAX_S32 57
125 #define NV_OP_MAX_U32 58
126 #define NV_OP_MIN_F32 NV_OP_MIN
127 #define NV_OP_MIN_S32 59
128 #define NV_OP_MIN_U32 60
129 #define NV_OP_SET_F32 61
130 #define NV_OP_SET_S32 62
131 #define NV_OP_SET_U32 63
132 #define NV_OP_SAR 64
133 #define NV_OP_RCP 65
134 #define NV_OP_RSQ 66
135 #define NV_OP_LG2 67
136 #define NV_OP_SIN 68
137 #define NV_OP_COS 69
138 #define NV_OP_EX2 70
139 #define NV_OP_PRESIN 71
140 #define NV_OP_PREEX2 72
141 #define NV_OP_SAT 73
142
143 /* newly added opcodes */
144 #define NV_OP_SET_F32_AND 74
145 #define NV_OP_SET_F32_OR 75
146 #define NV_OP_SET_F32_XOR 76
147 #define NV_OP_SELP 77
148 #define NV_OP_SLCT 78
149 #define NV_OP_SLCT_F32 NV_OP_SLCT
150 #define NV_OP_SLCT_S32 79
151 #define NV_OP_SLCT_U32 80
152 #define NV_OP_SUB_F32 NV_OP_SUB
153 #define NV_OP_SUB_S32 81
154 #define NV_OP_MAD_F32 NV_OP_MAD
155 #define NV_OP_FSET_F32 82
156 #define NV_OP_TXG 83
157
158 #define NV_OP_COUNT 84
159
160 /* nv50 files omitted */
161 #define NV_FILE_GPR 0
162 #define NV_FILE_COND 1
163 #define NV_FILE_PRED 2
164 #define NV_FILE_IMM 16
165 #define NV_FILE_MEM_S 32
166 #define NV_FILE_MEM_V 34
167 #define NV_FILE_MEM_A 35
168 #define NV_FILE_MEM_L 48
169 #define NV_FILE_MEM_G 64
170 #define NV_FILE_MEM_C(i) (80 + i)
171
172 #define NV_IS_MEMORY_FILE(f) ((f) >= NV_FILE_MEM_S)
173
174 #define NV_MOD_NEG 1
175 #define NV_MOD_ABS 2
176 #define NV_MOD_NOT 4
177 #define NV_MOD_SAT 8
178
179 #define NV_TYPE_U8 0x00
180 #define NV_TYPE_S8 0x01
181 #define NV_TYPE_U16 0x02
182 #define NV_TYPE_S16 0x03
183 #define NV_TYPE_U32 0x04
184 #define NV_TYPE_S32 0x05
185 #define NV_TYPE_P32 0x07
186 #define NV_TYPE_F32 0x09
187 #define NV_TYPE_F64 0x0b
188 #define NV_TYPE_VEC(x, n) (NV_TYPE_##x | (n << 4))
189 #define NV_TYPE_ANY 0xff
190
191 #define NV_TYPE_ISINT(t) ((t) < 7)
192 #define NV_TYPE_ISSGD(t) ((t) & 1)
193
194 #define NV_CC_FL 0x0
195 #define NV_CC_LT 0x1
196 #define NV_CC_EQ 0x2
197 #define NV_CC_LE 0x3
198 #define NV_CC_GT 0x4
199 #define NV_CC_NE 0x5
200 #define NV_CC_GE 0x6
201 #define NV_CC_U 0x8
202 #define NV_CC_TR 0xf
203 #define NV_CC_O 0x10
204 #define NV_CC_C 0x11
205 #define NV_CC_A 0x12
206 #define NV_CC_S 0x13
207
208 #define NV_PC_MAX_INSTRUCTIONS 2048
209 #define NV_PC_MAX_VALUES (NV_PC_MAX_INSTRUCTIONS * 4)
210
211 #define NV_PC_MAX_BASIC_BLOCKS 1024
212
213 struct nv_op_info {
214 uint base; /* e.g. ADD_S32 -> ADD */
215 char name[12];
216 uint8_t type;
217 uint8_t mods;
218 unsigned flow : 1;
219 unsigned commutative : 1;
220 unsigned vector : 1;
221 unsigned predicate : 1;
222 unsigned pseudo : 1;
223 unsigned immediate : 3;
224 unsigned memory : 3;
225 };
226
227 extern struct nv_op_info nvc0_op_info_table[];
228
229 #define NV_BASEOP(op) (nvc0_op_info_table[op].base)
230 #define NV_OPTYPE(op) (nvc0_op_info_table[op].type)
231
232 static INLINE uint
233 nv_op_base(uint opcode)
234 {
235 return nvc0_op_info_table[opcode].base;
236 }
237
238 static INLINE boolean
239 nv_is_texture_op(uint opcode)
240 {
241 return (opcode >= NV_OP_TEX && opcode <= NV_OP_TXQ);
242 }
243
244 static INLINE boolean
245 nv_is_vector_op(uint opcode)
246 {
247 return nvc0_op_info_table[opcode].vector ? TRUE : FALSE;
248 }
249
250 static INLINE boolean
251 nv_op_commutative(uint opcode)
252 {
253 return nvc0_op_info_table[opcode].commutative ? TRUE : FALSE;
254 }
255
256 static INLINE uint8_t
257 nv_op_supported_src_mods(uint opcode)
258 {
259 return nvc0_op_info_table[opcode].mods;
260 }
261
262 static INLINE boolean
263 nv_op_predicateable(uint opcode)
264 {
265 return nvc0_op_info_table[opcode].predicate ? TRUE : FALSE;
266 }
267
268 static INLINE uint
269 nv_type_order(ubyte type)
270 {
271 switch (type & 0xf) {
272 case NV_TYPE_U8:
273 case NV_TYPE_S8:
274 return 0;
275 case NV_TYPE_U16:
276 case NV_TYPE_S16:
277 return 1;
278 case NV_TYPE_U32:
279 case NV_TYPE_F32:
280 case NV_TYPE_S32:
281 case NV_TYPE_P32:
282 return 2;
283 case NV_TYPE_F64:
284 return 3;
285 }
286 assert(0);
287 return 0;
288 }
289
290 static INLINE uint
291 nv_type_sizeof(ubyte type)
292 {
293 if (type & 0xf0)
294 return (1 << nv_type_order(type)) * (type >> 4);
295 return 1 << nv_type_order(type);
296 }
297
298 static INLINE uint
299 nv_type_sizeof_base(ubyte type)
300 {
301 return 1 << nv_type_order(type);
302 }
303
304 struct nv_reg {
305 uint32_t address; /* for memory locations */
306 int id; /* for registers */
307 ubyte file;
308 ubyte size;
309 union {
310 int32_t s32;
311 int64_t s64;
312 uint64_t u64;
313 uint32_t u32;
314 float f32;
315 double f64;
316 } imm;
317 };
318
319 struct nv_range {
320 struct nv_range *next;
321 int bgn;
322 int end;
323 };
324
325 struct nv_ref;
326
327 struct nv_value {
328 struct nv_reg reg;
329 struct nv_instruction *insn;
330 struct nv_value *join;
331 struct nv_ref *last_use;
332 int n;
333 struct nv_range *livei;
334 int refc;
335 struct nv_value *next;
336 struct nv_value *prev;
337 };
338
339 struct nv_ref {
340 struct nv_value *value;
341 struct nv_instruction *insn;
342 struct list_head list; /* connects uses of the same value */
343 uint8_t mod;
344 uint8_t flags;
345 };
346
347 struct nv_basic_block;
348
349 struct nv_instruction {
350 struct nv_instruction *next;
351 struct nv_instruction *prev;
352 uint opcode;
353 uint serial;
354
355 struct nv_value *def[5];
356 struct nv_ref *src[6];
357
358 int8_t predicate; /* index of predicate src */
359 int8_t indirect; /* index of pointer src */
360
361 union {
362 struct {
363 uint8_t t; /* TIC binding */
364 uint8_t s; /* TSC binding */
365 } tex;
366 struct {
367 uint8_t d; /* output type */
368 uint8_t s; /* input type */
369 } cvt;
370 } ext;
371
372 struct nv_basic_block *bb;
373 struct nv_basic_block *target; /* target block of control flow insn */
374
375 unsigned cc : 5; /* condition code */
376 unsigned fixed : 1; /* don't optimize away (prematurely) */
377 unsigned terminator : 1;
378 unsigned join : 1;
379 unsigned set_cond : 4; /* 2nd byte */
380 unsigned saturate : 1;
381 unsigned centroid : 1;
382 unsigned flat : 1;
383 unsigned patch : 1;
384 unsigned lanes : 4; /* 3rd byte */
385 unsigned tex_dim : 2;
386 unsigned tex_array : 1;
387 unsigned tex_cube : 1;
388 unsigned tex_shadow : 1; /* 4th byte */
389 unsigned tex_live : 1;
390 unsigned tex_mask : 4;
391
392 uint8_t quadop;
393 };
394
395 static INLINE int
396 nvi_vector_size(struct nv_instruction *nvi)
397 {
398 int i;
399 assert(nvi);
400 for (i = 0; i < 5 && nvi->def[i]; ++i);
401 return i;
402 }
403
404 #define CFG_EDGE_FORWARD 0
405 #define CFG_EDGE_BACK 1
406 #define CFG_EDGE_LOOP_ENTER 2
407 #define CFG_EDGE_LOOP_LEAVE 4
408 #define CFG_EDGE_FAKE 8
409
410 /* 'WALL' edge means where reachability check doesn't follow */
411 /* 'LOOP' edge means just having to do with loops */
412 #define IS_LOOP_EDGE(k) ((k) & 7)
413 #define IS_WALL_EDGE(k) ((k) & 9)
414
415 struct nv_basic_block {
416 struct nv_instruction *entry; /* first non-phi instruction */
417 struct nv_instruction *exit;
418 struct nv_instruction *phi; /* very first instruction */
419 int num_instructions;
420
421 struct nv_basic_block *out[2]; /* no indirect branches -> 2 */
422 struct nv_basic_block *in[8]; /* hope that suffices */
423 uint num_in;
424 ubyte out_kind[2];
425 ubyte in_kind[8];
426
427 int id;
428 int subroutine;
429 uint priv; /* reset to 0 after you're done */
430 uint pass_seq;
431
432 uint32_t emit_pos; /* position, size in emitted code (in bytes) */
433 uint32_t emit_size;
434
435 uint32_t live_set[NV_PC_MAX_VALUES / 32];
436 };
437
438 struct nvc0_translation_info;
439
440 struct nv_pc {
441 struct nv_basic_block **root;
442 struct nv_basic_block *current_block;
443 struct nv_basic_block *parent_block;
444
445 int loop_nesting_bound;
446 uint pass_seq;
447
448 struct nv_value values[NV_PC_MAX_VALUES];
449 struct nv_instruction instructions[NV_PC_MAX_INSTRUCTIONS];
450 struct nv_ref **refs;
451 struct nv_basic_block *bb_list[NV_PC_MAX_BASIC_BLOCKS];
452 int num_values;
453 int num_instructions;
454 int num_refs;
455 int num_blocks;
456 int num_subroutines;
457
458 int max_reg[4];
459
460 uint32_t *immd_buf; /* populated on emit */
461 unsigned immd_count;
462
463 uint32_t *emit;
464 uint32_t emit_size;
465 uint32_t emit_pos;
466
467 void *reloc_entries;
468 unsigned num_relocs;
469
470 /* optimization enables */
471 boolean opt_reload_elim;
472 boolean is_fragprog;
473 };
474
475 void nvc0_insn_append(struct nv_basic_block *, struct nv_instruction *);
476 void nvc0_insn_insert_after(struct nv_instruction *, struct nv_instruction *);
477
478 static INLINE struct nv_instruction *
479 nv_alloc_instruction(struct nv_pc *pc, uint opcode)
480 {
481 struct nv_instruction *insn;
482
483 insn = &pc->instructions[pc->num_instructions++];
484 assert(pc->num_instructions < NV_PC_MAX_INSTRUCTIONS);
485
486 insn->opcode = opcode;
487 insn->cc = 0;
488 insn->indirect = -1;
489 insn->predicate = -1;
490
491 return insn;
492 }
493
494 static INLINE struct nv_instruction *
495 new_instruction(struct nv_pc *pc, uint opcode)
496 {
497 struct nv_instruction *insn = nv_alloc_instruction(pc, opcode);
498
499 nvc0_insn_append(pc->current_block, insn);
500 return insn;
501 }
502
503 static INLINE struct nv_instruction *
504 new_instruction_at(struct nv_pc *pc, struct nv_instruction *at, uint opcode)
505 {
506 struct nv_instruction *insn = nv_alloc_instruction(pc, opcode);
507
508 nvc0_insn_insert_after(at, insn);
509 return insn;
510 }
511
512 static INLINE struct nv_value *
513 new_value(struct nv_pc *pc, ubyte file, ubyte size)
514 {
515 struct nv_value *value = &pc->values[pc->num_values];
516
517 assert(pc->num_values < NV_PC_MAX_VALUES - 1);
518
519 value->n = pc->num_values++;
520 value->join = value;
521 value->reg.id = -1;
522 value->reg.file = file;
523 value->reg.size = size;
524 return value;
525 }
526
527 static INLINE struct nv_value *
528 new_value_like(struct nv_pc *pc, struct nv_value *like)
529 {
530 return new_value(pc, like->reg.file, like->reg.size);
531 }
532
533 static INLINE struct nv_ref *
534 new_ref(struct nv_pc *pc, struct nv_value *val)
535 {
536 int i;
537 struct nv_ref *ref;
538
539 if ((pc->num_refs % 64) == 0) {
540 const unsigned old_size = pc->num_refs * sizeof(struct nv_ref *);
541 const unsigned new_size = (pc->num_refs + 64) * sizeof(struct nv_ref *);
542
543 pc->refs = REALLOC(pc->refs, old_size, new_size);
544
545 ref = CALLOC(64, sizeof(struct nv_ref));
546 for (i = 0; i < 64; ++i)
547 pc->refs[pc->num_refs + i] = &ref[i];
548 }
549
550 ref = pc->refs[pc->num_refs++];
551 ref->value = val;
552
553 LIST_INITHEAD(&ref->list);
554
555 ++val->refc;
556 return ref;
557 }
558
559 static INLINE struct nv_basic_block *
560 new_basic_block(struct nv_pc *pc)
561 {
562 struct nv_basic_block *bb;
563
564 if (pc->num_blocks >= NV_PC_MAX_BASIC_BLOCKS)
565 return NULL;
566
567 bb = CALLOC_STRUCT(nv_basic_block);
568
569 bb->id = pc->num_blocks;
570 pc->bb_list[pc->num_blocks++] = bb;
571 return bb;
572 }
573
574 static INLINE void
575 nv_reference(struct nv_pc *pc,
576 struct nv_instruction *nvi, int c, struct nv_value *s)
577 {
578 struct nv_ref **d = &nvi->src[c];
579 assert(c < 6);
580
581 if (*d) {
582 --(*d)->value->refc;
583 LIST_DEL(&(*d)->list);
584 }
585
586 if (s) {
587 if (!*d) {
588 *d = new_ref(pc, s);
589 (*d)->insn = nvi;
590 } else {
591 LIST_DEL(&(*d)->list);
592 (*d)->value = s;
593 ++(s->refc);
594 }
595 if (!s->last_use)
596 s->last_use = *d;
597 else
598 LIST_ADDTAIL(&s->last_use->list, &(*d)->list);
599
600 s->last_use = *d;
601 (*d)->insn = nvi;
602 } else {
603 *d = NULL;
604 }
605 }
606
607 /* nvc0_emit.c */
608 void nvc0_emit_instruction(struct nv_pc *, struct nv_instruction *);
609
610 /* nvc0_print.c */
611 const char *nvc0_opcode_name(uint opcode);
612 void nvc0_print_instruction(struct nv_instruction *);
613
614 /* nvc0_pc.c */
615 void nvc0_print_function(struct nv_basic_block *root);
616 void nvc0_print_program(struct nv_pc *);
617
618 boolean nvc0_insn_can_load(struct nv_instruction *, int s,
619 struct nv_instruction *);
620 boolean nvc0_insn_is_predicateable(struct nv_instruction *);
621
622 int nvc0_insn_refcount(struct nv_instruction *);
623 void nvc0_insn_delete(struct nv_instruction *);
624 void nvc0_insns_permute(struct nv_instruction *prev, struct nv_instruction *);
625
626 void nvc0_bblock_attach(struct nv_basic_block *parent,
627 struct nv_basic_block *child, ubyte edge_kind);
628 boolean nvc0_bblock_dominated_by(struct nv_basic_block *,
629 struct nv_basic_block *);
630 boolean nvc0_bblock_reachable_by(struct nv_basic_block *future,
631 struct nv_basic_block *past,
632 struct nv_basic_block *final);
633 struct nv_basic_block *nvc0_bblock_dom_frontier(struct nv_basic_block *);
634
635 int nvc0_pc_replace_value(struct nv_pc *pc,
636 struct nv_value *old_val,
637 struct nv_value *new_val);
638
639 struct nv_value *nvc0_pc_find_immediate(struct nv_ref *);
640 struct nv_value *nvc0_pc_find_constant(struct nv_ref *);
641
642 typedef void (*nv_pc_pass_func)(void *priv, struct nv_basic_block *b);
643
644 void nvc0_pc_pass_in_order(struct nv_basic_block *, nv_pc_pass_func, void *);
645
646 int nvc0_pc_exec_pass0(struct nv_pc *pc);
647 int nvc0_pc_exec_pass1(struct nv_pc *pc);
648 int nvc0_pc_exec_pass2(struct nv_pc *pc);
649
650 int nvc0_tgsi_to_nc(struct nv_pc *, struct nvc0_translation_info *);
651
652 #endif // NV50_COMPILER_H