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