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