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