1724b25fbec3433ce9935eeb56ed3dcb6e5b4237
[libfirm] / ir / be / bessaconstr.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       SSA construction for a set of nodes
23  * @author      Sebastian Hack, Daniel Grund, Matthias Braun, Christian Wuerdig
24  * @date        04.05.2005
25  * @version     $Id$
26  *
27  * The problem: Given a value and a set of "copies" that are known to
28  * represent the same abstract value, rewire all usages of the original value
29  * to their closest copy while introducing phis as necessary.
30  *
31  * Algorithm: Mark all blocks in the iterated dominance frontiers of the value
32  * and it's copies. Link the copies ordered by dominance to the blocks.  The
33  * we search for each use all all definitions in the current block, if none is
34  * found, then we search one in the immediate dominator. If we are in a block
35  * of the dominance frontier, create a phi and search do the same search for
36  * the phi arguments.
37  *
38  * A copy in this context means, that you want to introduce several new
39  * abstract values (in Firm: nodes) for which you know, that they
40  * represent the same concrete value. This is the case if you
41  * - copy
42  * - spill and reload
43  * - re-materialize
44  * a value.
45  *
46  * This function reroutes all uses of the original value to the copies in the
47  * corresponding dominance subtrees and creates Phi functions where necessary.
48  */
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "bessaconstr.h"
54 #include "bemodule.h"
55 #include "besched_t.h"
56 #include "beintlive_t.h"
57 #include "beirg_t.h"
58 #include "be_t.h"
59
60 #include "debug.h"
61 #include "error.h"
62 #include "pdeq.h"
63 #include "array.h"
64 #include "irdom.h"
65
66 #include "ircons.h"
67 #include "iredges_t.h"
68
69 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
70
71 static INLINE int is_inside(unsigned what, unsigned low, unsigned hi)
72 {
73         return what - low < hi;
74 }
75
76 /**
77  * Calculates the iterated dominance frontier of a set of blocks. Marks the
78  * blocks as visited. Sets the link fields of the blocks in the dominance
79  * frontier to the block itself.
80  */
81 static
82 void mark_iterated_dominance_frontiers(const be_ssa_construction_env_t *env)
83 {
84         stat_ev_cnt_decl(blocks);
85         DBG((dbg, LEVEL_3, "Dominance Frontier:"));
86         stat_ev_tim_push();
87         while (!pdeq_empty(env->worklist)) {
88                 int i;
89                 ir_node *block = waitq_get(env->worklist);
90                 ir_node **domfront = be_get_dominance_frontier(env->domfronts, block);
91                 int domfront_len = ARR_LEN(domfront);
92
93                 for (i = 0; i < domfront_len; ++i) {
94                         ir_node *y = domfront[i];
95                         if (Block_block_visited(y))
96                                 continue;
97
98                         /*
99                          * It makes no sense to add phi-functions to blocks
100                          * that are not dominated by any definition;
101                          * all uses are dominated, hence the paths reaching the uses
102                          * have to stay in the dominance subtrees of the given definitions.
103                          */
104
105                         if (!is_inside(get_Block_dom_tree_pre_num(y), env->min_dom, env->max_dom))
106                                 continue;
107
108                         if (!irn_visited(y)) {
109                                 set_irn_link(y, NULL);
110                                 waitq_put(env->worklist, y);
111                         }
112
113                         DBG((dbg, LEVEL_3, " %+F", y));
114                         mark_Block_block_visited(y);
115                         stat_ev_cnt_inc(blocks);
116                 }
117         }
118         stat_ev_tim_pop("bessaconstr_idf_time");
119         stat_ev_cnt_done(blocks, "bessaconstr_idf_blocks");
120         DBG((dbg, LEVEL_3, "\n"));
121 }
122
123 static
124 ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
125                                  ir_node *block);
126
127 static
128 ir_node *create_phi(be_ssa_construction_env_t *env, ir_node *block,
129                     ir_node *link_with)
130 {
131         int i, n_preds = get_Block_n_cfgpreds(block);
132         ir_graph *irg = get_irn_irg(block);
133         ir_node *phi;
134         ir_node **ins = alloca(n_preds * sizeof(ins[0]));
135
136         assert(n_preds > 1);
137
138         for(i = 0; i < n_preds; ++i) {
139                 ins[i] = new_r_Unknown(irg, env->mode);
140         }
141         phi = new_r_Phi(irg, block, n_preds, ins, env->mode);
142         if(env->new_phis != NULL) {
143                 ARR_APP1(ir_node*, env->new_phis, phi);
144         }
145
146         if(env->mode != mode_M) {
147                 sched_add_after(block, phi);
148         }
149
150         DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
151         set_irn_link(link_with, phi);
152         mark_irn_visited(block);
153
154         for(i = 0; i < n_preds; ++i) {
155                 ir_node *pred_block = get_Block_cfgpred_block(block, i);
156                 ir_node *pred_def   = search_def_end_of_block(env, pred_block);
157
158                 set_irn_n(phi, i, pred_def);
159         }
160
161         return phi;
162 }
163
164 static
165 ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
166 {
167         ir_node *dom = get_Block_idom(block);
168         assert(dom != NULL);
169         return search_def_end_of_block(env, dom);
170 }
171
172 static
173 ir_node *search_def_end_of_block(be_ssa_construction_env_t *env, ir_node *block)
174 {
175         if(irn_visited(block)) {
176                 assert(get_irn_link(block) != NULL);
177                 return get_irn_link(block);
178         } else if(Block_block_visited(block)) {
179                 return create_phi(env, block, block);
180         } else {
181                 ir_node *def = get_def_at_idom(env, block);
182                 mark_irn_visited(block);
183                 set_irn_link(block, def);
184                 return def;
185         }
186 }
187
188 static
189 ir_node *search_def(be_ssa_construction_env_t *env, ir_node *at)
190 {
191         ir_node *block = get_nodes_block(at);
192         ir_node *node;
193         ir_node *def;
194
195         DBG((dbg, LEVEL_3, "\t...searching def at %+F\n", at));
196
197         /* no defs in the current block we can do the normal searching */
198         if(!irn_visited(block) && !Block_block_visited(block)) {
199                 DBG((dbg, LEVEL_3, "\t...continue at idom\n"));
200                 return get_def_at_idom(env, block);
201         }
202
203         /* there are defs in the current block, walk the linked list to find
204            the one immediately dominating us
205          */
206         node = block;
207         def  = get_irn_link(node);
208         while(def != NULL) {
209                 if(!value_dominates(at, def)) {
210                         DBG((dbg, LEVEL_3, "\t...found dominating def %+F\n", def));
211                         return def;
212                 }
213
214                 node = def;
215                 def  = get_irn_link(node);
216         }
217
218         /* block in dominance frontier? create a phi then */
219         if(Block_block_visited(block)) {
220                 DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
221                 assert(!is_Phi(node));
222                 return create_phi(env, block, node);
223         }
224
225         DBG((dbg, LEVEL_3, "\t...continue at idom (after checking block)\n"));
226         return get_def_at_idom(env, block);
227 }
228
229 static
230 void update_domzone(be_ssa_construction_env_t *env, const ir_node *bl)
231 {
232         int start = get_Block_dom_tree_pre_num(bl);
233         int end   = get_Block_dom_max_subtree_pre_num(bl) + 1;
234
235         env->min_dom = MIN(env->min_dom, start);
236         env->max_dom = MAX(env->max_dom, end);
237 }
238
239 /**
240  * Adds a definition into the link field of the block. The definitions are
241  * sorted by dominance. A non-visited block means no definition has been
242  * inserted yet.
243  */
244 static
245 void introduce_def_at_block(ir_node *block, ir_node *def)
246 {
247         if(irn_visited(block)) {
248                 ir_node *node = block;
249                 ir_node *current_def;
250
251                 while(1) {
252                         current_def = get_irn_link(node);
253                         if(current_def == def) {
254                                 /* already in block */
255                                 return;
256                         }
257                         if(current_def == NULL)
258                                 break;
259                         if(value_dominates(current_def, def))
260                                 break;
261                         node = current_def;
262                 }
263
264                 set_irn_link(node, def);
265                 set_irn_link(def, current_def);
266         } else {
267                 set_irn_link(block, def);
268                 set_irn_link(def, NULL);
269                 mark_irn_visited(block);
270         }
271 }
272
273 void be_ssa_construction_init(be_ssa_construction_env_t *env, be_irg_t *birg)
274 {
275         ir_graph *irg = be_get_birg_irg(birg);
276         ir_node *sb   = get_irg_start_block(irg);
277         int n_blocks  = get_Block_dom_max_subtree_pre_num(sb);
278
279         stat_ev_ctx_push_fobj("bessaconstr", irg);
280         stat_ev_tim_push();
281
282         (void) n_blocks;
283         stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
284
285         memset(env, 0, sizeof(env[0]));
286         be_assure_dom_front(birg);
287
288         env->irg       = irg;
289         env->domfronts = be_get_birg_dom_front(birg);
290         env->new_phis  = NEW_ARR_F(ir_node*, 0);
291         env->worklist  = new_waitq();
292         env->min_dom   = INT_MAX;
293         env->max_dom   = 0;
294
295         set_using_visited(irg);
296         set_using_block_visited(irg);
297         set_using_irn_link(irg);
298
299         /* we use the visited flag to indicate blocks in the dominance frontier
300          * and blocks that already have the relevant value at the end calculated */
301         inc_irg_visited(irg);
302         /* We use the block visited flag to indicate blocks in the dominance
303          * frontier of some values (and this potentially needing phis) */
304         inc_irg_block_visited(irg);
305 }
306
307 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
308 {
309         stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
310         del_waitq(env->worklist);
311         DEL_ARR_F(env->new_phis);
312
313         clear_using_visited(env->irg);
314         clear_using_block_visited(env->irg);
315         clear_using_irn_link(env->irg);
316
317         stat_ev_tim_pop("bessaconstr_total_time");
318         stat_ev_ctx_pop("bessaconstr");
319 }
320
321 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
322                                   ir_node *copy)
323 {
324         ir_node *block;
325
326         assert(env->iterated_domfront_calculated == 0);
327
328         if(env->mode == NULL) {
329                 env->mode = get_irn_mode(copy);
330         } else {
331                 assert(env->mode == get_irn_mode(copy));
332         }
333
334         block = get_nodes_block(copy);
335
336         if(!irn_visited(block)) {
337                 waitq_put(env->worklist, block);
338         }
339         introduce_def_at_block(block, copy);
340         update_domzone(env, block);
341 }
342
343 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
344                                     ir_node **copies, size_t copies_len)
345 {
346         size_t i;
347
348         assert(env->iterated_domfront_calculated == 0);
349
350         if(env->mode == NULL) {
351                 env->mode = get_irn_mode(copies[0]);
352         }
353
354         for(i = 0; i < copies_len; ++i) {
355                 ir_node *copy = copies[i];
356                 ir_node *block = get_nodes_block(copy);
357
358                 assert(env->mode == get_irn_mode(copy));
359                 if(!irn_visited(block)) {
360                         waitq_put(env->worklist, block);
361                 }
362                 introduce_def_at_block(block, copy);
363                 update_domzone(env, block);
364         }
365 }
366
367 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
368                                          const ir_nodeset_t *ignore_uses)
369 {
370         env->ignore_uses = ignore_uses;
371 }
372
373 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
374 {
375         return env->new_phis;
376 }
377
378 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
379                                          ir_node **nodes, size_t nodes_len)
380 {
381         stat_ev_cnt_decl(uses);
382         const ir_edge_t *edge, *next;
383         size_t i;
384
385         BE_TIMER_PUSH(t_ssa_constr);
386
387         if(!env->iterated_domfront_calculated) {
388                 mark_iterated_dominance_frontiers(env);
389                 env->iterated_domfront_calculated = 1;
390         }
391
392         stat_ev_int("bessaconstr_domzone", env->max_dom - env->min_dom);
393         stat_ev_tim_push();
394         for(i = 0; i < nodes_len; ++i) {
395                 ir_node *value = nodes[i];
396
397                 /*
398                  * Search the valid def for each use and set it.
399                  */
400                 foreach_out_edge_safe(value, edge, next) {
401                         ir_node *use = get_edge_src_irn(edge);
402                         ir_node *at  = use;
403                         int pos      = get_edge_src_pos(edge);
404                         ir_node *def;
405
406                         if(env->ignore_uses != NULL     &&
407                            ir_nodeset_contains(env->ignore_uses, use))
408                                 continue;
409                         if(is_Anchor(use))
410                                 continue;
411
412                         if(is_Phi(use)) {
413                                 ir_node *block = get_nodes_block(use);
414                                 ir_node *predblock = get_Block_cfgpred_block(block, pos);
415                                 at = sched_last(predblock);
416                         }
417
418                         def = search_def(env, at);
419
420                         if(def == NULL) {
421                                 panic("no definition found for %+F at position %d\n", use, pos);
422                         }
423
424                         DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", use, pos, def));
425                         set_irn_n(use, pos, def);
426                         stat_ev_cnt_inc(uses);
427                 }
428         }
429         BE_TIMER_POP(t_ssa_constr);
430
431         stat_ev_tim_pop("bessaconstr_fix_time");
432         stat_ev_cnt_done(uses, "bessaconstr_uses");
433 }
434
435 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
436 {
437         be_ssa_construction_fix_users_array(env, &value, 1);
438 }
439
440
441 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
442                                               be_lv_t *lv)
443 {
444         int i, n;
445
446         BE_TIMER_PUSH(t_ssa_constr);
447
448         n = ARR_LEN(env->new_phis);
449         for(i = 0; i < n; ++i) {
450                 ir_node *phi = env->new_phis[i];
451                 be_liveness_introduce(lv, phi);
452         }
453
454         BE_TIMER_POP(t_ssa_constr);
455 }
456
457 void be_init_ssaconstr(void)
458 {
459         FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");
460 }
461
462 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr);