727c0c205fe67d8e7d897cfa8d40edf4d43943f9
[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 *stack;
161 unsigned int stack_count;
162
163 /** Bit-set indicating, for each register, if it's in the stack */
164 BITSET_WORD *in_stack;
165
166 /**
167 * Tracks the start of the set of optimistically-colored registers in the
168 * stack.
169 */
170 unsigned int stack_optimistic_start;
171
172 unsigned int (*select_reg_callback)(struct ra_graph *g, BITSET_WORD *regs,
173 void *data);
174 void *select_reg_callback_data;
175 };
176
177 /**
178 * Creates a set of registers for the allocator.
179 *
180 * mem_ctx is a ralloc context for the allocator. The reg set may be freed
181 * using ralloc_free().
182 */
183 struct ra_regs *
184 ra_alloc_reg_set(void *mem_ctx, unsigned int count, bool need_conflict_lists)
185 {
186 unsigned int i;
187 struct ra_regs *regs;
188
189 regs = rzalloc(mem_ctx, struct ra_regs);
190 regs->count = count;
191 regs->regs = rzalloc_array(regs, struct ra_reg, count);
192
193 for (i = 0; i < count; i++) {
194 regs->regs[i].conflicts = rzalloc_array(regs->regs, BITSET_WORD,
195 BITSET_WORDS(count));
196 BITSET_SET(regs->regs[i].conflicts, i);
197
198 if (need_conflict_lists) {
199 regs->regs[i].conflict_list = ralloc_array(regs->regs,
200 unsigned int, 4);
201 regs->regs[i].conflict_list_size = 4;
202 regs->regs[i].conflict_list[0] = i;
203 } else {
204 regs->regs[i].conflict_list = NULL;
205 regs->regs[i].conflict_list_size = 0;
206 }
207 regs->regs[i].num_conflicts = 1;
208 }
209
210 return regs;
211 }
212
213 /**
214 * The register allocator by default prefers to allocate low register numbers,
215 * since it was written for hardware (gen4/5 Intel) that is limited in its
216 * multithreadedness by the number of registers used in a given shader.
217 *
218 * However, for hardware without that restriction, densely packed register
219 * allocation can put serious constraints on instruction scheduling. This
220 * function tells the allocator to rotate around the registers if possible as
221 * it allocates the nodes.
222 */
223 void
224 ra_set_allocate_round_robin(struct ra_regs *regs)
225 {
226 regs->round_robin = true;
227 }
228
229 static void
230 ra_add_conflict_list(struct ra_regs *regs, unsigned int r1, unsigned int r2)
231 {
232 struct ra_reg *reg1 = &regs->regs[r1];
233
234 if (reg1->conflict_list) {
235 if (reg1->conflict_list_size == reg1->num_conflicts) {
236 reg1->conflict_list_size *= 2;
237 reg1->conflict_list = reralloc(regs->regs, reg1->conflict_list,
238 unsigned int, reg1->conflict_list_size);
239 }
240 reg1->conflict_list[reg1->num_conflicts++] = r2;
241 }
242 BITSET_SET(reg1->conflicts, r2);
243 }
244
245 void
246 ra_add_reg_conflict(struct ra_regs *regs, unsigned int r1, unsigned int r2)
247 {
248 if (!BITSET_TEST(regs->regs[r1].conflicts, r2)) {
249 ra_add_conflict_list(regs, r1, r2);
250 ra_add_conflict_list(regs, r2, r1);
251 }
252 }
253
254 /**
255 * Adds a conflict between base_reg and reg, and also between reg and
256 * anything that base_reg conflicts with.
257 *
258 * This can simplify code for setting up multiple register classes
259 * which are aggregates of some base hardware registers, compared to
260 * explicitly using ra_add_reg_conflict.
261 */
262 void
263 ra_add_transitive_reg_conflict(struct ra_regs *regs,
264 unsigned int base_reg, unsigned int reg)
265 {
266 unsigned int i;
267
268 ra_add_reg_conflict(regs, reg, base_reg);
269
270 for (i = 0; i < regs->regs[base_reg].num_conflicts; i++) {
271 ra_add_reg_conflict(regs, reg, regs->regs[base_reg].conflict_list[i]);
272 }
273 }
274
275 /**
276 * Makes every conflict on the given register transitive. In other words,
277 * every register that conflicts with r will now conflict with every other
278 * register conflicting with r.
279 *
280 * This can simplify code for setting up multiple register classes
281 * which are aggregates of some base hardware registers, compared to
282 * explicitly using ra_add_reg_conflict.
283 */
284 void
285 ra_make_reg_conflicts_transitive(struct ra_regs *regs, unsigned int r)
286 {
287 struct ra_reg *reg = &regs->regs[r];
288 BITSET_WORD tmp;
289 int c;
290
291 BITSET_FOREACH_SET(c, tmp, reg->conflicts, regs->count) {
292 struct ra_reg *other = &regs->regs[c];
293 unsigned i;
294 for (i = 0; i < BITSET_WORDS(regs->count); i++)
295 other->conflicts[i] |= reg->conflicts[i];
296 }
297 }
298
299 unsigned int
300 ra_alloc_reg_class(struct ra_regs *regs)
301 {
302 struct ra_class *class;
303
304 regs->classes = reralloc(regs->regs, regs->classes, struct ra_class *,
305 regs->class_count + 1);
306
307 class = rzalloc(regs, struct ra_class);
308 regs->classes[regs->class_count] = class;
309
310 class->regs = rzalloc_array(class, BITSET_WORD, BITSET_WORDS(regs->count));
311
312 return regs->class_count++;
313 }
314
315 void
316 ra_class_add_reg(struct ra_regs *regs, unsigned int c, unsigned int r)
317 {
318 struct ra_class *class = regs->classes[c];
319
320 BITSET_SET(class->regs, r);
321 class->p++;
322 }
323
324 /**
325 * Returns true if the register belongs to the given class.
326 */
327 static bool
328 reg_belongs_to_class(unsigned int r, struct ra_class *c)
329 {
330 return BITSET_TEST(c->regs, r);
331 }
332
333 /**
334 * Must be called after all conflicts and register classes have been
335 * set up and before the register set is used for allocation.
336 * To avoid costly q value computation, use the q_values paramater
337 * to pass precomputed q values to this function.
338 */
339 void
340 ra_set_finalize(struct ra_regs *regs, unsigned int **q_values)
341 {
342 unsigned int b, c;
343
344 for (b = 0; b < regs->class_count; b++) {
345 regs->classes[b]->q = ralloc_array(regs, unsigned int, regs->class_count);
346 }
347
348 if (q_values) {
349 for (b = 0; b < regs->class_count; b++) {
350 for (c = 0; c < regs->class_count; c++) {
351 regs->classes[b]->q[c] = q_values[b][c];
352 }
353 }
354 } else {
355 /* Compute, for each class B and C, how many regs of B an
356 * allocation to C could conflict with.
357 */
358 for (b = 0; b < regs->class_count; b++) {
359 for (c = 0; c < regs->class_count; c++) {
360 unsigned int rc;
361 int max_conflicts = 0;
362
363 for (rc = 0; rc < regs->count; rc++) {
364 int conflicts = 0;
365 unsigned int i;
366
367 if (!reg_belongs_to_class(rc, regs->classes[c]))
368 continue;
369
370 for (i = 0; i < regs->regs[rc].num_conflicts; i++) {
371 unsigned int rb = regs->regs[rc].conflict_list[i];
372 if (reg_belongs_to_class(rb, regs->classes[b]))
373 conflicts++;
374 }
375 max_conflicts = MAX2(max_conflicts, conflicts);
376 }
377 regs->classes[b]->q[c] = max_conflicts;
378 }
379 }
380 }
381
382 for (b = 0; b < regs->count; b++) {
383 ralloc_free(regs->regs[b].conflict_list);
384 regs->regs[b].conflict_list = NULL;
385 }
386 }
387
388 static void
389 ra_add_node_adjacency(struct ra_graph *g, unsigned int n1, unsigned int n2)
390 {
391 BITSET_SET(g->nodes[n1].adjacency, n2);
392
393 assert(n1 != n2);
394
395 int n1_class = g->nodes[n1].class;
396 int n2_class = g->nodes[n2].class;
397 g->nodes[n1].q_total += g->regs->classes[n1_class]->q[n2_class];
398
399 if (g->nodes[n1].adjacency_count >=
400 g->nodes[n1].adjacency_list_size) {
401 g->nodes[n1].adjacency_list_size *= 2;
402 g->nodes[n1].adjacency_list = reralloc(g, g->nodes[n1].adjacency_list,
403 unsigned int,
404 g->nodes[n1].adjacency_list_size);
405 }
406
407 g->nodes[n1].adjacency_list[g->nodes[n1].adjacency_count] = n2;
408 g->nodes[n1].adjacency_count++;
409 }
410
411 struct ra_graph *
412 ra_alloc_interference_graph(struct ra_regs *regs, unsigned int count)
413 {
414 struct ra_graph *g;
415 unsigned int i;
416
417 g = rzalloc(NULL, struct ra_graph);
418 g->regs = regs;
419 g->nodes = rzalloc_array(g, struct ra_node, count);
420 g->count = count;
421
422 g->stack = rzalloc_array(g, unsigned int, count);
423
424 int bitset_count = BITSET_WORDS(count);
425 g->in_stack = rzalloc_array(g, BITSET_WORD, bitset_count);
426
427 for (i = 0; i < count; i++) {
428 g->nodes[i].adjacency = rzalloc_array(g, BITSET_WORD, bitset_count);
429
430 g->nodes[i].adjacency_list_size = 4;
431 g->nodes[i].adjacency_list =
432 ralloc_array(g, unsigned int, g->nodes[i].adjacency_list_size);
433 g->nodes[i].adjacency_count = 0;
434 g->nodes[i].q_total = 0;
435
436 g->nodes[i].reg = NO_REG;
437 }
438
439 return g;
440 }
441
442 void ra_set_select_reg_callback(struct ra_graph *g,
443 unsigned int (*callback)(struct ra_graph *g,
444 BITSET_WORD *regs,
445 void *data),
446 void *data)
447 {
448 g->select_reg_callback = callback;
449 g->select_reg_callback_data = data;
450 }
451
452 void
453 ra_set_node_class(struct ra_graph *g,
454 unsigned int n, unsigned int class)
455 {
456 g->nodes[n].class = class;
457 }
458
459 void
460 ra_add_node_interference(struct ra_graph *g,
461 unsigned int n1, unsigned int n2)
462 {
463 if (n1 != n2 && !BITSET_TEST(g->nodes[n1].adjacency, n2)) {
464 ra_add_node_adjacency(g, n1, n2);
465 ra_add_node_adjacency(g, n2, n1);
466 }
467 }
468
469 static bool
470 pq_test(struct ra_graph *g, unsigned int n)
471 {
472 int n_class = g->nodes[n].class;
473
474 return g->nodes[n].q_total < g->regs->classes[n_class]->p;
475 }
476
477 static void
478 decrement_q(struct ra_graph *g, unsigned int n)
479 {
480 unsigned int i;
481 int n_class = g->nodes[n].class;
482
483 for (i = 0; i < g->nodes[n].adjacency_count; i++) {
484 unsigned int n2 = g->nodes[n].adjacency_list[i];
485 unsigned int n2_class = g->nodes[n2].class;
486
487 if (!BITSET_TEST(g->in_stack, n2)) {
488 assert(g->nodes[n2].q_total >= g->regs->classes[n2_class]->q[n_class]);
489 g->nodes[n2].q_total -= g->regs->classes[n2_class]->q[n_class];
490 }
491 }
492 }
493
494 /**
495 * Simplifies the interference graph by pushing all
496 * trivially-colorable nodes into a stack of nodes to be colored,
497 * removing them from the graph, and rinsing and repeating.
498 *
499 * If we encounter a case where we can't push any nodes on the stack, then
500 * we optimistically choose a node and push it on the stack. We heuristically
501 * push the node with the lowest total q value, since it has the fewest
502 * neighbors and therefore is most likely to be allocated.
503 */
504 static void
505 ra_simplify(struct ra_graph *g)
506 {
507 bool progress = true;
508 unsigned int stack_optimistic_start = UINT_MAX;
509 int i;
510
511 while (progress) {
512 unsigned int best_optimistic_node = ~0;
513 unsigned int lowest_q_total = ~0;
514
515 progress = false;
516
517 for (i = g->count - 1; i >= 0; i--) {
518 if (BITSET_TEST(g->in_stack, i) || g->nodes[i].reg != NO_REG)
519 continue;
520
521 if (pq_test(g, i)) {
522 decrement_q(g, i);
523 g->stack[g->stack_count] = i;
524 g->stack_count++;
525 BITSET_SET(g->in_stack, i);
526 progress = true;
527 } else {
528 unsigned int new_q_total = g->nodes[i].q_total;
529 if (new_q_total < lowest_q_total) {
530 best_optimistic_node = i;
531 lowest_q_total = new_q_total;
532 }
533 }
534 }
535
536 if (!progress && best_optimistic_node != ~0U) {
537 if (stack_optimistic_start == UINT_MAX)
538 stack_optimistic_start = g->stack_count;
539
540 decrement_q(g, best_optimistic_node);
541 g->stack[g->stack_count] = best_optimistic_node;
542 g->stack_count++;
543 BITSET_SET(g->in_stack, best_optimistic_node);
544 progress = true;
545 }
546 }
547
548 g->stack_optimistic_start = stack_optimistic_start;
549 }
550
551 static bool
552 ra_any_neighbors_conflict(struct ra_graph *g, unsigned int n, unsigned int r)
553 {
554 unsigned int i;
555
556 for (i = 0; i < g->nodes[n].adjacency_count; i++) {
557 unsigned int n2 = g->nodes[n].adjacency_list[i];
558
559 if (!BITSET_TEST(g->in_stack, n2) &&
560 BITSET_TEST(g->regs->regs[r].conflicts, g->nodes[n2].reg)) {
561 return true;
562 }
563 }
564
565 return false;
566 }
567
568 /* Computes a bitfield of what regs are available for a given register
569 * selection.
570 *
571 * This lets drivers implement a more complicated policy than our simple first
572 * or round robin policies (which don't require knowing the whole bitset)
573 */
574 static bool
575 ra_compute_available_regs(struct ra_graph *g, unsigned int n, BITSET_WORD *regs)
576 {
577 struct ra_class *c = g->regs->classes[g->nodes[n].class];
578
579 /* Populate with the set of regs that are in the node's class. */
580 memcpy(regs, c->regs, BITSET_WORDS(g->regs->count) * sizeof(BITSET_WORD));
581
582 /* Remove any regs that conflict with nodes that we're adjacent to and have
583 * already colored.
584 */
585 for (int i = 0; i < g->nodes[n].adjacency_count; i++) {
586 unsigned int n2 = g->nodes[n].adjacency_list[i];
587 unsigned int r = g->nodes[n2].reg;
588
589 if (!BITSET_TEST(g->in_stack, n2)) {
590 for (int j = 0; j < BITSET_WORDS(g->regs->count); j++)
591 regs[j] &= ~g->regs->regs[r].conflicts[j];
592 }
593 }
594
595 for (int i = 0; i < BITSET_WORDS(g->regs->count); i++) {
596 if (regs[i])
597 return true;
598 }
599
600 return false;
601 }
602
603 /**
604 * Pops nodes from the stack back into the graph, coloring them with
605 * registers as they go.
606 *
607 * If all nodes were trivially colorable, then this must succeed. If
608 * not (optimistic coloring), then it may return false;
609 */
610 static bool
611 ra_select(struct ra_graph *g)
612 {
613 int start_search_reg = 0;
614 BITSET_WORD *select_regs = NULL;
615
616 if (g->select_reg_callback)
617 select_regs = malloc(BITSET_WORDS(g->regs->count) * sizeof(BITSET_WORD));
618
619 while (g->stack_count != 0) {
620 unsigned int ri;
621 unsigned int r = -1;
622 int n = g->stack[g->stack_count - 1];
623 struct ra_class *c = g->regs->classes[g->nodes[n].class];
624
625 /* set this to false even if we return here so that
626 * ra_get_best_spill_node() considers this node later.
627 */
628 BITSET_CLEAR(g->in_stack, n);
629
630 if (g->select_reg_callback) {
631 if (!ra_compute_available_regs(g, n, select_regs)) {
632 free(select_regs);
633 return false;
634 }
635
636 r = g->select_reg_callback(g, select_regs, g->select_reg_callback_data);
637 } else {
638 /* Find the lowest-numbered reg which is not used by a member
639 * of the graph adjacent to us.
640 */
641 for (ri = 0; ri < g->regs->count; ri++) {
642 r = (start_search_reg + ri) % g->regs->count;
643 if (!reg_belongs_to_class(r, c))
644 continue;
645
646 if (!ra_any_neighbors_conflict(g, n, r))
647 break;
648 }
649
650 if (ri >= g->regs->count)
651 return false;
652 }
653
654 g->nodes[n].reg = r;
655 g->stack_count--;
656
657 /* Rotate the starting point except for any nodes above the lowest
658 * optimistically colorable node. The likelihood that we will succeed
659 * at allocating optimistically colorable nodes is highly dependent on
660 * the way that the previous nodes popped off the stack are laid out.
661 * The round-robin strategy increases the fragmentation of the register
662 * file and decreases the number of nearby nodes assigned to the same
663 * color, what increases the likelihood of spilling with respect to the
664 * dense packing strategy.
665 */
666 if (g->regs->round_robin &&
667 g->stack_count - 1 <= g->stack_optimistic_start)
668 start_search_reg = r + 1;
669 }
670
671 free(select_regs);
672
673 return true;
674 }
675
676 bool
677 ra_allocate(struct ra_graph *g)
678 {
679 ra_simplify(g);
680 return ra_select(g);
681 }
682
683 unsigned int
684 ra_get_node_reg(struct ra_graph *g, unsigned int n)
685 {
686 return g->nodes[n].reg;
687 }
688
689 /**
690 * Forces a node to a specific register. This can be used to avoid
691 * creating a register class containing one node when handling data
692 * that must live in a fixed location and is known to not conflict
693 * with other forced register assignment (as is common with shader
694 * input data). These nodes do not end up in the stack during
695 * ra_simplify(), and thus at ra_select() time it is as if they were
696 * the first popped off the stack and assigned their fixed locations.
697 * Nodes that use this function do not need to be assigned a register
698 * class.
699 *
700 * Must be called before ra_simplify().
701 */
702 void
703 ra_set_node_reg(struct ra_graph *g, unsigned int n, unsigned int reg)
704 {
705 g->nodes[n].reg = reg;
706 BITSET_CLEAR(g->in_stack, n);
707 }
708
709 static float
710 ra_get_spill_benefit(struct ra_graph *g, unsigned int n)
711 {
712 unsigned int j;
713 float benefit = 0;
714 int n_class = g->nodes[n].class;
715
716 /* Define the benefit of eliminating an interference between n, n2
717 * through spilling as q(C, B) / p(C). This is similar to the
718 * "count number of edges" approach of traditional graph coloring,
719 * but takes classes into account.
720 */
721 for (j = 0; j < g->nodes[n].adjacency_count; j++) {
722 unsigned int n2 = g->nodes[n].adjacency_list[j];
723 unsigned int n2_class = g->nodes[n2].class;
724 benefit += ((float)g->regs->classes[n_class]->q[n2_class] /
725 g->regs->classes[n_class]->p);
726 }
727
728 return benefit;
729 }
730
731 /**
732 * Returns a node number to be spilled according to the cost/benefit using
733 * the pq test, or -1 if there are no spillable nodes.
734 */
735 int
736 ra_get_best_spill_node(struct ra_graph *g)
737 {
738 unsigned int best_node = -1;
739 float best_benefit = 0.0;
740 unsigned int n;
741
742 /* Consider any nodes that we colored successfully or the node we failed to
743 * color for spilling. When we failed to color a node in ra_select(), we
744 * only considered these nodes, so spilling any other ones would not result
745 * in us making progress.
746 */
747 for (n = 0; n < g->count; n++) {
748 float cost = g->nodes[n].spill_cost;
749 float benefit;
750
751 if (cost <= 0.0f)
752 continue;
753
754 if (BITSET_TEST(g->in_stack, n))
755 continue;
756
757 benefit = ra_get_spill_benefit(g, n);
758
759 if (benefit / cost > best_benefit) {
760 best_benefit = benefit / cost;
761 best_node = n;
762 }
763 }
764
765 return best_node;
766 }
767
768 /**
769 * Only nodes with a spill cost set (cost != 0.0) will be considered
770 * for register spilling.
771 */
772 void
773 ra_set_node_spill_cost(struct ra_graph *g, unsigned int n, float cost)
774 {
775 g->nodes[n].spill_cost = cost;
776 }