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