e1138c6a506dbc6048b0dae15dbf15792ba79bba
[mesa.git] / src / util / register_allocate.c
1 /*
2 * Copyright © 2010 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 /** @file register_allocate.c
29 *
30 * Graph-coloring register allocator.
31 *
32 * The basic idea of graph coloring is to make a node in a graph for
33 * every thing that needs a register (color) number assigned, and make
34 * edges in the graph between nodes that interfere (can't be allocated
35 * to the same register at the same time).
36 *
37 * During the "simplify" process, any any node with fewer edges than
38 * there are registers means that that edge can get assigned a
39 * register regardless of what its neighbors choose, so that node is
40 * pushed on a stack and removed (with its edges) from the graph.
41 * That likely causes other nodes to become trivially colorable as well.
42 *
43 * Then during the "select" process, nodes are popped off of that
44 * stack, their edges restored, and assigned a color different from
45 * their neighbors. Because they were pushed on the stack only when
46 * they were trivially colorable, any color chosen won't interfere
47 * with the registers to be popped later.
48 *
49 * The downside to most graph coloring is that real hardware often has
50 * limitations, like registers that need to be allocated to a node in
51 * pairs, or aligned on some boundary. This implementation follows
52 * the paper "Retargetable Graph-Coloring Register Allocation for
53 * Irregular Architectures" by Johan Runeson and Sven-Olof Nyström.
54 *
55 * In this system, there are register classes each containing various
56 * registers, and registers may interfere with other registers. For
57 * example, one might have a class of base registers, and a class of
58 * aligned register pairs that would each interfere with their pair of
59 * the base registers. Each node has a register class it needs to be
60 * assigned to. Define p(B) to be the size of register class B, and
61 * q(B,C) to be the number of registers in B that the worst choice
62 * register in C could conflict with. Then, this system replaces the
63 * basic graph coloring test of "fewer edges from this node than there
64 * are registers" with "For this node of class B, the sum of q(B,C)
65 * for each neighbor node of class C is less than pB".
66 *
67 * A nice feature of the pq test is that q(B,C) can be computed once
68 * up front and stored in a 2-dimensional array, so that the cost of
69 * coloring a node is constant with the number of registers. We do
70 * this during ra_set_finalize().
71 */
72
73 #include <stdbool.h>
74
75 #include "ralloc.h"
76 #include "main/imports.h"
77 #include "main/macros.h"
78 #include "util/bitset.h"
79 #include "register_allocate.h"
80
81 #define NO_REG ~0U
82
83 struct ra_reg {
84 BITSET_WORD *conflicts;
85 unsigned int *conflict_list;
86 unsigned int conflict_list_size;
87 unsigned int num_conflicts;
88 };
89
90 struct ra_regs {
91 struct ra_reg *regs;
92 unsigned int count;
93
94 struct ra_class **classes;
95 unsigned int class_count;
96
97 bool round_robin;
98 };
99
100 struct ra_class {
101 /**
102 * Bitset indicating which registers belong to this class.
103 *
104 * (If bit N is set, then register N belongs to this class.)
105 */
106 BITSET_WORD *regs;
107
108 /**
109 * p(B) in Runeson/Nyström paper.
110 *
111 * This is "how many regs are in the set."
112 */
113 unsigned int p;
114
115 /**
116 * q(B,C) (indexed by C, B is this register class) in
117 * Runeson/Nyström paper. This is "how many registers of B could
118 * the worst choice register from C conflict with".
119 */
120 unsigned int *q;
121 };
122
123 struct ra_node {
124 /** @{
125 *
126 * List of which nodes this node interferes with. This should be
127 * symmetric with the other node.
128 */
129 BITSET_WORD *adjacency;
130 unsigned int *adjacency_list;
131 unsigned int adjacency_list_size;
132 unsigned int adjacency_count;
133 /** @} */
134
135 unsigned int class;
136
137 /* Register, if assigned, or NO_REG. */
138 unsigned int reg;
139
140 /**
141 * The q total, as defined in the Runeson/Nyström paper, for all the
142 * interfering nodes not in the stack.
143 */
144 unsigned int q_total;
145
146 /* For an implementation that needs register spilling, this is the
147 * approximate cost of spilling this node.
148 */
149 float spill_cost;
150 };
151
152 struct ra_graph {
153 struct ra_regs *regs;
154 /**
155 * the variables that need register allocation.
156 */
157 struct ra_node *nodes;
158 unsigned int count; /**< count of nodes. */
159
160 unsigned int alloc; /**< count of nodes allocated. */
161
162 unsigned int *stack;
163 unsigned int stack_count;
164
165 /** Bit-set indicating, for each register, if it's in the stack */
166 BITSET_WORD *in_stack;
167
168 /** Bit-set indicating, for each register, if it pre-assigned */
169 BITSET_WORD *reg_assigned;
170
171 /** Bit-set indicating, for each register, the value of the pq test */
172 BITSET_WORD *pq_test;
173
174 /** For each BITSET_WORD, the minimum q value or ~0 if unknown */
175 unsigned int *min_q_total;
176
177 /*
178 * * For each BITSET_WORD, the node with the minimum q_total if
179 * min_q_total[i] != ~0.
180 */
181 unsigned int *min_q_node;
182
183 /**
184 * Tracks the start of the set of optimistically-colored registers in the
185 * stack.
186 */
187 unsigned int stack_optimistic_start;
188
189 unsigned int (*select_reg_callback)(struct ra_graph *g, BITSET_WORD *regs,
190 void *data);
191 void *select_reg_callback_data;
192 };
193
194 /**
195 * Creates a set of registers for the allocator.
196 *
197 * mem_ctx is a ralloc context for the allocator. The reg set may be freed
198 * using ralloc_free().
199 */
200 struct ra_regs *
201 ra_alloc_reg_set(void *mem_ctx, unsigned int count, bool need_conflict_lists)
202 {
203 unsigned int i;
204 struct ra_regs *regs;
205
206 regs = rzalloc(mem_ctx, struct ra_regs);
207 regs->count = count;
208 regs->regs = rzalloc_array(regs, struct ra_reg, count);
209
210 for (i = 0; i < count; i++) {
211 regs->regs[i].conflicts = rzalloc_array(regs->regs, BITSET_WORD,
212 BITSET_WORDS(count));
213 BITSET_SET(regs->regs[i].conflicts, i);
214
215 if (need_conflict_lists) {
216 regs->regs[i].conflict_list = ralloc_array(regs->regs,
217 unsigned int, 4);
218 regs->regs[i].conflict_list_size = 4;
219 regs->regs[i].conflict_list[0] = i;
220 } else {
221 regs->regs[i].conflict_list = NULL;
222 regs->regs[i].conflict_list_size = 0;
223 }
224 regs->regs[i].num_conflicts = 1;
225 }
226
227 return regs;
228 }
229
230 /**
231 * The register allocator by default prefers to allocate low register numbers,
232 * since it was written for hardware (gen4/5 Intel) that is limited in its
233 * multithreadedness by the number of registers used in a given shader.
234 *
235 * However, for hardware without that restriction, densely packed register
236 * allocation can put serious constraints on instruction scheduling. This
237 * function tells the allocator to rotate around the registers if possible as
238 * it allocates the nodes.
239 */
240 void
241 ra_set_allocate_round_robin(struct ra_regs *regs)
242 {
243 regs->round_robin = true;
244 }
245
246 static void
247 ra_add_conflict_list(struct ra_regs *regs, unsigned int r1, unsigned int r2)
248 {
249 struct ra_reg *reg1 = &regs->regs[r1];
250
251 if (reg1->conflict_list) {
252 if (reg1->conflict_list_size == reg1->num_conflicts) {
253 reg1->conflict_list_size *= 2;
254 reg1->conflict_list = reralloc(regs->regs, reg1->conflict_list,
255 unsigned int, reg1->conflict_list_size);
256 }
257 reg1->conflict_list[reg1->num_conflicts++] = r2;
258 }
259 BITSET_SET(reg1->conflicts, r2);
260 }
261
262 void
263 ra_add_reg_conflict(struct ra_regs *regs, unsigned int r1, unsigned int r2)
264 {
265 if (!BITSET_TEST(regs->regs[r1].conflicts, r2)) {
266 ra_add_conflict_list(regs, r1, r2);
267 ra_add_conflict_list(regs, r2, r1);
268 }
269 }
270
271 /**
272 * Adds a conflict between base_reg and reg, and also between reg and
273 * anything that base_reg conflicts with.
274 *
275 * This can simplify code for setting up multiple register classes
276 * which are aggregates of some base hardware registers, compared to
277 * explicitly using ra_add_reg_conflict.
278 */
279 void
280 ra_add_transitive_reg_conflict(struct ra_regs *regs,
281 unsigned int base_reg, unsigned int reg)
282 {
283 unsigned int i;
284
285 ra_add_reg_conflict(regs, reg, base_reg);
286
287 for (i = 0; i < regs->regs[base_reg].num_conflicts; i++) {
288 ra_add_reg_conflict(regs, reg, regs->regs[base_reg].conflict_list[i]);
289 }
290 }
291
292 /**
293 * Makes every conflict on the given register transitive. In other words,
294 * every register that conflicts with r will now conflict with every other
295 * register conflicting with r.
296 *
297 * This can simplify code for setting up multiple register classes
298 * which are aggregates of some base hardware registers, compared to
299 * explicitly using ra_add_reg_conflict.
300 */
301 void
302 ra_make_reg_conflicts_transitive(struct ra_regs *regs, unsigned int r)
303 {
304 struct ra_reg *reg = &regs->regs[r];
305 BITSET_WORD tmp;
306 int c;
307
308 BITSET_FOREACH_SET(c, tmp, reg->conflicts, regs->count) {
309 struct ra_reg *other = &regs->regs[c];
310 unsigned i;
311 for (i = 0; i < BITSET_WORDS(regs->count); i++)
312 other->conflicts[i] |= reg->conflicts[i];
313 }
314 }
315
316 unsigned int
317 ra_alloc_reg_class(struct ra_regs *regs)
318 {
319 struct ra_class *class;
320
321 regs->classes = reralloc(regs->regs, regs->classes, struct ra_class *,
322 regs->class_count + 1);
323
324 class = rzalloc(regs, struct ra_class);
325 regs->classes[regs->class_count] = class;
326
327 class->regs = rzalloc_array(class, BITSET_WORD, BITSET_WORDS(regs->count));
328
329 return regs->class_count++;
330 }
331
332 void
333 ra_class_add_reg(struct ra_regs *regs, unsigned int c, unsigned int r)
334 {
335 struct ra_class *class = regs->classes[c];
336
337 BITSET_SET(class->regs, r);
338 class->p++;
339 }
340
341 /**
342 * Returns true if the register belongs to the given class.
343 */
344 static bool
345 reg_belongs_to_class(unsigned int r, struct ra_class *c)
346 {
347 return BITSET_TEST(c->regs, r);
348 }
349
350 /**
351 * Must be called after all conflicts and register classes have been
352 * set up and before the register set is used for allocation.
353 * To avoid costly q value computation, use the q_values paramater
354 * to pass precomputed q values to this function.
355 */
356 void
357 ra_set_finalize(struct ra_regs *regs, unsigned int **q_values)
358 {
359 unsigned int b, c;
360
361 for (b = 0; b < regs->class_count; b++) {
362 regs->classes[b]->q = ralloc_array(regs, unsigned int, regs->class_count);
363 }
364
365 if (q_values) {
366 for (b = 0; b < regs->class_count; b++) {
367 for (c = 0; c < regs->class_count; c++) {
368 regs->classes[b]->q[c] = q_values[b][c];
369 }
370 }
371 } else {
372 /* Compute, for each class B and C, how many regs of B an
373 * allocation to C could conflict with.
374 */
375 for (b = 0; b < regs->class_count; b++) {
376 for (c = 0; c < regs->class_count; c++) {
377 unsigned int rc;
378 int max_conflicts = 0;
379
380 for (rc = 0; rc < regs->count; rc++) {
381 int conflicts = 0;
382 unsigned int i;
383
384 if (!reg_belongs_to_class(rc, regs->classes[c]))
385 continue;
386
387 for (i = 0; i < regs->regs[rc].num_conflicts; i++) {
388 unsigned int rb = regs->regs[rc].conflict_list[i];
389 if (reg_belongs_to_class(rb, regs->classes[b]))
390 conflicts++;
391 }
392 max_conflicts = MAX2(max_conflicts, conflicts);
393 }
394 regs->classes[b]->q[c] = max_conflicts;
395 }
396 }
397 }
398
399 for (b = 0; b < regs->count; b++) {
400 ralloc_free(regs->regs[b].conflict_list);
401 regs->regs[b].conflict_list = NULL;
402 }
403 }
404
405 static void
406 ra_add_node_adjacency(struct ra_graph *g, unsigned int n1, unsigned int n2)
407 {
408 BITSET_SET(g->nodes[n1].adjacency, n2);
409
410 assert(n1 != n2);
411
412 int n1_class = g->nodes[n1].class;
413 int n2_class = g->nodes[n2].class;
414 g->nodes[n1].q_total += g->regs->classes[n1_class]->q[n2_class];
415
416 if (g->nodes[n1].adjacency_count >=
417 g->nodes[n1].adjacency_list_size) {
418 g->nodes[n1].adjacency_list_size *= 2;
419 g->nodes[n1].adjacency_list = reralloc(g, g->nodes[n1].adjacency_list,
420 unsigned int,
421 g->nodes[n1].adjacency_list_size);
422 }
423
424 g->nodes[n1].adjacency_list[g->nodes[n1].adjacency_count] = n2;
425 g->nodes[n1].adjacency_count++;
426 }
427
428 static void
429 ra_node_remove_adjacency(struct ra_graph *g, unsigned int n1, unsigned int n2)
430 {
431 BITSET_CLEAR(g->nodes[n1].adjacency, n2);
432
433 assert(n1 != n2);
434
435 int n1_class = g->nodes[n1].class;
436 int n2_class = g->nodes[n2].class;
437 g->nodes[n1].q_total -= g->regs->classes[n1_class]->q[n2_class];
438
439 unsigned int i;
440 for (i = 0; i < g->nodes[n1].adjacency_count; i++) {
441 if (g->nodes[n1].adjacency_list[i] == n2) {
442 memmove(&g->nodes[n1].adjacency_list[i],
443 &g->nodes[n1].adjacency_list[i + 1],
444 (g->nodes[n1].adjacency_count - i - 1) *
445 sizeof(g->nodes[n1].adjacency_list[0]));
446 break;
447 }
448 }
449 assert(i < g->nodes[n1].adjacency_count);
450 g->nodes[n1].adjacency_count--;
451 }
452
453 static void
454 ra_realloc_interference_graph(struct ra_graph *g, unsigned int alloc)
455 {
456 if (alloc <= g->alloc)
457 return;
458
459 /* If we always have a whole number of BITSET_WORDs, it makes it much
460 * easier to memset the top of the growing bitsets.
461 */
462 assert(g->alloc % BITSET_WORDBITS == 0);
463 alloc = ALIGN(alloc, BITSET_WORDBITS);
464
465 g->nodes = reralloc(g, g->nodes, struct ra_node, alloc);
466
467 unsigned g_bitset_count = BITSET_WORDS(g->alloc);
468 unsigned bitset_count = BITSET_WORDS(alloc);
469 /* For nodes already in the graph, we just have to grow the adjacency set */
470 for (unsigned i = 0; i < g->alloc; i++) {
471 assert(g->nodes[i].adjacency != NULL);
472 g->nodes[i].adjacency = rerzalloc(g, g->nodes[i].adjacency, BITSET_WORD,
473 g_bitset_count, bitset_count);
474 }
475
476 /* For new nodes, we have to fully initialize them */
477 for (unsigned i = g->alloc; i < alloc; i++) {
478 memset(&g->nodes[i], 0, sizeof(g->nodes[i]));
479 g->nodes[i].adjacency = rzalloc_array(g, BITSET_WORD, bitset_count);
480 g->nodes[i].adjacency_list_size = 4;
481 g->nodes[i].adjacency_list =
482 ralloc_array(g, unsigned int, g->nodes[i].adjacency_list_size);
483 g->nodes[i].adjacency_count = 0;
484 g->nodes[i].q_total = 0;
485
486 g->nodes[i].reg = NO_REG;
487 }
488
489 g->stack = reralloc(g, g->stack, unsigned int, alloc);
490 g->in_stack = rerzalloc(g, g->in_stack, BITSET_WORD,
491 g_bitset_count, bitset_count);
492
493 g->reg_assigned = rerzalloc(g, g->reg_assigned, BITSET_WORD,
494 g_bitset_count, bitset_count);
495 g->pq_test = rerzalloc(g, g->pq_test, BITSET_WORD,
496 g_bitset_count, bitset_count);
497 g->min_q_total = rerzalloc(g, g->min_q_total, unsigned int,
498 g_bitset_count, bitset_count);
499 g->min_q_node = rerzalloc(g, g->min_q_node, unsigned int,
500 g_bitset_count, bitset_count);
501
502 g->alloc = alloc;
503 }
504
505 struct ra_graph *
506 ra_alloc_interference_graph(struct ra_regs *regs, unsigned int count)
507 {
508 struct ra_graph *g;
509
510 g = rzalloc(NULL, struct ra_graph);
511 g->regs = regs;
512 g->count = count;
513 ra_realloc_interference_graph(g, count);
514
515 return g;
516 }
517
518 void
519 ra_resize_interference_graph(struct ra_graph *g, unsigned int count)
520 {
521 g->count = count;
522 if (count > g->alloc)
523 ra_realloc_interference_graph(g, g->alloc * 2);
524 }
525
526 void ra_set_select_reg_callback(struct ra_graph *g,
527 unsigned int (*callback)(struct ra_graph *g,
528 BITSET_WORD *regs,
529 void *data),
530 void *data)
531 {
532 g->select_reg_callback = callback;
533 g->select_reg_callback_data = data;
534 }
535
536 void
537 ra_set_node_class(struct ra_graph *g,
538 unsigned int n, unsigned int class)
539 {
540 g->nodes[n].class = class;
541 }
542
543 unsigned int
544 ra_add_node(struct ra_graph *g, unsigned int class)
545 {
546 unsigned int n = g->count;
547 ra_resize_interference_graph(g, g->count + 1);
548
549 ra_set_node_class(g, n, class);
550
551 return n;
552 }
553
554 void
555 ra_add_node_interference(struct ra_graph *g,
556 unsigned int n1, unsigned int n2)
557 {
558 if (n1 != n2 && !BITSET_TEST(g->nodes[n1].adjacency, n2)) {
559 ra_add_node_adjacency(g, n1, n2);
560 ra_add_node_adjacency(g, n2, n1);
561 }
562 }
563
564 void
565 ra_reset_node_interference(struct ra_graph *g, unsigned int n)
566 {
567 for (unsigned int i = 0; i < g->nodes[n].adjacency_count; i++)
568 ra_node_remove_adjacency(g, g->nodes[n].adjacency_list[i], n);
569
570 memset(g->nodes[n].adjacency, 0,
571 BITSET_WORDS(g->count) * sizeof(BITSET_WORD));
572 g->nodes[n].adjacency_count = 0;
573 }
574
575 static void
576 update_pq_info(struct ra_graph *g, unsigned int n)
577 {
578 int i = n / BITSET_WORDBITS;
579 int n_class = g->nodes[n].class;
580 if (g->nodes[n].q_total < g->regs->classes[n_class]->p) {
581 BITSET_SET(g->pq_test, n);
582 } else if (g->min_q_total[i] != UINT_MAX) {
583 /* Only update min_q_total and min_q_node if min_q_total != UINT_MAX so
584 * that we don't update while we have stale data and accidentally mark
585 * it as non-stale. Also, in order to remain consistent with the old
586 * naive implementation of the algorithm, we do a lexicographical sort
587 * to ensure that we always choose the node with the highest node index.
588 */
589 if (g->nodes[n].q_total < g->min_q_total[i] ||
590 (g->nodes[n].q_total == g->min_q_total[i] &&
591 n > g->min_q_node[i])) {
592 g->min_q_total[i] = g->nodes[n].q_total;
593 g->min_q_node[i] = n;
594 }
595 }
596 }
597
598 static void
599 add_node_to_stack(struct ra_graph *g, unsigned int n)
600 {
601 unsigned int i;
602 int n_class = g->nodes[n].class;
603
604 assert(!BITSET_TEST(g->in_stack, n));
605
606 for (i = 0; i < g->nodes[n].adjacency_count; i++) {
607 unsigned int n2 = g->nodes[n].adjacency_list[i];
608 unsigned int n2_class = g->nodes[n2].class;
609
610 if (!BITSET_TEST(g->in_stack, n2) && !BITSET_TEST(g->reg_assigned, n2)) {
611 assert(g->nodes[n2].q_total >= g->regs->classes[n2_class]->q[n_class]);
612 g->nodes[n2].q_total -= g->regs->classes[n2_class]->q[n_class];
613 update_pq_info(g, n2);
614 }
615 }
616
617 g->stack[g->stack_count] = n;
618 g->stack_count++;
619 BITSET_SET(g->in_stack, n);
620
621 /* Flag the min_q_total for n's block as dirty so it gets recalculated */
622 g->min_q_total[n / BITSET_WORDBITS] = UINT_MAX;
623 }
624
625 /**
626 * Simplifies the interference graph by pushing all
627 * trivially-colorable nodes into a stack of nodes to be colored,
628 * removing them from the graph, and rinsing and repeating.
629 *
630 * If we encounter a case where we can't push any nodes on the stack, then
631 * we optimistically choose a node and push it on the stack. We heuristically
632 * push the node with the lowest total q value, since it has the fewest
633 * neighbors and therefore is most likely to be allocated.
634 */
635 static void
636 ra_simplify(struct ra_graph *g)
637 {
638 bool progress = true;
639 unsigned int stack_optimistic_start = UINT_MAX;
640
641 /* Figure out the high bit and bit mask for the first iteration of a loop
642 * over BITSET_WORDs.
643 */
644 const unsigned int top_word_high_bit = (g->count - 1) % BITSET_WORDBITS;
645
646 /* Do a quick pre-pass to set things up */
647 for (int i = BITSET_WORDS(g->count) - 1, high_bit = top_word_high_bit;
648 i >= 0; i--, high_bit = BITSET_WORDBITS - 1) {
649 g->min_q_total[i] = UINT_MAX;
650 g->min_q_node[i] = UINT_MAX;
651 for (int j = high_bit; j >= 0; j--) {
652 unsigned int n = i * BITSET_WORDBITS + j;
653 if (g->nodes[n].reg != NO_REG)
654 g->reg_assigned[i] |= BITSET_BIT(j);
655 update_pq_info(g, n);
656 }
657 }
658
659 while (progress) {
660 unsigned int min_q_total = UINT_MAX;
661 unsigned int min_q_node = UINT_MAX;
662
663 progress = false;
664
665 for (int i = BITSET_WORDS(g->count) - 1, high_bit = top_word_high_bit;
666 i >= 0; i--, high_bit = BITSET_WORDBITS - 1) {
667 BITSET_WORD mask = ~(BITSET_WORD)0 >> (31 - high_bit);
668
669 BITSET_WORD skip = g->in_stack[i] | g->reg_assigned[i];
670 if (skip == mask)
671 continue;
672
673 BITSET_WORD pq = g->pq_test[i] & ~skip;
674 if (pq) {
675 /* In this case, we have stuff we can immediately take off the
676 * stack. This also means that we're guaranteed to make progress
677 * and we don't need to bother updating lowest_q_total because we
678 * know we're going to loop again before attempting to do anything
679 * optimistic.
680 */
681 for (int j = high_bit; j >= 0; j--) {
682 if (pq & BITSET_BIT(j)) {
683 unsigned int n = i * BITSET_WORDBITS + j;
684 assert(n < g->count);
685 add_node_to_stack(g, n);
686 /* add_node_to_stack() may update pq_test for this word so
687 * we need to update our local copy.
688 */
689 pq = g->pq_test[i] & ~skip;
690 progress = true;
691 }
692 }
693 } else if (!progress) {
694 if (g->min_q_total[i] == UINT_MAX) {
695 /* The min_q_total and min_q_node are dirty because we added
696 * one of these nodes to the stack. It needs to be
697 * recalculated.
698 */
699 for (int j = high_bit; j >= 0; j--) {
700 if (skip & BITSET_BIT(j))
701 continue;
702
703 unsigned int n = i * BITSET_WORDBITS + j;
704 assert(n < g->count);
705 if (g->nodes[n].q_total < g->min_q_total[i]) {
706 g->min_q_total[i] = g->nodes[n].q_total;
707 g->min_q_node[i] = n;
708 }
709 }
710 }
711 if (g->min_q_total[i] < min_q_total) {
712 min_q_node = g->min_q_node[i];
713 min_q_total = g->min_q_total[i];
714 }
715 }
716 }
717
718 if (!progress && min_q_total != UINT_MAX) {
719 if (stack_optimistic_start == UINT_MAX)
720 stack_optimistic_start = g->stack_count;
721
722 add_node_to_stack(g, min_q_node);
723 progress = true;
724 }
725 }
726
727 g->stack_optimistic_start = stack_optimistic_start;
728 }
729
730 static bool
731 ra_any_neighbors_conflict(struct ra_graph *g, unsigned int n, unsigned int r)
732 {
733 unsigned int i;
734
735 for (i = 0; i < g->nodes[n].adjacency_count; i++) {
736 unsigned int n2 = g->nodes[n].adjacency_list[i];
737
738 if (!BITSET_TEST(g->in_stack, n2) &&
739 BITSET_TEST(g->regs->regs[r].conflicts, g->nodes[n2].reg)) {
740 return true;
741 }
742 }
743
744 return false;
745 }
746
747 /* Computes a bitfield of what regs are available for a given register
748 * selection.
749 *
750 * This lets drivers implement a more complicated policy than our simple first
751 * or round robin policies (which don't require knowing the whole bitset)
752 */
753 static bool
754 ra_compute_available_regs(struct ra_graph *g, unsigned int n, BITSET_WORD *regs)
755 {
756 struct ra_class *c = g->regs->classes[g->nodes[n].class];
757
758 /* Populate with the set of regs that are in the node's class. */
759 memcpy(regs, c->regs, BITSET_WORDS(g->regs->count) * sizeof(BITSET_WORD));
760
761 /* Remove any regs that conflict with nodes that we're adjacent to and have
762 * already colored.
763 */
764 for (int i = 0; i < g->nodes[n].adjacency_count; i++) {
765 unsigned int n2 = g->nodes[n].adjacency_list[i];
766 unsigned int r = g->nodes[n2].reg;
767
768 if (!BITSET_TEST(g->in_stack, n2)) {
769 for (int j = 0; j < BITSET_WORDS(g->regs->count); j++)
770 regs[j] &= ~g->regs->regs[r].conflicts[j];
771 }
772 }
773
774 for (int i = 0; i < BITSET_WORDS(g->regs->count); i++) {
775 if (regs[i])
776 return true;
777 }
778
779 return false;
780 }
781
782 /**
783 * Pops nodes from the stack back into the graph, coloring them with
784 * registers as they go.
785 *
786 * If all nodes were trivially colorable, then this must succeed. If
787 * not (optimistic coloring), then it may return false;
788 */
789 static bool
790 ra_select(struct ra_graph *g)
791 {
792 int start_search_reg = 0;
793 BITSET_WORD *select_regs = NULL;
794
795 if (g->select_reg_callback)
796 select_regs = malloc(BITSET_WORDS(g->regs->count) * sizeof(BITSET_WORD));
797
798 while (g->stack_count != 0) {
799 unsigned int ri;
800 unsigned int r = -1;
801 int n = g->stack[g->stack_count - 1];
802 struct ra_class *c = g->regs->classes[g->nodes[n].class];
803
804 /* set this to false even if we return here so that
805 * ra_get_best_spill_node() considers this node later.
806 */
807 BITSET_CLEAR(g->in_stack, n);
808
809 if (g->select_reg_callback) {
810 if (!ra_compute_available_regs(g, n, select_regs)) {
811 free(select_regs);
812 return false;
813 }
814
815 r = g->select_reg_callback(g, select_regs, g->select_reg_callback_data);
816 } else {
817 /* Find the lowest-numbered reg which is not used by a member
818 * of the graph adjacent to us.
819 */
820 for (ri = 0; ri < g->regs->count; ri++) {
821 r = (start_search_reg + ri) % g->regs->count;
822 if (!reg_belongs_to_class(r, c))
823 continue;
824
825 if (!ra_any_neighbors_conflict(g, n, r))
826 break;
827 }
828
829 if (ri >= g->regs->count)
830 return false;
831 }
832
833 g->nodes[n].reg = r;
834 g->stack_count--;
835
836 /* Rotate the starting point except for any nodes above the lowest
837 * optimistically colorable node. The likelihood that we will succeed
838 * at allocating optimistically colorable nodes is highly dependent on
839 * the way that the previous nodes popped off the stack are laid out.
840 * The round-robin strategy increases the fragmentation of the register
841 * file and decreases the number of nearby nodes assigned to the same
842 * color, what increases the likelihood of spilling with respect to the
843 * dense packing strategy.
844 */
845 if (g->regs->round_robin &&
846 g->stack_count - 1 <= g->stack_optimistic_start)
847 start_search_reg = r + 1;
848 }
849
850 free(select_regs);
851
852 return true;
853 }
854
855 bool
856 ra_allocate(struct ra_graph *g)
857 {
858 ra_simplify(g);
859 return ra_select(g);
860 }
861
862 unsigned int
863 ra_get_node_reg(struct ra_graph *g, unsigned int n)
864 {
865 return g->nodes[n].reg;
866 }
867
868 /**
869 * Forces a node to a specific register. This can be used to avoid
870 * creating a register class containing one node when handling data
871 * that must live in a fixed location and is known to not conflict
872 * with other forced register assignment (as is common with shader
873 * input data). These nodes do not end up in the stack during
874 * ra_simplify(), and thus at ra_select() time it is as if they were
875 * the first popped off the stack and assigned their fixed locations.
876 * Nodes that use this function do not need to be assigned a register
877 * class.
878 *
879 * Must be called before ra_simplify().
880 */
881 void
882 ra_set_node_reg(struct ra_graph *g, unsigned int n, unsigned int reg)
883 {
884 g->nodes[n].reg = reg;
885 BITSET_CLEAR(g->in_stack, n);
886 }
887
888 static float
889 ra_get_spill_benefit(struct ra_graph *g, unsigned int n)
890 {
891 unsigned int j;
892 float benefit = 0;
893 int n_class = g->nodes[n].class;
894
895 /* Define the benefit of eliminating an interference between n, n2
896 * through spilling as q(C, B) / p(C). This is similar to the
897 * "count number of edges" approach of traditional graph coloring,
898 * but takes classes into account.
899 */
900 for (j = 0; j < g->nodes[n].adjacency_count; j++) {
901 unsigned int n2 = g->nodes[n].adjacency_list[j];
902 unsigned int n2_class = g->nodes[n2].class;
903 benefit += ((float)g->regs->classes[n_class]->q[n2_class] /
904 g->regs->classes[n_class]->p);
905 }
906
907 return benefit;
908 }
909
910 /**
911 * Returns a node number to be spilled according to the cost/benefit using
912 * the pq test, or -1 if there are no spillable nodes.
913 */
914 int
915 ra_get_best_spill_node(struct ra_graph *g)
916 {
917 unsigned int best_node = -1;
918 float best_benefit = 0.0;
919 unsigned int n;
920
921 /* Consider any nodes that we colored successfully or the node we failed to
922 * color for spilling. When we failed to color a node in ra_select(), we
923 * only considered these nodes, so spilling any other ones would not result
924 * in us making progress.
925 */
926 for (n = 0; n < g->count; n++) {
927 float cost = g->nodes[n].spill_cost;
928 float benefit;
929
930 if (cost <= 0.0f)
931 continue;
932
933 if (BITSET_TEST(g->in_stack, n))
934 continue;
935
936 benefit = ra_get_spill_benefit(g, n);
937
938 if (benefit / cost > best_benefit) {
939 best_benefit = benefit / cost;
940 best_node = n;
941 }
942 }
943
944 return best_node;
945 }
946
947 /**
948 * Only nodes with a spill cost set (cost != 0.0) will be considered
949 * for register spilling.
950 */
951 void
952 ra_set_node_spill_cost(struct ra_graph *g, unsigned int n, float cost)
953 {
954 g->nodes[n].spill_cost = cost;
955 }