r300g: fix reference counting when translating indices
[mesa.git] / src / gallium / drivers / nv50 / nv50_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 __NV50_COMPILER_H__
24 #define __NV50_COMPILER_H__
25
26 #ifdef NV50PC_DEBUG
27 # define NV50_DBGMSG(args...) debug_printf(args)
28 #else
29 # define NV50_DBGMSG(args...)
30 #endif
31
32 #include "pipe/p_defines.h"
33 #include "util/u_inlines.h"
34 #include "util/u_memory.h"
35
36 #define NV_OP_PHI 0
37 #define NV_OP_EXTRACT 1
38 #define NV_OP_COMBINE 2
39 #define NV_OP_LDA 3
40 #define NV_OP_STA 4
41 #define NV_OP_MOV 5
42 #define NV_OP_ADD 6
43 #define NV_OP_SUB 7
44 #define NV_OP_NEG 8
45 #define NV_OP_MUL 9
46 #define NV_OP_MAD 10
47 #define NV_OP_CVT 11
48 #define NV_OP_SAT 12
49 #define NV_OP_NOT 13
50 #define NV_OP_AND 14
51 #define NV_OP_OR 15
52 #define NV_OP_XOR 16
53 #define NV_OP_SHL 17
54 #define NV_OP_SHR 18
55 #define NV_OP_RCP 19
56 #define NV_OP_UNDEF 20
57 #define NV_OP_RSQ 21
58 #define NV_OP_LG2 22
59 #define NV_OP_SIN 23
60 #define NV_OP_COS 24
61 #define NV_OP_EX2 25
62 #define NV_OP_PRESIN 26
63 #define NV_OP_PREEX2 27
64 #define NV_OP_MIN 28
65 #define NV_OP_MAX 29
66 #define NV_OP_SET 30
67 #define NV_OP_SAD 31
68 #define NV_OP_KIL 32
69 #define NV_OP_BRA 33
70 #define NV_OP_CALL 34
71 #define NV_OP_RET 35
72 #define NV_OP_BREAK 36
73 #define NV_OP_BREAKADDR 37
74 #define NV_OP_JOINAT 38
75 #define NV_OP_TEX 39
76 #define NV_OP_TXB 40
77 #define NV_OP_TXL 41
78 #define NV_OP_TXF 42
79 #define NV_OP_TXQ 43
80 #define NV_OP_DFDX 44
81 #define NV_OP_DFDY 45
82 #define NV_OP_QUADOP 46
83 #define NV_OP_LINTERP 47
84 #define NV_OP_PINTERP 48
85 #define NV_OP_ABS 49
86 #define NV_OP_CEIL 50
87 #define NV_OP_FLOOR 51
88 #define NV_OP_TRUNC 52
89 #define NV_OP_NOP 53
90 #define NV_OP_SELECT 54
91 #define NV_OP_EXPORT 55
92 #define NV_OP_JOIN 56
93 #define NV_OP_COUNT 57
94
95 #define NV_FILE_GPR 0
96 #define NV_FILE_OUT 1
97 #define NV_FILE_ADDR 2
98 #define NV_FILE_FLAGS 3
99 #define NV_FILE_IMM 16
100 #define NV_FILE_MEM_S 32
101 #define NV_FILE_MEM_P 33
102 #define NV_FILE_MEM_V 34
103 #define NV_FILE_MEM_L 48
104 #define NV_FILE_MEM_G(i) (64 + i)
105 #define NV_FILE_MEM_C(i) (80 + i)
106
107 #define NV_MOD_NEG 1
108 #define NV_MOD_ABS 2
109 #define NV_MOD_NOT 4
110 #define NV_MOD_SAT 8
111
112 #define NV_TYPE_U8 0x00
113 #define NV_TYPE_S8 0x01
114 #define NV_TYPE_U16 0x02
115 #define NV_TYPE_S16 0x03
116 #define NV_TYPE_U32 0x04
117 #define NV_TYPE_S32 0x05
118 #define NV_TYPE_P32 0x07
119 #define NV_TYPE_F32 0x09
120 #define NV_TYPE_F64 0x0b
121 #define NV_TYPE_VEC(x, n) (NV_TYPE_##x | (n << 4))
122 #define NV_TYPE_LO 0x00
123 #define NV_TYPE_HI 0x80
124 #define NV_TYPE_ANY 0xff
125
126 #define NV_TYPE_ISINT(t) ((t) <= 5)
127 #define NV_TYPE_ISFLT(t) ((t) & 0x08)
128
129 /* $cX registers contain 4 bits: OCSZ (Z is bit 0) */
130 #define NV_CC_FL 0x0
131 #define NV_CC_LT 0x1
132 #define NV_CC_EQ 0x2
133 #define NV_CC_LE 0x3
134 #define NV_CC_GT 0x4
135 #define NV_CC_NE 0x5
136 #define NV_CC_GE 0x6
137 #define NV_CC_U 0x8
138 #define NV_CC_TR 0xf
139 #define NV_CC_O 0x10
140 #define NV_CC_C 0x11
141 #define NV_CC_A 0x12
142 #define NV_CC_S 0x13
143
144 #define NV_PC_MAX_INSTRUCTIONS 2048
145 #define NV_PC_MAX_VALUES (NV_PC_MAX_INSTRUCTIONS * 4)
146
147 #define NV_PC_MAX_BASIC_BLOCKS 1024
148
149 static INLINE boolean
150 nv_is_vector_op(uint opcode)
151 {
152 return (opcode >= NV_OP_TEX) && (opcode <= NV_OP_TXQ);
153 }
154
155 static INLINE uint
156 nv_type_order(ubyte type)
157 {
158 switch (type & 0xf) {
159 case NV_TYPE_U8:
160 case NV_TYPE_S8:
161 return 0;
162 case NV_TYPE_U16:
163 case NV_TYPE_S16:
164 return 1;
165 case NV_TYPE_U32:
166 case NV_TYPE_F32:
167 case NV_TYPE_S32:
168 case NV_TYPE_P32:
169 return 2;
170 case NV_TYPE_F64:
171 return 3;
172 }
173 assert(0);
174 return 0;
175 }
176
177 static INLINE uint
178 nv_type_sizeof(ubyte type)
179 {
180 if (type & 0xf0)
181 return (1 << nv_type_order(type)) * (type >> 4);
182 return 1 << nv_type_order(type);
183 }
184
185 static INLINE uint
186 nv_type_sizeof_base(ubyte type)
187 {
188 return 1 << nv_type_order(type);
189 }
190
191 struct nv_reg {
192 int id;
193 ubyte file;
194 ubyte type; /* type of generating instruction's result */
195 ubyte as_type; /* default type for new references to this value */
196 union {
197 float f32;
198 double f64;
199 int32_t s32;
200 uint32_t u32;
201 } imm;
202 };
203
204 struct nv_range {
205 struct nv_range *next;
206 int bgn;
207 int end;
208 };
209
210 struct nv_value {
211 struct nv_reg reg;
212 struct nv_instruction *insn;
213 struct nv_value *join;
214 int n;
215 struct nv_range *livei;
216 int refc;
217
218 struct nv_value *next;
219 struct nv_value *prev;
220 };
221
222 struct nv_ref {
223 struct nv_value *value;
224 ubyte mod;
225 ubyte typecast;
226 ubyte flags; /* not used yet */
227 };
228
229 struct nv_basic_block;
230
231 struct nv_instruction {
232 struct nv_instruction *next;
233 struct nv_instruction *prev;
234 uint opcode;
235 int serial;
236 struct nv_value *def[4];
237 struct nv_value *flags_def;
238 struct nv_ref *src[5];
239 struct nv_ref *flags_src;
240 struct nv_basic_block *bb;
241 struct nv_basic_block *target; /* target block of control flow insn */
242 ubyte cc;
243 unsigned set_cond : 4;
244 unsigned fixed : 1; /* don't optimize away */
245 unsigned is_terminator : 1;
246 unsigned is_join : 1;
247 unsigned is_long : 1; /* for emission */
248 /* */
249 unsigned saturate : 1;
250 unsigned centroid : 1;
251 unsigned flat : 1;
252 unsigned lanes : 4;
253 unsigned tex_live : 1;
254 /* */
255 ubyte tex_t; /* TIC binding */
256 ubyte tex_s; /* TSC binding */
257 unsigned tex_argc : 3;
258 unsigned tex_cube : 1;
259 unsigned tex_mask : 4;
260 /* */
261 ubyte quadop;
262 };
263
264 #define CFG_EDGE_FORWARD 0
265 #define CFG_EDGE_BACK 1
266 #define CFG_EDGE_LOOP_ENTER 2
267 #define CFG_EDGE_LOOP_LEAVE 4
268 #define CFG_EDGE_FAKE 8
269
270 /* 'WALL' edge means where reachability check doesn't follow */
271 /* 'LOOP' edge means just having to do with loops */
272 #define IS_LOOP_EDGE(k) ((k) & 7)
273 #define IS_WALL_EDGE(k) ((k) & 9)
274
275 struct nv_basic_block {
276 struct nv_instruction *entry; /* first non-phi instruction */
277 struct nv_instruction *exit;
278 struct nv_instruction *phi; /* very first instruction */
279 int num_instructions;
280
281 struct nv_basic_block *out[2]; /* no indirect branches -> 2 */
282 struct nv_basic_block *in[8]; /* hope that suffices */
283 uint num_in;
284 ubyte out_kind[2];
285 ubyte in_kind[8];
286
287 int id;
288 int subroutine;
289 uint priv; /* reset to 0 after you're done */
290 uint pass_seq;
291
292 uint32_t bin_pos; /* position, size in emitted code */
293 uint32_t bin_size;
294
295 uint32_t live_set[NV_PC_MAX_VALUES / 32];
296 };
297
298 struct nv50_translation_info;
299
300 struct nv_pc {
301 struct nv_basic_block **root;
302 struct nv_basic_block *current_block;
303 struct nv_basic_block *parent_block;
304
305 int loop_nesting_bound;
306 uint pass_seq;
307
308 struct nv_value values[NV_PC_MAX_VALUES];
309 struct nv_instruction instructions[NV_PC_MAX_INSTRUCTIONS];
310 struct nv_ref **refs;
311 struct nv_basic_block *bb_list[NV_PC_MAX_BASIC_BLOCKS];
312 int num_values;
313 int num_instructions;
314 int num_refs;
315 int num_blocks;
316 int num_subroutines;
317
318 int max_reg[4];
319
320 uint32_t *immd_buf; /* populated on emit */
321 unsigned immd_count;
322
323 uint32_t *emit;
324 unsigned bin_size;
325 unsigned bin_pos;
326
327 void *fixups;
328 unsigned num_fixups;
329
330 /* optimization enables */
331 boolean opt_reload_elim;
332 };
333
334 void nvbb_insert_tail(struct nv_basic_block *, struct nv_instruction *);
335 void nvi_insert_after(struct nv_instruction *, struct nv_instruction *);
336
337 static INLINE struct nv_instruction *
338 nv_alloc_instruction(struct nv_pc *pc, uint opcode)
339 {
340 struct nv_instruction *insn;
341
342 insn = &pc->instructions[pc->num_instructions++];
343 assert(pc->num_instructions < NV_PC_MAX_INSTRUCTIONS);
344
345 insn->cc = NV_CC_TR;
346 insn->opcode = opcode;
347
348 return insn;
349 }
350
351 static INLINE struct nv_instruction *
352 new_instruction(struct nv_pc *pc, uint opcode)
353 {
354 struct nv_instruction *insn = nv_alloc_instruction(pc, opcode);
355
356 nvbb_insert_tail(pc->current_block, insn);
357 return insn;
358 }
359
360 static INLINE struct nv_instruction *
361 new_instruction_at(struct nv_pc *pc, struct nv_instruction *at, uint opcode)
362 {
363 struct nv_instruction *insn = nv_alloc_instruction(pc, opcode);
364
365 nvi_insert_after(at, insn);
366 return insn;
367 }
368
369 static INLINE struct nv_value *
370 new_value(struct nv_pc *pc, ubyte file, ubyte type)
371 {
372 struct nv_value *value = &pc->values[pc->num_values];
373
374 assert(pc->num_values < NV_PC_MAX_VALUES - 1);
375
376 value->n = pc->num_values++;
377 value->join = value;
378 value->reg.id = -1;
379 value->reg.file = file;
380 value->reg.type = value->reg.as_type = type;
381 return value;
382 }
383
384 static INLINE struct nv_value *
385 new_value_like(struct nv_pc *pc, struct nv_value *like)
386 {
387 struct nv_value *val = new_value(pc, like->reg.file, like->reg.type);
388 val->reg.as_type = like->reg.as_type;
389 return val;
390 }
391
392 static INLINE struct nv_ref *
393 new_ref(struct nv_pc *pc, struct nv_value *val)
394 {
395 int i;
396 struct nv_ref *ref;
397
398 if ((pc->num_refs % 64) == 0) {
399 const unsigned old_size = pc->num_refs * sizeof(struct nv_ref *);
400 const unsigned new_size = (pc->num_refs + 64) * sizeof(struct nv_ref *);
401
402 pc->refs = REALLOC(pc->refs, old_size, new_size);
403
404 ref = CALLOC(64, sizeof(struct nv_ref));
405 for (i = 0; i < 64; ++i)
406 pc->refs[pc->num_refs + i] = &ref[i];
407 }
408
409 ref = pc->refs[pc->num_refs++];
410 ref->value = val;
411 ref->typecast = val->reg.as_type;
412
413 ++val->refc;
414 return ref;
415 }
416
417 static INLINE struct nv_basic_block *
418 new_basic_block(struct nv_pc *pc)
419 {
420 struct nv_basic_block *bb;
421
422 if (pc->num_blocks >= NV_PC_MAX_BASIC_BLOCKS)
423 return NULL;
424
425 bb = CALLOC_STRUCT(nv_basic_block);
426
427 bb->id = pc->num_blocks;
428 pc->bb_list[pc->num_blocks++] = bb;
429 return bb;
430 }
431
432 static INLINE void
433 nv_reference(struct nv_pc *pc, struct nv_ref **d, struct nv_value *s)
434 {
435 if (*d)
436 --(*d)->value->refc;
437
438 if (s) {
439 if (!*d)
440 *d = new_ref(pc, s);
441 else {
442 (*d)->value = s;
443 ++(s->refc);
444 }
445 } else {
446 *d = NULL;
447 }
448 }
449
450 /* nv50_emit.c */
451 void nv50_emit_instruction(struct nv_pc *, struct nv_instruction *);
452 unsigned nv50_inst_min_size(struct nv_instruction *);
453
454 /* nv50_print.c */
455 const char *nv_opcode_name(uint opcode);
456 void nv_print_instruction(struct nv_instruction *);
457
458 /* nv50_pc.c */
459
460 void nv_print_function(struct nv_basic_block *root);
461 void nv_print_program(struct nv_pc *);
462
463 boolean nv_op_commutative(uint opcode);
464 int nv50_indirect_opnd(struct nv_instruction *);
465 boolean nv50_nvi_can_use_imm(struct nv_instruction *, int s);
466 boolean nv50_nvi_can_predicate(struct nv_instruction *);
467 boolean nv50_nvi_can_load(struct nv_instruction *, int s, struct nv_value *);
468 boolean nv50_op_can_write_flags(uint opcode);
469 ubyte nv50_supported_src_mods(uint opcode, int s);
470 int nv_nvi_refcount(struct nv_instruction *);
471 void nv_nvi_delete(struct nv_instruction *);
472 void nv_nvi_permute(struct nv_instruction *, struct nv_instruction *);
473 void nvbb_attach_block(struct nv_basic_block *parent,
474 struct nv_basic_block *, ubyte edge_kind);
475 boolean nvbb_dominated_by(struct nv_basic_block *, struct nv_basic_block *);
476 boolean nvbb_reachable_by(struct nv_basic_block *, struct nv_basic_block *,
477 struct nv_basic_block *);
478 struct nv_basic_block *nvbb_dom_frontier(struct nv_basic_block *);
479 int nvcg_replace_value(struct nv_pc *pc, struct nv_value *old_val,
480 struct nv_value *new_val);
481 struct nv_value *nvcg_find_immediate(struct nv_ref *);
482 struct nv_value *nvcg_find_constant(struct nv_ref *);
483
484 typedef void (*nv_pc_pass_func)(void *priv, struct nv_basic_block *b);
485
486 void nv_pc_pass_in_order(struct nv_basic_block *, nv_pc_pass_func, void *);
487
488 int nv_pc_exec_pass0(struct nv_pc *pc);
489 int nv_pc_exec_pass1(struct nv_pc *pc);
490 int nv_pc_exec_pass2(struct nv_pc *pc);
491
492 int nv50_tgsi_to_nc(struct nv_pc *, struct nv50_translation_info *);
493
494 #endif // NV50_COMPILER_H