Comments, bugfix
[libfirm] / ir / be / bephicoal.c
1 /**
2  * @author Daniel Grund
3  * @date 04.01.2005
4  */
5
6 #include <stdlib.h>
7
8 #include "obst.h"
9 #include "pset.h"
10 #include "bitset.h"
11 #include "debug.h"
12
13 #include "bechordal.h"
14 #include "belive.h"
15 #include "bera_t.h"
16 #include "bephicongr_t.h"
17 #include "bephicoal_t.h"
18
19 #define DEBUG_LVL SET_LEVEL_2
20
21 #define MAX_PHI_CLS_SIZE (1<<(sizeof(unsigned char)*8)) /* possible colors added should fit into unsigned char */
22 #define MAX_COLORS 16
23 #define CHANGE_IMPOSSIBLE -1
24 #define CHANGE_SAVE 1
25
26 #define DRYRUN 1
27 #define PERFORM 0
28
29 typedef struct _phi_unit_t {
30         unsigned char count;
31         unsigned char phi_count;
32
33         /* 1 phi */
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 */
39 } phi_unit_t;
40
41
42 static firm_dbg_module_t *dbgphi = NULL;
43
44 /**
45  * Contains ir_nodes of phi-classes whose colors were reassigned during coalescing.
46  * So one can perform a check not to switch them twice or more.
47  */
48 static pset *pinned_nodes = NULL;
49
50 /**
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.
53  */
54 static pset *free_nodes = NULL;
55
56
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)
59
60 #define is_color_free(bl,col) (!bitset_is_set(get_ra_block_info(bl)->used_colors, col))
61
62 /**
63  * Sets the color of node @p n to @p col, but acts
64  * pinning aware.
65  */
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);
71 }
72
73 /**
74  * Tries to set the color of @p n to @p col. Performs recoloring of other nodes
75  * as required to preserve correctness.
76  * @param  n The node to set the color for
77  * @param  col The color to set.
78  * @param  dryrun If true no colors are actually set, only testing is performed.
79  *                If false colors get set and it must be sure that no conflicts will occur.
80  * @return If setting the color is impossible CHANGE_IMPOSSIBLE is returned
81  *         else the number of nodes that need a (cascading) change is returned.
82  */
83 static int color_irn(ir_node *n, int col, int dryrun) {
84         ir_node *bl;
85         int res = 0;
86         DBG((dbgphi, LEVEL_2, "\t\t\t%n --> %d\n", n, col));
87         assert(is_color(col));
88
89         if (get_irn_color(n) == col) {
90                 DBG((dbgphi, LEVEL_2, "\t\t\t  Same\n"));
91                 /* to insert it into pinned_nodes color it with its color :) */
92                 set_color(n, col);
93                 return 0;
94         }
95
96         if (pset_find_ptr(pinned_nodes, n)) {
97                 DBG((dbgphi, LEVEL_2, "\t\t\t  Pinned\n"));
98                 if (!dryrun)
99                         assert(0 && "No prior check with dryrun or buggy");
100                 return CHANGE_IMPOSSIBLE;
101         }
102
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"));
106                 if (!dryrun)
107                         set_color(n, col);
108                 return 1;
109         }
110
111         /* for now, in the aldi-version return impossible */
112         DBG((dbgphi, LEVEL_2, "\t\t\t  Impossible\n"));
113         return CHANGE_IMPOSSIBLE;
114
115         return res;
116 }
117
118
119 /**
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 conflivt 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: Write sth. about dom-tree influence on this.
127  */
128 static void try_colors(phi_unit_t *pu, int col, int b_size, int b_changes) {
129         struct obstack ob;
130         struct obstack ob_changes;
131         int i, o, cand_size, total_changes, *changes;
132         ir_node **cand, **mis;
133
134         cand_size = 0;
135         obstack_init(&ob);
136         obstack_init(&ob_changes);
137
138         /* first init pessimistically, so we can just return
139          * if we see there wont be a better result */
140         for (i = 0; i < pu->count; ++i)
141                 pu->colors[i] = NO_COLOR;
142         pu->size = 0;
143         pu->changes = 0;
144
145         /* For all members check if color would be possible.
146          * Does not check if color is possible in combination with
147          * other members colors being set */
148         total_changes = 0;
149         for (i = 0; i < pu->count; ++i) {
150                 DBG((dbgphi, 1, "\t\t    Testing %n\n", pu->members[i]));
151                 int curr_changes = color_irn(pu->members[i], col, DRYRUN);
152                 if (curr_changes != CHANGE_IMPOSSIBLE) {
153                         DBG((dbgphi, 1, "\t\t\tAdding to cand\n"));
154                         obstack_ptr_grow(&ob, pu->members[i]);
155                         obstack_grow(&ob_changes, &curr_changes, sizeof(curr_changes));
156                         cand_size++;
157                         total_changes += curr_changes;
158                 } else {
159                         DBG((dbgphi, 1, "\t\t\tImpossible\n"));
160                 }
161                 /* if color is not possible for the phi node then exit (phi comes first)*/
162                 if (cand_size == 0)
163                         goto ret;
164         }
165         cand = obstack_finish(&ob);
166         changes = obstack_finish(&ob_changes);
167
168         /* shortcut: if cand is worse than best then mis wont be better. */
169         if (cand_size < b_size || (cand_size == b_size && total_changes >= b_changes))
170                 goto ret;
171
172         /* now take the candidates cand and determine a max independent set
173          * with respect to edges representing live range interference */
174         /* TODO: make this 'un-greedy' */
175         DBG((dbgphi, 1, "\t\t    Max indep set\n"));
176         for (i = 0; i < cand_size; ++i) {
177                 int intf_det = 0;
178                 for (o = 0; o < pu->size; ++o) {
179                         mis = (ir_node**) obstack_base(&ob);
180                         if (values_interfere(cand[i], mis[o])) {
181                                 intf_det = 1;
182                                 break;
183                         }
184                 }
185
186                 if (!intf_det) {
187                         DBG((dbgphi, 1, "\t\t\tAdding to mis %n\n", cand[i]));
188                         obstack_ptr_grow(&ob, cand[i]);
189                         pu->size++;
190                         pu->changes += changes[i];
191                 }
192         }
193         mis = obstack_finish(&ob);
194
195         /* Now set the colors of all nodes in the mis to col. */
196         for (i = 0; i < pu->count; ++i)
197                 for (o = 0; o < pu->size; ++o)
198                         if (pu->members[i] == mis[o] && get_irn_color(pu->members[i]) != col)
199                                 pu->colors[i] = col;
200
201 ret:
202         obstack_free(&ob_changes, NULL);
203         obstack_free(&ob, NULL);
204 }
205
206
207 /**
208  * Sets the colors of members[i] to colors[i] as far as possible.
209  * Each single change must be conflict free (checked by try_colors).
210  * In some cases not all colors can be set.
211  */
212  /*TODO : BUGGY due to false reasoning with live-outs
213   *       thus fallback to save variant with testing all before setting. */
214 static void set_colors(phi_unit_t *pu) {
215         int i;
216         int change_is_save, live_in_occured = 0;
217
218         for (i = 0; i < pu->count; ++i)
219                 if (pu->colors[i] != NO_COLOR) {
220                         if (pu->is_live_in[i])
221                                 live_in_occured = 1;
222
223 //                      change_is_save = CHANGE_SAVE;
224 //                      if (live_in_occured)
225                                 change_is_save = color_irn(pu->members[i], pu->colors[i], DRYRUN);
226
227                         /* HINT: Dont change to == CHANGE_SAVE */
228                         if (change_is_save != CHANGE_IMPOSSIBLE) {
229                                 DBG((dbgphi, 1, "\t\tSetting %n to %d\n", pu->members[i], pu->colors[i]));
230                                 color_irn(pu->members[i], pu->colors[i], PERFORM);
231                         } else {
232                                 DBG((dbgphi, 1, "\t\tConflict due to a live-in: %n\n", pu->members[i]));
233                         }
234                 }
235 }
236
237
238 /**
239  * Tries to re-allocate colors of this phi-class, to achieve a lower number of
240  * copies placed during phi destruction. Optimized version. Works only for
241  * phi-classes/phi-units with exactly 1 phi node, which is the case for approx.
242  * 80% of all phi classes.
243  */
244 static void coalesce_1_phi(phi_unit_t *pu) {
245         int *b_colors, b_size, b_changes, b_color;
246         int i, col;
247
248         /* init best search result */
249         b_colors = malloc(pu->count * sizeof(*b_colors));
250         for (i = 0; i < pu->count; ++i)
251                 b_colors[i] = NO_COLOR;
252         b_size = 0;
253         b_changes = 0;
254         b_color = NO_COLOR;
255
256         /* find optimum of all colors */
257         for (col = MAX_COLORS-1; col >= 0; --col) {
258                 DBG((dbgphi, 1, "\tTrying color %d\n", col));
259                 try_colors(pu, col, b_size, b_changes);
260
261                 /* did we find a better max ind. set? */
262                 if (pu->size > b_size || (pu->size == b_size && pu->changes < b_changes)) {
263                         b_size = pu->size;
264                         b_changes = pu->changes;
265                         b_color = col;
266                         memcpy(b_colors, pu->colors, pu->count * sizeof(*b_colors));
267                         DBG((dbgphi, 1, "\t!! Better size: %d  Changes: %d\n", b_size, b_changes));
268                 }
269
270                 /* shortcut: if all members can be colored we are content and doubt that
271                  * reducing b_changes justifies all the further trying. */
272                 if (b_size == pu->count)
273                         break;
274         }
275
276         /* now apply the found optimum */
277         DBG((dbgphi, 1, "\tBest color: %d  Copies: %d/%d  Changes: %d\n", b_color, pu->count-b_size, pu->count, b_changes));
278         pu->size = b_size;
279         pu->changes = b_changes;
280         memcpy(pu->colors, b_colors, pu->count * sizeof(*b_colors));
281         set_colors(pu);
282
283         free(b_colors);
284 }
285
286 /**
287  * Tries to re-allocate colors of this phi-class, to achieve a lower number of
288  * copies placed during phi destruction. General purpose version.
289  */
290 static void coalesce_n_phi(phi_unit_t *pu) {
291         DBG((dbgphi, 1, "\n"));
292         /* TODO */
293 }
294
295 /**
296  * Prepares a phi class for further processing as a phi unit.
297  * @param pc The phi class to prepare.
298  * @return A so called phi unit containing some prepared informations
299  *         needed by the following coalescing phase.
300  */
301 static phi_unit_t *new_phi_unit(pset *pc) {
302         phi_unit_t *pu;
303         ir_node *n, *phi = NULL;
304
305         assert(pset_count(pc) <= MAX_PHI_CLS_SIZE && "Phi class too large!");
306
307         /* get the phi count of this class */
308         pu = malloc(sizeof(*pu));
309         pu->phi_count = 0;
310         for (n = pset_first(pc); n; n = pset_next(pc))
311                 if (is_Phi(n)) {
312                         phi = n;
313                         pu->phi_count++;
314                 }
315
316         if (pu->phi_count == 1) {
317                 ir_node **tmp, *phi_block;
318                 int i, max;
319                 struct obstack ob;
320
321                 obstack_init(&ob);
322
323                 /* build member set not containing phi interferers */
324                 DBG((dbgphi, 1, "Phi-1 class:\n"));
325                 pu->count = 1; /*for the phi*/
326                 for (n = pset_first(pc); n; n = pset_next(pc)) {
327                         if (is_Phi(n))
328                                 continue;
329                         if (!values_interfere(phi, n)) {
330                                 DBG((dbgphi, 1, "\tAdding to members: %n\n", n));
331                                 obstack_ptr_grow(&ob, n);
332                                 pu->count++;
333                         } else {
334                                 DBG((dbgphi, 1, "\tPhi interferer: %n\n", n));
335                                 pset_insert_ptr(free_nodes, n);
336                         }
337                 }
338                 tmp = obstack_finish(&ob);
339                 pu->members = malloc(pu->count * sizeof(*pu->members));
340                 pu->members[0] = phi;
341                 memcpy(&pu->members[1], tmp, (pu->count-1) * sizeof(*tmp));
342
343                 /* init of colors array */
344                 pu->colors = malloc(pu->count * sizeof(*pu->colors));
345
346                 /* live-in analysis */
347                 /* HINT: It is possible that a node occurs twice as arg of a phi,
348                  * one time being live-in, and another time not being live-in.
349                  */
350                 pu->is_live_in = calloc(pu->count, sizeof(*pu->is_live_in));
351                 phi_block = get_nodes_block(phi);
352                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
353                         int midx, o;
354                 ir_node *arg, *block_ith_pred;
355
356                 arg = get_irn_n(phi, i);
357                         block_ith_pred = get_nodes_block(get_irn_n(phi_block, i));
358
359                         /* find the arg in the members array */
360                         midx = -1;
361                         for (o = 0; o < pu->count; ++o)
362                                 if (pu->members[o] == arg) {
363                                         midx = o;
364                                         break;
365                                 }
366                         if (midx == -1)
367                                 continue;
368
369                         if (is_live_in(block_ith_pred, arg)) {
370                                 pu->is_live_in[midx] |= 1;
371                                 DBG((dbgphi, 1, "\t%n is live-in in %n\n", arg, block_ith_pred));
372                         }
373                 }
374
375                 obstack_free(&ob, NULL);
376         } else {
377                 DBG((dbgphi, 1, "Phi-n class:\n"));
378                 /* TODO */
379         }
380
381         DBG((dbgphi, 1, "\n"));
382         return pu;
383 }
384
385
386 /**
387  * Deletes a phi unit
388  */
389 static void free_phi_unit(phi_unit_t *pu) {
390         DBG((dbgphi, 1, "\n"));
391         if (pu->phi_count == 1) {
392                 free(pu->members);
393                 free(pu->colors);
394                 free(pu->is_live_in);
395         } else {
396                 /* TODO */
397         }
398         free(pu);
399 }
400
401
402 void be_phi_coalesce(pset *all_phi_classes) {
403         pset *pc;
404
405         pinned_nodes = pset_new_ptr(256);
406         free_nodes = pset_new_ptr(64);
407
408         for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
409                 phi_unit_t *pu = new_phi_unit(pc);
410                 if (pu->phi_count == 1)
411                         coalesce_1_phi(pu);
412                 else
413                         coalesce_n_phi(pu);
414                 free_phi_unit(pu);
415         }
416
417         del_pset(free_nodes);
418         del_pset(pinned_nodes);
419 }
420
421
422 void be_phi_coal_init(void) {
423         dbgphi = firm_dbg_register("Phi coalescing");
424         firm_dbg_set_mask(dbgphi, DEBUG_LVL);
425 }