13 #include "bechordal.h"
16 #include "bephicongr_t.h"
17 #include "bephicoal_t.h"
19 #define DEBUG_LVL SET_LEVEL_2
21 #define MAX_PHI_CLS_SIZE (1<<(sizeof(unsigned char)*8)) /* possible colors added should fit into unsigned char */
23 #define CHANGE_IMPOSSIBLE 0
29 typedef struct _phi_unit_t {
31 unsigned char phi_count;
34 ir_node **members; /**< [0] is the phi node. [1..count-1] the arguments of the phi not interfering with it */
35 int *colors; /**< [i] is the color to set for members[i]. [i] == NO_COLOR means dont change anything for members[i]*/
36 char *is_live_in; /**< [i]==1 means members[i] is live-in in (all of) its cf-pred-blocks of the phi node */
40 static firm_dbg_module_t *dbgphi = NULL;
43 * Contains ir_nodes of phi-classes whose colors were reassigned during coalescing.
44 * So one can perform a check not to switch them twice or more.
46 static pset *pinned_nodes = NULL;
49 * Contains ir_nodes of phi-classes whose colors may change unlimited times.
50 * These nodes are not optimizable, so there is no need to pin their color.
52 static pset *free_nodes = NULL;
55 /* TODO: ask/check if additional ir_node-space is initialized with 0000 */
56 #define belongs_to_a_phi_class(n) (get_irn_phi_info(n)->phi)
58 #define is_color_free(bl,col) (!bitset_is_set(get_ra_block_info(bl)->used_colors, col))
61 * Sets the color of node @p n to @p col, but acts
64 static INLINE void set_color(ir_node *n, int col) {
65 assert(!pset_find_ptr(pinned_nodes, n));
66 set_irn_color(n, col);
67 if (belongs_to_a_phi_class(n) && !pset_find_ptr(free_nodes, n))
68 pset_insert_ptr(pinned_nodes, n);
72 * Tries to set the color of @p n to @p col. Performs recoloring of other nodes
73 * as required to preserve correctness.
74 * @param n The node to set the color for
75 * @param col The color to set.
76 * @param dryrun If true no colors are actually set, only testing is performed.
77 * If false colors get set and it must be sure that no conflicts will occur.
78 * @return If setting the color is impossible CHANGE_IMPOSSIBLE is returned
79 * else the number of nodes that need a (cascading) change is returned.
81 static int color_irn(ir_node *n, int col, int perform) {
83 int res = CHANGE_IMPOSSIBLE;
84 DBG((dbgphi, LEVEL_2, "\t\t\t%n --> %d\n", n, col));
85 assert(is_color(col));
87 if (get_irn_color(n) == col) {
88 DBG((dbgphi, LEVEL_2, "\t\t\t Same\n"));
89 /* insert it into pinned_nodes */
91 if (belongs_to_a_phi_class(n) && !pset_find_ptr(free_nodes, n))
92 pset_insert_ptr(pinned_nodes, n);
96 if (pset_find_ptr(pinned_nodes, n)) {
97 DBG((dbgphi, LEVEL_2, "\t\t\t Pinned\n"));
99 assert(0 && "No prior check with dryrun or buggy");
100 return CHANGE_IMPOSSIBLE;
103 bl = get_nodes_block(n);
104 if (is_color_free(bl, col) && !is_live_out(bl, n)) {
105 DBG((dbgphi, LEVEL_2, "\t\t\t Free\n"));
111 /* for now, in the aldi-version return impossible */
112 DBG((dbgphi, LEVEL_2, "\t\t\t Impossible\n"));
113 return CHANGE_IMPOSSIBLE;
120 * Tries to set as much members of a phi unit as possible to color @p col.
121 * - Each change taken alone is guaranteed to be conflict free.
122 * - _If_ all members are neither live-in nor live-out in their cf-pred-blocks
123 * _then_ all changes together can be applied conflict free.
124 * - _If_ there is a member, which is live-in or live-out in its cf-pred-block
125 * of the phi node, it is possible that all changes together will conflict.
126 * TODO: check above comment with swapping complete colors in mind
127 * - TODO: Write sth. about dom-tree influence on this.
129 static int try_colors(phi_unit_t *pu, int col, int b_size) {
131 int i, o, cand_size, mis_size;
132 ir_node **cand, **mis;
136 /* first init pessimistically, so we can just return
137 * if we see there wont be a better result */
139 for (i = 0; i < pu->count; ++i)
140 pu->colors[i] = NO_COLOR;
142 /* For all members check if color would be possible.
143 * Does not check if color is possible in combination with
144 * other members colors being set */
146 for (i = 0; i < pu->count; ++i) {
147 DBG((dbgphi, 1, "\t\t Testing %n\n", pu->members[i]));
148 if (color_irn(pu->members[i], col, DRYRUN) == CHANGE_SAVE) {
149 DBG((dbgphi, 1, "\t\t\tAdding to cand\n"));
150 obstack_ptr_grow(&ob, pu->members[i]);
153 DBG((dbgphi, 1, "\t\t\tImpossible\n"));
155 /* if color is not possible for the phi node then exit (phi comes first)*/
159 cand = obstack_finish(&ob);
161 /* shortcut: if cand is worse than best then mis wont be better. */
162 if (cand_size < b_size)
165 /* now take the candidates cand and determine a max independent set
166 * with respect to edges representing live range interference */
167 /* TODO: make this 'un-greedy' */
168 DBG((dbgphi, 1, "\t\t Max indep set\n"));
169 for (i = 0; i < cand_size; ++i) {
171 for (o = 0; o < mis_size; ++o) {
172 mis = (ir_node**) obstack_base(&ob);
173 if (values_interfere(cand[i], mis[o])) {
180 DBG((dbgphi, 1, "\t\t\tAdding to mis %n\n", cand[i]));
181 obstack_ptr_grow(&ob, cand[i]);
185 mis = obstack_finish(&ob);
187 /* Now set the colors of all nodes in the mis to col.
188 * HINT: Set the color of all nodes, even if one has the same color already */
189 for (i = 0; i < pu->count; ++i)
190 for (o = 0; o < mis_size; ++o)
191 if (pu->members[i] == mis[o])
195 obstack_free(&ob, NULL);
201 * Sets the colors of members[i] to colors[i] as far as possible.
202 * Each single change must be conflict free (checked by try_colors).
203 * In some cases not all colors can be set.
205 /*TODO : BUGGY due to false reasoning with live-outs
206 * thus fallback to save variant with testing all before setting. */
207 static void set_colors(phi_unit_t *pu) {
210 for (i = 0; i < pu->count; ++i)
211 if (pu->colors[i] != NO_COLOR) {
212 if (color_irn(pu->members[i], pu->colors[i], DRYRUN) == CHANGE_SAVE) {
213 DBG((dbgphi, 1, "\t\tSetting %n to %d\n", pu->members[i], pu->colors[i]));
214 color_irn(pu->members[i], pu->colors[i], PERFORM);
216 DBG((dbgphi, 1, "\t\tConflict due to sth.: %n\n", pu->members[i]));
223 * Tries to re-allocate colors of this phi-class, to achieve a lower number of
224 * copies placed during phi destruction. Optimized version. Works only for
225 * phi-classes/phi-units with exactly 1 phi node, which is the case for approx.
226 * 80% of all phi classes.
228 static void coalesce_1_phi(phi_unit_t *pu) {
229 int *b_colors, b_size, b_color;
232 /* init best search result */
233 b_colors = malloc(pu->count * sizeof(*b_colors));
236 for (i = 0; i < pu->count; ++i)
237 b_colors[i] = NO_COLOR;
239 /* find optimum of all colors */
240 for (col = MAX_COLORS-1; col >= 0; --col) {
241 DBG((dbgphi, 1, "\tTrying color %d\n", col));
242 size = try_colors(pu, col, b_size);
244 /* did we find a better max ind. set? */
248 memcpy(b_colors, pu->colors, pu->count * sizeof(*b_colors));
249 DBG((dbgphi, 1, "\t!! Better size: %d\n", b_size));
252 /* shortcut: if all members can be colored we are content */
253 if (b_size == pu->count)
256 DBG((dbgphi, 1, "\tBest color: %d Copies: %d/%d\n", b_color, pu->count-b_size, pu->count));
257 if (b_color == NO_COLOR)
260 /* now apply the found optimum */
261 memcpy(pu->colors, b_colors, pu->count * sizeof(*b_colors));
269 * Tries to re-allocate colors of this phi-class, to achieve a lower number of
270 * copies placed during phi destruction. General purpose version.
272 static void coalesce_n_phi(phi_unit_t *pu) {
273 DBG((dbgphi, 1, "\n"));
278 * Prepares a phi class for further processing as a phi unit.
279 * @param pc The phi class to prepare.
280 * @return A so called phi unit containing some prepared informations
281 * needed by the following coalescing phase.
283 static phi_unit_t *new_phi_unit(pset *pc) {
285 ir_node *n, *phi = NULL;
287 assert(pset_count(pc) <= MAX_PHI_CLS_SIZE && "Phi class too large!");
289 /* get the phi count of this class */
290 pu = malloc(sizeof(*pu));
292 for (n = pset_first(pc); n; n = pset_next(pc))
298 if (pu->phi_count == 1) {
299 ir_node **tmp, *phi_block;
305 /* build member set not containing phi interferers */
306 DBG((dbgphi, 1, "Phi-1 class:\n"));
307 pu->count = 1; /*for the phi*/
308 for (n = pset_first(pc); n; n = pset_next(pc)) {
311 if (!values_interfere(phi, n)) {
312 DBG((dbgphi, 1, "\tAdding to members: %n\n", n));
313 obstack_ptr_grow(&ob, n);
316 DBG((dbgphi, 1, "\tPhi interferer: %n\n", n));
317 pset_insert_ptr(free_nodes, n);
320 tmp = obstack_finish(&ob);
321 pu->members = malloc(pu->count * sizeof(*pu->members));
322 pu->members[0] = phi;
323 memcpy(&pu->members[1], tmp, (pu->count-1) * sizeof(*tmp));
325 /* init of colors array */
326 pu->colors = malloc(pu->count * sizeof(*pu->colors));
328 /* live-in analysis */
329 /* HINT: It is possible that a node occurs twice as arg of a phi,
330 * one time being live-in, and another time not being live-in.
332 pu->is_live_in = calloc(pu->count, sizeof(*pu->is_live_in));
333 phi_block = get_nodes_block(phi);
334 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
336 ir_node *arg, *block_ith_pred;
338 arg = get_irn_n(phi, i);
339 block_ith_pred = get_nodes_block(get_irn_n(phi_block, i));
341 /* find the arg in the members array */
343 for (o = 0; o < pu->count; ++o)
344 if (pu->members[o] == arg) {
351 if (is_live_in(block_ith_pred, arg)) {
352 pu->is_live_in[midx] |= 1;
353 DBG((dbgphi, 1, "\t%n is live-in in %n\n", arg, block_ith_pred));
357 obstack_free(&ob, NULL);
359 DBG((dbgphi, 1, "Phi-n class:\n"));
363 DBG((dbgphi, 1, "\n"));
371 static void free_phi_unit(phi_unit_t *pu) {
372 DBG((dbgphi, 1, "\n"));
373 if (pu->phi_count == 1) {
376 free(pu->is_live_in);
384 void be_phi_coalesce(pset *all_phi_classes) {
387 pinned_nodes = pset_new_ptr(256);
388 free_nodes = pset_new_ptr(64);
390 for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
391 phi_unit_t *pu = new_phi_unit(pc);
392 if (pu->phi_count == 1)
399 del_pset(free_nodes);
400 del_pset(pinned_nodes);
404 void be_phi_coal_init(void) {
405 dbgphi = firm_dbg_register("Phi coalescing");
406 firm_dbg_set_mask(dbgphi, DEBUG_LVL);