1f252d8eb46561f3ee9c9a087f1e0f3b2257db0a
[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 "irmode_t.h"
18 #include "irdom_t.h"
19 #include "iredges_t.h"
20 #include "irgopt.h"
21
22 #include "be_t.h"
23 #include "bearch.h"
24 #include "besched_t.h"
25 #include "belive_t.h"
26 #include "benode_t.h"
27
28 #include "beirgmod.h"
29
30 #define DBG_MODULE firm_dbg_register("firm.be.irgmod")
31
32 struct _dom_front_info_t {
33   pmap *df_map;
34 };
35
36 /**
37  * A wrapper for get_Block_idom.
38  * This function returns the block itself, if the block is the start
39  * block. Returning NULL would make any != comparison true which
40  * suggests, that the start block is dominated by some other node.
41  * @param bl The block.
42  * @return The immediate dominator of the block.
43  */
44 static INLINE ir_node *get_idom(ir_node *bl)
45 {
46   ir_node *idom = get_Block_idom(bl);
47   return idom == NULL ? bl : idom;
48 }
49
50 static void compute_df_local(ir_node *bl, void *data)
51 {
52   pmap *df_map = ((dom_front_info_t *) data)->df_map;
53   ir_node *idom = get_Block_idom(bl);
54   pset *df = pmap_get(df_map, bl);
55   int i, n;
56
57   /*
58    * In the case that the immediate dominator is NULL, the
59    * block is the start block and its immediate dominator
60    * must be itself, else it is inserted into its own
61    * dominance frontier.
62    */
63   if(idom == NULL)
64         idom = bl;
65
66   /*
67    * Create a new dom frot set for this node,
68    * if none exists.
69    */
70   if(!df)
71         pmap_insert(df_map, bl, pset_new_ptr(16));
72
73   for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
74
75     /* The predecessor block */
76     ir_node *pred = get_nodes_block(get_irn_n(bl, i));
77
78     /* The dominance frontier set of the predecessor. */
79     pset *df = pmap_get(df_map, pred);
80           if(!df) {
81                 df = pset_new_ptr(16);
82                 pmap_insert(df_map, pred, df);
83           }
84
85     assert(df && "dom front set must have been created for this node");
86
87     if(pred != idom && bl)
88       pset_insert_ptr(df, bl);
89   }
90 }
91
92 static void compute_df_up(ir_node *bl, void *data)
93 {
94   pmap *df_map = ((dom_front_info_t *) data)->df_map;
95   ir_node *y;
96
97   for(y = get_Block_dominated_first(bl); y; y = get_Block_dominated_next(y)) {
98     ir_node *w;
99     pset *df = pmap_get(df_map, y);
100
101     for(w = pset_first(df); w; w = pset_next(df))
102       if(!block_dominates(bl, w) || bl == w)
103         pset_insert_ptr(df, w);
104   }
105 }
106
107 static void compute_df(ir_node *n, pmap *df_map)
108 {
109   ir_node *y, *c;
110   const ir_edge_t *edge;
111   pset *df = pset_new_ptr_default();
112
113   /* Add local dominance frontiers */
114   foreach_block_succ(n, edge) {
115     ir_node *y = edge->src;
116
117     if(get_idom(y) != n)
118       pset_insert_ptr(df, y);
119   }
120
121   /*
122    * Go recursively down the dominance tree and add all blocks
123    * int the dominance frontiers of the children, which are not
124    * dominated by the given block.
125    */
126   for(c = get_Block_dominated_first(n); c; c = get_Block_dominated_next(c)) {
127     pset *df_c;
128     ir_node *w;
129
130     compute_df(c, df_map);
131     df_c = pmap_get(df_map, c);
132
133     for(w = pset_first(df_c); w; w = pset_next(df_c)) {
134       if(!block_dominates(n, w))
135         pset_insert_ptr(df, w);
136     }
137   }
138
139   pmap_insert(df_map, n, df);
140
141 }
142
143 dom_front_info_t *be_compute_dominance_frontiers(ir_graph *irg)
144 {
145   dom_front_info_t *info = malloc(sizeof(*info));
146
147   edges_assure(irg);
148   info->df_map = pmap_create();
149   compute_df(get_irg_start_block(irg), info->df_map);
150
151 #if 0
152   /*
153    * This must be called as a post walker, since the dom front sets
154    * of all predecessors must be created when a block is reached.
155    */
156   dom_tree_walk_irg(irg, NULL, compute_df_local, info);
157   dom_tree_walk_irg(irg, NULL, compute_df_up, info);
158 #endif
159   return info;
160 }
161
162 void be_free_dominance_frontiers(dom_front_info_t *info)
163 {
164   pmap_entry *ent;
165
166   for(ent = pmap_first(info->df_map); ent; ent = pmap_next(info->df_map))
167     del_pset(ent->value);
168
169   pmap_destroy(info->df_map);
170   free(info);
171 }
172
173 pset *be_get_dominance_frontier(dom_front_info_t *info, ir_node *block)
174 {
175   return pmap_get(info->df_map, block);
176 }
177
178 /**
179  * Algorithm to place the Phi-Functions.
180  * @see Appel, Modern Compiler Implementation in Java, 2nd ed., p. 399ff
181  *
182  * This function takes an original node and a set of already placed
183  * copies of that node called @p copies. It places phi nodes at the
184  * iterated dominance frontiers of these copies and puts these phi nodes
185  * in the @p copies set, since they are another form of copies of the
186  * original value.
187  *
188  * The rename phase (see below) is responsible for fixing up the usages
189  * of the original node.
190  *
191  * @param orig The original node.
192  * @param copies A set contianing nodes representing a copy of the
193  * original node. Each node must be inserted into the block's schedule.
194  * @param copy_blocks A set in which the blocks are recorded which
195  * contain a copy. This is just for efficiency in later phases (see
196  * rename).
197  */
198 static void place_phi_functions(ir_node *orig, pset *copies,
199     pset *copy_blocks, dom_front_info_t *df_info)
200 {
201   int i;
202   ir_node *orig_block = get_nodes_block(orig);
203   ir_graph *irg = get_irn_irg(orig);
204   ir_mode *mode = get_irn_mode(orig);
205   pdeq *worklist = new_pdeq();
206   pset *phi_blocks = pset_new_ptr(8);
207   ir_node **ins = NULL;
208   void *it;
209   firm_dbg_module_t *dbg = DBG_MODULE;
210
211   /*
212    * Allocate an array for all blocks where the copies and the original
213    * value were defined.
214    */
215   int n_orig_blocks = pset_count(copy_blocks);
216   ir_node **orig_blocks = malloc(n_orig_blocks * sizeof(orig_blocks[0]));
217
218   /*
219    * Fill the worklist queue and the rest of the orig blocks array.
220    */
221   for(it = pset_first(copies), i = 0; it; it = pset_next(copies)) {
222     ir_node *copy_block = get_nodes_block(it);
223
224     if(!block_dominates(orig_block, copy_block)) {
225         assert(block_dominates(orig_block, copy_block)
226                 && "The block of the copy must be dominated by the block of the value");
227     }
228
229     pdeq_putr(worklist, copy_block);
230     orig_blocks[i++] = copy_block;
231   }
232
233   while(!pdeq_empty(worklist)) {
234     ir_node *bl = pdeq_getl(worklist);
235     ir_node *y;
236     pset *df = be_get_dominance_frontier(df_info, bl);
237
238     DBG((dbg, LEVEL_3, "dom front of %+F\n", bl));
239     for(y = pset_first(df); y; y = pset_next(df))
240             DBG((dbg, LEVEL_3, "\t%+F\n", y));
241
242     for(y = pset_first(df); y; y = pset_next(df)) {
243       int n_preds = get_irn_arity(y);
244
245       if(!pset_find_ptr(phi_blocks, y)) {
246         ir_node *phi;
247         int insert = 1;
248
249         /*
250          * Set the orig node as the only operand of the
251          * phi node.
252          */
253         ins = realloc(ins, n_preds * sizeof(ins[0]));
254         for(i = 0; i < n_preds; ++i)
255           ins[i] = orig;
256
257         /* Insert phi node */
258         phi = new_r_Phi(irg, y, n_preds, ins, mode);
259         DBG((dbg, LEVEL_2, "    inserting phi %+F with %d args in block %+F\n",
260               phi, n_preds, y));
261
262         /*
263          * The phi node itself is also a copy of the original
264          * value. So put it in the copies set also, so that
265          * the rename phase can treat them right.
266          */
267         pset_insert_ptr(copies, phi);
268         pset_insert_ptr(copy_blocks, y);
269
270         /*
271          * Insert the phi node into the schedule if it
272          * can occur there (PhiM's are not to put into a schedule.
273          */
274         if(to_appear_in_schedule(phi))
275           sched_add_before(sched_first(y), phi);
276
277         /* Insert the phi node in the phi blocks set. */
278         pset_insert_ptr(phi_blocks, y);
279
280         /*
281          * If orig or a copy of it were not defined in y,
282          * add y to the worklist.
283          */
284         for(i = 0; i < n_orig_blocks; ++i)
285           if(orig_blocks[i] == y) {
286             insert = 0;
287             break;
288           }
289
290         if(insert)
291           pdeq_putr(worklist, y);
292
293       }
294     }
295   }
296
297   del_pset(phi_blocks);
298   del_pdeq(worklist);
299
300   free(orig_blocks);
301
302   if(ins)
303     free(ins);
304 }
305
306 /**
307  * Find the copy of the given original node whose value is 'active'
308  * at a usage.
309  *
310  * The usage is given as a node and a position. Initially, the given operand
311  * points to a node for which copies were introduced. We have to find
312  * the valid copy for this usage. This is done by travering the
313  * dominance tree upwards. If the usage is a phi function, we start
314  * traversing from the predecessor block which corresponds to the phi
315  * usage.
316  *
317  * @param usage The node which uses the original node.
318  * @param pos The number of the argument which corresponds to the
319  * original node.
320  * @param copy_blocks A set containing all basic block in which copies
321  * of the original node are located.
322  * @param copies A set containing all node which are copies from the
323  * original node.
324  * @return The valid copy for usage.
325  */
326 static ir_node *search_def(ir_node *usage, int pos, pset *copies, pset *copy_blocks)
327 {
328   ir_node *curr_bl;
329   ir_node *start_irn;
330   firm_dbg_module_t *dbg = DBG_MODULE;
331   firm_dbg_set_mask(dbg, -1);
332
333   curr_bl = get_nodes_block(usage);
334
335
336   DBG((dbg, LEVEL_1, "Searching valid def for use %+F at pos %d\n", usage, pos));
337   /*
338    * If the usage is in a phi node, search the copy in the
339    * predecessor denoted by pos.
340    */
341   if(is_Phi(usage)) {
342     curr_bl = get_Block_cfgpred_block(curr_bl, pos);
343     start_irn = sched_last(curr_bl);
344   } else {
345     start_irn = sched_prev(usage);
346   }
347
348   /*
349    * Traverse the dominance tree upwards from the
350    * predecessor block of the usage.
351    */
352   while(curr_bl != NULL) {
353
354     /*
355      * If this block contains a copy, search the block
356      * instruction by instruction.
357      */
358     if(pset_find_ptr(copy_blocks, curr_bl)) {
359       ir_node *irn;
360
361       /* Look at each instruction from last to first. */
362       for(irn = start_irn; !is_Block(irn); irn = sched_prev(irn)) {
363
364         /* Take the first copy we find. */
365
366         DBG((dbg, LEVEL_1, "Is %F a copy?\n", irn));
367         if(pset_find_ptr(copies, irn))
368           return irn;
369       }
370     }
371
372     /* If were not done yet, look in the immediate dominator */
373     curr_bl = get_Block_idom(curr_bl);
374     if(curr_bl)
375       start_irn = sched_last(curr_bl);
376   }
377
378   assert(0 && "Did not find a valid def");
379   return NULL;
380 }
381
382 static void fix_usages(ir_node *orig, pset *copies, pset *copy_blocks)
383 {
384   int i = 0;
385   int n_outs = 0;
386   const ir_edge_t *edge;
387   firm_dbg_module_t *dbg = DBG_MODULE;
388
389   struct {
390     ir_node *irn;
391     int pos;
392   } *outs;
393
394   /* Count the number of outs. */
395   foreach_out_edge(orig, edge)
396     n_outs++;
397
398   /*
399    * Put all outs into an array.
400    * This is neccessary, since the outs would be modified while
401    * interating on them what could bring the outs module in trouble.
402    */
403   DBG((dbg, LEVEL_2, "  Users of %+F\n", orig));
404   outs = malloc(n_outs * sizeof(outs[0]));
405   foreach_out_edge(orig, edge) {
406     outs[i].irn = get_edge_src_irn(edge);
407     outs[i].pos = get_edge_src_pos(edge);
408     i += 1;
409   }
410
411   /*
412    * Search the valid def for each out and set it.
413    */
414   for(i = 0; i < n_outs; ++i) {
415     ir_node *def;
416     ir_node *irn = outs[i].irn;
417     int pos = outs[i].pos;
418
419     DBG((dbg, LEVEL_2, "    %+F(%d) -> ???\n", irn, pos));
420     def = search_def(irn, pos, copies, copy_blocks);
421     DBG((dbg, LEVEL_2, "    %+F(%d) -> %+F\n", irn, pos, def));
422
423     if(def != NULL)
424       set_irn_n(irn, pos, def);
425   }
426
427   free(outs);
428 }
429
430 struct phi_collect_info {
431   const ir_node *orig;
432   pset *copies;
433   pset *copy_blocks;
434 };
435
436 static void add_all_phis_walker(ir_node *irn, void *data)
437 {
438   if(is_Phi(irn)) {
439     int i, o, n;
440     struct phi_collect_info *info = data;
441
442     /*
443      * Look at all operands of the phi. If one of them is the original
444      * node, insert the phi into the copies and copy_blocks set.
445      */
446     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
447       if(get_irn_n(irn, i) == info->orig) {
448         pset_insert_ptr(info->copies, irn);
449         pset_insert_ptr(info->copy_blocks, get_nodes_block(irn));
450
451         break;
452       }
453     }
454
455
456   }
457 }
458
459 /**
460  * Add all phis using a node to a set.
461  * @param orig        The node the phis shall use.
462  * @param copies      The set where the phis shall be put into.
463  * @param copy_blocks The set the blocks of the phis shall be put into.
464  */
465 static void add_all_phis_using(const ir_node *orig, pset *copies, pset *copy_blocks)
466 {
467   struct phi_collect_info info;
468
469   info.copies      = copies;
470   info.copy_blocks = copy_blocks;
471   info.orig        = orig;
472   irg_walk_graph(get_irn_irg(orig), add_all_phis_walker, NULL, &info);
473 }
474
475 void be_introduce_copies(dom_front_info_t *info, ir_node *orig, int n, ir_node *copy_nodes[])
476 {
477   pset *copies = pset_new_ptr(2 * n);
478   pset *copy_blocks = pset_new_ptr(2 * n);
479   int save_optimize = get_optimize();
480   int save_normalize = get_opt_normalize();
481   firm_dbg_module_t *dbg = DBG_MODULE;
482   int i;
483
484   firm_dbg_set_mask(dbg, -1);
485   DBG((dbg, LEVEL_1, "Introducing following copies of %+F\n", orig));
486
487   /* Fill the sets. */
488   pset_insert_ptr(copies, orig);
489   pset_insert_ptr(copy_blocks, get_nodes_block(orig));
490
491   /*
492    * All phis using the original value are also copies of it
493    * and must be present in the copies set.
494    */
495   /* add_all_phis_using(orig, copies, copy_blocks);*/
496
497   for(i = 0; i < n; ++i) {
498     DBG((dbg, LEVEL_1,
499           "  %+F in block %+F\n", copy_nodes[i], get_nodes_block(copy_nodes[i])));
500     pset_insert_ptr(copies, copy_nodes[i]);
501     pset_insert_ptr(copy_blocks, get_nodes_block(copy_nodes[i]));
502   }
503
504   /*
505    * Disable optimization so that the phi functions do not
506    * disappear.
507    */
508   set_optimize(0);
509   set_opt_normalize(0);
510
511   /*
512    * Place the phi functions and reroute the usages.
513    */
514   place_phi_functions(orig, copies, copy_blocks, info);
515   fix_usages(orig, copies, copy_blocks);
516
517   /* reset the optimizations */
518   set_optimize(save_optimize);
519   set_opt_normalize(save_normalize);
520
521   del_pset(copies);
522   del_pset(copy_blocks);
523 }