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