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