27 #include "iredges_t.h"
29 #include "irprintf_t.h"
33 #include "bechordal_t.h"
35 #include "besched_t.h"
43 #define DBG_MODULE "firm.be.irgmod"
44 #define DBG_LEVEL SET_LEVEL_0
48 | _ \ ___ _ __ ___ (_)_ __ __ _ _ __ ___ ___
49 | | | |/ _ \| '_ ` _ \| | '_ \ / _` | '_ \ / __/ _ \
50 | |_| | (_) | | | | | | | | | | (_| | | | | (_| __/
51 |____/ \___/|_| |_| |_|_|_| |_|\__,_|_| |_|\___\___|
52 | ___| __ ___ _ __ | |_(_) ___ _ __ ___
53 | |_ | '__/ _ \| '_ \| __| |/ _ \ '__/ __|
54 | _|| | | (_) | | | | |_| | __/ | \__ \
55 |_| |_| \___/|_| |_|\__|_|\___|_| |___/
60 struct _dom_front_info_t {
65 * A wrapper for get_Block_idom.
66 * This function returns the block itself, if the block is the start
67 * block. Returning NULL would make any != comparison true which
68 * suggests, that the start block is dominated by some other node.
69 * @param bl The block.
70 * @return The immediate dominator of the block.
72 static INLINE ir_node *get_idom(ir_node *bl)
74 ir_node *idom = get_Block_idom(bl);
75 return idom == NULL ? bl : idom;
78 static void compute_df(ir_node *n, pmap *df_map)
81 const ir_edge_t *edge;
82 pset *df = pset_new_ptr_default();
84 /* Add local dominance frontiers */
85 foreach_block_succ(n, edge) {
86 ir_node *y = edge->src;
89 pset_insert_ptr(df, y);
93 * Go recursively down the dominance tree and add all blocks
94 * int the dominance frontiers of the children, which are not
95 * dominated by the given block.
97 for(c = get_Block_dominated_first(n); c; c = get_Block_dominated_next(c)) {
101 compute_df(c, df_map);
102 df_c = pmap_get(df_map, c);
104 for(w = pset_first(df_c); w; w = pset_next(df_c)) {
106 pset_insert_ptr(df, w);
110 pmap_insert(df_map, n, df);
114 dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
116 dom_front_info_t *info = xmalloc(sizeof(*info));
119 info->df_map = pmap_create();
121 compute_df(get_irg_start_block(irg), info->df_map);
126 void be_free_dominance_frontiers(dom_front_info_t *info)
130 for(ent = pmap_first(info->df_map); ent; ent = pmap_next(info->df_map))
131 del_pset(ent->value);
133 pmap_destroy(info->df_map);
137 pset *be_get_dominance_frontier(dom_front_info_t *info, ir_node *block)
139 return pmap_get(info->df_map, block);
142 static void determine_phi_blocks(pset *copies, pset *copy_blocks, pset *phi_blocks, dom_front_info_t *df_info)
145 pdeq *worklist = new_pdeq();
146 FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
149 * Fill the worklist queue and the rest of the orig blocks array.
151 for(bl = pset_first(copy_blocks); bl; bl = pset_next(copy_blocks)) {
152 pdeq_putr(worklist, bl);
155 while (!pdeq_empty(worklist)) {
156 ir_node *bl = pdeq_getl(worklist);
157 pset *df = be_get_dominance_frontier(df_info, bl);
161 DBG((dbg, LEVEL_3, "dom front of %+F\n", bl));
163 for (y = pset_first(df); y; y = pset_next(df))
164 DBG((dbg, LEVEL_3, "\t%+F\n", y))
167 for(y = pset_first(df); y; y = pset_next(df)) {
168 if(!pset_find_ptr(phi_blocks, y)) {
169 pset_insert_ptr(phi_blocks, y);
172 * Clear the link field of a possible phi block, since
173 * the possibly created phi will be stored there. See,
176 set_irn_link(y, NULL);
178 if(!pset_find_ptr(copy_blocks, y))
179 pdeq_putr(worklist, y);
194 / ___|___ _ __ ___| |_ _ __ _ _ ___| |_(_) ___ _ __
195 | | / _ \| '_ \/ __| __| '__| | | |/ __| __| |/ _ \| '_ \
196 | |__| (_) | | | \__ \ |_| | | |_| | (__| |_| | (_) | | | |
197 \____\___/|_| |_|___/\__|_| \__,_|\___|\__|_|\___/|_| |_|
202 * Find the copy of the given original node whose value is 'active'
205 * The usage is given as a node and a position. Initially, the given operand
206 * points to a node for which copies were introduced. We have to find
207 * the valid copy for this usage. This is done by traversing the
208 * dominance tree upwards. If the usage is a phi function, we start
209 * traversing from the predecessor block which corresponds to the phi
212 * @param usage The node which uses the original node.
213 * @param pos The position of the argument which corresponds to the original node.
214 * @param copies A set containing all node which are copies from the original node.
215 * @param copy_blocks A set containing all basic block in which copies of the original node are located.
216 * @param phis A set where all created phis are recorded.
217 * @param phi_blocks A set of all blocks where Phis shall be inserted (iterated dominance frontier).
218 * @param mode The mode for the Phi if one has to be created.
219 * @return The valid copy for usage.
221 static ir_node *search_def(ir_node *usage, int pos, pset *copies, pset *copy_blocks, pset *phis, pset *phi_blocks, ir_mode *mode)
225 FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
227 curr_bl = get_nodes_block(usage);
229 DBG((dbg, LEVEL_1, "Searching valid def for use %+F at pos %d\n", usage, pos));
231 * If the usage is in a phi node, search the copy in the
232 * predecessor denoted by pos.
235 curr_bl = get_Block_cfgpred_block(curr_bl, pos);
236 start_irn = sched_last(curr_bl);
238 start_irn = sched_prev(usage);
242 * Traverse the dominance tree upwards from the
243 * predecessor block of the usage.
245 while(curr_bl != NULL) {
248 * If this block contains a copy, search the block
249 * instruction by instruction.
251 if(pset_find_ptr(copy_blocks, curr_bl)) {
254 /* Look at each instruction from last to first. */
255 sched_foreach_reverse_from(start_irn, irn) {
257 /* Take the first copy we find. */
258 if(pset_find_ptr(copies, irn))
263 if(pset_find_ptr(phi_blocks, curr_bl)) {
264 ir_node *phi = get_irn_link(curr_bl);
267 int i, n_preds = get_irn_arity(curr_bl);
268 ir_graph *irg = get_irn_irg(curr_bl);
269 ir_node **ins = xmalloc(n_preds * sizeof(ins[0]));
271 for(i = 0; i < n_preds; ++i)
272 ins[i] = new_r_Bad(irg);
274 phi = new_r_Phi(irg, curr_bl, n_preds, ins, mode);
275 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, curr_bl));
277 set_irn_link(curr_bl, phi);
278 sched_add_after(curr_bl, phi);
281 for(i = 0; i < n_preds; ++i) {
282 ir_node *arg = search_def(phi, i, copies, copy_blocks, phis, phi_blocks, mode);
283 DBG((dbg, LEVEL_2, "\t\t%+F(%d) -> %+F\n", phi, i, arg));
284 set_irn_n(phi, i, arg);
288 pset_insert_ptr(phis, phi);
294 /* If were not done yet, look in the immediate dominator */
295 curr_bl = get_Block_idom(curr_bl);
297 start_irn = sched_last(curr_bl);
303 static void fix_usages(pset *copies, pset *copy_blocks, pset *phi_blocks, pset *phis, pset *ignore_uses)
314 FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
319 * Put all outs into an array.
320 * This is necessary, since the outs would be modified while
321 * iterating on them what could bring the outs module in trouble.
323 for (irn = pset_first(copies); irn; irn = pset_next(copies)) {
324 const ir_edge_t *edge;
325 foreach_out_edge(irn, edge) {
326 if (!pset_find_ptr(ignore_uses, get_edge_src_irn(edge))) {
328 tmp.irn = get_edge_src_irn(edge);
329 tmp.pos = get_edge_src_pos(edge);
330 obstack_grow(&obst, &tmp, sizeof(tmp));
335 outs = obstack_finish(&obst);
338 * Search the valid def for each out and set it.
340 for(i = 0; i < n_outs; ++i) {
341 ir_node *irn = outs[i].irn;
342 int pos = outs[i].pos;
343 ir_mode *mode = get_irn_mode(get_irn_n(irn, pos));
347 def = search_def(irn, pos, copies, copy_blocks, phis, phi_blocks, mode);
348 DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", irn, pos, def));
351 set_irn_n(irn, pos, def);
354 obstack_free(&obst, NULL);
358 * Remove phis which are not necessary.
359 * During place_phi_functions() phi functions are put on the dominance
360 * frontiers blindly. However some of them will never be used (these
361 * have at least one predecessor which is NULL, see search_def() for
362 * this case). Since place_phi_functions() enters them into the
363 * schedule, we have to remove them from there.
365 * @param copies The set of all copies made (including the phi functions).
367 static void remove_odd_phis(pset *copies, pset *unused_copies)
371 for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
376 assert(sched_is_scheduled(irn) && "phi must be scheduled");
377 for(i = 0, n = get_irn_arity(irn); i < n && !illegal; ++i)
378 illegal = get_irn_n(irn, i) == NULL;
385 for(irn = pset_first(unused_copies); irn; irn = pset_next(unused_copies)) {
390 void be_ssa_constr_phis_ignore(dom_front_info_t *info, be_lv_t *lv, int n, ir_node *nodes[], pset *phis, pset *ignore_uses)
392 pset *irns = pset_new_ptr(n);
395 for(i = 0; i < n; ++i)
396 pset_insert_ptr(irns, nodes[i]);
397 be_ssa_constr_set_phis_ignore(info, lv, irns, phis, ignore_uses);
401 void be_ssa_constr_ignore(dom_front_info_t *info, be_lv_t *lv, int n, ir_node *nodes[], pset *ignore_uses)
403 be_ssa_constr_phis_ignore(info, lv, n, nodes, NULL, ignore_uses);
406 void be_ssa_constr(dom_front_info_t *info, be_lv_t *lv, int n, ir_node *nodes[])
408 pset *empty_set = be_empty_set();
409 be_ssa_constr_ignore(info, lv, n, nodes, empty_set);
412 void be_ssa_constr_set_phis_ignore(dom_front_info_t *df, be_lv_t *lv, pset *nodes, pset *phis, pset *ignore_uses)
414 int n = pset_count(nodes);
415 pset *blocks = pset_new_ptr(n);
416 pset *phi_blocks = pset_new_ptr(n);
417 int save_optimize = get_optimize();
418 int save_normalize = get_opt_normalize();
419 int phis_set_created = 0;
420 FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, DBG_MODULE);
424 /* We need to collect the phi functions to compute their liveness. */
426 phis_set_created = 1;
427 phis = pset_new_ptr_default();
430 DBG((dbg, LEVEL_1, "Introducing following copies for:\n"));
433 for(irn = pset_first(nodes); irn; irn = pset_next(nodes)) {
434 ir_node *bl = get_nodes_block(irn);
435 pset_insert_ptr(blocks, bl);
436 DBG((dbg, LEVEL_1, "\t%+F in %+F\n", irn, bl));
440 * Disable optimization so that the phi functions do not
444 set_opt_normalize(0);
447 * Place the phi functions and reroute the usages.
449 determine_phi_blocks(nodes, blocks, phi_blocks, df);
450 fix_usages(nodes, blocks, phi_blocks, phis, ignore_uses);
452 /* reset the optimizations */
453 set_optimize(save_optimize);
454 set_opt_normalize(save_normalize);
456 del_pset(phi_blocks);
459 /* Recompute the liveness (if wanted) of the original nodes, the copies and the inserted phis. */
462 foreach_pset(nodes, irn)
463 be_liveness_update(lv, irn);
465 foreach_pset(phis, irn)
466 be_liveness_introduce(lv, irn);
468 be_liveness_recompute(lv);
472 /* Free the phi set of we created it. */
478 void be_ssa_constr_set_phis(dom_front_info_t *df, be_lv_t *lv, pset *nodes, pset *phis)
480 pset *empty_set = be_empty_set();
481 be_ssa_constr_set_phis_ignore(df, lv, nodes, phis, empty_set);
484 void be_ssa_constr_set_ignore(dom_front_info_t *df, be_lv_t *lv, pset *nodes, pset *ignore_uses)
486 be_ssa_constr_set_phis_ignore(df, lv, nodes, NULL, ignore_uses);
489 void be_ssa_constr_set(dom_front_info_t *info, be_lv_t *lv, pset *nodes)
491 pset *empty_set = be_empty_set();
492 be_ssa_constr_set_ignore(info, lv, nodes, empty_set);
497 |_ _|_ __ ___ ___ _ __| |_ | _ \ ___ _ __ _ __ ___
498 | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
499 | || | | \__ \ __/ | | |_ | __/ __/ | | | | | | |
500 |___|_| |_|___/\___|_| \__| |_| \___|_| |_| |_| |_|
504 ir_node *insert_Perm_after(const arch_env_t *arch_env,
506 const arch_register_class_t *cls,
507 dom_front_info_t *dom_front,
510 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
511 ir_graph *irg = get_irn_irg(bl);
512 pset *live = pset_new_ptr_default();
513 FIRM_DBG_REGISTER(firm_dbg_module_t *dbg, "be.node");
515 ir_node *curr, *irn, *perm, **nodes;
518 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
520 be_liveness_nodes_live_at(lv, arch_env, cls, pos, live);
522 n = pset_count(live);
529 nodes = xmalloc(n * sizeof(nodes[0]));
531 DBG((dbg, LEVEL_1, "live:\n"));
532 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
533 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
538 perm = be_new_Perm(cls, irg, bl, n, nodes);
539 sched_add_after(pos, perm);
543 for(i = 0; i < n; ++i) {
545 ir_node *perm_op = get_irn_n(perm, i);
546 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
548 ir_mode *mode = get_irn_mode(perm_op);
549 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
550 arch_set_irn_register(arch_env, proj, reg);
552 sched_add_after(curr, proj);
558 be_ssa_constr(dom_front, lv, 2, copies);
564 struct _elr_closure_t {
566 const be_chordal_env_t *cenv;
569 static void elr_split_walker(ir_node *bl, void *data)
571 struct _elr_closure_t *c = data;
572 const be_chordal_env_t *cenv = c->cenv;
573 const arch_env_t *aenv = cenv->birg->main_env->arch_env;
577 be_insn_env_init(&ie, cenv->birg, cenv->cls, &c->obst);
579 for(insn = be_scan_insn(&ie, sched_first(bl)); !is_Block(insn->irn); insn = be_scan_insn(&ie, insn->next_insn)) {
580 ir_node *pred = sched_prev(insn->irn);
581 ir_printf("curr: %+F next: %+F, prev: %+F\n", insn->irn, insn->next_insn, pred);
582 if(!is_Block(pred) && !is_Phi(insn->irn))
583 insert_Perm_after(aenv, cenv->lv, cenv->cls, cenv->dom_front, insn->irn);
587 void extreme_liverange_splitting(struct _be_chordal_env_t *cenv)
589 struct _elr_closure_t c;
592 obstack_init(&c.obst);
593 be_liveness_recompute(cenv->lv);
594 irg_block_walk_graph(cenv->irg, elr_split_walker, NULL, &c);
595 be_liveness_recompute(cenv->lv);
596 obstack_free(&c.obst, NULL);