1ed67c08382fc5463d89ac894cf1b51a4fc948ad
[libfirm] / ir / be / bespillmorgan.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  * Author:      Matthias Braun
22  * Date:                05.05.2006
23  * Copyright:   (c) Universitaet Karlsruhe
24  * License:     This file is protected by GPL -  GNU GENERAL PUBLIC LICENSE.
25  *
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "bespillmorgan.h"
32
33 #include "bechordal_t.h"
34 #include "bespill.h"
35 #include "belive_t.h"
36 #include "beabi.h"
37 #include "irgwalk.h"
38 #include "besched.h"
39 #include "beutil.h"
40 #include "irloop_t.h"
41 #include "irgraph_t.h"
42 #include "irprintf.h"
43 #include "obst.h"
44
45 #include "bespillbelady.h"
46 #include "beverify.h"
47 #include "benodesets.h"
48 #include "bespilloptions.h"
49
50 #define DBG_LIVE                1
51 #define DBG_LOOPANA             2
52 #define DBG_PRESSURE    4
53 #define DBG_SPILLS      8
54 #define DBG_CHOOSE              16
55 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
56
57 typedef struct morgan_env {
58         const arch_env_t *arch;
59         const arch_register_class_t *cls;
60         ir_graph *irg;
61         const be_lv_t *lv;
62         struct obstack obst;
63         /** maximum safe register pressure */
64         int registers_available;
65
66         spill_env_t *senv;
67
68         set *loop_attr_set;
69         set *block_attr_set;
70 } morgan_env_t;
71
72 typedef struct loop_edge {
73         ir_node *block;
74         int pos;
75 } loop_edge_t;
76
77 typedef struct loop_attr {
78         const ir_loop *loop;
79         set *out_edges;
80         set *in_edges;
81         /** The set of all values that are live in the loop but not used in the loop */
82         bitset_t *livethrough_unused;
83 } loop_attr_t;
84
85 typedef struct morgan_block_attr {
86         const ir_node *block;
87         /** set of all values that are live in the block but not used in the block */
88         bitset_t *livethrough_unused;
89 } block_attr_t;
90
91 //---------------------------------------------------------------------------
92
93 static int loop_edge_cmp(const void* p1, const void* p2, size_t s) {
94         loop_edge_t *e1 = (loop_edge_t*) p1;
95         loop_edge_t *e2 = (loop_edge_t*) p2;
96
97         return e1->block != e2->block || e1->pos != e2->pos;
98 }
99
100 static int loop_attr_cmp(const void *e1, const void *e2, size_t s) {
101         loop_attr_t *la1 = (loop_attr_t*) e1;
102         loop_attr_t *la2 = (loop_attr_t*) e2;
103
104         return la1->loop != la2->loop;
105 }
106
107 static int block_attr_cmp(const void *e1, const void *e2, size_t s) {
108         block_attr_t *b1 = (block_attr_t*) e1;
109         block_attr_t *b2 = (block_attr_t*) e2;
110
111         return b1->block != b2->block;
112 }
113
114 static INLINE int loop_attr_hash(const loop_attr_t *a) {
115 #ifdef DEBUG_libfirm
116         return a->loop->loop_nr;
117 #else
118         return HASH_PTR(a->loop);
119 #endif
120 }
121
122 static INLINE int block_attr_hash(const block_attr_t *b) {
123         return nodeset_hash(b->block);
124 }
125
126 static INLINE int loop_edge_hash(const loop_edge_t *e) {
127         return nodeset_hash(e->block) ^ (e->pos * 31);
128 }
129
130 static INLINE loop_attr_t *get_loop_attr(morgan_env_t *env, const ir_loop *loop) {
131         loop_attr_t l_attr, *res;
132         int hash;
133         l_attr.loop = loop;
134
135         hash = loop_attr_hash(&l_attr);
136         res = set_find(env->loop_attr_set, &l_attr, sizeof(l_attr), hash);
137
138         // create new loop_attr if none exists yet
139         if (res == NULL) {
140                 l_attr.out_edges = new_set(loop_edge_cmp, 1);
141                 l_attr.in_edges = new_set(loop_edge_cmp, 1);
142                 l_attr.livethrough_unused = bitset_obstack_alloc(&env->obst, get_irg_last_idx(env->irg));
143                 res = set_insert(env->loop_attr_set, &l_attr, sizeof(l_attr), hash);
144         }
145
146         return res;
147 }
148
149 static INLINE block_attr_t *get_block_attr(morgan_env_t *env, const ir_node *block) {
150         block_attr_t b_attr, *res;
151         int hash;
152         b_attr.block = block;
153
154         hash = block_attr_hash(&b_attr);
155         res = set_find(env->block_attr_set, &b_attr, sizeof(b_attr), hash);
156
157         if(res == NULL) {
158                 b_attr.livethrough_unused = NULL;
159                 res = set_insert(env->block_attr_set, &b_attr, sizeof(b_attr), hash);
160         }
161
162         return res;
163 }
164
165 //---------------------------------------------------------------------------
166
167 static INLINE int consider_for_spilling(const arch_env_t *env, const arch_register_class_t *cls, const ir_node *node) {
168         if(!arch_irn_has_reg_class(env, node, -1, cls))
169                 return 0;
170
171         return !(arch_irn_get_flags(env, node) & (arch_irn_flags_ignore | arch_irn_flags_dont_spill));
172 }
173
174 /**
175  * Determine edges going out of a loop (= edges that go to a block that is not
176  * inside the loop or one of its subloops)
177  */
178 static INLINE void construct_loop_edges(ir_node *block, void *data) {
179         morgan_env_t *env = data;
180         int n_cfgpreds = get_Block_n_cfgpreds(block);
181         int i;
182         ir_loop* loop = get_irn_loop(block);
183         DBG((dbg, DBG_LOOPANA, "Loop for %+F: %d (depth %d)\n", block, loop->loop_nr, loop->depth));
184
185         for(i = 0; i < n_cfgpreds; ++i) {
186                 loop_edge_t edge;
187                 int hash;
188                 ir_node* cfgpred = get_Block_cfgpred(block, i);
189                 ir_node* cfgpred_block = get_nodes_block(cfgpred);
190                 ir_loop* cfgpred_loop = get_irn_loop(cfgpred_block);
191
192                 if(cfgpred_loop == loop)
193                         continue;
194
195                 assert(get_loop_depth(cfgpred_loop) != get_loop_depth(loop));
196
197                 edge.block = block;
198                 edge.pos = i;
199                 hash = loop_edge_hash(&edge);
200
201                 // edge out of a loop?
202                 if(get_loop_depth(cfgpred_loop) > get_loop_depth(loop)) {
203                         ir_loop *l;
204
205                         DBG((dbg, DBG_LOOPANA, "Loop out edge from %+F (loop %d) to %+F (loop %d)\n", block, get_loop_loop_nr(loop),
206                              cfgpred_block, get_loop_loop_nr(cfgpred_loop)));
207
208                         /* this might be a jump out of multiple loops, so add this to all
209                      * needed outedge sets */
210                         l = cfgpred_loop;
211                         do {
212                                 loop_attr_t *l_attr = get_loop_attr(env, l);
213                                 set_insert(l_attr->out_edges, &edge, sizeof(edge), hash);
214
215                                 l = get_loop_outer_loop(l);
216                                 assert(l != NULL);
217                         } while(l != loop);
218                 } else {
219                         ir_loop *l;
220
221                         // edge into a loop
222                         DBG((dbg, DBG_LOOPANA, "Loop in edge from %+F (loop %d) to %+F (loop %d)\n", block, get_loop_loop_nr(loop),
223                              cfgpred_block, get_loop_loop_nr(cfgpred_loop)));
224
225                         l = loop;
226                         do {
227                                 loop_attr_t *l_attr = get_loop_attr(env, l);
228                                 set_insert(l_attr->in_edges, &edge, sizeof(edge), hash);
229
230                                 l = get_loop_outer_loop(l);
231                         } while(l != cfgpred_loop);
232                 }
233         }
234 }
235
236 static void free_loop_edges(morgan_env_t *env) {
237         loop_attr_t *l_attr;
238
239         for(l_attr = set_first(env->loop_attr_set); l_attr != NULL; l_attr = set_next(env->loop_attr_set)) {
240                 del_set(l_attr->out_edges);
241                 del_set(l_attr->in_edges);
242         }
243 }
244
245 #if 0
246 /**
247  * Debugging help, shows all nodes in a (node-)bitset
248  */
249 static void show_nodebitset(ir_graph* irg, const bitset_t* bitset) {
250         int i;
251
252         bitset_foreach(bitset, i) {
253                 ir_node* node = get_idx_irn(irg, i);
254                 ir_fprintf(stderr, " %+F", node);
255         }
256         fprintf(stderr, "\n");
257 }
258 #endif
259
260 static INLINE void init_livethrough_unuseds(block_attr_t *attr, morgan_env_t *env) {
261         const ir_node *block;
262         int i;
263         const be_lv_t *lv = env->lv;
264
265         if(attr->livethrough_unused != NULL)
266                 return;
267
268         block = attr->block;
269
270         attr->livethrough_unused = bitset_obstack_alloc(&env->obst, get_irg_last_idx(env->irg));
271
272         // copy all live-outs into the livethrough_unused set
273         be_lv_foreach(lv, block, be_lv_state_in | be_lv_state_out, i) {
274                 ir_node *irn = be_lv_get_irn(lv, block, i);
275                 int node_idx;
276
277                 if(!consider_for_spilling(env->arch, env->cls, irn))
278                         continue;
279
280                 node_idx = get_irn_idx(irn);
281                 bitset_set(attr->livethrough_unused, node_idx);
282         }
283 }
284
285 /**
286  * Construct the livethrough unused set for a block
287  */
288 static void construct_block_livethrough_unused(ir_node *block, void *data) {
289         morgan_env_t* env = data;
290         block_attr_t *block_attr = get_block_attr(env, block);
291         ir_node *node;
292         int n_cfgpreds;
293         block_attr_t **pred_attrs = NULL;
294         int i;
295
296         init_livethrough_unuseds(block_attr, env);
297
298         DBG((dbg, DBG_LIVE, "Processing block %d\n", get_irn_node_nr(block)));
299
300         n_cfgpreds = get_Block_n_cfgpreds(block);
301         if(n_cfgpreds > 1) {
302                 pred_attrs = alloca(sizeof(pred_attrs[0]) * n_cfgpreds);
303                 for(i = 0; i < n_cfgpreds; ++i) {
304                         ir_node *pred_block = get_Block_cfgpred_block(block, i);
305                         pred_attrs[i] = get_block_attr(env, pred_block);
306                         init_livethrough_unuseds(pred_attrs[i], env);
307                 }
308         }
309
310         /*
311          * All values that are used within the block are not unused (and therefore not
312          * livethrough_unused)
313          */
314         sched_foreach(block, node) {
315                 int i, arity;
316
317                 // phis are really uses in the pred block
318                 if(is_Phi(node)) {
319                         int j;
320                         for(j = 0; j < n_cfgpreds; ++j) {
321                                 ir_node *used_value = get_Phi_pred(node, j);
322                                 int idx = get_irn_idx(used_value);
323                                 block_attr_t *pred_attr = pred_attrs[j];
324
325                                 bitset_clear(pred_attr->livethrough_unused, idx);
326                         }
327                 } else {
328                         // mark all used values as used
329                         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
330                                 int idx = get_irn_idx(get_irn_n(node, i));
331                                 bitset_clear(block_attr->livethrough_unused, idx);
332                         }
333                 }
334         }
335 }
336
337 /**
338  * Construct the livethrough unused set for a loop (and all its subloops+blocks)
339  */
340 static bitset_t *construct_loop_livethrough_unused(morgan_env_t *env, const ir_loop *loop) {
341         int i;
342         loop_attr_t* loop_attr = get_loop_attr(env, loop);
343
344         DBG((dbg, DBG_LIVE, "Processing Loop %d\n", loop->loop_nr));
345         assert(get_loop_n_elements(loop) > 0);
346         for(i = 0; i < get_loop_n_elements(loop); ++i) {
347                 loop_element elem = get_loop_element(loop, i);
348                 switch (*elem.kind) {
349                 case k_ir_node: {
350                         ir_node *block = elem.node;
351                         block_attr_t *block_attr = get_block_attr(env, block);
352                         bitset_t *livethrough_block_unused = block_attr->livethrough_unused;
353
354                         assert(is_Block(elem.node));
355                         assert(livethrough_block_unused != NULL);
356
357                         if(i == 0) {
358                                 bitset_copy(loop_attr->livethrough_unused, livethrough_block_unused);
359                         } else {
360                                 bitset_and(loop_attr->livethrough_unused, livethrough_block_unused);
361                         }
362                         break;
363                 }
364                 case k_ir_loop: {
365                         bitset_t *livethrough_son_unused;
366
367                         livethrough_son_unused = construct_loop_livethrough_unused(env, elem.son);
368                         if(i == 0) {
369                                 bitset_copy(loop_attr->livethrough_unused, livethrough_son_unused);
370                         } else {
371                                 bitset_and(loop_attr->livethrough_unused, livethrough_son_unused);
372                         }
373                         break;
374                 }
375             default:
376                         assert(0);
377                         break;
378                 }
379     }
380         DBG((dbg, DBG_LIVE, "Done with loop %d\n", loop->loop_nr));
381
382         // remove all unused livethroughs that are remembered for this loop from child loops and blocks
383         for(i = 0; i < get_loop_n_elements(loop); ++i) {
384                 const loop_element elem = get_loop_element(loop, i);
385
386                 if(*elem.kind == k_ir_loop) {
387                         loop_attr_t *son_attr = get_loop_attr(env, elem.son);
388                         bitset_andnot(son_attr->livethrough_unused, loop_attr->livethrough_unused);
389
390                         DBG((dbg, DBG_LIVE, "Livethroughs for loop %d:\n", loop->loop_nr));
391                 } else if(*elem.kind == k_ir_node) {
392                         block_attr_t *block_attr = get_block_attr(env, elem.node);
393                         bitset_andnot(block_attr->livethrough_unused, loop_attr->livethrough_unused);
394
395                         DBG((dbg, DBG_LIVE, "Livethroughs for block %+F\n", elem.node));
396                 } else {
397                         assert(0);
398                 }
399         }
400
401         return loop_attr->livethrough_unused;
402 }
403
404 /*---------------------------------------------------------------------------*/
405
406 typedef struct _spillcandidate_t {
407         ir_node *node;
408         int cost;
409 } spillcandidate_t;
410
411 static int compare_spillcandidates(const void *d1, const void *d2) {
412         const spillcandidate_t *cand1 = d1;
413         const spillcandidate_t *cand2 = d2;
414
415         return cand1->cost - cand2->cost;
416 }
417
418 static void spill_values(morgan_env_t *env, const loop_attr_t *loop_attr, int spills) {
419         const bitset_t *cand_bitset = loop_attr->livethrough_unused;
420         int candidatecount = bitset_popcnt(cand_bitset);
421         spillcandidate_t *candidates;
422         int i, c;
423         loop_edge_t *edge;
424
425         assert(spills <= candidatecount);
426
427         candidates = alloca(sizeof(candidates[0]) * candidatecount);
428
429         DBG((dbg, DBG_CHOOSE, "Candidates for loop %d\n", get_loop_loop_nr(loop_attr->loop)));
430         // build candidiatelist
431         c = 0;
432         bitset_foreach(cand_bitset, i) {
433                 ir_node *node = get_idx_irn(env->irg, i);
434                 candidates[c].node = node;
435                 candidates[c].cost = 0;
436
437                 for(edge = set_first(loop_attr->out_edges); edge != NULL; edge = set_next(loop_attr->out_edges)) {
438                         candidates[c].cost += be_get_reload_costs_on_edge(env->senv, node, edge->block, edge->pos);
439                 }
440                 DBG((dbg, DBG_CHOOSE, "%+F has costs %d\n", node, candidates[c].cost));
441
442                 c++;
443         }
444         assert(c == candidatecount);
445
446         // sort list
447         qsort(candidates, candidatecount, sizeof(candidates[0]), compare_spillcandidates);
448
449         // spill values
450         for(i = 0; i < spills; ++i) {
451                 ir_node *to_spill = candidates[i].node;
452                 DBG((dbg, DBG_CHOOSE, "Spilling %+F ", to_spill));
453
454                 for(edge = set_first(loop_attr->out_edges); edge != NULL; edge = set_next(loop_attr->out_edges)) {
455                         be_add_reload_on_edge(env->senv, to_spill, edge->block, edge->pos, env->cls, 1);
456                 }
457         }
458 }
459
460 static int reduce_register_pressure_in_block(morgan_env_t *env, const ir_node* block, int loop_unused_spills_possible) {
461         ir_node *node;
462         int max_pressure;
463         int loop_unused_spills_needed;
464         pset *live_nodes = pset_new_ptr_default();
465         const be_lv_t *lv = env->lv;
466
467         be_liveness_end_of_block(lv, env->arch, env->cls, block, live_nodes);
468         max_pressure = pset_count(live_nodes);
469
470         DBG((dbg, DBG_LIVE, "Reduce pressure to %d In Block %+F:\n", env->registers_available, block));
471
472         /**
473          * Determine register pressure in block
474          */
475         sched_foreach_reverse(block, node) {
476                 int pressure;
477
478                 if(is_Phi(node))
479                         break;
480
481                 be_liveness_transfer(env->arch, env->cls, node, live_nodes);
482                 pressure = pset_count(live_nodes);
483                 if(pressure > max_pressure)
484                         max_pressure = pressure;
485         }
486         del_pset(live_nodes);
487
488         loop_unused_spills_needed = max_pressure - env->registers_available;
489
490         if(loop_unused_spills_needed < 0) {
491                 loop_unused_spills_needed = 0;
492         } else if(loop_unused_spills_needed > loop_unused_spills_possible) {
493                 loop_unused_spills_needed = loop_unused_spills_possible;
494         }
495
496         DBG((dbg, DBG_PRESSURE, "Block %+F: max-pressure %d spills possible: %d spills used: %d\n",
497                  block, max_pressure, loop_unused_spills_possible, loop_unused_spills_needed));
498         return loop_unused_spills_needed;
499 }
500
501 /**
502  * Reduce register pressure in a loop
503  *
504  * @param unused_spills_possible        Number of spills from livethrough_unused variables possible in outer loops
505  * @return                                                      Number of spills of livethrough_unused variables needed in outer loops
506  */
507 static int reduce_register_pressure_in_loop(morgan_env_t *env, const ir_loop *loop, int outer_spills_possible) {
508         int i;
509         loop_attr_t* loop_attr = get_loop_attr(env, loop);
510         int spills_needed = 0;
511         int spills_possible = outer_spills_possible + bitset_popcnt(loop_attr->livethrough_unused);
512         int outer_spills_needed;
513
514         DBG((dbg, DBG_PRESSURE, "Reducing Pressure in loop %d\n", loop->loop_nr));
515         for(i = 0; i < get_loop_n_elements(loop); ++i) {
516                 loop_element elem = get_loop_element(loop, i);
517                 switch (*elem.kind) {
518                 case k_ir_node: {
519                         int needed;
520                         assert(is_Block(elem.node));
521                         needed = reduce_register_pressure_in_block(env, elem.node, spills_possible);
522                         assert(needed >= 0);
523                         assert(needed <= spills_possible);
524                         if(needed > spills_needed)
525                                 spills_needed = needed;
526                         break;
527                 }
528                 case k_ir_loop: {
529                         int needed = reduce_register_pressure_in_loop(env, elem.son, spills_possible);
530                         assert(needed >= 0);
531                         assert(needed <= spills_possible);
532                         if(needed > spills_needed)
533                                 spills_needed = needed;
534                         break;
535                 }
536             default:
537                         assert(0);
538                         break;
539                 }
540     }
541
542         /* calculate number of spills needed in outer loop and spill
543          * unused livethrough nodes around this loop */
544         if(spills_needed > outer_spills_possible) {
545                 int spills_to_place;
546                 outer_spills_needed = outer_spills_possible;
547                 spills_needed -= outer_spills_possible;
548
549                 spills_to_place = spills_needed;
550
551                 DBG((dbg, DBG_SPILLS, "%d values unused in loop %d, spilling %d\n",
552                  spills_possible - outer_spills_possible, loop->loop_nr, spills_to_place));
553
554                 spill_values(env, loop_attr, spills_to_place);
555         } else {
556                 outer_spills_needed = spills_needed;
557         }
558
559         return outer_spills_needed;
560 }
561
562 void be_spill_morgan(be_irg_t *birg, const arch_register_class_t *cls) {
563         ir_graph *irg = be_get_birg_irg(birg);
564         morgan_env_t env;
565
566         be_assure_liveness(birg);
567
568         env.arch = birg->main_env->arch_env;
569         env.irg = irg;
570         env.cls = cls;
571         env.lv = be_get_birg_liveness(birg);
572         env.senv = be_new_spill_env(birg);
573         DEBUG_ONLY(be_set_spill_env_dbg_module(env.senv, dbg);)
574
575         obstack_init(&env.obst);
576
577         env.registers_available = env.cls->n_regs - be_put_ignore_regs(birg, env.cls, NULL);
578
579         env.loop_attr_set = new_set(loop_attr_cmp, 5);
580         env.block_attr_set = new_set(block_attr_cmp, 20);
581
582         /*-- Part1: Analysis --*/
583
584         /* construct control flow loop tree */
585         if(! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
586                 construct_cf_backedges(irg);
587         }
588
589         /* construct loop out edges and livethrough_unused sets for loops and blocks */
590         irg_block_walk_graph(irg, construct_block_livethrough_unused, construct_loop_edges, &env);
591         construct_loop_livethrough_unused(&env, get_irg_loop(irg));
592
593         /*-- Part2: Transformation --*/
594
595         /* spill unused livethrough values around loops and blocks where
596          * the pressure is too high
597          */
598         reduce_register_pressure_in_loop(&env, get_irg_loop(irg), 0);
599
600         /* Insert real spill/reload nodes and fix usages */
601         be_insert_spills_reloads(env.senv);
602
603         /* Verify the result */
604         if(birg->main_env->options->vrfy_option == BE_VRFY_WARN) {
605                 be_verify_schedule(birg);
606         } else if (birg->main_env->options->vrfy_option == BE_VRFY_ASSERT) {
607                 assert(be_verify_schedule(birg));
608         }
609
610         /* cleanup */
611         free_loop_edges(&env);
612         del_set(env.loop_attr_set);
613         del_set(env.block_attr_set);
614
615         /* fix the remaining places with too high register pressure with beladies algorithm */
616         be_spill_belady_spill_env(birg, cls, env.senv);
617
618         be_delete_spill_env(env.senv);
619         obstack_free(&env.obst, NULL);
620 }
621
622 void be_init_spillmorgan(void)
623 {
624         static be_spiller_t morgan_spiller = {
625                 be_spill_morgan
626         };
627
628         be_register_spiller("morgan", &morgan_spiller);
629         FIRM_DBG_REGISTER(dbg, "ir.be.spillmorgan");
630 }
631
632 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillmorgan);