52dc3df93a5b5622ea06b98a011deb2c368b08fc
[libfirm] / ir / be / bestate.c
1 /*
2  * Copyright (C) 1995-2011 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       Handles state switching. This is basically the belady spill
23  *              algorithm optimized for the 1-register case.
24  * @author      Matthias Braun
25  * @date        26.03.2007
26  */
27 #include "config.h"
28
29 #include "bestate.h"
30
31 #include "obst.h"
32 #include "irgraph_t.h"
33 #include "irnode_t.h"
34 #include "irgwalk.h"
35 #include "irloop.h"
36 #include "iredges_t.h"
37 #include "ircons_t.h"
38 #include "irgmod.h"
39 #include "irnodeset.h"
40 #include "irnodehashmap.h"
41 #include "cpset.h"
42
43 #include "bearch.h"
44 #include "beirg.h"
45 #include "beuses.h"
46 #include "besched.h"
47 #include "belive_t.h"
48 #include "bemodule.h"
49 #include "benode.h"
50 #include "beirgmod.h"
51 #include "bessaconstr.h"
52
53 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
54
55 typedef struct spill_info_t {
56         struct spill_info_t *next;
57         ir_node *value;
58         ir_node *spill;
59         ir_node **reloads;
60 } spill_info_t;
61
62 typedef struct minibelady_env_t {
63         struct obstack         obst;
64         const arch_register_t *reg;
65         const be_lv_t         *lv;
66         void                  *func_env;
67         create_reload_func     create_reload;
68         create_spill_func      create_spill;
69         spill_info_t          *spills;
70         ir_nodehashmap_t       spill_infos;
71
72         be_uses_t             *uses;           /**< env for the next-use magic */
73 } minibelady_env_t;
74
75 typedef struct block_info_t {
76         ir_node *start_state;
77         ir_node *end_state;
78 } block_info_t;
79
80 static inline block_info_t *new_block_info(struct obstack *obst, ir_node *block)
81 {
82         block_info_t *res = OALLOCZ(obst, block_info_t);
83
84         assert(is_Block(block));
85         set_irn_link(block, res);
86         mark_irn_visited(block);
87
88         return res;
89 }
90
91 static inline block_info_t *get_block_info(ir_node *block)
92 {
93         assert(irn_visited(block));
94         return (block_info_t*) get_irn_link(block);
95 }
96
97 static inline spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
98 {
99         spill_info_t *spill_info = OALLOCZ(&env->obst, spill_info_t);
100         spill_info->value = state;
101         spill_info->reloads = NEW_ARR_F(ir_node*, 0);
102
103         ir_nodehashmap_insert(&env->spill_infos, state, spill_info);
104         //ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info);
105
106         spill_info->next = env->spills;
107         env->spills = spill_info;
108
109         return spill_info;
110 }
111
112 static inline spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
113 {
114         spill_info_t *spill_info = ir_nodehashmap_get(spill_info_t, &env->spill_infos, node);
115         //ir_fprintf(stderr, "Get %+F -> %p\n", node, spill_info);
116         return spill_info;
117 }
118
119 static spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
120 {
121         spill_info_t *spill_info;
122         ir_node *next;
123         ir_node *after;
124
125         spill_info = get_spill_info(env, state);
126         if (spill_info == NULL) {
127                 spill_info = create_spill_info(env, state);
128         } else if (spill_info->spill != NULL) {
129                 return spill_info;
130         }
131
132         if (sched_is_scheduled(state)) {
133                 next = state;
134                 do {
135                         after = next;
136                         next = sched_next(after);
137                 } while (is_Proj(next) || is_Phi(next) || be_is_Keep(next));
138         } else {
139                 after = state;
140         }
141         spill_info->spill = env->create_spill(env->func_env, state, force, after);
142
143         return spill_info;
144 }
145
146 static void create_reload(minibelady_env_t *env, ir_node *state,
147                           ir_node *before, ir_node *last_state)
148 {
149         spill_info_t *spill_info = create_spill(env, state, 0);
150         ir_node *spill = spill_info->spill;
151         ir_node *reload;
152
153         reload = env->create_reload(env->func_env, state, spill, before,
154                                     last_state);
155         ARR_APP1(ir_node*, spill_info->reloads, reload);
156 }
157
158 static void spill_phi(minibelady_env_t *env, ir_node *phi)
159 {
160         ir_graph     *irg           = get_irn_irg(phi);
161         ir_node      *block         = get_nodes_block(phi);
162         int           arity         = get_irn_arity(phi);
163         ir_node     **phi_in        = ALLOCAN(ir_node*, arity);
164         ir_node      *dummy         = new_r_Dummy(irg, mode_M);
165         ir_node      *spill_to_kill = NULL;
166         spill_info_t *spill_info;
167         int           i;
168
169         /* does a spill exist for the phis value? */
170         spill_info = get_spill_info(env, phi);
171         if (spill_info != NULL) {
172                 spill_to_kill = spill_info->spill;
173         } else {
174                 spill_info = create_spill_info(env, phi);
175         }
176
177         /* create a new phi-M with bad preds */
178         for (i = 0; i < arity; ++i) {
179                 phi_in[i] = dummy;
180         }
181
182         DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
183
184         /* create a Phi-M */
185         spill_info->spill = be_new_Phi(block, arity, phi_in, mode_M,
186                                        arch_no_register_req);
187         sched_add_after(block, spill_info->spill);
188
189         if (spill_to_kill != NULL) {
190                 exchange(spill_to_kill, spill_info->spill);
191                 sched_remove(spill_to_kill);
192         }
193
194         /* create spills for the phi values */
195         for (i = 0; i < arity; ++i) {
196                 ir_node *in = get_irn_n(phi, i);
197                 spill_info_t *pred_spill = create_spill(env, in, 1);
198                 set_irn_n(spill_info->spill, i, pred_spill->spill);
199         }
200 }
201
202 static void belady(minibelady_env_t *env, ir_node *block);
203
204 /**
205  * Collects all values live-in at block @p block and all phi results in this
206  * block.
207  * Then it adds the best values (at most n_regs) to the blocks start_workset.
208  * The phis among the remaining values get spilled: Introduce pseudo-copies of
209  * their args to break interference and make it possible to spill them to the
210  * same spill slot.
211  */
212 static block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
213 {
214         block_info_t  *block_info;
215         be_next_use_t  next_use;
216         ir_loop       *loop;
217         ir_node       *best_starter, *first;
218         int            n_cfgpreds;
219         unsigned       best_time;
220         int            outer_loop_allowed;
221
222         /* Create the block info for this block. */
223         block_info = new_block_info(&env->obst, block);
224         n_cfgpreds = get_Block_n_cfgpreds(block);
225
226         /* no cfgpred -> no value active */
227         if (n_cfgpreds == 0) {
228                 block_info->start_state = NULL;
229                 return block_info;
230         }
231
232         /* for 1 pred only: simply take the the end-state of the pred */
233         if (n_cfgpreds == 1) {
234                 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
235                 block_info_t *pred_info;
236
237                 /* process pred block */
238                 belady(env, pred_block);
239
240                 pred_info = get_block_info(pred_block);
241
242                 DBG((dbg, LEVEL_2, "Taking end state from %+F: %+F\n", pred_block, pred_info->end_state));
243                 block_info->start_state = pred_info->end_state;
244                 return block_info;
245         }
246
247         /* Collect all values living at start of block */
248         DBG((dbg, LEVEL_2, "Living at start of %+F:\n", block));
249         first = sched_first(block);
250         loop = get_irn_loop(block);
251         best_starter = NULL;
252         best_time = USES_INFINITY;
253         outer_loop_allowed = 1;
254
255         /* check all Phis first */
256         sched_foreach(block, node) {
257                 if (!is_Phi(node))
258                         break;
259                 if (arch_get_irn_register(node) != env->reg)
260                         continue;
261
262                 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
263                 next_use = be_get_next_use(env->uses, first, node, 0);
264
265                 if (USES_IS_INFINITE(next_use.time)) {
266                         DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
267                         continue;
268                 }
269
270                 if (next_use.outermost_loop >= get_loop_depth(loop)) {
271                         if (outer_loop_allowed || next_use.time < best_time) {
272                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
273                                      next_use.outermost_loop));
274
275                                 if (best_starter != NULL) {
276                                         /* spill the phi as it is not used */
277                                         spill_phi(env, best_starter);
278                                 }
279                                 best_starter = node;
280                                 best_time = next_use.time;
281                                 outer_loop_allowed = 0;
282                         }
283                 } else {
284                         if (outer_loop_allowed && next_use.time < best_time) {
285                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
286                                      next_use.outermost_loop));
287                                 if (best_starter != NULL) {
288                                         /* spill the phi as it is not used */
289                                         spill_phi(env, best_starter);
290                                 }
291                                 best_starter = node;
292                                 best_time = next_use.time;
293                         }
294                 }
295
296                 if (best_starter != node) {
297                         /* spill the phi as it is not used */
298                         spill_phi(env, best_starter);
299                 }
300         }
301
302         /* check all Live-Ins */
303         be_lv_foreach(env->lv, block, be_lv_state_in, node) {
304                 if (!mode_is_data(get_irn_mode(node)))
305                         continue;
306
307                 if (arch_get_irn_register(node) != env->reg)
308                         continue;
309
310                 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
311                 next_use = be_get_next_use(env->uses, first, node, 0);
312
313                 if (USES_IS_INFINITE(next_use.time)) {
314                         DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
315                         continue;
316                 }
317
318                 if (next_use.outermost_loop >= get_loop_depth(loop)) {
319                         if (outer_loop_allowed || next_use.time < best_time) {
320                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
321                                      next_use.outermost_loop));
322
323                                 if (best_starter != NULL && is_Phi(best_starter)) {
324                                         /* spill the phi as it is not used */
325                                         spill_phi(env, best_starter);
326                                 }
327                                 best_starter = node;
328                                 best_time = next_use.time;
329                                 outer_loop_allowed = 0;
330                         }
331                 } else {
332                         if (outer_loop_allowed && next_use.time < best_time) {
333                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
334                                      next_use.outermost_loop));
335                                 if (best_starter != NULL && is_Phi(best_starter)) {
336                                         /* spill the phi as it is not used */
337                                         spill_phi(env, best_starter);
338                                 }
339                                 best_starter = node;
340                                 best_time = next_use.time;
341                         }
342                 }
343         }
344
345         block_info->start_state = best_starter;
346
347         return block_info;
348 }
349
350 /**
351  * For the given block @p block, decide for each values
352  * whether it is used from a register or is reloaded
353  * before the use.
354  */
355 static void belady(minibelady_env_t *env, ir_node *block)
356 {
357         ir_node *current_state;
358         block_info_t *block_info;
359
360         /* Don't do a block twice */
361         if (irn_visited(block))
362                 return;
363
364         /* compute value to start with */
365         block_info = compute_block_start_state(env, block);
366
367         /* get the starting workset for this block */
368         DBG((dbg, LEVEL_3, "\n"));
369         DBG((dbg, LEVEL_3, "Decide for %+F\n", block));
370
371         current_state = block_info->start_state;
372         DBG((dbg, LEVEL_3, "Start value: %+F\n", current_state));
373
374         /* process the block from start to end */
375         DBG((dbg, LEVEL_3, "Processing...\n"));
376
377         sched_foreach(block, node) {
378                 int i, arity;
379                 ir_node *need_val = NULL;
380
381                 /* projs are handled with the tuple value.
382                  * Phis are no real instr (see insert_starters()) */
383                 if (is_Proj(node) || is_Phi(node)) {
384                         continue;
385                 }
386
387                 /* check which state is desired for the node */
388                 arity = get_irn_arity(node);
389                 for (i = 0; i < arity; ++i) {
390                         const arch_register_t *reg;
391                         ir_node *in = get_irn_n(node, i);
392
393                         if (!mode_is_data(get_irn_mode(in)))
394                                 continue;
395
396                         reg = arch_get_irn_register(in);
397                         if (reg == env->reg) {
398                                 assert(need_val == NULL);
399                                 need_val = in;
400                                 DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val));
401                         }
402                 }
403                 /* create a reload to match state if necessary */
404                 if (need_val != NULL && need_val != current_state) {
405                         ir_node *before = node;
406                         DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
407                         create_reload(env, need_val, before, current_state);
408                         current_state = need_val;
409                 }
410
411                 DBG((dbg, LEVEL_3, "  ...%+F\n", node));
412
413                 /* record state changes by the node */
414                 if (get_irn_mode(node) == mode_T) {
415                         foreach_out_edge(node, edge) {
416                                 const arch_register_t *reg;
417                                 ir_node *proj = get_edge_src_irn(edge);
418
419                                 if (!mode_is_data(get_irn_mode(proj)))
420                                         continue;
421
422                                 reg = arch_get_irn_register(proj);
423                                 if (reg == env->reg) {
424                                         current_state = proj;
425                                         DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
426                                 }
427                         }
428                 } else {
429                         if (mode_is_data(get_irn_mode(node))) {
430                                 const arch_register_t *reg = arch_get_irn_register(node);
431                                 if (reg == env->reg) {
432                                         current_state = node;
433                                         DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
434                                 }
435                         }
436                 }
437         }
438
439         /* Remember end-workset for this block */
440         block_info->end_state = current_state;
441         DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state));
442 }
443
444 static void belady_walker(ir_node *block, void *data)
445 {
446         belady((minibelady_env_t*) data, block);
447 }
448
449 static ir_node *get_end_of_block_insertion_point(ir_node *block)
450 {
451         ir_node *last = sched_last(block);
452
453         /* skip Projs and Keep-alikes behind the jump... */
454         while (is_Proj(last) || be_is_Keep(last)) {
455                 last = sched_prev(last);
456         }
457
458         if (!is_cfop(last)) {
459                 last = sched_next(last);
460                 /* last node must be a cfop, only exception is the start block */
461                 assert(last == get_irg_start_block(get_irn_irg(block)));
462         }
463
464         return last;
465 }
466
467 /**
468  * We must adapt the live-outs to the live-ins at each block-border.
469  */
470 static void fix_block_borders(ir_node *block, void *data)
471 {
472         minibelady_env_t *env = (minibelady_env_t*)data;
473         ir_graph *irg = get_irn_irg(block);
474         ir_node *startblock = get_irg_start_block(irg);
475         int i;
476         int arity;
477         block_info_t *block_info;
478
479         if (block == startblock)
480                 return;
481
482         DBG((dbg, LEVEL_3, "\n"));
483
484         block_info = get_block_info(block);
485
486         DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
487              block_info->start_state));
488
489         /* process all pred blocks */
490         arity = get_irn_arity(block);
491         for (i = 0; i < arity; ++i) {
492                 ir_node      *pred       = get_Block_cfgpred_block(block, i);
493                 block_info_t *pred_info  = get_block_info(pred);
494                 ir_node      *need_state = block_info->start_state;
495
496                 if (need_state == NULL)
497                         continue;
498
499                 if (is_Phi(need_state) && get_nodes_block(need_state) == block) {
500                         need_state = get_irn_n(need_state, i);
501                 }
502
503                 DBG((dbg, LEVEL_3, "  Pred %+F (ends in %+F, we need %+F)\n", pred,
504                      pred_info->end_state, need_state));
505
506                 if (pred_info->end_state != need_state) {
507                         ir_node *insert_point = get_end_of_block_insertion_point(pred);
508
509
510                         DBG((dbg, LEVEL_3, "  Creating reload for %+F\n", need_state));
511                         create_reload(env, need_state, insert_point, pred_info->end_state);
512                 }
513         }
514 }
515
516 void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env,
517                      create_spill_func create_spill,
518                      create_reload_func create_reload)
519 {
520         minibelady_env_t env;
521         spill_info_t *info;
522         be_lv_t *lv = be_get_irg_liveness(irg);
523
524         be_assure_live_sets(irg);
525         assure_loopinfo(irg);
526
527         obstack_init(&env.obst);
528         env.reg           = reg;
529         env.func_env      = func_env;
530         env.create_spill  = create_spill;
531         env.create_reload = create_reload;
532         env.lv            = be_get_irg_liveness(irg);
533         env.uses          = be_begin_uses(irg, env.lv);
534         env.spills        = NULL;
535         ir_nodehashmap_init(&env.spill_infos);
536
537         assure_doms(irg);
538         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
539         inc_irg_visited(irg);
540
541         /* process blocks */
542         irg_block_walk_graph(irg, NULL, belady_walker, &env);
543
544         /* fix block end_states that don't match the next blocks start_state */
545         irg_block_walk_graph(irg, fix_block_borders, NULL, &env);
546
547         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
548
549         /* reconstruct ssa-form */
550         info = env.spills;
551         while (info != NULL) {
552                 be_ssa_construction_env_t senv;
553                 size_t i, len;
554                 ir_node **phis;
555
556                 be_ssa_construction_init(&senv, irg);
557                 if (sched_is_scheduled(info->value))
558                         be_ssa_construction_add_copy(&senv, info->value);
559                 be_ssa_construction_add_copies(&senv,
560                                                info->reloads, ARR_LEN(info->reloads));
561                 be_ssa_construction_fix_users(&senv, info->value);
562
563                 if (lv != NULL) {
564                         be_ssa_construction_update_liveness_phis(&senv, lv);
565
566                         be_liveness_update(lv, info->value);
567                         len = ARR_LEN(info->reloads);
568                         for (i = 0; i < len; ++i) {
569                                 ir_node *reload = info->reloads[i];
570                                 be_liveness_update(lv, reload);
571                         }
572                 }
573
574                 phis = be_ssa_construction_get_new_phis(&senv);
575
576                 /* set register requirements for phis */
577                 len = ARR_LEN(phis);
578                 for (i = 0; i < len; ++i) {
579                         ir_node *phi = phis[i];
580                         arch_set_irn_register(phi, env.reg);
581                 }
582                 be_ssa_construction_destroy(&senv);
583
584                 info = info->next;
585         }
586
587         /* some nodes might be dead now. */
588         be_remove_dead_nodes_from_schedule(irg);
589
590         ir_nodehashmap_destroy(&env.spill_infos);
591         be_end_uses(env.uses);
592         obstack_free(&env.obst, NULL);
593 }
594
595 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state)
596 void be_init_state(void)
597 {
598         FIRM_DBG_REGISTER(dbg, "firm.be.state");
599 }