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