Minor corrections.
[libfirm] / ir / be / benewalloc.c
1 /*
2  * Copyright (C) 1995-2008 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       New approach to allocation and copy coalescing
23  * @author      Matthias Braun
24  * @date        14.2.2009
25  * @version     $Id$
26  *
27  * ... WE NEED A NAME FOR THIS ...
28  *
29  * Only a proof of concept at this moment...
30  *
31  * The idea is to allocate registers in 2 passes:
32  * 1. A first pass to determine "preferred" registers for live-ranges. This
33  *    calculates for each register and each live-range a value indicating
34  *    the usefulness. (You can roughly think of the value as the negative
35  *    costs needed for copies when the value is in the specific registers...)
36  *
37  * 2. Walk blocks and assigns registers in a greedy fashion. Preferring
38  *    registers with high preferences. When register constraints are not met,
39  *    add copies and split live-ranges.
40  *
41  * TODO:
42  *  - make use of free registers in the permutate_values code
43  *  - output constraints are not ensured. The algorithm fails to copy values
44  *    away, so the registers for constrained outputs are free.
45  *  - must_be_different constraint is not respected
46  *  - We have to pessimistically construct Phi_0s when not all predecessors
47  *    of a block are known.
48  *  - Phi color assignment should give bonus points towards registers already
49  *    assigned at predecessors.
50  *  - think about a smarter sequence of visiting the blocks. Sorted by
51  *    execfreq might be good, or looptree from inner to outermost loops going
52  *    over blocks in a reverse postorder
53  */
54 #include "config.h"
55
56 #include <float.h>
57
58 #include "obst.h"
59 #include "irnode_t.h"
60 #include "irgraph_t.h"
61 #include "iredges_t.h"
62 #include "ircons.h"
63 #include "irgwalk.h"
64 #include "execfreq.h"
65
66 #include "be.h"
67 #include "bera.h"
68 #include "belive_t.h"
69 #include "bemodule.h"
70 #include "bechordal_t.h"
71 #include "besched.h"
72 #include "beirg.h"
73 #include "benode_t.h"
74 #include "bespill.h"
75 #include "bespillutil.h"
76 #include "beverify.h"
77
78 #include "bipartite.h"
79 #include "hungarian.h"
80
81 #define USE_FACTOR       1.0f
82 #define DEF_FACTOR       1.0f
83 #define NEIGHBOR_FACTOR  0.2f
84 #define SHOULD_BE_SAME   1.0f
85
86 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
87
88 static struct obstack               obst;
89 static be_irg_t                    *birg;
90 static ir_graph                    *irg;
91 static const arch_register_class_t *cls;
92 static be_lv_t                     *lv;
93 static const ir_exec_freq          *execfreqs;
94 static unsigned                     n_regs;
95 static bitset_t                    *ignore_regs;
96
97 /** info about the current assignment for a register */
98 struct assignment_t {
99         ir_node *value;            /**< currently assigned value */
100 };
101 typedef struct assignment_t assignment_t;
102
103 /** currently active assignments (while processing a basic block) */
104 static assignment_t *assignments;
105
106 /**
107  * allocation information: last_uses, register preferences
108  * the information is per firm-node.
109  */
110 struct allocation_info_t {
111         unsigned      last_uses;   /**< bitset indicating last uses (input pos) */
112         assignment_t *current_assignment;
113         float         prefs[0];    /**< register preferences */
114 };
115 typedef struct allocation_info_t allocation_info_t;
116
117 /** helper datastructure used when sorting register preferences */
118 struct reg_pref_t {
119         unsigned num;
120         float    pref;
121 };
122 typedef struct reg_pref_t reg_pref_t;
123
124 /** per basic-block information */
125 struct block_info_t {
126         int          processed;       /**< indicate wether block is processed */
127         assignment_t assignments[0];  /**< register assignments at end of block */
128 };
129 typedef struct block_info_t block_info_t;
130
131 /**
132  * Get the allocation info for a node.
133  * The info is allocated on the first visit of a node.
134  */
135 static allocation_info_t *get_allocation_info(ir_node *node)
136 {
137         allocation_info_t *info;
138         if (!irn_visited_else_mark(node)) {
139                 size_t size = sizeof(info[0]) + n_regs * sizeof(info->prefs[0]);
140                 info = obstack_alloc(&obst, size);
141                 memset(info, 0, size);
142                 set_irn_link(node, info);
143         } else {
144                 info = get_irn_link(node);
145         }
146
147         return info;
148 }
149
150 /**
151  * Get allocation information for a basic block
152  */
153 static block_info_t *get_block_info(ir_node *block)
154 {
155         block_info_t *info;
156
157         assert(is_Block(block));
158         if (!irn_visited_else_mark(block)) {
159                 size_t size = sizeof(info[0]) + n_regs * sizeof(info->assignments[0]);
160                 info = obstack_alloc(&obst, size);
161                 memset(info, 0, size);
162                 set_irn_link(block, info);
163         } else {
164                 info = get_irn_link(block);
165         }
166
167         return info;
168 }
169
170 /**
171  * Link the allocation info of a node to a copy.
172  * Afterwards, both nodes uses the same allocation info.
173  * Copy must not have an allocation info assigned yet.
174  *
175  * @param copy   the node that gets the allocation info assigned
176  * @param value  the original node
177  */
178 static void link_to(ir_node *copy, ir_node *value)
179 {
180         allocation_info_t *info = get_allocation_info(value);
181         assert(!irn_visited(copy));
182         set_irn_link(copy, info);
183         mark_irn_visited(copy);
184 }
185
186 /**
187  * Calculate the penalties for every register on a node and its live neighbors.
188  *
189  * @param live_nodes   the set of live nodes at the current position, may be NULL
190  * @param penalty      the penalty to subtract from
191  * @param limited      a raw bitset containing the limited set for the node
192  * @param node         the node
193  */
194 static void give_penalties_for_limits(const ir_nodeset_t *live_nodes,
195                                       float penalty, const unsigned* limited,
196                                                                           ir_node *node)
197 {
198         ir_nodeset_iterator_t iter;
199         unsigned              r;
200         allocation_info_t     *info = get_allocation_info(node);
201         ir_node               *neighbor;
202
203         /* give penalty for all forbidden regs */
204         for (r = 0; r < n_regs; ++r) {
205                 if (rbitset_is_set(limited, r))
206                         continue;
207
208                 info->prefs[r] -= penalty;
209         }
210
211         /* all other live values should get a penalty for allowed regs */
212         if (live_nodes == NULL)
213                 return;
214
215         /* TODO: reduce penalty if there are multiple allowed registers... */
216         penalty *= NEIGHBOR_FACTOR;
217         foreach_ir_nodeset(live_nodes, neighbor, iter) {
218                 allocation_info_t *neighbor_info;
219
220                 /* TODO: if op is used on multiple inputs we might not do a
221                  * continue here */
222                 if (neighbor == node)
223                         continue;
224
225                 neighbor_info = get_allocation_info(neighbor);
226                 for (r = 0; r < n_regs; ++r) {
227                         if (!rbitset_is_set(limited, r))
228                                 continue;
229
230                         neighbor_info->prefs[r] -= penalty;
231                 }
232         }
233 }
234
235 /**
236  * Calculate the preferences of a definition for the current register class.
237  * If the definition uses a limited set of registers, reduce the preferences
238  * for the limited register on the node and its neighbors.
239  *
240  * @param live_nodes  the set of live nodes at the current node
241  * @param weight      the weight
242  * @param node        the current node
243  */
244 static void check_defs(const ir_nodeset_t *live_nodes, float weight,
245                        ir_node *node)
246 {
247         const arch_register_req_t *req;
248
249         if (get_irn_mode(node) == mode_T) {
250                 const ir_edge_t *edge;
251                 foreach_out_edge(node, edge) {
252                         ir_node *proj = get_edge_src_irn(edge);
253                         check_defs(live_nodes, weight, proj);
254                 }
255                 return;
256         }
257
258         if (!arch_irn_consider_in_reg_alloc(cls, node))
259                 return;
260
261         req = arch_get_register_req_out(node);
262         if (req->type & arch_register_req_type_limited) {
263                 const unsigned *limited = req->limited;
264                 float           penalty = weight * DEF_FACTOR;
265                 give_penalties_for_limits(live_nodes, penalty, limited, node);
266         }
267
268         if (req->type & arch_register_req_type_should_be_same) {
269                 ir_node           *insn  = skip_Proj(node);
270                 allocation_info_t *info  = get_allocation_info(node);
271                 int                arity = get_irn_arity(insn);
272                 int                i;
273
274                 float factor = 1.0f / rbitset_popcnt(&req->other_same, arity);
275                 for (i = 0; i < arity; ++i) {
276                         ir_node           *op;
277                         unsigned           r;
278                         allocation_info_t *op_info;
279
280                         if (!rbitset_is_set(&req->other_same, i))
281                                 continue;
282
283                         op      = get_irn_n(insn, i);
284                         op_info = get_allocation_info(op);
285                         for (r = 0; r < n_regs; ++r) {
286                                 if (bitset_is_set(ignore_regs, r))
287                                         continue;
288                                 op_info->prefs[r] += info->prefs[r] * factor;
289                         }
290                 }
291         }
292 }
293
294 /**
295  * Walker: Runs an a block calculates the preferences for any
296  * node and every register from the considered register class.
297  */
298 static void analyze_block(ir_node *block, void *data)
299 {
300         float         weight = get_block_execfreq(execfreqs, block);
301         ir_nodeset_t  live_nodes;
302         ir_node      *node;
303         (void) data;
304
305         ir_nodeset_init(&live_nodes);
306         be_liveness_end_of_block(lv, cls, block, &live_nodes);
307
308         sched_foreach_reverse(block, node) {
309                 allocation_info_t *info;
310                 int                i, arity;
311
312                 if (is_Phi(node)) {
313                         /* TODO: handle constrained phi-nodes */
314                         break;
315                 }
316
317                 /* TODO give/take penalties for should_be_same/different) */
318                 check_defs(&live_nodes, weight, node);
319
320                 /* mark last uses */
321                 arity = get_irn_arity(node);
322                 /* I was lazy, and only allocated 1 unsigned
323                    => maximum of 32 uses per node (rewrite if necessary) */
324                 assert(arity <= (int) sizeof(unsigned) * 8);
325
326                 info = get_allocation_info(node);
327                 for (i = 0; i < arity; ++i) {
328                         ir_node *op = get_irn_n(node, i);
329                         if (!arch_irn_consider_in_reg_alloc(cls, op))
330                                 continue;
331
332                         /* last usage of a value? */
333                         if (!ir_nodeset_contains(&live_nodes, op)) {
334                                 rbitset_set(&info->last_uses, i);
335                         }
336                 }
337
338                 be_liveness_transfer(cls, node, &live_nodes);
339
340                 /* update weights based on usage constraints */
341                 for (i = 0; i < arity; ++i) {
342                         const arch_register_req_t *req;
343                         const unsigned            *limited;
344                         ir_node                   *op = get_irn_n(node, i);
345
346                         if (!arch_irn_consider_in_reg_alloc(cls, op))
347                                 continue;
348
349                         req = arch_get_register_req(node, i);
350                         if (!(req->type & arch_register_req_type_limited))
351                                 continue;
352
353                         /* TODO: give penalties to neighbors for precolored nodes! */
354
355                         limited = req->limited;
356                         give_penalties_for_limits(&live_nodes, weight * USE_FACTOR, limited,
357                                                   op);
358                 }
359         }
360
361         ir_nodeset_destroy(&live_nodes);
362 }
363
364 /**
365  * Assign register reg to the given node.
366  *
367  * @param node  the node
368  * @param reg   the register
369  */
370 static void use_reg(ir_node *node, const arch_register_t *reg)
371 {
372         unsigned           r          = arch_register_get_index(reg);
373         assignment_t      *assignment = &assignments[r];
374         allocation_info_t *info;
375
376         assert(assignment->value == NULL);
377         assignment->value = node;
378
379         info = get_allocation_info(node);
380         info->current_assignment = assignment;
381
382         arch_set_irn_register(node, reg);
383 }
384
385 /**
386  * Compare two register preferences in decreasing order.
387  */
388 static int compare_reg_pref(const void *e1, const void *e2)
389 {
390         const reg_pref_t *rp1 = (const reg_pref_t*) e1;
391         const reg_pref_t *rp2 = (const reg_pref_t*) e2;
392         if (rp1->pref < rp2->pref)
393                 return 1;
394         if (rp1->pref > rp2->pref)
395                 return -1;
396         return 0;
397 }
398
399 static void fill_sort_candidates(reg_pref_t *regprefs,
400                                  const allocation_info_t *info)
401 {
402         unsigned r;
403
404         for (r = 0; r < n_regs; ++r) {
405                 float pref = info->prefs[r];
406                 if (bitset_is_set(ignore_regs, r)) {
407                         pref = -10000;
408                 }
409                 regprefs[r].num  = r;
410                 regprefs[r].pref = pref;
411         }
412         /* TODO: use a stable sort here to avoid unnecessary register jumping */
413         qsort(regprefs, n_regs, sizeof(regprefs[0]), compare_reg_pref);
414 }
415
416 /**
417  * Determine and assign a register for node @p node
418  */
419 static void assign_reg(const ir_node *block, ir_node *node)
420 {
421         const arch_register_t     *reg;
422         allocation_info_t         *info;
423         const arch_register_req_t *req;
424         reg_pref_t                *reg_prefs;
425         ir_node                   *in_node;
426         unsigned                  i;
427
428         assert(arch_irn_consider_in_reg_alloc(cls, node));
429
430         /* preassigned register? */
431         reg = arch_get_irn_register(node);
432         if (reg != NULL) {
433                 DB((dbg, LEVEL_2, "Preassignment %+F -> %s\n", node, reg->name));
434                 use_reg(node, reg);
435                 return;
436         }
437
438         /* give should_be_same boni */
439         info = get_allocation_info(node);
440         req  = arch_get_register_req_out(node);
441
442         in_node = skip_Proj(node);
443         if (req->type & arch_register_req_type_should_be_same) {
444                 float weight = get_block_execfreq(execfreqs, block);
445                 int   arity  = get_irn_arity(in_node);
446                 int   i;
447
448                 assert(arity <= (int) sizeof(req->other_same) * 8);
449                 for (i = 0; i < arity; ++i) {
450                         ir_node               *in;
451                         const arch_register_t *reg;
452                         unsigned               r;
453                         if (!rbitset_is_set(&req->other_same, i))
454                                 continue;
455
456                         in  = get_irn_n(in_node, i);
457                         reg = arch_get_irn_register(in);
458                         assert(reg != NULL);
459                         r = arch_register_get_index(reg);
460                         if (bitset_is_set(ignore_regs, r))
461                                 continue;
462                         info->prefs[r] += weight * SHOULD_BE_SAME;
463                 }
464         }
465
466         /* TODO: handle must_be_different */
467
468         /*  */
469         DB((dbg, LEVEL_2, "Candidates for %+F:", node));
470         reg_prefs = alloca(n_regs * sizeof(reg_prefs[0]));
471         fill_sort_candidates(reg_prefs, info);
472         for (i = 0; i < n_regs; ++i) {
473                 unsigned               num = reg_prefs[i].num;
474                 const arch_register_t *reg = arch_register_for_index(cls, num);
475                 DB((dbg, LEVEL_2, " %s(%f)", reg->name, reg_prefs[i].pref));
476         }
477         DB((dbg, LEVEL_2, "\n"));
478
479         for (i = 0; i < n_regs; ++i) {
480                 unsigned r = reg_prefs[i].num;
481                 /* ignores should be last and we should have a non-ignore left */
482                 assert(!bitset_is_set(ignore_regs, r));
483                 /* already used?
484            TODO: It might be better to copy the value occupying the register around here, find out when... */
485                 if (assignments[r].value != NULL)
486                         continue;
487                 reg = arch_register_for_index(cls, r);
488                 DB((dbg, LEVEL_2, "Assign %+F -> %s\n", node, reg->name));
489                 use_reg(node, reg);
490                 break;
491         }
492 }
493
494 static void free_reg_of_value(ir_node *node)
495 {
496         allocation_info_t *info;
497         assignment_t      *assignment;
498         unsigned          r;
499
500         if (!arch_irn_consider_in_reg_alloc(cls, node))
501                 return;
502
503         info       = get_allocation_info(node);
504         assignment = info->current_assignment;
505
506         assert(assignment != NULL);
507
508         r = assignment - assignments;
509         DB((dbg, LEVEL_2, "Value %+F ended, freeing %s\n",
510                 node, arch_register_for_index(cls, r)->name));
511         assignment->value        = NULL;
512         info->current_assignment = NULL;
513 }
514
515 /**
516  * Return the index of the currently assigned register of a node.
517  */
518 static unsigned get_current_reg(ir_node *node)
519 {
520         allocation_info_t *info       = get_allocation_info(node);
521         assignment_t      *assignment = info->current_assignment;
522         return assignment - assignments;
523 }
524
525 /**
526  * Return the currently assigned assignment of a node.
527  */
528 static assignment_t *get_current_assignment(ir_node *node)
529 {
530         allocation_info_t *info = get_allocation_info(node);
531         return info->current_assignment;
532 }
533
534 /**
535  * Add an permutation in front of a node and change the assignments
536  * due to this permutation.
537  *
538  * To understand this imagine a permutation like this:
539  *
540  * 1 -> 2
541  * 2 -> 3
542  * 3 -> 1, 5
543  * 4 -> 6
544  * 5
545  * 6
546  * 7 -> 7
547  *
548  * First we count how many destinations a single value has. At the same time
549  * we can be sure that each destination register has at most 1 source register
550  * (it can have 0 which means we don't care what value is in it).
551  * We ignore all fullfilled permuations (like 7->7)
552  * In a first pass we create as much copy instructions as possible as they
553  * are generally cheaper than exchanges. We do this by counting into how many
554  * destinations a register has to be copied (in the example it's 2 for register
555  * 3, or 1 for the registers 1,2,4 and 7).
556  * We can then create a copy into every destination register when the usecount
557  * of that register is 0 (= noone else needs the value in the register).
558  *
559  * After this step we should have cycles left. We implement a cyclic permutation
560  * of n registers with n-1 transpositions.
561  *
562  * @param live_nodes   the set of live nodes, updated due to live range split
563  * @param before       the node before we add the permutation
564  * @param permutation  the permutation array indices are the destination
565  *                     registers, the values in the array are the source
566  *                     registers.
567  */
568 static void permutate_values(ir_nodeset_t *live_nodes, ir_node *before,
569                              unsigned *permutation)
570 {
571         ir_node   *block;
572         ir_node  **ins    = ALLOCANZ(ir_node*, n_regs);
573         unsigned  *n_used = ALLOCANZ(unsigned, n_regs);
574         unsigned   r;
575
576         /* create a list of permutations. Leave out fix points. */
577         for (r = 0; r < n_regs; ++r) {
578                 unsigned      old_reg = permutation[r];
579                 assignment_t *assignment;
580                 ir_node      *value;
581
582                 /* no need to do anything for a fixpoint */
583                 if (old_reg == r)
584                         continue;
585
586                 assignment = &assignments[old_reg];
587                 value      = assignment->value;
588                 if (value == NULL) {
589                         /* nothing to do here, reg is not live. Mark it as fixpoint
590                          * so we ignore it in the next steps */
591                         permutation[r] = r;
592                         continue;
593                 }
594
595                 ins[old_reg] = value;
596                 ++n_used[old_reg];
597
598                 /* free occupation infos, we'll add the values back later */
599                 if (live_nodes != NULL) {
600                         free_reg_of_value(value);
601                         ir_nodeset_remove(live_nodes, value);
602                 }
603         }
604
605         block = get_nodes_block(before);
606
607         /* step1: create copies where immediately possible */
608         for (r = 0; r < n_regs; /* empty */) {
609                 ir_node *copy;
610                 ir_node *src;
611                 const arch_register_t *reg;
612                 unsigned               old_r = permutation[r];
613
614                 /* - no need to do anything for fixed points.
615                    - we can't copy if the value in the dest reg is still needed */
616                 if (old_r == r || n_used[r] > 0) {
617                         ++r;
618                         continue;
619                 }
620
621                 /* create a copy */
622                 src = ins[old_r];
623                 copy = be_new_Copy(cls, block, src);
624                 reg = arch_register_for_index(cls, r);
625                 DB((dbg, LEVEL_2, "Copy %+F (from %+F) -> %s\n", copy, src, reg->name));
626                 link_to(copy, src);
627                 use_reg(copy, reg);
628                 sched_add_before(before, copy);
629
630                 /* old register has 1 user less, permutation is resolved */
631                 assert(arch_register_get_index(arch_get_irn_register(src)) == old_r);
632                 assert(n_used[old_r] > 0);
633                 --n_used[old_r];
634                 permutation[r] = r;
635
636                 /* advance or jump back (this copy could have enabled another copy) */
637                 if (old_r < r && n_used[old_r] == 0) {
638                         r = old_r;
639                 } else {
640                         ++r;
641                 }
642         }
643
644         /* at this point we only have "cycles" left which we have to resolve with
645          * perm instructions
646          * TODO: if we have free registers left, then we should really use copy
647          * instructions for any cycle longer than 2 registers...
648          * (this is probably architecture dependent, there might be archs where
649          *  copies are preferable even for 2 cycles)
650          */
651
652         /* create perms with the rest */
653         for (r = 0; r < n_regs; /* empty */) {
654                 const arch_register_t *reg;
655                 unsigned  old_r = permutation[r];
656                 unsigned  r2;
657                 ir_node  *in[2];
658                 ir_node  *perm;
659                 ir_node  *proj0;
660                 ir_node  *proj1;
661
662                 if (old_r == r) {
663                         ++r;
664                         continue;
665                 }
666
667                 /* we shouldn't have copies from 1 value to multiple destinations left*/
668                 assert(n_used[old_r] == 1);
669
670                 /* exchange old_r and r2; after that old_r is a fixed point */
671                 r2 = permutation[old_r];
672
673                 in[0] = ins[r2];
674                 in[1] = ins[old_r];
675                 perm = be_new_Perm(cls, block, 2, in);
676
677                 proj0 = new_r_Proj(block, perm, get_irn_mode(in[0]), 0);
678                 link_to(proj0, in[0]);
679                 reg = arch_register_for_index(cls, old_r);
680                 use_reg(proj0, reg);
681
682                 proj1 = new_r_Proj(block, perm, get_irn_mode(in[1]), 1);
683
684                 /* 1 value is now in the correct register */
685                 permutation[old_r] = old_r;
686                 /* the source of r changed to r2 */
687                 permutation[r] = r2;
688                 ins[r2] = in[1];
689                 reg = arch_register_for_index(cls, r2);
690                 if (r == r2) {
691                         /* if we have reached a fixpoint update data structures */
692                         link_to(proj1, in[1]);
693                         use_reg(proj1, reg);
694                 } else {
695                         arch_set_irn_register(proj1, reg);
696                 }
697         }
698
699 #ifdef DEBUG_libfirm
700         /* now we should only have fixpoints left */
701         for (r = 0; r < n_regs; ++r) {
702                 assert(permutation[r] == r);
703         }
704 #endif
705 }
706
707 /**
708  * Free regs for values last used.
709  *
710  * @param live_nodes   set of live nodes, will be updated
711  * @param node         the node to consider
712  */
713 static void free_last_uses(ir_nodeset_t *live_nodes, ir_node *node)
714 {
715         allocation_info_t *info  = get_allocation_info(node);
716         int                arity = get_irn_arity(node);
717         int                i;
718         for (i = 0; i < arity; ++i) {
719                 ir_node *op;
720
721                 /* check if one operand is the last use */
722                 if (!rbitset_is_set(&info->last_uses, i))
723                         continue;
724
725                 op = get_irn_n(node, i);
726                 free_reg_of_value(op);
727                 ir_nodeset_remove(live_nodes, op);
728         }
729 }
730
731 /**
732  * Create a bitset of registers occupied with value living through an
733  * instruction
734  */
735 static void determine_live_through_regs(unsigned *bitset, ir_node *node)
736 {
737         const allocation_info_t *info = get_allocation_info(node);
738         unsigned r;
739         int i;
740         int arity;
741
742         /* mark all used registers as potentially live-through */
743         for (r = 0; r < n_regs; ++r) {
744                 const assignment_t *assignment = &assignments[r];
745                 if (assignment->value == NULL)
746                         continue;
747
748                 rbitset_set(bitset, r);
749         }
750
751         /* remove registers of value dying at the instruction */
752         arity = get_irn_arity(node);
753         for (i = 0; i < arity; ++i) {
754                 ir_node               *op;
755                 const arch_register_t *reg;
756
757                 if (!rbitset_is_set(&info->last_uses, i))
758                         continue;
759
760                 op  = get_irn_n(node, i);
761                 reg = arch_get_irn_register(op);
762                 rbitset_clear(bitset, arch_register_get_index(reg));
763         }
764 }
765
766 /**
767  * Enforce constraints at a node by live range splits.
768  *
769  * @param live_nodes  the set of live nodes, might be changed
770  * @param node        the current node
771  */
772 static void enforce_constraints(ir_nodeset_t *live_nodes, ir_node *node)
773 {
774         int arity = get_irn_arity(node);
775         int i, dummy, res;
776         hungarian_problem_t *bp;
777         unsigned l, r, p;
778         unsigned *assignment;
779
780         /* see if any use constraints are not met */
781         bool good = true;
782         for (i = 0; i < arity; ++i) {
783                 ir_node                   *op = get_irn_n(node, i);
784                 const arch_register_req_t *req;
785                 const unsigned            *limited;
786                 unsigned                  r;
787
788                 if (!arch_irn_consider_in_reg_alloc(cls, op))
789                         continue;
790
791                 /* are there any limitations for the i'th operand? */
792                 req = arch_get_register_req(node, i);
793                 if (!(req->type & arch_register_req_type_limited))
794                         continue;
795
796                 limited = req->limited;
797                 r       = get_current_reg(op);
798                 if (!rbitset_is_set(limited, r)) {
799                         /* found an assignement outside the limited set */
800                         good = false;
801                         break;
802                 }
803         }
804
805         /* construct a list of register occupied by live-through values */
806         unsigned *live_through_regs = NULL;
807         unsigned *output_regs       = NULL;
808
809         /* is any of the live-throughs using a constrainted output register? */
810         if (get_irn_mode(node) == mode_T) {
811                 const ir_edge_t *edge;
812
813                 foreach_out_edge(node, edge) {
814                         ir_node *proj = get_edge_src_irn(edge);
815                         const arch_register_req_t *req;
816
817                         if (!arch_irn_consider_in_reg_alloc(cls, proj))
818                                 continue;
819
820                         req = arch_get_register_req_out(proj);
821                         if (!(req->type & arch_register_req_type_limited))
822                                 continue;
823
824                         if (live_through_regs == NULL) {
825                                 rbitset_alloca(live_through_regs, n_regs);
826                                 determine_live_through_regs(live_through_regs, node);
827
828                                 rbitset_alloca(output_regs, n_regs);
829                         }
830
831                         rbitset_or(output_regs, req->limited, n_regs);
832                         if (rbitsets_have_common(req->limited, live_through_regs, n_regs)) {
833                                 good = false;
834                                 break;
835                         }
836                 }
837         } else {
838                 if (arch_irn_consider_in_reg_alloc(cls, node)) {
839                         const arch_register_req_t *req = arch_get_register_req_out(node);
840                         if (req->type & arch_register_req_type_limited) {
841                                 rbitset_alloca(live_through_regs, n_regs);
842                                 determine_live_through_regs(live_through_regs, node);
843                                 if (rbitsets_have_common(req->limited, live_through_regs, n_regs)) {
844                                         good = false;
845
846                                         rbitset_alloca(output_regs, n_regs);
847                                         rbitset_or(output_regs, req->limited, n_regs);
848                                 }
849                         }
850                 }
851         }
852
853         if (good)
854                 return;
855
856         if (live_through_regs == NULL) {
857                 rbitset_alloca(live_through_regs, n_regs);
858                 rbitset_alloca(output_regs, n_regs);
859         }
860
861         /* swap values around */
862         bp = hungarian_new(n_regs, n_regs, HUNGARIAN_MATCH_PERFECT);
863
864         /* add all combinations, then remove not allowed ones */
865         for (l = 0; l < n_regs; ++l) {
866                 if (bitset_is_set(ignore_regs, l)) {
867                         hungarian_add(bp, l, l, 90);
868                         continue;
869                 }
870
871                 for (r = 0; r < n_regs; ++r) {
872                         if (bitset_is_set(ignore_regs, r))
873                                 continue;
874                         /* livethrough values may not use constrainted output registers */
875                         if (rbitset_is_set(live_through_regs, l)
876                                         && rbitset_is_set(output_regs, r))
877                                 continue;
878
879                         hungarian_add(bp, l, r, l == r ? 90 : 89);
880                 }
881         }
882
883         for (i = 0; i < arity; ++i) {
884                 ir_node                   *op = get_irn_n(node, i);
885                 const arch_register_req_t *req;
886                 const unsigned            *limited;
887                 unsigned                  current_reg;
888
889                 if (!arch_irn_consider_in_reg_alloc(cls, op))
890                         continue;
891
892                 req = arch_get_register_req(node, i);
893                 if (!(req->type & arch_register_req_type_limited))
894                         continue;
895
896                 limited     = req->limited;
897                 current_reg = get_current_reg(op);
898                 for (r = 0; r < n_regs; ++r) {
899                         if (rbitset_is_set(limited, r))
900                                 continue;
901                         hungarian_remv(bp, current_reg, r);
902                 }
903         }
904
905         hungarian_print_costmatrix(bp, 1);
906         hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
907
908         assignment = ALLOCAN(unsigned, n_regs);
909         res = hungarian_solve(bp, (int*) assignment, &dummy, 0);
910         assert(res == 0);
911
912         printf("Swap result:");
913         for (p = 0; p < n_regs; ++p) {
914                 printf(" %d", assignment[p]);
915         }
916         printf("\n");
917
918         hungarian_free(bp);
919
920         permutate_values(live_nodes, node, assignment);
921 }
922
923 /** test wether a node @p n is a copy of the value of node @p of */
924 static int is_copy_of(ir_node *n, ir_node *of)
925 {
926         allocation_info_t *of_info;
927
928         if (n == NULL)
929                 return 0;
930
931         if (n == of)
932                 return 1;
933
934         of_info = get_allocation_info(of);
935         if (!irn_visited(n))
936                 return 0;
937
938         return of_info == get_irn_link(n);
939 }
940
941 /** find a value in the end-assignment of a basic block
942  * @returns the index into the assignment array if found
943  *          -1 if not found
944  */
945 static int find_value_in_block_info(block_info_t *info, ir_node *value)
946 {
947         unsigned      r;
948         assignment_t *assignments = info->assignments;
949         for (r = 0; r < n_regs; ++r) {
950                 const assignment_t *assignment = &assignments[r];
951                 if (is_copy_of(assignment->value, value))
952                         return (int) r;
953         }
954
955         return -1;
956 }
957
958 /**
959  * Create the necessary permutations at the end of a basic block to fullfill
960  * the register assignment for phi-nodes in the next block
961  */
962 static void add_phi_permutations(ir_node *block, int p)
963 {
964         unsigned  r;
965         unsigned *permutation;
966         assignment_t *old_assignments;
967         int       need_permutation;
968         ir_node  *node;
969         ir_node  *pred = get_Block_cfgpred_block(block, p);
970
971         block_info_t *pred_info = get_block_info(pred);
972
973         /* predecessor not processed yet? nothing to do */
974         if (!pred_info->processed)
975                 return;
976
977         permutation = ALLOCAN(unsigned, n_regs);
978         for (r = 0; r < n_regs; ++r) {
979                 permutation[r] = r;
980         }
981
982         /* check phi nodes */
983         need_permutation = 0;
984         node = sched_first(block);
985         for ( ; is_Phi(node); node = sched_next(node)) {
986                 const arch_register_t *reg;
987                 int                    regn;
988                 int                    a;
989                 ir_node               *op;
990
991                 if (!arch_irn_consider_in_reg_alloc(cls, node))
992                         continue;
993
994                 op = get_Phi_pred(node, p);
995                 a = find_value_in_block_info(pred_info, op);
996                 assert(a >= 0);
997
998                 reg = arch_get_irn_register(node);
999                 regn = arch_register_get_index(reg);
1000                 if (regn != a) {
1001                         permutation[regn] = a;
1002                         need_permutation = 1;
1003                 }
1004         }
1005
1006         old_assignments = assignments;
1007         assignments     = pred_info->assignments;
1008         permutate_values(NULL, be_get_end_of_block_insertion_point(pred),
1009                          permutation);
1010         assignments     = old_assignments;
1011
1012         node = sched_first(block);
1013         for ( ; is_Phi(node); node = sched_next(node)) {
1014                 int                    a;
1015                 ir_node               *op;
1016
1017                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1018                         continue;
1019
1020                 op = get_Phi_pred(node, p);
1021                 /* TODO: optimize */
1022                 a = find_value_in_block_info(pred_info, op);
1023                 assert(a >= 0);
1024
1025                 op = pred_info->assignments[a].value;
1026                 set_Phi_pred(node, p, op);
1027         }
1028 }
1029
1030 /**
1031  * Walker: assign registers to all nodes of a block that
1032  * need registers from the currently considered register class.
1033  */
1034 static void allocate_coalesce_block(ir_node *block, void *data)
1035 {
1036         int                    i;
1037         unsigned               r;
1038         ir_nodeset_t           live_nodes;
1039         ir_nodeset_iterator_t  iter;
1040         ir_node               *node, *start;
1041         int                    n_preds;
1042         block_info_t          *block_info;
1043         block_info_t         **pred_block_infos;
1044
1045         (void) data;
1046         DB((dbg, LEVEL_2, "Allocating in block %+F\n", block));
1047
1048         /* clear assignments */
1049         block_info  = get_block_info(block);
1050         assignments = block_info->assignments;
1051
1052         for (r = 0; r < n_regs; ++r) {
1053                 assignment_t       *assignment = &assignments[r];
1054                 ir_node            *value      = assignment->value;
1055                 allocation_info_t  *info;
1056
1057                 if (value == NULL)
1058                         continue;
1059
1060                 info                     = get_allocation_info(value);
1061                 info->current_assignment = assignment;
1062         }
1063
1064         ir_nodeset_init(&live_nodes);
1065
1066         /* gather regalloc infos of predecessor blocks */
1067         n_preds = get_Block_n_cfgpreds(block);
1068         pred_block_infos = ALLOCAN(block_info_t*, n_preds);
1069         for (i = 0; i < n_preds; ++i) {
1070                 ir_node *pred = get_Block_cfgpred_block(block, i);
1071                 pred_block_infos[i] = get_block_info(pred);
1072         }
1073
1074         /* collect live-in nodes and preassigned values */
1075         be_lv_foreach(lv, block, be_lv_state_in, i) {
1076                 const arch_register_t *reg;
1077
1078                 node = be_lv_get_irn(lv, block, i);
1079                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1080                         continue;
1081
1082                 /* remember that this node is live at the beginning of the block */
1083                 ir_nodeset_insert(&live_nodes, node);
1084
1085                 /* if the node already has a register assigned use it */
1086                 reg = arch_get_irn_register(node);
1087                 if (reg != NULL) {
1088                         /* TODO: consult pred-block infos here. The value could be copied
1089                            away in some/all predecessor blocks. We need to construct
1090                            phi-nodes in this case.
1091                            We even need to construct some Phi_0 like constructs in cases
1092                            where the predecessor allocation is not determined yet. */
1093                         use_reg(node, reg);
1094                 }
1095         }
1096
1097         /* handle phis... */
1098         node = sched_first(block);
1099         for ( ; is_Phi(node); node = sched_next(node)) {
1100                 const arch_register_t *reg;
1101
1102                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1103                         continue;
1104
1105                 /* fill in regs already assigned */
1106                 reg = arch_get_irn_register(node);
1107                 if (reg != NULL) {
1108                         use_reg(node, reg);
1109                 } else {
1110                         /* TODO: give boni for registers already assigned at the
1111                            predecessors */
1112                         assign_reg(block, node);
1113                 }
1114         }
1115         start = node;
1116
1117         /* assign regs for live-in values */
1118         foreach_ir_nodeset(&live_nodes, node, iter) {
1119                 const arch_register_t *reg = arch_get_irn_register(node);
1120                 if (reg != NULL)
1121                         continue;
1122
1123                 assign_reg(block, node);
1124         }
1125
1126         /* permutate values at end of predecessor blocks in case of phi-nodes */
1127         if (n_preds > 1) {
1128                 int p;
1129                 for (p = 0; p < n_preds; ++p) {
1130                         add_phi_permutations(block, p);
1131                 }
1132         }
1133
1134         /* assign instructions in the block */
1135         for (node = start; !sched_is_end(node); node = sched_next(node)) {
1136                 int arity = get_irn_arity(node);
1137                 int i;
1138
1139                 /* enforce use constraints */
1140                 enforce_constraints(&live_nodes, node);
1141
1142                 /* exchange values to copied values where needed */
1143                 for (i = 0; i < arity; ++i) {
1144                         ir_node      *op = get_irn_n(node, i);
1145                         assignment_t *assignment;
1146
1147                         if (!arch_irn_consider_in_reg_alloc(cls, op))
1148                                 continue;
1149                         assignment = get_current_assignment(op);
1150                         assert(assignment != NULL);
1151                         if (op != assignment->value) {
1152                                 set_irn_n(node, i, assignment->value);
1153                         }
1154                 }
1155
1156                 /* free registers of values last used at this instruction */
1157                 free_last_uses(&live_nodes, node);
1158
1159                 /* assign output registers */
1160                 /* TODO: 2 phases: first: pre-assigned ones, 2nd real regs */
1161                 if (get_irn_mode(node) == mode_T) {
1162                         const ir_edge_t *edge;
1163                         foreach_out_edge(node, edge) {
1164                                 ir_node *proj = get_edge_src_irn(edge);
1165                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
1166                                         continue;
1167                                 assign_reg(block, proj);
1168                         }
1169                 } else if (arch_irn_consider_in_reg_alloc(cls, node)) {
1170                         assign_reg(block, node);
1171                 }
1172         }
1173
1174         ir_nodeset_destroy(&live_nodes);
1175         assignments = NULL;
1176
1177         block_info->processed = 1;
1178
1179         /* if we have exactly 1 successor then we might be able to produce phi
1180            copies now */
1181         if (get_irn_n_edges_kind(block, EDGE_KIND_BLOCK) == 1) {
1182                 const ir_edge_t *edge
1183                         = get_irn_out_edge_first_kind(block, EDGE_KIND_BLOCK);
1184                 ir_node      *succ      = get_edge_src_irn(edge);
1185                 int           p         = get_edge_src_pos(edge);
1186                 block_info_t *succ_info = get_block_info(succ);
1187
1188                 if (succ_info->processed) {
1189                         add_phi_permutations(succ, p);
1190                 }
1191         }
1192 }
1193
1194 /**
1195  * Run the register allocator for the current register class.
1196  */
1197 static void be_straight_alloc_cls(void)
1198 {
1199         lv = be_assure_liveness(birg);
1200         be_liveness_assure_sets(lv);
1201         be_liveness_assure_chk(lv);
1202
1203         assignments = NULL;
1204
1205         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
1206         inc_irg_visited(irg);
1207
1208         DB((dbg, LEVEL_2, "=== Allocating registers of %s ===\n", cls->name));
1209
1210         irg_block_walk_graph(irg, NULL, analyze_block, NULL);
1211         irg_block_walk_graph(irg, NULL, allocate_coalesce_block, NULL);
1212
1213         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
1214 }
1215
1216 /**
1217  * Run the spiller on the current graph.
1218  */
1219 static void spill(void)
1220 {
1221         /* make sure all nodes show their real register pressure */
1222         BE_TIMER_PUSH(t_ra_constr);
1223         be_pre_spill_prepare_constr(birg, cls);
1224         BE_TIMER_POP(t_ra_constr);
1225
1226         /* spill */
1227         BE_TIMER_PUSH(t_ra_spill);
1228         be_do_spill(birg, cls);
1229         BE_TIMER_POP(t_ra_spill);
1230
1231         BE_TIMER_PUSH(t_ra_spill_apply);
1232         check_for_memory_operands(irg);
1233         BE_TIMER_POP(t_ra_spill_apply);
1234 }
1235
1236 /**
1237  * The straight register allocator for a whole procedure.
1238  */
1239 static void be_straight_alloc(be_irg_t *new_birg)
1240 {
1241         const arch_env_t *arch_env = new_birg->main_env->arch_env;
1242         int   n_cls                = arch_env_get_n_reg_class(arch_env);
1243         int   c;
1244
1245         obstack_init(&obst);
1246
1247         birg      = new_birg;
1248         irg       = be_get_birg_irg(birg);
1249         execfreqs = birg->exec_freq;
1250
1251         /* TODO: extract some of the stuff from bechordal allocator, like
1252          * statistics, time measurements, etc. and use them here too */
1253
1254         for (c = 0; c < n_cls; ++c) {
1255                 cls = arch_env_get_reg_class(arch_env, c);
1256                 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
1257                         continue;
1258
1259                 stat_ev_ctx_push_str("bestraight_cls", cls->name);
1260
1261                 n_regs      = arch_register_class_n_regs(cls);
1262                 ignore_regs = bitset_malloc(n_regs);
1263                 be_put_ignore_regs(birg, cls, ignore_regs);
1264
1265                 spill();
1266
1267                 /* verify schedule and register pressure */
1268                 BE_TIMER_PUSH(t_verify);
1269                 if (birg->main_env->options->vrfy_option == BE_CH_VRFY_WARN) {
1270                         be_verify_schedule(birg);
1271                         be_verify_register_pressure(birg, cls, irg);
1272                 } else if (birg->main_env->options->vrfy_option == BE_CH_VRFY_ASSERT) {
1273                         assert(be_verify_schedule(birg) && "Schedule verification failed");
1274                         assert(be_verify_register_pressure(birg, cls, irg)
1275                                 && "Register pressure verification failed");
1276                 }
1277                 BE_TIMER_POP(t_verify);
1278
1279                 BE_TIMER_PUSH(t_ra_color);
1280                 be_straight_alloc_cls();
1281                 BE_TIMER_POP(t_ra_color);
1282
1283                 bitset_free(ignore_regs);
1284
1285                 stat_ev_ctx_pop("bestraight_cls");
1286         }
1287
1288         BE_TIMER_PUSH(t_verify);
1289         if (birg->main_env->options->vrfy_option == BE_CH_VRFY_WARN) {
1290                 be_verify_register_allocation(birg);
1291         } else if(birg->main_env->options->vrfy_option == BE_CH_VRFY_ASSERT) {
1292                 assert(be_verify_register_allocation(birg)
1293                                 && "Register allocation invalid");
1294         }
1295         BE_TIMER_POP(t_verify);
1296
1297         obstack_free(&obst, NULL);
1298 }
1299
1300 /**
1301  * Initializes this module.
1302  */
1303 void be_init_straight_alloc(void)
1304 {
1305         static be_ra_t be_ra_straight = {
1306                 be_straight_alloc,
1307         };
1308
1309         FIRM_DBG_REGISTER(dbg, "firm.be.straightalloc");
1310
1311         be_register_allocator("straight", &be_ra_straight);
1312 }
1313
1314 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_straight_alloc);