20 #include "iredges_t.h"
25 #include "besched_t.h"
32 #define DBG_MODULE firm_dbg_register("firm.be.irgmod")
33 #define DBG_LEVEL SET_LEVEL_0
35 struct _dom_front_info_t {
40 * A wrapper for get_Block_idom.
41 * This function returns the block itself, if the block is the start
42 * block. Returning NULL would make any != comparison true which
43 * suggests, that the start block is dominated by some other node.
44 * @param bl The block.
45 * @return The immediate dominator of the block.
47 static INLINE ir_node *get_idom(ir_node *bl)
49 ir_node *idom = get_Block_idom(bl);
50 return idom == NULL ? bl : idom;
53 static void compute_df(ir_node *n, pmap *df_map)
56 const ir_edge_t *edge;
57 pset *df = pset_new_ptr_default();
59 /* Add local dominance frontiers */
60 foreach_block_succ(n, edge) {
61 ir_node *y = edge->src;
64 pset_insert_ptr(df, y);
68 * Go recursively down the dominance tree and add all blocks
69 * int the dominance frontiers of the children, which are not
70 * dominated by the given block.
72 for(c = get_Block_dominated_first(n); c; c = get_Block_dominated_next(c)) {
76 compute_df(c, df_map);
77 df_c = pmap_get(df_map, c);
79 for(w = pset_first(df_c); w; w = pset_next(df_c)) {
81 pset_insert_ptr(df, w);
85 pmap_insert(df_map, n, df);
89 dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
91 dom_front_info_t *info = malloc(sizeof(*info));
94 info->df_map = pmap_create();
95 compute_df(get_irg_start_block(irg), info->df_map);
100 void be_free_dominance_frontiers(dom_front_info_t *info)
104 for(ent = pmap_first(info->df_map); ent; ent = pmap_next(info->df_map))
105 del_pset(ent->value);
107 pmap_destroy(info->df_map);
111 pset *be_get_dominance_frontier(dom_front_info_t *info, ir_node *block)
113 return pmap_get(info->df_map, block);
116 static void determine_phi_blocks(ir_node *orig, pset *copies,
117 pset *copy_blocks, pset *phi_blocks, dom_front_info_t *df_info)
120 ir_node *orig_block = get_nodes_block(orig);
121 pdeq *worklist = new_pdeq();
122 firm_dbg_module_t *dbg = DBG_MODULE;
125 * Fill the worklist queue and the rest of the orig blocks array.
127 for(bl = pset_first(copy_blocks); bl; bl = pset_next(copy_blocks)) {
128 assert(block_dominates(orig_block, bl)
129 && "The block of the copy must be dominated by the block of the value");
131 pdeq_putr(worklist, bl);
134 while(!pdeq_empty(worklist)) {
135 ir_node *bl = pdeq_getl(worklist);
137 pset *df = be_get_dominance_frontier(df_info, bl);
139 DBG((dbg, LEVEL_3, "dom front of %+F\n", bl));
140 for(y = pset_first(df); y; y = pset_next(df))
141 DBG((dbg, LEVEL_3, "\t%+F\n", y));
143 for(y = pset_first(df); y; y = pset_next(df)) {
144 if(!pset_find_ptr(phi_blocks, y)) {
145 pset_insert_ptr(phi_blocks, y);
148 * Clear the link field of a possible phi block, since
149 * the possibly created phi will be stored there. See,
152 set_irn_link(y, NULL);
154 if(!pset_find_ptr(copy_blocks, y))
155 pdeq_putr(worklist, y);
165 * Find the copy of the given original node whose value is 'active'
168 * The usage is given as a node and a position. Initially, the given operand
169 * points to a node for which copies were introduced. We have to find
170 * the valid copy for this usage. This is done by travering the
171 * dominance tree upwards. If the usage is a phi function, we start
172 * traversing from the predecessor block which corresponds to the phi
175 * @param usage The node which uses the original node.
176 * @param pos The number of the argument which corresponds to the
178 * @param copy_blocks A set containing all basic block in which copies
179 * of the original node are located.
180 * @param copies A set containing all node which are copies from the
182 * @return The valid copy for usage.
184 static ir_node *search_def(ir_node *usage, int pos, pset *copies,
185 pset *copy_blocks, pset *phi_blocks, ir_mode *mode)
189 firm_dbg_module_t *dbg = DBG_MODULE;
191 curr_bl = get_nodes_block(usage);
194 DBG((dbg, LEVEL_1, "Searching valid def for use %+F at pos %d\n", usage, pos));
196 * If the usage is in a phi node, search the copy in the
197 * predecessor denoted by pos.
200 curr_bl = get_Block_cfgpred_block(curr_bl, pos);
201 start_irn = sched_last(curr_bl);
203 start_irn = sched_prev(usage);
207 * Traverse the dominance tree upwards from the
208 * predecessor block of the usage.
210 while(curr_bl != NULL) {
213 * If this block contains a copy, search the block
214 * instruction by instruction.
216 if(pset_find_ptr(copy_blocks, curr_bl)) {
219 /* Look at each instruction from last to first. */
220 sched_foreach_reverse_from(start_irn, irn) {
222 /* Take the first copy we find. */
223 if(pset_find_ptr(copies, irn))
228 if(pset_find_ptr(phi_blocks, curr_bl)) {
229 ir_node *phi = get_irn_link(curr_bl);
232 int i, n_preds = get_irn_arity(curr_bl);
233 ir_graph *irg = get_irn_irg(curr_bl);
234 ir_node **ins = malloc(n_preds * sizeof(ins[0]));
236 for(i = 0; i < n_preds; ++i)
237 ins[i] = new_r_Unknown(irg, mode);
239 phi = new_r_Phi(irg, curr_bl, n_preds, ins, mode);
240 DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, curr_bl));
242 set_irn_link(curr_bl, phi);
243 sched_add_after(curr_bl, phi);
246 for(i = 0; i < n_preds; ++i) {
247 ir_node *arg = search_def(phi, i, copies, copy_blocks, phi_blocks, mode);
248 DBG((dbg, LEVEL_2, "\t\t%+F(%d) -> %+F\n", phi, i, arg));
249 set_irn_n(phi, i, arg);
256 /* If were not done yet, look in the immediate dominator */
257 curr_bl = get_Block_idom(curr_bl);
259 start_irn = sched_last(curr_bl);
265 static void fix_usages(ir_node *orig, pset *copies, pset *copy_blocks,
266 pset *phi_blocks, pset *ignore_uses)
270 const ir_edge_t *edge;
271 ir_mode *mode = get_irn_mode(orig);
273 firm_dbg_module_t *dbg = DBG_MODULE;
280 /* Count the number of outs. */
281 foreach_out_edge(orig, edge)
282 n_outs += !pset_find_ptr(ignore_uses, get_edge_src_irn(edge));
285 * Put all outs into an array.
286 * This is neccessary, since the outs would be modified while
287 * interating on them what could bring the outs module in trouble.
289 outs = malloc(n_outs * sizeof(outs[0]));
290 foreach_out_edge(orig, edge) {
291 if(!pset_find_ptr(ignore_uses, get_edge_src_irn(edge))) {
292 outs[i].irn = get_edge_src_irn(edge);
293 outs[i].pos = get_edge_src_pos(edge);
299 * Search the valid def for each out and set it.
301 for(i = 0; i < n_outs; ++i) {
303 ir_node *irn = outs[i].irn;
304 int pos = outs[i].pos;
306 def = search_def(irn, pos, copies, copy_blocks, phi_blocks, mode);
307 DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", irn, pos, def));
310 set_irn_n(irn, pos, def);
317 * Remove phis which are not neccesary.
318 * During place_phi_functions() phi functions are put on the dominance
319 * frontiers blindly. However some of them will never be used (these
320 * have at least one predecessor which is NULL, see search_def() for
321 * this case). Since place_phi_functions() enters them into the
322 * schedule, we have to remove them from there.
324 * @param copies The set of all copies made (including the phi functions).
326 static void remove_odd_phis(pset *copies, pset *unused_copies)
330 for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
335 assert(sched_is_scheduled(irn) && "phi must be scheduled");
336 for(i = 0, n = get_irn_arity(irn); i < n && !illegal; ++i)
337 illegal = get_irn_n(irn, i) == NULL;
344 for(irn = pset_first(unused_copies); irn; irn = pset_next(unused_copies)) {
349 void be_introduce_copies_ignore(dom_front_info_t *info, ir_node *orig,
350 int n, ir_node *copy_nodes[], pset *ignore_uses)
352 pset *copies = pset_new_ptr(2 * n);
353 pset *copy_blocks = pset_new_ptr(2 * n);
354 pset *phi_blocks = pset_new_ptr(2 * n);
355 int save_optimize = get_optimize();
356 int save_normalize = get_opt_normalize();
357 firm_dbg_module_t *dbg = DBG_MODULE;
365 snprintf(buf, sizeof(buf), "-post-%d", ser++);
366 dump_ir_block_graph_sched(get_irn_irg(orig), buf);
370 firm_dbg_set_mask(dbg, DBG_LEVEL);
371 DBG((dbg, LEVEL_1, "Introducing following copies of %+F\n", orig));
374 pset_insert_ptr(copies, orig);
375 pset_insert_ptr(copy_blocks, get_nodes_block(orig));
378 * All phis using the original value are also copies of it
379 * and must be present in the copies set.
381 for(i = 0; i < n; ++i) {
383 " %+F in block %+F\n", copy_nodes[i], get_nodes_block(copy_nodes[i])));
384 pset_insert_ptr(copies, copy_nodes[i]);
385 pset_insert_ptr(copy_blocks, get_nodes_block(copy_nodes[i]));
389 * Disable optimization so that the phi functions do not
393 set_opt_normalize(0);
396 * Place the phi functions and reroute the usages.
398 determine_phi_blocks(orig, copies, copy_blocks, phi_blocks, info);
399 fix_usages(orig, copies, copy_blocks, phi_blocks, ignore_uses);
401 /* reset the optimizations */
402 set_optimize(save_optimize);
403 set_opt_normalize(save_normalize);
406 del_pset(phi_blocks);
407 del_pset(copy_blocks);
412 void be_introduce_copies(dom_front_info_t *info, ir_node *orig,
413 int n, ir_node *copy_nodes[])
415 static pset *empty_set = NULL;
418 empty_set = pset_new_ptr_default();
420 be_introduce_copies_ignore(info, orig, n, copy_nodes, empty_set);