pan/bi: Add register allocator
[mesa.git] / src / panfrost / bifrost / bi_ra.c
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 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27 #include "compiler.h"
28 #include "panfrost/util/lcra.h"
29 #include "util/u_memory.h"
30
31 static void
32 bi_compute_interference(bi_context *ctx, struct lcra_state *l)
33 {
34 bi_compute_liveness(ctx);
35
36 bi_foreach_block(ctx, _blk) {
37 bi_block *blk = (bi_block *) _blk;
38 uint16_t *live = mem_dup(_blk->live_out, l->node_count * sizeof(uint16_t));
39
40 bi_foreach_instr_in_block_rev(blk, ins) {
41 /* Mark all registers live after the instruction as
42 * interfering with the destination */
43
44 if (ins->dest && (ins->dest < l->node_count)) {
45 for (unsigned i = 1; i < l->node_count; ++i) {
46 if (live[i])
47 lcra_add_node_interference(l, ins->dest, ins->writemask, i, live[i]);
48 }
49 }
50
51 /* Update live_in */
52 bi_liveness_ins_update(live, ins, l->node_count);
53 }
54
55 free(live);
56 }
57 }
58
59 enum {
60 BI_REG_CLASS_WORK = 0,
61 } bi_reg_class;
62
63 static struct lcra_state *
64 bi_allocate_registers(bi_context *ctx, bool *success)
65 {
66 unsigned node_count = bi_max_temp(ctx);
67
68 struct lcra_state *l =
69 lcra_alloc_equations(node_count, 1, 8, 16, 1);
70
71 l->class_start[BI_REG_CLASS_WORK] = 0;
72 l->class_size[BI_REG_CLASS_WORK] = 64 * 4; /* R0 - R63, all 32-bit */
73
74 bi_foreach_instr_global(ctx, ins) {
75 unsigned dest = ins->dest;
76
77 if (!dest || (dest >= node_count))
78 continue;
79
80 l->class[dest] = BI_REG_CLASS_WORK;
81 lcra_set_alignment(l, dest, 2); /* 2^2 = 4 */
82 lcra_restrict_range(l, dest, 4);
83 }
84
85 bi_compute_interference(ctx, l);
86
87 *success = lcra_solve(l);
88
89 return l;
90 }
91
92 void
93 bi_register_allocate(bi_context *ctx)
94 {
95 struct lcra_state *l = NULL;
96 bool success = false;
97
98 do {
99 if (l) {
100 lcra_free(l);
101 l = NULL;
102 }
103
104 bi_invalidate_liveness(ctx);
105 l = bi_allocate_registers(ctx, &success);
106
107 /* TODO: Spilling */
108 assert(success);
109 } while(!success);
110
111 lcra_free(l);
112 }