bugfix, removal of unneccessary code
[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 0
24 #define CHANGE_SAVE 1
25
26 #define DRYRUN 0
27 #define PERFORM 1
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 } phi_unit_t;
38
39
40 static firm_dbg_module_t *dbgphi = NULL;
41
42 /**
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.
45  */
46 static pset *pinned_nodes = NULL;
47
48 /**
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.
51  */
52 static pset *free_nodes = NULL;
53
54
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)
57
58 #define is_color_free(bl,col) (!bitset_is_set(get_ra_block_info(bl)->used_colors, col))
59
60 /**
61  * Sets the color of node @p n to @p col, but acts
62  * pinning aware.
63  */
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);
69 }
70
71 /**
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.
80  */
81 static int color_irn(ir_node *n, int col, int perform) {
82         ir_node *bl;
83         int res = CHANGE_IMPOSSIBLE;
84         DBG((dbgphi, LEVEL_2, "\t\t\t%n --> %d\n", n, col));
85         assert(is_color(col));
86
87         if (get_irn_color(n) == col) {
88                 DBG((dbgphi, LEVEL_2, "\t\t\t  Same\n"));
89                 /* insert it into pinned_nodes */
90                 if (perform)
91                         if (belongs_to_a_phi_class(n) && !pset_find_ptr(free_nodes, n))
92                                 pset_insert_ptr(pinned_nodes, n);
93                 return CHANGE_SAVE;
94         }
95
96         if (pset_find_ptr(pinned_nodes, n)) {
97                 DBG((dbgphi, LEVEL_2, "\t\t\t  Pinned\n"));
98                 if (perform)
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 (perform)
107                         set_color(n, col);
108                 return CHANGE_SAVE;
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 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.
128  */
129 static int try_colors(phi_unit_t *pu, int col, int b_size) {
130         struct obstack ob;
131         int i, o, cand_size, mis_size;
132         ir_node **cand, **mis;
133
134         obstack_init(&ob);
135
136         /* first init pessimistically, so we can just return
137          * if we see there wont be a better result */
138         mis_size = 0;
139         for (i = 0; i < pu->count; ++i)
140                 pu->colors[i] = NO_COLOR;
141
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 */
145         cand_size = 0;
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]);
151                         cand_size++;
152                 } else {
153                         DBG((dbgphi, 1, "\t\t\tImpossible\n"));
154                 }
155                 /* if color is not possible for the phi node then exit (phi comes first)*/
156                 if (cand_size == 0)
157                         goto ret;
158         }
159         cand = obstack_finish(&ob);
160
161         /* shortcut: if cand is worse than best then mis wont be better. */
162         if (cand_size < b_size)
163                 goto ret;
164
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) {
170                 int intf_det = 0;
171                 for (o = 0; o < mis_size; ++o) {
172                         mis = (ir_node**) obstack_base(&ob);
173                         if (values_interfere(cand[i], mis[o])) {
174                                 intf_det = 1;
175                                 break;
176                         }
177                 }
178
179                 if (!intf_det) {
180                         DBG((dbgphi, 1, "\t\t\tAdding to mis %n\n", cand[i]));
181                         obstack_ptr_grow(&ob, cand[i]);
182                         mis_size++;
183                 }
184         }
185         mis = obstack_finish(&ob);
186
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])
192                                 pu->colors[i] = col;
193
194 ret:
195         obstack_free(&ob, NULL);
196         return mis_size;
197 }
198
199
200 /**
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.
204  */
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) {
208         int i;
209
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);
215                         } else {
216                                 DBG((dbgphi, 1, "\t\tConflict due to sth.: %n\n", pu->members[i]));
217                         }
218                 }
219 }
220
221
222 /**
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.
227  */
228 static void coalesce_1_phi(phi_unit_t *pu) {
229         int *b_colors, b_size, b_color;
230         int i, size, col;
231
232         /* init best search result */
233         b_colors = malloc(pu->count * sizeof(*b_colors));
234         b_size = 0;
235         b_color = NO_COLOR;
236         for (i = 0; i < pu->count; ++i)
237                 b_colors[i] = NO_COLOR;
238
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);
243
244                 /* did we find a better max ind. set? */
245                 if (size > b_size) {
246                         b_size = size;
247                         b_color = col;
248                         memcpy(b_colors, pu->colors, pu->count * sizeof(*b_colors));
249                         DBG((dbgphi, 1, "\t!! Better size: %d\n", b_size));
250                 }
251
252                 /* shortcut: if all members can be colored we are content */
253                 if (b_size == pu->count)
254                         break;
255         }
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)
258                 goto ret;
259
260         /* now apply the found optimum */
261         memcpy(pu->colors, b_colors, pu->count * sizeof(*b_colors));
262         set_colors(pu);
263
264 ret:
265         free(b_colors);
266 }
267
268 /**
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.
271  */
272 static void coalesce_n_phi(phi_unit_t *pu) {
273         DBG((dbgphi, 1, "\n"));
274         /* TODO */
275 }
276
277 /**
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.
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         /* get the phi count of this class */
290         pu = malloc(sizeof(*pu));
291         pu->phi_count = 0;
292         for (n = pset_first(pc); n; n = pset_next(pc))
293                 if (is_Phi(n)) {
294                         phi = n;
295                         pu->phi_count++;
296                 }
297
298         if (pu->phi_count == 1) {
299                 ir_node **tmp, *phi_block;
300                 int i, max;
301                 struct obstack ob;
302
303                 obstack_init(&ob);
304
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)) {
309                         if (is_Phi(n))
310                                 continue;
311                         if (!values_interfere(phi, n)) {
312                                 DBG((dbgphi, 1, "\tAdding to members: %n\n", n));
313                                 obstack_ptr_grow(&ob, n);
314                                 pu->count++;
315                         } else {
316                                 DBG((dbgphi, 1, "\tPhi interferer: %n\n", n));
317                                 pset_insert_ptr(free_nodes, n);
318                         }
319                 }
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));
324
325                 /* init of colors array */
326                 pu->colors = malloc(pu->count * sizeof(*pu->colors));
327
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.
331                  */
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) {
335                         int midx, o;
336                 ir_node *arg, *block_ith_pred;
337
338                 arg = get_irn_n(phi, i);
339                         block_ith_pred = get_nodes_block(get_irn_n(phi_block, i));
340
341                         /* find the arg in the members array */
342                         midx = -1;
343                         for (o = 0; o < pu->count; ++o)
344                                 if (pu->members[o] == arg) {
345                                         midx = o;
346                                         break;
347                                 }
348                         if (midx == -1)
349                                 continue;
350
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));
354                         }
355                 }
356
357                 obstack_free(&ob, NULL);
358         } else {
359                 DBG((dbgphi, 1, "Phi-n class:\n"));
360                 /* TODO */
361         }
362
363         DBG((dbgphi, 1, "\n"));
364         return pu;
365 }
366
367
368 /**
369  * Deletes a phi unit
370  */
371 static void free_phi_unit(phi_unit_t *pu) {
372         DBG((dbgphi, 1, "\n"));
373         if (pu->phi_count == 1) {
374                 free(pu->members);
375                 free(pu->colors);
376                 free(pu->is_live_in);
377         } else {
378                 /* TODO */
379         }
380         free(pu);
381 }
382
383
384 void be_phi_coalesce(pset *all_phi_classes) {
385         pset *pc;
386
387         pinned_nodes = pset_new_ptr(256);
388         free_nodes = pset_new_ptr(64);
389
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)
393                         coalesce_1_phi(pu);
394                 else
395                         coalesce_n_phi(pu);
396                 free_phi_unit(pu);
397         }
398
399         del_pset(free_nodes);
400         del_pset(pinned_nodes);
401 }
402
403
404 void be_phi_coal_init(void) {
405         dbgphi = firm_dbg_register("Phi coalescing");
406         firm_dbg_set_mask(dbgphi, DEBUG_LVL);
407 }