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