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