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