1002d480b16d33e006cb111d93deaa3a86776088
[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 "set.h"
10 #include "pset.h"
11 #include "bitset.h"
12 #include "debug.h"
13 #include "irouts.h"
14 #include "irdom.h"
15
16 #include "bechordal.h"
17 #include "belive.h"
18 #include "bera_t.h"
19 #include "phiclass_t.h"
20 #include "bephicoal_t.h"
21
22 #define DEBUG_LVL SET_LEVEL_3
23 #define MAX_COLORS 16
24
25 #define INITIAL_SLOTS_PINNED_GLOBAL 256
26 #define INITIAL_SLOTS_FREE_NODES    128
27 #define INITIAL_SLOTS_CHANGED_NODES 32
28
29 /* some things for readable code */
30 #define CHANGE_SAVE NULL
31 #define CHANGE_IMPOSSIBLE (ir_node *)1
32 #define CHANGE_NYI (ir_node *)2
33 #define is_conflicting_node(n) (((int)n) > 2)
34
35 /**
36  * Models conflicts between nodes. These may be life range conflicts or
37  * pinning conflicts which may occur while changing colors
38  */
39 typedef struct _conflict_t {
40         ir_node *n1, *n2;
41 } conflict_t;
42
43 /**
44  * If an irn is changed, the changes first get stored in a node_stat_t,
45  * to allow undo of changes in case of conflicts.
46  */
47 typedef struct _node_stat_t {
48         ir_node *irn;
49         int color;
50         int undo_color;
51         char status;            /**< Bit 0: pinned, Bit 1: removed */
52 } node_stat_t;
53
54 #define _set_pinned(nodestat)    nodestat->status |= 1
55 #define _set_removed(nodestat)   nodestat->status |= 2
56 #define _clear_pinned(nodestat)  nodestat->status &= 255 ^ 1
57 #define _clear_removed(nodestat) nodestat->status &= 255 ^ 2
58 #define _is_pinned(nodestat)     (nodestat->status & 1)
59 #define _is_removed(nodestat)    (nodestat->status & 2)
60
61 /**
62  * Central data structure. Contains infos needed during coalescing of the
63  * corresponding phi class.
64  */
65 typedef struct _phi_unit_t {
66         unsigned char phi_count;                        /**< the number of phi nodes in this unit */
67         /* 1 phi */
68         unsigned char node_count;                       /**< size of the nodes-array */
69         unsigned char conflict_count;           /**< size of the conflicts-array */
70         unsigned char conflict_count_org;       /**< initial size of the conflicts-array */
71         ir_node **nodes;                                        /**< [0] is the phi node. [1..node_count-1] the arguments of the phi not interfering with it */
72         conflict_t *conflicts;                          /**< pairs of conflicting ir_nodes. */
73         set *changed_nodes;                                     /**< contains node_stat_t's. */
74 } phi_unit_t;
75
76 static firm_dbg_module_t *dbgphi = NULL;
77
78 /**
79  * Contains ir_nodes of phi-classes whose colors may change unlimited times.
80  * These nodes are not optimizable, so there is no need to pin their color.
81  */
82 static pset *free_nodes = NULL;
83
84 /**
85  * Contains already optimized ir_nodes of phi-classes fully processed.
86  * So one can perform a check not to switch them twice or more.
87  */
88 static pset *pinned_global = NULL;
89
90 int set_cmp_node_stat_t(const void *x, const void *y, size_t size) {
91         return ((node_stat_t *)x)->irn != ((node_stat_t *)y)->irn;
92 }
93
94 /**
95  * Finds a node status entry of a node if existent.
96  */
97 static INLINE node_stat_t *pu_find_node(phi_unit_t *pu, ir_node *irn) {
98         node_stat_t find;
99         find.irn = irn;
100         return set_find(pu->changed_nodes, &find, sizeof(find), HASH_PTR(irn));
101 }
102
103 /**
104  * Finds a node status entry of a node if existent. Otherwise it will return
105  * an initialized new entry for this node.
106  */
107 static INLINE node_stat_t *pu_find_or_insert_node(phi_unit_t *pu, ir_node *irn) {
108         node_stat_t find;
109         find.irn = irn;
110         find.color = NO_COLOR;
111         find.undo_color = NO_COLOR;
112         find.status = 0;
113         return set_insert(pu->changed_nodes, &find, sizeof(find), HASH_PTR(irn));
114 }
115
116 /**
117  * @return The virtual color of a node, if set before, else just the real color.
118  */
119 static INLINE int pu_get_new_color(phi_unit_t *pu, ir_node *irn) {
120         node_stat_t *found = pu_find_node(pu, irn);
121         if (found)
122                 return found->color;
123         else
124                 return get_irn_color(irn);
125 }
126
127 /**
128  * Sets the virtual color of a node.
129  */
130 static INLINE void pu_set_new_color(phi_unit_t *pu, ir_node *irn, int color) {
131         node_stat_t *found = pu_find_or_insert_node(pu, irn);
132         /* TODO Think about
133          * This is only correct if no color is set >=2 times while changing
134          * a single phi-unit-member */
135         found->undo_color = found->color;
136         found->color = color;
137 }
138
139 /**
140  * Checks if a node is removed from consideration respectively building
141  * a maximum independent set.
142  */
143 static INLINE int pu_is_node_removed(phi_unit_t *pu, ir_node *irn) {
144         node_stat_t *found = pu_find_node(pu, irn);
145         if (found)
146                 return _is_removed(found);
147         else
148                 return 0;
149 }
150
151 /**
152  * Removes a node from the base set, out of which a maximum independet
153  * set gets build from.
154  */
155 static INLINE void pu_remove_node(phi_unit_t *pu, ir_node *irn) {
156         node_stat_t *found = pu_find_or_insert_node(pu, irn);
157         _set_removed(found);
158 }
159
160 /**
161  * Checks if a node is local pinned; i.e. it belongs to the same phi unit and
162  * has been optimized before the current processed one.
163  */
164 static INLINE int pu_is_node_pinned(phi_unit_t *pu, ir_node *irn) {
165         node_stat_t *found = pu_find_node(pu, irn);
166         if (found)
167                 return _is_pinned(found);
168         else
169                 return 0;
170 }
171
172 /**
173  * Local-pins a node, so optimizations of further nodes of the same phi unit
174  * can handle situations in which a color change would undo prior optimizations.
175  */
176 static INLINE void pu_pin_node(phi_unit_t *pu, ir_node *irn) {
177         node_stat_t *found = pu_find_or_insert_node(pu, irn);
178         _set_pinned(found);
179 }
180
181 /**
182  * If a local pinned conflict occurs, a new edge in the conflict graph is added.
183  * The next maximum independent set build, will regard it.
184  */
185 static INLINE void pu_add_conflict(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
186         int count = pu->conflict_count;
187
188         assert(count != 255 && "Too much conflicts. Can hold max 255 entries");
189         if ((count & 15) == 0)
190                 pu->conflicts = realloc(pu->conflicts, (count + 16)*sizeof(*pu->conflicts));
191
192         if ((int)n1 < (int)n2) {
193                 pu->conflicts[count].n1 = n1;
194                 pu->conflicts[count].n2 = n2;
195         } else {
196                 pu->conflicts[count].n1 = n2;
197                 pu->conflicts[count].n2 = n1;
198         }
199
200         pu->conflict_count++;
201 }
202
203 /**
204  * Checks if two nodes are in a conflict.
205  */
206 static INLINE int pu_are_conflicting(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
207         const ir_node *o1, *o2;
208         int i;
209
210         if ((int)n1 < (int)n2) {
211                 o1 = n1;
212                 o2 = n2;
213         } else {
214                 o1 = n2;
215                 o2 = n1;
216         }
217
218         for (i = 0; i < pu->conflict_count; ++i)
219                 if (pu->conflicts[i].n1 == o1 && pu->conflicts[i].n2 == o2)
220                         return 1;
221         return 0;
222 }
223
224 /**
225  * Determines a maximum independent set with respect to the conflict edges
226  * in pu->conflicts and the nodes beeing all non-removed nodes of pu->nodes.
227  * TODO: make this 'un-greedy'
228  * TODO: be aware that phi nodes should find their way in the set.
229  *       for 1 phi in greedy version this is no prob, cause is comes first at [0].
230  */
231 int pu_get_max_ind_set(phi_unit_t *pu, struct obstack *res) {
232         int i, o, size = 0;
233         ir_node **mis;
234
235         DBG((dbgphi, 1, "\t\t    Max indep set\n"));
236         for (i = 0; i < pu->node_count; ++i) {
237                 int intf_det = 0;
238                 if (pu_is_node_removed(pu, pu->nodes[i]))
239                         continue;
240                 mis = (ir_node**) obstack_base(res);
241                 for (o = 0; o < size; ++o)
242                         if (phi_ops_interfere(pu->nodes[i], mis[o])) {
243                                 intf_det = 1;
244                                 break;
245                         }
246
247                 if (!intf_det) {
248                         DBG((dbgphi, 1, "\t\t\tAdding to mis %n\n", pu->nodes[i]));
249                         obstack_ptr_grow(res, pu->nodes[i]);
250                         size++;
251                 }
252         }
253         return size;
254 }
255
256 /**
257  * Performs virtual re-coloring of node @p n to color @p col. Virtual colors of
258  * other nodes are changed too, as required to preserve correctness. Function is
259  * aware of local and global pinning. Recursive.
260  * @param  irn The node to set the color for
261  * @param  col The color to set.
262  * @param  trigger The irn that caused the wish to change the color of the irn
263  * @param  changed_nodes An obstack on which all ir_nodes get growed on, which are changed
264  * @return CHANGE_SAVE iff setting the color is possible, with all transiteve effects.
265  *         CHANGE_IMPOSSIBLE iff conflicts with reg-constraintsis occured.
266  *         CHANGE_NYI iff an unhandled situation occurs.
267  *         Else the first conflicting ir_node encountered is returned.
268  *
269  * ASSUMPTION: Assumes that a life range of a single value can't be spilt into
270  *                         several smaller intervals where other values can live in between.
271  */
272 static ir_node *_pu_color_irn(phi_unit_t *pu, ir_node *irn, int col, const ir_node *trigger, struct obstack *changed_nodes) {
273         ir_node *res;
274         struct obstack confl_ob;
275         ir_node **confl, *cn;
276         int i, irn_col;
277
278         obstack_init(&confl_ob);
279         irn_col = pu_get_new_color(pu, irn);
280
281         if (irn_col == col)
282                 goto ret_save;
283         if (pset_find_ptr(pinned_global, irn) || pu_is_node_pinned(pu, irn)) {
284                 DBG((dbgphi, LEVEL_2, "\t\t\t%n \t~~> %n := %d: Pinned\n", trigger, irn, col));
285                 res = irn;
286                 goto ret_confl;
287         }
288
289         /* get all nodes which would conflict with this change */
290         {
291                 struct obstack q;
292                 int in, out;
293
294                 /* setup the queue */
295                 obstack_init(&q);
296                 obstack_ptr_grow(&q, get_nodes_block(irn));
297                 in = 1;
298                 out = 0;
299
300                 /* process the queue */
301                 while (out < in) {
302                         ir_node *curr_bl, *sub_bl;
303                         int i, max;
304
305                         curr_bl = ((ir_node **)obstack_base(&q))[out++];
306
307                         /* Add to the result all nodes in the block which live in target color
308                          * and interfere with the irn */
309                         for (i = 0, max = get_irn_n_outs(curr_bl); i < max; ++i) {
310                                 ir_node *n = get_irn_out(curr_bl, i);
311                                 if (!is_allocatable_irn(n))
312                                         continue;
313                                 if (n != trigger && pu_get_new_color(pu, n) == col && phi_ops_interfere(irn, n))
314                                         obstack_ptr_grow(&confl_ob, n);
315                         }
316
317                         /* If irn lives out check i-dominated blocks where the irn lives in */
318                         /* Fill the queue */
319                         if (is_live_out(curr_bl, irn)) {
320                                 dominates_for_each(curr_bl, sub_bl)
321                                         if (is_live_in(sub_bl, irn)) {
322                                                 obstack_ptr_grow(&q, sub_bl);
323                                                 in++;
324                                         }
325                         }
326                 }
327                 obstack_free(&q, NULL);
328                 obstack_ptr_grow(&confl_ob, NULL);
329                 confl = (ir_node **) obstack_finish(&confl_ob);
330         }
331
332         /* process all nodes which would conflict with this change */
333         for (i = 0, cn = confl[0]; cn; cn = confl[++i]) {
334                 ir_node *sub_res;
335
336                 /* try to color the conflicting node cn with the color of the irn itself */
337                 DBG((dbgphi, LEVEL_3, "\t\t\t%n \t~~> %n := %d: Subcheck\n", trigger, irn, col));
338                 sub_res = _pu_color_irn(pu, cn, irn_col, irn, changed_nodes);
339                 if (sub_res != CHANGE_SAVE) {
340                         res = sub_res;
341                         goto ret_confl;
342                 }
343         }
344         /* if we arrive here all sub changes can be applied, so it's save to change this irn */
345
346 ret_save:
347         DBG((dbgphi, LEVEL_2, "\t\t\t%n \t~~> %n := %d: Save\n", trigger, irn, col));
348         obstack_free(&confl_ob, NULL);
349         pu_set_new_color(pu, irn, col);
350         obstack_ptr_grow(changed_nodes, irn);
351         return CHANGE_SAVE;
352
353 ret_confl:
354         DBG((dbgphi, LEVEL_2, "\t\t\t%n \t~~> %n := %d: Conflict\n", trigger, irn, col));
355         obstack_free(&confl_ob, NULL);
356         return res;
357 }
358
359 #define pu_color_irn(pu,irn,col,ob) _pu_color_irn(pu, irn, col, irn, ob)
360
361 /**
362  * Tries to set as much members of a phi unit as possible to color @p col.
363  * All changes taken together are guaranteed to be conflict free.
364  */
365 static int pu_try_color(phi_unit_t *pu, int col, int b_size) {
366         struct obstack ob_mis, ob_undo;
367         int i, redo, mis_size;
368         ir_node **mis;
369
370         /* first init pessimistically. Just return if we can't get a better result */
371         mis_size = 0;
372
373         obstack_init(&ob_mis);
374         obstack_init(&ob_undo);
375         redo = 1;
376         while (redo) {
377                 redo = 0;
378                 /* get a max independent set regarding current conflicts */
379                 mis_size = pu_get_max_ind_set(pu, &ob_mis);
380                 mis = obstack_finish(&ob_mis);
381
382                 /* shortcut: if mis size is worse than best, then mis won't be better. */
383                 if (mis_size < b_size)
384                         goto ret;
385
386                 /* check if its possible to set the color for all members of the maximum set*/
387                 for (i = 0; i < mis_size; ++i) {
388                         ir_node *test_node, *confl_node;
389
390                         test_node = mis[i];
391                         DBG((dbgphi, 1, "\t\t    Testing %n\n", test_node));
392                         confl_node = pu_color_irn(pu, test_node, col, &ob_undo);
393
394                         if (confl_node == CHANGE_SAVE) {
395                                 if (!pset_find_ptr(free_nodes, test_node))
396                                         pu_pin_node(pu, test_node);
397                                 obstack_free(&ob_undo, obstack_finish(&ob_undo));
398                                 continue;
399                         } else {
400                                 int i;
401                                 ir_node *undo_node, **undo_nodes;
402
403                                 obstack_ptr_grow(&ob_undo, NULL);
404                                 undo_nodes = obstack_finish(&ob_undo);
405                                 for (i = 0, undo_node = undo_nodes[0]; undo_node; undo_node = undo_nodes[++i]) {
406                                         node_stat_t *ns = pu_find_node(pu, undo_node);
407                                         ns->color = ns->undo_color;
408                                 }
409                                 obstack_free(&ob_undo, undo_nodes);
410
411                                 if (is_conflicting_node(confl_node)) {
412                                         if (pu_is_node_pinned(pu, confl_node))
413                                                 pu_add_conflict(pu, confl_node, test_node);
414                                         if (pset_find_ptr(pinned_global, confl_node))
415                                                 pu_remove_node(pu, test_node);
416                                 }
417                         }
418
419                         /* shortcut: color not possible for phi node (phi comes first) ==> exit */
420                         if (i == 0)
421                                 goto ret;
422                 }
423                 obstack_free(&ob_mis, mis);
424         }
425
426 ret:
427         obstack_free(&ob_undo, NULL);
428         obstack_free(&ob_mis, NULL);
429         return mis_size;
430 }
431
432 /**
433  * Tries to re-allocate colors of nodes in this phi unit, to achieve a lower
434  * number of copy instructions placed during phi destruction. Optimized version.
435  * Works only for phi-classes/phi-units with exactly 1 phi node, which is the
436  * case for approximately 80% of all phi units.
437  */
438 static void pu_coalesce_1_phi(phi_unit_t *pu) {
439         int size, col, b_size, b_color;
440         set *b_changes;
441
442         /* init best search result */
443         b_changes = NULL;
444         b_size = 0;
445         b_color = NO_COLOR;
446
447         /* find optimum of all colors */
448         for (col = MAX_COLORS-1; col >= 0; --col) {
449                 DBG((dbgphi, 1, "\tTrying color %d\n", col));
450                 size = pu_try_color(pu, col, b_size);
451
452                 /* did we find a better max ind. set? */
453                 if (size > b_size) {
454                         DBG((dbgphi, 1, "\t!! Better size: %d\n", size));
455                         if (b_changes)
456                                 del_set(b_changes);
457                         b_changes = pu->changed_nodes;
458                         b_size = size;
459                         b_color = col;
460                 } else {
461                         del_set(pu->changed_nodes);
462                 }
463
464                 /* reset the phi unit to original state for next color */
465                 pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
466                 pu->conflict_count = pu->conflict_count_org;
467
468                 /* shortcut: if all members can be colored we are (very) content */
469                 if (b_size == pu->node_count)
470                         break;
471         }
472
473         /* now apply the found optimum */
474         if (b_changes) {
475                 node_stat_t *ns;
476                 DBG((dbgphi, 1, "\tBest color: %d  Copies: %d/%d\n", b_color, pu->node_count-b_size, pu->node_count));
477                 for (ns = set_first(b_changes); ns; ns = set_next(b_changes))
478                         set_irn_color(ns->irn, ns->color);
479                 free(b_changes);
480         } else {
481                 DBG((dbgphi, 1, "\tBest color: none\n"));
482         }
483 }
484
485 /**
486  * Tries to re-allocate colors of nodes in this phi unit, to achieve a lower
487  * number of copy instructions placed during phi destruction.
488  * General purpose version.
489  */
490 static void pu_coalesce_n_phi(phi_unit_t *pu) {
491         DBG((dbgphi, 1, "\n"));
492         /* TODO */
493 }
494
495 /**
496  * Prepares a phi class for further processing as a phi unit.
497  * @param pc The phi class to prepare.
498  * @return A so called phi unit containing some prepared informations
499  *         needed by the following coalescing phase.
500  */
501 static phi_unit_t *new_pu(pset *pc) {
502         phi_unit_t *pu;
503         ir_node *n, *phi = NULL;
504
505         /* get the phi count of this class */
506         pu = calloc(1, sizeof(*pu));
507         for (n = pset_first(pc); n; n = pset_next(pc))
508                 if (is_Phi(n)) {
509                         phi = n;
510                         pu->phi_count++;
511                 }
512
513         if (pu->phi_count == 1) {
514                 ir_node **tmp;
515                 int i, o;
516                 struct obstack ob;
517
518                 obstack_init(&ob);
519
520                 /* build member set not containing phi interferers */
521                 DBG((dbgphi, 1, "Phi-1 class:\n"));
522                 pu->node_count = 1; /*for the phi*/
523                 for (n = pset_first(pc); n; n = pset_next(pc)) {
524                         if (is_Phi(n))
525                                 continue;
526                         if (!phi_ops_interfere(phi, n)) {
527                                 DBG((dbgphi, 1, "\tAdding to members: %n\n", n));
528                                 obstack_ptr_grow(&ob, n);
529                                 pu->node_count++;
530                         } else {
531                                 DBG((dbgphi, 1, "\tPhi interferer: %n\n", n));
532                                 pset_insert_ptr(free_nodes, n);
533                         }
534                 }
535                 tmp = obstack_finish(&ob);
536                 pu->nodes = malloc(pu->node_count * sizeof(*pu->nodes));
537                 pu->nodes[0] = phi;
538                 memcpy(&pu->nodes[1], tmp, (pu->node_count-1) * sizeof(*tmp));
539
540                 /* init conlict graph to life range interference */
541                 for (i = 0; i < pu->node_count; ++i)
542                         for (o = i+1; o < pu->node_count; ++o)
543                                 if (phi_ops_interfere(pu->nodes[i], pu->nodes[o]))
544                                         pu_add_conflict(pu, pu->nodes[i], pu->nodes[o]);
545                 pu->conflict_count_org = pu->conflict_count;
546
547                 pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
548
549                 obstack_free(&ob, NULL);
550         } else {
551                 DBG((dbgphi, 1, "Phi-n class:\n"));
552                 /* TODO */
553         }
554
555         DBG((dbgphi, 1, "\n"));
556         return pu;
557 }
558
559
560 /**
561  * Deletes a phi unit
562  */
563 static void free_pu(phi_unit_t *pu) {
564         if (pu->phi_count == 1) {
565                 free(pu->nodes);
566                 free(pu->changed_nodes);
567                 if (pu->conflicts)
568                         free(pu->conflicts);
569         } else {
570                 /* TODO */
571         }
572         free(pu);
573 }
574
575
576 void be_phi_coalesce(pset *all_phi_classes) {
577         pset *pc;
578
579         pinned_global = pset_new_ptr(INITIAL_SLOTS_PINNED_GLOBAL);
580         free_nodes = pset_new_ptr(INITIAL_SLOTS_FREE_NODES);
581
582         for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
583                 phi_unit_t *pu = new_pu(pc);
584                 if (pu->phi_count == 1)
585                         pu_coalesce_1_phi(pu);
586                 else
587                         pu_coalesce_n_phi(pu);
588                 free_pu(pu);
589         }
590
591         del_pset(free_nodes);
592         del_pset(pinned_global);
593 }
594
595
596 void be_phi_coal_init(void) {
597         dbgphi = firm_dbg_register("ir.be.phicoal");
598         firm_dbg_set_mask(dbgphi, DEBUG_LVL);
599 }