not needed
[libfirm] / ir / be / bestate.c
1 /*
2  * Copyright (C) 1995-2007 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  * @version     $Id$
27  * Copyright:   (c) Universitaet Karlsruhe
28  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
29  */
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "bestate.h"
35
36 #include "obst.h"
37 #include "irgraph_t.h"
38 #include "irnode_t.h"
39 #include "irgwalk.h"
40 #include "irloop.h"
41 #include "iredges_t.h"
42 #include "ircons_t.h"
43 #include "irgmod.h"
44
45 #include "bearch_t.h"
46 #include "beuses_t.h"
47 #include "besched_t.h"
48 #include "belive_t.h"
49 #include "bemodule.h"
50 #include "benode_t.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_env_t      *arch_env;
65         const arch_register_t *reg;
66         const be_lv_t         *lv;
67         void                  *func_env;
68         create_reload_func     create_reload;
69         create_spill_func      create_spill;
70         spill_info_t          *spills;
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
81 block_info_t *new_block_info(struct obstack *obst, ir_node *block)
82 {
83         block_info_t *res = obstack_alloc(obst, sizeof(*res));
84         memset(res, 0, sizeof(res[0]));
85
86         set_irn_link(block, res);
87         mark_irn_visited(block);
88
89         return res;
90 }
91
92 static INLINE
93 block_info_t *get_block_info(ir_node *block)
94 {
95         assert(irn_visited(block));
96         return (block_info_t*) get_irn_link(block);
97 }
98
99 static INLINE
100 spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
101 {
102         spill_info_t *spill_info = obstack_alloc(&env->obst, sizeof(spill_info[0]));
103         memset(spill_info, 0, sizeof(spill_info[0]));
104         spill_info->value = state;
105         spill_info->reloads = NEW_ARR_F(ir_node*, 0);
106
107         set_irn_link(state, spill_info);
108         mark_irn_visited(state);
109
110         spill_info->next = env->spills;
111         env->spills = spill_info;
112
113         return spill_info;
114 }
115
116 static
117 spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
118 {
119         spill_info_t *spill_info;
120         ir_node *next;
121         ir_node *after;
122
123         if(irn_visited(state)) {
124                 spill_info = (spill_info_t*) get_irn_link(state);
125                 if(spill_info->spill != NULL || !force)
126                         return spill_info;
127         } else {
128                 spill_info = create_spill_info(env, state);
129         }
130
131         if(sched_is_scheduled(state)) {
132                 next = state;
133                 do {
134                         after = next;
135                         next = sched_next(after);
136                 } while(is_Proj(next) || is_Phi(next) || be_is_Keep(next));
137         } else {
138                 after = state;
139         }
140         spill_info->spill = env->create_spill(env->func_env, state, force, after);
141
142         return spill_info;
143 }
144
145 static
146 void create_reload(minibelady_env_t *env, ir_node *state, ir_node *before,
147                    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
159 void spill_phi(minibelady_env_t *env, ir_node *phi)
160 {
161         ir_graph *irg = get_irn_irg(phi);
162         ir_node *block = get_nodes_block(phi);
163         int i, arity = get_irn_arity(phi);
164         ir_node **in = alloca(arity * sizeof(in[0]));
165         ir_node *spill_to_kill = NULL;
166         spill_info_t *spill_info;
167
168         /* does a spill exist for the phis value? */
169         if(irn_visited(phi)) {
170                 spill_info = (spill_info_t*) get_irn_link(phi);
171                 spill_to_kill = spill_info->spill;
172         } else {
173                 spill_info = create_spill_info(env, phi);
174         }
175
176         /* create a new phi-M with bad preds */
177         for(i = 0; i < arity; ++i) {
178                 in[i] = new_r_Bad(irg);
179         }
180
181         DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
182
183         /* create a Phi-M */
184         spill_info->spill = new_r_Phi(irg, block, arity, in, mode_M);
185
186         if(spill_to_kill != NULL) {
187                 exchange(spill_to_kill, spill_info->spill);
188                 sched_remove(spill_to_kill);
189         }
190
191         /* create spills for the phi values */
192         for(i = 0; i < arity; ++i) {
193                 ir_node *in = get_irn_n(phi, i);
194                 spill_info_t *pred_spill = create_spill(env, in, 1);
195                 set_irn_n(spill_info->spill, i, pred_spill->spill);
196         }
197 }
198
199 static
200 void belady(minibelady_env_t *env, ir_node *block);
201
202 /**
203  * Collects all values live-in at block @p block and all phi results in this
204  * block.
205  * Then it adds the best values (at most n_regs) to the blocks start_workset.
206  * The phis among the remaining values get spilled: Introduce psudo-copies of
207  * their args to break interference and make it possible to spill them to the
208  * same spill slot.
209  */
210 static
211 block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block)
212 {
213         block_info_t  *block_info;
214         be_next_use_t  next_use;
215         ir_loop       *loop;
216         ir_node       *best_starter, *first;
217         ir_node       *node;
218         int            n_cfgpreds;
219         unsigned       best_time;
220         int            outer_loop_allowed;
221         int            i;
222
223         /* Create the block info for this block. */
224         block_info = new_block_info(&env->obst, block);
225         n_cfgpreds = get_Block_n_cfgpreds(block);
226
227         /* no cfgpred -> no value active */
228         if(n_cfgpreds == 0) {
229                 block_info->start_state = NULL;
230                 return block_info;
231         }
232
233         /* for 1 pred only: simply take the the end-state of the pred */
234         if(n_cfgpreds == 1) {
235                 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
236                 block_info_t *pred_info;
237
238                 /* process pred block */
239                 belady(env, pred_block);
240
241                 pred_info = get_block_info(pred_block);
242
243                 DBG((dbg, LEVEL_2, "Taking end state from %+F: %+F\n", pred_block, pred_info->end_state));
244                 block_info->start_state = pred_info->end_state;
245                 return block_info;
246         }
247
248         /* Collect all values living at start of block */
249         DBG((dbg, LEVEL_2, "Living at start of %+F:\n", block));
250         first = sched_first(block);
251         loop = get_irn_loop(block);
252         best_starter = NULL;
253         best_time = USES_INFINITY;
254         outer_loop_allowed = 1;
255
256         /* check all Phis first */
257         sched_foreach(block, node) {
258                 if (!is_Phi(node))
259                         break;
260                 if (arch_get_irn_register(env->arch_env, node) != env->reg)
261                         continue;
262
263                 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
264                 next_use = be_get_next_use(env->uses, first, 0, node, 0);
265
266                 if(USES_IS_INFINITE(next_use.time)) {
267                         DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
268                         continue;
269                 }
270
271                 if(next_use.outermost_loop >= get_loop_depth(loop)) {
272                         if(outer_loop_allowed || next_use.time < best_time) {
273                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
274                                      next_use.outermost_loop));
275
276                                 if(best_starter != NULL) {
277                                         /* spill the phi as it is not used */
278                                         spill_phi(env, best_starter);
279                                 }
280                                 best_starter = node;
281                                 best_time = next_use.time;
282                                 outer_loop_allowed = 0;
283                         }
284                 } else {
285                         if(outer_loop_allowed && next_use.time < best_time) {
286                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
287                                      next_use.outermost_loop));
288                                 if(best_starter != NULL) {
289                                         /* spill the phi as it is not used */
290                                         spill_phi(env, best_starter);
291                                 }
292                                 best_starter = node;
293                                 best_time = next_use.time;
294                         }
295                 }
296
297                 if(best_starter != node) {
298                         /* spill the phi as it is not used */
299                         spill_phi(env, best_starter);
300                 }
301         }
302
303         /* check all Live-Ins */
304         be_lv_foreach(env->lv, block, be_lv_state_in, i) {
305                 node = be_lv_get_irn(env->lv, block, i);
306
307                 if (arch_get_irn_register(env->arch_env, 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, 0, 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
356 void belady(minibelady_env_t *env, ir_node *block)
357 {
358         ir_node *current_state;
359         ir_node *node;
360         block_info_t *block_info;
361
362         /* Don't do a block twice */
363         if(irn_visited(block))
364                 return;
365
366         /* compute value to start with */
367         block_info = compute_block_start_state(env, block);
368
369         /* get the starting workset for this block */
370         DBG((dbg, LEVEL_3, "\n"));
371         DBG((dbg, LEVEL_3, "Decide for %+F\n", block));
372
373         current_state = block_info->start_state;
374         DBG((dbg, LEVEL_3, "Start value: %+F\n", current_state));
375
376         /* process the block from start to end */
377         DBG((dbg, LEVEL_3, "Processing...\n"));
378
379         sched_foreach(block, node) {
380                 int i, arity;
381                 ir_node *need_val = NULL;
382
383                 /* projs are handled with the tuple value.
384                  * Phis are no real instr (see insert_starters()) */
385                 if (is_Proj(node) || is_Phi(node)) {
386                         continue;
387                 }
388
389                 /* check which state is desired for the node */
390                 arity = get_irn_arity(node);
391                 for(i = 0; i < arity; ++i) {
392                         ir_node *in = get_irn_n(node, i);
393                         const arch_register_t *reg =
394                                 arch_get_irn_register(env->arch_env, in);
395                         if(reg == env->reg) {
396                                 assert(need_val == NULL);
397                                 need_val = in;
398                                 DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val));
399                         }
400                 }
401                 /* create a reload to match state if necessary */
402                 if(need_val != NULL && need_val != current_state) {
403                         DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
404                         create_reload(env, need_val, node, current_state);
405                         current_state = need_val;
406                 }
407
408                 DBG((dbg, LEVEL_3, "  ...%+F\n", node));
409
410                 /* record state changes by the node */
411                 if (get_irn_mode(node) == mode_T) {
412                         ir_node *proj;
413                         for(proj = sched_next(node); is_Proj(proj);
414                             proj = sched_next(proj)) {
415                                 const arch_register_t *reg =
416                                         arch_get_irn_register(env->arch_env, proj);
417                                 if(reg == env->reg) {
418                                         current_state = proj;
419                                         DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
420                                 }
421                         }
422                 } else {
423                         const arch_register_t *reg =
424                                 arch_get_irn_register(env->arch_env, node);
425                         if(reg == env->reg) {
426                                 current_state = node;
427                                 DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
428                         }
429                 }
430         }
431
432         /* Remember end-workset for this block */
433         block_info->end_state = current_state;
434         DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state));
435 }
436
437 static
438 void belady_walker(ir_node *block, void *data)
439 {
440         belady((minibelady_env_t*) data, block);
441 }
442
443 static
444 ir_node *get_end_of_block_insertion_point(ir_node *block)
445 {
446         ir_node *last = sched_last(block);
447
448         /* skip projs and keepanies behind the jump... */
449         while(is_Proj(last) || be_is_Keep(last)) {
450                 last = sched_prev(last);
451         }
452
453         if(!is_cfop(last)) {
454                 last = sched_next(last);
455                 // last node must be a cfop, only exception is the start block
456                 assert(last     == get_irg_start_block(get_irn_irg(block)));
457         }
458
459         return last;
460 }
461
462 /**
463  * We must adapt the live-outs to the live-ins at each block-border.
464  */
465 static
466 void fix_block_borders(ir_node *block, void *data) {
467         minibelady_env_t *env = data;
468         ir_graph *irg = get_irn_irg(block);
469         ir_node *startblock = get_irg_start_block(irg);
470         int i;
471         int arity;
472         block_info_t *block_info;
473
474         if(block == startblock)
475                 return;
476
477         DBG((dbg, LEVEL_3, "\n"));
478         DBG((dbg, LEVEL_3, "Fixing %+F\n", block));
479
480         block_info = get_block_info(block);
481
482         /* process all pred blocks */
483         arity = get_irn_arity(block);
484         for (i = 0; i < arity; ++i) {
485                 ir_node *pred = get_Block_cfgpred_block(block, i);
486                 block_info_t *pred_info = get_block_info(pred);
487
488                 DBG((dbg, LEVEL_3, "  Pred %+F\n", pred));
489
490                 if(pred_info->end_state != block_info->start_state &&
491                         block_info->start_state != NULL) {
492                         ir_node *need_state = block_info->start_state;
493                         ir_node *insert_point =
494                                 get_end_of_block_insertion_point(pred);
495
496                         create_reload(env, need_state, insert_point, pred_info->end_state);
497                 }
498         }
499 }
500
501 void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env,
502                      create_spill_func create_spill,
503                      create_reload_func create_reload) {
504         minibelady_env_t env;
505         ir_graph *irg = be_get_birg_irg(birg);
506         spill_info_t *info;
507         be_lv_t *lv = be_get_birg_liveness(birg);
508
509         be_assure_liveness(birg);
510         be_assure_dom_front(birg);
511         /* construct control flow loop tree */
512         if(! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
513                 construct_cf_backedges(irg);
514         }
515
516         obstack_init(&env.obst);
517         env.arch_env      = be_get_birg_arch_env(birg);
518         env.reg           = reg;
519         env.func_env      = func_env;
520         env.create_spill  = create_spill;
521         env.create_reload = create_reload;
522         env.lv            = be_get_birg_liveness(birg);
523         env.uses          = be_begin_uses(irg, env.lv);
524         env.spills        = NULL;
525
526         set_using_visited(irg);
527         set_using_irn_link(irg);
528         inc_irg_visited(irg);
529
530         /* process blocks */
531         irg_block_walk_graph(irg, NULL, belady_walker, &env);
532
533         /* fix block end_states that don't match the next blocks start_state */
534         irg_block_walk_graph(irg, fix_block_borders, NULL, &env);
535
536         clear_using_visited(irg);
537         clear_using_irn_link(irg);
538
539         /* reconstruct ssa-form */
540         info = env.spills;
541         while(info != NULL) {
542                 be_ssa_construction_env_t senv;
543                 int i, len;
544                 ir_node **phis;
545
546                 be_ssa_construction_init(&senv, birg);
547                 if(sched_is_scheduled(info->value))
548                         be_ssa_construction_add_copy(&senv, info->value);
549                 be_ssa_construction_add_copies(&senv,
550                                                info->reloads, ARR_LEN(info->reloads));
551                 be_ssa_construction_fix_users(&senv, info->value);
552
553                 if(lv != NULL) {
554                         be_ssa_construction_update_liveness_phis(&senv, lv);
555
556                         be_liveness_update(lv, info->value);
557                         len = ARR_LEN(info->reloads);
558                         for(i = 0; i < len; ++i) {
559                                 ir_node *reload = info->reloads[i];
560                                 be_liveness_update(lv, reload);
561                         }
562                 }
563
564                 phis = be_ssa_construction_get_new_phis(&senv);
565
566                 /* set register requirements for phis */
567                 len = ARR_LEN(phis);
568                 for(i = 0; i < len; ++i) {
569                         ir_node *phi = phis[i];
570                         be_set_phi_flags(env.arch_env, phi, arch_irn_flags_ignore);
571                         arch_set_irn_register(env.arch_env, phi, env.reg);
572                 }
573                 be_ssa_construction_destroy(&senv);
574
575                 info = info->next;
576         }
577
578         /* some nodes might be dead now. */
579         be_remove_dead_nodes_from_schedule(irg);
580
581         be_end_uses(env.uses);
582         obstack_free(&env.obst, NULL);
583 }
584
585 void be_init_state(void)
586 {
587         FIRM_DBG_REGISTER(dbg, "firm.be.state");
588 }
589
590 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state);