extend register allocation verifier to check alignment and wide constraints
[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 #define get_reg(irn)         arch_get_irn_register(irn)
56 #define set_reg(irn, reg)    arch_set_irn_register(irn, reg)
57
58 static void clear_link(ir_node *irn, void *data)
59 {
60         (void) data;
61         set_irn_link(irn, NULL);
62 }
63
64 /**
65  * For each block build a linked list of phis that
66  *  - are in that block
67  *  - have the current register class
68  * The list is rooted at get_irn_link(BB).
69  */
70 static void collect_phis_walker(ir_node *irn, void *data)
71 {
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 {
95         const perm_proj_t *p = a;
96         const perm_proj_t *q = b;
97         (void) n;
98
99         return !(p->arg == q->arg);
100 }
101
102 typedef struct insert_all_perms_env_t {
103         be_chordal_env_t *chordal_env;
104         pmap *perm_map;
105 } insert_all_perms_env_t;
106
107 /**
108  * Insert Perms in all predecessors of a block containing a phi
109  */
110 static void insert_all_perms_walker(ir_node *bl, void *data)
111 {
112         insert_all_perms_env_t *env = data;
113         be_chordal_env_t *chordal_env = env->chordal_env;
114         pmap *perm_map = env->perm_map;
115         be_lv_t *lv = be_get_irg_liveness(chordal_env->irg);
116         int i, n;
117
118         assert(is_Block(bl));
119
120         /* If the link flag is NULL, this block has no phis. */
121         if (!get_irn_link(bl))
122                 return;
123
124         /* Look at all predecessors of the phi block */
125         for (i = 0, n = get_irn_arity(bl); i < n; ++i) {
126                 ir_node *phi, *perm, *insert_after, **in;
127                 perm_proj_t *pp;
128                 set *arg_set     = new_set(cmp_perm_proj, chordal_env->cls->n_regs);
129                 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
130                 int n_projs      = 0;
131
132                 assert(!pmap_contains(perm_map, pred_bl) && "Already permed that block");
133
134                 /*
135                  * Note that all phis in the list are in the same
136                  * register class by construction.
137                  */
138                 for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
139                         ir_node                   *arg = get_irn_n(phi, i);
140                         unsigned                   hash;
141                         perm_proj_t                templ;
142
143                         hash = hash_irn(arg);
144                         templ.arg  = arg;
145                         pp         = set_find(arg_set, &templ, sizeof(templ), hash);
146
147                         /*
148                          * If a proj_perm_t entry has not been made in the argument set,
149                          * create one. The only restriction is, that the phi argument
150                          * may not be live in at the current block, since this argument
151                          * interferes with the phi and must thus not be member of a
152                          * Perm. A copy will be inserted for this argument later on.
153                          */
154                         if (!pp && !be_is_live_in(lv, bl, arg)) {
155                                 templ.pos = n_projs++;
156                                 set_insert(arg_set, &templ, sizeof(templ), hash);
157                         }
158                 }
159
160
161                 if (n_projs) {
162                         /*
163                          * Create a new Perm with the arguments just collected
164                          * above in the arg_set and insert it into the schedule.
165                          */
166                         in = XMALLOCN(ir_node*, n_projs);
167                         for (pp = set_first(arg_set); pp; pp = set_next(arg_set))
168                                 in[pp->pos] = pp->arg;
169
170                         perm = be_new_Perm(chordal_env->cls, pred_bl, n_projs, in);
171                         be_stat_ev("phi_perm", n_projs);
172
173                         insert_after = sched_skip(sched_last(pred_bl), 0, sched_skip_cf_predicator, NULL);
174                         sched_add_after(insert_after, perm);
175
176                         /*
177                          * Make the Projs for the Perm and insert into schedule.
178                          * Register allocation is copied from the former phi
179                          * arguments to the projs (new phi arguments).
180                          */
181                         insert_after = perm;
182                         for (pp = set_first(arg_set); pp; pp = set_next(arg_set)) {
183                                 ir_node *proj = new_r_Proj(perm, get_irn_mode(pp->arg), pp->pos);
184                                 pp->proj = proj;
185                                 assert(get_reg(pp->arg));
186                                 set_reg(proj, get_reg(pp->arg));
187                                 insert_after = proj;
188                                 DBG((dbg, LEVEL_2, "Copy register assignment %s from %+F to %+F\n", get_reg(pp->arg)->name, pp->arg, pp->proj));
189                         }
190
191                         /*
192                          * Set the phi nodes to their new arguments: The Projs of the Perm
193                          */
194                         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
195                                 perm_proj_t templ;
196
197                                 templ.arg = get_irn_n(phi, i);
198                                 pp        = 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 = 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 = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
239                 ir_node                     *phi_block = get_nodes_block(phi);
240                 const arch_register_t       *phi_reg   = get_reg(phi);
241                 const arch_register_class_t *cls       = phi_reg->reg_class;
242                 int                          max;
243                 int                          i;
244
245                 assert(is_Phi(phi) && "Can only handle phi-destruction :)");
246
247                 /* process all arguments of the phi */
248                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
249                         ir_node                   *arg = get_irn_n(phi, i);
250                         const arch_register_t     *arg_reg;
251                         ir_node                   *arg_block;
252
253                         arg_block = get_Block_cfgpred_block(phi_block, i);
254                         arg_reg   = get_reg(arg);
255
256                         assert(arg_reg && "Register must be set while placing perms");
257
258                         DBG((dbg, LEVEL_1, "  for %+F(%s) -- %+F(%s)\n", phi, phi_reg->name, arg, arg_reg->name));
259
260                         if (phi_reg == arg_reg
261                                         || (arg_reg->type & arch_register_type_joker)
262                                         || (arg_reg->type & arch_register_type_virtual)) {
263                                 /* Phi and arg have the same register, so pin and continue */
264                                 pin_irn(arg, phi_block);
265                                 DBG((dbg, LEVEL_1, "      arg has same reg: pin %+F(%s)\n", arg, get_reg(arg)->name));
266                                 continue;
267                         }
268
269                         if (be_values_interfere(lv, phi, arg)) {
270                                 /*
271                                         Insert a duplicate in arguments block,
272                                         make it the new phi arg,
273                                         set its register,
274                                         insert it into schedule,
275                                         pin it
276                                 */
277                                 ir_node *dupl  = be_new_Copy(cls, arg_block, arg);
278
279                                 set_irn_n(phi, i, dupl);
280                                 set_reg(dupl, phi_reg);
281                                 sched_add_after(sched_skip(sched_last(arg_block), 0, sched_skip_cf_predicator, NULL), dupl);
282                                 pin_irn(dupl, phi_block);
283                                 be_liveness_introduce(lv, dupl);
284                                 be_liveness_update(lv, arg);
285                                 DBG((dbg, LEVEL_1, "    they do interfere: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
286                                 continue; /* with next argument */
287                         }
288
289                         DBG((dbg, LEVEL_1, "    they do not interfere\n"));
290                         assert(is_Proj(arg));
291                         /*
292                                 First check if there is an other phi
293                                 - in the same block
294                                 - having arg at the current pos in its arg-list
295                                 - having the same color as arg
296
297                                 If found, then pin the arg (for that phi)
298                         */
299                         if (! is_pinned(arg)) {
300                                 ir_node *other_phi;
301
302                                 DBG((dbg, LEVEL_1, "      searching for phi with same arg having args register\n"));
303
304                                 for (other_phi = get_irn_link(phi_block); other_phi; other_phi = get_irn_link(other_phi)) {
305
306                                         assert(is_Phi(other_phi)                               &&
307                                                 get_nodes_block(phi) == get_nodes_block(other_phi) &&
308                                                 "link fields are screwed up");
309
310                                         if (get_irn_n(other_phi, i) == arg && get_reg(other_phi) == arg_reg) {
311                                                 DBG((dbg, LEVEL_1, "        found %+F(%s)\n", other_phi, get_reg(other_phi)->name));
312                                                 pin_irn(arg, phi_block);
313                                                 break;
314                                         }
315                                 }
316                         }
317
318                         if (is_pinned(arg)) {
319                                 /*
320                                         Insert a duplicate of the original value in arguments block,
321                                         make it the new phi arg,
322                                         set its register,
323                                         insert it into schedule,
324                                         pin it
325                                 */
326                                 ir_node *perm = get_Proj_pred(arg);
327                                 ir_node *dupl = be_new_Copy(cls, arg_block, arg);
328                                 ir_node *ins;
329
330                                 set_irn_n(phi, i, dupl);
331                                 set_reg(dupl, phi_reg);
332                                 /* skip the Perm's Projs and insert the copies behind. */
333                                 for (ins = sched_next(perm); is_Proj(ins); ins = sched_next(ins)) {
334                                 }
335                                 sched_add_before(ins, dupl);
336                                 pin_irn(dupl, phi_block);
337                                 be_liveness_introduce(lv, dupl);
338                                 be_liveness_update(lv, arg);
339                                 DBG((dbg, LEVEL_1, "      arg is pinned: insert %+F(%s)\n", dupl, get_reg(dupl)->name));
340                         } else {
341                                 /*
342                                         No other phi has the same color (else arg would have been pinned),
343                                         so just set the register and pin
344                                 */
345                                 set_reg(arg, phi_reg);
346                                 pin_irn(arg, phi_block);
347                                 DBG((dbg, LEVEL_1, "      arg is not pinned: so pin %+F(%s)\n", arg, get_reg(arg)->name));
348                         }
349                 }
350         }
351 }
352
353 void be_ssa_destruction(be_chordal_env_t *chordal_env)
354 {
355         insert_all_perms_env_t insert_perms_env;
356         pmap     *perm_map = pmap_create();
357         ir_graph *irg      = chordal_env->irg;
358         be_lv_t  *lv       = be_assure_liveness(irg);
359
360         FIRM_DBG_REGISTER(dbg, "ir.be.ssadestr");
361
362         be_liveness_invalidate(lv);
363
364         /* create a map for fast lookup of perms: block --> perm */
365         irg_walk_graph(irg, clear_link, collect_phis_walker, chordal_env);
366
367         DBG((dbg, LEVEL_1, "Placing perms...\n"));
368         insert_perms_env.chordal_env = chordal_env;
369         insert_perms_env.perm_map = perm_map;
370         irg_block_walk_graph(irg, insert_all_perms_walker, NULL, &insert_perms_env);
371
372         // Matze: really needed here?
373         // Sebastian: Yes. the walker function uses interference.
374         be_liveness_invalidate(lv);
375
376         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
377                 dump_ir_graph(irg, "ssa_destr_perms_placed");
378
379         be_liveness_assure_chk(lv);
380
381         DBG((dbg, LEVEL_1, "Setting regs and placing dupls...\n"));
382         irg_block_walk_graph(irg, set_regs_or_place_dupls_walker, NULL, chordal_env);
383
384         /* TODO: unfortunately updating doesn't work yet. */
385         be_liveness_invalidate(lv);
386
387         if (chordal_env->opts->dump_flags & BE_CH_DUMP_SSADESTR)
388                 dump_ir_graph(irg, "ssa_destr_regs_set");
389
390         pmap_destroy(perm_map);
391 }
392
393 static void ssa_destruction_check_walker(ir_node *bl, void *data)
394 {
395         ir_node *phi;
396         int i, max;
397         (void)data;
398
399         for (phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
400                 const arch_register_t *phi_reg, *arg_reg;
401
402                 phi_reg = get_reg(phi);
403                 /* iterate over all args of phi */
404                 for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
405                         ir_node                   *arg = get_irn_n(phi, i);
406                         const arch_register_req_t *req = arch_get_register_req_out(arg);
407
408                         if (req->type & arch_register_req_type_ignore)
409                                 continue;
410
411                         arg_reg = get_reg(arg);
412
413                         if (phi_reg != arg_reg) {
414                                 DBG((dbg, 0, "Error: Registers of %+F and %+F differ: %s %s\n", phi, arg, phi_reg->name, arg_reg->name));
415                                 assert(0);
416                         }
417
418                         if (! is_pinned(arg)) {
419                                 DBG((dbg, 0, "Warning: Phi argument %+F is not pinned.\n", arg));
420                                 assert(0);
421                         }
422                 }
423         }
424 }
425
426 void be_ssa_destruction_check(be_chordal_env_t *chordal_env)
427 {
428         irg_block_walk_graph(chordal_env->irg, ssa_destruction_check_walker, NULL, NULL);
429 }