Adapted to stat events
[libfirm] / ir / be / bessadestr.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Performs SSA-Destruction.
23  * @author      Daniel Grund
24  * @date        25.05.2005
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "debug.h"
32 #include "set.h"
33 #include "pmap.h"
34 #include "irnode_t.h"
35 #include "ircons_t.h"
36 #include "iredges_t.h"
37 #include "irgwalk.h"
38 #include "irgmod.h"
39 #include "irdump.h"
40 #include "irprintf.h"
41
42 #include "be_t.h"
43 #include "beutil.h"
44 #include "bechordal_t.h"
45 #include "bearch_t.h"
46 #include "belive_t.h"
47 #include "benode_t.h"
48 #include "besched_t.h"
49 #include "benodesets.h"
50 #include "bestatevent.h"
51 #include "beirg_t.h"
52 #include "beintlive_t.h"
53
54 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
55
56 #define get_chordal_arch(ce) ((ce)->birg->main_env->arch_env)
57 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn)
58 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, reg)
59
60 #define is_Perm(irn)            (arch_irn_class_is(arch_env, irn, perm))
61 #define get_reg_cls(irn)        (arch_get_irn_reg_class(arch_env, irn, -1))
62 #define is_curr_reg_class(irn)  (get_reg_cls(p) == chordal_env->cls)
63
64 static void clear_link(ir_node *irn, void *data) {
65         (void) data;
66         set_irn_link(irn, NULL);
67 }
68
69 /**
70  * For each block build a linked list of phis that
71  *  - are in that block
72  *  - have the current register class
73  * The list is rooted at get_irn_link(BB).
74  */
75 static void collect_phis_walker(ir_node *irn, void *data) {
76         be_chordal_env_t *env = data;
77         if (is_Phi(irn) && chordal_has_class(env, irn)) {
78                 ir_node *bl = get_nodes_block(irn);
79                 set_irn_link(irn, get_irn_link(bl));
80                 set_irn_link(bl, irn);
81         }
82 }
83
84 /**
85  * This struct represents a Proj for a Perm.
86  * It records the argument in the Perm and the corresponding Proj of the
87  * Perm.
88  */
89 typedef struct {
90         ir_node *arg;  /**< The phi argument to make the Proj for. */
91         int pos;       /**< The proj number the Proj will get.
92                                                                          This also denotes the position of @p arg
93                                                                          in the in array of the Perm. */
94         ir_node *proj; /**< The proj created for @p arg. */
95 } perm_proj_t;
96
97 static int cmp_perm_proj(const void *a, const void *b, size_t n) {
98         const perm_proj_t *p = a;
99         const perm_proj_t *q = b;
100         (void) n;
101
102         return !(p->arg == q->arg);
103 }
104
105 typedef struct insert_all_perms_env_t {
106         be_chordal_env_t *chordal_env;
107         pmap *perm_map;
108 } insert_all_perms_env_t;
109
110 /**
111  * Insert Perms in all predecessors of a block containing a phi
112  */
113 static void insert_all_perms_walker(ir_node *bl, void *data) {
114         insert_all_perms_env_t *env = data;
115         be_chordal_env_t *chordal_env = env->chordal_env;
116         pmap *perm_map = env->perm_map;
117         ir_graph *irg = chordal_env->irg;
118         be_lv_t *lv = chordal_env->birg->lv;
119         int i, n;
120
121         assert(is_Block(bl));
122
123         /* If the link flag is NULL, this block has no phis. */
124         if(!get_irn_link(bl))
125                 return;
126
127         /* Look at all predecessors of the phi block */
128         for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
129                 ir_node *phi, *perm, *insert_after, **in;
130                 perm_proj_t *pp;
131                 set *arg_set     = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
132                 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
133                 int n_projs      = 0;
134
135                 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
136
137                 /*
138                  * Note that all phis in the list are in the same
139                  * register class by construction.
140                  */
141                 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
142                         perm_proj_t templ;
143                         ir_node *arg     = get_irn_n(phi, i);
144                         unsigned hash    = nodeset_hash(arg);
145
146                         if (arch_irn_is(chordal_env->birg->main_env->arch_env, arg, ignore))
147                                 continue;
148
149                         templ.arg  = arg;
150                         pp         = set_find(arg_set, &templ, sizeof(templ), hash);
151
152                         /*
153                          * If a proj_perm_t entry has not been made in the argument set,
154                          * create one. The only restriction is, that the phi argument
155                          * may not be live in at the current block, since this argument
156                          * interferes with the phi and must thus not be member of a
157                          * Perm. A copy will be inserted for this argument alter on.
158                          */
159                         if(!pp && !be_is_live_in(lv, bl, arg)) {
160                                 templ.pos = n_projs++;
161                                 set_insert(arg_set, &templ, sizeof(templ), hash);
162                         }
163                 }
164
165
166                 if (n_projs) {
167                         /*
168                          * Create a new Perm with the arguments just collected
169                          * above in the arg_set and insert it into the schedule.
170                          */
171                         in = xmalloc(n_projs * sizeof(in[0]));
172                         for(pp = set_first(arg_set); pp; pp = set_next(arg_set))
173                                 in[pp->pos] = pp->arg;
174
175                         perm = be_new_Perm(chordal_env->cls, irg, pred_bl, n_projs, in);
176                         be_stat_ev("phi_perm", n_projs);
177
178                         insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env);
179                         sched_add_after(insert_after, perm);
180
181                         /*
182                          * Make the Projs for the Perm and insert into schedule.
183                          * Register allocation is copied from the former phi
184                          * arguments to the projs (new phi arguments).
185                          */
186                         insert_after = perm;
187                         for(pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
188                                 ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(pp->arg), pp->pos);
189                                 pp->proj = proj;
190                                 assert(get_reg(pp->arg));
191                                 set_reg(proj, get_reg(pp->arg));
192 #ifdef SCHEDULE_PROJS
193                                 sched_add_after(insert_after, proj);
194 #endif
195                                 insert_after = proj;
196                                 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(pp->arg)->name, pp->arg, pp->proj));
197                         }
198
199                         /*
200                          * Set the phi nodes to their new arguments: The Projs of the Perm
201                          */
202                         for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
203                                 perm_proj_t templ;
204
205                                 templ.arg = get_irn_n(phi, i);
206                                 pp        = set_find(arg_set, &templ, sizeof(templ), nodeset_hash(templ.arg));
207
208                                 /* If not found, it was an interfering argument */
209                                 if (pp) {
210                                         set_irn_n(phi, i, pp->proj);
211                                         be_liveness_introduce(lv, pp->proj);
212                                 }
213                         }
214
215                         /* update the liveness of the Perm's operands. It might be changed. */
216                         {
217                                 int i;
218                                 for (i = 0; i < n_projs; ++i)
219                                         be_liveness_update(lv, in[i]);
220                         }
221                         free(in);
222
223                         /* register in perm map */
224                         pmap_insert(perm_map, pred_bl, perm);
225                 }
226
227                 del_set(arg_set);
228         }
229 }
230
231 #define is_pinned(irn) (get_irn_link(irn))
232 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
233 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
234
235 /**
236  * Adjusts the register allocation for the (new) phi-operands
237  * and insert duplicates iff necessary.
238  */
239 static void     set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
240         be_chordal_env_t *chordal_env = data;
241         be_lv_t *lv = chordal_env->birg->lv;
242         ir_node *phi;
243
244         /* Consider all phis of this block */
245         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
246                 int i, max;
247                 ir_node *arg, *phi_block, *arg_block;
248                 const arch_register_t *phi_reg, *arg_reg;
249                 const arch_register_class_t *cls;
250
251                 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
252
253                 phi_block = get_nodes_block(phi);
254                 phi_reg   = get_reg(phi);
255                 cls       = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, -1);
256
257                 /* process all arguments of the phi */
258                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
259                         arg       = get_irn_n(phi, i);
260                         arg_block = get_Block_cfgpred_block(phi_block, i);
261                         arg_reg   = get_reg(arg);
262
263                         if (arch_irn_is(chordal_env->birg->main_env->arch_env, arg, ignore))
264                                 continue;
265
266                         assert(arg_reg && "Register must be set while placing perms");
267
268                         DBG((dbg, LEVEL_1, "  for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
269
270                         if (values_interfere(chordal_env->birg, phi, arg)) {
271                                 /*
272                                         Insert a duplicate in arguments block,
273                                         make it the new phi arg,
274                                         set its register,
275                                         insert it into schedule,
276                                         pin it
277                                 */
278                                 ir_node *dupl  = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
279
280                                 /* this is commented out because it will fail in case of unknown float */
281 #if 0
282                                 ir_mode *m_phi = get_irn_mode(phi), *m_dupl = get_irn_mode(dupl);
283
284                                 /*
285                                         Conv signed <-> unsigned is killed on ia32
286                                         check for: (both int OR both float) AND equal mode sizes
287                                 */
288                                 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
289                                         (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
290                                         (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
291 #endif /* if 0 */
292
293                                 set_irn_n(phi, i, dupl);
294                                 set_reg(dupl, phi_reg);
295                                 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env), dupl);
296                                 pin_irn(dupl, phi_block);
297                                 be_liveness_introduce(lv, dupl);
298                                 be_liveness_update(lv, arg);
299                                 DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
300                                 continue; /* with next argument */
301                         }
302
303                         if (phi_reg == arg_reg) {
304                                 /* Phi and arg have the same register, so pin and continue */
305                                 pin_irn(arg, phi_block);
306                                 DBG((dbg, LEVEL_1, "      arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
307                                 continue;
308                         }
309
310                         DBG((dbg, LEVEL_1, "    they do not interfere\n"));
311                         assert(is_Proj(arg));
312                         /*
313                                 First check if there is an other phi
314                                 - in the same block
315                                 - having arg at the current pos in its arg-list
316                                 - having the same color as arg
317
318                                 If found, then pin the arg (for that phi)
319                         */
320                         if (! is_pinned(arg)) {
321                                 ir_node *other_phi;
322
323                                 DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
324
325                                 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
326
327                                         assert(is_Phi(other_phi)                               &&
328                                                 get_nodes_block(phi) == get_nodes_block(other_phi) &&
329                                                 "link fields are screwed up");
330
331                                         if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
332                                                 DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
333                                                 pin_irn(arg, phi_block);
334                                                 break;
335                                         }
336                                 }
337                         }
338
339                         if (is_pinned(arg)) {
340                                 /*
341                                         Insert a duplicate of the original value in arguments block,
342                                         make it the new phi arg,
343                                         set its register,
344                                         insert it into schedule,
345                                         pin it
346                                 */
347                                 ir_node *perm     = get_Proj_pred(arg);
348                                 ir_node *dupl     = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
349                                 ir_node *ins;
350
351                                 /* this is commented out because it will fail in case of unknown float */
352 #if 0
353                                 ir_mode *m_phi    = get_irn_mode(phi);
354                                 ir_mode *m_dupl   = get_irn_mode(dupl);
355
356                                 /*
357                                         Conv signed <-> unsigned is killed on ia32
358                                         check for: (both int OR both float) AND equal mode sizes
359                                 */
360                                 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
361                                         (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
362                                         (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
363 #endif /* if 0 */
364
365                                 set_irn_n(phi, i, dupl);
366                                 set_reg(dupl, phi_reg);
367                                 /* skip the Perm's Projs and insert the copies behind. */
368                                 for(ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins));
369                                 sched_add_before(ins, dupl);
370                                 pin_irn(dupl, phi_block);
371                                 be_liveness_introduce(lv, dupl);
372                                 be_liveness_update(lv, arg);
373                                 DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
374                         } else {
375                                 /*
376                                         No other phi has the same color (else arg would have been pinned),
377                                         so just set the register and pin
378                                 */
379                                 set_reg(arg, phi_reg);
380                                 pin_irn(arg, phi_block);
381                                 DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
382                         }
383                 }
384         }
385 }
386
387 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
388         insert_all_perms_env_t insert_perms_env;
389         pmap *perm_map = pmap_create();
390         ir_graph *irg  = chordal_env->irg;
391         be_lv_t *lv    = be_assure_liveness(chordal_env->birg);
392
393         FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
394
395         be_liveness_invalidate(lv);
396         be_liveness_assure_sets(lv);
397
398         /* create a map for fast lookup of perms: block --> perm */
399         irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
400
401         DBG((dbg, LEVEL_1, "Placing perms...\n"));
402         insert_perms_env.chordal_env = chordal_env;
403         insert_perms_env.perm_map = perm_map;
404         irg_block_walk_graph(irg, insert_all_perms_walker, NULL, &insert_perms_env);
405
406         // Matze: really needed here?
407         // Sebastian: Yes. the walker function uses interference.
408         be_liveness_invalidate(lv);
409
410         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
411                 be_dump(irg, "-ssa_destr_perms_placed", dump_ir_block_graph_sched);
412
413         be_liveness_assure_chk(lv);
414
415         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
416         irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
417
418         /* TODO: unfortunaltely updating doesn't work yet. */
419         be_liveness_invalidate(lv);
420
421         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
422                 be_dump(irg, "-ssa_destr_regs_set", dump_ir_block_graph_sched);
423
424         pmap_destroy(perm_map);
425 }
426
427 static void ssa_destruction_check_walker(ir_node *bl, void *data) {
428         be_chordal_env_t *chordal_env = data;
429         ir_node *phi;
430         int i, max;
431
432         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
433                 const arch_register_t *phi_reg, *arg_reg;
434
435                 phi_reg = get_reg(phi);
436                 /* iterate over all args of phi */
437                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
438                         ir_node *arg = get_irn_n(phi, i);
439
440                         if (arch_irn_is(chordal_env->birg->main_env->arch_env, arg, ignore))
441                                 continue;
442
443                         arg_reg = get_reg(arg);
444
445                         if (phi_reg != arg_reg) {
446                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
447                                 assert(0);
448                         }
449
450                         if (! is_pinned(arg)) {
451                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
452                                 assert(0);
453                         }
454                 }
455         }
456 }
457
458 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
459         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, chordal_env);
460 }