ia32: Inline get_ia32_orig_node() into its only caller.
[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  * @return Whether the node is a use.
139  */
140 static bool is_use(const be_ssa_construction_env_t *env, ir_node *node)
141 {
142         constr_info *info = get_info(env, node);
143         return info != NULL && info->is_use;
144 }
145
146 /**
147  * Introduces a definition at the corresponding block.
148  */
149 static void introduce_definition(be_ssa_construction_env_t *env, ir_node *def)
150 {
151         ir_node     *block      = get_nodes_block(def);
152         constr_info *def_info   = get_or_set_info(env, def);
153         ir_node     *skip       = skip_Proj(def);
154         constr_info *skip_info  = get_or_set_info(env, skip);
155         constr_info *block_info = get_or_set_info(env, block);
156
157         DBG((dbg, LEVEL_2, "\tintroducing definition %+F in %+F\n", def, block));
158
159         def_info->is_definition = true;
160
161         skip_info->is_definition = true;
162         skip_info->u.definition  = def;
163
164         // Set the last definition if we only introduce one definition for the block
165         if (has_definition(block)) {
166                 assert(!block_info->already_processed);
167                 block_info->u.last_definition = NULL;
168         } else {
169                 mark_irn_visited(block);
170                 block_info->u.last_definition = def;
171         }
172 }
173
174 static void introduce_use(be_ssa_construction_env_t *env, ir_node *use)
175 {
176         ir_node     *block      = get_nodes_block(use);
177         constr_info *info       = get_or_set_info(env, use);
178         constr_info *block_info = get_or_set_info(env, block);
179
180         DBG((dbg, LEVEL_2, "\tintroducing use %+F in %+F\n", use, block));
181
182         info->is_use       = true;
183         block_info->is_use = true;
184
185         waitq_put(env->worklist, use);
186 }
187
188 /**
189  * Calculates the iterated dominance frontier of a set of blocks. Marks the
190  * blocks as visited. Sets the link fields of the blocks in the dominance
191  * frontier to the block itself.
192  */
193 static void mark_iterated_dominance_frontiers(
194                                            const be_ssa_construction_env_t *env)
195 {
196         stat_ev_cnt_decl(blocks);
197         DBG((dbg, LEVEL_3, "Dominance Frontier:"));
198         stat_ev_tim_push();
199         while (!waitq_empty(env->worklist)) {
200                 int i;
201                 ir_node  *block    = (ir_node*)waitq_get(env->worklist);
202                 ir_node **domfront = ir_get_dominance_frontier(block);
203                 int domfront_len = ARR_LEN(domfront);
204
205                 for (i = 0; i < domfront_len; ++i) {
206                         ir_node *y = domfront[i];
207                         if (Block_block_visited(y))
208                                 continue;
209
210                         if (!irn_visited(y)) {
211                                 set_irn_link(y, NULL);
212                                 waitq_put(env->worklist, y);
213                         }
214
215                         DBG((dbg, LEVEL_3, " %+F", y));
216                         mark_Block_block_visited(y);
217                         stat_ev_cnt_inc(blocks);
218                 }
219         }
220         stat_ev_tim_pop("bessaconstr_idf_time");
221         stat_ev_cnt_done(blocks, "bessaconstr_idf_blocks");
222         DBG((dbg, LEVEL_3, "\n"));
223 }
224
225 /**
226  * Inserts a new phi at the given block.
227  *
228  * The constructed phi has only Dummy operands,
229  * but can be used as definition for other nodes.
230  *
231  * @see fix_phi_arguments
232  */
233 static ir_node *insert_dummy_phi(be_ssa_construction_env_t *env, ir_node *block)
234 {
235         int i, n_preds = get_Block_n_cfgpreds(block);
236         ir_graph *irg = get_Block_irg(block);
237         ir_node **ins = ALLOCAN(ir_node*, n_preds);
238         ir_node  *dummy;
239         ir_node  *phi;
240
241         DBG((dbg, LEVEL_3, "\t...create phi at block %+F\n", block));
242
243         assert(n_preds > 1);
244
245         dummy = new_r_Dummy(irg, env->mode);
246         for (i = 0; i < n_preds; ++i) {
247                 ins[i] = dummy;
248         }
249         phi = be_new_Phi(block, n_preds, ins, env->mode, env->phi_req);
250         sched_add_after(block, phi);
251         ARR_APP1(ir_node*, env->new_phis, phi);
252
253         DBG((dbg, LEVEL_2, "\tcreating phi %+F in %+F\n", phi, block));
254         introduce_definition(env, phi);
255
256         waitq_put(env->worklist, phi);
257
258         return phi;
259 }
260
261 /**
262  * @return Last definition of the immediate dominator.
263  */
264 static ir_node *get_def_at_idom(be_ssa_construction_env_t *env, ir_node *block)
265 {
266         ir_node *dom = get_Block_idom(block);
267         assert(dom != NULL);
268         DBG((dbg, LEVEL_3, "\t...continue at idom %+F\n", dom));
269         return search_def_end_of_block(env, dom);
270 }
271
272 /**
273  * Fixes all operands of a use.
274  *
275  * If an operand of the use is a (original) definition,
276  * it will be replaced by the given definition.
277  */
278 static void set_operands(be_ssa_construction_env_t *env, ir_node *use, ir_node *def)
279 {
280         constr_info *info  = get_info(env, use);
281         int          arity = get_irn_arity(use);
282         int          i;
283
284         for (i = 0; i < arity; ++i) {
285                 ir_node *op = get_irn_n(use, i);
286
287                 if (is_definition(env, op)) {
288                         DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", use, i, def));
289                         set_irn_n(use, i, def);
290                 }
291         }
292
293         info->already_processed = true;
294 }
295
296 /**
297  * Fixes all uses of the given block.
298  */
299 static void process_block(be_ssa_construction_env_t *env, ir_node *block)
300 {
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                         /* Search the last definition of the block. */
357                         sched_foreach_reverse(block, def) {
358                                 if (is_definition(env, def)) {
359                                         constr_info *info = get_info(env, def);
360                                         DBG((dbg, LEVEL_3, "\t...found definition %+F\n", info->u.definition));
361                                         block_info->u.last_definition = info->u.definition;
362                                         break;
363                                 }
364                         }
365
366                         assert(block_info->u.last_definition && "No definition found");
367                 }
368
369                 return block_info->u.last_definition;
370         } else if (Block_block_visited(block)) {
371                 ir_node *phi = insert_dummy_phi(env, block);
372
373                 block_info->u.last_definition = phi;
374
375                 return phi;
376         } else {
377                 ir_node *def = get_def_at_idom(env, block);
378
379                 block_info->u.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 = get_or_set_info(env, block);
392
393         if (block_info->already_processed)
394                 return;
395
396         if (has_definition(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 void be_ssa_construction_init(be_ssa_construction_env_t *env, ir_graph *irg)
410 {
411         ir_node *sb   = get_irg_start_block(irg);
412         int n_blocks  = get_Block_dom_max_subtree_pre_num(sb);
413
414         stat_ev_ctx_push_fmt("bessaconstr", "%+F", irg);
415         stat_ev_tim_push();
416
417         (void) n_blocks;
418         stat_ev_dbl("bessaconstr_n_blocks", n_blocks);
419
420         memset(env, 0, sizeof(env[0]));
421
422         env->irg       = irg;
423         env->new_phis  = NEW_ARR_F(ir_node*, 0);
424         env->worklist  = new_waitq();
425         ir_nodemap_init(&env->infos, irg);
426         obstack_init(&env->obst);
427
428         assure_irg_properties(env->irg,
429                               IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS);
430
431         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED
432                         | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
433
434         /* we use the visited flag to indicate blocks in the dominance frontier
435          * and blocks that already have the relevant value at the end calculated */
436         inc_irg_visited(irg);
437         /* We use the block visited flag to indicate blocks in the dominance
438          * frontier of some values (and this potentially needing phis) */
439         inc_irg_block_visited(irg);
440 }
441
442 void be_ssa_construction_destroy(be_ssa_construction_env_t *env)
443 {
444         stat_ev_int("bessaconstr_phis", ARR_LEN(env->new_phis));
445         obstack_free(&env->obst, NULL);
446         ir_nodemap_destroy(&env->infos);
447         del_waitq(env->worklist);
448         DEL_ARR_F(env->new_phis);
449
450         ir_free_resources(env->irg, IR_RESOURCE_IRN_VISITED
451                         | IR_RESOURCE_BLOCK_VISITED | IR_RESOURCE_IRN_LINK);
452
453         stat_ev_tim_pop("bessaconstr_total_time");
454         stat_ev_ctx_pop("bessaconstr");
455 }
456
457 static void determine_phi_req(be_ssa_construction_env_t *env, ir_node *value)
458 {
459         const arch_register_req_t *req   = arch_get_irn_register_req(value);
460         env->mode = get_irn_mode(value);
461         if (req->width == 1) {
462                 env->phi_req = req->cls->class_req;
463         } else {
464                 /* construct a new register req... */
465                 ir_graph            *irg     = get_irn_irg(value);
466                 struct obstack      *obst    = be_get_be_obst(irg);
467                 arch_register_req_t *new_req = OALLOCZ(obst, arch_register_req_t);
468                 new_req->cls   = req->cls;
469                 new_req->type  = req->type & arch_register_req_type_aligned;
470                 new_req->width = req->width;
471                 env->phi_req = new_req;
472         }
473 }
474
475 void be_ssa_construction_add_copy(be_ssa_construction_env_t *env,
476                                   ir_node *copy)
477 {
478         ir_node *block;
479
480         assert(env->iterated_domfront_calculated == 0);
481
482         if (env->mode == NULL) {
483                 determine_phi_req(env, copy);
484         } else {
485                 assert(env->mode == get_irn_mode(copy));
486         }
487
488         block = get_nodes_block(copy);
489
490         if (!has_definition(block)) {
491                 waitq_put(env->worklist, block);
492         }
493         introduce_definition(env, copy);
494 }
495
496 void be_ssa_construction_add_copies(be_ssa_construction_env_t *env,
497                                     ir_node **copies, size_t copies_len)
498 {
499         size_t i;
500
501         assert(env->iterated_domfront_calculated == 0);
502
503         if (env->mode == NULL) {
504                 determine_phi_req(env, copies[0]);
505         }
506
507         for (i = 0; i < copies_len; ++i) {
508                 ir_node *copy  = copies[i];
509                 ir_node *block = get_nodes_block(copy);
510
511                 assert(env->mode == get_irn_mode(copy));
512                 if (!has_definition(block)) {
513                         waitq_put(env->worklist, block);
514                 }
515                 introduce_definition(env, copy);
516         }
517 }
518
519 void be_ssa_construction_set_ignore_uses(be_ssa_construction_env_t *env,
520                                          const ir_nodeset_t *ignore_uses)
521 {
522         env->ignore_uses = ignore_uses;
523 }
524
525 ir_node **be_ssa_construction_get_new_phis(be_ssa_construction_env_t *env)
526 {
527         return env->new_phis;
528 }
529
530 /**
531  * Fixes all arguments of a newly constructed phi.
532  *
533  * @see insert_dummy_phi
534  */
535 static void fix_phi_arguments(be_ssa_construction_env_t *env, ir_node *phi)
536 {
537         constr_info *info    = get_info(env, phi);
538         ir_node     *block   = get_nodes_block(phi);
539         int          i;
540         int          n_preds = get_Block_n_cfgpreds(block);
541
542         DBG((dbg, LEVEL_3, "\tfixing phi arguments  %+F\n", phi));
543
544         for (i = 0; i < n_preds; ++i) {
545                 ir_node *op = get_irn_n(phi, i);
546
547                 if (is_definition(env, op) || is_Dummy(op)) {
548                         ir_node *pred_block = get_Block_cfgpred_block(block, i);
549                         ir_node *pred_def   = search_def_end_of_block(env, pred_block);
550
551                         DBG((dbg, LEVEL_1, "\t...%+F(%d) -> %+F\n", phi, i, pred_def));
552                         set_irn_n(phi, i, pred_def);
553                 }
554         }
555
556         info->already_processed = true;
557 }
558
559 void be_ssa_construction_fix_users_array(be_ssa_construction_env_t *env,
560                                          ir_node **nodes, size_t nodes_len)
561 {
562         size_t i;
563         stat_ev_cnt_decl(uses);
564
565         be_timer_push(T_SSA_CONSTR);
566
567         if (!env->iterated_domfront_calculated) {
568                 mark_iterated_dominance_frontiers(env);
569                 env->iterated_domfront_calculated = 1;
570         }
571
572         DBG((dbg, LEVEL_1, "\tfixing users array\n"));
573
574         assert(waitq_empty(env->worklist));
575
576         stat_ev_tim_push();
577
578         for (i = 0; i < nodes_len; ++i) {
579                 ir_node *value = nodes[i];
580                 DBG((dbg, LEVEL_3, "\tfixing users of %+F\n", value));
581                 introduce_definition(env, value);
582
583                 foreach_out_edge_safe(value, edge) {
584                         ir_node *use   = get_edge_src_irn(edge);
585
586                         if (env->ignore_uses != NULL &&
587                                         ir_nodeset_contains(env->ignore_uses, use))
588                                 continue;
589
590                         if (is_Anchor(use) || is_End(use))
591                                 continue;
592
593                         introduce_use(env, use);
594                 }
595         }
596
597         assert(!waitq_empty(env->worklist));
598
599         while (!waitq_empty(env->worklist)) {
600                 ir_node     *use  = (ir_node *)waitq_get(env->worklist);
601                 constr_info *info = get_info(env, use);
602
603                 if (info->already_processed)
604                         continue;
605
606                 if (is_Phi(use)) {
607                         fix_phi_arguments(env, use);
608                 }
609                 else {
610                         DBG((dbg, LEVEL_3, "\tsearching def for %+F at %+F\n", use, get_nodes_block(use)));
611                         search_def_at_block(env, use);
612                 }
613
614                 stat_ev_cnt_inc(uses);
615         }
616
617         be_timer_pop(T_SSA_CONSTR);
618
619         stat_ev_tim_pop("bessaconstr_fix_time");
620         stat_ev_cnt_done(uses, "bessaconstr_uses");
621 }
622
623 void be_ssa_construction_fix_users(be_ssa_construction_env_t *env, ir_node *value)
624 {
625         be_ssa_construction_fix_users_array(env, &value, 1);
626 }
627
628
629 void be_ssa_construction_update_liveness_phis(be_ssa_construction_env_t *env,
630                                               be_lv_t *lv)
631 {
632         int i, n;
633
634         be_timer_push(T_SSA_CONSTR);
635
636         n = ARR_LEN(env->new_phis);
637         for (i = 0; i < n; ++i) {
638                 ir_node *phi = env->new_phis[i];
639                 be_liveness_introduce(lv, phi);
640         }
641
642         be_timer_pop(T_SSA_CONSTR);
643 }
644
645 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_ssaconstr)
646 void be_init_ssaconstr(void)
647 {
648         FIRM_DBG_REGISTER(dbg, "firm.be.ssaconstr");
649 }