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