Comments, beautify
[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 1
20
21 #define MAX_PHI_CLS_SIZE (1<<(sizeof(unsigned char)*8)) /* possible colors added should fit into unsigned char */
22 #define MAX_COLORS 32
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         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 */
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 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  * Set 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.
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.
81  */
82 static int color_irn(ir_node *n, int col, int dryrun) {
83         ir_node *bl;
84         int res = 0;
85         DBG((dbgphi, 1, "\t\t\t\t%n  %d\n", n, col));
86         assert(is_color(col));
87
88         if (get_irn_color(n) == col) {
89                 DBG((dbgphi, 1, "\t\t\t\t\tSame\n"));
90                 return 0;
91         }
92
93         if (pset_find_ptr(pinned_nodes, n)) {
94                 DBG((dbgphi, 1, "\t\t\t\t\tPinned\n"));
95                 if (!dryrun)
96                         assert(0 && "No prior check with dryrun=1 or buggy");
97                 return CHANGE_IMPOSSIBLE;
98         }
99
100         bl = get_nodes_block(n);
101         if (is_color_free(bl, col) && !is_live_out(bl, n)) {
102                 DBG((dbgphi, 1, "\t\t\t\t\tFree\n"));
103                 if (!dryrun)
104                         set_color(n, col);
105                 return 1;
106         }
107
108         /* for now, in the aldi-version return impossible */
109         DBG((dbgphi, 1, "\t\t\t\t\tImpossible\n"));
110         return CHANGE_IMPOSSIBLE;
111
112         return res;
113 }
114
115
116 /**
117  * Tries to set as much members of a phi unit as possible to color @p col.
118  */
119 static void try_colors(phi_unit_t *pu, int col, int b_size, int b_changes) {
120         struct obstack ob;
121         int i, o, cand_size, mis_size, changes;
122         ir_node **cand, **mis;
123
124         cand_size = 0;
125         obstack_init(&ob);
126
127         /* first init pessimistically, so we can just return
128          * if we see there wont be a better result */
129         pu->size = 0;
130         for (i = 0; i < pu->count; ++i)
131                 pu->colors[i] = NO_COLOR;
132
133         /* For all members check if color would be possible.
134          * Does not check if color is possible in combination with
135          * other members colors being set */
136         changes = 0;
137         for (i = 0; i < pu->count; ++i) {
138                 int tmp_changes = color_irn(pu->members[i], col, DRYRUN);
139                 if (tmp_changes != CHANGE_IMPOSSIBLE) {
140                         DBG((dbgphi, 1, "\t\t\tAdding to cand %n\n", pu->members[i]));
141                         obstack_ptr_grow(&ob, pu->members[i]);
142                         cand_size++;
143                         changes += tmp_changes;
144                 }
145                 /* if color is not possible for the phi node then exit (phi comes first)*/
146                 if (cand_size == 0)
147                         goto ret;
148         }
149         cand = obstack_finish(&ob);
150
151         /* shortcut: if cand is worse than best the mis wont be better. */
152         if (cand_size < b_size || (cand_size == b_size && changes >= b_changes))
153                 goto ret;
154
155         /* now take the candidates cand and determine a max independent set
156          * with respect to edges representing live range interference */
157         /* TODO: make this 'un-greedy' */
158         mis_size = 0;
159         for (i = 0; i < cand_size; ++i) {
160                 int intf_det = 0;
161                 for (o = 0; o < mis_size; ++o) {
162                         mis = (ir_node**) obstack_base(&ob);
163                         if (values_interfere(cand[i], mis[o])) {
164                                 intf_det = 1;
165                                 break;
166                         }
167                 }
168
169                 if (!intf_det) {
170                         DBG((dbgphi, 1, "\t\t\tAdding to mis %n\n", cand[i]));
171                         obstack_ptr_grow(&ob, cand[i]);
172                         mis_size++;
173                 }
174         }
175         mis = obstack_finish(&ob);
176
177         /* Now set the colors of all nodes in the mis to col.
178          * - Each change taken alone is conflict free.
179          * - If all members are not live-in in their cf-pred-blocks there will be no
180          *   conflict applying all changes
181          * - If there is a member, which is live-in in its cf-pred-block of the phi
182          *   node, it is possible that all changes together will conflict.
183          */
184         for (i = 0; i < pu->count; ++i)
185                 for (o = 0; o < mis_size; ++o)
186                         if (pu->members[i] == mis[o] && get_irn_color(pu->members[i]) != col)
187                                 pu->colors[i] = col;
188
189 ret:
190         obstack_free(&ob, NULL);
191 }
192
193
194 /**
195  * Sets the colors of members[i] to colors[i] as far as possible.
196  * Each single change must be conflict free (checked by try_colors).
197  * In some cases not all colors can be set.
198  */
199 static void set_colors(phi_unit_t *pu) {
200         int i;
201         int change_is_save, live_in_occured = 0;
202
203         for (i = 0; i < pu->count; ++i)
204                 if (pu->colors[i] != NO_COLOR) {
205                         if (pu->is_live_in[i])
206                                 live_in_occured = 1;
207
208                         change_is_save = CHANGE_SAVE;
209                         if (live_in_occured)
210                                 change_is_save = color_irn(pu->members[i], pu->colors[i], DRYRUN);
211
212                         /* HINT: Dont change to == CHANGE_SAVE */
213                         if (change_is_save != CHANGE_IMPOSSIBLE) {
214                                 DBG((dbgphi, 1, "\t\tSetting %n to %d\n", pu->members[i], pu->colors[i]));
215                                 color_irn(pu->members[i], pu->colors[i], PERFORM);
216                         } else {
217                                 DBG((dbgphi, 1, "\t\tConflict due to a live-in: %n\n", pu->members[i]));
218                         }
219                 }
220 }
221
222
223 /**
224  * Tries to re-allocate colors of this phi-class, to achieve a lower number of
225  * copies placed during phi destruction. Optimized version. Works only for
226  * phi-classes/phi-units with exactly 1 phi node, which is the case for approx.
227  * 80%.
228  */
229 static void coalesce_1_phi(phi_unit_t *pu) {
230         int *b_colors, b_size, b_changes, b_color;
231         int i, col;
232
233         /* init best search result */
234         b_colors = malloc(pu->count * sizeof(*b_colors));
235         for (i = 0; i < pu->count; ++i)
236                 b_colors[i] = NO_COLOR;
237         b_size = 0;
238         b_changes = 0;
239         b_color = NO_COLOR;
240
241         /* find optimum of all colors */
242         for (col = MAX_COLORS-1; col >= 0; --col) {
243                 DBG((dbgphi, 1, "\tTrying color %d\n", col));
244                 try_colors(pu, col, b_size, b_changes);
245
246                 /* did we find a better max ind. set? */
247                 if (pu->size > b_size || (pu->size == b_size && pu->changes < b_changes)) {
248                         b_size = pu->size;
249                         b_changes = pu->changes;
250                         b_color = col;
251                         memcpy(b_colors, pu->colors, pu->count * sizeof(*b_colors));
252                         DBG((dbgphi, 1, "\t\tBetter! Size: %d  Changes: %d\n", b_size, b_changes));
253                 }
254
255                 /* shortcut: if all members can be colored we are content and doubt that
256                  * reducing b_changes justifies all the further trying. */
257                 if (b_size == pu->count)
258                         break;
259         }
260
261         /* now apply the found optimum */
262         DBG((dbgphi, 1, "\tBest color: %d  Copies: %d/%d  Changes: %d\n", b_color, pu->count-b_size, pu->count, b_changes));
263         pu->size = b_size;
264         pu->changes = b_changes;
265         memcpy(pu->colors, b_colors, pu->count * sizeof(*b_colors));
266         set_colors(pu);
267
268         free(b_colors);
269 }
270
271 /**
272  * Tries to re-allocate colors of this phi-class, to achieve a lower number of
273  * copies placed during phi destruction. General purpose version.
274  */
275 static void coalesce_n_phi(phi_unit_t *pu) {
276         DBG((dbgphi, 1, "\n"));
277         /* TODO */
278 }
279
280 /**
281  * Prepare a phi class for further processing as a phi unit.
282  */
283 static phi_unit_t *new_phi_unit(pset *pc) {
284         phi_unit_t *pu;
285         ir_node *n, *phi = NULL;
286
287         assert(pset_count(pc) <= MAX_PHI_CLS_SIZE && "Phi class too large!");
288
289         pu = malloc(sizeof(*pu));
290         pu->phi_count = 0;
291         for (n = pset_first(pc); n; n = pset_next(pc))
292                 if (is_Phi(n)) {
293                         phi = n;
294                         pu->phi_count++;
295                 }
296
297         if (pu->phi_count == 1) {
298                 ir_node **tmp, *phi_block;
299                 int i, max;
300                 struct obstack ob;
301
302                 obstack_init(&ob);
303
304                 /* build member set not containing phi interferers */
305                 DBG((dbgphi, 1, "Phi-1 class:\n"));
306                 pu->count = 1; /*for the phi*/
307                 for (n = pset_first(pc); n; n = pset_next(pc)) {
308                         if (!is_Phi(n) && !values_interfere(phi, n)) {
309                                 DBG((dbgphi, 1, "\tAdding to members: %n\n", n));
310                                 obstack_ptr_grow(&ob, n);
311                                 pu->count++;
312                         } else {
313                                 DBG((dbgphi, 1, "\tPhi interferer: %n\n", n));
314                                 pset_insert_ptr(free_nodes, n);
315                         }
316                 }
317                 tmp = obstack_finish(&ob);
318                 pu->members = malloc(pu->count * sizeof(*pu->members));
319                 pu->members[0] = phi;
320                 memcpy(&pu->members[1], tmp, (pu->count-1) * sizeof(*tmp));
321
322                 /* init of colors array */
323                 pu->colors = malloc(pu->count * sizeof(*pu->colors));
324
325                 /* live-in analysis */
326                 /* HINT: It is possible that a node occurs twice as arg of a phi,
327                  * one time being live-in, and another time not being live-in.
328                  */
329                 pu->is_live_in = calloc(pu->count, sizeof(*pu->is_live_in));
330                 phi_block = get_nodes_block(phi);
331                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
332                         int midx, o;
333                 ir_node *arg, *block_ith_pred;
334
335                 arg = get_irn_n(phi, i);
336                         block_ith_pred = get_nodes_block(get_irn_n(phi_block, i));
337
338                         /* find the arg in the members array */
339                         midx = -1;
340                         for (o = 0; o < pu->count; ++o)
341                                 if (pu->members[o] == arg) {
342                                         midx = o;
343                                         break;
344                                 }
345                         if (midx == -1)
346                                 continue;
347
348                         if (is_live_in(block_ith_pred, arg)) {
349                                 pu->is_live_in[midx] |= 1;
350                                 DBG((dbgphi, 1, "\t%n is live-in in %n\n", arg, block_ith_pred));
351                         }
352                 }
353
354                 obstack_free(&ob, NULL);
355         } else {
356                 DBG((dbgphi, 1, "Phi-n class:\n"));
357                 /* TODO */
358         }
359
360         DBG((dbgphi, 1, "\n"));
361         return pu;
362 }
363
364 /**
365  * Deletes a phi unit
366  */
367 static void free_phi_unit(phi_unit_t *pu) {
368         DBG((dbgphi, 1, "\n"));
369         if (pu->phi_count == 1) {
370                 free(pu->members);
371                 free(pu->colors);
372                 free(pu->is_live_in);
373         } else {
374                 /* TODO */
375         }
376         free(pu);
377 }
378
379
380 void be_phi_coalesce(pset *all_phi_classes) {
381         pset *pc;
382
383         pinned_nodes = pset_new_ptr(256);
384         free_nodes = pset_new_ptr(64);
385
386         for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
387                 phi_unit_t *pu = new_phi_unit(pc);
388                 if (pu->phi_count == 1)
389                         coalesce_1_phi(pu);
390                 else
391                         coalesce_n_phi(pu);
392                 free_phi_unit(pu);
393         }
394
395         del_pset(free_nodes);
396         del_pset(pinned_nodes);
397 }
398
399
400 void be_phi_coal_init(void) {
401         dbgphi = firm_dbg_register("Phi coalescing");
402         firm_dbg_set_mask(dbgphi, DEBUG_LVL);
403 }