98715a98c0b892ff603a9ec8994677b2aa7b00e0
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3.h
1 /*
2 * Copyright (c) 2013 Rob Clark <robdclark@gmail.com>
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #ifndef IR3_H_
25 #define IR3_H_
26
27 #include <stdint.h>
28 #include <stdbool.h>
29
30 #include "util/u_debug.h"
31
32 #include "instr-a3xx.h"
33 #include "disasm.h" /* TODO move 'enum shader_t' somewhere else.. */
34
35 /* low level intermediate representation of an adreno shader program */
36
37 struct ir3;
38 struct ir3_instruction;
39 struct ir3_block;
40
41 struct ir3 * fd_asm_parse(const char *src);
42
43 struct ir3_info {
44 uint16_t sizedwords;
45 uint16_t instrs_count; /* expanded to account for rpt's */
46 /* NOTE: max_reg, etc, does not include registers not touched
47 * by the shader (ie. vertex fetched via VFD_DECODE but not
48 * touched by shader)
49 */
50 int8_t max_reg; /* highest GPR # used by shader */
51 int8_t max_half_reg;
52 int16_t max_const;
53 };
54
55 struct ir3_register {
56 enum {
57 IR3_REG_CONST = 0x001,
58 IR3_REG_IMMED = 0x002,
59 IR3_REG_HALF = 0x004,
60 IR3_REG_RELATIV= 0x008,
61 IR3_REG_R = 0x010,
62 IR3_REG_NEGATE = 0x020,
63 IR3_REG_ABS = 0x040,
64 IR3_REG_EVEN = 0x080,
65 IR3_REG_POS_INF= 0x100,
66 /* (ei) flag, end-input? Set on last bary, presumably to signal
67 * that the shader needs no more input:
68 */
69 IR3_REG_EI = 0x200,
70 /* meta-flags, for intermediate stages of IR, ie.
71 * before register assignment is done:
72 */
73 IR3_REG_SSA = 0x1000, /* 'instr' is ptr to assigning instr */
74 IR3_REG_IA = 0x2000, /* meta-input dst is "assigned" */
75 IR3_REG_ADDR = 0x4000, /* register is a0.x */
76 } flags;
77 union {
78 /* normal registers:
79 * the component is in the low two bits of the reg #, so
80 * rN.x becomes: (N << 2) | x
81 */
82 int num;
83 /* immediate: */
84 int iim_val;
85 float fim_val;
86 /* relative: */
87 int offset;
88 /* for IR3_REG_SSA, src registers contain ptr back to
89 * assigning instruction.
90 */
91 struct ir3_instruction *instr;
92 };
93
94 /* used for cat5 instructions, but also for internal/IR level
95 * tracking of what registers are read/written by an instruction.
96 * wrmask may be a bad name since it is used to represent both
97 * src and dst that touch multiple adjacent registers.
98 */
99 int wrmask;
100 };
101
102 #define IR3_INSTR_SRCS 10
103
104 struct ir3_instruction {
105 struct ir3_block *block;
106 int category;
107 opc_t opc;
108 enum {
109 /* (sy) flag is set on first instruction, and after sample
110 * instructions (probably just on RAW hazard).
111 */
112 IR3_INSTR_SY = 0x001,
113 /* (ss) flag is set on first instruction, and first instruction
114 * to depend on the result of "long" instructions (RAW hazard):
115 *
116 * rcp, rsq, log2, exp2, sin, cos, sqrt
117 *
118 * It seems to synchronize until all in-flight instructions are
119 * completed, for example:
120 *
121 * rsq hr1.w, hr1.w
122 * add.f hr2.z, (neg)hr2.z, hc0.y
123 * mul.f hr2.w, (neg)hr2.y, (neg)hr2.y
124 * rsq hr2.x, hr2.x
125 * (rpt1)nop
126 * mad.f16 hr2.w, hr2.z, hr2.z, hr2.w
127 * nop
128 * mad.f16 hr2.w, (neg)hr0.w, (neg)hr0.w, hr2.w
129 * (ss)(rpt2)mul.f hr1.x, (r)hr1.x, hr1.w
130 * (rpt2)mul.f hr0.x, (neg)(r)hr0.x, hr2.x
131 *
132 * The last mul.f does not have (ss) set, presumably because the
133 * (ss) on the previous instruction does the job.
134 *
135 * The blob driver also seems to set it on WAR hazards, although
136 * not really clear if this is needed or just blob compiler being
137 * sloppy. So far I haven't found a case where removing the (ss)
138 * causes problems for WAR hazard, but I could just be getting
139 * lucky:
140 *
141 * rcp r1.y, r3.y
142 * (ss)(rpt2)mad.f32 r3.y, (r)c9.x, r1.x, (r)r3.z
143 *
144 */
145 IR3_INSTR_SS = 0x002,
146 /* (jp) flag is set on jump targets:
147 */
148 IR3_INSTR_JP = 0x004,
149 IR3_INSTR_UL = 0x008,
150 IR3_INSTR_3D = 0x010,
151 IR3_INSTR_A = 0x020,
152 IR3_INSTR_O = 0x040,
153 IR3_INSTR_P = 0x080,
154 IR3_INSTR_S = 0x100,
155 IR3_INSTR_S2EN = 0x200,
156 /* meta-flags, for intermediate stages of IR, ie.
157 * before register assignment is done:
158 */
159 IR3_INSTR_MARK = 0x1000,
160 } flags;
161 int repeat;
162 unsigned regs_count;
163 struct ir3_register *regs[1 + IR3_INSTR_SRCS];
164 union {
165 struct {
166 char inv;
167 char comp;
168 int immed;
169 } cat0;
170 struct {
171 type_t src_type, dst_type;
172 } cat1;
173 struct {
174 enum {
175 IR3_COND_LT = 0,
176 IR3_COND_LE = 1,
177 IR3_COND_GT = 2,
178 IR3_COND_GE = 3,
179 IR3_COND_EQ = 4,
180 IR3_COND_NE = 5,
181 } condition;
182 } cat2;
183 struct {
184 unsigned samp, tex;
185 type_t type;
186 } cat5;
187 struct {
188 type_t type;
189 int offset;
190 int iim_val;
191 } cat6;
192 /* for meta-instructions, just used to hold extra data
193 * before instruction scheduling, etc
194 */
195 struct {
196 int off; /* component/offset */
197 } fo;
198 struct {
199 struct ir3_block *if_block, *else_block;
200 } flow;
201 struct {
202 struct ir3_block *block;
203 } inout;
204 };
205
206 /* transient values used during various algorithms: */
207 union {
208 /* The instruction depth is the max dependency distance to output.
209 *
210 * You can also think of it as the "cost", if we did any sort of
211 * optimization for register footprint. Ie. a value that is just
212 * result of moving a const to a reg would have a low cost, so to
213 * it could make sense to duplicate the instruction at various
214 * points where the result is needed to reduce register footprint.
215 *
216 * DEPTH_UNUSED used to mark unused instructions after depth
217 * calculation pass.
218 */
219 #define DEPTH_UNUSED ~0
220 unsigned depth;
221
222 /* Used just during cp stage, which comes before depth pass.
223 * For fanin, where we need a sequence of consecutive registers,
224 * keep track of each src instructions left (ie 'n-1') and right
225 * (ie 'n+1') neighbor. The front-end must insert enough mov's
226 * to ensure that each instruction has at most one left and at
227 * most one right neighbor. During the copy-propagation pass,
228 * we only remove mov's when we can preserve this constraint.
229 */
230 struct {
231 struct ir3_instruction *left, *right;
232 uint16_t left_cnt, right_cnt;
233 } cp;
234 };
235 struct ir3_instruction *next;
236 #ifdef DEBUG
237 uint32_t serialno;
238 #endif
239 };
240
241 struct ir3_heap_chunk;
242
243 struct ir3 {
244 unsigned instrs_count, instrs_sz;
245 struct ir3_instruction **instrs;
246 unsigned baryfs_count, baryfs_sz;
247 struct ir3_instruction **baryfs;
248 struct ir3_block *block;
249 unsigned heap_idx;
250 struct ir3_heap_chunk *chunk;
251 };
252
253 struct ir3_block {
254 struct ir3 *shader;
255 unsigned ntemporaries, ninputs, noutputs;
256 /* maps TGSI_FILE_TEMPORARY index back to the assigning instruction: */
257 struct ir3_instruction **temporaries;
258 struct ir3_instruction **inputs;
259 struct ir3_instruction **outputs;
260 /* only a single address register: */
261 struct ir3_instruction *address;
262 struct ir3_block *parent;
263 struct ir3_instruction *head;
264 };
265
266 struct ir3 * ir3_create(void);
267 void ir3_destroy(struct ir3 *shader);
268 void * ir3_assemble(struct ir3 *shader,
269 struct ir3_info *info, uint32_t gpu_id);
270 void * ir3_alloc(struct ir3 *shader, int sz);
271
272 struct ir3_block * ir3_block_create(struct ir3 *shader,
273 unsigned ntmp, unsigned nin, unsigned nout);
274
275 struct ir3_instruction * ir3_instr_create(struct ir3_block *block,
276 int category, opc_t opc);
277 struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr);
278 const char *ir3_instr_name(struct ir3_instruction *instr);
279
280 struct ir3_register * ir3_reg_create(struct ir3_instruction *instr,
281 int num, int flags);
282
283
284 static inline bool ir3_instr_check_mark(struct ir3_instruction *instr)
285 {
286 if (instr->flags & IR3_INSTR_MARK)
287 return true; /* already visited */
288 instr->flags |= IR3_INSTR_MARK;
289 return false;
290 }
291
292 static inline void ir3_clear_mark(struct ir3 *shader)
293 {
294 /* TODO would be nice to drop the instruction array.. for
295 * new compiler, _clear_mark() is all we use it for, and
296 * we could probably manage a linked list instead..
297 *
298 * Also, we'll probably want to mark instructions within
299 * a block, so tracking the list of instrs globally is
300 * unlikely to be what we want.
301 */
302 unsigned i;
303 for (i = 0; i < shader->instrs_count; i++) {
304 struct ir3_instruction *instr = shader->instrs[i];
305 instr->flags &= ~IR3_INSTR_MARK;
306 }
307 }
308
309 static inline int ir3_instr_regno(struct ir3_instruction *instr,
310 struct ir3_register *reg)
311 {
312 unsigned i;
313 for (i = 0; i < instr->regs_count; i++)
314 if (reg == instr->regs[i])
315 return i;
316 return -1;
317 }
318
319
320 /* comp:
321 * 0 - x
322 * 1 - y
323 * 2 - z
324 * 3 - w
325 */
326 static inline uint32_t regid(int num, int comp)
327 {
328 return (num << 2) | (comp & 0x3);
329 }
330
331 static inline uint32_t reg_num(struct ir3_register *reg)
332 {
333 return reg->num >> 2;
334 }
335
336 static inline uint32_t reg_comp(struct ir3_register *reg)
337 {
338 return reg->num & 0x3;
339 }
340
341 static inline bool is_flow(struct ir3_instruction *instr)
342 {
343 return (instr->category == 0);
344 }
345
346 static inline bool is_kill(struct ir3_instruction *instr)
347 {
348 return is_flow(instr) && (instr->opc == OPC_KILL);
349 }
350
351 static inline bool is_nop(struct ir3_instruction *instr)
352 {
353 return is_flow(instr) && (instr->opc == OPC_NOP);
354 }
355
356 static inline bool is_alu(struct ir3_instruction *instr)
357 {
358 return (1 <= instr->category) && (instr->category <= 3);
359 }
360
361 static inline bool is_sfu(struct ir3_instruction *instr)
362 {
363 return (instr->category == 4);
364 }
365
366 static inline bool is_tex(struct ir3_instruction *instr)
367 {
368 return (instr->category == 5);
369 }
370
371 static inline bool is_input(struct ir3_instruction *instr)
372 {
373 return (instr->category == 2) && (instr->opc == OPC_BARY_F);
374 }
375
376 static inline bool is_meta(struct ir3_instruction *instr)
377 {
378 /* TODO how should we count PHI (and maybe fan-in/out) which
379 * might actually contribute some instructions to the final
380 * result?
381 */
382 return (instr->category == -1);
383 }
384
385 static inline bool is_addr(struct ir3_instruction *instr)
386 {
387 return is_meta(instr) && (instr->opc == OPC_META_DEREF);
388 }
389
390 static inline bool writes_addr(struct ir3_instruction *instr)
391 {
392 if (instr->regs_count > 0) {
393 struct ir3_register *dst = instr->regs[0];
394 return !!(dst->flags & IR3_REG_ADDR);
395 }
396 return false;
397 }
398
399 static inline bool writes_pred(struct ir3_instruction *instr)
400 {
401 if (instr->regs_count > 0) {
402 struct ir3_register *dst = instr->regs[0];
403 return reg_num(dst) == REG_P0;
404 }
405 return false;
406 }
407
408 static inline bool reg_gpr(struct ir3_register *r)
409 {
410 if (r->flags & (IR3_REG_CONST | IR3_REG_IMMED | IR3_REG_RELATIV | IR3_REG_ADDR))
411 return false;
412 if ((reg_num(r) == REG_A0) || (reg_num(r) == REG_P0))
413 return false;
414 return true;
415 }
416
417 /* dump: */
418 #include <stdio.h>
419 void ir3_dump(struct ir3 *shader, const char *name,
420 struct ir3_block *block /* XXX maybe 'block' ptr should move to ir3? */,
421 FILE *f);
422 void ir3_dump_instr_single(struct ir3_instruction *instr);
423 void ir3_dump_instr_list(struct ir3_instruction *instr);
424
425 /* flatten if/else: */
426 int ir3_block_flatten(struct ir3_block *block);
427
428 /* depth calculation: */
429 int ir3_delayslots(struct ir3_instruction *assigner,
430 struct ir3_instruction *consumer, unsigned n);
431 void ir3_block_depth(struct ir3_block *block);
432
433 /* copy-propagate: */
434 void ir3_block_cp(struct ir3_block *block);
435
436 /* scheduling: */
437 int ir3_block_sched(struct ir3_block *block);
438
439 /* register assignment: */
440 int ir3_block_ra(struct ir3_block *block, enum shader_t type,
441 bool half_precision, bool frag_coord, bool frag_face,
442 bool *has_samp, int *max_bary);
443
444 #ifndef ARRAY_SIZE
445 # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
446 #endif
447
448 /* ************************************************************************* */
449 /* split this out or find some helper to use.. like main/bitset.h.. */
450
451 #include <string.h>
452
453 #define MAX_REG 256
454
455 typedef uint8_t regmask_t[2 * MAX_REG / 8];
456
457 static inline unsigned regmask_idx(struct ir3_register *reg)
458 {
459 unsigned num = reg->num;
460 debug_assert(num < MAX_REG);
461 if (reg->flags & IR3_REG_HALF)
462 num += MAX_REG;
463 return num;
464 }
465
466 static inline void regmask_init(regmask_t *regmask)
467 {
468 memset(regmask, 0, sizeof(*regmask));
469 }
470
471 static inline void regmask_set(regmask_t *regmask, struct ir3_register *reg)
472 {
473 unsigned idx = regmask_idx(reg);
474 unsigned i;
475 for (i = 0; i < IR3_INSTR_SRCS; i++, idx++)
476 if (reg->wrmask & (1 << i))
477 (*regmask)[idx / 8] |= 1 << (idx % 8);
478 }
479
480 /* set bits in a if not set in b, conceptually:
481 * a |= (reg & ~b)
482 */
483 static inline void regmask_set_if_not(regmask_t *a,
484 struct ir3_register *reg, regmask_t *b)
485 {
486 unsigned idx = regmask_idx(reg);
487 unsigned i;
488 for (i = 0; i < IR3_INSTR_SRCS; i++, idx++)
489 if (reg->wrmask & (1 << i))
490 if (!((*b)[idx / 8] & (1 << (idx % 8))))
491 (*a)[idx / 8] |= 1 << (idx % 8);
492 }
493
494 static inline unsigned regmask_get(regmask_t *regmask,
495 struct ir3_register *reg)
496 {
497 unsigned idx = regmask_idx(reg);
498 unsigned i;
499 for (i = 0; i < IR3_INSTR_SRCS; i++, idx++)
500 if (reg->wrmask & (1 << i))
501 if ((*regmask)[idx / 8] & (1 << (idx % 8)))
502 return true;
503 return false;
504 }
505
506 /* ************************************************************************* */
507
508 #endif /* IR3_H_ */