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