adapted to new callback
[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 "irgwalk.h"
20 #include "irgmod.h"
21 #include "irdump.h"
22 #include "irprintf.h"
23
24 #include "be_t.h"
25 #include "beutil.h"
26 #include "bechordal_t.h"
27 #include "bearch.h"
28 #include "belive_t.h"
29 #include "benode_t.h"
30 #include "besched_t.h"
31 #include "benodesets.h"
32
33 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
34
35 #define get_chordal_arch(ce) ((ce)->birg->main_env->arch_env)
36 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn)
37 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, reg)
38
39 #define is_Perm(irn)            (arch_irn_class_is(arch_env, irn, perm))
40 #define get_reg_cls(irn)        (arch_get_irn_reg_class(arch_env, irn, -1))
41 #define is_curr_reg_class(irn)  (get_reg_cls(p) == chordal_env->cls)
42
43 static void clear_link(ir_node *irn, void *data) {
44         set_irn_link(irn, NULL);
45 }
46
47 /**
48  * For each block build a linked list of phis that
49  *  - are in that block
50  *  - have the current register class
51  * The list is rooted at get_irn_link(BB).
52  */
53 static void collect_phis_walker(ir_node *irn, void *data) {
54         be_chordal_env_t *env = data;
55         if(is_Phi(irn) && chordal_has_class(env, irn)) {
56                 ir_node *bl = get_nodes_block(irn);
57                 set_irn_link(irn, get_irn_link(bl));
58                 set_irn_link(bl, irn);
59         }
60 }
61
62 /**
63  * This struct represents a Proj for a Perm.
64  * It records the argument in the Perm and the corresponding Proj of the
65  * Perm.
66  */
67 typedef struct {
68         ir_node *arg;  /**< The phi argument to make the Proj for. */
69         int pos;       /**< The proj number the Proj will get.
70                                                                          This also denotes the position of @p arg
71                                                                          in the in array of the Perm. */
72         ir_node *proj; /**< The proj created for @p arg. */
73 } perm_proj_t;
74
75 static int cmp_perm_proj(const void *a, const void *b, size_t n) {
76         const perm_proj_t *p = a;
77         const perm_proj_t *q = b;
78         return !(p->arg == q->arg);
79 }
80
81 /**
82  * Insert Perms in all predecessors of a block containing a phi
83  */
84 static void insert_all_perms_walker(ir_node *bl, void *data) {
85         be_chordal_env_t *chordal_env = data;
86         pmap *perm_map = chordal_env->data;
87         ir_graph *irg = chordal_env->irg;
88         int i, n;
89
90         assert(is_Block(bl));
91
92         /* If the link flag is NULL, this block has no phis. */
93         if(!get_irn_link(bl))
94                 return;
95
96         /* Look at all predecessors of the phi block */
97         for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
98                 ir_node *phi, *perm, *insert_after, **in;
99                 perm_proj_t *pp;
100                 set *arg_set     = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
101                 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
102                 int n_projs      = 0;
103
104                 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
105
106                 /*
107                  * Note that all phis in the list are in the same
108                  * register class by construction.
109                  */
110                 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
111                         perm_proj_t templ;
112                         ir_node *arg     = get_irn_n(phi, i);
113                         unsigned hash    = nodeset_hash(arg);
114
115                         templ.arg  = arg;
116                         pp         = set_find(arg_set, &templ, sizeof(templ), hash);
117
118                         /*
119                          * If a proj_perm_t entry has not been made in the argument set,
120                          * create one. The only restriction is, that the phi argument
121                          * may not be live in at the current block, since this argument
122                          * interferes with the phi and must thus not be member of a
123                          * Perm. A copy will be inserted for this argument alter on.
124                          */
125                         if(!pp && !be_is_live_in(chordal_env->lv, bl, arg)) {
126                                 templ.pos = n_projs++;
127                                 set_insert(arg_set, &templ, sizeof(templ), hash);
128                         }
129                 }
130
131
132                 if (n_projs) {
133                         /*
134                          * Create a new Perm with the arguments just collected
135                          * above in the arg_set and insert it into the schedule.
136                          */
137                         in = xmalloc(n_projs * sizeof(in[0]));
138                         for(pp = set_first(arg_set); pp; pp = set_next(arg_set))
139                                 in[pp->pos] = pp->arg;
140
141                         perm = be_new_Perm(chordal_env->cls, irg, pred_bl, n_projs, in);
142                         free(in);
143                         insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env);
144                         sched_add_after(insert_after, perm);
145
146                         /*
147                          * Make the Projs for the Perm and insert into schedule.
148                          * Register allocation is copied from the former phi
149                          * arguments to the projs (new phi arguments).
150                          */
151                         insert_after = perm;
152                         for(pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
153                                 ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(pp->arg), pp->pos);
154                                 pp->proj = proj;
155                                 assert(get_reg(pp->arg));
156                                 set_reg(proj, get_reg(pp->arg));
157                                 sched_add_after(insert_after, proj);
158                                 insert_after = proj;
159                                 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(pp->arg)->name, pp->arg, pp->proj));
160                         }
161
162                         /*
163                          * Set the phi nodes to their new arguments: The Projs of the Perm
164                          */
165                         for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
166                                 perm_proj_t templ;
167
168                                 templ.arg = get_irn_n(phi, i);
169                                 pp        = set_find(arg_set, &templ, sizeof(templ), nodeset_hash(templ.arg));
170
171                                 /* If not found, it was an interfering argument */
172                                 if (pp)
173                                         set_irn_n(phi, i, pp->proj);
174                         }
175
176                         /* register in perm map */
177                         pmap_insert(perm_map, pred_bl, perm);
178                 }
179
180                 del_set(arg_set);
181         }
182 }
183
184 #define is_pinned(irn) (get_irn_link(irn))
185 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
186 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
187
188 /**
189  * Adjusts the register allocation for the (new) phi-operands
190  * and insert duplicates iff necessary.
191  */
192 static void     set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
193         be_chordal_env_t *chordal_env = data;
194         ir_node *phi;
195
196         /* Consider all phis of this block */
197         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
198                 int i, max;
199                 ir_node *arg, *phi_block, *arg_block;
200                 const arch_register_t *phi_reg, *arg_reg;
201                 const arch_register_class_t *cls;
202
203                 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
204
205                 phi_block = get_nodes_block(phi);
206                 phi_reg   = get_reg(phi);
207                 cls       = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, -1);
208
209                 /* process all arguments of the phi */
210                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
211                         arg       = get_irn_n(phi, i);
212                         arg_block = get_Block_cfgpred_block(phi_block, i);
213                         arg_reg   = get_reg(arg);
214
215                         assert(arg_reg && "Register must be set while placing perms");
216
217                         DBG((dbg, LEVEL_1, "  for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
218
219                         if(values_interfere(chordal_env->lv, phi, arg)) {
220                                 /*
221                                         Insert a duplicate in arguments block,
222                                         make it the new phi arg,
223                                         set its register,
224                                         insert it into schedule,
225                                         pin it
226                                 */
227                                 ir_node *dupl  = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
228
229                                 /* this is commented out because it will fail in case of unknown float */
230 #if 0
231                                 ir_mode *m_phi = get_irn_mode(phi), *m_dupl = get_irn_mode(dupl);
232
233                                 /*
234                                         Conv signed <-> unsigned is killed on ia32
235                                         check for: (both int OR both float) AND equal mode sizes
236                                 */
237                                 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
238                                         (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
239                                         (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
240 #endif /* if 0 */
241
242                                 set_irn_n(phi, i, dupl);
243                                 set_reg(dupl, phi_reg);
244                                 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env), dupl);
245                                 pin_irn(dupl, phi_block);
246                                 DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
247                                 continue; /* with next argument */
248                         }
249
250                         if (phi_reg == arg_reg) {
251                                 /* Phi and arg have the same register, so pin and continue */
252                                 pin_irn(arg, phi_block);
253                                 DBG((dbg, LEVEL_1, "      arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
254                                 continue;
255                         }
256
257                         DBG((dbg, LEVEL_1, "    they do not interfere\n"));
258                         assert(is_Proj(arg));
259                         /*
260                                 First check if there is an other phi
261                                 - in the same block
262                                 - having arg at the current pos in its arg-list
263                                 - having the same color as arg
264
265                                 If found, then pin the arg (for that phi)
266                         */
267                         if (! is_pinned(arg)) {
268                                 ir_node *other_phi;
269
270                                 DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
271
272                                 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
273
274                                         assert(is_Phi(other_phi)                               &&
275                                                 get_nodes_block(phi) == get_nodes_block(other_phi) &&
276                                                 "link fields are screwed up");
277
278                                         if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
279                                                 DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
280                                                 pin_irn(arg, phi_block);
281                                                 break;
282                                         }
283                                 }
284                         }
285
286                         if (is_pinned(arg)) {
287                                 /*
288                                         Insert a duplicate of the original value in arguments block,
289                                         make it the new phi arg,
290                                         set its register,
291                                         insert it into schedule,
292                                         pin it
293                                 */
294                                 ir_node *perm     = get_Proj_pred(arg);
295                                 ir_node *dupl     = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
296                                 ir_node *ins;
297
298                                 /* this is commented out because it will fail in case of unknown float */
299 #if 0
300                                 ir_mode *m_phi    = get_irn_mode(phi);
301                                 ir_mode *m_dupl   = get_irn_mode(dupl);
302
303                                 /*
304                                         Conv signed <-> unsigned is killed on ia32
305                                         check for: (both int OR both float) AND equal mode sizes
306                                 */
307                                 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
308                                         (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
309                                         (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
310 #endif /* if 0 */
311
312                                 set_irn_n(phi, i, dupl);
313                                 set_reg(dupl, phi_reg);
314                                 /* skip the Perm's Projs and insert the copies behind. */
315                                 for(ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins));
316                                 sched_add_before(ins, dupl);
317                                 pin_irn(dupl, phi_block);
318                                 DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
319                         } else {
320                                 /*
321                                         No other phi has the same color (else arg would have been pinned),
322                                         so just set the register and pin
323                                 */
324                                 set_reg(arg, phi_reg);
325                                 pin_irn(arg, phi_block);
326                                 DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
327                         }
328                 }
329         }
330 }
331
332 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
333         pmap *perm_map = pmap_create();
334         ir_graph *irg  = chordal_env->irg;
335
336         FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
337
338         /* create a map for fast lookup of perms: block --> perm */
339         chordal_env->data = perm_map;
340         irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
341
342         DBG((dbg, LEVEL_1, "Placing perms...\n"));
343         irg_block_walk_graph(irg, insert_all_perms_walker, NULL, chordal_env);
344
345         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
346                 be_dump(irg, "-ssa_destr_perms_placed", dump_ir_block_graph_sched);
347
348         be_liveness_recompute(chordal_env->lv);
349
350         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
351         irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
352
353         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
354                 be_dump(irg, "-ssa_destr_regs_set", dump_ir_block_graph_sched);
355
356         pmap_destroy(perm_map);
357 }
358
359 static void ssa_destruction_check_walker(ir_node *bl, void *data) {
360         be_chordal_env_t *chordal_env = data;
361         ir_node *phi;
362         int i, max;
363
364         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
365                 const arch_register_t *phi_reg, *arg_reg;
366
367                 phi_reg = get_reg(phi);
368                 /* iterate over all args of phi */
369                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
370                         ir_node *arg = get_irn_n(phi, i);
371
372                         arg_reg = get_reg(arg);
373
374                         if (phi_reg != arg_reg) {
375                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
376                                 assert(0);
377                         }
378
379                         if (! is_pinned(arg)) {
380                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
381                                 assert(0);
382                         }
383                 }
384         }
385 }
386
387 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
388         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, chordal_env);
389 }