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