10 #include "bechordal.h"
13 #include "bephicongr_t.h"
14 #include "bephicoal_t.h"
16 static firm_dbg_module_t *dbgphi = NULL;
18 void be_phi_coal_init(void) {
19 dbgphi = firm_dbg_register("Phi coalescing");
20 firm_dbg_set_mask(dbgphi, 1);
24 static INLINE ir_node **pset_to_array(pset *theset) {
28 count = pset_count(theset);
29 res = (ir_node **) malloc(count * sizeof(ir_node*));
30 for (i = 0, p = (ir_node *)pset_first(theset); p; p = (ir_node *)pset_next(theset))
38 static INLINE pset *clone(pset *theset) {
42 res = pset_new_ptr(8);
43 for (p = pset_first(theset); p; p = pset_next(theset))
44 pset_insert_ptr(res, p);
50 static void coalesce_locals(pset *phi_class, dominfo_t *dominfo) {
51 int i, count, phi_count, arity, intf_det, phi_col, allfree;
53 ir_node *phi = NULL, *n, *m;
56 count = pset_count(phi_class);
57 pc = clone(phi_class);
58 // members = pset_to_array(phi_class);
60 /* how many phi nodes are in this class? */
61 DBG((dbgphi, 1, "Checking phi count\n"));
63 for (n = (ir_node *)pset_first(pc); n; n = (ir_node *)pset_next(pc)) {
71 DBG((dbgphi, 1, "Dropped: Too many phis\n"));
74 assert(phi_count == 1 && phi);
77 /* where is the definition of the arguments? */
78 DBG((dbgphi, 1, "Checking arg def\n"));
79 arity = get_irn_arity(phi);
80 for (i = 0; i < arity; ++i) {
81 ir_node *block_of_arg, *block_ith_pred;
82 ir_node *arg = get_irn_n(phi, i);
84 block_of_arg = get_nodes_block(arg);
85 block_ith_pred = get_nodes_block(get_irn_n(get_nodes_block(phi), i));
87 if (block_of_arg != block_ith_pred) {
88 DBG((dbgphi, 1, "Dropped: Arg-def not in pred-block\n"));
94 /* determine a greedy set of non-interfering members
95 * crucial: starting with the phi node
97 DBG((dbgphi, 1, "Building greedy non-interfering set\n"));
98 intffree = pset_new_ptr(4);
100 pset_remove_ptr(pc, phi);
101 pset_insert_ptr(intffree, phi);
103 while (m = (ir_node *)pset_first(pc), m) {
104 DBG((dbgphi, 1, "Checking %n\n", m));
106 pset_remove_ptr(pc, m);
109 for (n = (ir_node *)pset_first(intffree); n; n = (ir_node *)pset_next(intffree)) {
110 DBG((dbgphi, 1, "\t%n", n));
111 if (phi_ops_interfere(m, n)) {
112 DBG((dbgphi, 1, "\tinterf\n"));
115 DBG((dbgphi, 1, "\tclean\n"));
120 DBG((dbgphi, 1, "Added to set\n"));
121 pset_insert_ptr(intffree, m);
126 * color the non interfering set
128 DBG((dbgphi, 1, "Coloring...\n"));
129 phi_col = get_irn_color(phi);
130 DBG((dbgphi, 1, "phi-color: %d\n", phi_col));
132 /* check if phi color is free in blocks of all members */
134 for (n = (ir_node *)pset_first(intffree); n; n = (ir_node *)pset_next(intffree)) {
136 bitset_t *used_colors;
141 block = get_nodes_block(n);
143 used_colors = get_ra_block_info(block)->used_colors;
145 if (bitset_is_set(used_colors, phi_col)) {
152 for (n = (ir_node *)pset_first(intffree); n; n = (ir_node *)pset_next(intffree))
153 set_irn_color(n, phi_col);
154 printf("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK\n");
165 void be_phi_coalesce(pset *all_phi_classes, dominfo_t *dominfo) {
168 for (phi_class = (pset *)pset_first(all_phi_classes); phi_class; phi_class = (pset *)pset_next(all_phi_classes))
169 coalesce_locals(phi_class, dominfo);