typo fixed
[libfirm] / ir / be / bessaconstr.c
1 /*
2  * Copyright (C) 1995-2007 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
59 #include "debug.h"
60 #include "error.h"
61 #include "pdeq.h"
62 #include "array.h"
63 #include "irdom.h"
64
65 #include "ircons.h"
66 #include "iredges_t.h"
67
68 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
69
70 static INLINE int is_inside(unsigned what, unsigned low, unsigned hi)
71 {
72         return what - low < hi;
73 }
74
75 /**
76  * Calculates the iterated dominance frontier of a set of blocks. Marks the
77  * blocks as visited. Sets the link fields of the blocks in the dominance
78  * frontier to the block itself.
79  */
80 static
81 void mark_iterated_dominance_frontiers(const be_ssa_construction_env_t *env)
82 {
83         stat_ev_cnt_decl(blocks);
84         DBG((dbg, LEVEL_3, "Dominance Frontier:"));
85         stat_ev_tim_push();
86         while (!pdeq_empty(env->worklist)) {
87                 int i;
88                 ir_node *block = waitq_get(env->worklist);
89                 ir_node **domfront = be_get_dominance_frontier(env->domfronts, block);
90                 int domfront_len = ARR_LEN(domfront);
91
92                 for (i = 0; i < domfront_len; ++i) {
93                         ir_node *y = domfront[i];
94                         if (Block_block_visited(y))
95                                 continue;
96
97                         /*
98                          * It makes no sense to add phi-functions to blocks
99                          * that are not dominated by any definition;
100                          * all uses are dominated, hence the paths reaching the uses
101                          * have to stay in the dominance subtrees of the given definitions.
102                          */
103
104                         if (!is_inside(get_Block_dom_tree_pre_num(y), env->min_dom, env->max_dom))
105                                 continue;
106
107                         if (!irn_visited(y)) {
108                                 set_irn_link(y, NULL);
109                                 waitq_put(env->worklist, y);
110                         }
111
112                         DBG((dbg, LEVEL_3, " %+F", y));
113                         mark_Block_block_visited(y);
114                         stat_ev_cnt_inc(blocks);
115                 }
116         }
117         stat_ev_tim_pop("bessaconstr_idf_time");
118         stat_ev_cnt_done(blocks, "bessaconstr_idf_blocks");
119         DBG((dbg, LEVEL_3, "\n"));
120 }
121
122 static
123 ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
124                                  ir_node *block);
125
126 static
127 ir_node *create_phi(be_ssa_construction_env_t *env, ir_node *block,
128                     ir_node *link_with)
129 {
130         int i, n_preds = get_Block_n_cfgpreds(block);
131         ir_graph *irg = get_irn_irg(block);
132         ir_node *phi;
133         ir_node **ins = alloca(n_preds * sizeof(ins[0]));
134
135         assert(n_preds > 1);
136
137         for(i = 0; i < n_preds; ++i) {
138                 ins[i] = new_r_Unknown(irg, env->mode);
139         }
140         phi = new_r_Phi(irg, block, n_preds, ins, env->mode);
141         if(env->new_phis != NULL) {
142                 ARR_APP1(ir_node*, env->new_phis, phi);
143         }
144
145         if(env->mode != mode_M) {
146                 sched_add_after(block, phi);
147         }
148
149         DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
150         set_irn_link(link_with, phi);
151         mark_irn_visited(block);
152
153         for(i = 0; i < n_preds; ++i) {
154                 ir_node *pred_block = get_Block_cfgpred_block(block, i);
155                 ir_node *pred_def   = search_def_end_of_block(env, pred_block);
156
157                 set_irn_n(phi, i, pred_def);
158         }
159
160         return phi;
161 }
162
163 static
164 ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
165 {
166         ir_node *dom = get_Block_idom(block);
167         assert(dom != NULL);
168         return search_def_end_of_block(env, dom);
169 }
170
171 static
172 ir_node *search_def_end_of_block(be_ssa_construction_env_t *env, ir_node *block)
173 {
174         if(irn_visited(block)) {
175                 assert(get_irn_link(block) != NULL);
176                 return get_irn_link(block);
177         } else if(Block_block_visited(block)) {
178                 return create_phi(env, block, block);
179         } else {
180                 ir_node *def = get_def_at_idom(env, block);
181                 mark_irn_visited(block);
182                 set_irn_link(block, def);
183                 return def;
184         }
185 }
186
187 static
188 ir_node *search_def(be_ssa_construction_env_t *env, ir_node *at)
189 {
190         ir_node *block = get_nodes_block(at);
191         ir_node *node;
192         ir_node *def;
193
194         DBG((dbg, LEVEL_3, "\t...searching def at %+F\n", at));
195
196         /* no defs in the current block we can do the normal searching */
197         if(!irn_visited(block) && !Block_block_visited(block)) {
198                 DBG((dbg, LEVEL_3, "\t...continue at idom\n"));
199                 return get_def_at_idom(env, block);
200         }
201
202         /* there are defs in the current block, walk the linked list to find
203            the one immediately dominating us
204          */
205         node = block;
206         def  = get_irn_link(node);
207         while(def != NULL) {
208                 if(!value_dominates(at, def)) {
209                         DBG((dbg, LEVEL_3, "\t...found dominating def %+F\n", def));
210                         return def;
211                 }
212
213                 node = def;
214                 def  = get_irn_link(node);
215         }
216
217         /* block in dominance frontier? create a phi then */
218         if(Block_block_visited(block)) {
219                 DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
220                 assert(!is_Phi(node));
221                 return create_phi(env, block, node);
222         }
223
224         DBG((dbg, LEVEL_3, "\t...continue at idom (after checking block)\n"));
225         return get_def_at_idom(env, block);
226 }
227
228 static
229 void update_domzone(be_ssa_construction_env_t *env, const ir_node *bl)
230 {
231         int start = get_Block_dom_tree_pre_num(bl);
232         int end   = get_Block_dom_max_subtree_pre_num(bl) + 1;
233
234         env->min_dom = MIN(env->min_dom, start);
235         env->max_dom = MAX(env->max_dom, end);
236 }
237
238 /**
239  * Adds a definition into the link field of the block. The definitions are
240  * sorted by dominance. A non-visited block means no definition has been
241  * inserted yet.
242  */
243 static
244 void introduce_def_at_block(ir_node *block, ir_node *def)
245 {
246         if(irn_visited(block)) {
247                 ir_node *node = block;
248                 ir_node *current_def;
249
250                 while(1) {
251                         current_def = get_irn_link(node);
252                         if(current_def == def) {
253                                 /* already in block */
254                                 return;
255                         }
256                         if(current_def == NULL)
257                                 break;
258                         if(value_dominates(current_def, def))
259                                 break;
260                         node = current_def;
261                 }
262
263                 set_irn_link(node, def);
264                 set_irn_link(def, current_def);
265         } else {
266                 set_irn_link(block, def);
267                 set_irn_link(def, NULL);
268                 mark_irn_visited(block);
269         }
270 }
271
272 void be_ssa_construction_init(be_ssa_construction_env_t *env, be_irg_t *birg)
273 {
274         ir_graph *irg = be_get_birg_irg(birg);
275         ir_node *sb   = get_irg_start_block(irg);
276         int n_blocks  = get_Block_dom_max_subtree_pre_num(sb);
277
278         stat_ev_ctx_push_fobj("bessaconstr", irg);
279         stat_ev_tim_push();
280
281         (void) n_blocks;
282         stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
283
284         memset(env, 0, sizeof(env[0]));
285         be_assure_dom_front(birg);
286
287         env->irg       = irg;
288         env->domfronts = be_get_birg_dom_front(birg);
289         env->new_phis  = NEW_ARR_F(ir_node*, 0);
290         env->worklist  = new_waitq();
291         env->min_dom   = INT_MAX;
292         env->max_dom   = 0;
293
294         set_using_visited(irg);
295         set_using_block_visited(irg);
296         set_using_irn_link(irg);
297
298         /* we use the visited flag to indicate blocks in the dominance frontier
299          * and blocks that already have the relevant value at the end calculated */
300         inc_irg_visited(irg);
301         /* We use the block visited flag to indicate blocks in the dominance
302          * frontier of some values (and this potentially needing phis) */
303         inc_irg_block_visited(irg);
304 }
305
306 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
307 {
308         stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
309         del_waitq(env->worklist);
310         DEL_ARR_F(env->new_phis);
311
312         clear_using_visited(env->irg);
313         clear_using_block_visited(env->irg);
314         clear_using_irn_link(env->irg);
315
316         stat_ev_tim_pop("bessaconstr_total_time");
317         stat_ev_ctx_pop("bessaconstr");
318 }
319
320 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
321                                   ir_node *copy)
322 {
323         ir_node *block;
324
325         assert(env->iterated_domfront_calculated == 0);
326
327         if(env->mode == NULL) {
328                 env->mode = get_irn_mode(copy);
329         } else {
330                 assert(env->mode == get_irn_mode(copy));
331         }
332
333         block = get_nodes_block(copy);
334
335         if(!irn_visited(block)) {
336                 waitq_put(env->worklist, block);
337         }
338         introduce_def_at_block(block, copy);
339         update_domzone(env, block);
340 }
341
342 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
343                                     ir_node **copies, size_t copies_len)
344 {
345         size_t i;
346
347         assert(env->iterated_domfront_calculated == 0);
348
349         if(env->mode == NULL) {
350                 env->mode = get_irn_mode(copies[0]);
351         }
352
353         for(i = 0; i < copies_len; ++i) {
354                 ir_node *copy = copies[i];
355                 ir_node *block = get_nodes_block(copy);
356
357                 assert(env->mode == get_irn_mode(copy));
358                 if(!irn_visited(block)) {
359                         waitq_put(env->worklist, block);
360                 }
361                 introduce_def_at_block(block, copy);
362                 update_domzone(env, block);
363         }
364 }
365
366 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
367                                          const ir_nodeset_t *ignore_uses)
368 {
369         env->ignore_uses = ignore_uses;
370 }
371
372 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
373 {
374         return env->new_phis;
375 }
376
377 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
378                                          ir_node **nodes, size_t nodes_len)
379 {
380         stat_ev_cnt_decl(uses);
381         const ir_edge_t *edge, *next;
382         size_t i;
383
384         if(!env->iterated_domfront_calculated) {
385                 mark_iterated_dominance_frontiers(env);
386                 env->iterated_domfront_calculated = 1;
387         }
388
389         stat_ev_int("bessaconstr_domzone", env->max_dom - env->min_dom);
390         stat_ev_tim_push();
391         for(i = 0; i < nodes_len; ++i) {
392                 ir_node *value = nodes[i];
393
394                 /*
395                  * Search the valid def for each use and set it.
396                  */
397                 foreach_out_edge_safe(value, edge, next) {
398                         ir_node *use = get_edge_src_irn(edge);
399                         ir_node *at  = use;
400                         int pos      = get_edge_src_pos(edge);
401                         ir_node *def;
402
403                         if(env->ignore_uses != NULL     &&
404                            ir_nodeset_contains(env->ignore_uses, use))
405                                 continue;
406                         if(is_Anchor(use))
407                                 continue;
408
409                         if(is_Phi(use)) {
410                                 ir_node *block = get_nodes_block(use);
411                                 ir_node *predblock = get_Block_cfgpred_block(block, pos);
412                                 at = sched_last(predblock);
413                         }
414
415                         def = search_def(env, at);
416
417                         if(def == NULL) {
418                                 panic("no definition found for %+F at position %d\n", use, pos);
419                         }
420
421                         DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", use, pos, def));
422                         set_irn_n(use, pos, def);
423                         stat_ev_cnt_inc(uses);
424                 }
425         }
426         stat_ev_tim_pop("bessaconstr_fix_time");
427         stat_ev_cnt_done(uses, "bessaconstr_uses");
428 }
429
430 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
431 {
432         be_ssa_construction_fix_users_array(env, &value, 1);
433 }
434
435
436 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
437                                               be_lv_t *lv)
438 {
439         int i, n;
440
441         n = ARR_LEN(env->new_phis);
442         for(i = 0; i < n; ++i) {
443                 ir_node *phi = env->new_phis[i];
444                 be_liveness_introduce(lv, phi);
445         }
446 }
447
448 void be_init_ssaconstr(void)
449 {
450         FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");
451 }
452
453 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr);