amd64: small changes w.r.t. stack alignment.
[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 "bessadestr.h"
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.h"
46 #include "belive_t.h"
47 #include "benode.h"
48 #include "besched.h"
49 #include "bestatevent.h"
50 #include "beirg.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 static void clear_link(ir_node *irn, void *data)
59 {
60         (void) data;
61         set_irn_link(irn, NULL);
62 }
63
64 /**
65  * For each block build a linked list of phis that
66  *  - are in that block
67  *  - have the current register class
68  * The list is rooted at get_irn_link(BB).
69  */
70 static void collect_phis_walker(ir_node *irn, void *data)
71 {
72         be_chordal_env_t *env = data;
73         if (is_Phi(irn) && chordal_has_class(env, irn)) {
74                 ir_node *bl = get_nodes_block(irn);
75                 set_irn_link(irn, get_irn_link(bl));
76                 set_irn_link(bl, irn);
77         }
78 }
79
80 /**
81  * This struct represents a Proj for a Perm.
82  * It records the argument in the Perm and the corresponding Proj of the
83  * Perm.
84  */
85 typedef struct {
86         ir_node *arg;  /**< The phi argument to make the Proj for. */
87         int pos;       /**< The proj number the Proj will get.
88                                                                          This also denotes the position of @p arg
89                                                                          in the in array of the Perm. */
90         ir_node *proj; /**< The proj created for @p arg. */
91 } perm_proj_t;
92
93 static int cmp_perm_proj(const void *a, const void *b, size_t n)
94 {
95         const perm_proj_t *p = a;
96         const perm_proj_t *q = b;
97         (void) n;
98
99         return !(p->arg == q->arg);
100 }
101
102 typedef struct insert_all_perms_env_t {
103         be_chordal_env_t *chordal_env;
104         pmap *perm_map;
105 } insert_all_perms_env_t;
106
107 /**
108  * Insert Perms in all predecessors of a block containing a phi
109  */
110 static void insert_all_perms_walker(ir_node *bl, void *data)
111 {
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         be_lv_t *lv = chordal_env->birg->lv;
116         int i, n;
117
118         assert(is_Block(bl));
119
120         /* If the link flag is NULL, this block has no phis. */
121         if (!get_irn_link(bl))
122                 return;
123
124         /* Look at all predecessors of the phi block */
125         for (i = 0, n = get_irn_arity(bl); i < n; ++i) {
126                 ir_node *phi, *perm, *insert_after, **in;
127                 perm_proj_t *pp;
128                 set *arg_set     = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
129                 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
130                 int n_projs      = 0;
131
132                 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
133
134                 /*
135                  * Note that all phis in the list are in the same
136                  * register class by construction.
137                  */
138                 for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
139                         ir_node                   *arg = get_irn_n(phi, i);
140                         const arch_register_req_t *req = arch_get_register_req_out(arg);
141                         unsigned                   hash;
142                         perm_proj_t                templ;
143
144                         if (req->type & arch_register_req_type_ignore)
145                                 continue;
146
147                         hash = hash_irn(arg);
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 later 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 = XMALLOCN(ir_node*, n_projs);
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, 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, NULL);
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(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 {
237         be_chordal_env_t *chordal_env = data;
238         be_lv_t *lv = chordal_env->birg->lv;
239         ir_node *phi;
240
241         /* Consider all phis of this block */
242         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
243                 ir_node                     *phi_block = get_nodes_block(phi);
244                 const arch_register_t       *phi_reg   = get_reg(phi);
245                 const arch_register_class_t *cls       = phi_reg->reg_class;
246                 int                          max;
247                 int                          i;
248
249                 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
250
251                 /* process all arguments of the phi */
252                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
253                         ir_node                   *arg = get_irn_n(phi, i);
254                         const arch_register_req_t *req = arch_get_register_req_out(arg);
255                         const arch_register_t     *arg_reg;
256                         ir_node                   *arg_block;
257
258                         if (req->type & arch_register_req_type_ignore)
259                                 continue;
260
261                         arg_block = get_Block_cfgpred_block(phi_block, i);
262                         arg_reg   = get_reg(arg);
263
264                         assert(arg_reg && "Register must be set while placing perms");
265
266                         DBG((dbg, LEVEL_1, "  for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
267
268                         if (be_values_interfere(lv, phi, arg)) {
269                                 /*
270                                         Insert a duplicate in arguments block,
271                                         make it the new phi arg,
272                                         set its register,
273                                         insert it into schedule,
274                                         pin it
275                                 */
276                                 ir_node *dupl  = be_new_Copy(cls, arg_block, arg);
277
278                                 /* this is commented out because it will fail in case of unknown float */
279 #if 0
280                                 ir_mode *m_phi = get_irn_mode(phi), *m_dupl = get_irn_mode(dupl);
281
282                                 /*
283                                         Conv signed <-> unsigned is killed on ia32
284                                         check for: (both int OR both float) AND equal mode sizes
285                                 */
286                                 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
287                                         (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
288                                         (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
289 #endif /* if 0 */
290
291                                 set_irn_n(phi, i, dupl);
292                                 set_reg(dupl, phi_reg);
293                                 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, NULL), dupl);
294                                 pin_irn(dupl, phi_block);
295                                 be_liveness_introduce(lv, dupl);
296                                 be_liveness_update(lv, arg);
297                                 DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
298                                 continue; /* with next argument */
299                         }
300
301                         if (phi_reg == arg_reg) {
302                                 /* Phi and arg have the same register, so pin and continue */
303                                 pin_irn(arg, phi_block);
304                                 DBG((dbg, LEVEL_1, "      arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
305                                 continue;
306                         }
307
308                         DBG((dbg, LEVEL_1, "    they do not interfere\n"));
309                         assert(is_Proj(arg));
310                         /*
311                                 First check if there is an other phi
312                                 - in the same block
313                                 - having arg at the current pos in its arg-list
314                                 - having the same color as arg
315
316                                 If found, then pin the arg (for that phi)
317                         */
318                         if (! is_pinned(arg)) {
319                                 ir_node *other_phi;
320
321                                 DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
322
323                                 for (other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
324
325                                         assert(is_Phi(other_phi)                               &&
326                                                 get_nodes_block(phi) == get_nodes_block(other_phi) &&
327                                                 "link fields are screwed up");
328
329                                         if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
330                                                 DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
331                                                 pin_irn(arg, phi_block);
332                                                 break;
333                                         }
334                                 }
335                         }
336
337                         if (is_pinned(arg)) {
338                                 /*
339                                         Insert a duplicate of the original value in arguments block,
340                                         make it the new phi arg,
341                                         set its register,
342                                         insert it into schedule,
343                                         pin it
344                                 */
345                                 ir_node *perm = get_Proj_pred(arg);
346                                 ir_node *dupl = be_new_Copy(cls, arg_block, arg);
347                                 ir_node *ins;
348
349                                 /* this is commented out because it will fail in case of unknown float */
350 #if 0
351                                 ir_mode *m_phi    = get_irn_mode(phi);
352                                 ir_mode *m_dupl   = get_irn_mode(dupl);
353
354                                 /*
355                                         Conv signed <-> unsigned is killed on ia32
356                                         check for: (both int OR both float) AND equal mode sizes
357                                 */
358                                 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
359                                         (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
360                                         (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
361 #endif /* if 0 */
362
363                                 set_irn_n(phi, i, dupl);
364                                 set_reg(dupl, phi_reg);
365                                 /* skip the Perm's Projs and insert the copies behind. */
366                                 for (ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins)) {
367                                 }
368                                 sched_add_before(ins, dupl);
369                                 pin_irn(dupl, phi_block);
370                                 be_liveness_introduce(lv, dupl);
371                                 be_liveness_update(lv, arg);
372                                 DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
373                         } else {
374                                 /*
375                                         No other phi has the same color (else arg would have been pinned),
376                                         so just set the register and pin
377                                 */
378                                 set_reg(arg, phi_reg);
379                                 pin_irn(arg, phi_block);
380                                 DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
381                         }
382                 }
383         }
384 }
385
386 void be_ssa_destruction(be_chordal_env_t *chordal_env)
387 {
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
397         /* create a map for fast lookup of perms: block --> perm */
398         irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
399
400         DBG((dbg, LEVEL_1, "Placing perms...\n"));
401         insert_perms_env.chordal_env = chordal_env;
402         insert_perms_env.perm_map = perm_map;
403         irg_block_walk_graph(irg, insert_all_perms_walker, NULL, &insert_perms_env);
404
405         // Matze: really needed here?
406         // Sebastian: Yes. the walker function uses interference.
407         be_liveness_invalidate(lv);
408
409         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
410                 dump_ir_graph(irg, "ssa_destr_perms_placed");
411
412         be_liveness_assure_chk(lv);
413
414         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
415         irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
416
417         /* TODO: unfortunately updating doesn't work yet. */
418         be_liveness_invalidate(lv);
419
420         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
421                 dump_ir_graph(irg, "ssa_destr_regs_set");
422
423         pmap_destroy(perm_map);
424 }
425
426 static void ssa_destruction_check_walker(ir_node *bl, void *data)
427 {
428         ir_node *phi;
429         int i, max;
430         (void)data;
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                         const arch_register_req_t *req = arch_get_register_req_out(arg);
440
441                         if (req->type & arch_register_req_type_ignore)
442                                 continue;
443
444                         arg_reg = get_reg(arg);
445
446                         if (phi_reg != arg_reg) {
447                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
448                                 assert(0);
449                         }
450
451                         if (! is_pinned(arg)) {
452                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
453                                 assert(0);
454                         }
455                 }
456         }
457 }
458
459 void be_ssa_destruction_check(be_chordal_env_t *chordal_env)
460 {
461         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, NULL);
462 }