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