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