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