Bugfixes
[libfirm] / ir / be / beirgmod.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdlib.h>
6
7 #include "hashptr.h"
8 #include "pdeq.h"
9 #include "pset.h"
10 #include "pmap.h"
11 #include "util.h"
12 #include "debug.h"
13
14 #include "irflag_t.h"
15 #include "ircons_t.h"
16 #include "irnode_t.h"
17 #include "ircons_t.h"
18 #include "irmode_t.h"
19 #include "irdom_t.h"
20 #include "iredges_t.h"
21 #include "irgopt.h"
22
23 #include "be_t.h"
24 #include "bearch.h"
25 #include "besched_t.h"
26 #include "belive_t.h"
27 #include "benode_t.h"
28 #include "beutil.h"
29
30 #include "beirgmod.h"
31
32 #define DBG_MODULE firm_dbg_register("firm.be.irgmod")
33 #define DBG_LEVEL SET_LEVEL_0
34
35 struct _dom_front_info_t {
36   pmap *df_map;
37 };
38
39 /**
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.
46  */
47 static INLINE ir_node *get_idom(ir_node *bl)
48 {
49   ir_node *idom = get_Block_idom(bl);
50   return idom == NULL ? bl : idom;
51 }
52
53 static void compute_df(ir_node *n, pmap *df_map)
54 {
55   ir_node *c;
56   const ir_edge_t *edge;
57   pset *df = pset_new_ptr_default();
58
59   /* Add local dominance frontiers */
60   foreach_block_succ(n, edge) {
61     ir_node *y = edge->src;
62
63     if(get_idom(y) != n)
64       pset_insert_ptr(df, y);
65   }
66
67   /*
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.
71    */
72   for(c = get_Block_dominated_first(n); c; c = get_Block_dominated_next(c)) {
73     pset *df_c;
74     ir_node *w;
75
76     compute_df(c, df_map);
77     df_c = pmap_get(df_map, c);
78
79     for(w = pset_first(df_c); w; w = pset_next(df_c)) {
80       if(get_idom(w) != n)
81         pset_insert_ptr(df, w);
82     }
83   }
84
85   pmap_insert(df_map, n, df);
86
87 }
88
89 dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
90 {
91   dom_front_info_t *info = malloc(sizeof(*info));
92
93   edges_assure(irg);
94   info->df_map = pmap_create();
95   compute_df(get_irg_start_block(irg), info->df_map);
96
97   return info;
98 }
99
100 void be_free_dominance_frontiers(dom_front_info_t *info)
101 {
102   pmap_entry *ent;
103
104   for(ent = pmap_first(info->df_map); ent; ent = pmap_next(info->df_map))
105     del_pset(ent->value);
106
107   pmap_destroy(info->df_map);
108   free(info);
109 }
110
111 pset *be_get_dominance_frontier(dom_front_info_t *info, ir_node *block)
112 {
113   return pmap_get(info->df_map, block);
114 }
115
116 static void determine_phi_blocks(ir_node *orig, pset *copies,
117     pset *copy_blocks, pset *phi_blocks, dom_front_info_t *df_info)
118 {
119         ir_node *bl;
120         ir_node *orig_block = get_nodes_block(orig);
121   pdeq *worklist = new_pdeq();
122   firm_dbg_module_t *dbg = DBG_MODULE;
123
124   /*
125    * Fill the worklist queue and the rest of the orig blocks array.
126    */
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");
130
131     pdeq_putr(worklist, bl);
132   }
133
134   while(!pdeq_empty(worklist)) {
135     ir_node *bl = pdeq_getl(worklist);
136     ir_node *y;
137     pset *df = be_get_dominance_frontier(df_info, bl);
138
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));
142
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);
146
147                                 /*
148                                  * Clear the link field of a possible phi block, since
149                                  * the possibly created phi will be stored there. See,
150                                  * search_def()
151                                  */
152                                 set_irn_link(y, NULL);
153
154                                 if(!pset_find_ptr(copy_blocks, y))
155                                         pdeq_putr(worklist, y);
156
157       }
158     }
159   }
160
161   del_pdeq(worklist);
162 }
163
164 /**
165  * Find the copy of the given original node whose value is 'active'
166  * at a usage.
167  *
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
173  * usage.
174  *
175  * @param usage The node which uses the original node.
176  * @param pos The number of the argument which corresponds to the
177  * original node.
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
181  * original node.
182  * @return The valid copy for usage.
183  */
184 static ir_node *search_def(ir_node *usage, int pos, pset *copies,
185                 pset *copy_blocks, pset *phi_blocks, ir_mode *mode)
186 {
187   ir_node *curr_bl;
188   ir_node *start_irn;
189   firm_dbg_module_t *dbg = DBG_MODULE;
190
191   curr_bl = get_nodes_block(usage);
192
193
194   DBG((dbg, LEVEL_1, "Searching valid def for use %+F at pos %d\n", usage, pos));
195   /*
196    * If the usage is in a phi node, search the copy in the
197    * predecessor denoted by pos.
198    */
199   if(is_Phi(usage)) {
200     curr_bl = get_Block_cfgpred_block(curr_bl, pos);
201     start_irn = sched_last(curr_bl);
202   } else {
203     start_irn = sched_prev(usage);
204   }
205
206   /*
207    * Traverse the dominance tree upwards from the
208    * predecessor block of the usage.
209    */
210   while(curr_bl != NULL) {
211
212     /*
213      * If this block contains a copy, search the block
214      * instruction by instruction.
215      */
216     if(pset_find_ptr(copy_blocks, curr_bl)) {
217       ir_node *irn;
218
219       /* Look at each instruction from last to first. */
220                         sched_foreach_reverse_from(start_irn, irn) {
221
222         /* Take the first copy we find. */
223         if(pset_find_ptr(copies, irn))
224           return irn;
225       }
226     }
227
228                 else if(pset_find_ptr(phi_blocks, curr_bl)) {
229                         ir_node *phi = get_irn_link(curr_bl);
230
231                         if(!phi) {
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]));
235
236                                 for(i = 0; i < n_preds; ++i)
237                                         ins[i] = new_r_Unknown(irg, mode);
238
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));
241
242                                 set_irn_link(curr_bl, phi);
243                                 sched_add_after(curr_bl, phi);
244                                 free(ins);
245
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);
250                                 }
251                         }
252
253                         return phi;
254                 }
255
256     /* If were not done yet, look in the immediate dominator */
257     curr_bl = get_Block_idom(curr_bl);
258     if(curr_bl)
259       start_irn = sched_last(curr_bl);
260   }
261
262   return NULL;
263 }
264
265 static void fix_usages(ir_node *orig, pset *copies, pset *copy_blocks,
266                 pset *phi_blocks, pset *ignore_uses)
267 {
268   int i = 0;
269   int n_outs = 0;
270   const ir_edge_t *edge;
271         ir_mode *mode = get_irn_mode(orig);
272
273   firm_dbg_module_t *dbg = DBG_MODULE;
274
275   struct {
276     ir_node *irn;
277     int pos;
278   } *outs;
279
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));
283
284   /*
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.
288    */
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);
294                         i += 1;
295                 }
296   }
297
298   /*
299    * Search the valid def for each out and set it.
300    */
301   for(i = 0; i < n_outs; ++i) {
302     ir_node *def;
303     ir_node *irn = outs[i].irn;
304     int pos = outs[i].pos;
305
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));
308
309     if(def != NULL)
310       set_irn_n(irn, pos, def);
311   }
312
313   free(outs);
314 }
315
316 /**
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.
323  *
324  * @param copies The set of all copies made (including the phi functions).
325  */
326 static void remove_odd_phis(pset *copies, pset *unused_copies)
327 {
328   ir_node *irn;
329
330   for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
331     if(is_Phi(irn)) {
332       int i, n;
333       int illegal = 0;
334
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;
338
339       if(illegal)
340         sched_remove(irn);
341     }
342   }
343
344   for(irn = pset_first(unused_copies); irn; irn = pset_next(unused_copies)) {
345                 sched_remove(irn);
346         }
347 }
348
349 void be_introduce_copies_ignore(dom_front_info_t *info, ir_node *orig,
350                 int n, ir_node *copy_nodes[], pset *ignore_uses)
351 {
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;
358   int i;
359
360 #if 0
361         {
362                 static int ser = 0;
363                 char buf[128];
364
365                 snprintf(buf, sizeof(buf), "-post-%d", ser++);
366                 dump_ir_block_graph_sched(get_irn_irg(orig), buf);
367         }
368 #endif
369
370   firm_dbg_set_mask(dbg, DBG_LEVEL);
371   DBG((dbg, LEVEL_1, "Introducing following copies of %+F\n", orig));
372
373   /* Fill the sets. */
374   pset_insert_ptr(copies, orig);
375   pset_insert_ptr(copy_blocks, get_nodes_block(orig));
376
377   /*
378    * All phis using the original value are also copies of it
379    * and must be present in the copies set.
380    */
381   for(i = 0; i < n; ++i) {
382     DBG((dbg, LEVEL_1,
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]));
386   }
387
388   /*
389    * Disable optimization so that the phi functions do not
390    * disappear.
391    */
392   set_optimize(0);
393   set_opt_normalize(0);
394
395   /*
396    * Place the phi functions and reroute the usages.
397    */
398   determine_phi_blocks(orig, copies, copy_blocks, phi_blocks, info);
399   fix_usages(orig, copies, copy_blocks, phi_blocks, ignore_uses);
400
401   /* reset the optimizations */
402   set_optimize(save_optimize);
403   set_opt_normalize(save_normalize);
404
405   del_pset(copies);
406   del_pset(phi_blocks);
407   del_pset(copy_blocks);
408
409
410 }
411
412 void be_introduce_copies(dom_front_info_t *info, ir_node *orig,
413                 int n, ir_node *copy_nodes[])
414 {
415         static pset *empty_set = NULL;
416
417         if(!empty_set)
418                 empty_set = pset_new_ptr_default();
419
420         be_introduce_copies_ignore(info, orig, n, copy_nodes, empty_set);
421 }