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