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