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