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