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