freedreno/ir3: fix regmask for merged regs
[mesa.git] / src / freedreno / ir3 / ir3.c
1 /*
2 * Copyright (c) 2012 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 #include "ir3.h"
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <stdbool.h>
31 #include <errno.h>
32
33 #include "util/bitscan.h"
34 #include "util/ralloc.h"
35 #include "util/u_math.h"
36
37 #include "instr-a3xx.h"
38 #include "ir3_compiler.h"
39
40 /* simple allocator to carve allocations out of an up-front allocated heap,
41 * so that we can free everything easily in one shot.
42 */
43 void * ir3_alloc(struct ir3 *shader, int sz)
44 {
45 return rzalloc_size(shader, sz); /* TODO: don't use rzalloc */
46 }
47
48 struct ir3 * ir3_create(struct ir3_compiler *compiler,
49 unsigned nin, unsigned nout)
50 {
51 struct ir3 *shader = rzalloc(compiler, struct ir3);
52
53 shader->compiler = compiler;
54 shader->ninputs = nin;
55 shader->inputs = ir3_alloc(shader, sizeof(shader->inputs[0]) * nin);
56
57 shader->noutputs = nout;
58 shader->outputs = ir3_alloc(shader, sizeof(shader->outputs[0]) * nout);
59
60 list_inithead(&shader->block_list);
61 list_inithead(&shader->array_list);
62
63 return shader;
64 }
65
66 void ir3_destroy(struct ir3 *shader)
67 {
68 ralloc_free(shader);
69 }
70
71 #define iassert(cond) do { \
72 if (!(cond)) { \
73 debug_assert(cond); \
74 return -1; \
75 } } while (0)
76
77 #define iassert_type(reg, full) do { \
78 if ((full)) { \
79 iassert(!((reg)->flags & IR3_REG_HALF)); \
80 } else { \
81 iassert((reg)->flags & IR3_REG_HALF); \
82 } } while (0);
83
84 static uint32_t reg(struct ir3_register *reg, struct ir3_info *info,
85 uint32_t repeat, uint32_t valid_flags)
86 {
87 reg_t val = { .dummy32 = 0 };
88
89 if (reg->flags & ~valid_flags) {
90 debug_printf("INVALID FLAGS: %x vs %x\n",
91 reg->flags, valid_flags);
92 }
93
94 if (!(reg->flags & IR3_REG_R))
95 repeat = 0;
96
97 if (reg->flags & IR3_REG_IMMED) {
98 val.iim_val = reg->iim_val;
99 } else {
100 unsigned components;
101 int16_t max;
102
103 if (reg->flags & IR3_REG_RELATIV) {
104 components = reg->size;
105 val.idummy10 = reg->array.offset;
106 max = (reg->array.offset + repeat + components - 1) >> 2;
107 } else {
108 components = util_last_bit(reg->wrmask);
109 val.comp = reg->num & 0x3;
110 val.num = reg->num >> 2;
111 max = (reg->num + repeat + components - 1) >> 2;
112 }
113
114 if (reg->flags & IR3_REG_CONST) {
115 info->max_const = MAX2(info->max_const, max);
116 } else if (val.num == 63) {
117 /* ignore writes to dummy register r63.x */
118 } else if (max < 48) {
119 if (reg->flags & IR3_REG_HALF) {
120 if (info->gpu_id >= 600) {
121 /* starting w/ a6xx, half regs conflict with full regs: */
122 info->max_reg = MAX2(info->max_reg, (max+1)/2);
123 } else {
124 info->max_half_reg = MAX2(info->max_half_reg, max);
125 }
126 } else {
127 info->max_reg = MAX2(info->max_reg, max);
128 }
129 }
130 }
131
132 return val.dummy32;
133 }
134
135 static int emit_cat0(struct ir3_instruction *instr, void *ptr,
136 struct ir3_info *info)
137 {
138 instr_cat0_t *cat0 = ptr;
139
140 if (info->gpu_id >= 500) {
141 cat0->a5xx.immed = instr->cat0.immed;
142 } else if (info->gpu_id >= 400) {
143 cat0->a4xx.immed = instr->cat0.immed;
144 } else {
145 cat0->a3xx.immed = instr->cat0.immed;
146 }
147 cat0->repeat = instr->repeat;
148 cat0->ss = !!(instr->flags & IR3_INSTR_SS);
149 cat0->inv = instr->cat0.inv;
150 cat0->comp = instr->cat0.comp;
151 cat0->opc = instr->opc;
152 cat0->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
153 cat0->sync = !!(instr->flags & IR3_INSTR_SY);
154 cat0->opc_cat = 0;
155
156 return 0;
157 }
158
159 static int emit_cat1(struct ir3_instruction *instr, void *ptr,
160 struct ir3_info *info)
161 {
162 struct ir3_register *dst = instr->regs[0];
163 struct ir3_register *src = instr->regs[1];
164 instr_cat1_t *cat1 = ptr;
165
166 iassert(instr->regs_count == 2);
167 iassert_type(dst, type_size(instr->cat1.dst_type) == 32);
168 if (!(src->flags & IR3_REG_IMMED))
169 iassert_type(src, type_size(instr->cat1.src_type) == 32);
170
171 if (src->flags & IR3_REG_IMMED) {
172 cat1->iim_val = src->iim_val;
173 cat1->src_im = 1;
174 } else if (src->flags & IR3_REG_RELATIV) {
175 cat1->off = reg(src, info, instr->repeat,
176 IR3_REG_R | IR3_REG_CONST | IR3_REG_HALF | IR3_REG_RELATIV);
177 cat1->src_rel = 1;
178 cat1->src_rel_c = !!(src->flags & IR3_REG_CONST);
179 } else {
180 cat1->src = reg(src, info, instr->repeat,
181 IR3_REG_R | IR3_REG_CONST | IR3_REG_HALF);
182 cat1->src_c = !!(src->flags & IR3_REG_CONST);
183 }
184
185 cat1->dst = reg(dst, info, instr->repeat,
186 IR3_REG_RELATIV | IR3_REG_EVEN |
187 IR3_REG_R | IR3_REG_POS_INF | IR3_REG_HALF);
188 cat1->repeat = instr->repeat;
189 cat1->src_r = !!(src->flags & IR3_REG_R);
190 cat1->ss = !!(instr->flags & IR3_INSTR_SS);
191 cat1->ul = !!(instr->flags & IR3_INSTR_UL);
192 cat1->dst_type = instr->cat1.dst_type;
193 cat1->dst_rel = !!(dst->flags & IR3_REG_RELATIV);
194 cat1->src_type = instr->cat1.src_type;
195 cat1->even = !!(dst->flags & IR3_REG_EVEN);
196 cat1->pos_inf = !!(dst->flags & IR3_REG_POS_INF);
197 cat1->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
198 cat1->sync = !!(instr->flags & IR3_INSTR_SY);
199 cat1->opc_cat = 1;
200
201 return 0;
202 }
203
204 static int emit_cat2(struct ir3_instruction *instr, void *ptr,
205 struct ir3_info *info)
206 {
207 struct ir3_register *dst = instr->regs[0];
208 struct ir3_register *src1 = instr->regs[1];
209 struct ir3_register *src2 = instr->regs[2];
210 instr_cat2_t *cat2 = ptr;
211 unsigned absneg = ir3_cat2_absneg(instr->opc);
212
213 iassert((instr->regs_count == 2) || (instr->regs_count == 3));
214
215 if (instr->nop) {
216 iassert(!instr->repeat);
217 iassert(instr->nop <= 3);
218
219 cat2->src1_r = instr->nop & 0x1;
220 cat2->src2_r = (instr->nop >> 1) & 0x1;
221 } else {
222 cat2->src1_r = !!(src1->flags & IR3_REG_R);
223 if (src2)
224 cat2->src2_r = !!(src2->flags & IR3_REG_R);
225 }
226
227 if (src1->flags & IR3_REG_RELATIV) {
228 iassert(src1->array.offset < (1 << 10));
229 cat2->rel1.src1 = reg(src1, info, instr->repeat,
230 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_R |
231 IR3_REG_HALF | absneg);
232 cat2->rel1.src1_c = !!(src1->flags & IR3_REG_CONST);
233 cat2->rel1.src1_rel = 1;
234 } else if (src1->flags & IR3_REG_CONST) {
235 iassert(src1->num < (1 << 12));
236 cat2->c1.src1 = reg(src1, info, instr->repeat,
237 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF);
238 cat2->c1.src1_c = 1;
239 } else {
240 iassert(src1->num < (1 << 11));
241 cat2->src1 = reg(src1, info, instr->repeat,
242 IR3_REG_IMMED | IR3_REG_R | IR3_REG_HALF |
243 absneg);
244 }
245 cat2->src1_im = !!(src1->flags & IR3_REG_IMMED);
246 cat2->src1_neg = !!(src1->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
247 cat2->src1_abs = !!(src1->flags & (IR3_REG_FABS | IR3_REG_SABS));
248
249 if (src2) {
250 iassert((src2->flags & IR3_REG_IMMED) ||
251 !((src1->flags ^ src2->flags) & IR3_REG_HALF));
252
253 if (src2->flags & IR3_REG_RELATIV) {
254 iassert(src2->array.offset < (1 << 10));
255 cat2->rel2.src2 = reg(src2, info, instr->repeat,
256 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_R |
257 IR3_REG_HALF | absneg);
258 cat2->rel2.src2_c = !!(src2->flags & IR3_REG_CONST);
259 cat2->rel2.src2_rel = 1;
260 } else if (src2->flags & IR3_REG_CONST) {
261 iassert(src2->num < (1 << 12));
262 cat2->c2.src2 = reg(src2, info, instr->repeat,
263 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF);
264 cat2->c2.src2_c = 1;
265 } else {
266 iassert(src2->num < (1 << 11));
267 cat2->src2 = reg(src2, info, instr->repeat,
268 IR3_REG_IMMED | IR3_REG_R | IR3_REG_HALF |
269 absneg);
270 }
271
272 cat2->src2_im = !!(src2->flags & IR3_REG_IMMED);
273 cat2->src2_neg = !!(src2->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
274 cat2->src2_abs = !!(src2->flags & (IR3_REG_FABS | IR3_REG_SABS));
275 }
276
277 cat2->dst = reg(dst, info, instr->repeat,
278 IR3_REG_R | IR3_REG_EI | IR3_REG_HALF);
279 cat2->repeat = instr->repeat;
280 cat2->sat = !!(instr->flags & IR3_INSTR_SAT);
281 cat2->ss = !!(instr->flags & IR3_INSTR_SS);
282 cat2->ul = !!(instr->flags & IR3_INSTR_UL);
283 cat2->dst_half = !!((src1->flags ^ dst->flags) & IR3_REG_HALF);
284 cat2->ei = !!(dst->flags & IR3_REG_EI);
285 cat2->cond = instr->cat2.condition;
286 cat2->full = ! (src1->flags & IR3_REG_HALF);
287 cat2->opc = instr->opc;
288 cat2->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
289 cat2->sync = !!(instr->flags & IR3_INSTR_SY);
290 cat2->opc_cat = 2;
291
292 return 0;
293 }
294
295 static int emit_cat3(struct ir3_instruction *instr, void *ptr,
296 struct ir3_info *info)
297 {
298 struct ir3_register *dst = instr->regs[0];
299 struct ir3_register *src1 = instr->regs[1];
300 struct ir3_register *src2 = instr->regs[2];
301 struct ir3_register *src3 = instr->regs[3];
302 unsigned absneg = ir3_cat3_absneg(instr->opc);
303 instr_cat3_t *cat3 = ptr;
304 uint32_t src_flags = 0;
305
306 switch (instr->opc) {
307 case OPC_MAD_F16:
308 case OPC_MAD_U16:
309 case OPC_MAD_S16:
310 case OPC_SEL_B16:
311 case OPC_SEL_S16:
312 case OPC_SEL_F16:
313 case OPC_SAD_S16:
314 case OPC_SAD_S32: // really??
315 src_flags |= IR3_REG_HALF;
316 break;
317 default:
318 break;
319 }
320
321 iassert(instr->regs_count == 4);
322 iassert(!((src1->flags ^ src_flags) & IR3_REG_HALF));
323 iassert(!((src2->flags ^ src_flags) & IR3_REG_HALF));
324 iassert(!((src3->flags ^ src_flags) & IR3_REG_HALF));
325
326 if (instr->nop) {
327 iassert(!instr->repeat);
328 iassert(instr->nop <= 3);
329
330 cat3->src1_r = instr->nop & 0x1;
331 cat3->src2_r = (instr->nop >> 1) & 0x1;
332 } else {
333 cat3->src1_r = !!(src1->flags & IR3_REG_R);
334 cat3->src2_r = !!(src2->flags & IR3_REG_R);
335 }
336
337 if (src1->flags & IR3_REG_RELATIV) {
338 iassert(src1->array.offset < (1 << 10));
339 cat3->rel1.src1 = reg(src1, info, instr->repeat,
340 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_R |
341 IR3_REG_HALF | absneg);
342 cat3->rel1.src1_c = !!(src1->flags & IR3_REG_CONST);
343 cat3->rel1.src1_rel = 1;
344 } else if (src1->flags & IR3_REG_CONST) {
345 iassert(src1->num < (1 << 12));
346 cat3->c1.src1 = reg(src1, info, instr->repeat,
347 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF);
348 cat3->c1.src1_c = 1;
349 } else {
350 iassert(src1->num < (1 << 11));
351 cat3->src1 = reg(src1, info, instr->repeat,
352 IR3_REG_R | IR3_REG_HALF | absneg);
353 }
354
355 cat3->src1_neg = !!(src1->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
356
357 cat3->src2 = reg(src2, info, instr->repeat,
358 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF | absneg);
359 cat3->src2_c = !!(src2->flags & IR3_REG_CONST);
360 cat3->src2_neg = !!(src2->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
361
362 if (src3->flags & IR3_REG_RELATIV) {
363 iassert(src3->array.offset < (1 << 10));
364 cat3->rel2.src3 = reg(src3, info, instr->repeat,
365 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_R |
366 IR3_REG_HALF | absneg);
367 cat3->rel2.src3_c = !!(src3->flags & IR3_REG_CONST);
368 cat3->rel2.src3_rel = 1;
369 } else if (src3->flags & IR3_REG_CONST) {
370 iassert(src3->num < (1 << 12));
371 cat3->c2.src3 = reg(src3, info, instr->repeat,
372 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF);
373 cat3->c2.src3_c = 1;
374 } else {
375 iassert(src3->num < (1 << 11));
376 cat3->src3 = reg(src3, info, instr->repeat,
377 IR3_REG_R | IR3_REG_HALF | absneg);
378 }
379
380 cat3->src3_neg = !!(src3->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
381 cat3->src3_r = !!(src3->flags & IR3_REG_R);
382
383 cat3->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
384 cat3->repeat = instr->repeat;
385 cat3->sat = !!(instr->flags & IR3_INSTR_SAT);
386 cat3->ss = !!(instr->flags & IR3_INSTR_SS);
387 cat3->ul = !!(instr->flags & IR3_INSTR_UL);
388 cat3->dst_half = !!((src_flags ^ dst->flags) & IR3_REG_HALF);
389 cat3->opc = instr->opc;
390 cat3->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
391 cat3->sync = !!(instr->flags & IR3_INSTR_SY);
392 cat3->opc_cat = 3;
393
394 return 0;
395 }
396
397 static int emit_cat4(struct ir3_instruction *instr, void *ptr,
398 struct ir3_info *info)
399 {
400 struct ir3_register *dst = instr->regs[0];
401 struct ir3_register *src = instr->regs[1];
402 instr_cat4_t *cat4 = ptr;
403
404 iassert(instr->regs_count == 2);
405
406 if (src->flags & IR3_REG_RELATIV) {
407 iassert(src->array.offset < (1 << 10));
408 cat4->rel.src = reg(src, info, instr->repeat,
409 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_FNEG |
410 IR3_REG_FABS | IR3_REG_R | IR3_REG_HALF);
411 cat4->rel.src_c = !!(src->flags & IR3_REG_CONST);
412 cat4->rel.src_rel = 1;
413 } else if (src->flags & IR3_REG_CONST) {
414 iassert(src->num < (1 << 12));
415 cat4->c.src = reg(src, info, instr->repeat,
416 IR3_REG_CONST | IR3_REG_FNEG | IR3_REG_FABS |
417 IR3_REG_R | IR3_REG_HALF);
418 cat4->c.src_c = 1;
419 } else {
420 iassert(src->num < (1 << 11));
421 cat4->src = reg(src, info, instr->repeat,
422 IR3_REG_IMMED | IR3_REG_FNEG | IR3_REG_FABS |
423 IR3_REG_R | IR3_REG_HALF);
424 }
425
426 cat4->src_im = !!(src->flags & IR3_REG_IMMED);
427 cat4->src_neg = !!(src->flags & IR3_REG_FNEG);
428 cat4->src_abs = !!(src->flags & IR3_REG_FABS);
429 cat4->src_r = !!(src->flags & IR3_REG_R);
430
431 cat4->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
432 cat4->repeat = instr->repeat;
433 cat4->sat = !!(instr->flags & IR3_INSTR_SAT);
434 cat4->ss = !!(instr->flags & IR3_INSTR_SS);
435 cat4->ul = !!(instr->flags & IR3_INSTR_UL);
436 cat4->dst_half = !!((src->flags ^ dst->flags) & IR3_REG_HALF);
437 cat4->full = ! (src->flags & IR3_REG_HALF);
438 cat4->opc = instr->opc;
439 cat4->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
440 cat4->sync = !!(instr->flags & IR3_INSTR_SY);
441 cat4->opc_cat = 4;
442
443 return 0;
444 }
445
446 static int emit_cat5(struct ir3_instruction *instr, void *ptr,
447 struct ir3_info *info)
448 {
449 struct ir3_register *dst = instr->regs[0];
450 /* To simplify things when there could be zero, one, or two args other
451 * than tex/sampler idx, we use the first src reg in the ir to hold
452 * samp_tex hvec2:
453 */
454 struct ir3_register *src1 = instr->regs[2];
455 struct ir3_register *src2 = instr->regs[3];
456 instr_cat5_t *cat5 = ptr;
457
458 iassert_type(dst, type_size(instr->cat5.type) == 32)
459
460 assume(src1 || !src2);
461
462 if (src1) {
463 cat5->full = ! (src1->flags & IR3_REG_HALF);
464 cat5->src1 = reg(src1, info, instr->repeat, IR3_REG_HALF);
465 }
466
467 if (instr->flags & IR3_INSTR_S2EN) {
468 struct ir3_register *samp_tex = instr->regs[1];
469 if (src2) {
470 iassert(!((src1->flags ^ src2->flags) & IR3_REG_HALF));
471 cat5->s2en.src2 = reg(src2, info, instr->repeat, IR3_REG_HALF);
472 }
473 iassert(samp_tex->flags & IR3_REG_HALF);
474 cat5->s2en.src3 = reg(samp_tex, info, instr->repeat, IR3_REG_HALF);
475 iassert(!(instr->cat5.samp | instr->cat5.tex));
476 } else {
477 if (src2) {
478 iassert(!((src1->flags ^ src2->flags) & IR3_REG_HALF));
479 cat5->norm.src2 = reg(src2, info, instr->repeat, IR3_REG_HALF);
480 }
481 cat5->norm.samp = instr->cat5.samp;
482 cat5->norm.tex = instr->cat5.tex;
483 }
484
485 cat5->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
486 cat5->wrmask = dst->wrmask;
487 cat5->type = instr->cat5.type;
488 cat5->is_3d = !!(instr->flags & IR3_INSTR_3D);
489 cat5->is_a = !!(instr->flags & IR3_INSTR_A);
490 cat5->is_s = !!(instr->flags & IR3_INSTR_S);
491 cat5->is_s2en = !!(instr->flags & IR3_INSTR_S2EN);
492 cat5->is_o = !!(instr->flags & IR3_INSTR_O);
493 cat5->is_p = !!(instr->flags & IR3_INSTR_P);
494 cat5->opc = instr->opc;
495 cat5->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
496 cat5->sync = !!(instr->flags & IR3_INSTR_SY);
497 cat5->opc_cat = 5;
498
499 return 0;
500 }
501
502 static int emit_cat6_a6xx(struct ir3_instruction *instr, void *ptr,
503 struct ir3_info *info)
504 {
505 struct ir3_register *src1, *src2;
506 instr_cat6_a6xx_t *cat6 = ptr;
507 bool has_dest = (instr->opc == OPC_LDIB);
508
509 /* first reg should be SSBO binding point: */
510 iassert(instr->regs[1]->flags & IR3_REG_IMMED);
511
512 src1 = instr->regs[2];
513
514 if (has_dest) {
515 /* the src2 field in the instruction is actually the destination
516 * register for load instructions:
517 */
518 src2 = instr->regs[0];
519 } else {
520 src2 = instr->regs[3];
521 }
522
523 cat6->type = instr->cat6.type;
524 cat6->d = instr->cat6.d - 1;
525 cat6->typed = instr->cat6.typed;
526 cat6->type_size = instr->cat6.iim_val - 1;
527 cat6->opc = instr->opc;
528 cat6->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
529 cat6->sync = !!(instr->flags & IR3_INSTR_SY);
530 cat6->opc_cat = 6;
531
532 cat6->src1 = reg(src1, info, instr->repeat, 0);
533 cat6->src2 = reg(src2, info, instr->repeat, 0);
534 cat6->ssbo = instr->regs[1]->iim_val;
535
536 switch (instr->opc) {
537 case OPC_ATOMIC_ADD:
538 case OPC_ATOMIC_SUB:
539 case OPC_ATOMIC_XCHG:
540 case OPC_ATOMIC_INC:
541 case OPC_ATOMIC_DEC:
542 case OPC_ATOMIC_CMPXCHG:
543 case OPC_ATOMIC_MIN:
544 case OPC_ATOMIC_MAX:
545 case OPC_ATOMIC_AND:
546 case OPC_ATOMIC_OR:
547 case OPC_ATOMIC_XOR:
548 cat6->pad1 = 0x1;
549 cat6->pad2 = 0xc;
550 cat6->pad3 = 0x0;
551 cat6->pad4 = 0x3;
552 break;
553 case OPC_STIB:
554 cat6->pad1 = 0x0;
555 cat6->pad2 = 0xc;
556 cat6->pad3 = 0x0;
557 cat6->pad4 = 0x2;
558 break;
559 case OPC_LDIB:
560 cat6->pad1 = 0x1;
561 cat6->pad2 = 0xc;
562 cat6->pad3 = 0x0;
563 cat6->pad4 = 0x2;
564 break;
565 case OPC_LDC:
566 cat6->pad1 = 0x0;
567 cat6->pad2 = 0x8;
568 cat6->pad3 = 0x0;
569 cat6->pad4 = 0x2;
570 break;
571 default:
572 iassert(0);
573 }
574
575 return 0;
576 }
577
578 static int emit_cat6(struct ir3_instruction *instr, void *ptr,
579 struct ir3_info *info)
580 {
581 struct ir3_register *dst, *src1, *src2;
582 instr_cat6_t *cat6 = ptr;
583
584 /* In a6xx we start using a new instruction encoding for some of
585 * these instructions:
586 */
587 if (info->gpu_id >= 600) {
588 switch (instr->opc) {
589 case OPC_ATOMIC_ADD:
590 case OPC_ATOMIC_SUB:
591 case OPC_ATOMIC_XCHG:
592 case OPC_ATOMIC_INC:
593 case OPC_ATOMIC_DEC:
594 case OPC_ATOMIC_CMPXCHG:
595 case OPC_ATOMIC_MIN:
596 case OPC_ATOMIC_MAX:
597 case OPC_ATOMIC_AND:
598 case OPC_ATOMIC_OR:
599 case OPC_ATOMIC_XOR:
600 /* The shared variants of these still use the old encoding: */
601 if (!(instr->flags & IR3_INSTR_G))
602 break;
603 /* fallthrough */
604 case OPC_STIB:
605 case OPC_LDIB:
606 case OPC_LDC:
607 return emit_cat6_a6xx(instr, ptr, info);
608 default:
609 break;
610 }
611 }
612
613 bool type_full = type_size(instr->cat6.type) == 32;
614
615 cat6->type = instr->cat6.type;
616 cat6->opc = instr->opc;
617 cat6->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
618 cat6->sync = !!(instr->flags & IR3_INSTR_SY);
619 cat6->g = !!(instr->flags & IR3_INSTR_G);
620 cat6->opc_cat = 6;
621
622 switch (instr->opc) {
623 case OPC_RESINFO:
624 case OPC_RESFMT:
625 iassert_type(instr->regs[0], type_full); /* dst */
626 iassert_type(instr->regs[1], type_full); /* src1 */
627 break;
628 case OPC_L2G:
629 case OPC_G2L:
630 iassert_type(instr->regs[0], true); /* dst */
631 iassert_type(instr->regs[1], true); /* src1 */
632 break;
633 case OPC_STG:
634 case OPC_STL:
635 case OPC_STP:
636 case OPC_STLW:
637 case OPC_STIB:
638 /* no dst, so regs[0] is dummy */
639 iassert_type(instr->regs[1], true); /* dst */
640 iassert_type(instr->regs[2], type_full); /* src1 */
641 iassert_type(instr->regs[3], true); /* src2 */
642 break;
643 default:
644 iassert_type(instr->regs[0], type_full); /* dst */
645 iassert_type(instr->regs[1], true); /* src1 */
646 if (instr->regs_count > 2)
647 iassert_type(instr->regs[2], true); /* src1 */
648 break;
649 }
650
651 /* the "dst" for a store instruction is (from the perspective
652 * of data flow in the shader, ie. register use/def, etc) in
653 * fact a register that is read by the instruction, rather
654 * than written:
655 */
656 if (is_store(instr)) {
657 iassert(instr->regs_count >= 3);
658
659 dst = instr->regs[1];
660 src1 = instr->regs[2];
661 src2 = (instr->regs_count >= 4) ? instr->regs[3] : NULL;
662 } else {
663 iassert(instr->regs_count >= 2);
664
665 dst = instr->regs[0];
666 src1 = instr->regs[1];
667 src2 = (instr->regs_count >= 3) ? instr->regs[2] : NULL;
668 }
669
670 /* TODO we need a more comprehensive list about which instructions
671 * can be encoded which way. Or possibly use IR3_INSTR_0 flag to
672 * indicate to use the src_off encoding even if offset is zero
673 * (but then what to do about dst_off?)
674 */
675 if (is_atomic(instr->opc)) {
676 instr_cat6ldgb_t *ldgb = ptr;
677
678 /* maybe these two bits both determine the instruction encoding? */
679 cat6->src_off = false;
680
681 ldgb->d = instr->cat6.d - 1;
682 ldgb->typed = instr->cat6.typed;
683 ldgb->type_size = instr->cat6.iim_val - 1;
684
685 ldgb->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
686
687 if (ldgb->g) {
688 struct ir3_register *src3 = instr->regs[3];
689 struct ir3_register *src4 = instr->regs[4];
690
691 /* first src is src_ssbo: */
692 iassert(src1->flags & IR3_REG_IMMED);
693 ldgb->src_ssbo = src1->uim_val;
694
695 ldgb->src1 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
696 ldgb->src1_im = !!(src2->flags & IR3_REG_IMMED);
697 ldgb->src2 = reg(src3, info, instr->repeat, IR3_REG_IMMED);
698 ldgb->src2_im = !!(src3->flags & IR3_REG_IMMED);
699
700 ldgb->src3 = reg(src4, info, instr->repeat, 0);
701 ldgb->pad0 = 0x1;
702 ldgb->pad3 = 0x1;
703 } else {
704 ldgb->src1 = reg(src1, info, instr->repeat, IR3_REG_IMMED);
705 ldgb->src1_im = !!(src1->flags & IR3_REG_IMMED);
706 ldgb->src2 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
707 ldgb->src2_im = !!(src2->flags & IR3_REG_IMMED);
708 ldgb->pad0 = 0x1;
709 ldgb->pad3 = 0x0;
710 }
711
712 return 0;
713 } else if (instr->opc == OPC_LDGB) {
714 struct ir3_register *src3 = instr->regs[3];
715 instr_cat6ldgb_t *ldgb = ptr;
716
717 /* maybe these two bits both determine the instruction encoding? */
718 cat6->src_off = false;
719
720 ldgb->d = instr->cat6.d - 1;
721 ldgb->typed = instr->cat6.typed;
722 ldgb->type_size = instr->cat6.iim_val - 1;
723
724 ldgb->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
725
726 /* first src is src_ssbo: */
727 iassert(src1->flags & IR3_REG_IMMED);
728 ldgb->src_ssbo = src1->uim_val;
729
730 /* then next two are src1/src2: */
731 ldgb->src1 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
732 ldgb->src1_im = !!(src2->flags & IR3_REG_IMMED);
733 ldgb->src2 = reg(src3, info, instr->repeat, IR3_REG_IMMED);
734 ldgb->src2_im = !!(src3->flags & IR3_REG_IMMED);
735
736 ldgb->pad0 = 0x0;
737 ldgb->pad3 = 0x1;
738
739 return 0;
740 } else if (instr->opc == OPC_RESINFO) {
741 instr_cat6ldgb_t *ldgb = ptr;
742
743 ldgb->d = instr->cat6.d - 1;
744
745 ldgb->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
746
747 /* first src is src_ssbo: */
748 iassert(src1->flags & IR3_REG_IMMED);
749 ldgb->src_ssbo = src1->uim_val;
750
751 return 0;
752 } else if ((instr->opc == OPC_STGB) || (instr->opc == OPC_STIB)) {
753 struct ir3_register *src3 = instr->regs[4];
754 instr_cat6stgb_t *stgb = ptr;
755
756 /* maybe these two bits both determine the instruction encoding? */
757 cat6->src_off = true;
758 stgb->pad3 = 0x2;
759
760 stgb->d = instr->cat6.d - 1;
761 stgb->typed = instr->cat6.typed;
762 stgb->type_size = instr->cat6.iim_val - 1;
763
764 /* first src is dst_ssbo: */
765 iassert(dst->flags & IR3_REG_IMMED);
766 stgb->dst_ssbo = dst->uim_val;
767
768 /* then src1/src2/src3: */
769 stgb->src1 = reg(src1, info, instr->repeat, 0);
770 stgb->src2 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
771 stgb->src2_im = !!(src2->flags & IR3_REG_IMMED);
772 stgb->src3 = reg(src3, info, instr->repeat, IR3_REG_IMMED);
773 stgb->src3_im = !!(src3->flags & IR3_REG_IMMED);
774
775 return 0;
776 } else if (instr->cat6.src_offset || (instr->opc == OPC_LDG) ||
777 (instr->opc == OPC_LDL)) {
778 instr_cat6a_t *cat6a = ptr;
779
780 cat6->src_off = true;
781
782 cat6a->src1 = reg(src1, info, instr->repeat, IR3_REG_IMMED);
783 cat6a->src1_im = !!(src1->flags & IR3_REG_IMMED);
784 if (src2) {
785 cat6a->src2 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
786 cat6a->src2_im = !!(src2->flags & IR3_REG_IMMED);
787 }
788 cat6a->off = instr->cat6.src_offset;
789 } else {
790 instr_cat6b_t *cat6b = ptr;
791
792 cat6->src_off = false;
793
794 cat6b->src1 = reg(src1, info, instr->repeat, IR3_REG_IMMED | IR3_REG_HALF);
795 cat6b->src1_im = !!(src1->flags & IR3_REG_IMMED);
796 if (src2) {
797 cat6b->src2 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
798 cat6b->src2_im = !!(src2->flags & IR3_REG_IMMED);
799 }
800 }
801
802 if (instr->cat6.dst_offset || (instr->opc == OPC_STG) ||
803 (instr->opc == OPC_STL)) {
804 instr_cat6c_t *cat6c = ptr;
805 cat6->dst_off = true;
806 cat6c->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
807 cat6c->off = instr->cat6.dst_offset;
808 } else {
809 instr_cat6d_t *cat6d = ptr;
810 cat6->dst_off = false;
811 cat6d->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
812 }
813
814 return 0;
815 }
816
817 static int emit_cat7(struct ir3_instruction *instr, void *ptr,
818 struct ir3_info *info)
819 {
820 instr_cat7_t *cat7 = ptr;
821
822 cat7->ss = !!(instr->flags & IR3_INSTR_SS);
823 cat7->w = instr->cat7.w;
824 cat7->r = instr->cat7.r;
825 cat7->l = instr->cat7.l;
826 cat7->g = instr->cat7.g;
827 cat7->opc = instr->opc;
828 cat7->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
829 cat7->sync = !!(instr->flags & IR3_INSTR_SY);
830 cat7->opc_cat = 7;
831
832 return 0;
833 }
834
835 static int (*emit[])(struct ir3_instruction *instr, void *ptr,
836 struct ir3_info *info) = {
837 emit_cat0, emit_cat1, emit_cat2, emit_cat3, emit_cat4, emit_cat5, emit_cat6,
838 emit_cat7,
839 };
840
841 void * ir3_assemble(struct ir3 *shader, struct ir3_info *info,
842 uint32_t gpu_id)
843 {
844 uint32_t *ptr, *dwords;
845
846 info->gpu_id = gpu_id;
847 info->max_reg = -1;
848 info->max_half_reg = -1;
849 info->max_const = -1;
850 info->instrs_count = 0;
851 info->sizedwords = 0;
852 info->ss = info->sy = 0;
853
854 list_for_each_entry (struct ir3_block, block, &shader->block_list, node) {
855 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
856 info->sizedwords += 2;
857 }
858 }
859
860 /* need an integer number of instruction "groups" (sets of 16
861 * instructions on a4xx or sets of 4 instructions on a3xx),
862 * so pad out w/ NOPs if needed: (NOTE each instruction is 64bits)
863 */
864 if (gpu_id >= 400) {
865 info->sizedwords = align(info->sizedwords, 16 * 2);
866 } else {
867 info->sizedwords = align(info->sizedwords, 4 * 2);
868 }
869
870 ptr = dwords = calloc(4, info->sizedwords);
871
872 list_for_each_entry (struct ir3_block, block, &shader->block_list, node) {
873 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
874 int ret = emit[opc_cat(instr->opc)](instr, dwords, info);
875 if (ret)
876 goto fail;
877 info->instrs_count += 1 + instr->repeat + instr->nop;
878 dwords += 2;
879
880 if (instr->flags & IR3_INSTR_SS)
881 info->ss++;
882
883 if (instr->flags & IR3_INSTR_SY)
884 info->sy++;
885 }
886 }
887
888 return ptr;
889
890 fail:
891 free(ptr);
892 return NULL;
893 }
894
895 static struct ir3_register * reg_create(struct ir3 *shader,
896 int num, int flags)
897 {
898 struct ir3_register *reg =
899 ir3_alloc(shader, sizeof(struct ir3_register));
900 reg->wrmask = 1;
901 reg->flags = flags;
902 reg->num = num;
903 if (shader->compiler->gpu_id >= 600)
904 reg->merged = true;
905 return reg;
906 }
907
908 static void insert_instr(struct ir3_block *block,
909 struct ir3_instruction *instr)
910 {
911 struct ir3 *shader = block->shader;
912 #ifdef DEBUG
913 instr->serialno = ++shader->instr_count;
914 #endif
915 list_addtail(&instr->node, &block->instr_list);
916
917 if (is_input(instr))
918 array_insert(shader, shader->baryfs, instr);
919 }
920
921 struct ir3_block * ir3_block_create(struct ir3 *shader)
922 {
923 struct ir3_block *block = ir3_alloc(shader, sizeof(*block));
924 #ifdef DEBUG
925 block->serialno = ++shader->block_count;
926 #endif
927 block->shader = shader;
928 list_inithead(&block->node);
929 list_inithead(&block->instr_list);
930 return block;
931 }
932
933 static struct ir3_instruction *instr_create(struct ir3_block *block, int nreg)
934 {
935 struct ir3_instruction *instr;
936 unsigned sz = sizeof(*instr) + (nreg * sizeof(instr->regs[0]));
937 char *ptr = ir3_alloc(block->shader, sz);
938
939 instr = (struct ir3_instruction *)ptr;
940 ptr += sizeof(*instr);
941 instr->regs = (struct ir3_register **)ptr;
942
943 #ifdef DEBUG
944 instr->regs_max = nreg;
945 #endif
946
947 return instr;
948 }
949
950 struct ir3_instruction * ir3_instr_create2(struct ir3_block *block,
951 opc_t opc, int nreg)
952 {
953 struct ir3_instruction *instr = instr_create(block, nreg);
954 instr->block = block;
955 instr->opc = opc;
956 insert_instr(block, instr);
957 return instr;
958 }
959
960 struct ir3_instruction * ir3_instr_create(struct ir3_block *block, opc_t opc)
961 {
962 /* NOTE: we could be slightly more clever, at least for non-meta,
963 * and choose # of regs based on category.
964 */
965 return ir3_instr_create2(block, opc, 4);
966 }
967
968 struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr)
969 {
970 struct ir3_instruction *new_instr = instr_create(instr->block,
971 instr->regs_count);
972 struct ir3_register **regs;
973 unsigned i;
974
975 regs = new_instr->regs;
976 *new_instr = *instr;
977 new_instr->regs = regs;
978
979 insert_instr(instr->block, new_instr);
980
981 /* clone registers: */
982 new_instr->regs_count = 0;
983 for (i = 0; i < instr->regs_count; i++) {
984 struct ir3_register *reg = instr->regs[i];
985 struct ir3_register *new_reg =
986 ir3_reg_create(new_instr, reg->num, reg->flags);
987 *new_reg = *reg;
988 }
989
990 return new_instr;
991 }
992
993 /* Add a false dependency to instruction, to ensure it is scheduled first: */
994 void ir3_instr_add_dep(struct ir3_instruction *instr, struct ir3_instruction *dep)
995 {
996 array_insert(instr, instr->deps, dep);
997 }
998
999 struct ir3_register * ir3_reg_create(struct ir3_instruction *instr,
1000 int num, int flags)
1001 {
1002 struct ir3 *shader = instr->block->shader;
1003 struct ir3_register *reg = reg_create(shader, num, flags);
1004 #ifdef DEBUG
1005 debug_assert(instr->regs_count < instr->regs_max);
1006 #endif
1007 instr->regs[instr->regs_count++] = reg;
1008 return reg;
1009 }
1010
1011 struct ir3_register * ir3_reg_clone(struct ir3 *shader,
1012 struct ir3_register *reg)
1013 {
1014 struct ir3_register *new_reg = reg_create(shader, 0, 0);
1015 *new_reg = *reg;
1016 return new_reg;
1017 }
1018
1019 void
1020 ir3_instr_set_address(struct ir3_instruction *instr,
1021 struct ir3_instruction *addr)
1022 {
1023 if (instr->address != addr) {
1024 struct ir3 *ir = instr->block->shader;
1025 instr->address = addr;
1026 array_insert(ir, ir->indirects, instr);
1027 }
1028 }
1029
1030 void
1031 ir3_block_clear_mark(struct ir3_block *block)
1032 {
1033 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node)
1034 instr->flags &= ~IR3_INSTR_MARK;
1035 }
1036
1037 void
1038 ir3_clear_mark(struct ir3 *ir)
1039 {
1040 list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
1041 ir3_block_clear_mark(block);
1042 }
1043 }
1044
1045 /* note: this will destroy instr->depth, don't do it until after sched! */
1046 unsigned
1047 ir3_count_instructions(struct ir3 *ir)
1048 {
1049 unsigned cnt = 0;
1050 list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
1051 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
1052 instr->ip = cnt++;
1053 }
1054 block->start_ip = list_first_entry(&block->instr_list, struct ir3_instruction, node)->ip;
1055 block->end_ip = list_last_entry(&block->instr_list, struct ir3_instruction, node)->ip;
1056 }
1057 return cnt;
1058 }
1059
1060 struct ir3_array *
1061 ir3_lookup_array(struct ir3 *ir, unsigned id)
1062 {
1063 list_for_each_entry (struct ir3_array, arr, &ir->array_list, node)
1064 if (arr->id == id)
1065 return arr;
1066 return NULL;
1067 }