sparc: use 128bit long double even for softfloat
[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 "beuses.h"
45 #include "besched.h"
46 #include "belive_t.h"
47 #include "bemodule.h"
48 #include "benode.h"
49 #include "beirgmod.h"
50 #include "bessaconstr.h"
51
52 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
53
54 typedef struct spill_info_t {
55         struct spill_info_t *next;
56         ir_node *value;
57         ir_node *spill;
58         ir_node **reloads;
59 } spill_info_t;
60
61 typedef struct minibelady_env_t {
62         struct obstack         obst;
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         ir_nodehashmap_t       spill_infos;
70
71         be_uses_t             *uses;           /**< env for the next-use magic */
72 } minibelady_env_t;
73
74 typedef struct block_info_t {
75         ir_node *start_state;
76         ir_node *end_state;
77 } block_info_t;
78
79 static inline block_info_t *new_block_info(struct obstack *obst, ir_node *block)
80 {
81         block_info_t *res = OALLOCZ(obst, block_info_t);
82
83         assert(is_Block(block));
84         set_irn_link(block, res);
85         mark_irn_visited(block);
86
87         return res;
88 }
89
90 static inline block_info_t *get_block_info(ir_node *block)
91 {
92         assert(irn_visited(block));
93         return (block_info_t*) get_irn_link(block);
94 }
95
96 static inline spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state)
97 {
98         spill_info_t *spill_info = OALLOCZ(&env->obst, spill_info_t);
99         spill_info->value = state;
100         spill_info->reloads = NEW_ARR_F(ir_node*, 0);
101
102         ir_nodehashmap_insert(&env->spill_infos, state, spill_info);
103         //ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info);
104
105         spill_info->next = env->spills;
106         env->spills = spill_info;
107
108         return spill_info;
109 }
110
111 static inline spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node)
112 {
113         spill_info_t *spill_info = ir_nodehashmap_get(spill_info_t, &env->spill_infos, node);
114         //ir_fprintf(stderr, "Get %+F -> %p\n", node, spill_info);
115         return spill_info;
116 }
117
118 static spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force)
119 {
120         spill_info_t *spill_info;
121         ir_node *next;
122         ir_node *after;
123
124         spill_info = get_spill_info(env, state);
125         if (spill_info == NULL) {
126                 spill_info = create_spill_info(env, state);
127         } else if (spill_info->spill != NULL) {
128                 return spill_info;
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 void create_reload(minibelady_env_t *env, ir_node *state,
146                           ir_node *before, ir_node *last_state)
147 {
148         spill_info_t *spill_info = create_spill(env, state, 0);
149         ir_node *spill = spill_info->spill;
150         ir_node *reload;
151
152         reload = env->create_reload(env->func_env, state, spill, before,
153                                     last_state);
154         ARR_APP1(ir_node*, spill_info->reloads, reload);
155 }
156
157 static 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           arity         = get_irn_arity(phi);
162         ir_node     **phi_in        = ALLOCAN(ir_node*, arity);
163         ir_node      *dummy         = new_r_Dummy(irg, mode_M);
164         ir_node      *spill_to_kill = NULL;
165         spill_info_t *spill_info;
166         int           i;
167
168         /* does a spill exist for the phis value? */
169         spill_info = get_spill_info(env, phi);
170         if (spill_info != NULL) {
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                 phi_in[i] = dummy;
179         }
180
181         DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi));
182
183         /* create a Phi-M */
184         spill_info->spill = be_new_Phi(block, arity, phi_in, mode_M,
185                                        arch_no_register_req);
186         sched_add_after(block, spill_info->spill);
187
188         if (spill_to_kill != NULL) {
189                 exchange(spill_to_kill, spill_info->spill);
190                 sched_remove(spill_to_kill);
191         }
192
193         /* create spills for the phi values */
194         for (i = 0; i < arity; ++i) {
195                 ir_node *in = get_irn_n(phi, i);
196                 spill_info_t *pred_spill = create_spill(env, in, 1);
197                 set_irn_n(spill_info->spill, i, pred_spill->spill);
198         }
199 }
200
201 static void belady(minibelady_env_t *env, ir_node *block);
202
203 /**
204  * Collects all values live-in at block @p block and all phi results in this
205  * block.
206  * Then it adds the best values (at most n_regs) to the blocks start_workset.
207  * The phis among the remaining values get spilled: Introduce pseudo-copies of
208  * their args to break interference and make it possible to spill them to the
209  * same spill slot.
210  */
211 static 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         int            n_cfgpreds;
218         unsigned       best_time;
219         int            outer_loop_allowed;
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(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, 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, node) {
303                 if (!mode_is_data(get_irn_mode(node)))
304                         continue;
305
306                 if (arch_get_irn_register(node) != env->reg)
307                         continue;
308
309                 DBG((dbg, LEVEL_2, "\t...checking %+F\n", node));
310                 next_use = be_get_next_use(env->uses, first, node, 0);
311
312                 if (USES_IS_INFINITE(next_use.time)) {
313                         DBG((dbg, LEVEL_2, "\tnot taken (dead)\n"));
314                         continue;
315                 }
316
317                 if (next_use.outermost_loop >= get_loop_depth(loop)) {
318                         if (outer_loop_allowed || next_use.time < best_time) {
319                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
320                                      next_use.outermost_loop));
321
322                                 if (best_starter != NULL && is_Phi(best_starter)) {
323                                         /* spill the phi as it is not used */
324                                         spill_phi(env, best_starter);
325                                 }
326                                 best_starter = node;
327                                 best_time = next_use.time;
328                                 outer_loop_allowed = 0;
329                         }
330                 } else {
331                         if (outer_loop_allowed && next_use.time < best_time) {
332                                 DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time,
333                                      next_use.outermost_loop));
334                                 if (best_starter != NULL && is_Phi(best_starter)) {
335                                         /* spill the phi as it is not used */
336                                         spill_phi(env, best_starter);
337                                 }
338                                 best_starter = node;
339                                 best_time = next_use.time;
340                         }
341                 }
342         }
343
344         block_info->start_state = best_starter;
345
346         return block_info;
347 }
348
349 /**
350  * For the given block @p block, decide for each values
351  * whether it is used from a register or is reloaded
352  * before the use.
353  */
354 static void belady(minibelady_env_t *env, ir_node *block)
355 {
356         ir_node *current_state;
357         block_info_t *block_info;
358
359         /* Don't do a block twice */
360         if (irn_visited(block))
361                 return;
362
363         /* compute value to start with */
364         block_info = compute_block_start_state(env, block);
365
366         /* get the starting workset for this block */
367         DBG((dbg, LEVEL_3, "\n"));
368         DBG((dbg, LEVEL_3, "Decide for %+F\n", block));
369
370         current_state = block_info->start_state;
371         DBG((dbg, LEVEL_3, "Start value: %+F\n", current_state));
372
373         /* process the block from start to end */
374         DBG((dbg, LEVEL_3, "Processing...\n"));
375
376         sched_foreach(block, node) {
377                 int i, arity;
378                 ir_node *need_val = NULL;
379
380                 /* projs are handled with the tuple value.
381                  * Phis are no real instr (see insert_starters()) */
382                 if (is_Proj(node) || is_Phi(node)) {
383                         continue;
384                 }
385
386                 /* check which state is desired for the node */
387                 arity = get_irn_arity(node);
388                 for (i = 0; i < arity; ++i) {
389                         const arch_register_t *reg;
390                         ir_node *in = get_irn_n(node, i);
391
392                         if (!mode_is_data(get_irn_mode(in)))
393                                 continue;
394
395                         reg = arch_get_irn_register(in);
396                         if (reg == env->reg) {
397                                 assert(need_val == NULL);
398                                 need_val = in;
399                                 DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val));
400                         }
401                 }
402                 /* create a reload to match state if necessary */
403                 if (need_val != NULL && need_val != current_state) {
404                         ir_node *before = node;
405                         DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val));
406                         create_reload(env, need_val, before, current_state);
407                         current_state = need_val;
408                 }
409
410                 DBG((dbg, LEVEL_3, "  ...%+F\n", node));
411
412                 /* record state changes by the node */
413                 if (get_irn_mode(node) == mode_T) {
414                         foreach_out_edge(node, edge) {
415                                 const arch_register_t *reg;
416                                 ir_node *proj = get_edge_src_irn(edge);
417
418                                 if (!mode_is_data(get_irn_mode(proj)))
419                                         continue;
420
421                                 reg = arch_get_irn_register(proj);
422                                 if (reg == env->reg) {
423                                         current_state = proj;
424                                         DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
425                                 }
426                         }
427                 } else {
428                         if (mode_is_data(get_irn_mode(node))) {
429                                 const arch_register_t *reg = arch_get_irn_register(node);
430                                 if (reg == env->reg) {
431                                         current_state = node;
432                                         DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state));
433                                 }
434                         }
435                 }
436         }
437
438         /* Remember end-workset for this block */
439         block_info->end_state = current_state;
440         DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state));
441 }
442
443 static void belady_walker(ir_node *block, void *data)
444 {
445         belady((minibelady_env_t*) data, block);
446 }
447
448 static ir_node *get_end_of_block_insertion_point(ir_node *block)
449 {
450         ir_node *last = sched_last(block);
451
452         /* skip Projs and Keep-alikes behind the jump... */
453         while (is_Proj(last) || be_is_Keep(last)) {
454                 last = sched_prev(last);
455         }
456
457         if (!is_cfop(last)) {
458                 last = sched_next(last);
459                 /* last node must be a cfop, only exception is the start block */
460                 assert(last == get_irg_start_block(get_irn_irg(block)));
461         }
462
463         return last;
464 }
465
466 /**
467  * We must adapt the live-outs to the live-ins at each block-border.
468  */
469 static void fix_block_borders(ir_node *block, void *data)
470 {
471         minibelady_env_t *env = (minibelady_env_t*)data;
472         ir_graph *irg = get_irn_irg(block);
473         ir_node *startblock = get_irg_start_block(irg);
474         int i;
475         int arity;
476         block_info_t *block_info;
477
478         if (block == startblock)
479                 return;
480
481         DBG((dbg, LEVEL_3, "\n"));
482
483         block_info = get_block_info(block);
484
485         DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block,
486              block_info->start_state));
487
488         /* process all pred blocks */
489         arity = get_irn_arity(block);
490         for (i = 0; i < arity; ++i) {
491                 ir_node      *pred       = get_Block_cfgpred_block(block, i);
492                 block_info_t *pred_info  = get_block_info(pred);
493                 ir_node      *need_state = block_info->start_state;
494
495                 if (need_state == NULL)
496                         continue;
497
498                 if (is_Phi(need_state) && get_nodes_block(need_state) == block) {
499                         need_state = get_irn_n(need_state, i);
500                 }
501
502                 DBG((dbg, LEVEL_3, "  Pred %+F (ends in %+F, we need %+F)\n", pred,
503                      pred_info->end_state, need_state));
504
505                 if (pred_info->end_state != need_state) {
506                         ir_node *insert_point = get_end_of_block_insertion_point(pred);
507
508
509                         DBG((dbg, LEVEL_3, "  Creating reload for %+F\n", need_state));
510                         create_reload(env, need_state, insert_point, pred_info->end_state);
511                 }
512         }
513 }
514
515 void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env,
516                      create_spill_func create_spill,
517                      create_reload_func create_reload)
518 {
519         minibelady_env_t env;
520         spill_info_t *info;
521         be_lv_t *lv = be_get_irg_liveness(irg);
522
523         be_assure_live_sets(irg);
524         assure_loopinfo(irg);
525
526         obstack_init(&env.obst);
527         env.reg           = reg;
528         env.func_env      = func_env;
529         env.create_spill  = create_spill;
530         env.create_reload = create_reload;
531         env.lv            = be_get_irg_liveness(irg);
532         env.uses          = be_begin_uses(irg, env.lv);
533         env.spills        = NULL;
534         ir_nodehashmap_init(&env.spill_infos);
535
536         assure_doms(irg);
537         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
538         inc_irg_visited(irg);
539
540         /* process blocks */
541         irg_block_walk_graph(irg, NULL, belady_walker, &env);
542
543         /* fix block end_states that don't match the next blocks start_state */
544         irg_block_walk_graph(irg, fix_block_borders, NULL, &env);
545
546         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK);
547
548         /* reconstruct ssa-form */
549         info = env.spills;
550         while (info != NULL) {
551                 be_ssa_construction_env_t senv;
552                 size_t i, len;
553                 ir_node **phis;
554
555                 be_ssa_construction_init(&senv, irg);
556                 if (sched_is_scheduled(info->value))
557                         be_ssa_construction_add_copy(&senv, info->value);
558                 be_ssa_construction_add_copies(&senv,
559                                                info->reloads, ARR_LEN(info->reloads));
560                 be_ssa_construction_fix_users(&senv, info->value);
561
562                 if (lv != NULL) {
563                         be_ssa_construction_update_liveness_phis(&senv, lv);
564
565                         be_liveness_update(lv, info->value);
566                         len = ARR_LEN(info->reloads);
567                         for (i = 0; i < len; ++i) {
568                                 ir_node *reload = info->reloads[i];
569                                 be_liveness_update(lv, reload);
570                         }
571                 }
572
573                 phis = be_ssa_construction_get_new_phis(&senv);
574
575                 /* set register requirements for phis */
576                 len = ARR_LEN(phis);
577                 for (i = 0; i < len; ++i) {
578                         ir_node *phi = phis[i];
579                         arch_set_irn_register(phi, env.reg);
580                 }
581                 be_ssa_construction_destroy(&senv);
582
583                 info = info->next;
584         }
585
586         /* some nodes might be dead now. */
587         be_remove_dead_nodes_from_schedule(irg);
588
589         ir_nodehashmap_destroy(&env.spill_infos);
590         be_end_uses(env.uses);
591         obstack_free(&env.obst, NULL);
592 }
593
594 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state)
595 void be_init_state(void)
596 {
597         FIRM_DBG_REGISTER(dbg, "firm.be.state");
598 }