merge kaps
[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         if (env->new_phis != NULL) {
244                 ARR_APP1(ir_node*, env->new_phis, phi);
245         }
246
247         DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
248         introduce_definition(env, phi);
249
250         waitq_put(env->worklist, phi);
251
252         return phi;
253 }
254
255 /**
256  * @return Last definition of the immediate dominator.
257  */
258 static ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
259 {
260         ir_node *dom = get_Block_idom(block);
261         assert(dom != NULL);
262         DBG((dbg, LEVEL_3, "\t...continue at idom %+F\n", dom));
263         return search_def_end_of_block(env, dom);
264 }
265
266 /**
267  * Fixes all operands of a use.
268  *
269  * If an operand of the use is a (original) definition,
270  * it will be replaced by the given definition.
271  */
272 static void set_operands(be_ssa_construction_env_t *env, ir_node *use, ir_node *def)
273 {
274         constr_info *info  = phase_get_irn_data(env->phase, use);
275         int          arity = get_irn_arity(use);
276         int          i;
277
278         for (i = 0; i < arity; ++i) {
279                 ir_node *op = get_irn_n(use, i);
280
281                 if (is_definition(env, op)) {
282                         DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", use, i, def));
283                         set_irn_n(use, i, def);
284                 }
285         }
286
287         info->already_processed = true;
288 }
289
290 /**
291  * Fixes all uses of the given block.
292  */
293 static void process_block(be_ssa_construction_env_t *env, ir_node *block)
294 {
295         ir_node     *node;
296         ir_node     *def        = NULL;
297         constr_info *block_info = phase_get_or_set_irn_data(env->phase, block);
298
299         assert(has_definition(env, block));
300         assert(!block_info->already_processed && "Block already processed");
301
302         DBG((dbg, LEVEL_3, "\tprocessing block  %+F\n", block));
303
304         sched_foreach(block, node) {
305                 if (is_use(env, node) && !is_Phi(node)) {
306                         DBG((dbg, LEVEL_3, "\t...found use %+F\n", node));
307
308                         if (def == NULL) {
309                                 /* Create a phi if the block is in the dominance frontier. */
310                                 if (Block_block_visited(block)) {
311                                         def = insert_dummy_phi(env, block);
312                                 }
313                                 else {
314                                         def = get_def_at_idom(env, block);
315                                 }
316                         }
317
318                         set_operands(env, node, def);
319                 }
320
321                 if (is_definition(env, node)) {
322                         constr_info *info = phase_get_irn_data(env->phase, node);
323                         def = info->definition;
324                         DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
325                 }
326         }
327
328         block_info->already_processed = true;
329         block_info->last_definition   = def;
330 }
331
332 /**
333  * @return Last definition of the given block.
334  */
335 static ir_node *search_def_end_of_block(be_ssa_construction_env_t *env,
336                                         ir_node *block)
337 {
338         constr_info *block_info      = phase_get_or_set_irn_data(env->phase, block);
339         ir_node     *last_definition = block_info->last_definition;
340
341         if (last_definition != NULL)
342                 return last_definition;
343
344         if (has_definition(env, block)) {
345                 if (has_use(env, block)) {
346                         if (!block_info->already_processed) {
347                                 process_block(env, block);
348                         }
349                 }
350                 else {
351                         ir_node *def = NULL;
352
353                         /* Search the last definition of the block. */
354                         sched_foreach_reverse(block, def) {
355                                 if (is_definition(env, def)) {
356                                         constr_info *info = phase_get_irn_data(env->phase, def);
357                                         def = info->definition;
358                                         DBG((dbg, LEVEL_3, "\t...found definition %+F\n", def));
359
360                                         break;
361                                 }
362                         }
363
364                         assert(def && "No definition found");
365
366                         block_info->last_definition = def;
367                 }
368
369                 return block_info->last_definition;
370         } else if (Block_block_visited(block)) {
371                 ir_node *phi = insert_dummy_phi(env, block);
372
373                 block_info->last_definition = phi;
374
375                 return phi;
376         } else {
377                 ir_node *def = get_def_at_idom(env, block);
378
379                 block_info->last_definition = def;
380
381                 return def;
382         }
383 }
384
385 /**
386  * Fixes all operands of the given use.
387  */
388 static void search_def_at_block(be_ssa_construction_env_t *env, ir_node *use)
389 {
390         ir_node     *block      = get_nodes_block(use);
391         constr_info *block_info = phase_get_or_set_irn_data(env->phase, block);
392
393         if (block_info->already_processed)
394                 return;
395
396         if (has_definition(env, block)) {
397                 process_block(env, block);
398         } else if (Block_block_visited(block)) {
399                 ir_node *phi = insert_dummy_phi(env, block);
400
401                 set_operands(env, use, phi);
402         } else {
403                 ir_node *def = get_def_at_idom(env, block);
404
405                 set_operands(env, use, def);
406         }
407 }
408
409 static void *init_constr_info(ir_phase *phase, const ir_node *node)
410 {
411         constr_info *info = phase_alloc(phase, sizeof(constr_info));
412         (void)node;
413
414         info->is_definition     = false;
415         info->is_use            = false;
416         info->already_processed = false;
417         info->definition        = NULL;
418
419         return info;
420 }
421
422 void be_ssa_construction_init(be_ssa_construction_env_t *env, ir_graph *irg)
423 {
424         ir_node *sb   = get_irg_start_block(irg);
425         int n_blocks  = get_Block_dom_max_subtree_pre_num(sb);
426
427         stat_ev_ctx_push_fobj("bessaconstr", irg);
428         stat_ev_tim_push();
429
430         (void) n_blocks;
431         stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
432
433         memset(env, 0, sizeof(env[0]));
434         be_assure_dom_front(irg);
435
436         env->irg       = irg;
437         env->domfronts = be_get_irg_dom_front(irg);
438         env->new_phis  = NEW_ARR_F(ir_node*, 0);
439         env->worklist  = new_waitq();
440         env->phase     = new_phase(irg, init_constr_info);
441
442         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
443                         | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
444
445         /* we use the visited flag to indicate blocks in the dominance frontier
446          * and blocks that already have the relevant value at the end calculated */
447         inc_irg_visited(irg);
448         /* We use the block visited flag to indicate blocks in the dominance
449          * frontier of some values (and this potentially needing phis) */
450         inc_irg_block_visited(irg);
451 }
452
453 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
454 {
455         stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
456         phase_free(env->phase);
457         del_waitq(env->worklist);
458         DEL_ARR_F(env->new_phis);
459
460         ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
461                         | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
462
463         stat_ev_tim_pop("bessaconstr_total_time");
464         stat_ev_ctx_pop("bessaconstr");
465 }
466
467 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
468                                   ir_node *copy)
469 {
470         ir_node *block;
471
472         assert(env->iterated_domfront_calculated == 0);
473
474         if (env->mode == NULL) {
475                 env->mode    = get_irn_mode(copy);
476                 env->phi_cls = arch_get_irn_reg_class_out(copy);
477         } else {
478                 assert(env->mode == get_irn_mode(copy));
479         }
480
481         block = get_nodes_block(copy);
482
483         if (!has_definition(env, block)) {
484                 waitq_put(env->worklist, block);
485         }
486         introduce_definition(env, copy);
487 }
488
489 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
490                                     ir_node **copies, size_t copies_len)
491 {
492         size_t i;
493
494         assert(env->iterated_domfront_calculated == 0);
495
496         if (env->mode == NULL) {
497                 env->mode    = get_irn_mode(copies[0]);
498                 env->phi_cls = arch_get_irn_reg_class_out(copies[0]);
499         }
500
501         for (i = 0; i < copies_len; ++i) {
502                 ir_node *copy  = copies[i];
503                 ir_node *block = get_nodes_block(copy);
504
505                 assert(env->mode == get_irn_mode(copy));
506                 if (!has_definition(env, block)) {
507                         waitq_put(env->worklist, block);
508                 }
509                 introduce_definition(env, copy);
510         }
511 }
512
513 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
514                                          const ir_nodeset_t *ignore_uses)
515 {
516         env->ignore_uses = ignore_uses;
517 }
518
519 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
520 {
521         return env->new_phis;
522 }
523
524 /**
525  * Fixes all arguments of a newly constructed phi.
526  *
527  * @see insert_dummy_phi
528  */
529 static void fix_phi_arguments(be_ssa_construction_env_t *env, ir_node *phi)
530 {
531         constr_info *info    = phase_get_irn_data(env->phase, phi);
532         ir_node     *block   = get_nodes_block(phi);
533         int          i;
534         int          n_preds = get_Block_n_cfgpreds(block);
535
536         DBG((dbg, LEVEL_3, "\tfixing phi arguments  %+F\n", phi));
537
538         for (i = 0; i < n_preds; ++i) {
539                 ir_node *op = get_irn_n(phi, i);
540
541                 if (is_definition(env, op) || is_Dummy(op)) {
542                         ir_node *pred_block = get_Block_cfgpred_block(block, i);
543                         ir_node *pred_def   = search_def_end_of_block(env, pred_block);
544
545                         DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", phi, i, pred_def));
546                         set_irn_n(phi, i, pred_def);
547                 }
548         }
549
550         info->already_processed = true;
551 }
552
553 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
554                                          ir_node **nodes, size_t nodes_len)
555 {
556         size_t i;
557         stat_ev_cnt_decl(uses);
558
559         be_timer_push(T_SSA_CONSTR);
560
561         if (!env->iterated_domfront_calculated) {
562                 mark_iterated_dominance_frontiers(env);
563                 env->iterated_domfront_calculated = 1;
564         }
565
566         DBG((dbg, LEVEL_1, "\tfixing users array\n"));
567
568         assert(waitq_empty(env->worklist));
569
570         stat_ev_tim_push();
571
572         for (i = 0; i < nodes_len; ++i) {
573                 const ir_edge_t *edge, *next;
574                 ir_node *value = nodes[i];
575                 DBG((dbg, LEVEL_3, "\tfixing users of %+F\n", value));
576                 introduce_definition(env, value);
577
578                 foreach_out_edge_safe(value, edge, next) {
579                         ir_node *use   = get_edge_src_irn(edge);
580
581                         if (env->ignore_uses != NULL &&
582                                         ir_nodeset_contains(env->ignore_uses, use))
583                                 continue;
584
585                         if (is_Anchor(use) || is_End(use))
586                                 continue;
587
588                         introduce_use(env, use);
589                 }
590         }
591
592         assert(!waitq_empty(env->worklist));
593
594         while (!waitq_empty(env->worklist)) {
595                 ir_node     *use  = (ir_node *)waitq_get(env->worklist);
596                 constr_info *info = phase_get_irn_data(env->phase, use);
597
598                 if (info->already_processed)
599                         continue;
600
601                 if (is_Phi(use)) {
602                         fix_phi_arguments(env, use);
603                 }
604                 else {
605                         DBG((dbg, LEVEL_3, "\tsearching def for %+F at %+F\n", use, get_nodes_block(use)));
606                         search_def_at_block(env, use);
607                 }
608
609                 stat_ev_cnt_inc(uses);
610         }
611
612         be_timer_pop(T_SSA_CONSTR);
613
614         stat_ev_tim_pop("bessaconstr_fix_time");
615         stat_ev_cnt_done(uses, "bessaconstr_uses");
616 }
617
618 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
619 {
620         be_ssa_construction_fix_users_array(env, &value, 1);
621 }
622
623
624 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
625                                               be_lv_t *lv)
626 {
627         int i, n;
628
629         be_timer_push(T_SSA_CONSTR);
630
631         n = ARR_LEN(env->new_phis);
632         for (i = 0; i < n; ++i) {
633                 ir_node *phi = env->new_phis[i];
634                 be_liveness_introduce(lv, phi);
635         }
636
637         be_timer_pop(T_SSA_CONSTR);
638 }
639
640 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr);
641 void be_init_ssaconstr(void)
642 {
643         FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");
644 }