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