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