2 * Copyright (c) 2012 Rob Clark <robdclark@gmail.com>
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:
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
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
33 #include "freedreno_util.h"
34 #include "instr-a3xx.h"
38 struct ir3_heap_chunk
{
39 struct ir3_heap_chunk
*next
;
40 uint32_t heap
[CHUNK_SZ
];
43 static void grow_heap(struct ir3
*shader
)
45 struct ir3_heap_chunk
*chunk
= calloc(1, sizeof(*chunk
));
46 chunk
->next
= shader
->chunk
;
47 shader
->chunk
= chunk
;
51 /* simple allocator to carve allocations out of an up-front allocated heap,
52 * so that we can free everything easily in one shot.
54 void * ir3_alloc(struct ir3
*shader
, int sz
)
58 sz
= align(sz
, 4) / 4;
60 if ((shader
->heap_idx
+ sz
) > CHUNK_SZ
)
63 ptr
= &shader
->chunk
->heap
[shader
->heap_idx
];
64 shader
->heap_idx
+= sz
;
69 struct ir3
* ir3_create(struct ir3_compiler
*compiler
,
70 unsigned nin
, unsigned nout
)
72 struct ir3
*shader
= calloc(1, sizeof(struct ir3
));
76 shader
->compiler
= compiler
;
77 shader
->ninputs
= nin
;
78 shader
->inputs
= ir3_alloc(shader
, sizeof(shader
->inputs
[0]) * nin
);
80 shader
->noutputs
= nout
;
81 shader
->outputs
= ir3_alloc(shader
, sizeof(shader
->outputs
[0]) * nout
);
83 list_inithead(&shader
->block_list
);
88 void ir3_destroy(struct ir3
*shader
)
90 while (shader
->chunk
) {
91 struct ir3_heap_chunk
*chunk
= shader
->chunk
;
92 shader
->chunk
= chunk
->next
;
95 free(shader
->indirects
);
96 free(shader
->predicates
);
101 #define iassert(cond) do { \
107 static uint32_t reg(struct ir3_register
*reg
, struct ir3_info
*info
,
108 uint32_t repeat
, uint32_t valid_flags
)
110 reg_t val
= { .dummy32
= 0 };
112 if (reg
->flags
& ~valid_flags
) {
113 debug_printf("INVALID FLAGS: %x vs %x\n",
114 reg
->flags
, valid_flags
);
117 if (!(reg
->flags
& IR3_REG_R
))
120 if (reg
->flags
& IR3_REG_IMMED
) {
121 val
.iim_val
= reg
->iim_val
;
125 if (reg
->flags
& IR3_REG_RELATIV
) {
126 components
= reg
->size
;
127 val
.dummy10
= reg
->offset
;
129 components
= util_last_bit(reg
->wrmask
);
130 val
.comp
= reg
->num
& 0x3;
131 val
.num
= reg
->num
>> 2;
134 int16_t max
= (reg
->num
+ repeat
+ components
- 1) >> 2;
136 if (reg
->flags
& IR3_REG_CONST
) {
137 info
->max_const
= MAX2(info
->max_const
, max
);
138 } else if (val
.num
== 63) {
139 /* ignore writes to dummy register r63.x */
140 } else if ((max
!= REG_A0
) && (max
!= REG_P0
)) {
141 if (reg
->flags
& IR3_REG_HALF
) {
142 info
->max_half_reg
= MAX2(info
->max_half_reg
, max
);
144 info
->max_reg
= MAX2(info
->max_reg
, max
);
152 static int emit_cat0(struct ir3_instruction
*instr
, void *ptr
,
153 struct ir3_info
*info
)
155 instr_cat0_t
*cat0
= ptr
;
157 if (info
->gpu_id
>= 400) {
158 cat0
->a4xx
.immed
= instr
->cat0
.immed
;
160 cat0
->a3xx
.immed
= instr
->cat0
.immed
;
162 cat0
->repeat
= instr
->repeat
;
163 cat0
->ss
= !!(instr
->flags
& IR3_INSTR_SS
);
164 cat0
->inv
= instr
->cat0
.inv
;
165 cat0
->comp
= instr
->cat0
.comp
;
166 cat0
->opc
= instr
->opc
;
167 cat0
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
168 cat0
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
174 static uint32_t type_flags(type_t type
)
176 return (type_size(type
) == 32) ? 0 : IR3_REG_HALF
;
179 static int emit_cat1(struct ir3_instruction
*instr
, void *ptr
,
180 struct ir3_info
*info
)
182 struct ir3_register
*dst
= instr
->regs
[0];
183 struct ir3_register
*src
= instr
->regs
[1];
184 instr_cat1_t
*cat1
= ptr
;
186 iassert(instr
->regs_count
== 2);
187 iassert(!((dst
->flags
^ type_flags(instr
->cat1
.dst_type
)) & IR3_REG_HALF
));
188 iassert((src
->flags
& IR3_REG_IMMED
) ||
189 !((src
->flags
^ type_flags(instr
->cat1
.src_type
)) & IR3_REG_HALF
));
191 if (src
->flags
& IR3_REG_IMMED
) {
192 cat1
->iim_val
= src
->iim_val
;
194 } else if (src
->flags
& IR3_REG_RELATIV
) {
195 cat1
->off
= reg(src
, info
, instr
->repeat
,
196 IR3_REG_R
| IR3_REG_CONST
| IR3_REG_HALF
| IR3_REG_RELATIV
);
198 cat1
->src_rel_c
= !!(src
->flags
& IR3_REG_CONST
);
200 cat1
->src
= reg(src
, info
, instr
->repeat
,
201 IR3_REG_R
| IR3_REG_CONST
| IR3_REG_HALF
);
202 cat1
->src_c
= !!(src
->flags
& IR3_REG_CONST
);
205 cat1
->dst
= reg(dst
, info
, instr
->repeat
,
206 IR3_REG_RELATIV
| IR3_REG_EVEN
|
207 IR3_REG_R
| IR3_REG_POS_INF
| IR3_REG_HALF
);
208 cat1
->repeat
= instr
->repeat
;
209 cat1
->src_r
= !!(src
->flags
& IR3_REG_R
);
210 cat1
->ss
= !!(instr
->flags
& IR3_INSTR_SS
);
211 cat1
->ul
= !!(instr
->flags
& IR3_INSTR_UL
);
212 cat1
->dst_type
= instr
->cat1
.dst_type
;
213 cat1
->dst_rel
= !!(dst
->flags
& IR3_REG_RELATIV
);
214 cat1
->src_type
= instr
->cat1
.src_type
;
215 cat1
->even
= !!(dst
->flags
& IR3_REG_EVEN
);
216 cat1
->pos_inf
= !!(dst
->flags
& IR3_REG_POS_INF
);
217 cat1
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
218 cat1
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
224 static int emit_cat2(struct ir3_instruction
*instr
, void *ptr
,
225 struct ir3_info
*info
)
227 struct ir3_register
*dst
= instr
->regs
[0];
228 struct ir3_register
*src1
= instr
->regs
[1];
229 struct ir3_register
*src2
= instr
->regs
[2];
230 instr_cat2_t
*cat2
= ptr
;
231 unsigned absneg
= ir3_cat2_absneg(instr
->opc
);
233 iassert((instr
->regs_count
== 2) || (instr
->regs_count
== 3));
235 if (src1
->flags
& IR3_REG_RELATIV
) {
236 iassert(src1
->num
< (1 << 10));
237 cat2
->rel1
.src1
= reg(src1
, info
, instr
->repeat
,
238 IR3_REG_RELATIV
| IR3_REG_CONST
| IR3_REG_R
|
239 IR3_REG_HALF
| absneg
);
240 cat2
->rel1
.src1_c
= !!(src1
->flags
& IR3_REG_CONST
);
241 cat2
->rel1
.src1_rel
= 1;
242 } else if (src1
->flags
& IR3_REG_CONST
) {
243 iassert(src1
->num
< (1 << 12));
244 cat2
->c1
.src1
= reg(src1
, info
, instr
->repeat
,
245 IR3_REG_CONST
| IR3_REG_R
| IR3_REG_HALF
);
248 iassert(src1
->num
< (1 << 11));
249 cat2
->src1
= reg(src1
, info
, instr
->repeat
,
250 IR3_REG_IMMED
| IR3_REG_R
| IR3_REG_HALF
|
253 cat2
->src1_im
= !!(src1
->flags
& IR3_REG_IMMED
);
254 cat2
->src1_neg
= !!(src1
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
255 cat2
->src1_abs
= !!(src1
->flags
& (IR3_REG_FABS
| IR3_REG_SABS
));
256 cat2
->src1_r
= !!(src1
->flags
& IR3_REG_R
);
259 iassert((src2
->flags
& IR3_REG_IMMED
) ||
260 !((src1
->flags
^ src2
->flags
) & IR3_REG_HALF
));
262 if (src2
->flags
& IR3_REG_RELATIV
) {
263 iassert(src2
->num
< (1 << 10));
264 cat2
->rel2
.src2
= reg(src2
, info
, instr
->repeat
,
265 IR3_REG_RELATIV
| IR3_REG_CONST
| IR3_REG_R
|
266 IR3_REG_HALF
| absneg
);
267 cat2
->rel2
.src2_c
= !!(src2
->flags
& IR3_REG_CONST
);
268 cat2
->rel2
.src2_rel
= 1;
269 } else if (src2
->flags
& IR3_REG_CONST
) {
270 iassert(src2
->num
< (1 << 12));
271 cat2
->c2
.src2
= reg(src2
, info
, instr
->repeat
,
272 IR3_REG_CONST
| IR3_REG_R
| IR3_REG_HALF
);
275 iassert(src2
->num
< (1 << 11));
276 cat2
->src2
= reg(src2
, info
, instr
->repeat
,
277 IR3_REG_IMMED
| IR3_REG_R
| IR3_REG_HALF
|
281 cat2
->src2_im
= !!(src2
->flags
& IR3_REG_IMMED
);
282 cat2
->src2_neg
= !!(src2
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
283 cat2
->src2_abs
= !!(src2
->flags
& (IR3_REG_FABS
| IR3_REG_SABS
));
284 cat2
->src2_r
= !!(src2
->flags
& IR3_REG_R
);
287 cat2
->dst
= reg(dst
, info
, instr
->repeat
,
288 IR3_REG_R
| IR3_REG_EI
| IR3_REG_HALF
);
289 cat2
->repeat
= instr
->repeat
;
290 cat2
->ss
= !!(instr
->flags
& IR3_INSTR_SS
);
291 cat2
->ul
= !!(instr
->flags
& IR3_INSTR_UL
);
292 cat2
->dst_half
= !!((src1
->flags
^ dst
->flags
) & IR3_REG_HALF
);
293 cat2
->ei
= !!(dst
->flags
& IR3_REG_EI
);
294 cat2
->cond
= instr
->cat2
.condition
;
295 cat2
->full
= ! (src1
->flags
& IR3_REG_HALF
);
296 cat2
->opc
= instr
->opc
;
297 cat2
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
298 cat2
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
304 static int emit_cat3(struct ir3_instruction
*instr
, void *ptr
,
305 struct ir3_info
*info
)
307 struct ir3_register
*dst
= instr
->regs
[0];
308 struct ir3_register
*src1
= instr
->regs
[1];
309 struct ir3_register
*src2
= instr
->regs
[2];
310 struct ir3_register
*src3
= instr
->regs
[3];
311 unsigned absneg
= ir3_cat3_absneg(instr
->opc
);
312 instr_cat3_t
*cat3
= ptr
;
313 uint32_t src_flags
= 0;
315 switch (instr
->opc
) {
323 case OPC_SAD_S32
: // really??
324 src_flags
|= IR3_REG_HALF
;
330 iassert(instr
->regs_count
== 4);
331 iassert(!((src1
->flags
^ src_flags
) & IR3_REG_HALF
));
332 iassert(!((src2
->flags
^ src_flags
) & IR3_REG_HALF
));
333 iassert(!((src3
->flags
^ src_flags
) & IR3_REG_HALF
));
335 if (src1
->flags
& IR3_REG_RELATIV
) {
336 iassert(src1
->num
< (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
);
348 iassert(src1
->num
< (1 << 11));
349 cat3
->src1
= reg(src1
, info
, instr
->repeat
,
350 IR3_REG_R
| IR3_REG_HALF
| absneg
);
353 cat3
->src1_neg
= !!(src1
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
354 cat3
->src1_r
= !!(src1
->flags
& IR3_REG_R
);
356 cat3
->src2
= reg(src2
, info
, instr
->repeat
,
357 IR3_REG_CONST
| IR3_REG_R
| IR3_REG_HALF
| absneg
);
358 cat3
->src2_c
= !!(src2
->flags
& IR3_REG_CONST
);
359 cat3
->src2_neg
= !!(src2
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
360 cat3
->src2_r
= !!(src2
->flags
& IR3_REG_R
);
363 if (src3
->flags
& IR3_REG_RELATIV
) {
364 iassert(src3
->num
< (1 << 10));
365 cat3
->rel2
.src3
= reg(src3
, info
, instr
->repeat
,
366 IR3_REG_RELATIV
| IR3_REG_CONST
| IR3_REG_R
|
367 IR3_REG_HALF
| absneg
);
368 cat3
->rel2
.src3_c
= !!(src3
->flags
& IR3_REG_CONST
);
369 cat3
->rel2
.src3_rel
= 1;
370 } else if (src3
->flags
& IR3_REG_CONST
) {
371 iassert(src3
->num
< (1 << 12));
372 cat3
->c2
.src3
= reg(src3
, info
, instr
->repeat
,
373 IR3_REG_CONST
| IR3_REG_R
| IR3_REG_HALF
);
376 iassert(src3
->num
< (1 << 11));
377 cat3
->src3
= reg(src3
, info
, instr
->repeat
,
378 IR3_REG_R
| IR3_REG_HALF
| absneg
);
381 cat3
->src3_neg
= !!(src3
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
382 cat3
->src3_r
= !!(src3
->flags
& IR3_REG_R
);
384 cat3
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
385 cat3
->repeat
= instr
->repeat
;
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
);
397 static int emit_cat4(struct ir3_instruction
*instr
, void *ptr
,
398 struct ir3_info
*info
)
400 struct ir3_register
*dst
= instr
->regs
[0];
401 struct ir3_register
*src
= instr
->regs
[1];
402 instr_cat4_t
*cat4
= ptr
;
404 iassert(instr
->regs_count
== 2);
406 if (src
->flags
& IR3_REG_RELATIV
) {
407 iassert(src
->num
< (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
);
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
);
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
);
431 cat4
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
432 cat4
->repeat
= instr
->repeat
;
433 cat4
->ss
= !!(instr
->flags
& IR3_INSTR_SS
);
434 cat4
->ul
= !!(instr
->flags
& IR3_INSTR_UL
);
435 cat4
->dst_half
= !!((src
->flags
^ dst
->flags
) & IR3_REG_HALF
);
436 cat4
->full
= ! (src
->flags
& IR3_REG_HALF
);
437 cat4
->opc
= instr
->opc
;
438 cat4
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
439 cat4
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
445 static int emit_cat5(struct ir3_instruction
*instr
, void *ptr
,
446 struct ir3_info
*info
)
448 struct ir3_register
*dst
= instr
->regs
[0];
449 struct ir3_register
*src1
= instr
->regs
[1];
450 struct ir3_register
*src2
= instr
->regs
[2];
451 struct ir3_register
*src3
= instr
->regs
[3];
452 instr_cat5_t
*cat5
= ptr
;
454 iassert(!((dst
->flags
^ type_flags(instr
->cat5
.type
)) & IR3_REG_HALF
));
457 cat5
->full
= ! (src1
->flags
& IR3_REG_HALF
);
458 cat5
->src1
= reg(src1
, info
, instr
->repeat
, IR3_REG_HALF
);
462 if (instr
->flags
& IR3_INSTR_S2EN
) {
464 iassert(!((src1
->flags
^ src2
->flags
) & IR3_REG_HALF
));
465 cat5
->s2en
.src2
= reg(src2
, info
, instr
->repeat
, IR3_REG_HALF
);
468 iassert(src3
->flags
& IR3_REG_HALF
);
469 cat5
->s2en
.src3
= reg(src3
, info
, instr
->repeat
, IR3_REG_HALF
);
471 iassert(!(instr
->cat5
.samp
| instr
->cat5
.tex
));
475 iassert(!((src1
->flags
^ src2
->flags
) & IR3_REG_HALF
));
476 cat5
->norm
.src2
= reg(src2
, info
, instr
->repeat
, IR3_REG_HALF
);
478 cat5
->norm
.samp
= instr
->cat5
.samp
;
479 cat5
->norm
.tex
= instr
->cat5
.tex
;
482 cat5
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
483 cat5
->wrmask
= dst
->wrmask
;
484 cat5
->type
= instr
->cat5
.type
;
485 cat5
->is_3d
= !!(instr
->flags
& IR3_INSTR_3D
);
486 cat5
->is_a
= !!(instr
->flags
& IR3_INSTR_A
);
487 cat5
->is_s
= !!(instr
->flags
& IR3_INSTR_S
);
488 cat5
->is_s2en
= !!(instr
->flags
& IR3_INSTR_S2EN
);
489 cat5
->is_o
= !!(instr
->flags
& IR3_INSTR_O
);
490 cat5
->is_p
= !!(instr
->flags
& IR3_INSTR_P
);
491 cat5
->opc
= instr
->opc
;
492 cat5
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
493 cat5
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
499 static int emit_cat6(struct ir3_instruction
*instr
, void *ptr
,
500 struct ir3_info
*info
)
502 struct ir3_register
*dst
= instr
->regs
[0];
503 struct ir3_register
*src1
= instr
->regs
[1];
504 struct ir3_register
*src2
= (instr
->regs_count
>= 3) ? instr
->regs
[2] : NULL
;
505 instr_cat6_t
*cat6
= ptr
;
507 iassert(instr
->regs_count
>= 2);
509 if (instr
->cat6
.offset
|| instr
->opc
== OPC_LDG
) {
510 instr_cat6a_t
*cat6a
= ptr
;
512 cat6
->has_off
= true;
514 cat6a
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
515 cat6a
->src1
= reg(src1
, info
, instr
->repeat
, IR3_REG_IMMED
);
516 cat6a
->src1_im
= !!(src1
->flags
& IR3_REG_IMMED
);
518 cat6a
->src2
= reg(src2
, info
, instr
->repeat
, IR3_REG_IMMED
);
519 cat6a
->src2_im
= !!(src2
->flags
& IR3_REG_IMMED
);
521 cat6a
->off
= instr
->cat6
.offset
;
523 instr_cat6b_t
*cat6b
= ptr
;
525 cat6
->has_off
= false;
527 cat6b
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
528 cat6b
->src1
= reg(src1
, info
, instr
->repeat
, IR3_REG_IMMED
);
529 cat6b
->src1_im
= !!(src1
->flags
& IR3_REG_IMMED
);
531 cat6b
->src2
= reg(src2
, info
, instr
->repeat
, IR3_REG_IMMED
);
532 cat6b
->src2_im
= !!(src2
->flags
& IR3_REG_IMMED
);
536 cat6
->type
= instr
->cat6
.type
;
537 cat6
->opc
= instr
->opc
;
538 cat6
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
539 cat6
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
545 static int (*emit
[])(struct ir3_instruction
*instr
, void *ptr
,
546 struct ir3_info
*info
) = {
547 emit_cat0
, emit_cat1
, emit_cat2
, emit_cat3
, emit_cat4
, emit_cat5
, emit_cat6
,
550 void * ir3_assemble(struct ir3
*shader
, struct ir3_info
*info
,
553 uint32_t *ptr
, *dwords
;
555 info
->gpu_id
= gpu_id
;
557 info
->max_half_reg
= -1;
558 info
->max_const
= -1;
559 info
->instrs_count
= 0;
560 info
->sizedwords
= 0;
562 list_for_each_entry (struct ir3_block
, block
, &shader
->block_list
, node
) {
563 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
564 info
->sizedwords
+= 2;
568 /* need a integer number of instruction "groups" (sets of 16
569 * instructions on a4xx or sets of 4 instructions on a3xx),
570 * so pad out w/ NOPs if needed: (NOTE each instruction is 64bits)
573 info
->sizedwords
= align(info
->sizedwords
, 16 * 2);
575 info
->sizedwords
= align(info
->sizedwords
, 4 * 2);
578 ptr
= dwords
= calloc(4, info
->sizedwords
);
580 list_for_each_entry (struct ir3_block
, block
, &shader
->block_list
, node
) {
581 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
582 int ret
= emit
[instr
->category
](instr
, dwords
, info
);
585 info
->instrs_count
+= 1 + instr
->repeat
;
597 static struct ir3_register
* reg_create(struct ir3
*shader
,
600 struct ir3_register
*reg
=
601 ir3_alloc(shader
, sizeof(struct ir3_register
));
608 static void insert_instr(struct ir3_block
*block
,
609 struct ir3_instruction
*instr
)
611 struct ir3
*shader
= block
->shader
;
613 static uint32_t serialno
= 0;
614 instr
->serialno
= ++serialno
;
616 list_addtail(&instr
->node
, &block
->instr_list
);
619 array_insert(shader
->baryfs
, instr
);
622 struct ir3_block
* ir3_block_create(struct ir3
*shader
)
624 struct ir3_block
*block
= ir3_alloc(shader
, sizeof(*block
));
626 static uint32_t serialno
= 0;
627 block
->serialno
= ++serialno
;
629 block
->shader
= shader
;
630 list_inithead(&block
->node
);
631 list_inithead(&block
->instr_list
);
635 static struct ir3_instruction
*instr_create(struct ir3_block
*block
, int nreg
)
637 struct ir3_instruction
*instr
;
638 unsigned sz
= sizeof(*instr
) + (nreg
* sizeof(instr
->regs
[0]));
639 char *ptr
= ir3_alloc(block
->shader
, sz
);
641 instr
= (struct ir3_instruction
*)ptr
;
642 ptr
+= sizeof(*instr
);
643 instr
->regs
= (struct ir3_register
**)ptr
;
646 instr
->regs_max
= nreg
;
652 struct ir3_instruction
* ir3_instr_create2(struct ir3_block
*block
,
653 int category
, opc_t opc
, int nreg
)
655 struct ir3_instruction
*instr
= instr_create(block
, nreg
);
656 instr
->block
= block
;
657 instr
->category
= category
;
659 insert_instr(block
, instr
);
663 struct ir3_instruction
* ir3_instr_create(struct ir3_block
*block
,
664 int category
, opc_t opc
)
666 /* NOTE: we could be slightly more clever, at least for non-meta,
667 * and choose # of regs based on category.
669 return ir3_instr_create2(block
, category
, opc
, 4);
672 /* only used by old compiler: */
673 struct ir3_instruction
* ir3_instr_clone(struct ir3_instruction
*instr
)
675 struct ir3_instruction
*new_instr
= instr_create(instr
->block
,
677 struct ir3_register
**regs
;
680 regs
= new_instr
->regs
;
682 new_instr
->regs
= regs
;
684 insert_instr(instr
->block
, new_instr
);
686 /* clone registers: */
687 new_instr
->regs_count
= 0;
688 for (i
= 0; i
< instr
->regs_count
; i
++) {
689 struct ir3_register
*reg
= instr
->regs
[i
];
690 struct ir3_register
*new_reg
=
691 ir3_reg_create(new_instr
, reg
->num
, reg
->flags
);
698 struct ir3_register
* ir3_reg_create(struct ir3_instruction
*instr
,
701 struct ir3
*shader
= instr
->block
->shader
;
702 struct ir3_register
*reg
= reg_create(shader
, num
, flags
);
704 debug_assert(instr
->regs_count
< instr
->regs_max
);
706 instr
->regs
[instr
->regs_count
++] = reg
;
711 ir3_block_clear_mark(struct ir3_block
*block
)
713 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
)
714 instr
->flags
&= ~IR3_INSTR_MARK
;
718 ir3_clear_mark(struct ir3
*ir
)
720 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
721 ir3_block_clear_mark(block
);
725 /* note: this will destroy instr->depth, don't do it until after sched! */
727 ir3_count_instructions(struct ir3
*ir
)
730 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
731 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
734 block
->start_ip
= list_first_entry(&block
->instr_list
, struct ir3_instruction
, node
)->ip
;
735 block
->end_ip
= list_last_entry(&block
->instr_list
, struct ir3_instruction
, node
)->ip
;