Corrected setting register classes of Return and Barrier nodes
[libfirm] / ir / be / beirgmod.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdlib.h>
6
7 #ifdef WIN32
8 #include <malloc.h>
9 #else
10 #include <alloca.h>
11 #endif
12
13 #include "hashptr.h"
14 #include "pdeq.h"
15 #include "pset.h"
16 #include "pmap.h"
17 #include "util.h"
18 #include "debug.h"
19
20 #include "irflag_t.h"
21 #include "ircons_t.h"
22 #include "irnode_t.h"
23 #include "ircons_t.h"
24 #include "irmode_t.h"
25 #include "irdom_t.h"
26 #include "iredges_t.h"
27 #include "irgopt.h"
28
29 #include "be_t.h"
30 #include "bearch.h"
31 #include "besched_t.h"
32 #include "belive_t.h"
33 #include "benode_t.h"
34 #include "beutil.h"
35
36 #include "beirgmod.h"
37
38 #define DBG_MODULE firm_dbg_register("firm.be.irgmod")
39 #define DBG_LEVEL SET_LEVEL_0
40
41 /*
42   ____                  _
43  |  _ \  ___  _ __ ___ (_)_ __   __ _ _ __   ___ ___
44  | | | |/ _ \| '_ ` _ \| | '_ \ / _` | '_ \ / __/ _ \
45  | |_| | (_) | | | | | | | | | | (_| | | | | (_|  __/
46  |____/ \___/|_| |_| |_|_|_| |_|\__,_|_| |_|\___\___|
47  |  ___| __ ___  _ __ | |_(_) ___ _ __ ___
48  | |_ | '__/ _ \| '_ \| __| |/ _ \ '__/ __|
49  |  _|| | | (_) | | | | |_| |  __/ |  \__ \
50  |_|  |_|  \___/|_| |_|\__|_|\___|_|  |___/
51
52 */
53
54
55 struct _dom_front_info_t {
56   pmap *df_map;
57 };
58
59 /**
60  * A wrapper for get_Block_idom.
61  * This function returns the block itself, if the block is the start
62  * block. Returning NULL would make any != comparison true which
63  * suggests, that the start block is dominated by some other node.
64  * @param bl The block.
65  * @return The immediate dominator of the block.
66  */
67 static INLINE ir_node *get_idom(ir_node *bl)
68 {
69   ir_node *idom = get_Block_idom(bl);
70   return idom == NULL ? bl : idom;
71 }
72
73 static void compute_df(ir_node *n, pmap *df_map)
74 {
75   ir_node *c;
76   const ir_edge_t *edge;
77   pset *df = pset_new_ptr_default();
78
79   /* Add local dominance frontiers */
80   foreach_block_succ(n, edge) {
81     ir_node *y = edge->src;
82
83     if(get_idom(y) != n)
84       pset_insert_ptr(df, y);
85   }
86
87   /*
88    * Go recursively down the dominance tree and add all blocks
89    * int the dominance frontiers of the children, which are not
90    * dominated by the given block.
91    */
92   for(c = get_Block_dominated_first(n); c; c = get_Block_dominated_next(c)) {
93     pset *df_c;
94     ir_node *w;
95
96     compute_df(c, df_map);
97     df_c = pmap_get(df_map, c);
98
99     for(w = pset_first(df_c); w; w = pset_next(df_c)) {
100       if(get_idom(w) != n)
101         pset_insert_ptr(df, w);
102     }
103   }
104
105   pmap_insert(df_map, n, df);
106
107 }
108
109 dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
110 {
111   dom_front_info_t *info = malloc(sizeof(*info));
112
113   edges_assure(irg);
114   info->df_map = pmap_create();
115   compute_doms(irg);
116   compute_df(get_irg_start_block(irg), info->df_map);
117
118   return info;
119 }
120
121 void be_free_dominance_frontiers(dom_front_info_t *info)
122 {
123   pmap_entry *ent;
124
125   for(ent = pmap_first(info->df_map); ent; ent = pmap_next(info->df_map))
126     del_pset(ent->value);
127
128   pmap_destroy(info->df_map);
129   free(info);
130 }
131
132 pset *be_get_dominance_frontier(dom_front_info_t *info, ir_node *block)
133 {
134   return pmap_get(info->df_map, block);
135 }
136
137 static void determine_phi_blocks(pset *copies, pset* copy_blocks, pset *phi_blocks, dom_front_info_t *df_info)
138 {
139         ir_node *bl;
140         pdeq *worklist = new_pdeq();
141         firm_dbg_module_t *dbg = DBG_MODULE;
142
143         /*
144         * Fill the worklist queue and the rest of the orig blocks array.
145         */
146         for(bl = pset_first(copy_blocks); bl; bl = pset_next(copy_blocks)) {
147                 pdeq_putr(worklist, bl);
148         }
149
150         while(!pdeq_empty(worklist)) {
151                 ir_node *bl = pdeq_getl(worklist);
152                 pset *df    = be_get_dominance_frontier(df_info, bl);
153
154                 ir_node *y;
155
156                 DBG((dbg, LEVEL_3, "dom front of %+F\n", bl));
157                 for(y = pset_first(df); y; y = pset_next(df))
158                         DBG((dbg, LEVEL_3, "\t%+F\n", y));
159
160                 for(y = pset_first(df); y; y = pset_next(df)) {
161                         if(!pset_find_ptr(phi_blocks, y)) {
162                                 pset_insert_ptr(phi_blocks, y);
163
164                                 /*
165                                 * Clear the link field of a possible phi block, since
166                                 * the possibly created phi will be stored there. See,
167                                 * search_def()
168                                 */
169                                 set_irn_link(y, NULL);
170
171                                 if(!pset_find_ptr(copy_blocks, y))
172                                         pdeq_putr(worklist, y);
173
174                         }
175                 }
176         }
177
178         del_pdeq(worklist);
179 }
180
181 /*
182   ____ ____    _
183  / ___/ ___|  / \
184  \___ \___ \ / _ \
185   ___) |__) / ___ \
186  |____/____/_/   \_\
187    ____                _                   _   _
188   / ___|___  _ __  ___| |_ _ __ _   _  ___| |_(_) ___  _ __
189  | |   / _ \| '_ \/ __| __| '__| | | |/ __| __| |/ _ \| '_ \
190  | |__| (_) | | | \__ \ |_| |  | |_| | (__| |_| | (_) | | | |
191   \____\___/|_| |_|___/\__|_|   \__,_|\___|\__|_|\___/|_| |_|
192
193 */
194
195 /**
196  * Find the copy of the given original node whose value is 'active'
197  * at a usage.
198  *
199  * The usage is given as a node and a position. Initially, the given operand
200  * points to a node for which copies were introduced. We have to find
201  * the valid copy for this usage. This is done by travering the
202  * dominance tree upwards. If the usage is a phi function, we start
203  * traversing from the predecessor block which corresponds to the phi
204  * usage.
205  *
206  * @param usage       The node which uses the original node.
207  * @param pos         The position of the argument which corresponds to the original node.
208  * @param copies      A set containing all node which are copies from the original node.
209  * @param copy_blocks A set containing all basic block in which copies of the original node are located.
210  * @param phis        A set where all created phis are recorded.
211  * @param phi_blocks  A set of all blocks where Phis shall be inserted (iterated dominance frontier).
212  * @param mode        The mode for the Phi if one has to be created.
213  * @return            The valid copy for usage.
214  */
215 static ir_node *search_def(ir_node *usage, int pos, pset *copies, pset *copy_blocks, pset *phis, pset *phi_blocks, ir_mode *mode)
216 {
217         ir_node *curr_bl;
218         ir_node *start_irn;
219         firm_dbg_module_t *dbg = DBG_MODULE;
220
221         curr_bl = get_nodes_block(usage);
222
223         DBG((dbg, LEVEL_1, "Searching valid def for use %+F at pos %d\n", usage, pos));
224         /*
225         * If the usage is in a phi node, search the copy in the
226         * predecessor denoted by pos.
227         */
228         if(is_Phi(usage)) {
229                 curr_bl = get_Block_cfgpred_block(curr_bl, pos);
230                 start_irn = sched_last(curr_bl);
231         } else {
232                 start_irn = sched_prev(usage);
233         }
234
235         /*
236          * Traverse the dominance tree upwards from the
237          * predecessor block of the usage.
238          */
239         while(curr_bl != NULL) {
240
241             /*
242                  * If this block contains a copy, search the block
243                  * instruction by instruction.
244                  */
245                 if(pset_find_ptr(copy_blocks, curr_bl)) {
246                         ir_node *irn;
247
248                         /* Look at each instruction from last to first. */
249                         sched_foreach_reverse_from(start_irn, irn) {
250
251                                 /* Take the first copy we find. */
252                                 if(pset_find_ptr(copies, irn))
253                                         return irn;
254                         }
255                 }
256
257                 if(pset_find_ptr(phi_blocks, curr_bl)) {
258                         ir_node *phi = get_irn_link(curr_bl);
259
260                         if(!phi) {
261                                 int i, n_preds = get_irn_arity(curr_bl);
262                                 ir_graph *irg = get_irn_irg(curr_bl);
263                                 ir_node **ins = xmalloc(n_preds * sizeof(ins[0]));
264
265                                 for(i = 0; i < n_preds; ++i)
266                                         ins[i] = new_r_Unknown(irg, mode);
267
268                                 phi = new_r_Phi(irg, curr_bl, n_preds, ins, mode);
269                                 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, curr_bl));
270
271                                 set_irn_link(curr_bl, phi);
272                                 sched_add_after(curr_bl, phi);
273                                 free(ins);
274
275                                 for(i = 0; i < n_preds; ++i) {
276                                         ir_node *arg = search_def(phi, i, copies, copy_blocks, phis, phi_blocks, mode);
277                                         DBG((dbg, LEVEL_2, "\t\t%+F(%d) -> %+F\n", phi, i, arg));
278                                         set_irn_n(phi, i, arg);
279                                 }
280
281                                 if(phis)
282                                         pset_insert_ptr(phis, phi);
283                         }
284
285                         return phi;
286                 }
287
288                 /* If were not done yet, look in the immediate dominator */
289                 curr_bl = get_Block_idom(curr_bl);
290                 if(curr_bl)
291                         start_irn = sched_last(curr_bl);
292         }
293
294         return NULL;
295 }
296
297 static void fix_usages(pset *copies, pset *copy_blocks, pset *phi_blocks, pset *phis, pset *ignore_uses)
298 {
299         firm_dbg_module_t *dbg = DBG_MODULE;
300         int n_outs             = 0;
301
302         struct obstack obst;
303         ir_node *irn;
304         int i;
305
306         struct out {
307                 ir_node *irn;
308                 int pos;
309         } *outs;
310
311         obstack_init(&obst);
312
313         /*
314         * Put all outs into an array.
315         * This is necessary, since the outs would be modified while
316         * iterating on them what could bring the outs module in trouble.
317         */
318         for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
319                 const ir_edge_t *edge;
320                 foreach_out_edge(irn, edge) {
321                         if(!pset_find_ptr(ignore_uses, get_edge_src_irn(edge))) {
322                                 struct out tmp;
323                                 tmp.irn = get_edge_src_irn(edge);
324                                 tmp.pos = get_edge_src_pos(edge);
325                                 obstack_grow(&obst, &tmp, sizeof(tmp));
326                                 n_outs++;
327                         }
328                 }
329         }
330         outs = obstack_finish(&obst);
331
332         /*
333          * Search the valid def for each out and set it.
334          */
335         for(i = 0; i < n_outs; ++i) {
336                 ir_node *irn  = outs[i].irn;
337                 int pos       = outs[i].pos;
338                 ir_mode *mode = get_irn_mode(get_irn_n(irn, pos));
339
340                 ir_node *def;
341
342                 def = search_def(irn, pos, copies, copy_blocks, phis, phi_blocks, mode);
343                 DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", irn, pos, def));
344
345                 if(def != NULL)
346                         set_irn_n(irn, pos, def);
347         }
348
349         obstack_free(&obst, NULL);
350 }
351
352 /**
353  * Remove phis which are not necessary.
354  * During place_phi_functions() phi functions are put on the dominance
355  * frontiers blindly. However some of them will never be used (these
356  * have at least one predecessor which is NULL, see search_def() for
357  * this case). Since place_phi_functions() enters them into the
358  * schedule, we have to remove them from there.
359  *
360  * @param copies The set of all copies made (including the phi functions).
361  */
362 static void remove_odd_phis(pset *copies, pset *unused_copies)
363 {
364   ir_node *irn;
365
366   for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
367     if(is_Phi(irn)) {
368       int i, n;
369       int illegal = 0;
370
371       assert(sched_is_scheduled(irn) && "phi must be scheduled");
372       for(i = 0, n = get_irn_arity(irn); i < n && !illegal; ++i)
373         illegal = get_irn_n(irn, i) == NULL;
374
375       if(illegal)
376         sched_remove(irn);
377     }
378   }
379
380   for(irn = pset_first(unused_copies); irn; irn = pset_next(unused_copies)) {
381                 sched_remove(irn);
382         }
383 }
384
385 void be_ssa_constr_phis_ignore(dom_front_info_t *info, int n, ir_node *nodes[], pset *phis, pset *ignore_uses)
386 {
387         pset *irns = pset_new_ptr(n);
388         int i;
389
390         for(i = 0; i < n; ++i)
391                 pset_insert_ptr(irns, nodes[i]);
392         be_ssa_constr_set_phis_ignore(info, irns, phis, ignore_uses);
393         del_pset(irns);
394 }
395
396 void be_ssa_constr_ignore(dom_front_info_t *info, int n, ir_node *nodes[], pset *ignore_uses)
397 {
398         be_ssa_constr_phis_ignore(info, n, nodes, NULL, ignore_uses);
399 }
400
401 void be_ssa_constr(dom_front_info_t *info, int n, ir_node *nodes[])
402 {
403         pset *empty_set = be_empty_set();
404         assert(pset_count(empty_set) == 0);
405         be_ssa_constr_ignore(info, n, nodes, empty_set);
406 }
407
408 void be_ssa_constr_set_phis_ignore(dom_front_info_t *df, pset *nodes, pset *phis, pset *ignore_uses)
409 {
410         int n                  = pset_count(nodes);
411         pset *blocks           = pset_new_ptr(n);
412         pset *phi_blocks       = pset_new_ptr(n);
413         int save_optimize      = get_optimize();
414         int save_normalize     = get_opt_normalize();
415         firm_dbg_module_t *dbg = DBG_MODULE;
416
417         ir_node *irn;
418
419         firm_dbg_set_mask(dbg, DBG_LEVEL);
420         DBG((dbg, LEVEL_1, "Introducing following copies for:\n"));
421
422         /* Fill the sets. */
423         for(irn = pset_first(nodes); irn; irn = pset_next(nodes)) {
424                 ir_node *bl = get_nodes_block(irn);
425                 pset_insert_ptr(blocks, bl);
426                 DBG((dbg, LEVEL_1, "\t%+F in %+F\n", irn, bl));
427         }
428
429         /*
430         * Disable optimization so that the phi functions do not
431         * disappear.
432         */
433         set_optimize(0);
434         set_opt_normalize(0);
435
436         /*
437         * Place the phi functions and reroute the usages.
438         */
439         determine_phi_blocks(nodes, blocks, phi_blocks, df);
440         fix_usages(nodes, blocks, phi_blocks, phis, ignore_uses);
441
442         /* reset the optimizations */
443         set_optimize(save_optimize);
444         set_opt_normalize(save_normalize);
445
446         del_pset(phi_blocks);
447         del_pset(blocks);
448
449 }
450
451 void be_ssa_constr_set_phis(dom_front_info_t *df, pset *nodes, pset *phis)
452 {
453         pset *empty_set = be_empty_set();
454         assert(pset_count(empty_set) == 0);
455
456         be_ssa_constr_set_phis_ignore(df, nodes,phis, empty_set);
457 }
458
459 void be_ssa_constr_set_ignore(dom_front_info_t *df, pset *nodes, pset *ignore_uses)
460 {
461         be_ssa_constr_set_phis_ignore(df, nodes, NULL, ignore_uses);
462 }
463
464 void be_ssa_constr_set(dom_front_info_t *info, pset *nodes)
465 {
466         pset *empty_set = be_empty_set();
467         assert(pset_count(empty_set) == 0);
468         be_ssa_constr_set_ignore(info, nodes, empty_set);
469 }
470
471 /*
472   ___                     _     ____
473  |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___ _ __ _ __ ___
474   | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
475   | || | | \__ \  __/ |  | |_  |  __/  __/ |  | | | | | |
476  |___|_| |_|___/\___|_|   \__| |_|   \___|_|  |_| |_| |_|
477
478 */
479
480 ir_node *insert_Perm_after(const arch_env_t *arch_env,
481                                                    const arch_register_class_t *cls,
482                                                    dom_front_info_t *dom_front,
483                                                    ir_node *pos)
484 {
485         ir_node *bl                 = is_Block(pos) ? pos : get_nodes_block(pos);
486         ir_graph *irg               = get_irn_irg(bl);
487         pset *live                  = pset_new_ptr_default();
488         firm_dbg_module_t *dbg      = firm_dbg_register("be.node");
489
490         ir_node *curr, *irn, *perm, **nodes;
491         int i, n;
492
493         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
494
495         if(!be_liveness_nodes_live_at(arch_env, cls, pos, live));
496
497         n = pset_count(live);
498
499         if(n == 0)
500                 return NULL;
501
502         nodes = malloc(n * sizeof(nodes[0]));
503
504         DBG((dbg, LEVEL_1, "live:\n"));
505         for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
506                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
507                 nodes[i] = irn;
508         }
509
510         perm = be_new_Perm(cls, irg, bl, n, nodes);
511         sched_add_after(pos, perm);
512         free(nodes);
513
514         curr = perm;
515         for(i = 0; i < n; ++i) {
516                 ir_node *copies[2];
517                 ir_node *perm_op = get_irn_n(perm, i);
518                 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
519
520                 ir_mode *mode = get_irn_mode(perm_op);
521                 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
522                 arch_set_irn_register(arch_env, proj, reg);
523
524                 sched_add_after(curr, proj);
525                 curr = proj;
526
527                 copies[0] = perm_op;
528                 copies[1] = proj;
529                 be_ssa_constr(dom_front, 2, copies);
530         }
531         return perm;
532 }