8dc3bfdf27ed66ece072db71004997b5bfb91ab3
[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  *              Sebastian Buchwald
25  * @date        04.05.2005
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 #include "irnodeset.h"
68
69 #include "ircons.h"
70 #include "iredges_t.h"
71 #include "statev_t.h"
72
73 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
74
75 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
76                                         ir_node *block);
77
78 struct constr_info {
79         bool is_definition     : 1;
80         bool is_use            : 1;
81         bool already_processed : 1;
82         union {
83                 /* Since we only consider scheduled nodes,
84                  * this points to the real definition (e.g. a Proj). */
85                 ir_node *definition;
86
87                 /* Last definition of a block. */
88                 ir_node *last_definition;
89         } u;
90 };
91
92 typedef struct constr_info constr_info;
93
94 /**
95  * @return Whether the block contains a definition.
96  */
97 static bool has_definition(const ir_node *block)
98 {
99         return irn_visited(block);
100 }
101
102 static constr_info *get_or_set_info(be_ssa_construction_env_t *env,
103                                     const ir_node *node)
104 {
105         constr_info *info = ir_nodemap_get(constr_info, &env->infos, node);
106         if (info == NULL) {
107                 info = OALLOCZ(&env->obst, constr_info);
108                 ir_nodemap_insert(&env->infos, node, info);
109         }
110         return info;
111 }
112
113 static constr_info *get_info(const be_ssa_construction_env_t *env,
114                              const ir_node *node)
115 {
116         return ir_nodemap_get(constr_info, &env->infos, node);
117 }
118
119 /**
120  * @return Whether the block contains a use.
121  */
122 static inline bool has_use(be_ssa_construction_env_t *env, ir_node *block)
123 {
124         constr_info *info = get_or_set_info(env, block);
125         return info->is_use;
126 }
127
128 /**
129  * @return Whether the node is a definition.
130  */
131 static bool is_definition(be_ssa_construction_env_t *env, ir_node *node)
132 {
133         constr_info *info = get_info(env, node);
134         return info != NULL && info->is_definition;
135 }
136
137 /**
138  * Introduces a definition at the corresponding block.
139  */
140 static void introduce_definition(be_ssa_construction_env_t *env, ir_node *def)
141 {
142         ir_node     *block      = get_nodes_block(def);
143         constr_info *def_info   = get_or_set_info(env, def);
144         ir_node     *skip       = skip_Proj(def);
145         constr_info *skip_info  = get_or_set_info(env, skip);
146         constr_info *block_info = get_or_set_info(env, block);
147
148         DBG((dbg, LEVEL_2, "\tintroducing definition %+F in %+F\n", def, block));
149
150         def_info->is_definition = true;
151
152         skip_info->is_definition = true;
153         skip_info->u.definition  = def;
154
155         // Set the last definition if we only introduce one definition for the block
156         if (has_definition(block)) {
157                 assert(!block_info->already_processed);
158                 block_info->u.last_definition = NULL;
159         } else {
160                 mark_irn_visited(block);
161                 block_info->u.last_definition = def;
162         }
163 }
164
165 static void introduce_use(be_ssa_construction_env_t *env, ir_node *use)
166 {
167         ir_node     *block      = get_nodes_block(use);
168         constr_info *info       = get_or_set_info(env, use);
169         constr_info *block_info = get_or_set_info(env, block);
170
171         DBG((dbg, LEVEL_2, "\tintroducing use %+F in %+F\n", use, block));
172
173         info->is_use       = true;
174         block_info->is_use = true;
175
176         waitq_put(env->worklist, use);
177 }
178
179 /**
180  * Calculates the iterated dominance frontier of a set of blocks. Marks the
181  * blocks as visited. Sets the link fields of the blocks in the dominance
182  * frontier to the block itself.
183  */
184 static void mark_iterated_dominance_frontiers(
185                                            const be_ssa_construction_env_t *env)
186 {
187         stat_ev_cnt_decl(blocks);
188         DBG((dbg, LEVEL_3, "Dominance Frontier:"));
189         stat_ev_tim_push();
190         while (!waitq_empty(env->worklist)) {
191                 int i;
192                 ir_node  *block    = (ir_node*)waitq_get(env->worklist);
193                 ir_node **domfront = ir_get_dominance_frontier(block);
194                 int domfront_len = ARR_LEN(domfront);
195
196                 for (i = 0; i < domfront_len; ++i) {
197                         ir_node *y = domfront[i];
198                         if (Block_block_visited(y))
199                                 continue;
200
201                         if (!irn_visited(y)) {
202                                 set_irn_link(y, NULL);
203                                 waitq_put(env->worklist, y);
204                         }
205
206                         DBG((dbg, LEVEL_3, " %+F", y));
207                         mark_Block_block_visited(y);
208                         stat_ev_cnt_inc(blocks);
209                 }
210         }
211         stat_ev_tim_pop("bessaconstr_idf_time");
212         stat_ev_cnt_done(blocks, "bessaconstr_idf_blocks");
213         DBG((dbg, LEVEL_3, "\n"));
214 }
215
216 /**
217  * Inserts a new phi at the given block.
218  *
219  * The constructed phi has only Dummy operands,
220  * but can be used as definition for other nodes.
221  *
222  * @see fix_phi_arguments
223  */
224 static ir_node *insert_dummy_phi(be_ssa_construction_env_t *env, ir_node *block)
225 {
226         int i, n_preds = get_Block_n_cfgpreds(block);
227         ir_graph *irg = get_Block_irg(block);
228         ir_node **ins = ALLOCAN(ir_node*, n_preds);
229         ir_node  *dummy;
230         ir_node  *phi;
231
232         DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
233
234         assert(n_preds > 1);
235
236         dummy = new_r_Dummy(irg, env->mode);
237         for (i = 0; i < n_preds; ++i) {
238                 ins[i] = dummy;
239         }
240         phi = be_new_Phi(block, n_preds, ins, env->mode, env->phi_req);
241         sched_add_after(block, phi);
242         ARR_APP1(ir_node*, env->new_phis, phi);
243
244         DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
245         introduce_definition(env, phi);
246
247         waitq_put(env->worklist, phi);
248
249         return phi;
250 }
251
252 /**
253  * @return Last definition of the immediate dominator.
254  */
255 static ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
256 {
257         ir_node *dom = get_Block_idom(block);
258         assert(dom != NULL);
259         DBG((dbg, LEVEL_3, "\t...continue at idom %+F\n", dom));
260         return search_def_end_of_block(env, dom);
261 }
262
263 /**
264  * Fixes all operands of a use.
265  *
266  * If an operand of the use is a (original) definition,
267  * it will be replaced by the given definition.
268  */
269 static void set_operands(be_ssa_construction_env_t *env, ir_node *use, ir_node *def, constr_info *const use_info)
270 {
271         int arity = get_irn_arity(use);
272         for (int i = 0; i < arity; ++i) {
273                 ir_node *op = get_irn_n(use, i);
274
275                 if (is_definition(env, op)) {
276                         DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", use, i, def));
277                         set_irn_n(use, i, def);
278                 }
279         }
280
281         use_info->already_processed = true;
282 }
283
284 /**
285  * Fixes all uses of the given block.
286  */
287 static void process_block(be_ssa_construction_env_t *env, ir_node *block)
288 {
289         ir_node     *def        = NULL;
290         constr_info *block_info = get_or_set_info(env, block);
291
292         assert(has_definition(block));
293         assert(!block_info->already_processed && "Block already processed");
294
295         DBG((dbg, LEVEL_3, "\tprocessing block  %+F\n", block));
296
297         sched_foreach(block, node) {
298                 constr_info *const info = get_info(env, node);
299                 if (!info)
300                         continue;
301
302                 if (info->is_use && !is_Phi(node)) {
303                         DBG((dbg, LEVEL_3, "\t...found use %+F\n", node));
304
305                         if (def == NULL) {
306                                 /* Create a phi if the block is in the dominance frontier. */
307                                 if (Block_block_visited(block)) {
308                                         def = insert_dummy_phi(env, block);
309                                 }
310                                 else {
311                                         def = get_def_at_idom(env, block);
312                                 }
313                         }
314
315                         set_operands(env, node, def, info);
316                 }
317
318                 if (info->is_definition) {
319                         def = info->u.definition;
320                         DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
321                 }
322         }
323
324         block_info->already_processed = true;
325         block_info->u.last_definition = def;
326 }
327
328 /**
329  * @return Last definition of the given block.
330  */
331 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
332                                         ir_node *block)
333 {
334         constr_info *block_info      = get_or_set_info(env, block);
335         ir_node     *last_definition = block_info->u.last_definition;
336
337         if (last_definition != NULL)
338                 return last_definition;
339
340         if (has_definition(block)) {
341                 if (has_use(env, block)) {
342                         if (!block_info->already_processed) {
343                                 process_block(env, block);
344                         }
345                 }
346                 else {
347                         /* Search the last definition of the block. */
348                         sched_foreach_reverse(block, def) {
349                                 constr_info const *const info = get_info(env, def);
350                                 if (info && info->is_definition) {
351                                         DBG((dbg, LEVEL_3, "\t...found definition %+F\n", info->u.definition));
352                                         block_info->u.last_definition = info->u.definition;
353                                         break;
354                                 }
355                         }
356
357                         assert(block_info->u.last_definition && "No definition found");
358                 }
359
360                 return block_info->u.last_definition;
361         } else if (Block_block_visited(block)) {
362                 ir_node *phi = insert_dummy_phi(env, block);
363
364                 block_info->u.last_definition = phi;
365
366                 return phi;
367         } else {
368                 ir_node *def = get_def_at_idom(env, block);
369
370                 block_info->u.last_definition = def;
371
372                 return def;
373         }
374 }
375
376 /**
377  * Fixes all operands of the given use.
378  */
379 static void search_def_at_block(be_ssa_construction_env_t *const env, ir_node *const use, constr_info *const info)
380 {
381         ir_node     *block      = get_nodes_block(use);
382         constr_info *block_info = get_or_set_info(env, block);
383
384         if (block_info->already_processed)
385                 return;
386
387         if (has_definition(block)) {
388                 process_block(env, block);
389         } else if (Block_block_visited(block)) {
390                 ir_node *phi = insert_dummy_phi(env, block);
391
392                 set_operands(env, use, phi, info);
393         } else {
394                 ir_node *def = get_def_at_idom(env, block);
395
396                 set_operands(env, use, def, info);
397         }
398 }
399
400 void be_ssa_construction_init(be_ssa_construction_env_t *env, ir_graph *irg)
401 {
402         ir_node *sb   = get_irg_start_block(irg);
403         int n_blocks  = get_Block_dom_max_subtree_pre_num(sb);
404
405         stat_ev_ctx_push_fmt("bessaconstr", "%+F", irg);
406         stat_ev_tim_push();
407
408         (void) n_blocks;
409         stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
410
411         memset(env, 0, sizeof(env[0]));
412
413         env->irg       = irg;
414         env->new_phis  = NEW_ARR_F(ir_node*, 0);
415         env->worklist  = new_waitq();
416         ir_nodemap_init(&env->infos, irg);
417         obstack_init(&env->obst);
418
419         assure_irg_properties(env->irg,
420                               IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS);
421
422         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
423                         | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
424
425         /* we use the visited flag to indicate blocks in the dominance frontier
426          * and blocks that already have the relevant value at the end calculated */
427         inc_irg_visited(irg);
428         /* We use the block visited flag to indicate blocks in the dominance
429          * frontier of some values (and this potentially needing phis) */
430         inc_irg_block_visited(irg);
431 }
432
433 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
434 {
435         stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
436         obstack_free(&env->obst, NULL);
437         ir_nodemap_destroy(&env->infos);
438         del_waitq(env->worklist);
439         DEL_ARR_F(env->new_phis);
440
441         ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
442                         | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
443
444         stat_ev_tim_pop("bessaconstr_total_time");
445         stat_ev_ctx_pop("bessaconstr");
446 }
447
448 static void determine_phi_req(be_ssa_construction_env_t *env, ir_node *value)
449 {
450         const arch_register_req_t *req   = arch_get_irn_register_req(value);
451         env->mode = get_irn_mode(value);
452         if (req->width == 1) {
453                 env->phi_req = req->cls->class_req;
454         } else {
455                 /* construct a new register req... */
456                 ir_graph            *irg     = get_irn_irg(value);
457                 struct obstack      *obst    = be_get_be_obst(irg);
458                 arch_register_req_t *new_req = OALLOCZ(obst, arch_register_req_t);
459                 new_req->cls   = req->cls;
460                 new_req->type  = req->type & arch_register_req_type_aligned;
461                 new_req->width = req->width;
462                 env->phi_req = new_req;
463         }
464 }
465
466 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
467                                   ir_node *copy)
468 {
469         ir_node *block;
470
471         assert(env->iterated_domfront_calculated == 0);
472
473         if (env->mode == NULL) {
474                 determine_phi_req(env, copy);
475         } else {
476                 assert(env->mode == get_irn_mode(copy));
477         }
478
479         block = get_nodes_block(copy);
480
481         if (!has_definition(block)) {
482                 waitq_put(env->worklist, block);
483         }
484         introduce_definition(env, copy);
485 }
486
487 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
488                                     ir_node **copies, size_t copies_len)
489 {
490         size_t i;
491
492         assert(env->iterated_domfront_calculated == 0);
493
494         if (env->mode == NULL) {
495                 determine_phi_req(env, copies[0]);
496         }
497
498         for (i = 0; i < copies_len; ++i) {
499                 ir_node *copy  = copies[i];
500                 ir_node *block = get_nodes_block(copy);
501
502                 assert(env->mode == get_irn_mode(copy));
503                 if (!has_definition(block)) {
504                         waitq_put(env->worklist, block);
505                 }
506                 introduce_definition(env, copy);
507         }
508 }
509
510 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
511 {
512         return env->new_phis;
513 }
514
515 /**
516  * Fixes all arguments of a newly constructed phi.
517  *
518  * @see insert_dummy_phi
519  */
520 static void fix_phi_arguments(be_ssa_construction_env_t *const env, ir_node *const phi, constr_info *const info)
521 {
522         ir_node *block   = get_nodes_block(phi);
523         int      n_preds = get_Block_n_cfgpreds(block);
524
525         DBG((dbg, LEVEL_3, "\tfixing phi arguments  %+F\n", phi));
526
527         for (int i = 0; i < n_preds; ++i) {
528                 ir_node *op = get_irn_n(phi, i);
529
530                 if (is_definition(env, op) || is_Dummy(op)) {
531                         ir_node *pred_block = get_Block_cfgpred_block(block, i);
532                         ir_node *pred_def   = search_def_end_of_block(env, pred_block);
533
534                         DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", phi, i, pred_def));
535                         set_irn_n(phi, i, pred_def);
536                 }
537         }
538
539         info->already_processed = true;
540 }
541
542 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
543                                          ir_node **nodes, size_t nodes_len)
544 {
545         size_t i;
546         stat_ev_cnt_decl(uses);
547
548         be_timer_push(T_SSA_CONSTR);
549
550         if (!env->iterated_domfront_calculated) {
551                 mark_iterated_dominance_frontiers(env);
552                 env->iterated_domfront_calculated = 1;
553         }
554
555         DBG((dbg, LEVEL_1, "\tfixing users array\n"));
556
557         assert(waitq_empty(env->worklist));
558
559         stat_ev_tim_push();
560
561         for (i = 0; i < nodes_len; ++i) {
562                 ir_node *value = nodes[i];
563                 DBG((dbg, LEVEL_3, "\tfixing users of %+F\n", value));
564                 introduce_definition(env, value);
565
566                 foreach_out_edge_safe(value, edge) {
567                         ir_node *const use = get_edge_src_irn(edge);
568                         if (is_Anchor(use) || is_End(use))
569                                 continue;
570
571                         introduce_use(env, use);
572                 }
573         }
574
575         assert(!waitq_empty(env->worklist));
576
577         while (!waitq_empty(env->worklist)) {
578                 ir_node     *use  = (ir_node *)waitq_get(env->worklist);
579                 constr_info *info = get_info(env, use);
580
581                 if (info->already_processed)
582                         continue;
583
584                 if (is_Phi(use)) {
585                         fix_phi_arguments(env, use, info);
586                 }
587                 else {
588                         DBG((dbg, LEVEL_3, "\tsearching def for %+F at %+F\n", use, get_nodes_block(use)));
589                         search_def_at_block(env, use, info);
590                 }
591
592                 stat_ev_cnt_inc(uses);
593         }
594
595         be_timer_pop(T_SSA_CONSTR);
596
597         stat_ev_tim_pop("bessaconstr_fix_time");
598         stat_ev_cnt_done(uses, "bessaconstr_uses");
599 }
600
601 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
602 {
603         be_ssa_construction_fix_users_array(env, &value, 1);
604 }
605
606
607 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
608                                               be_lv_t *lv)
609 {
610         int i, n;
611
612         be_timer_push(T_SSA_CONSTR);
613
614         n = ARR_LEN(env->new_phis);
615         for (i = 0; i < n; ++i) {
616                 ir_node *phi = env->new_phis[i];
617                 be_liveness_introduce(lv, phi);
618         }
619
620         be_timer_pop(T_SSA_CONSTR);
621 }
622
623 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr)
624 void be_init_ssaconstr(void)
625 {
626         FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");
627 }