| 1 |
/* |
| 2 |
* Copyright (C) 2021 Collabora Ltd. |
| 3 |
* Copyright (C) 2025 Arm Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "bi_builder.h" |
| 8 |
#include "va_compiler.h" |
| 9 |
|
| 10 |
/* |
| 11 |
* Bifrost uses split 64-bit addresses, specified as two consecutive sources. |
| 12 |
* Valhall uses contiguous 64-bit addresses, specified as a single source with |
| 13 |
* an aligned register pair. This simple pass inserts moves to lower split |
| 14 |
* 64-bit sources into a collect, ensuring the register constraints will be |
| 15 |
* respected by RA. This could be optimized, but this is deferred for now. |
| 16 |
*/ |
| 17 |
static void |
| 18 |
lower_split_src(bi_context *ctx, bi_instr *I, unsigned s, bi_instr** lut) |
| 19 |
{ |
| 20 |
/* Skip sources that are already split properly */ |
| 21 |
bi_index offset_fau = I->src[s]; |
| 22 |
offset_fau.offset++; |
| 23 |
|
| 24 |
if (I->src[s].type == BI_INDEX_FAU && I->src[s].offset == 0 && |
| 25 |
bi_is_value_equiv(offset_fau, I->src[s + 1])) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
/* Check if the source regs are already coming from a split/collect pair. */ |
| 30 |
bi_index *src_a = &I->src[s]; |
| 31 |
bi_index *src_b = &I->src[s + 1]; |
| 32 |
if (bi_is_ssa(*src_a) && bi_is_ssa(*src_b)) { |
| 33 |
bi_instr *src_ins_a = lut[src_a->value]; |
| 34 |
bi_instr *src_ins_b = lut[src_b->value]; |
| 35 |
if (src_ins_a->op == BI_OPCODE_SPLIT_I32 && src_ins_a == src_ins_b) { |
| 36 |
bi_index split_src = src_ins_a->src[0]; |
| 37 |
if (!bi_is_ssa(split_src) || |
| 38 |
lut[split_src.value]->op == BI_OPCODE_COLLECT_I32) |
| 39 |
return; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/* Allocate temporary before the instruction */ |
| 44 |
bi_builder b = bi_init_builder(ctx, bi_before_instr(I)); |
| 45 |
bi_index vec = bi_temp(ctx); |
| 46 |
bi_instr *collect = bi_collect_i32_to(&b, vec, 2); |
| 47 |
bi_instr *split = bi_split_i32_to(&b, 2, vec); |
| 48 |
|
| 49 |
/* Emit collect */ |
| 50 |
for (unsigned w = 0; w < 2; ++w) { |
| 51 |
collect->src[w] = I->src[s + w]; |
| 52 |
|
| 53 |
split->dest[w] = bi_temp(ctx); |
| 54 |
I->src[s + w] = split->dest[w]; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
void |
| 59 |
va_lower_split_64bit(bi_context *ctx) |
| 60 |
{ |
| 61 |
unsigned count = ctx->ssa_alloc; |
| 62 |
bi_instr **lut = rzalloc_array(ctx, bi_instr *, count); |
| 63 |
|
| 64 |
if (!lut) |
| 65 |
return; |
| 66 |
|
| 67 |
/* Record producers */ |
| 68 |
bi_foreach_block(ctx, block) { |
| 69 |
bi_foreach_instr_in_block(block, I) { |
| 70 |
bi_foreach_dest(I, d) { |
| 71 |
lut[I->dest[d].value] = I; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
bi_foreach_instr_global(ctx, I) { |
| 77 |
bi_foreach_src(I, s) { |
| 78 |
if (bi_is_null(I->src[s]) || s >= 4) |
| 79 |
continue; |
| 80 |
|
| 81 |
struct va_src_info info = va_src_info(I->op, s); |
| 82 |
|
| 83 |
/* Only split if the instruction expects 64-bit inputs as two separate |
| 84 |
* sources. */ |
| 85 |
if (info.size == VA_SIZE_64 && bi_count_read_registers(I, s) == 1) |
| 86 |
lower_split_src(ctx, I, s, lut); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
ralloc_free(lut); |
| 91 |
} |