updated Header
[libfirm] / ir / be / bessadestr.c
1 /*
2  * Copyright (C) 1995-2007 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  * Author:      Daniel Grund
22  * Date:                25.05.2005
23  * Copyright:   (c) Universitaet Karlsruhe
24  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
25  *
26  * Performs SSA-Destruction.
27  */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "debug.h"
33 #include "set.h"
34 #include "pmap.h"
35 #include "irnode_t.h"
36 #include "ircons_t.h"
37 #include "iredges_t.h"
38 #include "irgwalk.h"
39 #include "irgmod.h"
40 #include "irdump.h"
41 #include "irprintf.h"
42
43 #include "be_t.h"
44 #include "beutil.h"
45 #include "bechordal_t.h"
46 #include "bearch_t.h"
47 #include "belive_t.h"
48 #include "benode_t.h"
49 #include "besched_t.h"
50 #include "benodesets.h"
51 #include "bestatevent.h"
52 #include "beirg_t.h"
53 #include "bera.h"
54
55 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
56
57 #define get_chordal_arch(ce) ((ce)->birg->main_env->arch_env)
58 #define get_reg(irn) arch_get_irn_register(get_chordal_arch(chordal_env), irn)
59 #define set_reg(irn, reg) arch_set_irn_register(get_chordal_arch(chordal_env), irn, reg)
60
61 #define is_Perm(irn)            (arch_irn_class_is(arch_env, irn, perm))
62 #define get_reg_cls(irn)        (arch_get_irn_reg_class(arch_env, irn, -1))
63 #define is_curr_reg_class(irn)  (get_reg_cls(p) == chordal_env->cls)
64
65 static void clear_link(ir_node *irn, void *data) {
66         set_irn_link(irn, NULL);
67 }
68
69 /**
70  * For each block build a linked list of phis that
71  *  - are in that block
72  *  - have the current register class
73  * The list is rooted at get_irn_link(BB).
74  */
75 static void collect_phis_walker(ir_node *irn, void *data) {
76         be_chordal_env_t *env = data;
77         if (is_Phi(irn) && chordal_has_class(env, irn)) {
78                 ir_node *bl = get_nodes_block(irn);
79                 set_irn_link(irn, get_irn_link(bl));
80                 set_irn_link(bl, irn);
81         }
82 }
83
84 /**
85  * This struct represents a Proj for a Perm.
86  * It records the argument in the Perm and the corresponding Proj of the
87  * Perm.
88  */
89 typedef struct {
90         ir_node *arg;  /**< The phi argument to make the Proj for. */
91         int pos;       /**< The proj number the Proj will get.
92                                                                          This also denotes the position of @p arg
93                                                                          in the in array of the Perm. */
94         ir_node *proj; /**< The proj created for @p arg. */
95 } perm_proj_t;
96
97 static int cmp_perm_proj(const void *a, const void *b, size_t n) {
98         const perm_proj_t *p = a;
99         const perm_proj_t *q = b;
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                         perm_proj_t templ;
141                         ir_node *arg     = get_irn_n(phi, i);
142                         unsigned hash    = nodeset_hash(arg);
143
144                         templ.arg  = arg;
145                         pp         = set_find(arg_set, &templ, sizeof(templ), hash);
146
147                         /*
148                          * If a proj_perm_t entry has not been made in the argument set,
149                          * create one. The only restriction is, that the phi argument
150                          * may not be live in at the current block, since this argument
151                          * interferes with the phi and must thus not be member of a
152                          * Perm. A copy will be inserted for this argument alter on.
153                          */
154                         if(!pp && !be_is_live_in(lv, bl, arg)) {
155                                 templ.pos = n_projs++;
156                                 set_insert(arg_set, &templ, sizeof(templ), hash);
157                         }
158                 }
159
160
161                 if (n_projs) {
162                         /*
163                          * Create a new Perm with the arguments just collected
164                          * above in the arg_set and insert it into the schedule.
165                          */
166                         in = xmalloc(n_projs * sizeof(in[0]));
167                         for(pp = set_first(arg_set); pp; pp = set_next(arg_set))
168                                 in[pp->pos] = pp->arg;
169
170                         perm = be_new_Perm(chordal_env->cls, irg, pred_bl, n_projs, in);
171                         be_stat_ev("phi_perm", n_projs);
172
173                         free(in);
174                         insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env);
175                         sched_add_after(insert_after, perm);
176
177                         /*
178                          * Make the Projs for the Perm and insert into schedule.
179                          * Register allocation is copied from the former phi
180                          * arguments to the projs (new phi arguments).
181                          */
182                         insert_after = perm;
183                         for(pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
184                                 ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(pp->arg), pp->pos);
185                                 pp->proj = proj;
186                                 assert(get_reg(pp->arg));
187                                 set_reg(proj, get_reg(pp->arg));
188                                 sched_add_after(insert_after, proj);
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), nodeset_hash(templ.arg));
201
202                                 /* If not found, it was an interfering argument */
203                                 if (pp)
204                                         set_irn_n(phi, i, pp->proj);
205                         }
206
207                         /* register in perm map */
208                         pmap_insert(perm_map, pred_bl, perm);
209                 }
210
211                 del_set(arg_set);
212         }
213 }
214
215 #define is_pinned(irn) (get_irn_link(irn))
216 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
217 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
218
219 /**
220  * Adjusts the register allocation for the (new) phi-operands
221  * and insert duplicates iff necessary.
222  */
223 static void     set_regs_or_place_dupls_walker(ir_node *bl, void *data) {
224         be_chordal_env_t *chordal_env = data;
225         be_lv_t *lv = chordal_env->birg->lv;
226         ir_node *phi;
227
228         /* Consider all phis of this block */
229         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
230                 int i, max;
231                 ir_node *arg, *phi_block, *arg_block;
232                 const arch_register_t *phi_reg, *arg_reg;
233                 const arch_register_class_t *cls;
234
235                 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
236
237                 phi_block = get_nodes_block(phi);
238                 phi_reg   = get_reg(phi);
239                 cls       = arch_get_irn_reg_class(get_chordal_arch(chordal_env), phi, -1);
240
241                 /* process all arguments of the phi */
242                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
243                         arg       = get_irn_n(phi, i);
244                         arg_block = get_Block_cfgpred_block(phi_block, i);
245                         arg_reg   = get_reg(arg);
246
247                         assert(arg_reg && "Register must be set while placing perms");
248
249                         DBG((dbg, LEVEL_1, "  for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
250
251                         if (values_interfere(lv, phi, arg)) {
252                                 /*
253                                         Insert a duplicate in arguments block,
254                                         make it the new phi arg,
255                                         set its register,
256                                         insert it into schedule,
257                                         pin it
258                                 */
259                                 ir_node *dupl  = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
260
261                                 /* this is commented out because it will fail in case of unknown float */
262 #if 0
263                                 ir_mode *m_phi = get_irn_mode(phi), *m_dupl = get_irn_mode(dupl);
264
265                                 /*
266                                         Conv signed <-> unsigned is killed on ia32
267                                         check for: (both int OR both float) AND equal mode sizes
268                                 */
269                                 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
270                                         (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
271                                         (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
272 #endif /* if 0 */
273
274                                 set_irn_n(phi, i, dupl);
275                                 set_reg(dupl, phi_reg);
276                                 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, chordal_env->birg->main_env->arch_env), dupl);
277                                 pin_irn(dupl, phi_block);
278                                 DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
279                                 continue; /* with next argument */
280                         }
281
282                         if (phi_reg == arg_reg) {
283                                 /* Phi and arg have the same register, so pin and continue */
284                                 pin_irn(arg, phi_block);
285                                 DBG((dbg, LEVEL_1, "      arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
286                                 continue;
287                         }
288
289                         DBG((dbg, LEVEL_1, "    they do not interfere\n"));
290                         assert(is_Proj(arg));
291                         /*
292                                 First check if there is an other phi
293                                 - in the same block
294                                 - having arg at the current pos in its arg-list
295                                 - having the same color as arg
296
297                                 If found, then pin the arg (for that phi)
298                         */
299                         if (! is_pinned(arg)) {
300                                 ir_node *other_phi;
301
302                                 DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
303
304                                 for(other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
305
306                                         assert(is_Phi(other_phi)                               &&
307                                                 get_nodes_block(phi) == get_nodes_block(other_phi) &&
308                                                 "link fields are screwed up");
309
310                                         if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
311                                                 DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
312                                                 pin_irn(arg, phi_block);
313                                                 break;
314                                         }
315                                 }
316                         }
317
318                         if (is_pinned(arg)) {
319                                 /*
320                                         Insert a duplicate of the original value in arguments block,
321                                         make it the new phi arg,
322                                         set its register,
323                                         insert it into schedule,
324                                         pin it
325                                 */
326                                 ir_node *perm     = get_Proj_pred(arg);
327                                 ir_node *dupl     = be_new_Copy(cls, chordal_env->irg, arg_block, arg);
328                                 ir_node *ins;
329
330                                 /* this is commented out because it will fail in case of unknown float */
331 #if 0
332                                 ir_mode *m_phi    = get_irn_mode(phi);
333                                 ir_mode *m_dupl   = get_irn_mode(dupl);
334
335                                 /*
336                                         Conv signed <-> unsigned is killed on ia32
337                                         check for: (both int OR both float) AND equal mode sizes
338                                 */
339                                 assert(((mode_is_int(m_phi) && mode_is_int(m_dupl)) ||
340                                         (mode_is_float(m_phi) && mode_is_float(m_dupl))) &&
341                                         (get_mode_size_bits(m_phi) == get_mode_size_bits(m_dupl)));
342 #endif /* if 0 */
343
344                                 set_irn_n(phi, i, dupl);
345                                 set_reg(dupl, phi_reg);
346                                 /* skip the Perm's Projs and insert the copies behind. */
347                                 for(ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins));
348                                 sched_add_before(ins, dupl);
349                                 pin_irn(dupl, phi_block);
350                                 DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
351                         } else {
352                                 /*
353                                         No other phi has the same color (else arg would have been pinned),
354                                         so just set the register and pin
355                                 */
356                                 set_reg(arg, phi_reg);
357                                 pin_irn(arg, phi_block);
358                                 DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
359                         }
360                 }
361         }
362 }
363
364 void be_ssa_destruction(be_chordal_env_t *chordal_env) {
365         pmap *perm_map = pmap_create();
366         ir_graph *irg  = chordal_env->irg;
367         insert_all_perms_env_t insert_perms_env;
368
369         FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
370
371         be_assure_liveness(chordal_env->birg);
372
373         /* create a map for fast lookup of perms: block --> perm */
374         irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
375
376         DBG((dbg, LEVEL_1, "Placing perms...\n"));
377         insert_perms_env.chordal_env = chordal_env;
378         insert_perms_env.perm_map = perm_map;
379         irg_block_walk_graph(irg, insert_all_perms_walker, NULL, &insert_perms_env);
380
381         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
382                 be_dump(irg, "-ssa_destr_perms_placed", dump_ir_block_graph_sched);
383
384         // Matze: really needed here?
385         be_invalidate_liveness(chordal_env->birg);
386         be_assure_liveness(chordal_env->birg);
387
388         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
389         irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
390
391         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
392                 be_dump(irg, "-ssa_destr_regs_set", dump_ir_block_graph_sched);
393
394         pmap_destroy(perm_map);
395 }
396
397 static void ssa_destruction_check_walker(ir_node *bl, void *data) {
398         be_chordal_env_t *chordal_env = data;
399         ir_node *phi;
400         int i, max;
401
402         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
403                 const arch_register_t *phi_reg, *arg_reg;
404
405                 phi_reg = get_reg(phi);
406                 /* iterate over all args of phi */
407                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
408                         ir_node *arg = get_irn_n(phi, i);
409
410                         arg_reg = get_reg(arg);
411
412                         if (phi_reg != arg_reg) {
413                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
414                                 assert(0);
415                         }
416
417                         if (! is_pinned(arg)) {
418                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
419                                 assert(0);
420                         }
421                 }
422         }
423 }
424
425 void be_ssa_destruction_check(be_chordal_env_t *chordal_env) {
426         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, chordal_env);
427 }