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