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