less abreviations: rebitset_cpy => rebitset_copy
[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 registers
38  *    with high preferences. When register constraints are not met, add copies
39  *    and split live-ranges.
40  *
41  * TODO:
42  *  - output constraints are not ensured. The algorithm fails to copy values
43  *    away, so the registers for constrained outputs are free.
44  *  - must_be_different constraint is not respected
45  *  - No parallel copies at basic block borders are created, no additional phis
46  *    created after copies have been inserted.
47  *  - Phi color assignment should give bonus points towards registers already
48  *    assigned at predecessors.
49  *  - think about a smarter sequence of visiting the blocks. Sorted by
50  *    execfreq might be good, or looptree from inner to outermost loops going
51  *    over blocks in a reverse postorder
52  */
53 #include "config.h"
54
55 #include <float.h>
56
57 #include "obst.h"
58 #include "irnode_t.h"
59 #include "irgraph_t.h"
60 #include "iredges_t.h"
61 #include "ircons.h"
62 #include "irgwalk.h"
63 #include "execfreq.h"
64
65 #include "be.h"
66 #include "bera.h"
67 #include "belive_t.h"
68 #include "bemodule.h"
69 #include "bechordal_t.h"
70 #include "besched_t.h"
71 #include "beirg_t.h"
72 #include "benode_t.h"
73 #include "bespilloptions.h"
74 #include "beverify.h"
75
76 #include "bipartite.h"
77 #include "hungarian.h"
78
79 #define USE_FACTOR       1.0f
80 #define DEF_FACTOR       1.0f
81 #define NEIGHBOR_FACTOR  0.2f
82 #define SHOULD_BE_SAME   1.0f
83
84 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
85
86 static struct obstack               obst;
87 static be_irg_t                    *birg;
88 static ir_graph                    *irg;
89 static const arch_register_class_t *cls;
90 static be_lv_t                     *lv;
91 static const ir_exec_freq          *execfreqs;
92 static unsigned                     n_regs;
93 static bitset_t                    *ignore_regs;
94
95 typedef struct assignment_t assignment_t;
96 struct assignment_t {
97         ir_node *value;            /**< currently assigned value */
98 };
99
100 static assignment_t *assignments;
101
102 typedef struct allocation_info_t allocation_info_t;
103 struct allocation_info_t {
104         unsigned      last_uses;   /**< bitset indicating last uses (input pos) */
105         assignment_t *current_assignment;
106         float         prefs[0];    /**< register preferences */
107 };
108
109 typedef struct reg_pref_t reg_pref_t;
110 struct reg_pref_t {
111         unsigned num;
112         float    pref;
113 };
114
115 /**
116  * Get the allocation info for a node.
117  * The info is allocated on the first visit of a node.
118  */
119 static allocation_info_t *get_allocation_info(ir_node *node)
120 {
121         allocation_info_t *info;
122         if (!irn_visited(node)) {
123                 size_t size = sizeof(info[0]) + n_regs * sizeof(float);
124                 info = obstack_alloc(&obst, size);
125                 memset(info, 0, size);
126                 set_irn_link(node, info);
127                 mark_irn_visited(node);
128         } else {
129                 info = get_irn_link(node);
130         }
131
132         return info;
133 }
134
135 /**
136  * Link the allocation info of a node to a copy.
137  * Afterwards, both nodes uses the same allocation info.
138  * Copy must not have an allocation info assigned yet.
139  *
140  * @param copy   the node the gets the allocation info assigned
141  * @param value  the original node
142  */
143 static void link_to(ir_node *copy, ir_node *value)
144 {
145         allocation_info_t *info = get_allocation_info(value);
146         assert(!irn_visited(copy));
147         set_irn_link(copy, info);
148         mark_irn_visited(copy);
149 }
150
151 /**
152  * Calculate the penalties for every register on a node and its live neighbors.
153  *
154  * @param live_nodes   the set of live nodes at the current position, may be NULL
155  * @param penalty      the penalty to subtract from
156  * @param limited      a raw bitset containing the limited set for the node
157  * @param node         the node
158  */
159 static void give_penalties_for_limits(const ir_nodeset_t *live_nodes,
160                                       float penalty, const unsigned* limited,
161                                                                           ir_node *node)
162 {
163         ir_nodeset_iterator_t iter;
164         unsigned              r;
165         allocation_info_t     *info = get_allocation_info(node);
166         ir_node               *neighbor;
167
168         /* give penalty for all forbidden regs */
169         for (r = 0; r < n_regs; ++r) {
170                 if (rbitset_is_set(limited, r))
171                         continue;
172
173                 info->prefs[r] -= penalty;
174         }
175
176         /* all other live values should get a penalty for allowed regs */
177         if (live_nodes == NULL)
178                 return;
179
180         /* TODO: reduce penalty if there are multiple allowed registers... */
181         penalty *= NEIGHBOR_FACTOR;
182         foreach_ir_nodeset(live_nodes, neighbor, iter) {
183                 allocation_info_t *neighbor_info;
184
185                 /* TODO: if op is used on multiple inputs we might not do a
186                  * continue here */
187                 if (neighbor == node)
188                         continue;
189
190                 neighbor_info = get_allocation_info(neighbor);
191                 for (r = 0; r < n_regs; ++r) {
192                         if (!rbitset_is_set(limited, r))
193                                 continue;
194
195                         neighbor_info->prefs[r] -= penalty;
196                 }
197         }
198 }
199
200 /**
201  * Calculate the preferences of a definition for the current register class.
202  * If the definition uses a limited set of registers, reduce the preferences
203  * for the limited register on the node and its neighbors.
204  *
205  * @param live_nodes  the set of live nodes at the current node
206  * @param weight      the weight
207  * @param node        the current node
208  */
209 static void check_defs(const ir_nodeset_t *live_nodes, float weight,
210                        ir_node *node)
211 {
212         const arch_register_req_t *req;
213
214         if (get_irn_mode(node) == mode_T) {
215                 const ir_edge_t *edge;
216                 foreach_out_edge(node, edge) {
217                         ir_node *proj = get_edge_src_irn(edge);
218                         check_defs(live_nodes, weight, proj);
219                 }
220                 return;
221         }
222
223         if (!arch_irn_consider_in_reg_alloc(cls, node))
224                 return;
225
226         req = arch_get_register_req_out(node);
227         if (req->type & arch_register_req_type_limited) {
228                 const unsigned *limited = req->limited;
229                 float           penalty = weight * DEF_FACTOR;
230                 give_penalties_for_limits(live_nodes, penalty, limited, node);
231         }
232
233         if (req->type & arch_register_req_type_should_be_same) {
234                 ir_node           *insn  = skip_Proj(node);
235                 allocation_info_t *info  = get_allocation_info(node);
236                 int                arity = get_irn_arity(insn);
237                 int                i;
238
239                 float factor = 1.0f / rbitset_popcnt(&req->other_same, arity);
240                 for (i = 0; i < arity; ++i) {
241                         ir_node           *op;
242                         unsigned          r;
243                         allocation_info_t *op_info;
244
245                         if (!rbitset_is_set(&req->other_same, i))
246                                 continue;
247
248                         op      = get_irn_n(insn, i);
249                         op_info = get_allocation_info(op);
250                         for (r = 0; r < n_regs; ++r) {
251                                 if (bitset_is_set(ignore_regs, r))
252                                         continue;
253                                 op_info->prefs[r] += info->prefs[r] * factor;
254                         }
255                 }
256         }
257 }
258
259 /**
260  * Walker: Runs an a block calculates the preferences for any
261  * node and every register from the considered register class.
262  */
263 static void analyze_block(ir_node *block, void *data)
264 {
265         float         weight = get_block_execfreq(execfreqs, block);
266         ir_nodeset_t  live_nodes;
267         ir_node      *node;
268         (void) data;
269
270         ir_nodeset_init(&live_nodes);
271         be_liveness_end_of_block(lv, cls, block, &live_nodes);
272
273         sched_foreach_reverse(block, node) {
274                 allocation_info_t *info;
275                 int               i, arity;
276
277                 if (is_Phi(node)) {
278                         /* TODO: handle constrained phi-nodes */
279                         break;
280                 }
281
282                 /* TODO give/take penalties for should_be_same/different) */
283                 check_defs(&live_nodes, weight, node);
284
285                 /* mark last uses */
286                 arity = get_irn_arity(node);
287                 /* I was lazy, and only allocated 1 unsigned
288                    => maximum of 32 uses per node (rewrite if necessary) */
289                 assert(arity <= (int) sizeof(unsigned) * 8);
290
291                 info = get_allocation_info(node);
292                 for (i = 0; i < arity; ++i) {
293                         ir_node *op = get_irn_n(node, i);
294                         if (!arch_irn_consider_in_reg_alloc(cls, op))
295                                 continue;
296
297                         /* last usage of a value? */
298                         if (!ir_nodeset_contains(&live_nodes, op)) {
299                                 rbitset_set(&info->last_uses, i);
300                         }
301                 }
302
303                 be_liveness_transfer(cls, node, &live_nodes);
304
305                 /* update weights based on usage constraints */
306                 for (i = 0; i < arity; ++i) {
307                         const arch_register_req_t *req;
308                         const unsigned            *limited;
309                         ir_node                   *op = get_irn_n(node, i);
310
311                         if (!arch_irn_consider_in_reg_alloc(cls, op))
312                                 continue;
313
314                         req = arch_get_register_req(node, i);
315                         if ((req->type & arch_register_req_type_limited) == 0)
316                                 continue;
317
318                         /* TODO: give penalties to neighbors for precolored nodes! */
319
320                         limited = req->limited;
321                         give_penalties_for_limits(&live_nodes, weight * USE_FACTOR, limited,
322                                                   op);
323                 }
324         }
325
326         ir_nodeset_destroy(&live_nodes);
327 }
328
329 /**
330  * Assign register reg to the given node.
331  *
332  * @param node  the node
333  * @param reg   the register
334  */
335 static void use_reg(ir_node *node, const arch_register_t *reg)
336 {
337         unsigned      r          = arch_register_get_index(reg);
338         assignment_t *assignment = &assignments[r];
339         allocation_info_t *info;
340
341         assert(assignment->value == NULL);
342         assignment->value = node;
343
344         info = get_allocation_info(node);
345         info->current_assignment = assignment;
346
347         arch_set_irn_register(node, reg);
348 }
349
350 /**
351  * Compare two register preferences in decreasing order.
352  */
353 static int compare_reg_pref(const void *e1, const void *e2)
354 {
355         const reg_pref_t *rp1 = (const reg_pref_t*) e1;
356         const reg_pref_t *rp2 = (const reg_pref_t*) e2;
357         if (rp1->pref < rp2->pref)
358                 return 1;
359         if (rp1->pref > rp2->pref)
360                 return -1;
361         return 0;
362 }
363
364 static void fill_sort_candidates(reg_pref_t *regprefs,
365                                  const allocation_info_t *info)
366 {
367         unsigned r;
368
369         for (r = 0; r < n_regs; ++r) {
370                 float pref = info->prefs[r];
371                 if (bitset_is_set(ignore_regs, r)) {
372                         pref = -10000;
373                 }
374                 regprefs[r].num  = r;
375                 regprefs[r].pref = pref;
376         }
377         /* TODO: use a stable sort here to avoid unnecessary register jumping */
378         qsort(regprefs, n_regs, sizeof(regprefs[0]), compare_reg_pref);
379 }
380
381 static void assign_reg(const ir_node *block, ir_node *node)
382 {
383         const arch_register_t     *reg;
384         allocation_info_t         *info;
385         const arch_register_req_t *req;
386         reg_pref_t                *reg_prefs;
387         ir_node                   *in_node;
388         unsigned                  i;
389
390         assert(arch_irn_consider_in_reg_alloc(cls, node));
391
392         /* preassigned register? */
393         reg = arch_get_irn_register(node);
394         if (reg != NULL) {
395                 DB((dbg, LEVEL_2, "Preassignment %+F -> %s\n", node, reg->name));
396                 use_reg(node, reg);
397                 return;
398         }
399
400         /* give should_be_same boni */
401         info = get_allocation_info(node);
402         req  = arch_get_register_req_out(node);
403
404         in_node = skip_Proj(node);
405         if (req->type & arch_register_req_type_should_be_same) {
406                 float weight = get_block_execfreq(execfreqs, block);
407                 int   arity  = get_irn_arity(in_node);
408                 int   i;
409
410                 assert(arity <= (int) sizeof(req->other_same) * 8);
411                 for (i = 0; i < arity; ++i) {
412                         ir_node               *in;
413                         const arch_register_t *reg;
414                         unsigned               r;
415                         if (!rbitset_is_set(&req->other_same, i))
416                                 continue;
417
418                         in  = get_irn_n(in_node, i);
419                         reg = arch_get_irn_register(in);
420                         assert(reg != NULL);
421                         r = arch_register_get_index(reg);
422                         if (bitset_is_set(ignore_regs, r))
423                                 continue;
424                         info->prefs[r] += weight * SHOULD_BE_SAME;
425                 }
426         }
427
428         /* TODO: handle must_be_different */
429
430         /*  */
431         DB((dbg, LEVEL_2, "Candidates for %+F:", node));
432         reg_prefs = alloca(n_regs * sizeof(reg_prefs[0]));
433         fill_sort_candidates(reg_prefs, info);
434         for (i = 0; i < n_regs; ++i) {
435                 unsigned               num = reg_prefs[i].num;
436                 const arch_register_t *reg = arch_register_for_index(cls, num);
437                 DB((dbg, LEVEL_2, " %s(%f)", reg->name, reg_prefs[i].pref));
438         }
439         DB((dbg, LEVEL_2, "\n"));
440
441         for (i = 0; i < n_regs; ++i) {
442                 unsigned r = reg_prefs[i].num;
443                 /* ignores should be last and we should have a non-ignore left */
444                 assert(!bitset_is_set(ignore_regs, r));
445                 /* already used? TODO: It might be better to copy the value occupying the register around here, find out when... */
446                 if (assignments[r].value != NULL)
447                         continue;
448                 use_reg(node, arch_register_for_index(cls, r));
449                 break;
450         }
451 }
452
453 static void free_reg_of_value(ir_node *node)
454 {
455         allocation_info_t *info;
456         assignment_t      *assignment;
457         unsigned          r;
458
459         if (!arch_irn_consider_in_reg_alloc(cls, node))
460                 return;
461
462         info       = get_allocation_info(node);
463         assignment = info->current_assignment;
464
465         assert(assignment != NULL);
466
467         r = assignment - assignments;
468         DB((dbg, LEVEL_2, "Value %+F ended, freeing %s\n",
469                 node, arch_register_for_index(cls, r)->name));
470         assignment->value        = NULL;
471         info->current_assignment = NULL;
472 }
473
474 /**
475  * Return the index of the currently assigned register of a node.
476  */
477 static unsigned get_current_reg(ir_node *node)
478 {
479         allocation_info_t *info       = get_allocation_info(node);
480         assignment_t      *assignment = info->current_assignment;
481         return assignment - assignments;
482 }
483
484 /**
485  * Return the currently assigned assignment of a node.
486  */
487 static assignment_t *get_current_assignment(ir_node *node)
488 {
489         allocation_info_t *info = get_allocation_info(node);
490         return info->current_assignment;
491 }
492
493 /**
494  * Add an permutation in front of a node and change the assignments
495  * due to this permutation.
496  *
497  * @param live_nodes   the set of live nodes, updated due to live range split
498  * @param before       the node before we add the permutation
499  * @param permutation  the permutation array (map indexes to indexes)
500  */
501 static void permutate_values(ir_nodeset_t *live_nodes, ir_node *before,
502                              unsigned *permutation)
503 {
504         ir_node *block, *perm;
505         ir_node **in = ALLOCAN(ir_node*, n_regs);
506         size_t    r;
507
508         int i = 0;
509         for (r = 0; r < n_regs; ++r) {
510                 unsigned     new_reg = permutation[r];
511                 assignment_t *assignment;
512                 ir_node      *value;
513
514                 if (new_reg == r)
515                         continue;
516
517                 assignment = &assignments[r];
518                 value      = assignment->value;
519                 if (value == NULL) {
520                         /* nothing to do here, reg is not live */
521                         permutation[r] = r;
522                         continue;
523                 }
524
525                 in[i++] = value;
526
527                 free_reg_of_value(value);
528                 ir_nodeset_remove(live_nodes, value);
529         }
530
531         block = get_nodes_block(before);
532         perm  = be_new_Perm(cls, block, i, in);
533
534         sched_add_before(before, perm);
535
536         i = 0;
537         for (r = 0; r < n_regs; ++r) {
538                 unsigned new_reg = permutation[r];
539                 ir_node  *value;
540                 ir_mode  *mode;
541                 ir_node  *proj;
542                 const arch_register_t *reg;
543
544                 if (new_reg == r)
545                         continue;
546
547                 value = in[i];
548                 mode  = get_irn_mode(value);
549                 proj  = new_r_Proj(block, perm, mode, i);
550
551                 reg = arch_register_for_index(cls, new_reg);
552
553                 link_to(proj, value);
554                 use_reg(proj, reg);
555                 ir_nodeset_insert(live_nodes, proj);
556
557                 ++i;
558         }
559 }
560
561 /**
562  * Free regs for values last used.
563  *
564  * @param live_nodes   set of live nodes, will be updated
565  * @param node         the node to consider
566  */
567 static void free_last_uses(ir_nodeset_t *live_nodes, ir_node *node)
568 {
569         allocation_info_t *info  = get_allocation_info(node);
570         int                arity = get_irn_arity(node);
571         int                i;
572         for (i = 0; i < arity; ++i) {
573                 ir_node *op;
574
575                 /* check if one operand is the last use */
576                 if (!rbitset_is_set(&info->last_uses, i))
577                         continue;
578
579                 op = get_irn_n(node, i);
580                 free_reg_of_value(op);
581                 ir_nodeset_remove(live_nodes, op);
582         }
583 }
584
585 /**
586  * Enforce constraints at a node by live range splits.
587  *
588  * @param live_nodes  the set of live nodes, might be changed
589  * @param node        the current node
590  */
591 static void enforce_constraints(ir_nodeset_t *live_nodes, ir_node *node)
592 {
593         int arity = get_irn_arity(node);
594         int i, dummy, res;
595         hungarian_problem_t *bp;
596         unsigned l, r, p;
597         unsigned *assignment;
598
599         /* see if any use constraints are not met */
600         bool good = true;
601         for (i = 0; i < arity; ++i) {
602                 ir_node                   *op = get_irn_n(node, i);
603                 const arch_register_req_t *req;
604                 const unsigned            *limited;
605                 unsigned                  r;
606
607                 if (!arch_irn_consider_in_reg_alloc(cls, op))
608                         continue;
609
610                 /* are there any limitations for the i'th operand? */
611                 req = arch_get_register_req(node, i);
612                 if ((req->type & arch_register_req_type_limited) == 0)
613                         continue;
614
615                 limited = req->limited;
616                 r       = get_current_reg(op);
617                 if (!rbitset_is_set(limited, r)) {
618                         /* found an assignement outside the limited set */
619                         good = false;
620                         break;
621                 }
622         }
623
624         if (good)
625                 return;
626
627         /* swap values around */
628         bp = hungarian_new(n_regs, n_regs, HUNGARIAN_MATCH_PERFECT);
629
630         /* add all combinations, then remove not allowed ones */
631         for (l = 0; l < n_regs; ++l) {
632                 if (bitset_is_set(ignore_regs, l)) {
633                         hungarian_add(bp, l, l, 90);
634                         continue;
635                 }
636
637                 for (r = 0; r < n_regs; ++r) {
638                         if (bitset_is_set(ignore_regs, r))
639                                 continue;
640
641                         hungarian_add(bp, l, r, l == r ? 90 : 89);
642                 }
643         }
644
645         for (i = 0; i < arity; ++i) {
646                 ir_node                   *op = get_irn_n(node, i);
647                 const arch_register_req_t *req;
648                 const unsigned            *limited;
649                 unsigned                  current_reg;
650
651                 if (!arch_irn_consider_in_reg_alloc(cls, op))
652                         continue;
653
654                 req = arch_get_register_req(node, i);
655                 if ((req->type & arch_register_req_type_limited) == 0)
656                         continue;
657
658                 limited     = req->limited;
659                 current_reg = get_current_reg(op);
660                 for (r = 0; r < n_regs; ++r) {
661                         if (rbitset_is_set(limited, r))
662                                 continue;
663                         hungarian_remv(bp, current_reg, r);
664                 }
665         }
666
667         hungarian_print_costmatrix(bp, 1);
668         hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
669
670         assignment = ALLOCAN(unsigned, n_regs);
671         res = hungarian_solve(bp, (int*) assignment, &dummy, 0);
672         assert(res == 0);
673
674         printf("Swap result:");
675         for (p = 0; p < n_regs; ++p) {
676                 printf(" %d", assignment[p]);
677         }
678         printf("\n");
679
680         hungarian_free(bp);
681
682         permutate_values(live_nodes, node, assignment);
683 }
684
685 /**
686  * Walker: assign registers to all nodes of a block that
687  * needs registers from the currently considered register class.
688  */
689 static void allocate_coalesce_block(ir_node *block, void *data)
690 {
691         int                   i;
692         ir_nodeset_t          live_nodes;
693         ir_nodeset_iterator_t iter;
694         ir_node               *node, *start;
695
696         /* clear assignments */
697         memset(assignments, 0, n_regs * sizeof(assignments[0]));
698
699         (void) data;
700
701         /* collect live-in nodes and preassigned values */
702         ir_nodeset_init(&live_nodes);
703         be_lv_foreach(lv, block, be_lv_state_in, i) {
704                 const arch_register_t *reg;
705
706                 node = be_lv_get_irn(lv, block, i);
707                 if (!arch_irn_consider_in_reg_alloc(cls, node))
708                         continue;
709
710                 ir_nodeset_insert(&live_nodes, node);
711
712                 /* fill in regs already assigned */
713                 reg = arch_get_irn_register(node);
714                 if (reg != NULL) {
715                         use_reg(node, reg);
716                 }
717         }
718
719         /* handle phis... */
720         node = sched_first(block);
721         for ( ; is_Phi(node); node = sched_next(node)) {
722                 const arch_register_t *reg;
723
724                 if (!arch_irn_consider_in_reg_alloc(cls, node))
725                         continue;
726
727                 /* fill in regs already assigned */
728                 reg = arch_get_irn_register(node);
729                 if (reg != NULL) {
730                         use_reg(node, reg);
731                 } else {
732                         /* TODO: give boni for registers already assigned at the
733                            predecessors */
734                 }
735         }
736         start = node;
737
738         /* assign regs for live-in values */
739         foreach_ir_nodeset(&live_nodes, node, iter) {
740                 assign_reg(block, node);
741         }
742
743         /* assign instructions in the block */
744         for (node = start; !sched_is_end(node); node = sched_next(node)) {
745                 int arity = get_irn_arity(node);
746                 int i;
747
748                 /* enforce use constraints */
749                 enforce_constraints(&live_nodes, node);
750
751                 /* exchange values to copied values where needed */
752                 for (i = 0; i < arity; ++i) {
753                         ir_node      *op = get_irn_n(node, i);
754                         assignment_t *assignment;
755
756                         if (!arch_irn_consider_in_reg_alloc(cls, op))
757                                 continue;
758                         assignment = get_current_assignment(op);
759                         assert(assignment != NULL);
760                         if (op != assignment->value) {
761                                 set_irn_n(node, i, assignment->value);
762                         }
763                 }
764
765                 free_last_uses(&live_nodes, node);
766
767                 /* assign output registers */
768                 /* TODO: 2 phases: first: pre-assigned ones, 2nd real regs */
769                 if (get_irn_mode(node) == mode_T) {
770                         const ir_edge_t *edge;
771                         foreach_out_edge(node, edge) {
772                                 ir_node *proj = get_edge_src_irn(edge);
773                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
774                                         continue;
775                                 assign_reg(block, proj);
776                         }
777                 } else if (arch_irn_consider_in_reg_alloc(cls, node)) {
778                         assign_reg(block, node);
779                 }
780         }
781
782         foreach_ir_nodeset(&live_nodes, node, iter) {
783                 free_reg_of_value(node);
784         }
785
786         ir_nodeset_destroy(&live_nodes);
787 }
788
789 /**
790  * Run the register allocator for the current register class.
791  */
792 static void be_straight_alloc_cls(void)
793 {
794         n_regs         = arch_register_class_n_regs(cls);
795         lv             = be_assure_liveness(birg);
796         be_liveness_assure_sets(lv);
797         be_liveness_assure_chk(lv);
798
799         assignments = obstack_alloc(&obst, n_regs * sizeof(assignments[0]));
800
801         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
802         inc_irg_visited(irg);
803
804         irg_block_walk_graph(irg, analyze_block, NULL, NULL);
805         irg_block_walk_graph(irg, allocate_coalesce_block, NULL, NULL);
806
807         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
808 }
809
810 /**
811  * Run the spiller on the current graph.
812  */
813 static void spill(void)
814 {
815         /* TODO: rewrite pre_spill_prepare to work without chordal_env... */
816         be_chordal_env_t  cenv;
817         memset(&cenv, 0, sizeof(cenv));
818         cenv.obst          = &obst;
819         cenv.irg           = irg;
820         cenv.birg          = birg;
821         cenv.cls           = cls;
822         cenv.ignore_colors = ignore_regs;
823
824         /* make sure all nodes show their real register pressure */
825         be_pre_spill_prepare_constr(&cenv);
826
827         /* spill */
828         be_do_spill(birg, cls);
829
830         check_for_memory_operands(irg);
831 }
832
833 /**
834  * The straight register allocator for a whole procedure.
835  */
836 static void be_straight_alloc(be_irg_t *new_birg)
837 {
838         const arch_env_t *arch_env = new_birg->main_env->arch_env;
839         int   n_cls                = arch_env_get_n_reg_class(arch_env);
840         int   c;
841
842         obstack_init(&obst);
843
844         birg      = new_birg;
845         irg       = be_get_birg_irg(birg);
846         execfreqs = birg->exec_freq;
847
848         /* TODO: extract some of the stuff from bechordal allocator, like
849          * statistics, time measurements, etc. and use them here too */
850
851         for (c = 0; c < n_cls; ++c) {
852                 cls = arch_env_get_reg_class(arch_env, c);
853                 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
854                         continue;
855
856                 stat_ev_ctx_push_str("bestraight_cls", cls->name);
857
858                 n_regs      = cls->n_regs;
859                 ignore_regs = bitset_malloc(n_regs);
860                 be_put_ignore_regs(birg, cls, ignore_regs);
861
862                 spill();
863
864                 /* verify schedule and register pressure */
865                 BE_TIMER_PUSH(t_verify);
866                 if (birg->main_env->options->vrfy_option == BE_CH_VRFY_WARN) {
867                         be_verify_schedule(birg);
868                         be_verify_register_pressure(birg, cls, irg);
869                 } else if (birg->main_env->options->vrfy_option == BE_CH_VRFY_ASSERT) {
870                         assert(be_verify_schedule(birg) && "Schedule verification failed");
871                         assert(be_verify_register_pressure(birg, cls, irg)
872                                 && "Register pressure verification failed");
873                 }
874                 BE_TIMER_POP(t_verify);
875
876                 BE_TIMER_PUSH(t_ra_color);
877                 be_straight_alloc_cls();
878                 BE_TIMER_POP(t_ra_color);
879
880                 bitset_free(ignore_regs);
881
882                 /* TODO: dump intermediate results */
883
884                 stat_ev_ctx_pop("bestraight_cls");
885         }
886
887         obstack_free(&obst, NULL);
888 }
889
890 static be_ra_t be_ra_straight = {
891         be_straight_alloc,
892 };
893
894 /**
895  * Initializes this module.
896  */
897 void be_init_straight_alloc(void)
898 {
899         FIRM_DBG_REGISTER(dbg, "firm.be.straightalloc");
900
901         be_register_allocator("straight", &be_ra_straight);
902 }
903
904 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_straight_alloc);