make firm compilable with a c++ compiler
[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 "bessadestr.h"
30
31 #include "debug.h"
32 #include "set.h"
33 #include "pmap.h"
34 #include "irnode_t.h"
35 #include "ircons_t.h"
36 #include "iredges_t.h"
37 #include "irgwalk.h"
38 #include "irgmod.h"
39 #include "irdump.h"
40 #include "irprintf.h"
41
42 #include "be_t.h"
43 #include "beutil.h"
44 #include "bechordal_t.h"
45 #include "bearch.h"
46 #include "belive_t.h"
47 #include "benode.h"
48 #include "besched.h"
49 #include "bestatevent.h"
50 #include "beirg.h"
51 #include "beintlive_t.h"
52
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54
55 static void clear_link(ir_node *irn, void *data)
56 {
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 {
69         be_chordal_env_t *env = (be_chordal_env_t*)data;
70         if (is_Phi(irn) && chordal_has_class(env, irn)) {
71                 ir_node *bl = get_nodes_block(irn);
72                 set_irn_link(irn, get_irn_link(bl));
73                 set_irn_link(bl, irn);
74         }
75 }
76
77 /**
78  * This struct represents a Proj for a Perm.
79  * It records the argument in the Perm and the corresponding Proj of the
80  * Perm.
81  */
82 typedef struct {
83         ir_node *arg;  /**< The phi argument to make the Proj for. */
84         int pos;       /**< The proj number the Proj will get.
85                                                                          This also denotes the position of @p arg
86                                                                          in the in array of the Perm. */
87         ir_node *proj; /**< The proj created for @p arg. */
88 } perm_proj_t;
89
90 static int cmp_perm_proj(const void *a, const void *b, size_t n)
91 {
92         const perm_proj_t *p = (const perm_proj_t*)a;
93         const perm_proj_t *q = (const perm_proj_t*)b;
94         (void) n;
95
96         return !(p->arg == q->arg);
97 }
98
99 typedef struct insert_all_perms_env_t {
100         be_chordal_env_t *chordal_env;
101         pmap *perm_map;
102 } insert_all_perms_env_t;
103
104 /**
105  * Insert Perms in all predecessors of a block containing a phi
106  */
107 static void insert_all_perms_walker(ir_node *bl, void *data)
108 {
109         insert_all_perms_env_t *env = (insert_all_perms_env_t*)data;
110         be_chordal_env_t *chordal_env = env->chordal_env;
111         pmap *perm_map = env->perm_map;
112         be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
113         int i, n;
114
115         assert(is_Block(bl));
116
117         /* If the link flag is NULL, this block has no phis. */
118         if (!get_irn_link(bl))
119                 return;
120
121         /* Look at all predecessors of the phi block */
122         for (i = 0, n = get_irn_arity(bl); i < n; ++i) {
123                 ir_node *phi, *perm, *insert_after, **in;
124                 perm_proj_t *pp;
125                 set *arg_set     = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
126                 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
127                 int n_projs      = 0;
128
129                 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
130
131                 /*
132                  * Note that all phis in the list are in the same
133                  * register class by construction.
134                  */
135                 for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
136                      phi = (ir_node*)get_irn_link(phi)) {
137                         ir_node                   *arg = get_irn_n(phi, i);
138                         unsigned                   hash;
139                         perm_proj_t                templ;
140
141                         hash = hash_irn(arg);
142                         templ.arg  = arg;
143                         pp         = (perm_proj_t*)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 later 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 = XMALLOCN(ir_node*, n_projs);
165                         foreach_set(arg_set, perm_proj_t*, pp) {
166                                 in[pp->pos] = pp->arg;
167                         }
168
169                         perm = be_new_Perm(chordal_env->cls, pred_bl, n_projs, in);
170                         be_stat_ev("phi_perm", n_projs);
171
172                         insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, NULL);
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                         foreach_set(arg_set, perm_proj_t*, pp) {
182                                 ir_node *proj = new_r_Proj(perm, get_irn_mode(pp->arg), pp->pos);
183                                 pp->proj = proj;
184                                 assert(arch_get_irn_register(pp->arg));
185                                 arch_set_irn_register(proj, arch_get_irn_register(pp->arg));
186                                 insert_after = proj;
187                                 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", arch_get_irn_register(pp->arg)->name, pp->arg, pp->proj));
188                         }
189
190                         /*
191                          * Set the phi nodes to their new arguments: The Projs of the Perm
192                          */
193                         for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
194                              phi = (ir_node*)get_irn_link(phi)) {
195                                 perm_proj_t templ;
196
197                                 templ.arg = get_irn_n(phi, i);
198                                 pp        = (perm_proj_t*)set_find(arg_set, &templ, sizeof(templ), hash_irn(templ.arg));
199
200                                 /* If not found, it was an interfering argument */
201                                 if (pp) {
202                                         set_irn_n(phi, i, pp->proj);
203                                         be_liveness_introduce(lv, pp->proj);
204                                 }
205                         }
206
207                         /* update the liveness of the Perm's operands. It might be changed. */
208                         {
209                                 int i;
210                                 for (i = 0; i < n_projs; ++i)
211                                         be_liveness_update(lv, in[i]);
212                         }
213                         free(in);
214
215                         /* register in perm map */
216                         pmap_insert(perm_map, pred_bl, perm);
217                 }
218
219                 del_set(arg_set);
220         }
221 }
222
223 #define is_pinned(irn) (get_irn_link(irn))
224 #define get_pinning_block(irn) ((ir_node *)get_irn_link(irn))
225 #define pin_irn(irn, lock) (set_irn_link(irn, lock))
226
227 /**
228  * Adjusts the register allocation for the (new) phi-operands
229  * and insert duplicates iff necessary.
230  */
231 static void set_regs_or_place_dupls_walker(ir_node *bl, void *data)
232 {
233         be_chordal_env_t *chordal_env = (be_chordal_env_t*)data;
234         be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
235         ir_node *phi;
236
237         /* Consider all phis of this block */
238         for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
239              phi = (ir_node*)get_irn_link(phi)) {
240                 ir_node                     *phi_block = get_nodes_block(phi);
241                 const arch_register_t       *phi_reg   = arch_get_irn_register(phi);
242                 const arch_register_class_t *cls       = phi_reg->reg_class;
243                 int                          max;
244                 int                          i;
245
246                 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
247
248                 /* process all arguments of the phi */
249                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
250                         ir_node                   *arg = get_irn_n(phi, i);
251                         const arch_register_t     *arg_reg;
252                         ir_node                   *arg_block;
253
254                         arg_block = get_Block_cfgpred_block(phi_block, i);
255                         arg_reg   = arch_get_irn_register(arg);
256
257                         assert(arg_reg && "Register must be set while placing perms");
258
259                         DBG((dbg, LEVEL_1, "  for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
260
261                         if (phi_reg == arg_reg
262                                         || (arg_reg->type & arch_register_type_joker)
263                                         || (arg_reg->type & arch_register_type_virtual)) {
264                                 /* Phi and arg have the same register, so pin and continue */
265                                 pin_irn(arg, phi_block);
266                                 DBG((dbg, LEVEL_1, "      arg has same reg: pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
267                                 continue;
268                         }
269
270                         if (be_values_interfere(lv, phi, arg)) {
271                                 /*
272                                         Insert a duplicate in arguments block,
273                                         make it the new phi arg,
274                                         set its register,
275                                         insert it into schedule,
276                                         pin it
277                                 */
278                                 ir_node *dupl  = be_new_Copy(cls, arg_block, arg);
279
280                                 set_irn_n(phi, i, dupl);
281                                 arch_set_irn_register(dupl, phi_reg);
282                                 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, NULL), dupl);
283                                 pin_irn(dupl, phi_block);
284                                 be_liveness_introduce(lv, dupl);
285                                 be_liveness_update(lv, arg);
286                                 DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
287                                 continue; /* with next argument */
288                         }
289
290                         DBG((dbg, LEVEL_1, "    they do not interfere\n"));
291                         assert(is_Proj(arg));
292                         /*
293                                 First check if there is an other phi
294                                 - in the same block
295                                 - having arg at the current pos in its arg-list
296                                 - having the same color as arg
297
298                                 If found, then pin the arg (for that phi)
299                         */
300                         if (! is_pinned(arg)) {
301                                 ir_node *other_phi;
302
303                                 DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
304
305                                 for (other_phi = (ir_node*)get_irn_link(phi_block);
306                                      other_phi != NULL;
307                                      other_phi = (ir_node*)get_irn_link(other_phi)) {
308
309                                         assert(is_Phi(other_phi)                               &&
310                                                 get_nodes_block(phi) == get_nodes_block(other_phi) &&
311                                                 "link fields are screwed up");
312
313                                         if (get_irn_n(other_phi, i) == arg && arch_get_irn_register(other_phi) == arg_reg) {
314                                                 DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, arch_get_irn_register(other_phi)->name));
315                                                 pin_irn(arg, phi_block);
316                                                 break;
317                                         }
318                                 }
319                         }
320
321                         if (is_pinned(arg)) {
322                                 /*
323                                         Insert a duplicate of the original value in arguments block,
324                                         make it the new phi arg,
325                                         set its register,
326                                         insert it into schedule,
327                                         pin it
328                                 */
329                                 ir_node *perm = get_Proj_pred(arg);
330                                 ir_node *dupl = be_new_Copy(cls, arg_block, arg);
331                                 ir_node *ins;
332
333                                 set_irn_n(phi, i, dupl);
334                                 arch_set_irn_register(dupl, phi_reg);
335                                 /* skip the Perm's Projs and insert the copies behind. */
336                                 for (ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins)) {
337                                 }
338                                 sched_add_before(ins, dupl);
339                                 pin_irn(dupl, phi_block);
340                                 be_liveness_introduce(lv, dupl);
341                                 be_liveness_update(lv, arg);
342                                 DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, arch_get_irn_register(dupl)->name));
343                         } else {
344                                 /*
345                                         No other phi has the same color (else arg would have been pinned),
346                                         so just set the register and pin
347                                 */
348                                 arch_set_irn_register(arg, phi_reg);
349                                 pin_irn(arg, phi_block);
350                                 DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, arch_get_irn_register(arg)->name));
351                         }
352                 }
353         }
354 }
355
356 void be_ssa_destruction(be_chordal_env_t *chordal_env)
357 {
358         insert_all_perms_env_t insert_perms_env;
359         pmap     *perm_map = pmap_create();
360         ir_graph *irg      = chordal_env->irg;
361         be_lv_t  *lv       = be_assure_liveness(irg);
362
363         FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
364
365         be_liveness_invalidate(lv);
366
367         /* create a map for fast lookup of perms: block --> perm */
368         irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
369
370         DBG((dbg, LEVEL_1, "Placing perms...\n"));
371         insert_perms_env.chordal_env = chordal_env;
372         insert_perms_env.perm_map = perm_map;
373         irg_block_walk_graph(irg, insert_all_perms_walker, NULL, &insert_perms_env);
374
375         // Matze: really needed here?
376         // Sebastian: Yes. the walker function uses interference.
377         be_liveness_invalidate(lv);
378
379         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
380                 dump_ir_graph(irg, "ssa_destr_perms_placed");
381
382         be_liveness_assure_chk(lv);
383
384         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
385         irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
386
387         /* TODO: unfortunately updating doesn't work yet. */
388         be_liveness_invalidate(lv);
389
390         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
391                 dump_ir_graph(irg, "ssa_destr_regs_set");
392
393         pmap_destroy(perm_map);
394 }
395
396 static void ssa_destruction_check_walker(ir_node *bl, void *data)
397 {
398         ir_node *phi;
399         int i, max;
400         (void)data;
401
402         for (phi = (ir_node*)get_irn_link(bl); phi != NULL;
403              phi = (ir_node*)get_irn_link(phi)) {
404                 const arch_register_t *phi_reg, *arg_reg;
405
406                 phi_reg = arch_get_irn_register(phi);
407                 /* iterate over all args of phi */
408                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
409                         ir_node                   *arg = get_irn_n(phi, i);
410                         const arch_register_req_t *req = arch_get_register_req_out(arg);
411
412                         if (req->type & arch_register_req_type_ignore)
413                                 continue;
414
415                         arg_reg = arch_get_irn_register(arg);
416
417                         if (phi_reg != arg_reg) {
418                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
419                                 assert(0);
420                         }
421
422                         if (! is_pinned(arg)) {
423                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
424                                 assert(0);
425                         }
426                 }
427         }
428 }
429
430 void be_ssa_destruction_check(be_chordal_env_t *chordal_env)
431 {
432         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, NULL);
433 }