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
);
84 list_inithead(&shader
->array_list
);
89 void ir3_destroy(struct ir3
*shader
)
91 while (shader
->chunk
) {
92 struct ir3_heap_chunk
*chunk
= shader
->chunk
;
93 shader
->chunk
= chunk
->next
;
96 free(shader
->indirects
);
97 free(shader
->predicates
);
102 #define iassert(cond) do { \
108 static uint32_t reg(struct ir3_register
*reg
, struct ir3_info
*info
,
109 uint32_t repeat
, uint32_t valid_flags
)
111 reg_t val
= { .dummy32
= 0 };
113 if (reg
->flags
& ~valid_flags
) {
114 debug_printf("INVALID FLAGS: %x vs %x\n",
115 reg
->flags
, valid_flags
);
118 if (!(reg
->flags
& IR3_REG_R
))
121 if (reg
->flags
& IR3_REG_IMMED
) {
122 val
.iim_val
= reg
->iim_val
;
127 if (reg
->flags
& IR3_REG_RELATIV
) {
128 components
= reg
->size
;
129 val
.idummy10
= reg
->array
.offset
;
130 max
= (reg
->array
.offset
+ repeat
+ components
- 1) >> 2;
132 components
= util_last_bit(reg
->wrmask
);
133 val
.comp
= reg
->num
& 0x3;
134 val
.num
= reg
->num
>> 2;
135 max
= (reg
->num
+ repeat
+ components
- 1) >> 2;
138 if (reg
->flags
& IR3_REG_CONST
) {
139 info
->max_const
= MAX2(info
->max_const
, max
);
140 } else if (val
.num
== 63) {
141 /* ignore writes to dummy register r63.x */
142 } else if ((max
!= REG_A0
) && (max
!= REG_P0
)) {
143 if (reg
->flags
& IR3_REG_HALF
) {
144 info
->max_half_reg
= MAX2(info
->max_half_reg
, max
);
146 info
->max_reg
= MAX2(info
->max_reg
, max
);
154 static int emit_cat0(struct ir3_instruction
*instr
, void *ptr
,
155 struct ir3_info
*info
)
157 instr_cat0_t
*cat0
= ptr
;
159 if (info
->gpu_id
>= 400) {
160 cat0
->a4xx
.immed
= instr
->cat0
.immed
;
162 cat0
->a3xx
.immed
= instr
->cat0
.immed
;
164 cat0
->repeat
= instr
->repeat
;
165 cat0
->ss
= !!(instr
->flags
& IR3_INSTR_SS
);
166 cat0
->inv
= instr
->cat0
.inv
;
167 cat0
->comp
= instr
->cat0
.comp
;
168 cat0
->opc
= instr
->opc
;
169 cat0
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
170 cat0
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
176 static uint32_t type_flags(type_t type
)
178 return (type_size(type
) == 32) ? 0 : IR3_REG_HALF
;
181 static int emit_cat1(struct ir3_instruction
*instr
, void *ptr
,
182 struct ir3_info
*info
)
184 struct ir3_register
*dst
= instr
->regs
[0];
185 struct ir3_register
*src
= instr
->regs
[1];
186 instr_cat1_t
*cat1
= ptr
;
188 iassert(instr
->regs_count
== 2);
189 iassert(!((dst
->flags
^ type_flags(instr
->cat1
.dst_type
)) & IR3_REG_HALF
));
190 iassert((src
->flags
& IR3_REG_IMMED
) ||
191 !((src
->flags
^ type_flags(instr
->cat1
.src_type
)) & IR3_REG_HALF
));
193 if (src
->flags
& IR3_REG_IMMED
) {
194 cat1
->iim_val
= src
->iim_val
;
196 } else if (src
->flags
& IR3_REG_RELATIV
) {
197 cat1
->off
= reg(src
, info
, instr
->repeat
,
198 IR3_REG_R
| IR3_REG_CONST
| IR3_REG_HALF
| IR3_REG_RELATIV
);
200 cat1
->src_rel_c
= !!(src
->flags
& IR3_REG_CONST
);
202 cat1
->src
= reg(src
, info
, instr
->repeat
,
203 IR3_REG_R
| IR3_REG_CONST
| IR3_REG_HALF
);
204 cat1
->src_c
= !!(src
->flags
& IR3_REG_CONST
);
207 cat1
->dst
= reg(dst
, info
, instr
->repeat
,
208 IR3_REG_RELATIV
| IR3_REG_EVEN
|
209 IR3_REG_R
| IR3_REG_POS_INF
| IR3_REG_HALF
);
210 cat1
->repeat
= instr
->repeat
;
211 cat1
->src_r
= !!(src
->flags
& IR3_REG_R
);
212 cat1
->ss
= !!(instr
->flags
& IR3_INSTR_SS
);
213 cat1
->ul
= !!(instr
->flags
& IR3_INSTR_UL
);
214 cat1
->dst_type
= instr
->cat1
.dst_type
;
215 cat1
->dst_rel
= !!(dst
->flags
& IR3_REG_RELATIV
);
216 cat1
->src_type
= instr
->cat1
.src_type
;
217 cat1
->even
= !!(dst
->flags
& IR3_REG_EVEN
);
218 cat1
->pos_inf
= !!(dst
->flags
& IR3_REG_POS_INF
);
219 cat1
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
220 cat1
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
226 static int emit_cat2(struct ir3_instruction
*instr
, void *ptr
,
227 struct ir3_info
*info
)
229 struct ir3_register
*dst
= instr
->regs
[0];
230 struct ir3_register
*src1
= instr
->regs
[1];
231 struct ir3_register
*src2
= instr
->regs
[2];
232 instr_cat2_t
*cat2
= ptr
;
233 unsigned absneg
= ir3_cat2_absneg(instr
->opc
);
235 iassert((instr
->regs_count
== 2) || (instr
->regs_count
== 3));
237 if (src1
->flags
& IR3_REG_RELATIV
) {
238 iassert(src1
->array
.offset
< (1 << 10));
239 cat2
->rel1
.src1
= reg(src1
, info
, instr
->repeat
,
240 IR3_REG_RELATIV
| IR3_REG_CONST
| IR3_REG_R
|
241 IR3_REG_HALF
| absneg
);
242 cat2
->rel1
.src1_c
= !!(src1
->flags
& IR3_REG_CONST
);
243 cat2
->rel1
.src1_rel
= 1;
244 } else if (src1
->flags
& IR3_REG_CONST
) {
245 iassert(src1
->num
< (1 << 12));
246 cat2
->c1
.src1
= reg(src1
, info
, instr
->repeat
,
247 IR3_REG_CONST
| IR3_REG_R
| IR3_REG_HALF
);
250 iassert(src1
->num
< (1 << 11));
251 cat2
->src1
= reg(src1
, info
, instr
->repeat
,
252 IR3_REG_IMMED
| IR3_REG_R
| IR3_REG_HALF
|
255 cat2
->src1_im
= !!(src1
->flags
& IR3_REG_IMMED
);
256 cat2
->src1_neg
= !!(src1
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
257 cat2
->src1_abs
= !!(src1
->flags
& (IR3_REG_FABS
| IR3_REG_SABS
));
258 cat2
->src1_r
= !!(src1
->flags
& IR3_REG_R
);
261 iassert((src2
->flags
& IR3_REG_IMMED
) ||
262 !((src1
->flags
^ src2
->flags
) & IR3_REG_HALF
));
264 if (src2
->flags
& IR3_REG_RELATIV
) {
265 iassert(src2
->array
.offset
< (1 << 10));
266 cat2
->rel2
.src2
= reg(src2
, info
, instr
->repeat
,
267 IR3_REG_RELATIV
| IR3_REG_CONST
| IR3_REG_R
|
268 IR3_REG_HALF
| absneg
);
269 cat2
->rel2
.src2_c
= !!(src2
->flags
& IR3_REG_CONST
);
270 cat2
->rel2
.src2_rel
= 1;
271 } else if (src2
->flags
& IR3_REG_CONST
) {
272 iassert(src2
->num
< (1 << 12));
273 cat2
->c2
.src2
= reg(src2
, info
, instr
->repeat
,
274 IR3_REG_CONST
| IR3_REG_R
| IR3_REG_HALF
);
277 iassert(src2
->num
< (1 << 11));
278 cat2
->src2
= reg(src2
, info
, instr
->repeat
,
279 IR3_REG_IMMED
| IR3_REG_R
| IR3_REG_HALF
|
283 cat2
->src2_im
= !!(src2
->flags
& IR3_REG_IMMED
);
284 cat2
->src2_neg
= !!(src2
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
285 cat2
->src2_abs
= !!(src2
->flags
& (IR3_REG_FABS
| IR3_REG_SABS
));
286 cat2
->src2_r
= !!(src2
->flags
& IR3_REG_R
);
289 cat2
->dst
= reg(dst
, info
, instr
->repeat
,
290 IR3_REG_R
| IR3_REG_EI
| IR3_REG_HALF
);
291 cat2
->repeat
= instr
->repeat
;
292 cat2
->ss
= !!(instr
->flags
& IR3_INSTR_SS
);
293 cat2
->ul
= !!(instr
->flags
& IR3_INSTR_UL
);
294 cat2
->dst_half
= !!((src1
->flags
^ dst
->flags
) & IR3_REG_HALF
);
295 cat2
->ei
= !!(dst
->flags
& IR3_REG_EI
);
296 cat2
->cond
= instr
->cat2
.condition
;
297 cat2
->full
= ! (src1
->flags
& IR3_REG_HALF
);
298 cat2
->opc
= instr
->opc
;
299 cat2
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
300 cat2
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
306 static int emit_cat3(struct ir3_instruction
*instr
, void *ptr
,
307 struct ir3_info
*info
)
309 struct ir3_register
*dst
= instr
->regs
[0];
310 struct ir3_register
*src1
= instr
->regs
[1];
311 struct ir3_register
*src2
= instr
->regs
[2];
312 struct ir3_register
*src3
= instr
->regs
[3];
313 unsigned absneg
= ir3_cat3_absneg(instr
->opc
);
314 instr_cat3_t
*cat3
= ptr
;
315 uint32_t src_flags
= 0;
317 switch (instr
->opc
) {
325 case OPC_SAD_S32
: // really??
326 src_flags
|= IR3_REG_HALF
;
332 iassert(instr
->regs_count
== 4);
333 iassert(!((src1
->flags
^ src_flags
) & IR3_REG_HALF
));
334 iassert(!((src2
->flags
^ src_flags
) & IR3_REG_HALF
));
335 iassert(!((src3
->flags
^ src_flags
) & IR3_REG_HALF
));
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
);
350 iassert(src1
->num
< (1 << 11));
351 cat3
->src1
= reg(src1
, info
, instr
->repeat
,
352 IR3_REG_R
| IR3_REG_HALF
| absneg
);
355 cat3
->src1_neg
= !!(src1
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
356 cat3
->src1_r
= !!(src1
->flags
& IR3_REG_R
);
358 cat3
->src2
= reg(src2
, info
, instr
->repeat
,
359 IR3_REG_CONST
| IR3_REG_R
| IR3_REG_HALF
| absneg
);
360 cat3
->src2_c
= !!(src2
->flags
& IR3_REG_CONST
);
361 cat3
->src2_neg
= !!(src2
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
362 cat3
->src2_r
= !!(src2
->flags
& IR3_REG_R
);
365 if (src3
->flags
& IR3_REG_RELATIV
) {
366 iassert(src3
->array
.offset
< (1 << 10));
367 cat3
->rel2
.src3
= reg(src3
, info
, instr
->repeat
,
368 IR3_REG_RELATIV
| IR3_REG_CONST
| IR3_REG_R
|
369 IR3_REG_HALF
| absneg
);
370 cat3
->rel2
.src3_c
= !!(src3
->flags
& IR3_REG_CONST
);
371 cat3
->rel2
.src3_rel
= 1;
372 } else if (src3
->flags
& IR3_REG_CONST
) {
373 iassert(src3
->num
< (1 << 12));
374 cat3
->c2
.src3
= reg(src3
, info
, instr
->repeat
,
375 IR3_REG_CONST
| IR3_REG_R
| IR3_REG_HALF
);
378 iassert(src3
->num
< (1 << 11));
379 cat3
->src3
= reg(src3
, info
, instr
->repeat
,
380 IR3_REG_R
| IR3_REG_HALF
| absneg
);
383 cat3
->src3_neg
= !!(src3
->flags
& (IR3_REG_FNEG
| IR3_REG_SNEG
| IR3_REG_BNOT
));
384 cat3
->src3_r
= !!(src3
->flags
& IR3_REG_R
);
386 cat3
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
387 cat3
->repeat
= instr
->repeat
;
388 cat3
->ss
= !!(instr
->flags
& IR3_INSTR_SS
);
389 cat3
->ul
= !!(instr
->flags
& IR3_INSTR_UL
);
390 cat3
->dst_half
= !!((src_flags
^ dst
->flags
) & IR3_REG_HALF
);
391 cat3
->opc
= instr
->opc
;
392 cat3
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
393 cat3
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
399 static int emit_cat4(struct ir3_instruction
*instr
, void *ptr
,
400 struct ir3_info
*info
)
402 struct ir3_register
*dst
= instr
->regs
[0];
403 struct ir3_register
*src
= instr
->regs
[1];
404 instr_cat4_t
*cat4
= ptr
;
406 iassert(instr
->regs_count
== 2);
408 if (src
->flags
& IR3_REG_RELATIV
) {
409 iassert(src
->array
.offset
< (1 << 10));
410 cat4
->rel
.src
= reg(src
, info
, instr
->repeat
,
411 IR3_REG_RELATIV
| IR3_REG_CONST
| IR3_REG_FNEG
|
412 IR3_REG_FABS
| IR3_REG_R
| IR3_REG_HALF
);
413 cat4
->rel
.src_c
= !!(src
->flags
& IR3_REG_CONST
);
414 cat4
->rel
.src_rel
= 1;
415 } else if (src
->flags
& IR3_REG_CONST
) {
416 iassert(src
->num
< (1 << 12));
417 cat4
->c
.src
= reg(src
, info
, instr
->repeat
,
418 IR3_REG_CONST
| IR3_REG_FNEG
| IR3_REG_FABS
|
419 IR3_REG_R
| IR3_REG_HALF
);
422 iassert(src
->num
< (1 << 11));
423 cat4
->src
= reg(src
, info
, instr
->repeat
,
424 IR3_REG_IMMED
| IR3_REG_FNEG
| IR3_REG_FABS
|
425 IR3_REG_R
| IR3_REG_HALF
);
428 cat4
->src_im
= !!(src
->flags
& IR3_REG_IMMED
);
429 cat4
->src_neg
= !!(src
->flags
& IR3_REG_FNEG
);
430 cat4
->src_abs
= !!(src
->flags
& IR3_REG_FABS
);
431 cat4
->src_r
= !!(src
->flags
& IR3_REG_R
);
433 cat4
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
434 cat4
->repeat
= instr
->repeat
;
435 cat4
->ss
= !!(instr
->flags
& IR3_INSTR_SS
);
436 cat4
->ul
= !!(instr
->flags
& IR3_INSTR_UL
);
437 cat4
->dst_half
= !!((src
->flags
^ dst
->flags
) & IR3_REG_HALF
);
438 cat4
->full
= ! (src
->flags
& IR3_REG_HALF
);
439 cat4
->opc
= instr
->opc
;
440 cat4
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
441 cat4
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
447 static int emit_cat5(struct ir3_instruction
*instr
, void *ptr
,
448 struct ir3_info
*info
)
450 struct ir3_register
*dst
= instr
->regs
[0];
451 struct ir3_register
*src1
= instr
->regs
[1];
452 struct ir3_register
*src2
= instr
->regs
[2];
453 struct ir3_register
*src3
= instr
->regs
[3];
454 instr_cat5_t
*cat5
= ptr
;
456 iassert(!((dst
->flags
^ type_flags(instr
->cat5
.type
)) & IR3_REG_HALF
));
459 cat5
->full
= ! (src1
->flags
& IR3_REG_HALF
);
460 cat5
->src1
= reg(src1
, info
, instr
->repeat
, IR3_REG_HALF
);
464 if (instr
->flags
& IR3_INSTR_S2EN
) {
466 iassert(!((src1
->flags
^ src2
->flags
) & IR3_REG_HALF
));
467 cat5
->s2en
.src2
= reg(src2
, info
, instr
->repeat
, IR3_REG_HALF
);
470 iassert(src3
->flags
& IR3_REG_HALF
);
471 cat5
->s2en
.src3
= reg(src3
, info
, instr
->repeat
, IR3_REG_HALF
);
473 iassert(!(instr
->cat5
.samp
| instr
->cat5
.tex
));
477 iassert(!((src1
->flags
^ src2
->flags
) & IR3_REG_HALF
));
478 cat5
->norm
.src2
= reg(src2
, info
, instr
->repeat
, IR3_REG_HALF
);
480 cat5
->norm
.samp
= instr
->cat5
.samp
;
481 cat5
->norm
.tex
= instr
->cat5
.tex
;
484 cat5
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
485 cat5
->wrmask
= dst
->wrmask
;
486 cat5
->type
= instr
->cat5
.type
;
487 cat5
->is_3d
= !!(instr
->flags
& IR3_INSTR_3D
);
488 cat5
->is_a
= !!(instr
->flags
& IR3_INSTR_A
);
489 cat5
->is_s
= !!(instr
->flags
& IR3_INSTR_S
);
490 cat5
->is_s2en
= !!(instr
->flags
& IR3_INSTR_S2EN
);
491 cat5
->is_o
= !!(instr
->flags
& IR3_INSTR_O
);
492 cat5
->is_p
= !!(instr
->flags
& IR3_INSTR_P
);
493 cat5
->opc
= instr
->opc
;
494 cat5
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
495 cat5
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
501 static int emit_cat6(struct ir3_instruction
*instr
, void *ptr
,
502 struct ir3_info
*info
)
504 struct ir3_register
*dst
, *src1
, *src2
;
505 instr_cat6_t
*cat6
= ptr
;
507 /* the "dst" for a store instruction is (from the perspective
508 * of data flow in the shader, ie. register use/def, etc) in
509 * fact a register that is read by the instruction, rather
512 if (is_store(instr
)) {
513 iassert(instr
->regs_count
>= 3);
515 dst
= instr
->regs
[1];
516 src1
= instr
->regs
[2];
517 src2
= (instr
->regs_count
>= 4) ? instr
->regs
[3] : NULL
;
519 iassert(instr
->regs_count
>= 2);
521 dst
= instr
->regs
[0];
522 src1
= instr
->regs
[1];
523 src2
= (instr
->regs_count
>= 3) ? instr
->regs
[2] : NULL
;
527 /* TODO we need a more comprehensive list about which instructions
528 * can be encoded which way. Or possibly use IR3_INSTR_0 flag to
529 * indicate to use the src_off encoding even if offset is zero
530 * (but then what to do about dst_off?)
532 if (instr
->cat6
.src_offset
|| (instr
->opc
== OPC_LDG
)) {
533 instr_cat6a_t
*cat6a
= ptr
;
535 cat6
->src_off
= true;
537 cat6a
->src1
= reg(src1
, info
, instr
->repeat
, IR3_REG_IMMED
);
538 cat6a
->src1_im
= !!(src1
->flags
& IR3_REG_IMMED
);
540 cat6a
->src2
= reg(src2
, info
, instr
->repeat
, IR3_REG_IMMED
);
541 cat6a
->src2_im
= !!(src2
->flags
& IR3_REG_IMMED
);
543 cat6a
->off
= instr
->cat6
.src_offset
;
545 instr_cat6b_t
*cat6b
= ptr
;
547 cat6
->src_off
= false;
549 cat6b
->src1
= reg(src1
, info
, instr
->repeat
, IR3_REG_IMMED
);
550 cat6b
->src1_im
= !!(src1
->flags
& IR3_REG_IMMED
);
552 cat6b
->src2
= reg(src2
, info
, instr
->repeat
, IR3_REG_IMMED
);
553 cat6b
->src2_im
= !!(src2
->flags
& IR3_REG_IMMED
);
557 if (instr
->cat6
.dst_offset
|| (instr
->opc
== OPC_STG
)) {
558 instr_cat6c_t
*cat6c
= ptr
;
559 cat6
->dst_off
= true;
560 cat6c
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
561 cat6c
->off
= instr
->cat6
.dst_offset
;
563 instr_cat6d_t
*cat6d
= ptr
;
564 cat6
->dst_off
= false;
565 cat6d
->dst
= reg(dst
, info
, instr
->repeat
, IR3_REG_R
| IR3_REG_HALF
);
568 cat6
->type
= instr
->cat6
.type
;
569 cat6
->opc
= instr
->opc
;
570 cat6
->jmp_tgt
= !!(instr
->flags
& IR3_INSTR_JP
);
571 cat6
->sync
= !!(instr
->flags
& IR3_INSTR_SY
);
572 cat6
->g
= !!(instr
->flags
& IR3_INSTR_G
);
578 static int (*emit
[])(struct ir3_instruction
*instr
, void *ptr
,
579 struct ir3_info
*info
) = {
580 emit_cat0
, emit_cat1
, emit_cat2
, emit_cat3
, emit_cat4
, emit_cat5
, emit_cat6
,
583 void * ir3_assemble(struct ir3
*shader
, struct ir3_info
*info
,
586 uint32_t *ptr
, *dwords
;
588 info
->gpu_id
= gpu_id
;
590 info
->max_half_reg
= -1;
591 info
->max_const
= -1;
592 info
->instrs_count
= 0;
593 info
->sizedwords
= 0;
595 list_for_each_entry (struct ir3_block
, block
, &shader
->block_list
, node
) {
596 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
597 info
->sizedwords
+= 2;
601 /* need a integer number of instruction "groups" (sets of 16
602 * instructions on a4xx or sets of 4 instructions on a3xx),
603 * so pad out w/ NOPs if needed: (NOTE each instruction is 64bits)
606 info
->sizedwords
= align(info
->sizedwords
, 16 * 2);
608 info
->sizedwords
= align(info
->sizedwords
, 4 * 2);
611 ptr
= dwords
= calloc(4, info
->sizedwords
);
613 list_for_each_entry (struct ir3_block
, block
, &shader
->block_list
, node
) {
614 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
615 int ret
= emit
[instr
->category
](instr
, dwords
, info
);
618 info
->instrs_count
+= 1 + instr
->repeat
;
630 static struct ir3_register
* reg_create(struct ir3
*shader
,
633 struct ir3_register
*reg
=
634 ir3_alloc(shader
, sizeof(struct ir3_register
));
641 static void insert_instr(struct ir3_block
*block
,
642 struct ir3_instruction
*instr
)
644 struct ir3
*shader
= block
->shader
;
646 static uint32_t serialno
= 0;
647 instr
->serialno
= ++serialno
;
649 list_addtail(&instr
->node
, &block
->instr_list
);
652 array_insert(shader
->baryfs
, instr
);
655 struct ir3_block
* ir3_block_create(struct ir3
*shader
)
657 struct ir3_block
*block
= ir3_alloc(shader
, sizeof(*block
));
659 static uint32_t serialno
= 0;
660 block
->serialno
= ++serialno
;
662 block
->shader
= shader
;
663 list_inithead(&block
->node
);
664 list_inithead(&block
->instr_list
);
668 static struct ir3_instruction
*instr_create(struct ir3_block
*block
, int nreg
)
670 struct ir3_instruction
*instr
;
671 unsigned sz
= sizeof(*instr
) + (nreg
* sizeof(instr
->regs
[0]));
672 char *ptr
= ir3_alloc(block
->shader
, sz
);
674 instr
= (struct ir3_instruction
*)ptr
;
675 ptr
+= sizeof(*instr
);
676 instr
->regs
= (struct ir3_register
**)ptr
;
679 instr
->regs_max
= nreg
;
685 struct ir3_instruction
* ir3_instr_create2(struct ir3_block
*block
,
686 int category
, opc_t opc
, int nreg
)
688 struct ir3_instruction
*instr
= instr_create(block
, nreg
);
689 instr
->block
= block
;
690 instr
->category
= category
;
691 debug_assert(opc_cat(opc
) == category
);
693 insert_instr(block
, instr
);
697 struct ir3_instruction
* ir3_instr_create(struct ir3_block
*block
,
698 int category
, opc_t opc
)
700 /* NOTE: we could be slightly more clever, at least for non-meta,
701 * and choose # of regs based on category.
703 return ir3_instr_create2(block
, category
, opc
, 4);
706 struct ir3_instruction
* ir3_instr_clone(struct ir3_instruction
*instr
)
708 struct ir3_instruction
*new_instr
= instr_create(instr
->block
,
710 struct ir3_register
**regs
;
713 regs
= new_instr
->regs
;
715 new_instr
->regs
= regs
;
717 insert_instr(instr
->block
, new_instr
);
719 /* clone registers: */
720 new_instr
->regs_count
= 0;
721 for (i
= 0; i
< instr
->regs_count
; i
++) {
722 struct ir3_register
*reg
= instr
->regs
[i
];
723 struct ir3_register
*new_reg
=
724 ir3_reg_create(new_instr
, reg
->num
, reg
->flags
);
731 struct ir3_register
* ir3_reg_create(struct ir3_instruction
*instr
,
734 struct ir3
*shader
= instr
->block
->shader
;
735 struct ir3_register
*reg
= reg_create(shader
, num
, flags
);
737 debug_assert(instr
->regs_count
< instr
->regs_max
);
739 instr
->regs
[instr
->regs_count
++] = reg
;
743 struct ir3_register
* ir3_reg_clone(struct ir3
*shader
,
744 struct ir3_register
*reg
)
746 struct ir3_register
*new_reg
= reg_create(shader
, 0, 0);
752 ir3_instr_set_address(struct ir3_instruction
*instr
,
753 struct ir3_instruction
*addr
)
755 if (instr
->address
!= addr
) {
756 struct ir3
*ir
= instr
->block
->shader
;
757 instr
->address
= addr
;
758 array_insert(ir
->indirects
, instr
);
763 ir3_block_clear_mark(struct ir3_block
*block
)
765 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
)
766 instr
->flags
&= ~IR3_INSTR_MARK
;
770 ir3_clear_mark(struct ir3
*ir
)
772 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
773 ir3_block_clear_mark(block
);
777 /* note: this will destroy instr->depth, don't do it until after sched! */
779 ir3_count_instructions(struct ir3
*ir
)
782 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
783 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
786 block
->start_ip
= list_first_entry(&block
->instr_list
, struct ir3_instruction
, node
)->ip
;
787 block
->end_ip
= list_last_entry(&block
->instr_list
, struct ir3_instruction
, node
)->ip
;
793 ir3_lookup_array(struct ir3
*ir
, unsigned id
)
795 list_for_each_entry (struct ir3_array
, arr
, &ir
->array_list
, node
)