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