1ff9db865de548971da50f71228e5f3eec0f7cf4
[libfirm] / ir / be / beirgmod.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdlib.h>
6
7 #ifdef WIN32
8 #include <malloc.h>
9 #else
10 #include <alloca.h>
11 #endif
12
13 #include "hashptr.h"
14 #include "pdeq.h"
15 #include "pset.h"
16 #include "pmap.h"
17 #include "util.h"
18 #include "debug.h"
19
20 #include "irflag_t.h"
21 #include "ircons_t.h"
22 #include "irnode_t.h"
23 #include "ircons_t.h"
24 #include "irmode_t.h"
25 #include "irdom_t.h"
26 #include "iredges_t.h"
27 #include "irgopt.h"
28
29 #include "be_t.h"
30 #include "bearch.h"
31 #include "besched_t.h"
32 #include "belive_t.h"
33 #include "benode_t.h"
34 #include "beutil.h"
35
36 #include "beirgmod.h"
37
38 #define DBG_MODULE firm_dbg_register("firm.be.irgmod")
39 #define DBG_LEVEL SET_LEVEL_0
40
41 struct _dom_front_info_t {
42   pmap *df_map;
43 };
44
45 /**
46  * A wrapper for get_Block_idom.
47  * This function returns the block itself, if the block is the start
48  * block. Returning NULL would make any != comparison true which
49  * suggests, that the start block is dominated by some other node.
50  * @param bl The block.
51  * @return The immediate dominator of the block.
52  */
53 static INLINE ir_node *get_idom(ir_node *bl)
54 {
55   ir_node *idom = get_Block_idom(bl);
56   return idom == NULL ? bl : idom;
57 }
58
59 static void compute_df(ir_node *n, pmap *df_map)
60 {
61   ir_node *c;
62   const ir_edge_t *edge;
63   pset *df = pset_new_ptr_default();
64
65   /* Add local dominance frontiers */
66   foreach_block_succ(n, edge) {
67     ir_node *y = edge->src;
68
69     if(get_idom(y) != n)
70       pset_insert_ptr(df, y);
71   }
72
73   /*
74    * Go recursively down the dominance tree and add all blocks
75    * int the dominance frontiers of the children, which are not
76    * dominated by the given block.
77    */
78   for(c = get_Block_dominated_first(n); c; c = get_Block_dominated_next(c)) {
79     pset *df_c;
80     ir_node *w;
81
82     compute_df(c, df_map);
83     df_c = pmap_get(df_map, c);
84
85     for(w = pset_first(df_c); w; w = pset_next(df_c)) {
86       if(get_idom(w) != n)
87         pset_insert_ptr(df, w);
88     }
89   }
90
91   pmap_insert(df_map, n, df);
92
93 }
94
95 dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
96 {
97   dom_front_info_t *info = malloc(sizeof(*info));
98
99   edges_assure(irg);
100   info->df_map = pmap_create();
101   compute_df(get_irg_start_block(irg), info->df_map);
102
103   return info;
104 }
105
106 void be_free_dominance_frontiers(dom_front_info_t *info)
107 {
108   pmap_entry *ent;
109
110   for(ent = pmap_first(info->df_map); ent; ent = pmap_next(info->df_map))
111     del_pset(ent->value);
112
113   pmap_destroy(info->df_map);
114   free(info);
115 }
116
117 pset *be_get_dominance_frontier(dom_front_info_t *info, ir_node *block)
118 {
119   return pmap_get(info->df_map, block);
120 }
121
122 static void determine_phi_blocks(pset *copies, pset *copy_blocks, pset *phi_blocks, dom_front_info_t *df_info)
123 {
124         ir_node *bl;
125         pdeq *worklist = new_pdeq();
126         firm_dbg_module_t *dbg = DBG_MODULE;
127
128         /*
129         * Fill the worklist queue and the rest of the orig blocks array.
130         */
131         for(bl = pset_first(copy_blocks); bl; bl = pset_next(copy_blocks)) {
132                 pdeq_putr(worklist, bl);
133         }
134
135         while(!pdeq_empty(worklist)) {
136                 ir_node *bl = pdeq_getl(worklist);
137                 pset *df    = be_get_dominance_frontier(df_info, bl);
138
139                 ir_node *y;
140
141                 DBG((dbg, LEVEL_3, "dom front of %+F\n", bl));
142                 for(y = pset_first(df); y; y = pset_next(df))
143                         DBG((dbg, LEVEL_3, "\t%+F\n", y));
144
145                 for(y = pset_first(df); y; y = pset_next(df)) {
146                         if(!pset_find_ptr(phi_blocks, y)) {
147                                 pset_insert_ptr(phi_blocks, y);
148
149                                 /*
150                                 * Clear the link field of a possible phi block, since
151                                 * the possibly created phi will be stored there. See,
152                                 * search_def()
153                                 */
154                                 set_irn_link(y, NULL);
155
156                                 if(!pset_find_ptr(copy_blocks, y))
157                                         pdeq_putr(worklist, y);
158
159                         }
160                 }
161         }
162
163         del_pdeq(worklist);
164 }
165
166 /**
167  * Find the copy of the given original node whose value is 'active'
168  * at a usage.
169  *
170  * The usage is given as a node and a position. Initially, the given operand
171  * points to a node for which copies were introduced. We have to find
172  * the valid copy for this usage. This is done by travering the
173  * dominance tree upwards. If the usage is a phi function, we start
174  * traversing from the predecessor block which corresponds to the phi
175  * usage.
176  *
177  * @param usage The node which uses the original node.
178  * @param pos The number of the argument which corresponds to the
179  * original node.
180  * @param copy_blocks A set containing all basic block in which copies
181  * of the original node are located.
182  * @param copies A set containing all node which are copies from the
183  * original node.
184  * @return The valid copy for usage.
185  */
186 static ir_node *search_def(ir_node *usage, int pos, pset *copies, pset *copy_blocks, pset *phi_blocks, ir_mode *mode)
187 {
188         ir_node *curr_bl;
189         ir_node *start_irn;
190         firm_dbg_module_t *dbg = DBG_MODULE;
191
192         curr_bl = get_nodes_block(usage);
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                 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(int n_origs, ir_node *orig[], pset *copies,
266                                            pset *copy_blocks, pset *phi_blocks, pset *ignore_uses)
267 {
268         firm_dbg_module_t *dbg = DBG_MODULE;
269         int n_outs             = 0;
270         ir_mode *mode          = get_irn_mode(orig[0]);
271
272         int i, j;
273
274         struct {
275                 ir_node *irn;
276                 int pos;
277         } *outs;
278
279         /* Count the number of outs. */
280         for(i = 0; i < n_origs; ++i) {
281                 const ir_edge_t *edge;
282                 foreach_out_edge(orig[i], edge)
283                         n_outs += !pset_find_ptr(ignore_uses, get_edge_src_irn(edge));
284         }
285
286         /*
287          * Put all outs into an array.
288          * This is necessary, since the outs would be modified while
289          * iterating on them what could bring the outs module in trouble.
290          */
291         outs = alloca(n_outs * sizeof(outs[0]));
292         for(i = 0, j = 0; i < n_origs; ++i) {
293                 const ir_edge_t *edge;
294                 foreach_out_edge(orig[i], edge) {
295                         if(!pset_find_ptr(ignore_uses, get_edge_src_irn(edge))) {
296                                 outs[j].irn = get_edge_src_irn(edge);
297                                 outs[j].pos = get_edge_src_pos(edge);
298                                 j += 1;
299                         }
300                 }
301         }
302
303         /*
304          * Search the valid def for each out and set it.
305          */
306         for(i = 0; i < n_outs; ++i) {
307                 ir_node *def;
308                 ir_node *irn = outs[i].irn;
309                 int pos      = outs[i].pos;
310
311                 def = search_def(irn, pos, copies, copy_blocks, phi_blocks, mode);
312                 DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", irn, pos, def));
313
314                 if(def != NULL)
315                         set_irn_n(irn, pos, def);
316         }
317 }
318
319 /**
320  * Remove phis which are not necessary.
321  * During place_phi_functions() phi functions are put on the dominance
322  * frontiers blindly. However some of them will never be used (these
323  * have at least one predecessor which is NULL, see search_def() for
324  * this case). Since place_phi_functions() enters them into the
325  * schedule, we have to remove them from there.
326  *
327  * @param copies The set of all copies made (including the phi functions).
328  */
329 static void remove_odd_phis(pset *copies, pset *unused_copies)
330 {
331   ir_node *irn;
332
333   for(irn = pset_first(copies); irn; irn = pset_next(copies)) {
334     if(is_Phi(irn)) {
335       int i, n;
336       int illegal = 0;
337
338       assert(sched_is_scheduled(irn) && "phi must be scheduled");
339       for(i = 0, n = get_irn_arity(irn); i < n && !illegal; ++i)
340         illegal = get_irn_n(irn, i) == NULL;
341
342       if(illegal)
343         sched_remove(irn);
344     }
345   }
346
347   for(irn = pset_first(unused_copies); irn; irn = pset_next(unused_copies)) {
348                 sched_remove(irn);
349         }
350 }
351
352 void be_ssa_constr_single_ignore(dom_front_info_t *info, ir_node *orig, int n, ir_node *copies[], pset *ignore_uses)
353 {
354         ir_node *origs[1];
355         origs[0] = orig;
356         be_ssa_constr_ignore(info, 1, origs, n, copies, ignore_uses);
357 }
358
359 void be_ssa_constr_ignore(dom_front_info_t *info, int n_origs, ir_node *orig_nodes[],
360                                                   int n_copies, ir_node *copy_nodes[], pset *ignore_uses)
361 {
362         int n_all              = n_copies + n_origs;
363         pset *copies           = pset_new_ptr(2 * n_all);
364         pset *copy_blocks      = pset_new_ptr(2 * n_all);
365         pset *phi_blocks       = pset_new_ptr(2 * n_all);
366         int save_optimize      = get_optimize();
367         int save_normalize     = get_opt_normalize();
368         firm_dbg_module_t *dbg = DBG_MODULE;
369
370         int i;
371
372         firm_dbg_set_mask(dbg, DBG_LEVEL);
373         // DBG((dbg, LEVEL_1, "Introducing following copies of %+F\n", orig));
374
375         /* Fill the sets. */
376         for(i = 0; i < n_origs; ++i) {
377                 pset_insert_ptr(copies, orig_nodes[i]);
378                 pset_insert_ptr(copy_blocks, get_nodes_block(orig_nodes[i]));
379         }
380
381         /*
382         * All phis using the original values are also copies of it
383         * and must be present in the copies set.
384         */
385         for(i = 0; i < n_copies; ++i) {
386                 ir_node *bl = get_nodes_block(copy_nodes[i]);
387                 DBG((dbg, LEVEL_1, "\t%+F in block %+F\n", copy_nodes[i], bl));
388                 pset_insert_ptr(copies, copy_nodes[i]);
389                 pset_insert_ptr(copy_blocks, get_nodes_block(bl));
390         }
391
392         /*
393         * Disable optimization so that the phi functions do not
394         * disappear.
395         */
396         set_optimize(0);
397         set_opt_normalize(0);
398
399         /*
400         * Place the phi functions and reroute the usages.
401         */
402         determine_phi_blocks(copies, copy_blocks, phi_blocks, info);
403         fix_usages(n_origs, orig_nodes, copies, copy_blocks, phi_blocks, ignore_uses);
404
405         /* reset the optimizations */
406         set_optimize(save_optimize);
407         set_opt_normalize(save_normalize);
408
409         del_pset(copies);
410         del_pset(phi_blocks);
411         del_pset(copy_blocks);
412
413 }
414
415
416 void be_ssa_constr_single(dom_front_info_t *info, ir_node *orig, int n, ir_node *copy_nodes[])
417 {
418         pset *empty_set = be_empty_set();
419
420         assert(pset_count(empty_set) == 0);
421         be_ssa_constr_single_ignore(info, orig, n, copy_nodes, empty_set);
422 }
423
424 void be_ssa_constr_sets(dom_front_info_t *info, pset *origs, pset *copies)
425 {
426         int n_origs  = pset_count(origs);
427         int n_copies = pset_count(copies);
428
429         ir_node **orig_nodes = alloca(n_origs * sizeof(orig_nodes[0]));
430         ir_node **copy_nodes = alloca(n_copies * sizeof(orig_nodes[0]));
431
432         ir_node *irn;
433         int i;
434
435         for(i = 0, irn = pset_first(origs); irn; irn = pset_next(origs))
436                 orig_nodes[i++] = irn;
437
438         for(i = 0, irn = pset_first(copies); irn; irn = pset_next(copies))
439                 copy_nodes[i++] = irn;
440
441         be_ssa_constr(info, n_origs, orig_nodes, n_copies, copy_nodes);
442 }