Copied register assignments from in's to out's for all phi-perms.
[libfirm] / ir / be / bessadestr.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                25.05.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  *
7  * Performs SSA-Destruction.
8  */
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "debug.h"
14 #include "set.h"
15 #include "pmap.h"
16 #include "irnode_t.h"
17 #include "ircons_t.h"
18 #include "iredges_t.h"
19 #include "irgmod.h"
20 #include "irdump.h"
21 #include "irprintf.h"
22
23 #include "be_t.h"
24 #include "beutil.h"
25 #include "bechordal_t.h"
26 #include "bearch.h"
27 #include "benode_t.h"
28 #include "besched_t.h"
29
30 static firm_dbg_module_t *dbg = NULL;
31 #define DEBUG_LVL SET_LEVEL_3
32
33
34 #define get_chordal_arch(ce) ((ce)->session_env->main_env->arch_env)
35 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn, 0)
36 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, 0, reg)
37
38 #define is_Perm(irn)            (arch_irn_classify(arch_env, irn) == arch_irn_class_perm)
39 #define get_reg_cls(irn)        (arch_get_irn_reg_class(arch_env, irn, arch_pos_make_out(0)))
40 #define is_curr_reg_class(irn)  (get_reg_cls(p) == chordal_env->cls)
41
42 static void clear_link(ir_node *irn, void *data)
43 {
44   set_irn_link(irn, NULL);
45 }
46
47 /**
48  * Build a list of phis of a block.
49  */
50 static void collect_phis(ir_node *irn, void *data)
51 {
52   be_chordal_env_t *env = data;
53   if(is_Phi(irn) &&
54       arch_irn_has_reg_class(env->session_env->main_env->arch_env,
55         irn, arch_pos_make_out(0), env->cls)) {
56
57     ir_node *bl = get_nodes_block(irn);
58     set_irn_link(irn, get_irn_link(bl));
59     set_irn_link(bl, irn);
60   }
61 }
62
63 /**
64  * Build a ring of phis for each block in the link field.
65  * @param env The chordal env.
66  */
67 static INLINE void build_phi_rings(be_chordal_env_t *env)
68 {
69   irg_walk_graph(env->session_env->irg, clear_link, collect_phis, env);
70 }
71
72 static int skip_cf_predicator(const ir_node *irn, void *data) {
73   be_chordal_env_t *chordal_env = data;
74   arch_env_t *ae = chordal_env->session_env->main_env->arch_env;
75   return arch_irn_classify(ae, irn) == arch_irn_class_branch;
76 }
77
78 static void insert_all_perms_walker(ir_node *bl, void *data)
79 {
80   be_chordal_env_t *chordal_env = data;
81   pmap *perm_map = chordal_env->data;
82   ir_graph *irg = chordal_env->session_env->irg;
83   const be_node_factory_t *fact = chordal_env->session_env->main_env->node_factory;
84
85   /* Dummy targets for the projs */
86   ir_node *dummy = new_rd_Unknown(irg, mode_T);
87
88   assert(is_Block(bl));
89
90   /* If the link flag is NULL, this block has no phis. */
91   if(get_irn_link(bl)) {
92     int i, n;
93
94     /* Look at all predecessors of the phi block */
95     for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
96       ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
97       ir_node *phi, *perm, *insert_after;
98       ir_node **in;
99       int j, n_projs = 0;
100       pmap_entry *ent;
101       pmap *arg_map = pmap_create();
102
103       assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
104
105       /*
106        * Note that all phis in the list are in the same register class
107        * by construction.
108        */
109       for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
110         ir_node *arg = get_irn_n(phi, i);
111         ir_node *proj = pmap_get(arg_map, arg);
112
113         if(!proj) {
114           proj = new_r_Proj(irg, pred_bl, dummy, get_irn_mode(arg), n_projs++);
115           pmap_insert(arg_map, arg, proj);
116         }
117
118         set_irn_n(phi, i, proj);
119       }
120
121       j = 0;
122       in = malloc(n_projs * sizeof(in[0]));
123       pmap_foreach(arg_map, ent) {
124         in[j++] = ent->key;
125         /* register allocation is copied form former arguments
126          * to the projs (new arguments)
127          */
128         DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(ent->key)->name, ent->key, ent->value));
129         set_reg(ent->value, get_reg(ent->key));
130       }
131
132       perm = new_Perm(fact, chordal_env->cls, irg, pred_bl, n_projs, in);
133       insert_after = sched_skip(sched_last(pred_bl), 0, skip_cf_predicator, chordal_env);
134       sched_add_after(insert_after, perm);
135       exchange(dummy, perm);
136
137       free(in);
138       pmap_destroy(arg_map);
139
140       /* register in perm map */
141       pmap_insert(perm_map, pred_bl, perm);
142     }
143   }
144 }
145
146 static void insert_all_perms(be_chordal_env_t *chordal_env) {
147         DBG((dbg, LEVEL_1, "Placing perms...\n"));
148         irg_block_walk_graph(chordal_env->session_env->irg, insert_all_perms_walker, NULL, chordal_env);
149 }
150
151 #define is_pinned(irn) (get_irn_link(irn))
152 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
153 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
154
155 /**
156  * Adjusts the register allocation for the phi-operands
157  * by inserting perm nodes, if necessary.
158  * @param phi The phi node to adjust operands for
159  */
160 static void adjust_phi_arguments(be_chordal_env_t *chordal_env, ir_node *phi) {
161         int i, max;
162         ir_node *arg, *phi_block, *arg_block;
163         const be_main_session_env_t *session = chordal_env->session_env;
164         const arch_register_t *phi_reg, *arg_reg;
165         const arch_register_class_t *cls;
166
167         assert(is_Phi(phi) && "Can only handle phi-destruction :)");
168         DBG((dbg, LEVEL_1, "  for %+F\n", phi));
169
170         cls = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, arch_pos_make_out(0));
171         phi_block = get_nodes_block(phi);
172         phi_reg = get_reg(phi);
173
174         /* process all arguments of the phi */
175         for(i=0, max=get_irn_arity(phi); i<max; ++i) {
176                 ir_node *perm;
177
178                 arg = get_irn_n(phi, i);
179                 assert(is_Proj(arg));
180                 arg_block = get_nodes_block(arg);
181                 arg_reg = get_reg(arg);
182                 assert(arg_reg && "Register must be set while placing perms");
183                 DBG((dbg, LEVEL_3, "%+F has register %s assigned\n", arg, arg_reg->name));
184                 perm = get_Proj_pred(arg);
185                 //TODO reenable this if classify is implemented. assert(is_Perm(perm));
186
187                 DBG((dbg, LEVEL_1, "    arg %+F has perm %+F\n", arg, perm));
188                 /* if registers don't match ...*/
189                 if (phi_reg != arg_reg) {
190                         DBG((dbg, LEVEL_1, "      regs don't match %d %d\n", phi_reg->name, arg_reg->name));
191
192                         /* First check if there is another phi in the same block
193                          * having arg at the same pos in its arg-list and the same color as arg */
194                         if (!is_pinned(arg)) {
195                                 DBG((dbg, LEVEL_1, "        arg is not pinned\n"));
196                                 ir_node *other_phi = phi;
197                                 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
198                                         if(other_phi == phi)
199                                                 continue;
200                                         assert(is_Phi(other_phi) && get_nodes_block(phi) == get_nodes_block(other_phi) && "link fields are screwed up");
201                                         if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
202                                                 DBG((dbg, LEVEL_1, "        but other phi (%+F) just pinned it\n", other_phi));
203                                                 pin_irn(arg, phi_block);
204                                         }
205                                 }
206                         }
207
208                         /* If arg is pinned, another phi set the color of arg and pinned it.
209                          * So this phi can't change the color again and a duplicate must be inserted.
210                          *
211                          * If arg interferes with phi, one can never set the same color for both
212                          * Hence, a duplicate must be inserted */
213                         if (is_pinned(arg) || nodes_interfere(chordal_env, phi, arg)) {
214                                 ir_node *dupl, *tmp;
215                                 assert(get_pinning_block(arg) == phi_block && "If arg is pinned it must be due to a phi in the same block");
216
217                                 dupl = new_Copy(session->main_env->node_factory, cls, session->irg, arg_block, arg);
218                                 set_irn_n(phi, i, dupl);
219                                 set_reg(dupl, phi_reg);
220                                 DBG((dbg, LEVEL_1, "      arg is pinned so insert dupl %+F\n", dupl));
221
222                                 /* Add dupl to schedule */
223                                 tmp = sched_next(perm);
224                                 while (is_Proj(tmp) && sched_has_next(tmp))
225                                         tmp = sched_next(tmp);
226                                 sched_add_after(tmp, dupl);
227
228                                 /* now the arg is the dupl */
229                                 arg = dupl;
230                         } else {
231                                 /* Arg is not pinned. So set its color to the color of the phi.
232                                  * If the phi color is used by another proj of this perm
233                                  * one must NOT swap the colors. Proof: Critical edges removed,
234                                  * livein(PhiBl) = liveout(ArgBl), if all phis are processed then
235                                  * every color is used exactly once.
236                                  */
237                                 DBG((dbg, LEVEL_1, "      arg is not pinned so just set register to %s\n", phi_reg->name));
238                                 set_reg(arg, phi_reg);
239                         }
240                 } else {
241                         DBG((dbg, LEVEL_1, "      regs match %d\n"));
242                 }
243
244                 /* Now the color of the arg (arg may be a dupl now) and the phi-result are equal.
245                  * Pin it, so everyone knows and it never gets changed again.
246                  * An arg never is a phi, because perms were inserted. So the link field is free */
247                 DBG((dbg, LEVEL_1, "      arg has correct color (now), so pin it\n"));
248                 pin_irn(arg, phi_block);
249         }
250 }
251
252 static void     set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
253         be_chordal_env_t *chordal_env = data;
254         ir_node *phi;
255
256         for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
257                 adjust_phi_arguments(chordal_env, phi);
258 }
259
260 static void     set_regs_or_place_dupls(be_chordal_env_t *chordal_env)
261 {
262         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
263         irg_block_walk_graph(chordal_env->session_env->irg,
264                 set_regs_or_place_dupls_walker, NULL, chordal_env);
265 }
266
267
268 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
269         pmap *perm_map = pmap_create();
270         ir_graph *irg = chordal_env->session_env->irg;
271
272         dbg = firm_dbg_register("ir.be.ssadestr");
273         firm_dbg_set_mask(dbg, DEBUG_LVL);
274
275         /* create a map for fast lookup of perms: block --> perm */
276         chordal_env->data = perm_map;
277
278         build_phi_rings(chordal_env);
279         insert_all_perms(chordal_env);
280         dump_ir_block_graph(irg, "-ssa_destr_perms_placed");
281
282         set_regs_or_place_dupls(chordal_env);
283         dump_ir_block_graph(irg, "-ssa_destr_regs_set");
284
285         pmap_destroy(perm_map);
286 }
287
288 static void ssa_destruction_check_walker(ir_node *bl, void *data)
289 {
290         be_chordal_env_t *chordal_env = data;
291         ir_node *phi;
292         int i, max;
293
294         for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
295                 const arch_register_t *phi_reg, *arg_reg;
296
297                 phi_reg = get_reg(phi);
298                 /* iterate over all args of phi */
299                 for(i=0, max=get_irn_arity(phi); i<max; ++i) {
300                         ir_node *arg = get_irn_n(phi, i);
301                         arg_reg = get_reg(arg);
302                         if(phi_reg != arg_reg) {
303                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
304                                 assert(0);
305                         }
306                         if(!is_pinned(arg)) {
307                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
308                                 assert(0);
309                         }
310                 }
311         }
312 }
313
314 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
315         irg_block_walk_graph(chordal_env->session_env->irg, ssa_destruction_check_walker, NULL, chordal_env);
316 }