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