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