keep timer interface simple - no names and descriptions for timers anymore, you shoul...
[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.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 = 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  *phi;
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 = be_new_Phi(block, n_preds, ins, env->mode, env->phi_cls);
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 ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
150 {
151         ir_node *dom = get_Block_idom(block);
152         assert(dom != NULL);
153         return search_def_end_of_block(env, dom);
154 }
155
156 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
157                                         ir_node *block)
158 {
159         if(irn_visited(block)) {
160                 assert(get_irn_link(block) != NULL);
161                 return get_irn_link(block);
162         } else if(Block_block_visited(block)) {
163                 return create_phi(env, block, block);
164         } else {
165                 ir_node *def = get_def_at_idom(env, block);
166                 mark_irn_visited(block);
167                 set_irn_link(block, def);
168                 return def;
169         }
170 }
171
172 static ir_node *search_def(be_ssa_construction_env_t *env, ir_node *at)
173 {
174         ir_node *block = get_nodes_block(at);
175         ir_node *node;
176         ir_node *def;
177
178         DBG((dbg, LEVEL_3, "\t...searching def at %+F\n", at));
179
180         /* no defs in the current block we can do the normal searching */
181         if(!irn_visited(block) && !Block_block_visited(block)) {
182                 DBG((dbg, LEVEL_3, "\t...continue at idom\n"));
183                 return get_def_at_idom(env, block);
184         }
185
186         /* there are defs in the current block, walk the linked list to find
187            the one immediately dominating us
188          */
189         node = block;
190         def  = get_irn_link(node);
191         while(def != NULL) {
192                 if(!value_dominates(at, def)) {
193                         DBG((dbg, LEVEL_3, "\t...found dominating def %+F\n", def));
194                         return def;
195                 }
196
197                 node = def;
198                 def  = get_irn_link(node);
199         }
200
201         /* block in dominance frontier? create a phi then */
202         if(Block_block_visited(block)) {
203                 DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
204                 assert(!is_Phi(node));
205                 return create_phi(env, block, node);
206         }
207
208         DBG((dbg, LEVEL_3, "\t...continue at idom (after checking block)\n"));
209         return get_def_at_idom(env, block);
210 }
211
212 /**
213  * Adds a definition into the link field of the block. The definitions are
214  * sorted by dominance. A non-visited block means no definition has been
215  * inserted yet.
216  */
217 static void introduce_def_at_block(ir_node *block, ir_node *def)
218 {
219         if (irn_visited_else_mark(block)) {
220                 ir_node *node = block;
221                 ir_node *current_def;
222
223                 while(1) {
224                         current_def = get_irn_link(node);
225                         if(current_def == def) {
226                                 /* already in block */
227                                 return;
228                         }
229                         if(current_def == NULL)
230                                 break;
231                         if(value_dominates(current_def, def))
232                                 break;
233                         node = current_def;
234                 }
235
236                 set_irn_link(node, def);
237                 set_irn_link(def, current_def);
238         } else {
239                 set_irn_link(block, def);
240                 set_irn_link(def, NULL);
241         }
242 }
243
244 void be_ssa_construction_init(be_ssa_construction_env_t *env, be_irg_t *birg)
245 {
246         ir_graph *irg = be_get_birg_irg(birg);
247         ir_node *sb   = get_irg_start_block(irg);
248         int n_blocks  = get_Block_dom_max_subtree_pre_num(sb);
249
250         stat_ev_ctx_push_fobj("bessaconstr", irg);
251         stat_ev_tim_push();
252
253         (void) n_blocks;
254         stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
255
256         memset(env, 0, sizeof(env[0]));
257         be_assure_dom_front(birg);
258
259         env->irg       = irg;
260         env->domfronts = be_get_birg_dom_front(birg);
261         env->new_phis  = NEW_ARR_F(ir_node*, 0);
262         env->worklist  = new_waitq();
263
264         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
265                         | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
266
267         /* we use the visited flag to indicate blocks in the dominance frontier
268          * and blocks that already have the relevant value at the end calculated */
269         inc_irg_visited(irg);
270         /* We use the block visited flag to indicate blocks in the dominance
271          * frontier of some values (and this potentially needing phis) */
272         inc_irg_block_visited(irg);
273 }
274
275 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
276 {
277         stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
278         del_waitq(env->worklist);
279         DEL_ARR_F(env->new_phis);
280
281         ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
282                         | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
283
284         stat_ev_tim_pop("bessaconstr_total_time");
285         stat_ev_ctx_pop("bessaconstr");
286 }
287
288 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
289                                   ir_node *copy)
290 {
291         ir_node *block;
292
293         assert(env->iterated_domfront_calculated == 0);
294
295         if(env->mode == NULL) {
296                 env->mode    = get_irn_mode(copy);
297                 env->phi_cls = arch_get_irn_reg_class_out(copy);
298         } else {
299                 assert(env->mode == get_irn_mode(copy));
300         }
301
302         block = get_nodes_block(copy);
303
304         if(!irn_visited(block)) {
305                 waitq_put(env->worklist, block);
306         }
307         introduce_def_at_block(block, copy);
308 }
309
310 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
311                                     ir_node **copies, size_t copies_len)
312 {
313         size_t i;
314
315         assert(env->iterated_domfront_calculated == 0);
316
317         if(env->mode == NULL) {
318                 env->mode    = get_irn_mode(copies[0]);
319                 env->phi_cls = arch_get_irn_reg_class_out(copies[0]);
320         }
321
322         for(i = 0; i < copies_len; ++i) {
323                 ir_node *copy  = copies[i];
324                 ir_node *block = get_nodes_block(copy);
325
326                 assert(env->mode == get_irn_mode(copy));
327                 if(!irn_visited(block)) {
328                         waitq_put(env->worklist, block);
329                 }
330                 introduce_def_at_block(block, copy);
331         }
332 }
333
334 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
335                                          const ir_nodeset_t *ignore_uses)
336 {
337         env->ignore_uses = ignore_uses;
338 }
339
340 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
341 {
342         return env->new_phis;
343 }
344
345 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
346                                          ir_node **nodes, size_t nodes_len)
347 {
348         const ir_edge_t *edge, *next;
349         size_t i;
350         stat_ev_cnt_decl(uses);
351
352         be_timer_push(T_SSA_CONSTR);
353
354         if(!env->iterated_domfront_calculated) {
355                 mark_iterated_dominance_frontiers(env);
356                 env->iterated_domfront_calculated = 1;
357         }
358
359         stat_ev_tim_push();
360         for(i = 0; i < nodes_len; ++i) {
361                 ir_node *value = nodes[i];
362
363                 /*
364                  * Search the valid def for each use and set it.
365                  */
366                 foreach_out_edge_safe(value, edge, next) {
367                         ir_node *use = get_edge_src_irn(edge);
368                         ir_node *at  = use;
369                         int pos      = get_edge_src_pos(edge);
370                         ir_node *def;
371
372                         if(env->ignore_uses != NULL     &&
373                            ir_nodeset_contains(env->ignore_uses, use))
374                                 continue;
375                         if(is_Anchor(use) || is_End(use))
376                                 continue;
377
378                         if(is_Phi(use)) {
379                                 ir_node *block     = get_nodes_block(use);
380                                 ir_node *predblock = get_Block_cfgpred_block(block, pos);
381                                 at = sched_last(predblock);
382                         }
383
384                         def = search_def(env, at);
385
386                         if(def == NULL) {
387                                 panic("no definition found for %+F at position %d", use, pos);
388                         }
389
390                         DBG((dbg, LEVEL_2, "\t%+F(%d) -> %+F\n", use, pos, def));
391                         set_irn_n(use, pos, def);
392                         stat_ev_cnt_inc(uses);
393                 }
394         }
395         be_timer_pop(T_SSA_CONSTR);
396
397         stat_ev_tim_pop("bessaconstr_fix_time");
398         stat_ev_cnt_done(uses, "bessaconstr_uses");
399 }
400
401 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
402 {
403         be_ssa_construction_fix_users_array(env, &value, 1);
404 }
405
406
407 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
408                                               be_lv_t *lv)
409 {
410         int i, n;
411
412         be_timer_push(T_SSA_CONSTR);
413
414         n = ARR_LEN(env->new_phis);
415         for(i = 0; i < n; ++i) {
416                 ir_node *phi = env->new_phis[i];
417                 be_liveness_introduce(lv, phi);
418         }
419
420         be_timer_pop(T_SSA_CONSTR);
421 }
422
423 void be_init_ssaconstr(void)
424 {
425         FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");
426 }
427
428 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr);