Remove address name SymConsts.
[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 #include "config.h"
28
29 #include "debug.h"
30 #include "set.h"
31 #include "pmap.h"
32 #include "irnode_t.h"
33 #include "ircons_t.h"
34 #include "iredges_t.h"
35 #include "irgwalk.h"
36 #include "irgmod.h"
37 #include "irdump.h"
38 #include "irprintf.h"
39
40 #include "be_t.h"
41 #include "beutil.h"
42 #include "bechordal_t.h"
43 #include "bearch.h"
44 #include "belive_t.h"
45 #include "benode.h"
46 #include "besched.h"
47 #include "bestatevent.h"
48 #include "beirg.h"
49 #include "beintlive_t.h"
50
51 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
52
53 #define get_reg(irn)         arch_get_irn_register(irn)
54 #define set_reg(irn, reg)    arch_set_irn_register(irn, reg)
55
56 static void clear_link(ir_node *irn, void *data)
57 {
58         (void) data;
59         set_irn_link(irn, NULL);
60 }
61
62 /**
63  * For each block build a linked list of phis that
64  *  - are in that block
65  *  - have the current register class
66  * The list is rooted at get_irn_link(BB).
67  */
68 static void collect_phis_walker(ir_node *irn, void *data)
69 {
70         be_chordal_env_t *env = data;
71         if (is_Phi(irn) && chordal_has_class(env, irn)) {
72                 ir_node *bl = get_nodes_block(irn);
73                 set_irn_link(irn, get_irn_link(bl));
74                 set_irn_link(bl, irn);
75         }
76 }
77
78 /**
79  * This struct represents a Proj for a Perm.
80  * It records the argument in the Perm and the corresponding Proj of the
81  * Perm.
82  */
83 typedef struct {
84         ir_node *arg;  /**< The phi argument to make the Proj for. */
85         int pos;       /**< The proj number the Proj will get.
86                                                                          This also denotes the position of @p arg
87                                                                          in the in array of the Perm. */
88         ir_node *proj; /**< The proj created for @p arg. */
89 } perm_proj_t;
90
91 static int cmp_perm_proj(const void *a, const void *b, size_t n)
92 {
93         const perm_proj_t *p = a;
94         const perm_proj_t *q = b;
95         (void) n;
96
97         return !(p->arg == q->arg);
98 }
99
100 typedef struct insert_all_perms_env_t {
101         be_chordal_env_t *chordal_env;
102         pmap *perm_map;
103 } insert_all_perms_env_t;
104
105 /**
106  * Insert Perms in all predecessors of a block containing a phi
107  */
108 static void insert_all_perms_walker(ir_node *bl, void *data)
109 {
110         insert_all_perms_env_t *env = data;
111         be_chordal_env_t *chordal_env = env->chordal_env;
112         pmap *perm_map = env->perm_map;
113         be_lv_t *lv = chordal_env->birg->lv;
114         int i, n;
115
116         assert(is_Block(bl));
117
118         /* If the link flag is NULL, this block has no phis. */
119         if (!get_irn_link(bl))
120                 return;
121
122         /* Look at all predecessors of the phi block */
123         for (i = 0, n = get_irn_arity(bl); i < n; ++i) {
124                 ir_node *phi, *perm, *insert_after, **in;
125                 perm_proj_t *pp;
126                 set *arg_set     = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
127                 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
128                 int n_projs      = 0;
129
130                 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
131
132                 /*
133                  * Note that all phis in the list are in the same
134                  * register class by construction.
135                  */
136                 for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
137                         ir_node                   *arg = get_irn_n(phi, i);
138                         const arch_register_req_t *req = arch_get_register_req_out(arg);
139                         unsigned                   hash;
140                         perm_proj_t                templ;
141
142                         if (req->type & arch_register_req_type_ignore)
143                                 continue;
144
145                         hash = hash_irn(arg);
146                         templ.arg  = arg;
147                         pp         = set_find(arg_set, &templ, sizeof(templ), hash);
148
149                         /*
150                          * If a proj_perm_t entry has not been made in the argument set,
151                          * create one. The only restriction is, that the phi argument
152                          * may not be live in at the current block, since this argument
153                          * interferes with the phi and must thus not be member of a
154                          * Perm. A copy will be inserted for this argument later on.
155                          */
156                         if (!pp && !be_is_live_in(lv, bl, arg)) {
157                                 templ.pos = n_projs++;
158                                 set_insert(arg_set, &templ, sizeof(templ), hash);
159                         }
160                 }
161
162
163                 if (n_projs) {
164                         /*
165                          * Create a new Perm with the arguments just collected
166                          * above in the arg_set and insert it into the schedule.
167                          */
168                         in = XMALLOCN(ir_node*, n_projs);
169                         for (pp = set_first(arg_set); pp; pp = set_next(arg_set))
170                                 in[pp->pos] = pp->arg;
171
172                         perm = be_new_Perm(chordal_env->cls, pred_bl, n_projs, in);
173                         be_stat_ev("phi_perm", n_projs);
174
175                         insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, NULL);
176                         sched_add_after(insert_after, perm);
177
178                         /*
179                          * Make the Projs for the Perm and insert into schedule.
180                          * Register allocation is copied from the former phi
181                          * arguments to the projs (new phi arguments).
182                          */
183                         insert_after = perm;
184                         for (pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
185                                 ir_node *proj = new_r_Proj(pred_bl, perm, get_irn_mode(pp->arg), pp->pos);
186                                 pp->proj = proj;
187                                 assert(get_reg(pp->arg));
188                                 set_reg(proj, get_reg(pp->arg));
189                                 insert_after = proj;
190                                 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(pp->arg)->name, pp->arg, pp->proj));
191                         }
192
193                         /*
194                          * Set the phi nodes to their new arguments: The Projs of the Perm
195                          */
196                         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
197                                 perm_proj_t templ;
198
199                                 templ.arg = get_irn_n(phi, i);
200                                 pp        = set_find(arg_set, &templ, sizeof(templ), hash_irn(templ.arg));
201
202                                 /* If not found, it was an interfering argument */
203                                 if (pp) {
204                                         set_irn_n(phi, i, pp->proj);
205                                         be_liveness_introduce(lv, pp->proj);
206                                 }
207                         }
208
209                         /* update the liveness of the Perm's operands. It might be changed. */
210                         {
211                                 int i;
212                                 for (i = 0; i < n_projs; ++i)
213                                         be_liveness_update(lv, in[i]);
214                         }
215                         free(in);
216
217                         /* register in perm map */
218                         pmap_insert(perm_map, pred_bl, perm);
219                 }
220
221                 del_set(arg_set);
222         }
223 }
224
225 #define is_pinned(irn) (get_irn_link(irn))
226 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
227 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
228
229 /**
230  * Adjusts the register allocation for the (new) phi-operands
231  * and insert duplicates iff necessary.
232  */
233 static void     set_regs_or_place_dupls_walker(ir_node *bl, void *data)
234 {
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                 ir_node                     *phi_block = get_nodes_block(phi);
242                 const arch_register_t       *phi_reg   = get_reg(phi);
243                 const arch_register_class_t *cls       = phi_reg->reg_class;
244                 int                          max;
245                 int                          i;
246
247                 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
248
249                 /* process all arguments of the phi */
250                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
251                         ir_node                   *arg = get_irn_n(phi, i);
252                         const arch_register_req_t *req = arch_get_register_req_out(arg);
253                         const arch_register_t     *arg_reg;
254                         ir_node                   *arg_block;
255
256                         if (req->type & arch_register_req_type_ignore)
257                                 continue;
258
259                         arg_block = get_Block_cfgpred_block(phi_block, i);
260                         arg_reg   = get_reg(arg);
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 (be_values_interfere(lv, 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, 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, NULL), 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, 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 {
385         insert_all_perms_env_t insert_perms_env;
386         pmap *perm_map = pmap_create();
387         ir_graph *irg  = chordal_env->irg;
388         be_lv_t *lv    = be_assure_liveness(chordal_env->birg);
389
390         FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
391
392         be_liveness_invalidate(lv);
393
394         /* create a map for fast lookup of perms: block --> perm */
395         irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
396
397         DBG((dbg, LEVEL_1, "Placing perms...\n"));
398         insert_perms_env.chordal_env = chordal_env;
399         insert_perms_env.perm_map = perm_map;
400         irg_block_walk_graph(irg, insert_all_perms_walker, NULL, &insert_perms_env);
401
402         // Matze: really needed here?
403         // Sebastian: Yes. the walker function uses interference.
404         be_liveness_invalidate(lv);
405
406         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
407                 be_dump(irg, "-ssa_destr_perms_placed", dump_ir_block_graph_sched);
408
409         be_liveness_assure_chk(lv);
410
411         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
412         irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
413
414         /* TODO: unfortunately updating doesn't work yet. */
415         be_liveness_invalidate(lv);
416
417         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
418                 be_dump(irg, "-ssa_destr_regs_set", dump_ir_block_graph_sched);
419
420         pmap_destroy(perm_map);
421 }
422
423 static void ssa_destruction_check_walker(ir_node *bl, void *data)
424 {
425         ir_node *phi;
426         int i, max;
427         (void)data;
428
429         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
430                 const arch_register_t *phi_reg, *arg_reg;
431
432                 phi_reg = get_reg(phi);
433                 /* iterate over all args of phi */
434                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
435                         ir_node                   *arg = get_irn_n(phi, i);
436                         const arch_register_req_t *req = arch_get_register_req_out(arg);
437
438                         if (req->type & arch_register_req_type_ignore)
439                                 continue;
440
441                         arg_reg = get_reg(arg);
442
443                         if (phi_reg != arg_reg) {
444                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
445                                 assert(0);
446                         }
447
448                         if (! is_pinned(arg)) {
449                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
450                                 assert(0);
451                         }
452                 }
453         }
454 }
455
456 void be_ssa_destruction_check(be_chordal_env_t *chordal_env)
457 {
458         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, NULL);
459 }