26 #include "iredges_t.h"
31 #include "besched_t.h"
38 #define DBG_MODULE firm_dbg_register("firm.be.irgmod")
39 #define DBG_LEVEL SET_LEVEL_0
43 | _ \ ___ _ __ ___ (_)_ __ __ _ _ __ ___ ___
44 | | | |/ _ \| '_ ` _ \| | '_ \ / _` | '_ \ / __/ _ \
45 | |_| | (_) | | | | | | | | | | (_| | | | | (_| __/
46 |____/ \___/|_| |_| |_|_|_| |_|\__,_|_| |_|\___\___|
47 | ___| __ ___ _ __ | |_(_) ___ _ __ ___
48 | |_ | '__/ _ \| '_ \| __| |/ _ \ '__/ __|
49 | _|| | | (_) | | | | |_| | __/ | \__ \
50 |_| |_| \___/|_| |_|\__|_|\___|_| |___/
55 struct _dom_front_info_t {
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.
67 static INLINE ir_node *get_idom(ir_node *bl)
69 ir_node *idom = get_Block_idom(bl);
70 return idom == NULL ? bl : idom;
73 static void compute_df(ir_node *n, pmap *df_map)
76 const ir_edge_t *edge;
77 pset *df = pset_new_ptr_default();
79 /* Add local dominance frontiers */
80 foreach_block_succ(n, edge) {
81 ir_node *y = edge->src;
84 pset_insert_ptr(df, y);
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.
92 for(c = get_Block_dominated_first(n); c; c = get_Block_dominated_next(c)) {
96 compute_df(c, df_map);
97 df_c = pmap_get(df_map, c);
99 for(w = pset_first(df_c); w; w = pset_next(df_c)) {
101 pset_insert_ptr(df, w);
105 pmap_insert(df_map, n, df);
109 dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
111 dom_front_info_t *info = malloc(sizeof(*info));
114 info->df_map = pmap_create();
116 compute_df(get_irg_start_block(irg), info->df_map);
121 void be_free_dominance_frontiers(dom_front_info_t *info)
125 for(ent = pmap_first(info->df_map); ent; ent = pmap_next(info->df_map))
126 del_pset(ent->value);
128 pmap_destroy(info->df_map);
132 pset *be_get_dominance_frontier(dom_front_info_t *info, ir_node *block)
134 return pmap_get(info->df_map, block);
137 static void determine_phi_blocks(pset *copies, pset* copy_blocks, pset *phi_blocks, dom_front_info_t *df_info)
140 pdeq *worklist = new_pdeq();
141 firm_dbg_module_t *dbg = DBG_MODULE;
144 * Fill the worklist queue and the rest of the orig blocks array.
146 for(bl = pset_first(copy_blocks); bl; bl = pset_next(copy_blocks)) {
147 pdeq_putr(worklist, bl);
150 while(!pdeq_empty(worklist)) {
151 ir_node *bl = pdeq_getl(worklist);
152 pset *df = be_get_dominance_frontier(df_info, bl);
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));
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);
165 * Clear the link field of a possible phi block, since
166 * the possibly created phi will be stored there. See,
169 set_irn_link(y, NULL);
171 if(!pset_find_ptr(copy_blocks, y))
172 pdeq_putr(worklist, y);
188 / ___|___ _ __ ___| |_ _ __ _ _ ___| |_(_) ___ _ __
189 | | / _ \| '_ \/ __| __| '__| | | |/ __| __| |/ _ \| '_ \
190 | |__| (_) | | | \__ \ |_| | | |_| | (__| |_| | (_) | | | |
191 \____\___/|_| |_|___/\__|_| \__,_|\___|\__|_|\___/|_| |_|
196 * Find the copy of the given original node whose value is 'active'
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
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.
215 static ir_node *search_def(ir_node *usage, int pos, pset *copies, pset *copy_blocks, pset *phis, pset *phi_blocks, ir_mode *mode)
219 firm_dbg_module_t *dbg = DBG_MODULE;
221 curr_bl = get_nodes_block(usage);
223 DBG((dbg, LEVEL_1, "Searching valid def for use %+F at pos %d\n", usage, pos));
225 * If the usage is in a phi node, search the copy in the
226 * predecessor denoted by pos.
229 curr_bl = get_Block_cfgpred_block(curr_bl, pos);
230 start_irn = sched_last(curr_bl);
232 start_irn = sched_prev(usage);
236 * Traverse the dominance tree upwards from the
237 * predecessor block of the usage.
239 while(curr_bl != NULL) {
242 * If this block contains a copy, search the block
243 * instruction by instruction.
245 if(pset_find_ptr(copy_blocks, curr_bl)) {
248 /* Look at each instruction from last to first. */
249 sched_foreach_reverse_from(start_irn, irn) {
251 /* Take the first copy we find. */
252 if(pset_find_ptr(copies, irn))
257 if(pset_find_ptr(phi_blocks, curr_bl)) {
258 ir_node *phi = get_irn_link(curr_bl);
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]));
265 for(i = 0; i < n_preds; ++i)
266 ins[i] = new_r_Unknown(irg, mode);
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));
271 set_irn_link(curr_bl, phi);
272 sched_add_after(curr_bl, phi);
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);
282 pset_insert_ptr(phis, phi);
288 /* If were not done yet, look in the immediate dominator */
289 curr_bl = get_Block_idom(curr_bl);
291 start_irn = sched_last(curr_bl);
297 static void fix_usages(pset *copies, pset *copy_blocks, pset *phi_blocks, pset *phis, pset *ignore_uses)
299 firm_dbg_module_t *dbg = DBG_MODULE;
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.
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))) {
323 tmp.irn = get_edge_src_irn(edge);
324 tmp.pos = get_edge_src_pos(edge);
325 obstack_grow(&obst, &tmp, sizeof(tmp));
330 outs = obstack_finish(&obst);
333 * Search the valid def for each out and set it.
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));
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));
346 set_irn_n(irn, pos, def);
349 obstack_free(&obst, NULL);
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.
360 * @param copies The set of all copies made (including the phi functions).
362 static void remove_odd_phis(pset *copies, pset *unused_copies)
366 for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
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;
380 for(irn = pset_first(unused_copies); irn; irn = pset_next(unused_copies)) {
385 void be_ssa_constr_phis_ignore(dom_front_info_t *info, int n, ir_node *nodes[], pset *phis, pset *ignore_uses)
387 pset *irns = pset_new_ptr(n);
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);
396 void be_ssa_constr_ignore(dom_front_info_t *info, int n, ir_node *nodes[], pset *ignore_uses)
398 be_ssa_constr_phis_ignore(info, n, nodes, NULL, ignore_uses);
401 void be_ssa_constr(dom_front_info_t *info, int n, ir_node *nodes[])
403 pset *empty_set = be_empty_set();
404 assert(pset_count(empty_set) == 0);
405 be_ssa_constr_ignore(info, n, nodes, empty_set);
408 void be_ssa_constr_set_phis_ignore(dom_front_info_t *df, pset *nodes, pset *phis, pset *ignore_uses)
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;
419 firm_dbg_set_mask(dbg, DBG_LEVEL);
420 DBG((dbg, LEVEL_1, "Introducing following copies for:\n"));
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));
430 * Disable optimization so that the phi functions do not
434 set_opt_normalize(0);
437 * Place the phi functions and reroute the usages.
439 determine_phi_blocks(nodes, blocks, phi_blocks, df);
440 fix_usages(nodes, blocks, phi_blocks, phis, ignore_uses);
442 /* reset the optimizations */
443 set_optimize(save_optimize);
444 set_opt_normalize(save_normalize);
446 del_pset(phi_blocks);
451 void be_ssa_constr_set_phis(dom_front_info_t *df, pset *nodes, pset *phis)
453 pset *empty_set = be_empty_set();
454 assert(pset_count(empty_set) == 0);
456 be_ssa_constr_set_phis_ignore(df, nodes,phis, empty_set);
459 void be_ssa_constr_set_ignore(dom_front_info_t *df, pset *nodes, pset *ignore_uses)
461 be_ssa_constr_set_phis_ignore(df, nodes, NULL, ignore_uses);
464 void be_ssa_constr_set(dom_front_info_t *info, pset *nodes)
466 pset *empty_set = be_empty_set();
467 assert(pset_count(empty_set) == 0);
468 be_ssa_constr_set_ignore(info, nodes, empty_set);
473 |_ _|_ __ ___ ___ _ __| |_ | _ \ ___ _ __ _ __ ___
474 | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ '__| '_ ` _ \
475 | || | | \__ \ __/ | | |_ | __/ __/ | | | | | | |
476 |___|_| |_|___/\___|_| \__| |_| \___|_| |_| |_| |_|
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,
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");
490 ir_node *curr, *irn, *perm, **nodes;
493 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
495 if(!be_liveness_nodes_live_at(arch_env, cls, pos, live));
497 n = pset_count(live);
502 nodes = malloc(n * sizeof(nodes[0]));
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));
510 perm = be_new_Perm(cls, irg, bl, n, nodes);
511 sched_add_after(pos, perm);
515 for(i = 0; i < n; ++i) {
517 ir_node *perm_op = get_irn_n(perm, i);
518 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
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);
524 sched_add_after(curr, proj);
529 be_ssa_constr(dom_front, 2, copies);