| 1 |
/* |
| 2 |
* Copyright (C) 2020 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "compiler.h" |
| 7 |
|
| 8 |
/* The scheduler packs multiple instructions into a clause (grouped as tuple), |
| 9 |
* and the packing code takes in a clause and emits it to the wire. During |
| 10 |
* scheduling, we need to lay out the instructions (tuples) and constants |
| 11 |
* within the clause so constraints can be resolved during scheduling instead |
| 12 |
* of failing packing. These routines will help building clauses from |
| 13 |
* instructions so the scheduler can focus on the high-level algorithm, and |
| 14 |
* manipulating clause layouts. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* Is embedded constant 0 packed for free in a clause with this many tuples? */ |
| 18 |
|
| 19 |
bool |
| 20 |
bi_ec0_packed(unsigned tuple_count) |
| 21 |
{ |
| 22 |
return (tuple_count == 3) || (tuple_count == 5) || (tuple_count == 6) || |
| 23 |
(tuple_count == 8); |
| 24 |
} |
| 25 |
|
| 26 |
/* Helper to calculate the number of quadwords in a clause. This is a function |
| 27 |
* of the number of instructions and constants; it doesn't require actually |
| 28 |
* packing, which is useful for branch offsets. |
| 29 |
* |
| 30 |
* Table of instruction count to instruction quadwords, per the packing |
| 31 |
* algorithm, where * indicates a constant is packed for free: |
| 32 |
* |
| 33 |
* X | Y |
| 34 |
* ---|--- |
| 35 |
* 1 | 1 |
| 36 |
* 2 | 2 |
| 37 |
* 3 | 3* |
| 38 |
* 4 | 3 |
| 39 |
* 5 | 4* |
| 40 |
* 6 | 5* |
| 41 |
* 7 | 5 |
| 42 |
* 8 | 6* |
| 43 |
* |
| 44 |
* Y = { X if X <= 3 |
| 45 |
* { X - 1 if 4 <= X <= 6 |
| 46 |
* { X - 2 if 7 <= X <= 8 |
| 47 |
* |
| 48 |
* and there is a constant for free if X is in {3, 5, 6, 8}. The remaining |
| 49 |
* constants are packed two-by-two as constant quadwords. |
| 50 |
*/ |
| 51 |
|
| 52 |
static unsigned |
| 53 |
bi_clause_quadwords(bi_clause *clause) |
| 54 |
{ |
| 55 |
unsigned X = clause->tuple_count; |
| 56 |
unsigned Y = X - ((X >= 7) ? 2 : (X >= 4) ? 1 : 0); |
| 57 |
|
| 58 |
unsigned constants = clause->constant_count; |
| 59 |
|
| 60 |
if ((X != 4) && (X != 7) && (X >= 3) && constants) |
| 61 |
constants--; |
| 62 |
|
| 63 |
return Y + DIV_ROUND_UP(constants, 2); |
| 64 |
} |
| 65 |
|
| 66 |
/* Measures the number of quadwords a branch jumps. Bifrost relative offsets |
| 67 |
* are from the beginning of a clause so to jump forward we count the current |
| 68 |
* clause length, but to jump backwards we do not. */ |
| 69 |
|
| 70 |
signed |
| 71 |
bi_block_offset(bi_context *ctx, bi_clause *start, bi_block *target) |
| 72 |
{ |
| 73 |
/* Signed since we might jump backwards */ |
| 74 |
signed ret = 0; |
| 75 |
|
| 76 |
/* Determine if the block we're branching to is strictly greater in |
| 77 |
* source order */ |
| 78 |
bool forwards = target->index > start->block->index; |
| 79 |
|
| 80 |
if (forwards) { |
| 81 |
/* We have to jump through this block from the start of this |
| 82 |
* clause to the end */ |
| 83 |
bi_foreach_clause_in_block_from(start->block, clause, start) { |
| 84 |
ret += bi_clause_quadwords(clause); |
| 85 |
} |
| 86 |
|
| 87 |
/* We then need to jump through every clause of every following |
| 88 |
* block until the target */ |
| 89 |
bi_foreach_block_from(ctx, start->block, blk) { |
| 90 |
/* Don't double-count the first block */ |
| 91 |
if (blk == start->block) |
| 92 |
continue; |
| 93 |
|
| 94 |
/* End just before the target */ |
| 95 |
if (blk == target) |
| 96 |
break; |
| 97 |
|
| 98 |
/* Count every clause in the block */ |
| 99 |
bi_foreach_clause_in_block(blk, clause) { |
| 100 |
ret += bi_clause_quadwords(clause); |
| 101 |
} |
| 102 |
} |
| 103 |
} else { |
| 104 |
/* We start at the beginning of the clause but have to jump |
| 105 |
* through the clauses before us in the block */ |
| 106 |
bi_foreach_clause_in_block_from_rev(start->block, clause, start) { |
| 107 |
if (clause == start) |
| 108 |
continue; |
| 109 |
|
| 110 |
ret -= bi_clause_quadwords(clause); |
| 111 |
} |
| 112 |
|
| 113 |
/* And jump back every clause of preceding blocks up through |
| 114 |
* and including the target to get to the beginning of the |
| 115 |
* target */ |
| 116 |
bi_foreach_block_from_rev(ctx, start->block, blk) { |
| 117 |
if (blk == start->block) |
| 118 |
continue; |
| 119 |
|
| 120 |
bi_foreach_clause_in_block(blk, clause) { |
| 121 |
ret -= bi_clause_quadwords(clause); |
| 122 |
} |
| 123 |
|
| 124 |
/* End just after the target */ |
| 125 |
if (blk == target) |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return ret; |
| 131 |
} |