49b9300a97543334f320cafdbd3a9637d4258c19
[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 permute_values code
43  *  - think about a smarter sequence of visiting the blocks. Sorted by
44  *    execfreq might be good, or looptree from inner to outermost loops going
45  *    over blocks in a reverse postorder
46  *  - propagate preferences through Phis
47  */
48 #include "config.h"
49
50 #include <float.h>
51 #include <stdbool.h>
52 #include <math.h>
53
54 #include "error.h"
55 #include "execfreq.h"
56 #include "ircons.h"
57 #include "irdom.h"
58 #include "iredges_t.h"
59 #include "irgraph_t.h"
60 #include "irgwalk.h"
61 #include "irnode_t.h"
62 #include "irprintf.h"
63 #include "obst.h"
64 #include "raw_bitset.h"
65 #include "unionfind.h"
66 #include "pdeq.h"
67 #include "hungarian.h"
68
69 #include "beabi.h"
70 #include "bechordal_t.h"
71 #include "be.h"
72 #include "beirg.h"
73 #include "belive_t.h"
74 #include "bemodule.h"
75 #include "benode_t.h"
76 #include "bera.h"
77 #include "besched.h"
78 #include "bespill.h"
79 #include "bespillutil.h"
80 #include "beverify.h"
81 #include "beutil.h"
82
83 #define USE_FACTOR         1.0f
84 #define DEF_FACTOR         1.0f
85 #define NEIGHBOR_FACTOR    0.2f
86 #define AFF_SHOULD_BE_SAME 0.5f
87 #define AFF_PHI            1.0f
88 #define SPLIT_DELTA        1.0f
89
90 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
91
92 static struct obstack               obst;
93 static be_irg_t                    *birg;
94 static ir_graph                    *irg;
95 static const arch_register_class_t *cls;
96 static const arch_register_req_t   *default_cls_req;
97 static be_lv_t                     *lv;
98 static const ir_exec_freq          *execfreqs;
99 static unsigned                     n_regs;
100 static unsigned                    *normal_regs;
101 static int                         *congruence_classes;
102 static ir_node                    **block_order;
103 static int                          n_block_order;
104
105 /** currently active assignments (while processing a basic block)
106  * maps registers to values(their current copies) */
107 static ir_node **assignments;
108
109 /**
110  * allocation information: last_uses, register preferences
111  * the information is per firm-node.
112  */
113 struct allocation_info_t {
114         unsigned  last_uses;      /**< bitset indicating last uses (input pos) */
115         ir_node  *current_value;  /**< copy of the value that should be used */
116         ir_node  *original_value; /**< for copies point to original value */
117         float     prefs[0];       /**< register preferences */
118 };
119 typedef struct allocation_info_t allocation_info_t;
120
121 /** helper datastructure used when sorting register preferences */
122 struct reg_pref_t {
123         unsigned num;
124         float    pref;
125 };
126 typedef struct reg_pref_t reg_pref_t;
127
128 /** per basic-block information */
129 struct block_info_t {
130         bool     processed;       /**< indicate wether block is processed */
131         ir_node *assignments[0];  /**< register assignments at end of block */
132 };
133 typedef struct block_info_t block_info_t;
134
135 /**
136  * Get the allocation info for a node.
137  * The info is allocated on the first visit of a node.
138  */
139 static allocation_info_t *get_allocation_info(ir_node *node)
140 {
141         allocation_info_t *info = get_irn_link(node);
142         if (info == NULL) {
143                 info = OALLOCFZ(&obst, allocation_info_t, prefs, n_regs);
144                 info->current_value  = node;
145                 info->original_value = node;
146                 set_irn_link(node, info);
147         }
148
149         return info;
150 }
151
152 /**
153  * Get allocation information for a basic block
154  */
155 static block_info_t *get_block_info(ir_node *block)
156 {
157         block_info_t *info = get_irn_link(block);
158
159         assert(is_Block(block));
160         if (info == NULL) {
161                 info = OALLOCFZ(&obst, block_info_t, assignments, n_regs);
162                 set_irn_link(block, info);
163         }
164
165         return info;
166 }
167
168 /**
169  * Get default register requirement for the current register class
170  */
171 static const arch_register_req_t *get_default_req_current_cls(void)
172 {
173         if (default_cls_req == NULL) {
174                 struct obstack      *obst = get_irg_obstack(irg);
175                 arch_register_req_t *req  = OALLOCZ(obst, arch_register_req_t);
176
177                 req->type = arch_register_req_type_normal;
178                 req->cls  = cls;
179
180                 default_cls_req = req;
181         }
182         return default_cls_req;
183 }
184
185 /**
186  * Link the allocation info of a node to a copy.
187  * Afterwards, both nodes uses the same allocation info.
188  * Copy must not have an allocation info assigned yet.
189  *
190  * @param copy   the node that gets the allocation info assigned
191  * @param value  the original node
192  */
193 static void mark_as_copy_of(ir_node *copy, ir_node *value)
194 {
195         ir_node           *original;
196         allocation_info_t *info      = get_allocation_info(value);
197         allocation_info_t *copy_info = get_allocation_info(copy);
198
199         /* find original value */
200         original = info->original_value;
201         if (original != value) {
202                 info = get_allocation_info(original);
203         }
204
205         assert(info->original_value == original);
206         info->current_value = copy;
207
208         /* the copy should not be linked to something else yet */
209         assert(copy_info->original_value == copy);
210         copy_info->original_value = original;
211
212         /* copy over allocation preferences */
213         memcpy(copy_info->prefs, info->prefs, n_regs * sizeof(copy_info->prefs[0]));
214 }
215
216 /**
217  * Calculate the penalties for every register on a node and its live neighbors.
218  *
219  * @param live_nodes  the set of live nodes at the current position, may be NULL
220  * @param penalty     the penalty to subtract from
221  * @param limited     a raw bitset containing the limited set for the node
222  * @param node        the node
223  */
224 static void give_penalties_for_limits(const ir_nodeset_t *live_nodes,
225                                       float penalty, const unsigned* limited,
226                                       ir_node *node)
227 {
228         ir_nodeset_iterator_t iter;
229         unsigned              r;
230         unsigned              n_allowed;
231         allocation_info_t     *info = get_allocation_info(node);
232         ir_node               *neighbor;
233
234         /* give penalty for all forbidden regs */
235         for (r = 0; r < n_regs; ++r) {
236                 if (rbitset_is_set(limited, r))
237                         continue;
238
239                 info->prefs[r] -= penalty;
240         }
241
242         /* all other live values should get a penalty for allowed regs */
243         if (live_nodes == NULL)
244                 return;
245
246         penalty   *= NEIGHBOR_FACTOR;
247         n_allowed  = rbitset_popcnt(limited, n_regs);
248         if (n_allowed > 1) {
249                 /* only create a very weak penalty if multiple regs are allowed */
250                 penalty = (penalty * 0.8) / n_allowed;
251         }
252         foreach_ir_nodeset(live_nodes, neighbor, iter) {
253                 allocation_info_t *neighbor_info;
254
255                 /* TODO: if op is used on multiple inputs we might not do a
256                  * continue here */
257                 if (neighbor == node)
258                         continue;
259
260                 neighbor_info = get_allocation_info(neighbor);
261                 for (r = 0; r < n_regs; ++r) {
262                         if (!rbitset_is_set(limited, r))
263                                 continue;
264
265                         neighbor_info->prefs[r] -= penalty;
266                 }
267         }
268 }
269
270 /**
271  * Calculate the preferences of a definition for the current register class.
272  * If the definition uses a limited set of registers, reduce the preferences
273  * for the limited register on the node and its neighbors.
274  *
275  * @param live_nodes  the set of live nodes at the current node
276  * @param weight      the weight
277  * @param node        the current node
278  */
279 static void check_defs(const ir_nodeset_t *live_nodes, float weight,
280                        ir_node *node)
281 {
282         const arch_register_req_t *req;
283
284         if (get_irn_mode(node) == mode_T) {
285                 const ir_edge_t *edge;
286                 foreach_out_edge(node, edge) {
287                         ir_node *proj = get_edge_src_irn(edge);
288                         check_defs(live_nodes, weight, proj);
289                 }
290                 return;
291         }
292
293         if (!arch_irn_consider_in_reg_alloc(cls, node))
294                 return;
295
296         req = arch_get_register_req_out(node);
297         if (req->type & arch_register_req_type_limited) {
298                 const unsigned *limited = req->limited;
299                 float           penalty = weight * DEF_FACTOR;
300                 give_penalties_for_limits(live_nodes, penalty, limited, node);
301         }
302
303         if (req->type & arch_register_req_type_should_be_same) {
304                 ir_node           *insn  = skip_Proj(node);
305                 allocation_info_t *info  = get_allocation_info(node);
306                 int                arity = get_irn_arity(insn);
307                 int                i;
308
309                 float factor = 1.0f / rbitset_popcnt(&req->other_same, arity);
310                 for (i = 0; i < arity; ++i) {
311                         ir_node           *op;
312                         unsigned           r;
313                         allocation_info_t *op_info;
314
315                         if (!rbitset_is_set(&req->other_same, i))
316                                 continue;
317
318                         op = get_irn_n(insn, i);
319
320                         /* if we the value at the should_be_same input doesn't die at the
321                          * node, then it is no use to propagate the constraints (since a
322                          * copy will emerge anyway) */
323                         if (ir_nodeset_contains(live_nodes, op))
324                                 continue;
325
326                         op_info = get_allocation_info(op);
327                         for (r = 0; r < n_regs; ++r) {
328                                 op_info->prefs[r] += info->prefs[r] * factor;
329                         }
330                 }
331         }
332 }
333
334 /**
335  * Walker: Runs an a block calculates the preferences for any
336  * node and every register from the considered register class.
337  */
338 static void analyze_block(ir_node *block, void *data)
339 {
340         float         weight = get_block_execfreq(execfreqs, block);
341         ir_nodeset_t  live_nodes;
342         ir_node      *node;
343         (void) data;
344
345         ir_nodeset_init(&live_nodes);
346         be_liveness_end_of_block(lv, cls, block, &live_nodes);
347
348         sched_foreach_reverse(block, node) {
349                 allocation_info_t *info;
350                 int                i;
351                 int                arity;
352
353                 if (is_Phi(node))
354                         break;
355
356                 check_defs(&live_nodes, weight, node);
357
358                 /* mark last uses */
359                 arity = get_irn_arity(node);
360
361                 /* the allocation info node currently only uses 1 unsigned value
362                    to mark last used inputs. So we will fail for a node with more than
363                    32 inputs. */
364                 if (arity >= (int) sizeof(unsigned) * 8) {
365                         panic("Node with more than %d inputs not supported yet",
366                                         (int) sizeof(unsigned) * 8);
367                 }
368
369                 info = get_allocation_info(node);
370                 for (i = 0; i < arity; ++i) {
371                         ir_node *op = get_irn_n(node, i);
372                         if (!arch_irn_consider_in_reg_alloc(cls, op))
373                                 continue;
374
375                         /* last usage of a value? */
376                         if (!ir_nodeset_contains(&live_nodes, op)) {
377                                 rbitset_set(&info->last_uses, i);
378                         }
379                 }
380
381                 be_liveness_transfer(cls, node, &live_nodes);
382
383                 /* update weights based on usage constraints */
384                 for (i = 0; i < arity; ++i) {
385                         const arch_register_req_t *req;
386                         const unsigned            *limited;
387                         ir_node                   *op = get_irn_n(node, i);
388
389                         if (!arch_irn_consider_in_reg_alloc(cls, op))
390                                 continue;
391
392                         req = arch_get_register_req(node, i);
393                         if (!(req->type & arch_register_req_type_limited))
394                                 continue;
395
396                         limited = req->limited;
397                         give_penalties_for_limits(&live_nodes, weight * USE_FACTOR, limited,
398                                                   op);
399                 }
400         }
401
402         ir_nodeset_destroy(&live_nodes);
403 }
404
405 static void congruence_def(ir_nodeset_t *live_nodes, ir_node *node)
406 {
407         const arch_register_req_t *req;
408
409         if (get_irn_mode(node) == mode_T) {
410                 const ir_edge_t *edge;
411                 foreach_out_edge(node, edge) {
412                         ir_node *def = get_edge_src_irn(edge);
413                         congruence_def(live_nodes, def);
414                 }
415                 return;
416         }
417
418         if (!arch_irn_consider_in_reg_alloc(cls, node))
419                 return;
420
421         /* should be same constraint? */
422         req = arch_get_register_req_out(node);
423         if (req->type & arch_register_req_type_should_be_same) {
424                 ir_node *insn  = skip_Proj(node);
425                 int      arity = get_irn_arity(insn);
426                 int      i;
427                 unsigned node_idx = get_irn_idx(node);
428                 node_idx          = uf_find(congruence_classes, node_idx);
429
430                 for (i = 0; i < arity; ++i) {
431                         ir_node               *live;
432                         ir_node               *op;
433                         int                    op_idx;
434                         ir_nodeset_iterator_t  iter;
435                         bool                   interferes = false;
436
437                         if (!rbitset_is_set(&req->other_same, i))
438                                 continue;
439
440                         op     = get_irn_n(insn, i);
441                         op_idx = get_irn_idx(op);
442                         op_idx = uf_find(congruence_classes, op_idx);
443
444                         /* do we interfere with the value */
445                         foreach_ir_nodeset(live_nodes, live, iter) {
446                                 int lv_idx = get_irn_idx(live);
447                                 lv_idx     = uf_find(congruence_classes, lv_idx);
448                                 if (lv_idx == op_idx) {
449                                         interferes = true;
450                                         break;
451                                 }
452                         }
453                         /* don't put in same affinity class if we interfere */
454                         if (interferes)
455                                 continue;
456
457                         node_idx = uf_union(congruence_classes, node_idx, op_idx);
458                         DB((dbg, LEVEL_3, "Merge %+F and %+F congruence classes\n",
459                             node, op));
460                         /* one should_be_same is enough... */
461                         break;
462                 }
463         }
464 }
465
466 static void create_congurence_class(ir_node *block, void *data)
467 {
468         ir_nodeset_t  live_nodes;
469         ir_node      *node;
470
471         (void) data;
472         ir_nodeset_init(&live_nodes);
473         be_liveness_end_of_block(lv, cls, block, &live_nodes);
474
475         /* check should be same constraints */
476         sched_foreach_reverse(block, node) {
477                 if (is_Phi(node))
478                         break;
479
480                 congruence_def(&live_nodes, node);
481                 be_liveness_transfer(cls, node, &live_nodes);
482         }
483
484         /* check phi congruence classes */
485         sched_foreach_reverse_from(node, node) {
486                 int i;
487                 int arity;
488                 int node_idx;
489                 assert(is_Phi(node));
490
491                 if (!arch_irn_consider_in_reg_alloc(cls, node))
492                         continue;
493
494                 node_idx = get_irn_idx(node);
495                 node_idx = uf_find(congruence_classes, node_idx);
496
497                 arity = get_irn_arity(node);
498                 for (i = 0; i < arity; ++i) {
499                         bool                  interferes = false;
500                         ir_nodeset_iterator_t iter;
501                         ir_node *live;
502                         ir_node *phi;
503                         ir_node *op     = get_Phi_pred(node, i);
504                         int      op_idx = get_irn_idx(op);
505                         op_idx = uf_find(congruence_classes, op_idx);
506
507                         /* do we interfere with the value */
508                         foreach_ir_nodeset(&live_nodes, live, iter) {
509                                 int lv_idx = get_irn_idx(live);
510                                 lv_idx     = uf_find(congruence_classes, lv_idx);
511                                 if (lv_idx == op_idx) {
512                                         interferes = true;
513                                         break;
514                                 }
515                         }
516                         /* don't put in same affinity class if we interfere */
517                         if (interferes)
518                                 continue;
519                         /* any other phi has the same input? */
520                         sched_foreach(block, phi) {
521                                 ir_node *oop;
522                                 int      oop_idx;
523                                 if (!is_Phi(phi))
524                                         break;
525                                 if (!arch_irn_consider_in_reg_alloc(cls, phi))
526                                         continue;
527                                 oop = get_Phi_pred(phi, i);
528                                 if (oop == op)
529                                         continue;
530                                 oop_idx = get_irn_idx(oop);
531                                 oop_idx = uf_find(congruence_classes, oop_idx);
532                                 if (oop_idx == op_idx) {
533                                         interferes = true;
534                                         break;
535                                 }
536                         }
537                         if (interferes)
538                                 continue;
539
540                         node_idx = uf_union(congruence_classes, node_idx, op_idx);
541                         DB((dbg, LEVEL_3, "Merge %+F and %+F congruence classes\n",
542                             node, op));
543                 }
544         }
545 }
546
547 static void merge_congruence_prefs(ir_node *node, void *data)
548 {
549         allocation_info_t *info;
550         allocation_info_t *head_info;
551         unsigned node_idx = get_irn_idx(node);
552         unsigned node_set = uf_find(congruence_classes, node_idx);
553         unsigned r;
554
555         (void) data;
556
557         /* head of congruence class or not in any class */
558         if (node_set == node_idx)
559                 return;
560
561         if (!arch_irn_consider_in_reg_alloc(cls, node))
562                 return;
563
564         head_info = get_allocation_info(get_idx_irn(irg, node_set));
565         info      = get_allocation_info(node);
566
567         for (r = 0; r < n_regs; ++r) {
568                 head_info->prefs[r] += info->prefs[r];
569         }
570 }
571
572 static void set_congruence_prefs(ir_node *node, void *data)
573 {
574         allocation_info_t *info;
575         allocation_info_t *head_info;
576         unsigned node_idx = get_irn_idx(node);
577         unsigned node_set = uf_find(congruence_classes, node_idx);
578
579         (void) data;
580
581         /* head of congruence class or not in any class */
582         if (node_set == node_idx)
583                 return;
584
585         if (!arch_irn_consider_in_reg_alloc(cls, node))
586                 return;
587
588         head_info = get_allocation_info(get_idx_irn(irg, node_set));
589         info      = get_allocation_info(node);
590
591         memcpy(info->prefs, head_info->prefs, n_regs * sizeof(info->prefs[0]));
592 }
593
594 static void combine_congruence_classes(void)
595 {
596         size_t n = get_irg_last_idx(irg);
597         congruence_classes = XMALLOCN(int, n);
598         uf_init(congruence_classes, n);
599
600         /* create congruence classes */
601         irg_block_walk_graph(irg, create_congurence_class, NULL, NULL);
602         /* merge preferences */
603         irg_walk_graph(irg, merge_congruence_prefs, NULL, NULL);
604         irg_walk_graph(irg, set_congruence_prefs, NULL, NULL);
605 }
606
607
608
609
610
611 /**
612  * Assign register reg to the given node.
613  *
614  * @param node  the node
615  * @param reg   the register
616  */
617 static void use_reg(ir_node *node, const arch_register_t *reg)
618 {
619         unsigned r = arch_register_get_index(reg);
620         assignments[r] = node;
621         arch_set_irn_register(node, reg);
622 }
623
624 static void free_reg_of_value(ir_node *node)
625 {
626         const arch_register_t *reg;
627         unsigned               r;
628
629         if (!arch_irn_consider_in_reg_alloc(cls, node))
630                 return;
631
632         reg        = arch_get_irn_register(node);
633         r          = arch_register_get_index(reg);
634         /* assignment->value may be NULL if a value is used at 2 inputs
635            so it gets freed twice. */
636         assert(assignments[r] == node || assignments[r] == NULL);
637         assignments[r] = NULL;
638 }
639
640 /**
641  * Compare two register preferences in decreasing order.
642  */
643 static int compare_reg_pref(const void *e1, const void *e2)
644 {
645         const reg_pref_t *rp1 = (const reg_pref_t*) e1;
646         const reg_pref_t *rp2 = (const reg_pref_t*) e2;
647         if (rp1->pref < rp2->pref)
648                 return 1;
649         if (rp1->pref > rp2->pref)
650                 return -1;
651         return 0;
652 }
653
654 static void fill_sort_candidates(reg_pref_t *regprefs,
655                                  const allocation_info_t *info)
656 {
657         unsigned r;
658
659         for (r = 0; r < n_regs; ++r) {
660                 float pref = info->prefs[r];
661                 regprefs[r].num  = r;
662                 regprefs[r].pref = pref;
663         }
664         /* TODO: use a stable sort here to avoid unnecessary register jumping */
665         qsort(regprefs, n_regs, sizeof(regprefs[0]), compare_reg_pref);
666 }
667
668 static bool try_optimistic_split(ir_node *to_split, ir_node *before,
669                                  float pref, float pref_delta,
670                                  unsigned *output_regs)
671 {
672         const arch_register_t *reg;
673         ir_node               *original_insn;
674         ir_node               *block;
675         ir_node               *copy;
676         unsigned               r;
677         unsigned               i;
678         allocation_info_t     *info = get_allocation_info(to_split);
679         reg_pref_t            *prefs;
680         float                  delta;
681         float                  split_threshold;
682
683         (void) pref;
684
685         /* stupid hack: don't optimisticallt split don't spill nodes...
686          * (so we don't split away the values produced because of
687          *  must_be_different constraints) */
688         original_insn = skip_Proj(info->original_value);
689         if (arch_irn_get_flags(original_insn) & arch_irn_flags_dont_spill)
690                 return false;
691
692         /* find the best free position where we could move to */
693         prefs = ALLOCAN(reg_pref_t, n_regs);
694         fill_sort_candidates(prefs, info);
695         for (i = 0; i < n_regs; ++i) {
696                 r = prefs[i].num;
697                 if (!rbitset_is_set(normal_regs, r))
698                         continue;
699                 if (rbitset_is_set(output_regs, r))
700                         continue;
701                 if (assignments[r] == NULL)
702                         break;
703         }
704         if (i >= n_regs) {
705                 return false;
706         }
707         /* TODO: use execfreq somehow... */
708         block = get_nodes_block(before);
709         delta = pref_delta + prefs[i].pref;
710         split_threshold = get_block_execfreq(execfreqs, block) * SPLIT_DELTA;
711         if (delta < split_threshold) {
712                 DB((dbg, LEVEL_3, "Not doing optimistical split, win %f too low\n",
713                     delta));
714                 return false;
715         }
716
717         reg   = arch_register_for_index(cls, r);
718         copy = be_new_Copy(cls, block, to_split);
719         mark_as_copy_of(copy, to_split);
720         free_reg_of_value(to_split);
721         use_reg(copy, reg);
722         sched_add_before(before, copy);
723
724         DB((dbg, LEVEL_3,
725             "Optimistic live-range split %+F move %+F -> %s before %+F (win %f)\n",
726             copy, to_split, reg->name, before, delta));
727         return true;
728 }
729
730 /**
731  * Determine and assign a register for node @p node
732  */
733 static void assign_reg(const ir_node *block, ir_node *node,
734                        unsigned *output_regs)
735 {
736         const arch_register_t     *reg;
737         allocation_info_t         *info;
738         const arch_register_req_t *req;
739         reg_pref_t                *reg_prefs;
740         ir_node                   *in_node;
741         unsigned                   i;
742         const unsigned            *allowed_regs;
743         unsigned                   r;
744
745         assert(arch_irn_consider_in_reg_alloc(cls, node));
746
747         /* preassigned register? */
748         reg = arch_get_irn_register(node);
749         if (reg != NULL) {
750                 DB((dbg, LEVEL_2, "Preassignment %+F -> %s\n", node, reg->name));
751                 use_reg(node, reg);
752                 return;
753         }
754
755         /* give should_be_same boni */
756         info = get_allocation_info(node);
757         req  = arch_get_register_req_out(node);
758
759         in_node = skip_Proj(node);
760         if (req->type & arch_register_req_type_should_be_same) {
761                 float weight = get_block_execfreq(execfreqs, block);
762                 int   arity  = get_irn_arity(in_node);
763                 int   i;
764
765                 assert(arity <= (int) sizeof(req->other_same) * 8);
766                 for (i = 0; i < arity; ++i) {
767                         ir_node               *in;
768                         const arch_register_t *reg;
769                         unsigned               r;
770                         if (!rbitset_is_set(&req->other_same, i))
771                                 continue;
772
773                         in  = get_irn_n(in_node, i);
774                         reg = arch_get_irn_register(in);
775                         assert(reg != NULL);
776                         r = arch_register_get_index(reg);
777
778                         /* if the value didn't die here then we should not propagate the
779                          * should_be_same info */
780                         if (assignments[r] == in)
781                                 continue;
782
783                         info->prefs[r] += weight * AFF_SHOULD_BE_SAME;
784                 }
785         }
786
787         /* create list of register candidates and sort by their preference */
788         DB((dbg, LEVEL_2, "Candidates for %+F:", node));
789         reg_prefs = alloca(n_regs * sizeof(reg_prefs[0]));
790         fill_sort_candidates(reg_prefs, info);
791         for (i = 0; i < n_regs; ++i) {
792                 unsigned num = reg_prefs[i].num;
793                 const arch_register_t *reg;
794
795                 if (!rbitset_is_set(normal_regs, num))
796                         continue;
797
798                 reg = arch_register_for_index(cls, num);
799                 DB((dbg, LEVEL_2, " %s(%f)", reg->name, reg_prefs[i].pref));
800         }
801         DB((dbg, LEVEL_2, "\n"));
802
803         allowed_regs = normal_regs;
804         if (req->type & arch_register_req_type_limited) {
805                 allowed_regs = req->limited;
806         }
807
808         for (i = 0; i < n_regs; ++i) {
809                 r = reg_prefs[i].num;
810                 if (!rbitset_is_set(allowed_regs, r))
811                         continue;
812                 if (assignments[r] == NULL)
813                         break;
814                 if (!is_Phi(node)) {
815                         float    pref   = reg_prefs[i].pref;
816                         float    delta  = i+1 < n_regs ? pref - reg_prefs[i+1].pref : 0;
817                         ir_node *before = skip_Proj(node);
818                         bool     res    = try_optimistic_split(assignments[r], before,
819                                                                pref, delta,
820                                                                output_regs);
821                         if (res)
822                                 break;
823                 }
824         }
825         if (i >= n_regs) {
826                 panic("No register left for %+F\n", node);
827         }
828
829         reg = arch_register_for_index(cls, r);
830         DB((dbg, LEVEL_2, "Assign %+F -> %s\n", node, reg->name));
831         use_reg(node, reg);
832 }
833
834 /**
835  * Add an permutation in front of a node and change the assignments
836  * due to this permutation.
837  *
838  * To understand this imagine a permutation like this:
839  *
840  * 1 -> 2
841  * 2 -> 3
842  * 3 -> 1, 5
843  * 4 -> 6
844  * 5
845  * 6
846  * 7 -> 7
847  *
848  * First we count how many destinations a single value has. At the same time
849  * we can be sure that each destination register has at most 1 source register
850  * (it can have 0 which means we don't care what value is in it).
851  * We ignore all fullfilled permuations (like 7->7)
852  * In a first pass we create as much copy instructions as possible as they
853  * are generally cheaper than exchanges. We do this by counting into how many
854  * destinations a register has to be copied (in the example it's 2 for register
855  * 3, or 1 for the registers 1,2,4 and 7).
856  * We can then create a copy into every destination register when the usecount
857  * of that register is 0 (= noone else needs the value in the register).
858  *
859  * After this step we should have cycles left. We implement a cyclic permutation
860  * of n registers with n-1 transpositions.
861  *
862  * @param live_nodes   the set of live nodes, updated due to live range split
863  * @param before       the node before we add the permutation
864  * @param permutation  the permutation array indices are the destination
865  *                     registers, the values in the array are the source
866  *                     registers.
867  */
868 static void permute_values(ir_nodeset_t *live_nodes, ir_node *before,
869                              unsigned *permutation)
870 {
871         unsigned  *n_used = ALLOCANZ(unsigned, n_regs);
872         ir_node   *block;
873         unsigned   r;
874
875         /* determine how often each source register needs to be read */
876         for (r = 0; r < n_regs; ++r) {
877                 unsigned  old_reg = permutation[r];
878                 ir_node  *value;
879
880                 value = assignments[old_reg];
881                 if (value == NULL) {
882                         /* nothing to do here, reg is not live. Mark it as fixpoint
883                          * so we ignore it in the next steps */
884                         permutation[r] = r;
885                         continue;
886                 }
887
888                 ++n_used[old_reg];
889         }
890
891         block = get_nodes_block(before);
892
893         /* step1: create copies where immediately possible */
894         for (r = 0; r < n_regs; /* empty */) {
895                 ir_node *copy;
896                 ir_node *src;
897                 const arch_register_t *reg;
898                 unsigned               old_r = permutation[r];
899
900                 /* - no need to do anything for fixed points.
901                    - we can't copy if the value in the dest reg is still needed */
902                 if (old_r == r || n_used[r] > 0) {
903                         ++r;
904                         continue;
905                 }
906
907                 /* create a copy */
908                 src  = assignments[old_r];
909                 copy = be_new_Copy(cls, block, src);
910                 sched_add_before(before, copy);
911                 reg = arch_register_for_index(cls, r);
912                 DB((dbg, LEVEL_2, "Copy %+F (from %+F, before %+F) -> %s\n",
913                     copy, src, before, reg->name));
914                 mark_as_copy_of(copy, src);
915                 use_reg(copy, reg);
916
917                 if (live_nodes != NULL) {
918                         ir_nodeset_insert(live_nodes, copy);
919                 }
920
921                 /* old register has 1 user less, permutation is resolved */
922                 assert(arch_register_get_index(arch_get_irn_register(src)) == old_r);
923                 permutation[r] = r;
924
925                 assert(n_used[old_r] > 0);
926                 --n_used[old_r];
927                 if (n_used[old_r] == 0) {
928                         if (live_nodes != NULL) {
929                                 ir_nodeset_remove(live_nodes, src);
930                         }
931                         free_reg_of_value(src);
932                 }
933
934                 /* advance or jump back (if this copy enabled another copy) */
935                 if (old_r < r && n_used[old_r] == 0) {
936                         r = old_r;
937                 } else {
938                         ++r;
939                 }
940         }
941
942         /* at this point we only have "cycles" left which we have to resolve with
943          * perm instructions
944          * TODO: if we have free registers left, then we should really use copy
945          * instructions for any cycle longer than 2 registers...
946          * (this is probably architecture dependent, there might be archs where
947          *  copies are preferable even for 2-cycles) */
948
949         /* create perms with the rest */
950         for (r = 0; r < n_regs; /* empty */) {
951                 const arch_register_t *reg;
952                 unsigned  old_r = permutation[r];
953                 unsigned  r2;
954                 ir_node  *in[2];
955                 ir_node  *perm;
956                 ir_node  *proj0;
957                 ir_node  *proj1;
958
959                 if (old_r == r) {
960                         ++r;
961                         continue;
962                 }
963
964                 /* we shouldn't have copies from 1 value to multiple destinations left*/
965                 assert(n_used[old_r] == 1);
966
967                 /* exchange old_r and r2; after that old_r is a fixed point */
968                 r2 = permutation[old_r];
969
970                 in[0] = assignments[r2];
971                 in[1] = assignments[old_r];
972                 perm = be_new_Perm(cls, block, 2, in);
973                 sched_add_before(before, perm);
974                 DB((dbg, LEVEL_2, "Perm %+F (perm %+F,%+F, before %+F)\n",
975                     perm, in[0], in[1], before));
976
977                 proj0 = new_r_Proj(block, perm, get_irn_mode(in[0]), 0);
978                 mark_as_copy_of(proj0, in[0]);
979                 reg = arch_register_for_index(cls, old_r);
980                 use_reg(proj0, reg);
981
982                 proj1 = new_r_Proj(block, perm, get_irn_mode(in[1]), 1);
983                 mark_as_copy_of(proj1, in[1]);
984                 reg = arch_register_for_index(cls, r2);
985                 use_reg(proj1, reg);
986
987                 /* 1 value is now in the correct register */
988                 permutation[old_r] = old_r;
989                 /* the source of r changed to r2 */
990                 permutation[r] = r2;
991
992                 /* if we have reached a fixpoint update data structures */
993                 if (live_nodes != NULL) {
994                         ir_nodeset_remove(live_nodes, in[0]);
995                         ir_nodeset_remove(live_nodes, in[1]);
996                         ir_nodeset_remove(live_nodes, proj0);
997                         ir_nodeset_insert(live_nodes, proj1);
998                 }
999         }
1000
1001 #ifdef DEBUG_libfirm
1002         /* now we should only have fixpoints left */
1003         for (r = 0; r < n_regs; ++r) {
1004                 assert(permutation[r] == r);
1005         }
1006 #endif
1007 }
1008
1009 /**
1010  * Free regs for values last used.
1011  *
1012  * @param live_nodes   set of live nodes, will be updated
1013  * @param node         the node to consider
1014  */
1015 static void free_last_uses(ir_nodeset_t *live_nodes, ir_node *node)
1016 {
1017         allocation_info_t     *info      = get_allocation_info(node);
1018         const unsigned        *last_uses = &info->last_uses;
1019         int                    arity     = get_irn_arity(node);
1020         int                    i;
1021
1022         for (i = 0; i < arity; ++i) {
1023                 ir_node *op;
1024
1025                 /* check if one operand is the last use */
1026                 if (!rbitset_is_set(last_uses, i))
1027                         continue;
1028
1029                 op = get_irn_n(node, i);
1030                 free_reg_of_value(op);
1031                 ir_nodeset_remove(live_nodes, op);
1032         }
1033 }
1034
1035 /**
1036  * change inputs of a node to the current value (copies/perms)
1037  */
1038 static void rewire_inputs(ir_node *node)
1039 {
1040         int i;
1041         int arity = get_irn_arity(node);
1042
1043         for (i = 0; i < arity; ++i) {
1044                 ir_node           *op = get_irn_n(node, i);
1045                 allocation_info_t *info;
1046
1047                 if (!arch_irn_consider_in_reg_alloc(cls, op))
1048                         continue;
1049
1050                 info = get_allocation_info(op);
1051                 info = get_allocation_info(info->original_value);
1052                 if (info->current_value != op) {
1053                         set_irn_n(node, i, info->current_value);
1054                 }
1055         }
1056 }
1057
1058 /**
1059  * Create a bitset of registers occupied with value living through an
1060  * instruction
1061  */
1062 static void determine_live_through_regs(unsigned *bitset, ir_node *node)
1063 {
1064         const allocation_info_t *info = get_allocation_info(node);
1065         unsigned r;
1066         int i;
1067         int arity;
1068
1069         /* mark all used registers as potentially live-through */
1070         for (r = 0; r < n_regs; ++r) {
1071                 if (assignments[r] == NULL)
1072                         continue;
1073                 if (!rbitset_is_set(normal_regs, r))
1074                         continue;
1075
1076                 rbitset_set(bitset, r);
1077         }
1078
1079         /* remove registers of value dying at the instruction */
1080         arity = get_irn_arity(node);
1081         for (i = 0; i < arity; ++i) {
1082                 ir_node               *op;
1083                 const arch_register_t *reg;
1084
1085                 if (!rbitset_is_set(&info->last_uses, i))
1086                         continue;
1087
1088                 op  = get_irn_n(node, i);
1089                 reg = arch_get_irn_register(op);
1090                 rbitset_clear(bitset, arch_register_get_index(reg));
1091         }
1092 }
1093
1094 /**
1095  * Enforce constraints at a node by live range splits.
1096  *
1097  * @param  live_nodes  the set of live nodes, might be changed
1098  * @param  node        the current node
1099  */
1100 static void enforce_constraints(ir_nodeset_t *live_nodes, ir_node *node,
1101                                 unsigned *output_regs)
1102 {
1103         int arity = get_irn_arity(node);
1104         int i, res;
1105         hungarian_problem_t *bp;
1106         unsigned l, r;
1107         unsigned *assignment;
1108
1109         /* construct a list of register occupied by live-through values */
1110         unsigned *live_through_regs = NULL;
1111
1112         /* see if any use constraints are not met */
1113         bool good = true;
1114         for (i = 0; i < arity; ++i) {
1115                 ir_node                   *op = get_irn_n(node, i);
1116                 const arch_register_t     *reg;
1117                 const arch_register_req_t *req;
1118                 const unsigned            *limited;
1119                 unsigned                  r;
1120
1121                 if (!arch_irn_consider_in_reg_alloc(cls, op))
1122                         continue;
1123
1124                 /* are there any limitations for the i'th operand? */
1125                 req = arch_get_register_req(node, i);
1126                 if (!(req->type & arch_register_req_type_limited))
1127                         continue;
1128
1129                 limited = req->limited;
1130                 reg     = arch_get_irn_register(op);
1131                 r       = arch_register_get_index(reg);
1132                 if (!rbitset_is_set(limited, r)) {
1133                         /* found an assignment outside the limited set */
1134                         good = false;
1135                         break;
1136                 }
1137         }
1138
1139         /* is any of the live-throughs using a constrained output register? */
1140         if (get_irn_mode(node) == mode_T) {
1141                 const ir_edge_t *edge;
1142
1143                 foreach_out_edge(node, edge) {
1144                         ir_node *proj = get_edge_src_irn(edge);
1145                         const arch_register_req_t *req;
1146
1147                         if (!arch_irn_consider_in_reg_alloc(cls, proj))
1148                                 continue;
1149
1150                         req = arch_get_register_req_out(proj);
1151                         if (!(req->type & arch_register_req_type_limited))
1152                                 continue;
1153
1154                         if (live_through_regs == NULL) {
1155                                 rbitset_alloca(live_through_regs, n_regs);
1156                                 determine_live_through_regs(live_through_regs, node);
1157                         }
1158
1159                         rbitset_or(output_regs, req->limited, n_regs);
1160                         if (rbitsets_have_common(req->limited, live_through_regs, n_regs)) {
1161                                 good = false;
1162                         }
1163                 }
1164         } else {
1165                 if (arch_irn_consider_in_reg_alloc(cls, node)) {
1166                         const arch_register_req_t *req = arch_get_register_req_out(node);
1167                         if (req->type & arch_register_req_type_limited) {
1168                                 rbitset_alloca(live_through_regs, n_regs);
1169                                 determine_live_through_regs(live_through_regs, node);
1170                                 if (rbitsets_have_common(req->limited, live_through_regs, n_regs)) {
1171                                         good = false;
1172                                         rbitset_or(output_regs, req->limited, n_regs);
1173                                 }
1174                         }
1175                 }
1176         }
1177
1178         if (good)
1179                 return;
1180
1181         /* create these arrays if we haven't yet */
1182         if (live_through_regs == NULL) {
1183                 rbitset_alloca(live_through_regs, n_regs);
1184         }
1185
1186         /* at this point we have to construct a bipartite matching problem to see
1187          * which values should go to which registers
1188          * Note: We're building the matrix in "reverse" - source registers are
1189          *       right, destinations left because this will produce the solution
1190          *       in the format required for permute_values.
1191          */
1192         bp = hungarian_new(n_regs, n_regs, HUNGARIAN_MATCH_PERFECT);
1193
1194         /* add all combinations, then remove not allowed ones */
1195         for (l = 0; l < n_regs; ++l) {
1196                 if (!rbitset_is_set(normal_regs, l)) {
1197                         hungarian_add(bp, l, l, 1);
1198                         continue;
1199                 }
1200
1201                 for (r = 0; r < n_regs; ++r) {
1202                         if (!rbitset_is_set(normal_regs, r))
1203                                 continue;
1204                         /* livethrough values may not use constrainted output registers */
1205                         if (rbitset_is_set(live_through_regs, l)
1206                                         && rbitset_is_set(output_regs, r))
1207                                 continue;
1208
1209                         hungarian_add(bp, r, l, l == r ? 9 : 8);
1210                 }
1211         }
1212
1213         for (i = 0; i < arity; ++i) {
1214                 ir_node                   *op = get_irn_n(node, i);
1215                 const arch_register_t     *reg;
1216                 const arch_register_req_t *req;
1217                 const unsigned            *limited;
1218                 unsigned                   current_reg;
1219
1220                 if (!arch_irn_consider_in_reg_alloc(cls, op))
1221                         continue;
1222
1223                 req = arch_get_register_req(node, i);
1224                 if (!(req->type & arch_register_req_type_limited))
1225                         continue;
1226
1227                 limited     = req->limited;
1228                 reg         = arch_get_irn_register(op);
1229                 current_reg = arch_register_get_index(reg);
1230                 for (r = 0; r < n_regs; ++r) {
1231                         if (rbitset_is_set(limited, r))
1232                                 continue;
1233                         hungarian_remv(bp, r, current_reg);
1234                 }
1235         }
1236
1237         //hungarian_print_cost_matrix(bp, 1);
1238         hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
1239
1240         assignment = ALLOCAN(unsigned, n_regs);
1241         res = hungarian_solve(bp, (int*) assignment, NULL, 0);
1242         assert(res == 0);
1243
1244 #if 0
1245         fprintf(stderr, "Swap result:");
1246         for (i = 0; i < (int) n_regs; ++i) {
1247                 fprintf(stderr, " %d", assignment[i]);
1248         }
1249         fprintf(stderr, "\n");
1250 #endif
1251
1252         hungarian_free(bp);
1253
1254         permute_values(live_nodes, node, assignment);
1255 }
1256
1257 /** test wether a node @p n is a copy of the value of node @p of */
1258 static bool is_copy_of(ir_node *value, ir_node *test_value)
1259 {
1260         allocation_info_t *test_info;
1261         allocation_info_t *info;
1262
1263         if (value == test_value)
1264                 return true;
1265
1266         info      = get_allocation_info(value);
1267         test_info = get_allocation_info(test_value);
1268         return test_info->original_value == info->original_value;
1269 }
1270
1271 /**
1272  * find a value in the end-assignment of a basic block
1273  * @returns the index into the assignment array if found
1274  *          -1 if not found
1275  */
1276 static int find_value_in_block_info(block_info_t *info, ir_node *value)
1277 {
1278         unsigned   r;
1279         ir_node  **assignments = info->assignments;
1280         for (r = 0; r < n_regs; ++r) {
1281                 ir_node *a_value = assignments[r];
1282
1283                 if (a_value == NULL)
1284                         continue;
1285                 if (is_copy_of(a_value, value))
1286                         return (int) r;
1287         }
1288
1289         return -1;
1290 }
1291
1292 /**
1293  * Create the necessary permutations at the end of a basic block to fullfill
1294  * the register assignment for phi-nodes in the next block
1295  */
1296 static void add_phi_permutations(ir_node *block, int p)
1297 {
1298         unsigned   r;
1299         unsigned  *permutation;
1300         ir_node  **old_assignments;
1301         bool       need_permutation;
1302         ir_node   *node;
1303         ir_node   *pred = get_Block_cfgpred_block(block, p);
1304
1305         block_info_t *pred_info = get_block_info(pred);
1306
1307         /* predecessor not processed yet? nothing to do */
1308         if (!pred_info->processed)
1309                 return;
1310
1311         permutation = ALLOCAN(unsigned, n_regs);
1312         for (r = 0; r < n_regs; ++r) {
1313                 permutation[r] = r;
1314         }
1315
1316         /* check phi nodes */
1317         need_permutation = false;
1318         node = sched_first(block);
1319         for ( ; is_Phi(node); node = sched_next(node)) {
1320                 const arch_register_t *reg;
1321                 int                    regn;
1322                 int                    a;
1323                 ir_node               *op;
1324
1325                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1326                         continue;
1327
1328                 op = get_Phi_pred(node, p);
1329                 if (!arch_irn_consider_in_reg_alloc(cls, op))
1330                         continue;
1331
1332                 a  = find_value_in_block_info(pred_info, op);
1333                 assert(a >= 0);
1334
1335                 reg  = arch_get_irn_register(node);
1336                 regn = arch_register_get_index(reg);
1337                 if (regn != a) {
1338                         permutation[regn] = a;
1339                         need_permutation  = true;
1340                 }
1341         }
1342
1343         if (need_permutation) {
1344                 /* permute values at end of predecessor */
1345                 old_assignments = assignments;
1346                 assignments     = pred_info->assignments;
1347                 permute_values(NULL, be_get_end_of_block_insertion_point(pred),
1348                                                  permutation);
1349                 assignments = old_assignments;
1350         }
1351
1352         /* change phi nodes to use the copied values */
1353         node = sched_first(block);
1354         for ( ; is_Phi(node); node = sched_next(node)) {
1355                 int      a;
1356                 ir_node *op;
1357
1358                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1359                         continue;
1360
1361                 op = get_Phi_pred(node, p);
1362                 /* no need to do anything for Unknown inputs */
1363                 if (!arch_irn_consider_in_reg_alloc(cls, op))
1364                         continue;
1365
1366                 /* we have permuted all values into the correct registers so we can
1367                    simply query which value occupies the phis register in the
1368                    predecessor */
1369                 a  = arch_register_get_index(arch_get_irn_register(node));
1370                 op = pred_info->assignments[a];
1371                 set_Phi_pred(node, p, op);
1372         }
1373 }
1374
1375 /**
1376  * Set preferences for a phis register based on the registers used on the
1377  * phi inputs.
1378  */
1379 static void adapt_phi_prefs(ir_node *phi)
1380 {
1381         int i;
1382         int arity = get_irn_arity(phi);
1383         ir_node           *block = get_nodes_block(phi);
1384         allocation_info_t *info  = get_allocation_info(phi);
1385
1386         for (i = 0; i < arity; ++i) {
1387                 ir_node               *op  = get_irn_n(phi, i);
1388                 const arch_register_t *reg = arch_get_irn_register(op);
1389                 ir_node               *pred_block;
1390                 block_info_t          *pred_block_info;
1391                 float                  weight;
1392                 unsigned               r;
1393
1394                 if (reg == NULL)
1395                         continue;
1396                 /* we only give the bonus if the predecessor already has registers
1397                  * assigned, otherwise we only see a dummy value
1398                  * and any conclusions about its register are useless */
1399                 pred_block = get_Block_cfgpred_block(block, i);
1400                 pred_block_info = get_block_info(pred_block);
1401                 if (!pred_block_info->processed)
1402                         continue;
1403
1404                 /* give bonus for already assigned register */
1405                 weight = get_block_execfreq(execfreqs, pred_block);
1406                 r      = arch_register_get_index(reg);
1407                 info->prefs[r] += weight * AFF_PHI;
1408         }
1409 }
1410
1411 /**
1412  * After a phi has been assigned a register propagate preference inputs
1413  * to the phi inputs.
1414  */
1415 static void propagate_phi_register(ir_node *phi, unsigned assigned_r)
1416 {
1417         int                    i;
1418         ir_node               *block = get_nodes_block(phi);
1419         int                    arity = get_irn_arity(phi);
1420
1421         for (i = 0; i < arity; ++i) {
1422                 ir_node           *op         = get_Phi_pred(phi, i);
1423                 allocation_info_t *info       = get_allocation_info(op);
1424                 ir_node           *pred_block = get_Block_cfgpred_block(block, i);
1425                 unsigned           r;
1426                 float              weight
1427                         = get_block_execfreq(execfreqs, pred_block) * AFF_PHI;
1428
1429                 if (info->prefs[assigned_r] >= weight)
1430                         continue;
1431
1432                 /* promote the prefered register */
1433                 for (r = 0; r < n_regs; ++r) {
1434                         if (r == assigned_r) {
1435                                 info->prefs[r] = AFF_PHI * weight;
1436                         } else {
1437                                 info->prefs[r] -= AFF_PHI * weight;
1438                         }
1439                 }
1440
1441                 if (is_Phi(op))
1442                         propagate_phi_register(op, assigned_r);
1443         }
1444 }
1445
1446 static void assign_phi_registers(ir_node *block)
1447 {
1448         int                  n_phis = 0;
1449         int                  n;
1450         int                  res;
1451         int                 *assignment;
1452         ir_node             *node;
1453         hungarian_problem_t *bp;
1454
1455         /* count phi nodes */
1456         sched_foreach(block, node) {
1457                 if (!is_Phi(node))
1458                         break;
1459                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1460                         continue;
1461                 ++n_phis;
1462         }
1463
1464         if (n_phis == 0)
1465                 return;
1466
1467         /* build a bipartite matching problem for all phi nodes */
1468         bp = hungarian_new(n_phis, n_regs, HUNGARIAN_MATCH_PERFECT);
1469         n  = 0;
1470         sched_foreach(block, node) {
1471                 unsigned r;
1472
1473                 allocation_info_t *info;
1474                 if (!is_Phi(node))
1475                         break;
1476                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1477                         continue;
1478
1479                 /* give boni for predecessor colorings */
1480                 adapt_phi_prefs(node);
1481                 /* add stuff to bipartite problem */
1482                 info = get_allocation_info(node);
1483                 DB((dbg, LEVEL_3, "Prefs for %+F: ", node));
1484                 for (r = 0; r < n_regs; ++r) {
1485                         float costs;
1486
1487                         if (!rbitset_is_set(normal_regs, r))
1488                                 continue;
1489
1490                         costs = info->prefs[r];
1491                         costs = costs < 0 ? -logf(-costs+1) : logf(costs+1);
1492                         costs *= 100;
1493                         costs += 10000;
1494                         hungarian_add(bp, n, r, costs);
1495                         DB((dbg, LEVEL_3, " %s(%f)", arch_register_for_index(cls, r)->name,
1496                                                 info->prefs[r]));
1497                 }
1498                 DB((dbg, LEVEL_3, "\n"));
1499                 ++n;
1500         }
1501
1502         //hungarian_print_cost_matrix(bp, 7);
1503         hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
1504
1505         assignment = ALLOCAN(int, n_regs);
1506         res        = hungarian_solve(bp, assignment, NULL, 0);
1507         assert(res == 0);
1508
1509         /* apply results */
1510         n = 0;
1511         sched_foreach(block, node) {
1512                 unsigned               r;
1513                 const arch_register_t *reg;
1514
1515                 if (!is_Phi(node))
1516                         break;
1517                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1518                         continue;
1519
1520                 r   = assignment[n++];
1521                 assert(rbitset_is_set(normal_regs, r));
1522                 reg = arch_register_for_index(cls, r);
1523                 DB((dbg, LEVEL_2, "Assign %+F -> %s\n", node, reg->name));
1524                 use_reg(node, reg);
1525
1526                 /* adapt preferences for phi inputs */
1527                 propagate_phi_register(node, r);
1528         }
1529 }
1530
1531 /**
1532  * Walker: assign registers to all nodes of a block that
1533  * need registers from the currently considered register class.
1534  */
1535 static void allocate_coalesce_block(ir_node *block, void *data)
1536 {
1537         int                    i;
1538         ir_nodeset_t           live_nodes;
1539         ir_nodeset_iterator_t  iter;
1540         ir_node               *node;
1541         int                    n_preds;
1542         block_info_t          *block_info;
1543         block_info_t         **pred_block_infos;
1544         ir_node              **phi_ins;
1545         unsigned              *output_regs; /**< collects registers which must not
1546                                                                                      be used for optimistic splits */
1547
1548         (void) data;
1549         DB((dbg, LEVEL_2, "* Block %+F\n", block));
1550
1551         /* clear assignments */
1552         block_info  = get_block_info(block);
1553         assignments = block_info->assignments;
1554
1555         ir_nodeset_init(&live_nodes);
1556
1557         /* gather regalloc infos of predecessor blocks */
1558         n_preds             = get_Block_n_cfgpreds(block);
1559         pred_block_infos    = ALLOCAN(block_info_t*, n_preds);
1560         for (i = 0; i < n_preds; ++i) {
1561                 ir_node      *pred      = get_Block_cfgpred_block(block, i);
1562                 block_info_t *pred_info = get_block_info(pred);
1563                 pred_block_infos[i]     = pred_info;
1564         }
1565
1566         phi_ins = ALLOCAN(ir_node*, n_preds);
1567
1568         /* collect live-in nodes and preassigned values */
1569         be_lv_foreach(lv, block, be_lv_state_in, i) {
1570                 const arch_register_t *reg;
1571                 int                    p;
1572                 bool                   need_phi = false;
1573
1574                 node = be_lv_get_irn(lv, block, i);
1575                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1576                         continue;
1577
1578                 /* check all predecessors for this value, if it is not everywhere the
1579                    same or unknown then we have to construct a phi
1580                    (we collect the potential phi inputs here) */
1581                 for (p = 0; p < n_preds; ++p) {
1582                         block_info_t *pred_info = pred_block_infos[p];
1583
1584                         if (!pred_info->processed) {
1585                                 /* use node for now, it will get fixed later */
1586                                 phi_ins[p] = node;
1587                                 need_phi   = true;
1588                         } else {
1589                                 int a = find_value_in_block_info(pred_info, node);
1590
1591                                 /* must live out of predecessor */
1592                                 assert(a >= 0);
1593                                 phi_ins[p] = pred_info->assignments[a];
1594                                 /* different value from last time? then we need a phi */
1595                                 if (p > 0 && phi_ins[p-1] != phi_ins[p]) {
1596                                         need_phi = true;
1597                                 }
1598                         }
1599                 }
1600
1601                 if (need_phi) {
1602                         ir_mode                   *mode = get_irn_mode(node);
1603                         const arch_register_req_t *req  = get_default_req_current_cls();
1604                         ir_node                   *phi;
1605                         int                        i;
1606
1607                         phi = new_r_Phi(block, n_preds, phi_ins, mode);
1608                         be_set_phi_reg_req(phi, req);
1609
1610                         DB((dbg, LEVEL_3, "Create Phi %+F (for %+F) -", phi, node));
1611 #ifdef DEBUG_libfirm
1612                         for (i = 0; i < n_preds; ++i) {
1613                                 DB((dbg, LEVEL_3, " %+F", phi_ins[i]));
1614                         }
1615                         DB((dbg, LEVEL_3, "\n"));
1616 #endif
1617                         mark_as_copy_of(phi, node);
1618                         sched_add_after(block, phi);
1619
1620                         node = phi;
1621                 } else {
1622                         allocation_info_t *info = get_allocation_info(node);
1623                         info->current_value = phi_ins[0];
1624
1625                         /* Grab 1 of the inputs we constructed (might not be the same as
1626                          * "node" as we could see the same copy of the value in all
1627                          * predecessors */
1628                         node = phi_ins[0];
1629                 }
1630
1631                 /* if the node already has a register assigned use it */
1632                 reg = arch_get_irn_register(node);
1633                 if (reg != NULL) {
1634                         /* TODO: consult pred-block infos here. The value could be copied
1635                            away in some/all predecessor blocks. We need to construct
1636                            phi-nodes in this case.
1637                            We even need to construct some Phi_0 like constructs in cases
1638                            where the predecessor allocation is not determined yet. */
1639                         use_reg(node, reg);
1640                 }
1641
1642                 /* remember that this node is live at the beginning of the block */
1643                 ir_nodeset_insert(&live_nodes, node);
1644         }
1645
1646         rbitset_alloca(output_regs, n_regs);
1647
1648         /* handle phis... */
1649         assign_phi_registers(block);
1650
1651         /* assign regs for live-in values */
1652         foreach_ir_nodeset(&live_nodes, node, iter) {
1653                 const arch_register_t *reg = arch_get_irn_register(node);
1654                 if (reg != NULL)
1655                         continue;
1656
1657                 assign_reg(block, node, output_regs);
1658                 /* shouldn't happen if we color in dominance order */
1659                 assert (!is_Phi(node));
1660         }
1661
1662         /* assign instructions in the block */
1663         sched_foreach(block, node) {
1664                 unsigned r;
1665
1666                 /* phis are already assigned */
1667                 if (is_Phi(node))
1668                         continue;
1669
1670                 rewire_inputs(node);
1671
1672                 /* enforce use constraints */
1673                 rbitset_clear_all(output_regs, n_regs);
1674                 enforce_constraints(&live_nodes, node, output_regs);
1675                 /* we may not use registers occupied here for optimistic splits */
1676                 for (r = 0; r < n_regs; ++r) {
1677                         if (assignments[r] != NULL)
1678                                 rbitset_set(output_regs, r);
1679                 }
1680
1681                 rewire_inputs(node);
1682
1683                 /* free registers of values last used at this instruction */
1684                 free_last_uses(&live_nodes, node);
1685
1686                 /* assign output registers */
1687                 /* TODO: 2 phases: first: pre-assigned ones, 2nd real regs */
1688                 if (get_irn_mode(node) == mode_T) {
1689                         const ir_edge_t *edge;
1690                         foreach_out_edge(node, edge) {
1691                                 ir_node *proj = get_edge_src_irn(edge);
1692                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
1693                                         continue;
1694                                 assign_reg(block, proj, output_regs);
1695                         }
1696                 } else if (arch_irn_consider_in_reg_alloc(cls, node)) {
1697                         assign_reg(block, node, output_regs);
1698                 }
1699         }
1700
1701         ir_nodeset_destroy(&live_nodes);
1702         assignments = NULL;
1703
1704         block_info->processed = true;
1705
1706         /* permute values at end of predecessor blocks in case of phi-nodes */
1707         if (n_preds > 1) {
1708                 int p;
1709                 for (p = 0; p < n_preds; ++p) {
1710                         add_phi_permutations(block, p);
1711                 }
1712         }
1713
1714         /* if we have exactly 1 successor then we might be able to produce phi
1715            copies now */
1716         if (get_irn_n_edges_kind(block, EDGE_KIND_BLOCK) == 1) {
1717                 const ir_edge_t *edge
1718                         = get_irn_out_edge_first_kind(block, EDGE_KIND_BLOCK);
1719                 ir_node      *succ      = get_edge_src_irn(edge);
1720                 int           p         = get_edge_src_pos(edge);
1721                 block_info_t *succ_info = get_block_info(succ);
1722
1723                 if (succ_info->processed) {
1724                         add_phi_permutations(succ, p);
1725                 }
1726         }
1727 }
1728
1729 typedef struct block_costs_t block_costs_t;
1730 struct block_costs_t {
1731         float costs;   /**< costs of the block */
1732         int   dfs_num; /**< depth first search number (to detect backedges) */
1733 };
1734
1735 static int cmp_block_costs(const void *d1, const void *d2)
1736 {
1737         const ir_node       * const *block1 = d1;
1738         const ir_node       * const *block2 = d2;
1739         const block_costs_t *info1  = get_irn_link(*block1);
1740         const block_costs_t *info2  = get_irn_link(*block2);
1741         return QSORT_CMP(info2->costs, info1->costs);
1742 }
1743
1744 static void determine_block_order(void)
1745 {
1746         int i;
1747         ir_node **blocklist = be_get_cfgpostorder(irg);
1748         int       n_blocks  = ARR_LEN(blocklist);
1749         int       dfs_num   = 0;
1750         pdeq     *worklist  = new_pdeq();
1751         ir_node **order     = XMALLOCN(ir_node*, n_blocks);
1752         int       order_p   = 0;
1753
1754         /* clear block links... */
1755         for (i = 0; i < n_blocks; ++i) {
1756                 ir_node *block = blocklist[i];
1757                 set_irn_link(block, NULL);
1758         }
1759
1760         /* walk blocks in reverse postorder, the costs for each block are the
1761          * sum of the costs of its predecessors (excluding the costs on backedges
1762          * which we can't determine) */
1763         for (i = n_blocks-1; i >= 0; --i) {
1764                 block_costs_t *cost_info;
1765                 ir_node *block = blocklist[i];
1766
1767                 float execfreq   = get_block_execfreq(execfreqs, block);
1768                 float costs      = execfreq;
1769                 int   n_cfgpreds = get_Block_n_cfgpreds(block);
1770                 int   p;
1771                 for (p = 0; p < n_cfgpreds; ++p) {
1772                         ir_node       *pred_block = get_Block_cfgpred_block(block, p);
1773                         block_costs_t *pred_costs = get_irn_link(pred_block);
1774                         /* we don't have any info for backedges */
1775                         if (pred_costs == NULL)
1776                                 continue;
1777                         costs += pred_costs->costs;
1778                 }
1779
1780                 cost_info          = OALLOCZ(&obst, block_costs_t);
1781                 cost_info->costs   = costs;
1782                 cost_info->dfs_num = dfs_num++;
1783                 set_irn_link(block, cost_info);
1784         }
1785
1786         /* sort array by block costs */
1787         qsort(blocklist, n_blocks, sizeof(blocklist[0]), cmp_block_costs);
1788
1789         ir_reserve_resources(irg, IR_RESOURCE_BLOCK_VISITED);
1790         inc_irg_block_visited(irg);
1791
1792         for (i = 0; i < n_blocks; ++i) {
1793                 ir_node       *block = blocklist[i];
1794                 if (Block_block_visited(block))
1795                         continue;
1796
1797                 /* continually add predecessors with highest costs to worklist
1798                  * (without using backedges) */
1799                 do {
1800                         block_costs_t *info       = get_irn_link(block);
1801                         ir_node       *best_pred  = NULL;
1802                         float          best_costs = -1;
1803                         int            n_cfgpred  = get_Block_n_cfgpreds(block);
1804                         int            i;
1805
1806                         pdeq_putr(worklist, block);
1807                         mark_Block_block_visited(block);
1808                         for (i = 0; i < n_cfgpred; ++i) {
1809                                 ir_node       *pred_block = get_Block_cfgpred_block(block, i);
1810                                 block_costs_t *pred_info  = get_irn_link(pred_block);
1811
1812                                 /* ignore backedges */
1813                                 if (pred_info->dfs_num > info->dfs_num)
1814                                         continue;
1815
1816                                 if (info->costs > best_costs) {
1817                                         best_costs = info->costs;
1818                                         best_pred  = pred_block;
1819                                 }
1820                         }
1821                         block = best_pred;
1822                 } while(block != NULL && !Block_block_visited(block));
1823
1824                 /* now put all nodes in the worklist in our final order */
1825                 while (!pdeq_empty(worklist)) {
1826                         ir_node *pblock = pdeq_getr(worklist);
1827                         assert(order_p < n_blocks);
1828                         order[order_p++] = pblock;
1829                 }
1830         }
1831         assert(order_p == n_blocks);
1832         del_pdeq(worklist);
1833
1834         ir_free_resources(irg, IR_RESOURCE_BLOCK_VISITED);
1835
1836         DEL_ARR_F(blocklist);
1837
1838         obstack_free(&obst, NULL);
1839         obstack_init(&obst);
1840
1841         block_order   = order;
1842         n_block_order = n_blocks;
1843 }
1844
1845 /**
1846  * Run the register allocator for the current register class.
1847  */
1848 static void be_straight_alloc_cls(void)
1849 {
1850         int i;
1851
1852         lv = be_assure_liveness(birg);
1853         be_liveness_assure_sets(lv);
1854         be_liveness_assure_chk(lv);
1855
1856         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
1857
1858         DB((dbg, LEVEL_2, "=== Allocating registers of %s ===\n", cls->name));
1859
1860         be_clear_links(irg);
1861         irg_block_walk_graph(irg, NULL, analyze_block, NULL);
1862         combine_congruence_classes();
1863
1864         for (i = 0; i < n_block_order; ++i) {
1865                 ir_node *block = block_order[i];
1866                 allocate_coalesce_block(block, NULL);
1867         }
1868
1869         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1870 }
1871
1872 static void dump(int mask, ir_graph *irg, const char *suffix,
1873                  void (*dumper)(ir_graph *, const char *))
1874 {
1875         if(birg->main_env->options->dump_flags & mask)
1876                 be_dump(irg, suffix, dumper);
1877 }
1878
1879 /**
1880  * Run the spiller on the current graph.
1881  */
1882 static void spill(void)
1883 {
1884         /* make sure all nodes show their real register pressure */
1885         BE_TIMER_PUSH(t_ra_constr);
1886         be_pre_spill_prepare_constr(birg, cls);
1887         BE_TIMER_POP(t_ra_constr);
1888
1889         dump(DUMP_RA, irg, "-spillprepare", dump_ir_block_graph_sched);
1890
1891         /* spill */
1892         BE_TIMER_PUSH(t_ra_spill);
1893         be_do_spill(birg, cls);
1894         BE_TIMER_POP(t_ra_spill);
1895
1896         BE_TIMER_PUSH(t_ra_spill_apply);
1897         check_for_memory_operands(irg);
1898         BE_TIMER_POP(t_ra_spill_apply);
1899
1900         dump(DUMP_RA, irg, "-spill", dump_ir_block_graph_sched);
1901 }
1902
1903 /**
1904  * The straight register allocator for a whole procedure.
1905  */
1906 static void be_straight_alloc(be_irg_t *new_birg)
1907 {
1908         const arch_env_t *arch_env = new_birg->main_env->arch_env;
1909         int   n_cls                = arch_env_get_n_reg_class(arch_env);
1910         int   c;
1911
1912         obstack_init(&obst);
1913
1914         birg      = new_birg;
1915         irg       = be_get_birg_irg(birg);
1916         execfreqs = birg->exec_freq;
1917
1918         /* determine a good coloring order */
1919         determine_block_order();
1920
1921         for (c = 0; c < n_cls; ++c) {
1922                 cls             = arch_env_get_reg_class(arch_env, c);
1923                 default_cls_req = NULL;
1924                 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
1925                         continue;
1926
1927                 stat_ev_ctx_push_str("regcls", cls->name);
1928
1929                 n_regs      = arch_register_class_n_regs(cls);
1930                 normal_regs = rbitset_malloc(n_regs);
1931                 be_abi_set_non_ignore_regs(birg->abi, cls, normal_regs);
1932
1933                 spill();
1934
1935                 /* verify schedule and register pressure */
1936                 BE_TIMER_PUSH(t_verify);
1937                 if (birg->main_env->options->vrfy_option == BE_VRFY_WARN) {
1938                         be_verify_schedule(birg);
1939                         be_verify_register_pressure(birg, cls, irg);
1940                 } else if (birg->main_env->options->vrfy_option == BE_VRFY_ASSERT) {
1941                         assert(be_verify_schedule(birg) && "Schedule verification failed");
1942                         assert(be_verify_register_pressure(birg, cls, irg)
1943                                 && "Register pressure verification failed");
1944                 }
1945                 BE_TIMER_POP(t_verify);
1946
1947                 BE_TIMER_PUSH(t_ra_color);
1948                 be_straight_alloc_cls();
1949                 BE_TIMER_POP(t_ra_color);
1950
1951                 /* we most probably constructed new Phis so liveness info is invalid
1952                  * now */
1953                 /* TODO: test liveness_introduce */
1954                 be_liveness_invalidate(lv);
1955                 free(normal_regs);
1956
1957                 stat_ev_ctx_pop("regcls");
1958         }
1959
1960         BE_TIMER_PUSH(t_ra_spill_apply);
1961         be_abi_fix_stack_nodes(birg->abi);
1962         BE_TIMER_POP(t_ra_spill_apply);
1963
1964         BE_TIMER_PUSH(t_verify);
1965         if (birg->main_env->options->vrfy_option == BE_VRFY_WARN) {
1966                 be_verify_register_allocation(birg);
1967         } else if (birg->main_env->options->vrfy_option == BE_VRFY_ASSERT) {
1968                 assert(be_verify_register_allocation(birg)
1969                                 && "Register allocation invalid");
1970         }
1971         BE_TIMER_POP(t_verify);
1972
1973         obstack_free(&obst, NULL);
1974 }
1975
1976 /**
1977  * Initializes this module.
1978  */
1979 void be_init_straight_alloc(void)
1980 {
1981         static be_ra_t be_ra_straight = {
1982                 be_straight_alloc,
1983         };
1984
1985         FIRM_DBG_REGISTER(dbg, "firm.be.straightalloc");
1986
1987         be_register_allocator("straight", &be_ra_straight);
1988 }
1989
1990 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_straight_alloc);