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