5f4b86d610d2bea990db4c63d57cd5da1b80d7e5
[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, gl_shader_stage type)
49 {
50 struct ir3 *shader = rzalloc(NULL, struct ir3);
51
52 shader->compiler = compiler;
53 shader->type = type;
54
55 list_inithead(&shader->block_list);
56 list_inithead(&shader->array_list);
57
58 return shader;
59 }
60
61 void ir3_destroy(struct ir3 *shader)
62 {
63 ralloc_free(shader);
64 }
65
66 #define iassert(cond) do { \
67 if (!(cond)) { \
68 debug_assert(cond); \
69 return -1; \
70 } } while (0)
71
72 #define iassert_type(reg, full) do { \
73 if ((full)) { \
74 iassert(!((reg)->flags & IR3_REG_HALF)); \
75 } else { \
76 iassert((reg)->flags & IR3_REG_HALF); \
77 } } while (0);
78
79 static uint32_t reg(struct ir3_register *reg, struct ir3_info *info,
80 uint32_t repeat, uint32_t valid_flags)
81 {
82 reg_t val = { .dummy32 = 0 };
83
84 if (reg->flags & ~valid_flags) {
85 debug_printf("INVALID FLAGS: %x vs %x\n",
86 reg->flags, valid_flags);
87 }
88
89 if (!(reg->flags & IR3_REG_R))
90 repeat = 0;
91
92 if (reg->flags & IR3_REG_IMMED) {
93 val.iim_val = reg->iim_val;
94 } else {
95 unsigned components;
96 int16_t max;
97
98 if (reg->flags & IR3_REG_RELATIV) {
99 components = reg->size;
100 val.idummy10 = reg->array.offset;
101 max = (reg->array.offset + repeat + components - 1);
102 } else {
103 components = util_last_bit(reg->wrmask);
104 val.comp = reg->num & 0x3;
105 val.num = reg->num >> 2;
106 max = (reg->num + repeat + components - 1);
107 }
108
109 if (reg->flags & IR3_REG_CONST) {
110 info->max_const = MAX2(info->max_const, max >> 2);
111 } else if (val.num == 63) {
112 /* ignore writes to dummy register r63.x */
113 } else if (max < regid(48, 0)) {
114 if (reg->flags & IR3_REG_HALF) {
115 if (info->gpu_id >= 600) {
116 /* starting w/ a6xx, half regs conflict with full regs: */
117 info->max_reg = MAX2(info->max_reg, max >> 3);
118 } else {
119 info->max_half_reg = MAX2(info->max_half_reg, max >> 2);
120 }
121 } else {
122 info->max_reg = MAX2(info->max_reg, max >> 2);
123 }
124 }
125 }
126
127 return val.dummy32;
128 }
129
130 static int emit_cat0(struct ir3_instruction *instr, void *ptr,
131 struct ir3_info *info)
132 {
133 instr_cat0_t *cat0 = ptr;
134
135 if (info->gpu_id >= 500) {
136 cat0->a5xx.immed = instr->cat0.immed;
137 } else if (info->gpu_id >= 400) {
138 cat0->a4xx.immed = instr->cat0.immed;
139 } else {
140 cat0->a3xx.immed = instr->cat0.immed;
141 }
142 cat0->repeat = instr->repeat;
143 cat0->ss = !!(instr->flags & IR3_INSTR_SS);
144 cat0->inv0 = instr->cat0.inv;
145 cat0->comp0 = instr->cat0.comp;
146 cat0->opc = instr->opc;
147 cat0->opc_hi = instr->opc >= 16;
148 cat0->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
149 cat0->sync = !!(instr->flags & IR3_INSTR_SY);
150 cat0->opc_cat = 0;
151
152 return 0;
153 }
154
155 static int emit_cat1(struct ir3_instruction *instr, void *ptr,
156 struct ir3_info *info)
157 {
158 struct ir3_register *dst = instr->regs[0];
159 struct ir3_register *src = instr->regs[1];
160 instr_cat1_t *cat1 = ptr;
161
162 iassert(instr->regs_count == 2);
163 iassert_type(dst, type_size(instr->cat1.dst_type) == 32);
164 if (!(src->flags & IR3_REG_IMMED))
165 iassert_type(src, type_size(instr->cat1.src_type) == 32);
166
167 if (src->flags & IR3_REG_IMMED) {
168 cat1->iim_val = src->iim_val;
169 cat1->src_im = 1;
170 } else if (src->flags & IR3_REG_RELATIV) {
171 cat1->off = reg(src, info, instr->repeat,
172 IR3_REG_R | IR3_REG_CONST | IR3_REG_HALF | IR3_REG_RELATIV);
173 cat1->src_rel = 1;
174 cat1->src_rel_c = !!(src->flags & IR3_REG_CONST);
175 } else {
176 cat1->src = reg(src, info, instr->repeat,
177 IR3_REG_R | IR3_REG_CONST | IR3_REG_HALF);
178 cat1->src_c = !!(src->flags & IR3_REG_CONST);
179 }
180
181 cat1->dst = reg(dst, info, instr->repeat,
182 IR3_REG_RELATIV | IR3_REG_EVEN |
183 IR3_REG_R | IR3_REG_POS_INF | IR3_REG_HALF);
184 cat1->repeat = instr->repeat;
185 cat1->src_r = !!(src->flags & IR3_REG_R);
186 cat1->ss = !!(instr->flags & IR3_INSTR_SS);
187 cat1->ul = !!(instr->flags & IR3_INSTR_UL);
188 cat1->dst_type = instr->cat1.dst_type;
189 cat1->dst_rel = !!(dst->flags & IR3_REG_RELATIV);
190 cat1->src_type = instr->cat1.src_type;
191 cat1->even = !!(dst->flags & IR3_REG_EVEN);
192 cat1->pos_inf = !!(dst->flags & IR3_REG_POS_INF);
193 cat1->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
194 cat1->sync = !!(instr->flags & IR3_INSTR_SY);
195 cat1->opc_cat = 1;
196
197 return 0;
198 }
199
200 static int emit_cat2(struct ir3_instruction *instr, void *ptr,
201 struct ir3_info *info)
202 {
203 struct ir3_register *dst = instr->regs[0];
204 struct ir3_register *src1 = instr->regs[1];
205 struct ir3_register *src2 = instr->regs[2];
206 instr_cat2_t *cat2 = ptr;
207 unsigned absneg = ir3_cat2_absneg(instr->opc);
208
209 iassert((instr->regs_count == 2) || (instr->regs_count == 3));
210
211 if (instr->nop) {
212 iassert(!instr->repeat);
213 iassert(instr->nop <= 3);
214
215 cat2->src1_r = instr->nop & 0x1;
216 cat2->src2_r = (instr->nop >> 1) & 0x1;
217 } else {
218 cat2->src1_r = !!(src1->flags & IR3_REG_R);
219 if (src2)
220 cat2->src2_r = !!(src2->flags & IR3_REG_R);
221 }
222
223 if (src1->flags & IR3_REG_RELATIV) {
224 iassert(src1->array.offset < (1 << 10));
225 cat2->rel1.src1 = reg(src1, info, instr->repeat,
226 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_R |
227 IR3_REG_HALF | absneg);
228 cat2->rel1.src1_c = !!(src1->flags & IR3_REG_CONST);
229 cat2->rel1.src1_rel = 1;
230 } else if (src1->flags & IR3_REG_CONST) {
231 iassert(src1->num < (1 << 12));
232 cat2->c1.src1 = reg(src1, info, instr->repeat,
233 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF |
234 absneg);
235 cat2->c1.src1_c = 1;
236 } else {
237 iassert(src1->num < (1 << 11));
238 cat2->src1 = reg(src1, info, instr->repeat,
239 IR3_REG_IMMED | IR3_REG_R | IR3_REG_HALF |
240 absneg);
241 }
242 cat2->src1_im = !!(src1->flags & IR3_REG_IMMED);
243 cat2->src1_neg = !!(src1->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
244 cat2->src1_abs = !!(src1->flags & (IR3_REG_FABS | IR3_REG_SABS));
245
246 if (src2) {
247 iassert((src2->flags & IR3_REG_IMMED) ||
248 !((src1->flags ^ src2->flags) & IR3_REG_HALF));
249
250 if (src2->flags & IR3_REG_RELATIV) {
251 iassert(src2->array.offset < (1 << 10));
252 cat2->rel2.src2 = reg(src2, info, instr->repeat,
253 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_R |
254 IR3_REG_HALF | absneg);
255 cat2->rel2.src2_c = !!(src2->flags & IR3_REG_CONST);
256 cat2->rel2.src2_rel = 1;
257 } else if (src2->flags & IR3_REG_CONST) {
258 iassert(src2->num < (1 << 12));
259 cat2->c2.src2 = reg(src2, info, instr->repeat,
260 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF |
261 absneg);
262 cat2->c2.src2_c = 1;
263 } else {
264 iassert(src2->num < (1 << 11));
265 cat2->src2 = reg(src2, info, instr->repeat,
266 IR3_REG_IMMED | IR3_REG_R | IR3_REG_HALF |
267 absneg);
268 }
269
270 cat2->src2_im = !!(src2->flags & IR3_REG_IMMED);
271 cat2->src2_neg = !!(src2->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
272 cat2->src2_abs = !!(src2->flags & (IR3_REG_FABS | IR3_REG_SABS));
273 }
274
275 cat2->dst = reg(dst, info, instr->repeat,
276 IR3_REG_R | IR3_REG_EI | IR3_REG_HALF);
277 cat2->repeat = instr->repeat;
278 cat2->sat = !!(instr->flags & IR3_INSTR_SAT);
279 cat2->ss = !!(instr->flags & IR3_INSTR_SS);
280 cat2->ul = !!(instr->flags & IR3_INSTR_UL);
281 cat2->dst_half = !!((src1->flags ^ dst->flags) & IR3_REG_HALF);
282 cat2->ei = !!(dst->flags & IR3_REG_EI);
283 cat2->cond = instr->cat2.condition;
284 cat2->full = ! (src1->flags & IR3_REG_HALF);
285 cat2->opc = instr->opc;
286 cat2->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
287 cat2->sync = !!(instr->flags & IR3_INSTR_SY);
288 cat2->opc_cat = 2;
289
290 return 0;
291 }
292
293 static int emit_cat3(struct ir3_instruction *instr, void *ptr,
294 struct ir3_info *info)
295 {
296 struct ir3_register *dst = instr->regs[0];
297 struct ir3_register *src1 = instr->regs[1];
298 struct ir3_register *src2 = instr->regs[2];
299 struct ir3_register *src3 = instr->regs[3];
300 unsigned absneg = ir3_cat3_absneg(instr->opc);
301 instr_cat3_t *cat3 = ptr;
302 uint32_t src_flags = 0;
303
304 switch (instr->opc) {
305 case OPC_MAD_F16:
306 case OPC_MAD_U16:
307 case OPC_MAD_S16:
308 case OPC_SEL_B16:
309 case OPC_SEL_S16:
310 case OPC_SEL_F16:
311 case OPC_SAD_S16:
312 case OPC_SAD_S32: // really??
313 src_flags |= IR3_REG_HALF;
314 break;
315 default:
316 break;
317 }
318
319 iassert(instr->regs_count == 4);
320 iassert(!((src1->flags ^ src_flags) & IR3_REG_HALF));
321 iassert(!((src2->flags ^ src_flags) & IR3_REG_HALF));
322 iassert(!((src3->flags ^ src_flags) & IR3_REG_HALF));
323
324 if (instr->nop) {
325 iassert(!instr->repeat);
326 iassert(instr->nop <= 3);
327
328 cat3->src1_r = instr->nop & 0x1;
329 cat3->src2_r = (instr->nop >> 1) & 0x1;
330 } else {
331 cat3->src1_r = !!(src1->flags & IR3_REG_R);
332 cat3->src2_r = !!(src2->flags & IR3_REG_R);
333 }
334
335 if (src1->flags & IR3_REG_RELATIV) {
336 iassert(src1->array.offset < (1 << 10));
337 cat3->rel1.src1 = reg(src1, info, instr->repeat,
338 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_R |
339 IR3_REG_HALF | absneg);
340 cat3->rel1.src1_c = !!(src1->flags & IR3_REG_CONST);
341 cat3->rel1.src1_rel = 1;
342 } else if (src1->flags & IR3_REG_CONST) {
343 iassert(src1->num < (1 << 12));
344 cat3->c1.src1 = reg(src1, info, instr->repeat,
345 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF | absneg);
346 cat3->c1.src1_c = 1;
347 } else {
348 iassert(src1->num < (1 << 11));
349 cat3->src1 = reg(src1, info, instr->repeat,
350 IR3_REG_R | IR3_REG_HALF | absneg);
351 }
352
353 cat3->src1_neg = !!(src1->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
354
355 cat3->src2 = reg(src2, info, instr->repeat,
356 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF | absneg);
357 cat3->src2_c = !!(src2->flags & IR3_REG_CONST);
358 cat3->src2_neg = !!(src2->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
359
360 if (src3->flags & IR3_REG_RELATIV) {
361 iassert(src3->array.offset < (1 << 10));
362 cat3->rel2.src3 = reg(src3, info, instr->repeat,
363 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_R |
364 IR3_REG_HALF | absneg);
365 cat3->rel2.src3_c = !!(src3->flags & IR3_REG_CONST);
366 cat3->rel2.src3_rel = 1;
367 } else if (src3->flags & IR3_REG_CONST) {
368 iassert(src3->num < (1 << 12));
369 cat3->c2.src3 = reg(src3, info, instr->repeat,
370 IR3_REG_CONST | IR3_REG_R | IR3_REG_HALF | absneg);
371 cat3->c2.src3_c = 1;
372 } else {
373 iassert(src3->num < (1 << 11));
374 cat3->src3 = reg(src3, info, instr->repeat,
375 IR3_REG_R | IR3_REG_HALF | absneg);
376 }
377
378 cat3->src3_neg = !!(src3->flags & (IR3_REG_FNEG | IR3_REG_SNEG | IR3_REG_BNOT));
379 cat3->src3_r = !!(src3->flags & IR3_REG_R);
380
381 cat3->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
382 cat3->repeat = instr->repeat;
383 cat3->sat = !!(instr->flags & IR3_INSTR_SAT);
384 cat3->ss = !!(instr->flags & IR3_INSTR_SS);
385 cat3->ul = !!(instr->flags & IR3_INSTR_UL);
386 cat3->dst_half = !!((src_flags ^ dst->flags) & IR3_REG_HALF);
387 cat3->opc = instr->opc;
388 cat3->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
389 cat3->sync = !!(instr->flags & IR3_INSTR_SY);
390 cat3->opc_cat = 3;
391
392 return 0;
393 }
394
395 static int emit_cat4(struct ir3_instruction *instr, void *ptr,
396 struct ir3_info *info)
397 {
398 struct ir3_register *dst = instr->regs[0];
399 struct ir3_register *src = instr->regs[1];
400 instr_cat4_t *cat4 = ptr;
401
402 iassert(instr->regs_count == 2);
403
404 if (src->flags & IR3_REG_RELATIV) {
405 iassert(src->array.offset < (1 << 10));
406 cat4->rel.src = reg(src, info, instr->repeat,
407 IR3_REG_RELATIV | IR3_REG_CONST | IR3_REG_FNEG |
408 IR3_REG_FABS | IR3_REG_R | IR3_REG_HALF);
409 cat4->rel.src_c = !!(src->flags & IR3_REG_CONST);
410 cat4->rel.src_rel = 1;
411 } else if (src->flags & IR3_REG_CONST) {
412 iassert(src->num < (1 << 12));
413 cat4->c.src = reg(src, info, instr->repeat,
414 IR3_REG_CONST | IR3_REG_FNEG | IR3_REG_FABS |
415 IR3_REG_R | IR3_REG_HALF);
416 cat4->c.src_c = 1;
417 } else {
418 iassert(src->num < (1 << 11));
419 cat4->src = reg(src, info, instr->repeat,
420 IR3_REG_IMMED | IR3_REG_FNEG | IR3_REG_FABS |
421 IR3_REG_R | IR3_REG_HALF);
422 }
423
424 cat4->src_im = !!(src->flags & IR3_REG_IMMED);
425 cat4->src_neg = !!(src->flags & IR3_REG_FNEG);
426 cat4->src_abs = !!(src->flags & IR3_REG_FABS);
427 cat4->src_r = !!(src->flags & IR3_REG_R);
428
429 cat4->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
430 cat4->repeat = instr->repeat;
431 cat4->sat = !!(instr->flags & IR3_INSTR_SAT);
432 cat4->ss = !!(instr->flags & IR3_INSTR_SS);
433 cat4->ul = !!(instr->flags & IR3_INSTR_UL);
434 cat4->dst_half = !!((src->flags ^ dst->flags) & IR3_REG_HALF);
435 cat4->full = ! (src->flags & IR3_REG_HALF);
436 cat4->opc = instr->opc;
437 cat4->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
438 cat4->sync = !!(instr->flags & IR3_INSTR_SY);
439 cat4->opc_cat = 4;
440
441 return 0;
442 }
443
444 static int emit_cat5(struct ir3_instruction *instr, void *ptr,
445 struct ir3_info *info)
446 {
447 struct ir3_register *dst = instr->regs[0];
448 /* To simplify things when there could be zero, one, or two args other
449 * than tex/sampler idx, we use the first src reg in the ir to hold
450 * samp_tex hvec2:
451 */
452 struct ir3_register *src1;
453 struct ir3_register *src2;
454 instr_cat5_t *cat5 = ptr;
455
456 iassert((instr->regs_count == 1) ||
457 (instr->regs_count == 2) ||
458 (instr->regs_count == 3) ||
459 (instr->regs_count == 4));
460
461 if (instr->flags & IR3_INSTR_S2EN) {
462 src1 = instr->regs[2];
463 src2 = instr->regs_count > 3 ? instr->regs[3] : NULL;
464 } else {
465 src1 = instr->regs_count > 1 ? instr->regs[1] : NULL;
466 src2 = instr->regs_count > 2 ? instr->regs[2] : NULL;
467 }
468
469 assume(src1 || !src2);
470
471 if (src1) {
472 cat5->full = ! (src1->flags & IR3_REG_HALF);
473 cat5->src1 = reg(src1, info, instr->repeat, IR3_REG_HALF);
474 }
475
476 if (src2) {
477 iassert(!((src1->flags ^ src2->flags) & IR3_REG_HALF));
478 cat5->src2 = reg(src2, info, instr->repeat, IR3_REG_HALF);
479 }
480
481 if (instr->flags & IR3_INSTR_B) {
482 cat5->s2en_bindless.base_hi = instr->cat5.tex_base >> 1;
483 cat5->base_lo = instr->cat5.tex_base & 1;
484 }
485
486 if (instr->flags & IR3_INSTR_S2EN) {
487 struct ir3_register *samp_tex = instr->regs[1];
488 iassert(samp_tex->flags & IR3_REG_HALF);
489 cat5->s2en_bindless.src3 = reg(samp_tex, info, instr->repeat,
490 (instr->flags & IR3_INSTR_B) ? 0 : IR3_REG_HALF);
491 if (instr->flags & IR3_INSTR_B) {
492 if (instr->flags & IR3_INSTR_A1EN) {
493 cat5->s2en_bindless.desc_mode = CAT5_BINDLESS_A1_UNIFORM;
494 } else {
495 cat5->s2en_bindless.desc_mode = CAT5_BINDLESS_UNIFORM;
496 }
497 } else {
498 /* TODO: This should probably be CAT5_UNIFORM, at least on a6xx,
499 * as this is what the blob does and it is presumably faster, but
500 * first we should confirm it is actually nonuniform and figure
501 * out when the whole descriptor mode mechanism was introduced.
502 */
503 cat5->s2en_bindless.desc_mode = CAT5_NONUNIFORM;
504 }
505 iassert(!(instr->cat5.samp | instr->cat5.tex));
506 } else if (instr->flags & IR3_INSTR_B) {
507 cat5->s2en_bindless.src3 = instr->cat5.samp;
508 if (instr->flags & IR3_INSTR_A1EN) {
509 cat5->s2en_bindless.desc_mode = CAT5_BINDLESS_A1_IMM;
510 } else {
511 cat5->s2en_bindless.desc_mode = CAT5_BINDLESS_IMM;
512 }
513 } else {
514 cat5->norm.samp = instr->cat5.samp;
515 cat5->norm.tex = instr->cat5.tex;
516 }
517
518 cat5->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
519 cat5->wrmask = dst->wrmask;
520 cat5->type = instr->cat5.type;
521 cat5->is_3d = !!(instr->flags & IR3_INSTR_3D);
522 cat5->is_a = !!(instr->flags & IR3_INSTR_A);
523 cat5->is_s = !!(instr->flags & IR3_INSTR_S);
524 cat5->is_s2en_bindless = !!(instr->flags & (IR3_INSTR_S2EN | IR3_INSTR_B));
525 cat5->is_o = !!(instr->flags & IR3_INSTR_O);
526 cat5->is_p = !!(instr->flags & IR3_INSTR_P);
527 cat5->opc = instr->opc;
528 cat5->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
529 cat5->sync = !!(instr->flags & IR3_INSTR_SY);
530 cat5->opc_cat = 5;
531
532 return 0;
533 }
534
535 static int emit_cat6_a6xx(struct ir3_instruction *instr, void *ptr,
536 struct ir3_info *info)
537 {
538 struct ir3_register *ssbo;
539 instr_cat6_a6xx_t *cat6 = ptr;
540
541 ssbo = instr->regs[1];
542
543 cat6->type = instr->cat6.type;
544 cat6->d = instr->cat6.d - (instr->opc == OPC_LDC ? 0 : 1);
545 cat6->typed = instr->cat6.typed;
546 cat6->type_size = instr->cat6.iim_val - 1;
547 cat6->opc = instr->opc;
548 cat6->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
549 cat6->sync = !!(instr->flags & IR3_INSTR_SY);
550 cat6->opc_cat = 6;
551
552 cat6->ssbo = reg(ssbo, info, instr->repeat, IR3_REG_IMMED);
553
554 /* For unused sources in an opcode, initialize contents with the ir3 dest
555 * reg
556 */
557 switch (instr->opc) {
558 case OPC_RESINFO:
559 cat6->src1 = reg(instr->regs[0], info, instr->repeat, 0);
560 cat6->src2 = reg(instr->regs[0], info, instr->repeat, 0);
561 break;
562 case OPC_LDC:
563 case OPC_LDIB:
564 cat6->src1 = reg(instr->regs[2], info, instr->repeat, 0);
565 cat6->src2 = reg(instr->regs[0], info, instr->repeat, 0);
566 break;
567 default:
568 cat6->src1 = reg(instr->regs[2], info, instr->repeat, 0);
569 cat6->src2 = reg(instr->regs[3], info, instr->repeat, 0);
570 break;
571 }
572
573 if (instr->flags & IR3_INSTR_B) {
574 if (ssbo->flags & IR3_REG_IMMED) {
575 cat6->desc_mode = CAT6_BINDLESS_IMM;
576 } else {
577 cat6->desc_mode = CAT6_BINDLESS_UNIFORM;
578 }
579 cat6->base = instr->cat6.base;
580 } else {
581 if (ssbo->flags & IR3_REG_IMMED)
582 cat6->desc_mode = CAT6_IMM;
583 else
584 cat6->desc_mode = CAT6_UNIFORM;
585 }
586
587 switch (instr->opc) {
588 case OPC_ATOMIC_ADD:
589 case OPC_ATOMIC_SUB:
590 case OPC_ATOMIC_XCHG:
591 case OPC_ATOMIC_INC:
592 case OPC_ATOMIC_DEC:
593 case OPC_ATOMIC_CMPXCHG:
594 case OPC_ATOMIC_MIN:
595 case OPC_ATOMIC_MAX:
596 case OPC_ATOMIC_AND:
597 case OPC_ATOMIC_OR:
598 case OPC_ATOMIC_XOR:
599 cat6->pad1 = 0x1;
600 cat6->pad3 = 0xc;
601 cat6->pad5 = 0x3;
602 break;
603 case OPC_STIB:
604 cat6->pad1 = 0x0;
605 cat6->pad3 = 0xc;
606 cat6->pad5 = 0x2;
607 break;
608 case OPC_LDIB:
609 case OPC_RESINFO:
610 cat6->pad1 = 0x1;
611 cat6->pad3 = 0xc;
612 cat6->pad5 = 0x2;
613 break;
614 case OPC_LDC:
615 cat6->pad1 = 0x0;
616 cat6->pad3 = 0x8;
617 cat6->pad5 = 0x2;
618 break;
619 default:
620 iassert(0);
621 }
622 cat6->pad2 = 0x0;
623 cat6->pad4 = 0x0;
624
625 return 0;
626 }
627
628 static int emit_cat6(struct ir3_instruction *instr, void *ptr,
629 struct ir3_info *info)
630 {
631 struct ir3_register *dst, *src1, *src2;
632 instr_cat6_t *cat6 = ptr;
633
634 /* In a6xx we start using a new instruction encoding for some of
635 * these instructions:
636 */
637 if (info->gpu_id >= 600) {
638 switch (instr->opc) {
639 case OPC_ATOMIC_ADD:
640 case OPC_ATOMIC_SUB:
641 case OPC_ATOMIC_XCHG:
642 case OPC_ATOMIC_INC:
643 case OPC_ATOMIC_DEC:
644 case OPC_ATOMIC_CMPXCHG:
645 case OPC_ATOMIC_MIN:
646 case OPC_ATOMIC_MAX:
647 case OPC_ATOMIC_AND:
648 case OPC_ATOMIC_OR:
649 case OPC_ATOMIC_XOR:
650 /* The shared variants of these still use the old encoding: */
651 if (!(instr->flags & IR3_INSTR_G))
652 break;
653 /* fallthrough */
654 case OPC_STIB:
655 case OPC_LDIB:
656 case OPC_LDC:
657 case OPC_RESINFO:
658 return emit_cat6_a6xx(instr, ptr, info);
659 default:
660 break;
661 }
662 }
663
664 bool type_full = type_size(instr->cat6.type) == 32;
665
666 cat6->type = instr->cat6.type;
667 cat6->opc = instr->opc;
668 cat6->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
669 cat6->sync = !!(instr->flags & IR3_INSTR_SY);
670 cat6->g = !!(instr->flags & IR3_INSTR_G);
671 cat6->opc_cat = 6;
672
673 switch (instr->opc) {
674 case OPC_RESINFO:
675 case OPC_RESFMT:
676 iassert_type(instr->regs[0], type_full); /* dst */
677 iassert_type(instr->regs[1], type_full); /* src1 */
678 break;
679 case OPC_L2G:
680 case OPC_G2L:
681 iassert_type(instr->regs[0], true); /* dst */
682 iassert_type(instr->regs[1], true); /* src1 */
683 break;
684 case OPC_STG:
685 case OPC_STL:
686 case OPC_STP:
687 case OPC_STLW:
688 case OPC_STIB:
689 /* no dst, so regs[0] is dummy */
690 iassert_type(instr->regs[1], true); /* dst */
691 iassert_type(instr->regs[2], type_full); /* src1 */
692 iassert_type(instr->regs[3], true); /* src2 */
693 break;
694 default:
695 iassert_type(instr->regs[0], type_full); /* dst */
696 iassert_type(instr->regs[1], true); /* src1 */
697 if (instr->regs_count > 2)
698 iassert_type(instr->regs[2], true); /* src1 */
699 break;
700 }
701
702 /* the "dst" for a store instruction is (from the perspective
703 * of data flow in the shader, ie. register use/def, etc) in
704 * fact a register that is read by the instruction, rather
705 * than written:
706 */
707 if (is_store(instr)) {
708 iassert(instr->regs_count >= 3);
709
710 dst = instr->regs[1];
711 src1 = instr->regs[2];
712 src2 = (instr->regs_count >= 4) ? instr->regs[3] : NULL;
713 } else {
714 iassert(instr->regs_count >= 2);
715
716 dst = instr->regs[0];
717 src1 = instr->regs[1];
718 src2 = (instr->regs_count >= 3) ? instr->regs[2] : NULL;
719 }
720
721 /* TODO we need a more comprehensive list about which instructions
722 * can be encoded which way. Or possibly use IR3_INSTR_0 flag to
723 * indicate to use the src_off encoding even if offset is zero
724 * (but then what to do about dst_off?)
725 */
726 if (is_atomic(instr->opc)) {
727 instr_cat6ldgb_t *ldgb = ptr;
728
729 /* maybe these two bits both determine the instruction encoding? */
730 cat6->src_off = false;
731
732 ldgb->d = instr->cat6.d - 1;
733 ldgb->typed = instr->cat6.typed;
734 ldgb->type_size = instr->cat6.iim_val - 1;
735
736 ldgb->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
737
738 if (ldgb->g) {
739 struct ir3_register *src3 = instr->regs[3];
740 struct ir3_register *src4 = instr->regs[4];
741
742 /* first src is src_ssbo: */
743 iassert(src1->flags & IR3_REG_IMMED);
744 ldgb->src_ssbo = src1->uim_val;
745 ldgb->src_ssbo_im = 0x1;
746
747 ldgb->src1 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
748 ldgb->src1_im = !!(src2->flags & IR3_REG_IMMED);
749 ldgb->src2 = reg(src3, info, instr->repeat, IR3_REG_IMMED);
750 ldgb->src2_im = !!(src3->flags & IR3_REG_IMMED);
751
752 ldgb->src3 = reg(src4, info, instr->repeat, 0);
753 ldgb->pad0 = 0x1;
754 } else {
755 ldgb->src1 = reg(src1, info, instr->repeat, IR3_REG_IMMED);
756 ldgb->src1_im = !!(src1->flags & IR3_REG_IMMED);
757 ldgb->src2 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
758 ldgb->src2_im = !!(src2->flags & IR3_REG_IMMED);
759 ldgb->pad0 = 0x1;
760 ldgb->src_ssbo_im = 0x0;
761 }
762
763 return 0;
764 } else if (instr->opc == OPC_LDGB) {
765 struct ir3_register *src3 = instr->regs[3];
766 instr_cat6ldgb_t *ldgb = ptr;
767
768 /* maybe these two bits both determine the instruction encoding? */
769 cat6->src_off = false;
770
771 ldgb->d = instr->cat6.d - 1;
772 ldgb->typed = instr->cat6.typed;
773 ldgb->type_size = instr->cat6.iim_val - 1;
774
775 ldgb->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
776
777 /* first src is src_ssbo: */
778 iassert(src1->flags & IR3_REG_IMMED);
779 ldgb->src_ssbo = src1->uim_val;
780
781 /* then next two are src1/src2: */
782 ldgb->src1 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
783 ldgb->src1_im = !!(src2->flags & IR3_REG_IMMED);
784 ldgb->src2 = reg(src3, info, instr->repeat, IR3_REG_IMMED);
785 ldgb->src2_im = !!(src3->flags & IR3_REG_IMMED);
786
787 ldgb->pad0 = 0x0;
788 ldgb->src_ssbo_im = true;
789
790 return 0;
791 } else if (instr->opc == OPC_RESINFO) {
792 instr_cat6ldgb_t *ldgb = ptr;
793
794 ldgb->d = instr->cat6.d - 1;
795
796 ldgb->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
797
798 /* first src is src_ssbo: */
799 ldgb->src_ssbo = reg(src1, info, instr->repeat, IR3_REG_IMMED);
800 ldgb->src_ssbo_im = !!(src1->flags & IR3_REG_IMMED);
801
802 return 0;
803 } else if ((instr->opc == OPC_STGB) || (instr->opc == OPC_STIB)) {
804 struct ir3_register *src3 = instr->regs[4];
805 instr_cat6stgb_t *stgb = ptr;
806
807 /* maybe these two bits both determine the instruction encoding? */
808 cat6->src_off = true;
809 stgb->pad3 = 0x2;
810
811 stgb->d = instr->cat6.d - 1;
812 stgb->typed = instr->cat6.typed;
813 stgb->type_size = instr->cat6.iim_val - 1;
814
815 /* first src is dst_ssbo: */
816 iassert(dst->flags & IR3_REG_IMMED);
817 stgb->dst_ssbo = dst->uim_val;
818
819 /* then src1/src2/src3: */
820 stgb->src1 = reg(src1, info, instr->repeat, 0);
821 stgb->src2 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
822 stgb->src2_im = !!(src2->flags & IR3_REG_IMMED);
823 stgb->src3 = reg(src3, info, instr->repeat, IR3_REG_IMMED);
824 stgb->src3_im = !!(src3->flags & IR3_REG_IMMED);
825
826 return 0;
827 } else if (instr->cat6.src_offset || (instr->opc == OPC_LDG) ||
828 (instr->opc == OPC_LDL) || (instr->opc == OPC_LDLW)) {
829 struct ir3_register *src3 = instr->regs[3];
830 instr_cat6a_t *cat6a = ptr;
831
832 cat6->src_off = true;
833
834 if (instr->opc == OPC_LDG) {
835 /* For LDG src1 can not be immediate, so src1_imm is redundant and
836 * instead used to signal whether (when true) 'off' is a 32 bit
837 * register or an immediate offset.
838 */
839 cat6a->src1 = reg(src1, info, instr->repeat, 0);
840 cat6a->src1_im = !(src3->flags & IR3_REG_IMMED);
841 cat6a->off = reg(src3, info, instr->repeat, IR3_REG_IMMED);
842 } else {
843 cat6a->src1 = reg(src1, info, instr->repeat, IR3_REG_IMMED);
844 cat6a->src1_im = !!(src1->flags & IR3_REG_IMMED);
845 cat6a->off = reg(src3, info, instr->repeat, IR3_REG_IMMED);
846 iassert(src3->flags & IR3_REG_IMMED);
847 }
848
849 /* Num components */
850 cat6a->src2 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
851 cat6a->src2_im = true;
852 } else {
853 instr_cat6b_t *cat6b = ptr;
854
855 cat6->src_off = false;
856
857 cat6b->src1 = reg(src1, info, instr->repeat, IR3_REG_IMMED | IR3_REG_HALF);
858 cat6b->src1_im = !!(src1->flags & IR3_REG_IMMED);
859 if (src2) {
860 cat6b->src2 = reg(src2, info, instr->repeat, IR3_REG_IMMED);
861 cat6b->src2_im = !!(src2->flags & IR3_REG_IMMED);
862 }
863 }
864
865 if (instr->cat6.dst_offset || (instr->opc == OPC_STG) ||
866 (instr->opc == OPC_STL) || (instr->opc == OPC_STLW)) {
867 instr_cat6c_t *cat6c = ptr;
868 cat6->dst_off = true;
869 cat6c->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
870
871 if (instr->flags & IR3_INSTR_G) {
872 struct ir3_register *src3 = instr->regs[4];
873 cat6c->off = reg(src3, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
874 if (src3->flags & IR3_REG_IMMED) {
875 /* Immediate offsets are in bytes... */
876 cat6->g = false;
877 cat6c->off *= 4;
878 }
879 } else {
880 cat6c->off = instr->cat6.dst_offset;
881 }
882 } else {
883 instr_cat6d_t *cat6d = ptr;
884 cat6->dst_off = false;
885 cat6d->dst = reg(dst, info, instr->repeat, IR3_REG_R | IR3_REG_HALF);
886 }
887
888 return 0;
889 }
890
891 static int emit_cat7(struct ir3_instruction *instr, void *ptr,
892 struct ir3_info *info)
893 {
894 instr_cat7_t *cat7 = ptr;
895
896 cat7->ss = !!(instr->flags & IR3_INSTR_SS);
897 cat7->w = instr->cat7.w;
898 cat7->r = instr->cat7.r;
899 cat7->l = instr->cat7.l;
900 cat7->g = instr->cat7.g;
901 cat7->opc = instr->opc;
902 cat7->jmp_tgt = !!(instr->flags & IR3_INSTR_JP);
903 cat7->sync = !!(instr->flags & IR3_INSTR_SY);
904 cat7->opc_cat = 7;
905
906 return 0;
907 }
908
909 static int (*emit[])(struct ir3_instruction *instr, void *ptr,
910 struct ir3_info *info) = {
911 emit_cat0, emit_cat1, emit_cat2, emit_cat3, emit_cat4, emit_cat5, emit_cat6,
912 emit_cat7,
913 };
914
915 void * ir3_assemble(struct ir3 *shader, struct ir3_info *info,
916 uint32_t gpu_id)
917 {
918 uint32_t *ptr, *dwords;
919
920 memset(info, 0, sizeof(*info));
921 info->gpu_id = gpu_id;
922 info->max_reg = -1;
923 info->max_half_reg = -1;
924 info->max_const = -1;
925
926 foreach_block (block, &shader->block_list) {
927 foreach_instr (instr, &block->instr_list) {
928 info->sizedwords += 2;
929 }
930 }
931
932 /* need an integer number of instruction "groups" (sets of 16
933 * instructions on a4xx or sets of 4 instructions on a3xx),
934 * so pad out w/ NOPs if needed: (NOTE each instruction is 64bits)
935 */
936 if (gpu_id >= 400) {
937 info->sizedwords = align(info->sizedwords, 16 * 2);
938 } else {
939 info->sizedwords = align(info->sizedwords, 4 * 2);
940 }
941
942 ptr = dwords = calloc(4, info->sizedwords);
943
944 foreach_block (block, &shader->block_list) {
945 unsigned sfu_delay = 0;
946
947 foreach_instr (instr, &block->instr_list) {
948 int ret = emit[opc_cat(instr->opc)](instr, dwords, info);
949 if (ret)
950 goto fail;
951
952 if ((instr->opc == OPC_BARY_F) && (instr->regs[0]->flags & IR3_REG_EI))
953 info->last_baryf = info->instrs_count;
954
955 info->instrs_count += 1 + instr->repeat + instr->nop;
956 info->nops_count += instr->nop;
957 if (instr->opc == OPC_NOP)
958 info->nops_count += 1 + instr->repeat;
959 if (instr->opc == OPC_MOV) {
960 if (instr->cat1.src_type == instr->cat1.dst_type) {
961 info->mov_count += 1 + instr->repeat;
962 } else {
963 info->cov_count += 1 + instr->repeat;
964 }
965 }
966 dwords += 2;
967
968 if (instr->flags & IR3_INSTR_SS) {
969 info->ss++;
970 info->sstall += sfu_delay;
971 }
972
973 if (instr->flags & IR3_INSTR_SY)
974 info->sy++;
975
976 if (is_sfu(instr)) {
977 sfu_delay = 10;
978 } else if (sfu_delay > 0) {
979 sfu_delay--;
980 }
981 }
982 }
983
984 return ptr;
985
986 fail:
987 free(ptr);
988 return NULL;
989 }
990
991 static struct ir3_register * reg_create(struct ir3 *shader,
992 int num, int flags)
993 {
994 struct ir3_register *reg =
995 ir3_alloc(shader, sizeof(struct ir3_register));
996 reg->wrmask = 1;
997 reg->flags = flags;
998 reg->num = num;
999 if (shader->compiler->gpu_id >= 600)
1000 reg->merged = true;
1001 return reg;
1002 }
1003
1004 static void insert_instr(struct ir3_block *block,
1005 struct ir3_instruction *instr)
1006 {
1007 struct ir3 *shader = block->shader;
1008 #ifdef DEBUG
1009 instr->serialno = ++shader->instr_count;
1010 #endif
1011 list_addtail(&instr->node, &block->instr_list);
1012
1013 if (is_input(instr))
1014 array_insert(shader, shader->baryfs, instr);
1015 }
1016
1017 struct ir3_block * ir3_block_create(struct ir3 *shader)
1018 {
1019 struct ir3_block *block = ir3_alloc(shader, sizeof(*block));
1020 #ifdef DEBUG
1021 block->serialno = ++shader->block_count;
1022 #endif
1023 block->shader = shader;
1024 list_inithead(&block->node);
1025 list_inithead(&block->instr_list);
1026 block->predecessors = _mesa_pointer_set_create(block);
1027 return block;
1028 }
1029
1030 static struct ir3_instruction *instr_create(struct ir3_block *block, int nreg)
1031 {
1032 struct ir3_instruction *instr;
1033 unsigned sz = sizeof(*instr) + (nreg * sizeof(instr->regs[0]));
1034 char *ptr = ir3_alloc(block->shader, sz);
1035
1036 instr = (struct ir3_instruction *)ptr;
1037 ptr += sizeof(*instr);
1038 instr->regs = (struct ir3_register **)ptr;
1039
1040 #ifdef DEBUG
1041 instr->regs_max = nreg;
1042 #endif
1043
1044 return instr;
1045 }
1046
1047 struct ir3_instruction * ir3_instr_create2(struct ir3_block *block,
1048 opc_t opc, int nreg)
1049 {
1050 struct ir3_instruction *instr = instr_create(block, nreg);
1051 instr->block = block;
1052 instr->opc = opc;
1053 insert_instr(block, instr);
1054 return instr;
1055 }
1056
1057 struct ir3_instruction * ir3_instr_create(struct ir3_block *block, opc_t opc)
1058 {
1059 /* NOTE: we could be slightly more clever, at least for non-meta,
1060 * and choose # of regs based on category.
1061 */
1062 return ir3_instr_create2(block, opc, 4);
1063 }
1064
1065 struct ir3_instruction * ir3_instr_clone(struct ir3_instruction *instr)
1066 {
1067 struct ir3_instruction *new_instr = instr_create(instr->block,
1068 instr->regs_count);
1069 struct ir3_register **regs;
1070 unsigned i;
1071
1072 regs = new_instr->regs;
1073 *new_instr = *instr;
1074 new_instr->regs = regs;
1075
1076 insert_instr(instr->block, new_instr);
1077
1078 /* clone registers: */
1079 new_instr->regs_count = 0;
1080 for (i = 0; i < instr->regs_count; i++) {
1081 struct ir3_register *reg = instr->regs[i];
1082 struct ir3_register *new_reg =
1083 ir3_reg_create(new_instr, reg->num, reg->flags);
1084 *new_reg = *reg;
1085 }
1086
1087 return new_instr;
1088 }
1089
1090 /* Add a false dependency to instruction, to ensure it is scheduled first: */
1091 void ir3_instr_add_dep(struct ir3_instruction *instr, struct ir3_instruction *dep)
1092 {
1093 array_insert(instr, instr->deps, dep);
1094 }
1095
1096 struct ir3_register * ir3_reg_create(struct ir3_instruction *instr,
1097 int num, int flags)
1098 {
1099 struct ir3 *shader = instr->block->shader;
1100 struct ir3_register *reg = reg_create(shader, num, flags);
1101 #ifdef DEBUG
1102 debug_assert(instr->regs_count < instr->regs_max);
1103 #endif
1104 instr->regs[instr->regs_count++] = reg;
1105 return reg;
1106 }
1107
1108 struct ir3_register * ir3_reg_clone(struct ir3 *shader,
1109 struct ir3_register *reg)
1110 {
1111 struct ir3_register *new_reg = reg_create(shader, 0, 0);
1112 *new_reg = *reg;
1113 return new_reg;
1114 }
1115
1116 void
1117 ir3_instr_set_address(struct ir3_instruction *instr,
1118 struct ir3_instruction *addr)
1119 {
1120 if (instr->address != addr) {
1121 struct ir3 *ir = instr->block->shader;
1122
1123 debug_assert(!instr->address);
1124 debug_assert(instr->block == addr->block);
1125
1126 instr->address = addr;
1127 debug_assert(reg_num(addr->regs[0]) == REG_A0);
1128 unsigned comp = reg_comp(addr->regs[0]);
1129 if (comp == 0) {
1130 array_insert(ir, ir->a0_users, instr);
1131 } else {
1132 debug_assert(comp == 1);
1133 array_insert(ir, ir->a1_users, instr);
1134 }
1135 }
1136 }
1137
1138 void
1139 ir3_block_clear_mark(struct ir3_block *block)
1140 {
1141 foreach_instr (instr, &block->instr_list)
1142 instr->flags &= ~IR3_INSTR_MARK;
1143 }
1144
1145 void
1146 ir3_clear_mark(struct ir3 *ir)
1147 {
1148 foreach_block (block, &ir->block_list) {
1149 ir3_block_clear_mark(block);
1150 }
1151 }
1152
1153 unsigned
1154 ir3_count_instructions(struct ir3 *ir)
1155 {
1156 unsigned cnt = 1;
1157 foreach_block (block, &ir->block_list) {
1158 block->start_ip = cnt;
1159 foreach_instr (instr, &block->instr_list) {
1160 instr->ip = cnt++;
1161 }
1162 block->end_ip = cnt;
1163 }
1164 return cnt;
1165 }
1166
1167 /* When counting instructions for RA, we insert extra fake instructions at the
1168 * beginning of each block, where values become live, and at the end where
1169 * values die. This prevents problems where values live-in at the beginning or
1170 * live-out at the end of a block from being treated as if they were
1171 * live-in/live-out at the first/last instruction, which would be incorrect.
1172 * In ir3_legalize these ip's are assumed to be actual ip's of the final
1173 * program, so it would be incorrect to use this everywhere.
1174 */
1175
1176 unsigned
1177 ir3_count_instructions_ra(struct ir3 *ir)
1178 {
1179 unsigned cnt = 1;
1180 foreach_block (block, &ir->block_list) {
1181 block->start_ip = cnt++;
1182 foreach_instr (instr, &block->instr_list) {
1183 instr->ip = cnt++;
1184 }
1185 block->end_ip = cnt++;
1186 }
1187 return cnt;
1188 }
1189
1190 struct ir3_array *
1191 ir3_lookup_array(struct ir3 *ir, unsigned id)
1192 {
1193 foreach_array (arr, &ir->array_list)
1194 if (arr->id == id)
1195 return arr;
1196 return NULL;
1197 }
1198
1199 void
1200 ir3_find_ssa_uses(struct ir3 *ir, void *mem_ctx, bool falsedeps)
1201 {
1202 /* We could do this in a single pass if we can assume instructions
1203 * are always sorted. Which currently might not always be true.
1204 * (In particular after ir3_group pass, but maybe other places.)
1205 */
1206 foreach_block (block, &ir->block_list)
1207 foreach_instr (instr, &block->instr_list)
1208 instr->uses = NULL;
1209
1210 foreach_block (block, &ir->block_list) {
1211 foreach_instr (instr, &block->instr_list) {
1212 foreach_ssa_src_n (src, n, instr) {
1213 if (__is_false_dep(instr, n) && !falsedeps)
1214 continue;
1215 if (!src->uses)
1216 src->uses = _mesa_pointer_set_create(mem_ctx);
1217 _mesa_set_add(src->uses, instr);
1218 }
1219 }
1220 }
1221 }
1222
1223 /**
1224 * Set the destination type of an instruction, for example if a
1225 * conversion is folded in, handling the special cases where the
1226 * instruction's dest type or opcode needs to be fixed up.
1227 */
1228 void
1229 ir3_set_dst_type(struct ir3_instruction *instr, bool half)
1230 {
1231 if (half) {
1232 instr->regs[0]->flags |= IR3_REG_HALF;
1233 } else {
1234 instr->regs[0]->flags &= ~IR3_REG_HALF;
1235 }
1236
1237 switch (opc_cat(instr->opc)) {
1238 case 1: /* move instructions */
1239 if (half) {
1240 instr->cat1.dst_type = half_type(instr->cat1.dst_type);
1241 } else {
1242 instr->cat1.dst_type = full_type(instr->cat1.dst_type);
1243 }
1244 break;
1245 case 4:
1246 if (half) {
1247 instr->opc = cat4_half_opc(instr->opc);
1248 } else {
1249 instr->opc = cat4_full_opc(instr->opc);
1250 }
1251 break;
1252 case 5:
1253 if (half) {
1254 instr->cat5.type = half_type(instr->cat5.type);
1255 } else {
1256 instr->cat5.type = full_type(instr->cat5.type);
1257 }
1258 break;
1259 }
1260 }
1261
1262 /**
1263 * One-time fixup for instruction src-types. Other than cov's that
1264 * are folded, an instruction's src type does not change.
1265 */
1266 void
1267 ir3_fixup_src_type(struct ir3_instruction *instr)
1268 {
1269 bool half = !!(instr->regs[1]->flags & IR3_REG_HALF);
1270
1271 switch (opc_cat(instr->opc)) {
1272 case 1: /* move instructions */
1273 if (half) {
1274 instr->cat1.src_type = half_type(instr->cat1.src_type);
1275 } else {
1276 instr->cat1.src_type = full_type(instr->cat1.src_type);
1277 }
1278 break;
1279 case 3:
1280 if (half) {
1281 instr->opc = cat3_half_opc(instr->opc);
1282 } else {
1283 instr->opc = cat3_full_opc(instr->opc);
1284 }
1285 break;
1286 }
1287 }