1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker * Copyright (C) 2020 Collabora, Ltd.
3*61046927SAndroid Build Coastguard Worker *
4*61046927SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
5*61046927SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
6*61046927SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
7*61046927SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*61046927SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
9*61046927SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
10*61046927SAndroid Build Coastguard Worker *
11*61046927SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next
12*61046927SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the
13*61046927SAndroid Build Coastguard Worker * Software.
14*61046927SAndroid Build Coastguard Worker *
15*61046927SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*61046927SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*61046927SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*61046927SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*61046927SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20*61046927SAndroid Build Coastguard Worker * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21*61046927SAndroid Build Coastguard Worker * SOFTWARE.
22*61046927SAndroid Build Coastguard Worker */
23*61046927SAndroid Build Coastguard Worker
24*61046927SAndroid Build Coastguard Worker #include "compiler.h"
25*61046927SAndroid Build Coastguard Worker
26*61046927SAndroid Build Coastguard Worker /* The scheduler packs multiple instructions into a clause (grouped as tuple),
27*61046927SAndroid Build Coastguard Worker * and the packing code takes in a clause and emits it to the wire. During
28*61046927SAndroid Build Coastguard Worker * scheduling, we need to lay out the instructions (tuples) and constants
29*61046927SAndroid Build Coastguard Worker * within the clause so constraints can be resolved during scheduling instead
30*61046927SAndroid Build Coastguard Worker * of failing packing. These routines will help building clauses from
31*61046927SAndroid Build Coastguard Worker * instructions so the scheduler can focus on the high-level algorithm, and
32*61046927SAndroid Build Coastguard Worker * manipulating clause layouts.
33*61046927SAndroid Build Coastguard Worker */
34*61046927SAndroid Build Coastguard Worker
35*61046927SAndroid Build Coastguard Worker /* Is embedded constant 0 packed for free in a clause with this many tuples? */
36*61046927SAndroid Build Coastguard Worker
37*61046927SAndroid Build Coastguard Worker bool
bi_ec0_packed(unsigned tuple_count)38*61046927SAndroid Build Coastguard Worker bi_ec0_packed(unsigned tuple_count)
39*61046927SAndroid Build Coastguard Worker {
40*61046927SAndroid Build Coastguard Worker return (tuple_count == 3) || (tuple_count == 5) || (tuple_count == 6) ||
41*61046927SAndroid Build Coastguard Worker (tuple_count == 8);
42*61046927SAndroid Build Coastguard Worker }
43*61046927SAndroid Build Coastguard Worker
44*61046927SAndroid Build Coastguard Worker /* Helper to calculate the number of quadwords in a clause. This is a function
45*61046927SAndroid Build Coastguard Worker * of the number of instructions and constants; it doesn't require actually
46*61046927SAndroid Build Coastguard Worker * packing, which is useful for branch offsets.
47*61046927SAndroid Build Coastguard Worker *
48*61046927SAndroid Build Coastguard Worker * Table of instruction count to instruction quadwords, per the packing
49*61046927SAndroid Build Coastguard Worker * algorithm, where * indicates a constant is packed for free:
50*61046927SAndroid Build Coastguard Worker *
51*61046927SAndroid Build Coastguard Worker * X | Y
52*61046927SAndroid Build Coastguard Worker * ---|---
53*61046927SAndroid Build Coastguard Worker * 1 | 1
54*61046927SAndroid Build Coastguard Worker * 2 | 2
55*61046927SAndroid Build Coastguard Worker * 3 | 3*
56*61046927SAndroid Build Coastguard Worker * 4 | 3
57*61046927SAndroid Build Coastguard Worker * 5 | 4*
58*61046927SAndroid Build Coastguard Worker * 6 | 5*
59*61046927SAndroid Build Coastguard Worker * 7 | 5
60*61046927SAndroid Build Coastguard Worker * 8 | 6*
61*61046927SAndroid Build Coastguard Worker *
62*61046927SAndroid Build Coastguard Worker * Y = { X if X <= 3
63*61046927SAndroid Build Coastguard Worker * { X - 1 if 4 <= X <= 6
64*61046927SAndroid Build Coastguard Worker * { X - 2 if 7 <= X <= 8
65*61046927SAndroid Build Coastguard Worker *
66*61046927SAndroid Build Coastguard Worker * and there is a constant for free if X is in {3, 5, 6, 8}. The remaining
67*61046927SAndroid Build Coastguard Worker * constants are packed two-by-two as constant quadwords.
68*61046927SAndroid Build Coastguard Worker */
69*61046927SAndroid Build Coastguard Worker
70*61046927SAndroid Build Coastguard Worker static unsigned
bi_clause_quadwords(bi_clause * clause)71*61046927SAndroid Build Coastguard Worker bi_clause_quadwords(bi_clause *clause)
72*61046927SAndroid Build Coastguard Worker {
73*61046927SAndroid Build Coastguard Worker unsigned X = clause->tuple_count;
74*61046927SAndroid Build Coastguard Worker unsigned Y = X - ((X >= 7) ? 2 : (X >= 4) ? 1 : 0);
75*61046927SAndroid Build Coastguard Worker
76*61046927SAndroid Build Coastguard Worker unsigned constants = clause->constant_count;
77*61046927SAndroid Build Coastguard Worker
78*61046927SAndroid Build Coastguard Worker if ((X != 4) && (X != 7) && (X >= 3) && constants)
79*61046927SAndroid Build Coastguard Worker constants--;
80*61046927SAndroid Build Coastguard Worker
81*61046927SAndroid Build Coastguard Worker return Y + DIV_ROUND_UP(constants, 2);
82*61046927SAndroid Build Coastguard Worker }
83*61046927SAndroid Build Coastguard Worker
84*61046927SAndroid Build Coastguard Worker /* Measures the number of quadwords a branch jumps. Bifrost relative offsets
85*61046927SAndroid Build Coastguard Worker * are from the beginning of a clause so to jump forward we count the current
86*61046927SAndroid Build Coastguard Worker * clause length, but to jump backwards we do not. */
87*61046927SAndroid Build Coastguard Worker
88*61046927SAndroid Build Coastguard Worker signed
bi_block_offset(bi_context * ctx,bi_clause * start,bi_block * target)89*61046927SAndroid Build Coastguard Worker bi_block_offset(bi_context *ctx, bi_clause *start, bi_block *target)
90*61046927SAndroid Build Coastguard Worker {
91*61046927SAndroid Build Coastguard Worker /* Signed since we might jump backwards */
92*61046927SAndroid Build Coastguard Worker signed ret = 0;
93*61046927SAndroid Build Coastguard Worker
94*61046927SAndroid Build Coastguard Worker /* Determine if the block we're branching to is strictly greater in
95*61046927SAndroid Build Coastguard Worker * source order */
96*61046927SAndroid Build Coastguard Worker bool forwards = target->index > start->block->index;
97*61046927SAndroid Build Coastguard Worker
98*61046927SAndroid Build Coastguard Worker if (forwards) {
99*61046927SAndroid Build Coastguard Worker /* We have to jump through this block from the start of this
100*61046927SAndroid Build Coastguard Worker * clause to the end */
101*61046927SAndroid Build Coastguard Worker bi_foreach_clause_in_block_from(start->block, clause, start) {
102*61046927SAndroid Build Coastguard Worker ret += bi_clause_quadwords(clause);
103*61046927SAndroid Build Coastguard Worker }
104*61046927SAndroid Build Coastguard Worker
105*61046927SAndroid Build Coastguard Worker /* We then need to jump through every clause of every following
106*61046927SAndroid Build Coastguard Worker * block until the target */
107*61046927SAndroid Build Coastguard Worker bi_foreach_block_from(ctx, start->block, blk) {
108*61046927SAndroid Build Coastguard Worker /* Don't double-count the first block */
109*61046927SAndroid Build Coastguard Worker if (blk == start->block)
110*61046927SAndroid Build Coastguard Worker continue;
111*61046927SAndroid Build Coastguard Worker
112*61046927SAndroid Build Coastguard Worker /* End just before the target */
113*61046927SAndroid Build Coastguard Worker if (blk == target)
114*61046927SAndroid Build Coastguard Worker break;
115*61046927SAndroid Build Coastguard Worker
116*61046927SAndroid Build Coastguard Worker /* Count every clause in the block */
117*61046927SAndroid Build Coastguard Worker bi_foreach_clause_in_block(blk, clause) {
118*61046927SAndroid Build Coastguard Worker ret += bi_clause_quadwords(clause);
119*61046927SAndroid Build Coastguard Worker }
120*61046927SAndroid Build Coastguard Worker }
121*61046927SAndroid Build Coastguard Worker } else {
122*61046927SAndroid Build Coastguard Worker /* We start at the beginning of the clause but have to jump
123*61046927SAndroid Build Coastguard Worker * through the clauses before us in the block */
124*61046927SAndroid Build Coastguard Worker bi_foreach_clause_in_block_from_rev(start->block, clause, start) {
125*61046927SAndroid Build Coastguard Worker if (clause == start)
126*61046927SAndroid Build Coastguard Worker continue;
127*61046927SAndroid Build Coastguard Worker
128*61046927SAndroid Build Coastguard Worker ret -= bi_clause_quadwords(clause);
129*61046927SAndroid Build Coastguard Worker }
130*61046927SAndroid Build Coastguard Worker
131*61046927SAndroid Build Coastguard Worker /* And jump back every clause of preceding blocks up through
132*61046927SAndroid Build Coastguard Worker * and including the target to get to the beginning of the
133*61046927SAndroid Build Coastguard Worker * target */
134*61046927SAndroid Build Coastguard Worker bi_foreach_block_from_rev(ctx, start->block, blk) {
135*61046927SAndroid Build Coastguard Worker if (blk == start->block)
136*61046927SAndroid Build Coastguard Worker continue;
137*61046927SAndroid Build Coastguard Worker
138*61046927SAndroid Build Coastguard Worker bi_foreach_clause_in_block(blk, clause) {
139*61046927SAndroid Build Coastguard Worker ret -= bi_clause_quadwords(clause);
140*61046927SAndroid Build Coastguard Worker }
141*61046927SAndroid Build Coastguard Worker
142*61046927SAndroid Build Coastguard Worker /* End just after the target */
143*61046927SAndroid Build Coastguard Worker if (blk == target)
144*61046927SAndroid Build Coastguard Worker break;
145*61046927SAndroid Build Coastguard Worker }
146*61046927SAndroid Build Coastguard Worker }
147*61046927SAndroid Build Coastguard Worker
148*61046927SAndroid Build Coastguard Worker return ret;
149*61046927SAndroid Build Coastguard Worker }
150