• 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 #ifndef REGISTER_ALLOCATE_H
29 #define REGISTER_ALLOCATE_H
30 
31 #include <stdbool.h>
32 #include "util/bitset.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 
39 struct ra_class;
40 struct ra_regs;
41 
42 struct blob;
43 struct blob_reader;
44 
45 /* @{
46  * Register set setup.
47  *
48  * This should be done once at backend initializaion, as
49  * ra_set_finalize is O(r^2*c^2).  The registers may be virtual
50  * registers, such as aligned register pairs that conflict with the
51  * two real registers from which they are composed.
52  */
53 struct ra_regs *ra_alloc_reg_set(void *mem_ctx, unsigned int count,
54                                  bool need_conflict_lists);
55 void ra_set_allocate_round_robin(struct ra_regs *regs);
56 struct ra_class *ra_alloc_reg_class(struct ra_regs *regs);
57 struct ra_class *ra_alloc_contig_reg_class(struct ra_regs *regs, int contig_len);
58 unsigned int ra_class_index(struct ra_class *c);
59 void ra_add_reg_conflict(struct ra_regs *regs,
60                          unsigned int r1, unsigned int r2);
61 void ra_add_transitive_reg_conflict(struct ra_regs *regs,
62                                     unsigned int base_reg, unsigned int reg);
63 
64 void
65 ra_add_transitive_reg_pair_conflict(struct ra_regs *regs,
66                                     unsigned int base_reg, unsigned int reg0, unsigned int reg1);
67 
68 void ra_make_reg_conflicts_transitive(struct ra_regs *regs, unsigned int reg);
69 void ra_class_add_reg(struct ra_class *c, unsigned int reg);
70 struct ra_class *ra_get_class_from_index(struct ra_regs *regs, unsigned int c);
71 void ra_set_finalize(struct ra_regs *regs, unsigned int **conflicts);
72 
73 void ra_set_serialize(const struct ra_regs *regs, struct blob *blob);
74 struct ra_regs *ra_set_deserialize(void *mem_ctx, struct blob_reader *blob);
75 /** @} */
76 
77 /** @{ Interference graph setup.
78  *
79  * Each interference graph node is a virtual variable in the IL.  It
80  * is up to the user to ra_set_node_class() for the virtual variable,
81  * and compute live ranges and ra_node_interfere() between conflicting
82  * live ranges. Note that an interference *must not* be added between
83  * two nodes if their classes haven't been assigned yet. The user
84  * should set the class of each node before building the interference
85  * graph.
86  */
87 struct ra_graph *ra_alloc_interference_graph(struct ra_regs *regs,
88                                              unsigned int count);
89 void ra_resize_interference_graph(struct ra_graph *g, unsigned int count);
90 void ra_set_node_class(struct ra_graph *g, unsigned int n, struct ra_class *c);
91 struct ra_class *ra_get_node_class(struct ra_graph *g, unsigned int n);
92 unsigned int ra_add_node(struct ra_graph *g, struct ra_class *c);
93 
94 /** @{ Register selection callback.
95  *
96  * The register allocator can use either one of two built-in register
97  * selection behaviors (ie. lowest-available or round-robin), or the
98  * user can implement it's own selection policy by setting an register
99  * selection callback.  The parameters to the callback are:
100  *
101  *  - n       the graph node, ie. the virtual variable to select a
102  *            register for
103  *  - regs    bitset of available registers to choose; this bitset
104  *            contains *all* registers, but registers of different
105  *            classes will not have their corresponding bit set.
106  *  - data    callback data specified in ra_set_select_reg_callback()
107  */
108 typedef unsigned int (*ra_select_reg_callback)(
109       unsigned int n,        /* virtual variable to choose a physical reg for */
110       BITSET_WORD *regs,     /* available physical regs to choose from */
111       void *data);
112 
113 void ra_set_select_reg_callback(struct ra_graph *g,
114                                 ra_select_reg_callback callback,
115                                 void *data);
116 void ra_add_node_interference(struct ra_graph *g,
117                               unsigned int n1, unsigned int n2);
118 void ra_reset_node_interference(struct ra_graph *g, unsigned int n);
119 /** @} */
120 
121 /** @{ Graph-coloring register allocation */
122 bool ra_allocate(struct ra_graph *g);
123 
124 #define NO_REG ~0U
125 /**
126  * Returns NO_REG for a node that has not (yet) been assigned.
127  */
128 unsigned int ra_get_node_reg(struct ra_graph *g, unsigned int n);
129 void ra_set_node_reg(struct ra_graph * g, unsigned int n, unsigned int reg);
130 void ra_set_node_spill_cost(struct ra_graph *g, unsigned int n, float cost);
131 int ra_get_best_spill_node(struct ra_graph *g);
132 /** @} */
133 
134 float ra_debug_get_node_spill_cost(struct ra_graph *g, unsigned int n);
135 float ra_debug_get_spill_benefit(struct ra_graph *g, unsigned int n);
136 
137 #ifdef __cplusplus
138 }  // extern "C"
139 #endif
140 
141 #endif /* REGISTER_ALLOCATE_H */
142