1d547026b748aee4dce6db400a94c83447f99ded
[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 static allocation_info_t *get_allocation_info(ir_node *node)
116 {
117         allocation_info_t *info;
118         if (!irn_visited(node)) {
119                 size_t size = sizeof(info[0]) + n_regs * sizeof(float);
120                 info = obstack_alloc(&obst, size);
121                 memset(info, 0, size);
122                 set_irn_link(node, info);
123                 mark_irn_visited(node);
124         } else {
125                 info = get_irn_link(node);
126         }
127
128         return info;
129 }
130
131 static void link_to(ir_node *copy, ir_node *value)
132 {
133         allocation_info_t *info = get_allocation_info(value);
134         assert(!irn_visited(copy));
135         set_irn_link(copy, info);
136         mark_irn_visited(copy);
137 }
138
139 static void give_penalties_for_limits(const ir_nodeset_t *live_nodes,
140                                       float penalty, const unsigned* limited,
141                                                                           ir_node *node)
142 {
143         ir_nodeset_iterator_t iter;
144         unsigned              r;
145         allocation_info_t     *info = get_allocation_info(node);
146         ir_node               *neighbor;
147
148         /* give penalty for all forbidden regs */
149         for (r = 0; r < n_regs; ++r) {
150                 if (rbitset_is_set(limited, r))
151                         continue;
152
153                 info->prefs[r] -= penalty;
154         }
155
156         /* all other live values should get a penalty for allowed regs */
157         if (live_nodes == NULL)
158                 return;
159
160         /* TODO: reduce penalty if there are multiple allowed registers... */
161         penalty *= NEIGHBOR_FACTOR;
162         foreach_ir_nodeset(live_nodes, neighbor, iter) {
163                 allocation_info_t *neighbor_info;
164
165                 /* TODO: if op is used on multiple inputs we might not do a
166                  * continue here */
167                 if (neighbor == node)
168                         continue;
169
170                 neighbor_info = get_allocation_info(neighbor);
171                 for (r = 0; r < n_regs; ++r) {
172                         if (!rbitset_is_set(limited, r))
173                                 continue;
174
175                         neighbor_info->prefs[r] -= penalty;
176                 }
177         }
178 }
179
180 static void check_defs(const ir_nodeset_t *live_nodes, float weight,
181                        ir_node *node)
182 {
183         const arch_register_req_t *req;
184
185         if (get_irn_mode(node) == mode_T) {
186                 const ir_edge_t *edge;
187                 foreach_out_edge(node, edge) {
188                         ir_node *proj = get_edge_src_irn(edge);
189                         check_defs(live_nodes, weight, proj);
190                 }
191                 return;
192         }
193
194         if (!arch_irn_consider_in_reg_alloc(cls, node))
195                 return;
196
197         req = arch_get_register_req_out(node);
198         if (req->type & arch_register_req_type_limited) {
199                 const unsigned *limited = req->limited;
200                 float           penalty = weight * DEF_FACTOR;
201                 give_penalties_for_limits(live_nodes, penalty, limited, node);
202         }
203
204         if (req->type & arch_register_req_type_should_be_same) {
205                 ir_node           *insn  = skip_Proj(node);
206                 allocation_info_t *info  = get_allocation_info(node);
207                 int                arity = get_irn_arity(insn);
208                 int                i;
209
210                 float factor = 1.0f / rbitset_popcnt(&req->other_same, arity);
211                 for (i = 0; i < arity; ++i) {
212                         ir_node           *op;
213                         unsigned          r;
214                         allocation_info_t *op_info;
215
216                         if (!rbitset_is_set(&req->other_same, i))
217                                 continue;
218
219                         op      = get_irn_n(insn, i);
220                         op_info = get_allocation_info(op);
221                         for (r = 0; r < n_regs; ++r) {
222                                 if (bitset_is_set(ignore_regs, r))
223                                         continue;
224                                 op_info->prefs[r] += info->prefs[r] * factor;
225                         }
226                 }
227         }
228 }
229
230 static void analyze_block(ir_node *block, void *data)
231 {
232         float         weight = get_block_execfreq(execfreqs, block);
233         ir_nodeset_t  live_nodes;
234         ir_node      *node;
235         (void) data;
236
237         ir_nodeset_init(&live_nodes);
238         be_liveness_end_of_block(lv, cls, block, &live_nodes);
239
240         sched_foreach_reverse(block, node) {
241                 allocation_info_t *info;
242                 int               i, arity;
243
244                 if (is_Phi(node)) {
245                         /* TODO: handle constrained phi-nodes */
246                         break;
247                 }
248
249                 /* TODO give/take penalties for should_be_same/different) */
250                 check_defs(&live_nodes, weight, node);
251
252                 /* mark last uses */
253                 arity = get_irn_arity(node);
254                 /* I was lazy, and only allocated 1 unsigned
255                    => maximum of 32 uses per node (rewrite if necessary) */
256                 assert(arity <= (int) sizeof(unsigned) * 8);
257
258                 info = get_allocation_info(node);
259                 for (i = 0; i < arity; ++i) {
260                         ir_node *op = get_irn_n(node, i);
261                         if (!arch_irn_consider_in_reg_alloc(cls, op))
262                                 continue;
263
264                         /* last usage of a value? */
265                         if (!ir_nodeset_contains(&live_nodes, op)) {
266                                 rbitset_set(&info->last_uses, i);
267                         }
268                 }
269
270                 be_liveness_transfer(cls, node, &live_nodes);
271
272                 /* update weights based on usage constraints */
273                 for (i = 0; i < arity; ++i) {
274                         const arch_register_req_t *req;
275                         const unsigned            *limited;
276                         ir_node                   *op = get_irn_n(node, i);
277
278                         if (!arch_irn_consider_in_reg_alloc(cls, op))
279                                 continue;
280
281                         req = arch_get_register_req(node, i);
282                         if ((req->type & arch_register_req_type_limited) == 0)
283                                 continue;
284
285                         /* TODO: give penalties to neighbors for precolored nodes! */
286
287                         limited = req->limited;
288                         give_penalties_for_limits(&live_nodes, weight * USE_FACTOR, limited,
289                                                   op);
290                 }
291         }
292
293         ir_nodeset_destroy(&live_nodes);
294 }
295
296 static void use_reg(ir_node *node, const arch_register_t *reg)
297 {
298         unsigned      r          = arch_register_get_index(reg);
299         assignment_t *assignment = &assignments[r];
300         allocation_info_t *info;
301
302         assert(assignment->value == NULL);
303         assignment->value = node;
304
305         info = get_allocation_info(node);
306         info->current_assignment = assignment;
307
308         arch_set_irn_register(node, reg);
309 }
310
311 static int compare_reg_pref(const void *e1, const void *e2)
312 {
313         const reg_pref_t *rp1 = (const reg_pref_t*) e1;
314         const reg_pref_t *rp2 = (const reg_pref_t*) e2;
315         if (rp1->pref < rp2->pref)
316                 return 1;
317         if (rp1->pref > rp2->pref)
318                 return -1;
319         return 0;
320 }
321
322 static void fill_sort_candidates(reg_pref_t *regprefs,
323                                  const allocation_info_t *info)
324 {
325         unsigned r;
326
327         for (r = 0; r < n_regs; ++r) {
328                 float pref = info->prefs[r];
329                 if (bitset_is_set(ignore_regs, r)) {
330                         pref = -10000;
331                 }
332                 regprefs[r].num  = r;
333                 regprefs[r].pref = pref;
334         }
335         /* TODO: use a stable sort here to avoid unnecessary register jumping */
336         qsort(regprefs, n_regs, sizeof(regprefs[0]), compare_reg_pref);
337 }
338
339 static void assign_reg(const ir_node *block, ir_node *node)
340 {
341         const arch_register_t     *reg;
342         allocation_info_t         *info;
343         const arch_register_req_t *req;
344         reg_pref_t                *reg_prefs;
345         ir_node                   *in_node;
346         unsigned                  i;
347
348         assert(arch_irn_consider_in_reg_alloc(cls, node));
349
350         /* preassigned register? */
351         reg = arch_get_irn_register(node);
352         if (reg != NULL) {
353                 DB((dbg, LEVEL_2, "Preassignment %+F -> %s\n", node, reg->name));
354                 use_reg(node, reg);
355                 return;
356         }
357
358         /* give should_be_same boni */
359         info = get_allocation_info(node);
360         req  = arch_get_register_req_out(node);
361
362         in_node = skip_Proj(node);
363         if (req->type & arch_register_req_type_should_be_same) {
364                 float weight = get_block_execfreq(execfreqs, block);
365                 int   arity  = get_irn_arity(in_node);
366                 int   i;
367
368                 assert(arity <= (int) sizeof(req->other_same) * 8);
369                 for (i = 0; i < arity; ++i) {
370                         ir_node               *in;
371                         const arch_register_t *reg;
372                         unsigned               r;
373                         if (!rbitset_is_set(&req->other_same, i))
374                                 continue;
375
376                         in  = get_irn_n(in_node, i);
377                         reg = arch_get_irn_register(in);
378                         assert(reg != NULL);
379                         r = arch_register_get_index(reg);
380                         if (bitset_is_set(ignore_regs, r))
381                                 continue;
382                         info->prefs[r] += weight * SHOULD_BE_SAME;
383                 }
384         }
385
386         /* TODO: handle must_be_different */
387
388         /*  */
389         DB((dbg, LEVEL_2, "Candidates for %+F:", node));
390         reg_prefs = alloca(n_regs * sizeof(reg_prefs[0]));
391         fill_sort_candidates(reg_prefs, info);
392         for (i = 0; i < n_regs; ++i) {
393                 unsigned               num = reg_prefs[i].num;
394                 const arch_register_t *reg = arch_register_for_index(cls, num);
395                 DB((dbg, LEVEL_2, " %s(%f)", reg->name, reg_prefs[i].pref));
396         }
397         DB((dbg, LEVEL_2, "\n"));
398
399         for (i = 0; i < n_regs; ++i) {
400                 unsigned r = reg_prefs[i].num;
401                 /* ignores should be last and we should have a non-ignore left */
402                 assert(!bitset_is_set(ignore_regs, r));
403                 /* already used? TODO: It might be better to copy the value occupying the register around here, find out when... */
404                 if (assignments[r].value != NULL)
405                         continue;
406                 use_reg(node, arch_register_for_index(cls, r));
407                 break;
408         }
409 }
410
411 static void free_reg_of_value(ir_node *node)
412 {
413         allocation_info_t *info;
414         assignment_t      *assignment;
415         unsigned          r;
416
417         if (!arch_irn_consider_in_reg_alloc(cls, node))
418                 return;
419
420         info       = get_allocation_info(node);
421         assignment = info->current_assignment;
422
423         assert(assignment != NULL);
424
425         r = assignment - assignments;
426         DB((dbg, LEVEL_2, "Value %+F ended, freeing %s\n",
427                 node, arch_register_for_index(cls, r)->name));
428         assignment->value        = NULL;
429         info->current_assignment = NULL;
430 }
431
432 static unsigned get_current_reg(ir_node *node)
433 {
434         allocation_info_t *info       = get_allocation_info(node);
435         assignment_t      *assignment = info->current_assignment;
436         return assignment - assignments;
437 }
438
439 static assignment_t *get_current_assignment(ir_node *node)
440 {
441         allocation_info_t *info = get_allocation_info(node);
442         return info->current_assignment;
443 }
444
445 static void permutate_values(ir_nodeset_t *live_nodes, ir_node *before,
446                              unsigned *permutation)
447 {
448         ir_node *block, *perm;
449         ir_node **in = ALLOCAN(ir_node*, n_regs);
450         size_t    r;
451
452         int i = 0;
453         for (r = 0; r < n_regs; ++r) {
454                 unsigned     new_reg = permutation[r];
455                 assignment_t *assignment;
456                 ir_node      *value;
457
458                 if (new_reg == r)
459                         continue;
460
461                 assignment = &assignments[r];
462                 value      = assignment->value;
463                 if (value == NULL) {
464                         /* nothing to do here, reg is not live */
465                         permutation[r] = r;
466                         continue;
467                 }
468
469                 in[i++] = value;
470
471                 free_reg_of_value(value);
472                 ir_nodeset_remove(live_nodes, value);
473         }
474
475         block = get_nodes_block(before);
476         perm  = be_new_Perm(cls, irg, block, i, in);
477
478         sched_add_before(before, perm);
479
480         i = 0;
481         for (r = 0; r < n_regs; ++r) {
482                 unsigned new_reg = permutation[r];
483                 ir_node  *value;
484                 ir_mode  *mode;
485                 ir_node  *proj;
486                 const arch_register_t *reg;
487
488                 if (new_reg == r)
489                         continue;
490
491                 value = in[i];
492                 mode  = get_irn_mode(value);
493                 proj  = new_r_Proj(irg, block, perm, mode, i);
494
495                 reg = arch_register_for_index(cls, new_reg);
496
497                 link_to(proj, value);
498                 use_reg(proj, reg);
499                 ir_nodeset_insert(live_nodes, proj);
500
501                 ++i;
502         }
503 }
504
505 /* free regs for values last used */
506 static void free_last_uses(ir_nodeset_t *live_nodes, ir_node *node)
507 {
508         allocation_info_t *info  = get_allocation_info(node);
509         int                arity = get_irn_arity(node);
510         int                i;
511         for (i = 0; i < arity; ++i) {
512                 ir_node *op;
513
514                 if (!rbitset_is_set(&info->last_uses, i))
515                         continue;
516
517                 op = get_irn_n(node, i);
518                 free_reg_of_value(op);
519                 ir_nodeset_remove(live_nodes, op);
520         }
521 }
522
523 static void enforce_constraints(ir_nodeset_t *live_nodes, ir_node *node)
524 {
525         int arity = get_irn_arity(node);
526         int i, dummy, res;
527         hungarian_problem_t *bp;
528         unsigned l, r, p;
529         unsigned *assignment;
530
531         /* see if any use constraints are not met */
532         bool good = true;
533         for (i = 0; i < arity; ++i) {
534                 ir_node                   *op = get_irn_n(node, i);
535                 const arch_register_req_t *req;
536                 const unsigned            *limited;
537                 unsigned                  r;
538
539                 if (!arch_irn_consider_in_reg_alloc(cls, op))
540                         continue;
541
542                 req = arch_get_register_req(node, i);
543                 if ((req->type & arch_register_req_type_limited) == 0)
544                         continue;
545
546                 limited = req->limited;
547                 r       = get_current_reg(op);
548                 if (!rbitset_is_set(limited, r)) {
549                         good = false;
550                         break;
551                 }
552         }
553
554         if (good)
555                 return;
556
557         /* swap values around */
558         bp = hungarian_new(n_regs, n_regs, HUNGARIAN_MATCH_PERFECT);
559
560         /* add all combinations, then remove not allowed ones */
561         for (l = 0; l < n_regs; ++l) {
562                 if (bitset_is_set(ignore_regs, l)) {
563                         hungarian_add(bp, l, l, 90);
564                         continue;
565                 }
566
567                 for (r = 0; r < n_regs; ++r) {
568                         if (bitset_is_set(ignore_regs, r))
569                                 continue;
570
571                         hungarian_add(bp, l, r, l == r ? 90 : 89);
572                 }
573         }
574
575         for (i = 0; i < arity; ++i) {
576                 ir_node                   *op = get_irn_n(node, i);
577                 const arch_register_req_t *req;
578                 const unsigned            *limited;
579                 unsigned                  current_reg;
580
581                 if (!arch_irn_consider_in_reg_alloc(cls, op))
582                         continue;
583
584                 req = arch_get_register_req(node, i);
585                 if ((req->type & arch_register_req_type_limited) == 0)
586                         continue;
587
588                 limited     = req->limited;
589                 current_reg = get_current_reg(op);
590                 for (r = 0; r < n_regs; ++r) {
591                         if (rbitset_is_set(limited, r))
592                                 continue;
593                         hungarian_remv(bp, current_reg, r);
594                 }
595         }
596
597         hungarian_print_costmatrix(bp, 1);
598         hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
599
600         assignment = ALLOCAN(unsigned, n_regs);
601         res = hungarian_solve(bp, (int*) assignment, &dummy, 0);
602         assert(res == 0);
603
604         printf("Swap result:");
605         for (p = 0; p < n_regs; ++p) {
606                 printf(" %d", assignment[p]);
607         }
608         printf("\n");
609
610         hungarian_free(bp);
611
612         permutate_values(live_nodes, node, assignment);
613 }
614
615 static void allocate_coalesce_block(ir_node *block, void *data)
616 {
617         int                   i;
618         ir_nodeset_t          live_nodes;
619         ir_nodeset_iterator_t iter;
620         ir_node               *node, *start;
621
622         /* clear assignments */
623         memset(assignments, 0, n_regs * sizeof(assignments[0]));
624
625         (void) data;
626
627         /* collect live-in nodes and preassigned values */
628         ir_nodeset_init(&live_nodes);
629         be_lv_foreach(lv, block, be_lv_state_in, i) {
630                 const arch_register_t *reg;
631
632                 node = be_lv_get_irn(lv, block, i);
633                 if (!arch_irn_consider_in_reg_alloc(cls, node))
634                         continue;
635
636                 ir_nodeset_insert(&live_nodes, node);
637
638                 /* fill in regs already assigned */
639                 reg = arch_get_irn_register(node);
640                 if (reg != NULL) {
641                         use_reg(node, reg);
642                 }
643         }
644
645         /* handle phis... */
646         node = sched_first(block);
647         for ( ; is_Phi(node); node = sched_next(node)) {
648                 const arch_register_t *reg;
649
650                 if (!arch_irn_consider_in_reg_alloc(cls, node))
651                         continue;
652
653                 /* fill in regs already assigned */
654                 reg = arch_get_irn_register(node);
655                 if (reg != NULL) {
656                         use_reg(node, reg);
657                 } else {
658                         /* TODO: give boni for registers already assigned at the
659                            predecessors */
660                 }
661         }
662         start = node;
663
664         /* assign regs for live-in values */
665         foreach_ir_nodeset(&live_nodes, node, iter) {
666                 assign_reg(block, node);
667         }
668
669         /* assign instructions in the block */
670         for (node = start; !sched_is_end(node); node = sched_next(node)) {
671                 int arity = get_irn_arity(node);
672                 int i;
673
674                 /* enforce use constraints */
675                 enforce_constraints(&live_nodes, node);
676
677                 /* exchange values to copied values where needed */
678                 for (i = 0; i < arity; ++i) {
679                         ir_node      *op = get_irn_n(node, i);
680                         assignment_t *assignment;
681
682                         if (!arch_irn_consider_in_reg_alloc(cls, op))
683                                 continue;
684                         assignment = get_current_assignment(op);
685                         assert(assignment != NULL);
686                         if (op != assignment->value) {
687                                 set_irn_n(node, i, assignment->value);
688                         }
689                 }
690
691                 free_last_uses(&live_nodes, node);
692
693                 /* assign output registers */
694                 /* TODO: 2 phases: first: pre-assigned ones, 2nd real regs */
695                 if (get_irn_mode(node) == mode_T) {
696                         const ir_edge_t *edge;
697                         foreach_out_edge(node, edge) {
698                                 ir_node *proj = get_edge_src_irn(edge);
699                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
700                                         continue;
701                                 assign_reg(block, proj);
702                         }
703                 } else if (arch_irn_consider_in_reg_alloc(cls, node)) {
704                         assign_reg(block, node);
705                 }
706         }
707
708         foreach_ir_nodeset(&live_nodes, node, iter) {
709                 free_reg_of_value(node);
710         }
711
712         ir_nodeset_destroy(&live_nodes);
713 }
714
715 void be_straight_alloc_cls(void)
716 {
717         n_regs         = arch_register_class_n_regs(cls);
718         lv             = be_assure_liveness(birg);
719         be_liveness_assure_sets(lv);
720         be_liveness_assure_chk(lv);
721
722         assignments = obstack_alloc(&obst, n_regs * sizeof(assignments[0]));
723
724         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
725         inc_irg_visited(irg);
726
727         irg_block_walk_graph(irg, analyze_block, NULL, NULL);
728         irg_block_walk_graph(irg, allocate_coalesce_block, NULL, NULL);
729
730         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
731 }
732
733 static void spill(void)
734 {
735         /* TODO: rewrite pre_spill_prepare to work without chordal_env... */
736         be_chordal_env_t  cenv;
737         memset(&cenv, 0, sizeof(cenv));
738         cenv.obst          = &obst;
739         cenv.irg           = irg;
740         cenv.birg          = birg;
741         cenv.cls           = cls;
742         cenv.ignore_colors = ignore_regs;
743
744         /* make sure all nodes show their real register pressure */
745         be_pre_spill_prepare_constr(&cenv);
746
747         /* spill */
748         be_do_spill(birg, cls);
749
750         check_for_memory_operands(irg);
751 }
752
753 static void be_straight_alloc(be_irg_t *new_birg)
754 {
755         const arch_env_t *arch_env = new_birg->main_env->arch_env;
756         int   n_cls                = arch_env_get_n_reg_class(arch_env);
757         int   c;
758
759         obstack_init(&obst);
760
761         birg      = new_birg;
762         irg       = be_get_birg_irg(birg);
763         execfreqs = birg->exec_freq;
764
765         /* TODO: extract some of the stuff from bechordal allocator, like
766          * statistics, time measurements, etc. and use them here too */
767
768         for (c = 0; c < n_cls; ++c) {
769                 cls = arch_env_get_reg_class(arch_env, c);
770                 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
771                         continue;
772
773                 stat_ev_ctx_push_str("bestraight_cls", cls->name);
774
775                 n_regs      = cls->n_regs;
776                 ignore_regs = bitset_malloc(n_regs);
777                 be_put_ignore_regs(birg, cls, ignore_regs);
778
779                 spill();
780
781                 /* verify schedule and register pressure */
782                 BE_TIMER_PUSH(t_verify);
783                 if (birg->main_env->options->vrfy_option == BE_CH_VRFY_WARN) {
784                         be_verify_schedule(birg);
785                         be_verify_register_pressure(birg, cls, irg);
786                 } else if (birg->main_env->options->vrfy_option == BE_CH_VRFY_ASSERT) {
787                         assert(be_verify_schedule(birg) && "Schedule verification failed");
788                         assert(be_verify_register_pressure(birg, cls, irg)
789                                 && "Register pressure verification failed");
790                 }
791                 BE_TIMER_POP(t_verify);
792
793                 BE_TIMER_PUSH(t_ra_color);
794                 be_straight_alloc_cls();
795                 BE_TIMER_POP(t_ra_color);
796
797                 bitset_free(ignore_regs);
798
799                 /* TODO: dump intermediate results */
800
801                 stat_ev_ctx_pop("bestraight_cls");
802         }
803
804         obstack_free(&obst, NULL);
805 }
806
807 static be_ra_t be_ra_straight = {
808         be_straight_alloc,
809 };
810
811 void be_init_straight_alloc(void)
812 {
813         FIRM_DBG_REGISTER(dbg, "firm.be.straightalloc");
814
815         be_register_allocator("straight", &be_ra_straight);
816 }
817
818 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_straight_alloc);