disable experimental code for now
[libfirm] / ir / be / bespillslots.c
1 /*
2  * Author:      Matthias Braun
3  * Date:                26.7.06
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <stdlib.h>
12
13 #include "set.h"
14
15 #include "irgwalk.h"
16 #include "ircons.h"
17 #include "irprintf.h"
18 #include "execfreq.h"
19 #include "unionfind.h"
20 #include "type.h"
21 #include "irdump_t.h"
22
23 #include "benode_t.h"
24 #include "besched.h"
25 #include "bespillslots.h"
26 #include "bechordal_t.h"
27 #include "bejavacoal.h"
28 #include "benodesets.h"
29 #include "bestatevent.h"
30 #include "bespilloptions.h"
31 #include "bemodule.h"
32
33 #define DBG_COALESCING          1
34 #define DBG_INTERFERENCES       2
35
36 DEBUG_ONLY(
37 static firm_dbg_module_t *dbg = NULL;
38 )
39
40 typedef struct _spill_t {
41         ir_node *spill;
42         /** mode of the spilled value */
43         const ir_mode *mode;
44         /** alignment for the spilled value */
45         int alignment;
46         /** index into spillslot_unionfind unionfind structure */
47         int spillslot;
48 } spill_t;
49
50 typedef struct _affinity_edge_t {
51         double affinity;
52         int slot1, slot2;
53 } affinity_edge_t;
54
55 struct _be_fec_env_t {
56         struct obstack obst;
57         const arch_env_t *arch_env;
58         be_irg_t *birg;
59         set *spills;
60         ir_node **reloads;
61         affinity_edge_t **affinity_edges;
62         set *memperms;
63 };
64
65 /** Compare 2 affinity edges (used in quicksort) */
66 static int cmp_affinity(const void *d1, const void *d2)
67 {
68         const affinity_edge_t * const *e1 = d1;
69         const affinity_edge_t * const *e2 = d2;
70
71         // sort in descending order
72         return (*e1)->affinity < (*e2)->affinity ? 1 : -1;
73 }
74
75 static int cmp_spill(const void* d1, const void* d2, size_t size)
76 {
77         const spill_t* s1 = d1;
78         const spill_t* s2 = d2;
79         return s1->spill != s2->spill;
80 }
81
82 static spill_t *get_spill(be_fec_env_t *env, ir_node *node)
83 {
84         spill_t spill, *res;
85         int hash = nodeset_hash(node);
86
87         spill.spill = node;
88         res = set_find(env->spills, &spill, sizeof(spill), hash);
89
90         return res;
91 }
92
93 /*
94  *   ____      _ _           _     ____        _ _ _
95  *  / ___|___ | | | ___  ___| |_  / ___| _ __ (_) | |___
96  * | |   / _ \| | |/ _ \/ __| __| \___ \| '_ \| | | / __|
97  * | |__| (_) | | |  __/ (__| |_   ___) | |_) | | | \__ \
98  *  \____\___/|_|_|\___|\___|\__| |____/| .__/|_|_|_|___/
99  *                                      |_|
100  */
101
102 static INLINE ir_node *get_memory_edge(const ir_node *node)
103 {
104         int i, arity;
105
106         arity = get_irn_arity(node);
107         for(i = arity - 1; i >= 0; --i) {
108                 ir_node *arg = get_irn_n(node, i);
109                 if(get_irn_mode(arg) == mode_M)
110                         return arg;
111         }
112
113         return NULL;
114 }
115
116 static spill_t *collect_spill(be_fec_env_t *env, ir_node *node,
117                                       const ir_mode *mode, int align)
118 {
119         spill_t spill, *res;
120         int hash = nodeset_hash(node);
121
122         /* insert into set of spills if not already there */
123         spill.spill = node;
124         res = set_find(env->spills, &spill, sizeof(spill), hash);
125
126         if(res == NULL) {
127                 spill.spillslot = set_count(env->spills);
128                 spill.mode = mode;
129                 spill.alignment = align;
130                 res = set_insert(env->spills, &spill, sizeof(spill), hash);
131         } else {
132                 assert(res->mode == mode);
133                 assert(res->alignment == align);
134         }
135
136         return res;
137 }
138
139 static spill_t *collect_memphi(be_fec_env_t *env, ir_node *node,
140                                const ir_mode *mode, int align)
141 {
142         int i, arity;
143         spill_t spill, *res;
144         int hash = nodeset_hash(node);
145         const ir_exec_freq *exec_freq = be_get_birg_exec_freq(env->birg);
146
147         assert(is_Phi(node));
148
149         spill.spill = node;
150         res = set_find(env->spills, &spill, sizeof(spill), hash);
151         if(res != NULL) {
152                 assert(res->mode == mode);
153                 assert(res->alignment == align);
154                 return res;
155         }
156
157         spill.spillslot = set_count(env->spills);
158         spill.mode = mode;
159         spill.alignment = align;
160         res = set_insert(env->spills, &spill, sizeof(spill), hash);
161
162         // collect attached spills and mem-phis
163         arity = get_irn_arity(node);
164         for(i = 0; i < arity; ++i) {
165                 affinity_edge_t *affinty_edge;
166                 ir_node *arg = get_irn_n(node, i);
167                 spill_t *arg_spill;
168
169                 if(is_Phi(arg)) {
170                         arg_spill = collect_memphi(env, arg, mode, align);
171                 } else {
172                         arg_spill = collect_spill(env, arg, mode, align);
173                 }
174
175                 // add an affinity edge
176                 affinty_edge = obstack_alloc(&env->obst, sizeof(affinty_edge[0]));
177                 affinty_edge->affinity = get_block_execfreq(exec_freq, get_nodes_block(arg));
178                 affinty_edge->slot1 = res->spillslot;
179                 affinty_edge->slot2 = arg_spill->spillslot;
180                 ARR_APP1(affinity_edge_t*, env->affinity_edges, affinty_edge);
181         }
182
183         return res;
184 }
185
186 void be_node_needs_frame_entity(be_fec_env_t *env, ir_node *node,
187                                 const ir_mode *mode, int align)
188 {
189         ir_node *spillnode = get_memory_edge(node);
190         spill_t *spill;
191
192         assert(spillnode != NULL);
193
194         if (is_Phi(spillnode)) {
195                 spill = collect_memphi(env, spillnode, mode, align);
196         } else {
197                 spill = collect_spill(env, spillnode, mode, align);
198         }
199
200         ARR_APP1(ir_node *, env->reloads, node);
201 }
202
203 /*
204  *   ____            _                      ____  _       _
205  *  / ___|___   __ _| | ___  ___  ___ ___  / ___|| | ___ | |_ ___
206  * | |   / _ \ / _` | |/ _ \/ __|/ __/ _ \ \___ \| |/ _ \| __/ __|
207  * | |__| (_) | (_| | |  __/\__ \ (_|  __/  ___) | | (_) | |_\__ \
208  *  \____\___/ \__,_|_|\___||___/\___\___| |____/|_|\___/ \__|___/
209  */
210
211 static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
212                                int* spillslot_unionfind, int s1, int s2)
213 {
214         int res;
215         int i;
216         int spillcount;
217
218         // merge spillslots and interferences
219         res = uf_union(spillslot_unionfind, s1, s2);
220         // we assume that we always merge s2 to s1 so swap s1, s2 if necessary
221         if(res != 0) {
222                 int t = s1;
223                 s1 = s2;
224                 s2 = t;
225         }
226
227         bitset_or(interferences[s1], interferences[s2]);
228
229         // update other interferences
230         spillcount = set_count(env->spills);
231         for(i = 0; i < spillcount; ++i) {
232                 bitset_t *intfs = interferences[i];
233                 if(bitset_is_set(intfs, s2))
234                         bitset_set(intfs, s1);
235         }
236
237         return res;
238 }
239
240 /**
241  * A greedy coalescing algorithm for spillslots:
242  *  1. Sort the list of affinity edges
243  *  2. Try to merge slots with affinity edges (most expensive slots first)
244  *  3. Try to merge everything else that is possible
245  */
246 static void do_greedy_coalescing(be_fec_env_t *env)
247 {
248         int spillcount;
249         spill_t **spilllist;
250         spill_t *spill;
251         int i, i2;
252         int affinity_edge_count;
253         bitset_t **interferences;
254         int* spillslot_unionfind;
255         const be_lv_t *lv = be_get_birg_liveness(env->birg);
256
257         spillcount = set_count(env->spills);
258         if(spillcount == 0)
259                 return;
260
261         DBG((dbg, DBG_COALESCING, "Coalescing %d spillslots\n", spillcount));
262
263         interferences = alloca(spillcount * sizeof(interferences[0]));
264         spillslot_unionfind = alloca(spillcount * sizeof(spillslot_unionfind[0]));
265         spilllist = alloca(spillcount * sizeof(spilllist[0]));
266
267         uf_init(spillslot_unionfind, 0, spillcount);
268
269         DEBUG_ONLY(
270                 memset(spilllist, 0, spillcount * sizeof(spilllist[0]));
271         );
272
273         for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
274                 assert(spill->spillslot < spillcount);
275                 spilllist[spill->spillslot] = spill;
276         }
277
278         for(i = 0; i < spillcount; ++i) {
279                 interferences[i] = bitset_alloca(spillcount);
280         }
281
282         // construct interferences
283         for(i = 0; i < spillcount; ++i) {
284                 for(i2 = i+1; i2 < spillcount; ++i2) {
285                         if(values_interfere(lv, spilllist[i]->spill, spilllist[i2]->spill)) {
286                                 DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
287                                 bitset_set(interferences[i], i2);
288                                 bitset_set(interferences[i2], i);
289                         }
290                 }
291         }
292
293         // sort affinity edges
294         affinity_edge_count = ARR_LEN(env->affinity_edges);
295         qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
296
297         //dump_interference_graph(env, interferences, "before");
298
299         // try to merge affine nodes
300         for(i = 0; i < affinity_edge_count; ++i) {
301                 const affinity_edge_t *edge = env->affinity_edges[i];
302                 int s1 = uf_find(spillslot_unionfind, edge->slot1);
303                 int s2 = uf_find(spillslot_unionfind, edge->slot2);
304
305                 /* test if values interfere */
306                 if(bitset_is_set(interferences[s1], s2)) {
307                         assert(bitset_is_set(interferences[s2], s1));
308                         continue;
309                 }
310
311                 DBG((dbg, DBG_COALESCING, "Merging %d and %d because of affinity edge\n", s1, s2));
312
313                 merge_interferences(env, interferences, spillslot_unionfind, s1, s2);
314         }
315
316         // try to merge as much remaining spillslots as possible
317         for(i = 0; i < spillcount; ++i) {
318                 int s1 = uf_find(spillslot_unionfind, i);
319                 if(s1 != i)
320                         continue;
321
322                 for(i2 = i+1; i2 < spillcount; ++i2) {
323                         int s2 = uf_find(spillslot_unionfind, i2);
324                         if(s2 != i2)
325                                 continue;
326
327                         /* test if values interfere
328                          * we have to test n1-n2 and n2-n1, because only 1 side gets updated
329                          * when node merging occurs
330                          */
331                         if(bitset_is_set(interferences[s1], s2)) {
332                                 assert(bitset_is_set(interferences[s2], s1));
333                                 continue;
334                         }
335
336                         DBG((dbg, DBG_COALESCING, "Merging %d and %d because it is possible\n", s1, s2));
337
338                         if(merge_interferences(env, interferences, spillslot_unionfind, s1, s2) != 0) {
339                                 // we can break the loop here, because s2 is the new supernode now
340                                 // and we'll test s2 again later anyway
341                                 break;
342                         }
343                 }
344         }
345
346         // assign spillslots to spills
347         for(i = 0; i < spillcount; ++i) {
348                 spill_t *spill = spilllist[i];
349
350                 spill->spillslot = uf_find(spillslot_unionfind, i);
351         }
352
353         //dump_interference_graph(env, interferences, "after");
354 }
355
356 /*
357  *     _            _               _____       _   _ _   _
358  *    / \   ___ ___(_) __ _ _ __   | ____|_ __ | |_(_) |_(_) ___  ___
359  *   / _ \ / __/ __| |/ _` | '_ \  |  _| | '_ \| __| | __| |/ _ \/ __|
360  *  / ___ \\__ \__ \ | (_| | | | | | |___| | | | |_| | |_| |  __/\__ \
361  * /_/   \_\___/___/_|\__, |_| |_| |_____|_| |_|\__|_|\__|_|\___||___/
362  *                    |___/
363  */
364
365 typedef struct _spill_slot_t {
366         int size;
367         int align;
368         ir_entity *entity;
369 } spill_slot_t;
370
371 typedef struct _memperm_entry_t {
372         ir_node* node;
373         int pos;
374         ir_entity *in;
375         ir_entity *out;
376         struct _memperm_entry_t *next;
377 } memperm_entry_t;
378
379 typedef struct _memperm_t {
380         ir_node *block;
381         int entrycount;
382         memperm_entry_t *entries;
383 } memperm_t;
384
385 static int cmp_memperm(const void* d1, const void* d2, size_t size)
386 {
387         const memperm_t* e1 = d1;
388         const memperm_t* e2 = d2;
389         return e1->block != e2->block;
390 }
391
392 static memperm_t *get_memperm(be_fec_env_t *env, ir_node *block)
393 {
394         memperm_t entry, *res;
395         int hash;
396
397         entry.block = block;
398         hash = nodeset_hash(block);
399
400         res = set_find(env->memperms, &entry, sizeof(entry), hash);
401
402         if(res == NULL) {
403                 entry.entrycount = 0;
404                 entry.entries = NULL;
405                 res = set_insert(env->memperms, &entry, sizeof(entry), hash);
406         }
407
408         return res;
409 }
410
411 static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
412 {
413         ir_graph *irg = be_get_birg_irg(env->birg);
414         ir_type *frame = get_irg_frame_type(irg);
415         ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, 0);
416
417         // adjust size of the entity type...
418         ir_type *enttype = get_entity_type(res);
419         set_type_size_bytes(enttype, slot->size);
420
421         slot->entity = res;
422
423         return res;
424 }
425
426 /**
427  * Enlarges a spillslot (if necessary) so that it can carry a value of size
428  * @p othersize and alignment @p otheralign.
429  */
430 static void enlarge_spillslot(spill_slot_t *slot, int otheralign, int othersize)
431 {
432         if(othersize > slot->size) {
433                 slot->size = othersize;
434         }
435         if(otheralign > slot->align) {
436                 if(otheralign % slot->align != 0)
437                         slot->align *= otheralign;
438                 else
439                         slot->align = otheralign;
440         } else if(slot->align % otheralign != 0) {
441                 slot->align *= otheralign;
442         }
443 }
444
445 /**
446  * Create stack entities for the spillslots and assign them to the spill and
447  * reload nodes.
448  */
449 static void assign_spillslots(be_fec_env_t *env)
450 {
451         const arch_env_t *arch_env = env->arch_env;
452         int i;
453         int spillcount;
454         spill_t *spill;
455         spill_slot_t* spillslots;
456
457         spillcount = set_count(env->spills);
458         spillslots = alloca(spillcount * sizeof(spillslots[0]));
459
460         memset(spillslots, 0, spillcount * sizeof(spillslots[0]));
461
462         // construct spillslots
463         for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
464                 int slotid = spill->spillslot;
465                 const ir_mode *mode = spill->mode;
466                 spill_slot_t *slot = & (spillslots[slotid]);
467                 int size = get_mode_size_bytes(mode);
468                 int align = spill->alignment;
469
470                 if(slot->align == 0 && slot->size == 0) {
471                         slot->align = align;
472                         slot->size = size;
473                 } else {
474                         enlarge_spillslot(slot, align, size);
475                 }
476         }
477
478         for(spill = set_first(env->spills); spill != NULL; spill = set_next(env->spills)) {
479                 spill_slot_t *slot;
480                 ir_node *node = spill->spill;
481                 int slotid = spill->spillslot;
482
483                 slot = &spillslots[slotid];
484                 if(slot->entity == NULL) {
485                         create_stack_entity(env, slot);
486                 }
487
488                 if(is_Phi(node)) {
489                         int i, arity;
490                         ir_node *block = get_nodes_block(node);
491
492                         // should be a PhiM
493                         assert(is_Phi(node));
494
495                         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
496                                 ir_node *arg = get_irn_n(node, i);
497                                 ir_node *predblock = get_Block_cfgpred_block(block, i);
498                                 spill_t *argspill;
499                                 int argslotid;
500
501                                 argspill = get_spill(env, arg);
502                                 assert(argspill != NULL);
503
504                                 argslotid = argspill->spillslot;
505                                 if(slotid != argslotid) {
506                                         memperm_t *memperm;
507                                         memperm_entry_t *entry;
508                                         spill_slot_t *argslot = &spillslots[argslotid];
509                                         if(argslot->entity == NULL) {
510                                                 create_stack_entity(env, argslot);
511                                         }
512
513                                         memperm = get_memperm(env, predblock);
514
515                                         entry = obstack_alloc(&env->obst, sizeof(entry[0]));
516                                         entry->node = node;
517                                         entry->pos = i;
518                                         entry->in = argslot->entity;
519                                         entry->out = slot->entity;
520                                         entry->next = memperm->entries;
521                                         memperm->entrycount++;
522                                         memperm->entries = entry;
523                                 }
524                         }
525                 } else {
526                         arch_set_frame_entity(arch_env, node, slot->entity);
527                 }
528         }
529
530         for(i = 0; i < ARR_LEN(env->reloads); ++i) {
531                 ir_node* reload = env->reloads[i];
532                 ir_node* spillnode = get_memory_edge(reload);
533                 spill_t *spill = get_spill(env, spillnode);
534                 const spill_slot_t *slot = & spillslots[spill->spillslot];
535
536                 assert(slot->entity != NULL);
537
538                 arch_set_frame_entity(arch_env, reload, slot->entity);
539         }
540 }
541
542 /**
543  * Returns the last node in a block which is no control flow changing node
544  */
545 static ir_node *get_end_of_block_insertion_point(ir_node* block)
546 {
547         ir_node* ins = sched_last(block);
548         while(is_Proj(ins) && get_irn_mode(ins) == mode_X) {
549                 ins = sched_prev(ins);
550                 assert(ins != NULL);
551         }
552
553         if(is_cfop(ins)) {
554                 while(1) {
555                         ir_node *prev = sched_prev(ins);
556                         if(!is_cfop(prev))
557                                 break;
558                         ins = prev;
559                 }
560         }
561
562         return ins;
563 }
564
565 static void create_memperms(be_fec_env_t *env)
566 {
567         const arch_env_t *arch_env = env->arch_env;
568         ir_graph *irg = be_get_birg_irg(env->birg);
569         memperm_t *memperm;
570
571         for(memperm = set_first(env->memperms); memperm != NULL; memperm = set_next(env->memperms)) {
572                 int i;
573                 memperm_entry_t *entry;
574                 ir_node *blockend;
575                 ir_node** nodes = alloca(memperm->entrycount * sizeof(nodes[0]));
576                 ir_node* mempermnode;
577
578                 assert(memperm->entrycount > 0);
579
580                 for(entry = memperm->entries, i = 0; entry != NULL; entry = entry->next, ++i) {
581                         ir_node* arg = get_irn_n(entry->node, entry->pos);
582                         nodes[i] = arg;
583                 }
584
585                 mempermnode = be_new_MemPerm(arch_env, irg, memperm->block,
586                                              memperm->entrycount, nodes);
587
588                 // insert node into schedule
589                 blockend = get_end_of_block_insertion_point(memperm->block);
590                 sched_add_before(blockend, mempermnode);
591                 be_stat_ev("mem_perm", memperm->entrycount);
592
593                 i = 0;
594                 for(entry = memperm->entries; entry != NULL; entry = entry->next, ++i) {
595                         ir_node *proj;
596                         ir_node* arg = get_irn_n(entry->node, entry->pos);
597
598                         be_set_MemPerm_in_entity(mempermnode, i, entry->in);
599                         be_set_MemPerm_out_entity(mempermnode, i, entry->out);
600                         set_irg_current_block(irg, memperm->block);
601                         proj = new_Proj(mempermnode, get_irn_mode(arg), i);
602                         sched_add_before(blockend, proj);
603
604                         set_irn_n(entry->node, entry->pos, proj);
605                 }
606         }
607 }
608
609 static int count_spillslots(const be_fec_env_t *env)
610 {
611         const spill_t *spill;
612         int spillcount = set_count(env->spills);
613         bitset_t *counted = bitset_alloca(spillcount);
614         int slotcount;
615
616         slotcount = 0;
617         for(spill = set_first(env->spills); spill != NULL;
618             spill = set_next(env->spills)) {
619                 int spillslot = spill->spillslot;
620                 if(!bitset_is_set(counted, spillslot)) {
621                         slotcount++;
622                         bitset_set(counted, spillslot);
623                 }
624         }
625
626         return slotcount;
627 }
628
629 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
630 {
631         const arch_env_t *arch_env = birg->main_env->arch_env;
632
633         be_fec_env_t *env = xmalloc(sizeof(env[0]));
634
635         be_assure_liveness(birg);
636
637         obstack_init(&env->obst);
638         env->arch_env = arch_env;
639         env->birg = birg;
640         env->spills = new_set(cmp_spill, 10);
641         env->reloads = NEW_ARR_F(ir_node*, 0);
642         env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
643         env->memperms = new_set(cmp_memperm, 10);
644
645         return env;
646 }
647
648 void be_free_frame_entity_coalescer(be_fec_env_t *env)
649 {
650         del_set(env->memperms);
651         DEL_ARR_F(env->reloads);
652         DEL_ARR_F(env->affinity_edges);
653         del_set(env->spills);
654         obstack_free(&env->obst, NULL);
655
656         free(env);
657 }
658
659 void be_assign_entities(be_fec_env_t *env)
660 {
661         if(be_stat_ev_is_active()) {
662                 int count = set_count(env->spills);
663                 be_stat_ev("spillslots", count);
664         }
665
666         if(be_coalesce_spill_slots) {
667                 do_greedy_coalescing(env);
668         }
669
670         if(be_stat_ev_is_active()) {
671                 int count = count_spillslots(env);
672                 be_stat_ev("spillslots_after_coalescing", count);
673         }
674
675         assign_spillslots(env);
676
677         create_memperms(env);
678 }
679
680 /**
681  * This walker function searches for reloads and collects all the spills
682  * and memphis attached to them.
683  */
684 static void collect_spills_walker(ir_node *node, void *data)
685 {
686         be_fec_env_t *env = data;
687         const arch_env_t *arch_env = env->arch_env;
688         const ir_mode *mode;
689         const arch_register_class_t *cls;
690         int align;
691
692         /* classify returns classification of the irn the proj is attached to */
693         if (is_Proj(node))
694                 return;
695
696         if (!arch_irn_class_is(arch_env, node, reload))
697                 return;
698
699         mode = get_irn_mode(node);
700         cls = arch_get_irn_reg_class(arch_env, node, -1);
701         align = arch_isa_get_reg_class_alignment(arch_env_get_isa(arch_env), cls);
702
703         be_node_needs_frame_entity(env, node, mode, align);
704 }
705
706 void be_coalesce_spillslots(be_irg_t *birg)
707 {
708         be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
709
710         /* collect reloads */
711         irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
712
713         be_assign_entities(env);
714
715         be_free_frame_entity_coalescer(env);
716 }
717
718 void be_init_spillslots(void)
719 {
720         FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
721 }
722
723 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);