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 -1
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 char *is_live_in; /**< [i]==1 means members[i] is live-in in (all of) its cf-pred-blocks of the phi node */
36 int *colors; /**< [i] is the color to set for members[i]. [i] == NO_COLOR means dont change anything for members[i]*/
37 int size; /**< size of the max independent set of members. The set is markes by colors[i] != NO_COLOR */
38 int changes; /**< number of re-assigned nodes belonging to phi-classes */
42 static firm_dbg_module_t *dbgphi = NULL;
45 * Contains ir_nodes of phi-classes whose colors were reassigned during coalescing.
46 * So one can check not to switch them twice or more.
48 static pset *pinned_nodes = NULL;
51 * Contains ir_nodes of phi-classes whose colors may change unlimited times.
52 * These nodes are not optimizable, so there is no need to pin their color.
54 static pset *free_nodes = NULL;
57 /* TODO: ask/check if additional ir_node-space is initialized with 0000 */
58 #define belongs_to_a_phi_class(n) (get_irn_phi_info(n)->phi)
60 #define is_color_free(bl,col) (!bitset_is_set(get_ra_block_info(bl)->used_colors, col))
63 * Set the color of node @p n to @p col, but acts
66 static INLINE void set_color(ir_node *n, int col) {
67 assert(!pset_find_ptr(pinned_nodes, n));
68 set_irn_color(n, col);
69 if (belongs_to_a_phi_class(n) && !pset_find_ptr(free_nodes, n))
70 pset_insert_ptr(pinned_nodes, n);
74 * Tries to set the color of @p n to @p col.
75 * @param n The node to set the color for
76 * @param col The color to set.
77 * @param dryrun If true no colors are actually set, only testing is performed.
78 * If false colors get set and it must be sure that no conflicts will occur.
79 * @return If setting the color is impossible CHANGE_IMPOSSIBLE is returned
80 * else the number of nodes that need a (cascading) change is returned.
82 static int color_irn(ir_node *n, int col, int dryrun) {
85 DBG((dbgphi, LEVEL_2, "\t\t\t%n --> %d\n", n, col));
86 assert(is_color(col));
88 if (get_irn_color(n) == col) {
89 DBG((dbgphi, LEVEL_2, "\t\t\t Same\n"));
93 if (pset_find_ptr(pinned_nodes, n)) {
94 DBG((dbgphi, LEVEL_2, "\t\t\t Pinned\n"));
96 assert(0 && "No prior check with dryrun=1 or buggy");
97 return CHANGE_IMPOSSIBLE;
100 bl = get_nodes_block(n);
101 if (is_color_free(bl, col) && !is_live_out(bl, n)) {
102 DBG((dbgphi, LEVEL_2, "\t\t\t Free\n"));
108 /* for now, in the aldi-version return impossible */
109 DBG((dbgphi, LEVEL_2, "\t\t\t Impossible\n"));
110 return CHANGE_IMPOSSIBLE;
117 * Tries to set as much members of a phi unit as possible to color @p col.
119 static void try_colors(phi_unit_t *pu, int col, int b_size, int b_changes) {
121 struct obstack ob_changes;
122 int i, o, cand_size, total_changes, *changes;
123 ir_node **cand, **mis;
127 obstack_init(&ob_changes);
129 /* first init pessimistically, so we can just return
130 * if we see there wont be a better result */
131 for (i = 0; i < pu->count; ++i)
132 pu->colors[i] = NO_COLOR;
136 /* For all members check if color would be possible.
137 * Does not check if color is possible in combination with
138 * other members colors being set */
140 for (i = 0; i < pu->count; ++i) {
141 DBG((dbgphi, 1, "\t\t Testing %n\n", pu->members[i]));
142 int curr_changes = color_irn(pu->members[i], col, DRYRUN);
143 if (curr_changes != CHANGE_IMPOSSIBLE) {
144 DBG((dbgphi, 1, "\t\t\tAdding to cand\n"));
145 obstack_ptr_grow(&ob, pu->members[i]);
146 obstack_grow(&ob_changes, &curr_changes, sizeof(curr_changes));
148 total_changes += curr_changes;
150 DBG((dbgphi, 1, "\t\t\tImpossible\n"));
152 /* if color is not possible for the phi node then exit (phi comes first)*/
156 cand = obstack_finish(&ob);
157 changes = obstack_finish(&ob_changes);
159 /* shortcut: if cand is worse than best the mis wont be better. */
160 if (cand_size < b_size || (cand_size == b_size && total_changes >= b_changes))
163 /* now take the candidates cand and determine a max independent set
164 * with respect to edges representing live range interference */
165 /* TODO: make this 'un-greedy' */
166 DBG((dbgphi, 1, "\t\t Max indep set\n"));
167 for (i = 0; i < cand_size; ++i) {
169 for (o = 0; o < pu->size; ++o) {
170 mis = (ir_node**) obstack_base(&ob);
171 if (values_interfere(cand[i], mis[o])) {
178 DBG((dbgphi, 1, "\t\t\tAdding to mis %n\n", cand[i]));
179 obstack_ptr_grow(&ob, cand[i]);
181 pu->changes += changes[i];
184 mis = obstack_finish(&ob);
186 /* Now set the colors of all nodes in the mis to col.
187 * - Each change taken alone is conflict free.
188 * - If all members are not live-in in their cf-pred-blocks there will be no
189 * conflict applying all changes
190 * - If there is a member, which is live-in in its cf-pred-block of the phi
191 * node, it is possible that all changes together will conflict.
193 for (i = 0; i < pu->count; ++i)
194 for (o = 0; o < pu->size; ++o)
195 if (pu->members[i] == mis[o] && get_irn_color(pu->members[i]) != col)
199 obstack_free(&ob_changes, NULL);
200 obstack_free(&ob, NULL);
205 * Sets the colors of members[i] to colors[i] as far as possible.
206 * Each single change must be conflict free (checked by try_colors).
207 * In some cases not all colors can be set.
209 static void set_colors(phi_unit_t *pu) {
211 int change_is_save, live_in_occured = 0;
213 for (i = 0; i < pu->count; ++i)
214 if (pu->colors[i] != NO_COLOR) {
215 if (pu->is_live_in[i])
218 change_is_save = CHANGE_SAVE;
220 change_is_save = color_irn(pu->members[i], pu->colors[i], DRYRUN);
222 /* HINT: Dont change to == CHANGE_SAVE */
223 if (change_is_save != CHANGE_IMPOSSIBLE) {
224 DBG((dbgphi, 1, "\t\tSetting %n to %d\n", pu->members[i], pu->colors[i]));
225 color_irn(pu->members[i], pu->colors[i], PERFORM);
227 DBG((dbgphi, 1, "\t\tConflict due to a live-in: %n\n", pu->members[i]));
234 * Tries to re-allocate colors of this phi-class, to achieve a lower number of
235 * copies placed during phi destruction. Optimized version. Works only for
236 * phi-classes/phi-units with exactly 1 phi node, which is the case for approx.
239 static void coalesce_1_phi(phi_unit_t *pu) {
240 int *b_colors, b_size, b_changes, b_color;
243 /* init best search result */
244 b_colors = malloc(pu->count * sizeof(*b_colors));
245 for (i = 0; i < pu->count; ++i)
246 b_colors[i] = NO_COLOR;
251 /* find optimum of all colors */
252 for (col = MAX_COLORS-1; col >= 0; --col) {
253 DBG((dbgphi, 1, "\tTrying color %d\n", col));
254 try_colors(pu, col, b_size, b_changes);
256 /* did we find a better max ind. set? */
257 if (pu->size > b_size || (pu->size == b_size && pu->changes < b_changes)) {
259 b_changes = pu->changes;
261 memcpy(b_colors, pu->colors, pu->count * sizeof(*b_colors));
262 DBG((dbgphi, 1, "\t!! Better size: %d Changes: %d\n", b_size, b_changes));
265 /* shortcut: if all members can be colored we are content and doubt that
266 * reducing b_changes justifies all the further trying. */
267 if (b_size == pu->count)
271 /* now apply the found optimum */
272 DBG((dbgphi, 1, "\tBest color: %d Copies: %d/%d Changes: %d\n", b_color, pu->count-b_size, pu->count, b_changes));
274 pu->changes = b_changes;
275 memcpy(pu->colors, b_colors, pu->count * sizeof(*b_colors));
282 * Tries to re-allocate colors of this phi-class, to achieve a lower number of
283 * copies placed during phi destruction. General purpose version.
285 static void coalesce_n_phi(phi_unit_t *pu) {
286 DBG((dbgphi, 1, "\n"));
291 * Prepare a phi class for further processing as a phi unit.
293 static phi_unit_t *new_phi_unit(pset *pc) {
295 ir_node *n, *phi = NULL;
297 assert(pset_count(pc) <= MAX_PHI_CLS_SIZE && "Phi class too large!");
299 pu = malloc(sizeof(*pu));
301 for (n = pset_first(pc); n; n = pset_next(pc))
307 if (pu->phi_count == 1) {
308 ir_node **tmp, *phi_block;
314 /* build member set not containing phi interferers */
315 DBG((dbgphi, 1, "Phi-1 class:\n"));
316 pu->count = 1; /*for the phi*/
317 for (n = pset_first(pc); n; n = pset_next(pc)) {
320 if (!values_interfere(phi, n)) {
321 DBG((dbgphi, 1, "\tAdding to members: %n\n", n));
322 obstack_ptr_grow(&ob, n);
325 DBG((dbgphi, 1, "\tPhi interferer: %n\n", n));
326 pset_insert_ptr(free_nodes, n);
329 tmp = obstack_finish(&ob);
330 pu->members = malloc(pu->count * sizeof(*pu->members));
331 pu->members[0] = phi;
332 memcpy(&pu->members[1], tmp, (pu->count-1) * sizeof(*tmp));
334 /* init of colors array */
335 pu->colors = malloc(pu->count * sizeof(*pu->colors));
337 /* live-in analysis */
338 /* HINT: It is possible that a node occurs twice as arg of a phi,
339 * one time being live-in, and another time not being live-in.
341 pu->is_live_in = calloc(pu->count, sizeof(*pu->is_live_in));
342 phi_block = get_nodes_block(phi);
343 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
345 ir_node *arg, *block_ith_pred;
347 arg = get_irn_n(phi, i);
348 block_ith_pred = get_nodes_block(get_irn_n(phi_block, i));
350 /* find the arg in the members array */
352 for (o = 0; o < pu->count; ++o)
353 if (pu->members[o] == arg) {
360 if (is_live_in(block_ith_pred, arg)) {
361 pu->is_live_in[midx] |= 1;
362 DBG((dbgphi, 1, "\t%n is live-in in %n\n", arg, block_ith_pred));
366 obstack_free(&ob, NULL);
368 DBG((dbgphi, 1, "Phi-n class:\n"));
372 DBG((dbgphi, 1, "\n"));
379 static void free_phi_unit(phi_unit_t *pu) {
380 DBG((dbgphi, 1, "\n"));
381 if (pu->phi_count == 1) {
384 free(pu->is_live_in);
392 void be_phi_coalesce(pset *all_phi_classes) {
395 pinned_nodes = pset_new_ptr(256);
396 free_nodes = pset_new_ptr(64);
398 for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
399 phi_unit_t *pu = new_phi_unit(pc);
400 if (pu->phi_count == 1)
407 del_pset(free_nodes);
408 del_pset(pinned_nodes);
412 void be_phi_coal_init(void) {
413 dbgphi = firm_dbg_register("Phi coalescing");
414 firm_dbg_set_mask(dbgphi, DEBUG_LVL);