d90b47fee745e89e7f9b7aeffd93c8d0255084a9
[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 #include "raw_bitset.h"
67
68 #include "beabi.h"
69 #include "bechordal_t.h"
70 #include "be.h"
71 #include "beirg.h"
72 #include "belive_t.h"
73 #include "bemodule.h"
74 #include "benode_t.h"
75 #include "bera.h"
76 #include "besched.h"
77 #include "bespill.h"
78 #include "bespillutil.h"
79 #include "beverify.h"
80
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 unsigned                    *normal_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                                 op_info->prefs[r] += info->prefs[r] * factor;
326                         }
327                 }
328         }
329 }
330
331 /**
332  * Walker: Runs an a block calculates the preferences for any
333  * node and every register from the considered register class.
334  */
335 static void analyze_block(ir_node *block, void *data)
336 {
337         float         weight = get_block_execfreq(execfreqs, block);
338         ir_nodeset_t  live_nodes;
339         ir_node      *node;
340         (void) data;
341
342         ir_nodeset_init(&live_nodes);
343         be_liveness_end_of_block(lv, cls, block, &live_nodes);
344
345         sched_foreach_reverse(block, node) {
346                 allocation_info_t *info;
347                 int                i;
348                 int                arity;
349
350                 if (is_Phi(node))
351                         break;
352
353                 /* TODO give/take penalties for should_be_same/different) */
354                 check_defs(&live_nodes, weight, node);
355
356                 /* mark last uses */
357                 arity = get_irn_arity(node);
358
359                 /* the allocation info node currently only uses 1 unsigned value
360                    to mark last used inputs. So we will fail for a node with more than
361                    32 inputs. */
362                 if (arity >= (int) sizeof(unsigned) * 8) {
363                         panic("Node with more than %d inputs not supported yet",
364                                         (int) sizeof(unsigned) * 8);
365                 }
366
367                 info = get_allocation_info(node);
368                 for (i = 0; i < arity; ++i) {
369                         ir_node *op = get_irn_n(node, i);
370                         if (!arch_irn_consider_in_reg_alloc(cls, op))
371                                 continue;
372
373                         /* last usage of a value? */
374                         if (!ir_nodeset_contains(&live_nodes, op)) {
375                                 rbitset_set(&info->last_uses, i);
376                         }
377                 }
378
379                 be_liveness_transfer(cls, node, &live_nodes);
380
381                 /* update weights based on usage constraints */
382                 for (i = 0; i < arity; ++i) {
383                         const arch_register_req_t *req;
384                         const unsigned            *limited;
385                         ir_node                   *op = get_irn_n(node, i);
386
387                         if (!arch_irn_consider_in_reg_alloc(cls, op))
388                                 continue;
389
390                         req = arch_get_register_req(node, i);
391                         if (!(req->type & arch_register_req_type_limited))
392                                 continue;
393
394                         /* TODO: give penalties to neighbors for precolored nodes! */
395
396                         limited = req->limited;
397                         give_penalties_for_limits(&live_nodes, weight * USE_FACTOR, limited,
398                                                   op);
399                 }
400         }
401
402         ir_nodeset_destroy(&live_nodes);
403 }
404
405 /**
406  * Assign register reg to the given node.
407  *
408  * @param node  the node
409  * @param reg   the register
410  */
411 static void use_reg(ir_node *node, const arch_register_t *reg)
412 {
413         unsigned r = arch_register_get_index(reg);
414         assignments[r].value = node;
415         arch_set_irn_register(node, reg);
416 }
417
418 /**
419  * Compare two register preferences in decreasing order.
420  */
421 static int compare_reg_pref(const void *e1, const void *e2)
422 {
423         const reg_pref_t *rp1 = (const reg_pref_t*) e1;
424         const reg_pref_t *rp2 = (const reg_pref_t*) e2;
425         if (rp1->pref < rp2->pref)
426                 return 1;
427         if (rp1->pref > rp2->pref)
428                 return -1;
429         return 0;
430 }
431
432 static void fill_sort_candidates(reg_pref_t *regprefs,
433                                  const allocation_info_t *info)
434 {
435         unsigned r;
436
437         for (r = 0; r < n_regs; ++r) {
438                 float pref = info->prefs[r];
439                 regprefs[r].num  = r;
440                 regprefs[r].pref = pref;
441         }
442         /* TODO: use a stable sort here to avoid unnecessary register jumping */
443         qsort(regprefs, n_regs, sizeof(regprefs[0]), compare_reg_pref);
444 }
445
446 /**
447  * Determine and assign a register for node @p node
448  */
449 static void assign_reg(const ir_node *block, ir_node *node)
450 {
451         const arch_register_t     *reg;
452         allocation_info_t         *info;
453         const arch_register_req_t *req;
454         reg_pref_t                *reg_prefs;
455         ir_node                   *in_node;
456         unsigned                   i;
457         const unsigned            *allowed_regs;
458
459         assert(arch_irn_consider_in_reg_alloc(cls, node));
460
461         /* preassigned register? */
462         reg = arch_get_irn_register(node);
463         if (reg != NULL) {
464                 DB((dbg, LEVEL_2, "Preassignment %+F -> %s\n", node, reg->name));
465                 use_reg(node, reg);
466                 return;
467         }
468
469         /* give should_be_same boni */
470         info = get_allocation_info(node);
471         req  = arch_get_register_req_out(node);
472
473         in_node = skip_Proj(node);
474         if (req->type & arch_register_req_type_should_be_same) {
475                 float weight = get_block_execfreq(execfreqs, block);
476                 int   arity  = get_irn_arity(in_node);
477                 int   i;
478
479                 assert(arity <= (int) sizeof(req->other_same) * 8);
480                 for (i = 0; i < arity; ++i) {
481                         ir_node               *in;
482                         const arch_register_t *reg;
483                         unsigned               r;
484                         if (!rbitset_is_set(&req->other_same, i))
485                                 continue;
486
487                         in  = get_irn_n(in_node, i);
488                         reg = arch_get_irn_register(in);
489                         assert(reg != NULL);
490                         r = arch_register_get_index(reg);
491                         info->prefs[r] += weight * AFF_SHOULD_BE_SAME;
492                 }
493         }
494
495         DB((dbg, LEVEL_2, "Candidates for %+F:", node));
496         reg_prefs = alloca(n_regs * sizeof(reg_prefs[0]));
497         fill_sort_candidates(reg_prefs, info);
498         for (i = 0; i < n_regs; ++i) {
499                 unsigned               num = reg_prefs[i].num;
500                 const arch_register_t *reg = arch_register_for_index(cls, num);
501                 DB((dbg, LEVEL_2, " %s(%f)", reg->name, reg_prefs[i].pref));
502         }
503         DB((dbg, LEVEL_2, "\n"));
504
505         allowed_regs = normal_regs;
506         if (req->type & arch_register_req_type_limited) {
507                 allowed_regs = req->limited;
508         }
509
510         for (i = 0; i < n_regs; ++i) {
511                 unsigned r = reg_prefs[i].num;
512                 /* already used?
513                    TODO: It might be better to copy the value occupying the register
514                    around here instead of trying the next one, find out when... */
515                 if (assignments[r].value != NULL)
516                         continue;
517                 if (!rbitset_is_set(allowed_regs, r))
518                         continue;
519                 reg = arch_register_for_index(cls, r);
520                 DB((dbg, LEVEL_2, "Assign %+F -> %s\n", node, reg->name));
521                 use_reg(node, reg);
522                 break;
523         }
524 }
525
526 static void free_reg_of_value(ir_node *node)
527 {
528         assignment_t          *assignment;
529         const arch_register_t *reg;
530         unsigned               r;
531
532         if (!arch_irn_consider_in_reg_alloc(cls, node))
533                 return;
534
535         reg               = arch_get_irn_register(node);
536         r                 = arch_register_get_index(reg);
537         assignment        = &assignments[r];
538         /* assignment->value may be NULL if a value is used at 2 inputs
539            so it gets freed twice. */
540         assert(assignment->value == node || assignment->value == NULL);
541         assignment->value = NULL;
542 }
543
544 /**
545  * Add an permutation in front of a node and change the assignments
546  * due to this permutation.
547  *
548  * To understand this imagine a permutation like this:
549  *
550  * 1 -> 2
551  * 2 -> 3
552  * 3 -> 1, 5
553  * 4 -> 6
554  * 5
555  * 6
556  * 7 -> 7
557  *
558  * First we count how many destinations a single value has. At the same time
559  * we can be sure that each destination register has at most 1 source register
560  * (it can have 0 which means we don't care what value is in it).
561  * We ignore all fullfilled permuations (like 7->7)
562  * In a first pass we create as much copy instructions as possible as they
563  * are generally cheaper than exchanges. We do this by counting into how many
564  * destinations a register has to be copied (in the example it's 2 for register
565  * 3, or 1 for the registers 1,2,4 and 7).
566  * We can then create a copy into every destination register when the usecount
567  * of that register is 0 (= noone else needs the value in the register).
568  *
569  * After this step we should have cycles left. We implement a cyclic permutation
570  * of n registers with n-1 transpositions.
571  *
572  * @param live_nodes   the set of live nodes, updated due to live range split
573  * @param before       the node before we add the permutation
574  * @param permutation  the permutation array indices are the destination
575  *                     registers, the values in the array are the source
576  *                     registers.
577  */
578 static void permutate_values(ir_nodeset_t *live_nodes, ir_node *before,
579                              unsigned *permutation)
580 {
581         unsigned  *n_used = ALLOCANZ(unsigned, n_regs);
582         ir_node   *block;
583         unsigned   r;
584
585         /* determine how often each source register needs to be read */
586         for (r = 0; r < n_regs; ++r) {
587                 unsigned  old_reg = permutation[r];
588                 ir_node  *value;
589
590                 value = assignments[old_reg].value;
591                 if (value == NULL) {
592                         /* nothing to do here, reg is not live. Mark it as fixpoint
593                          * so we ignore it in the next steps */
594                         permutation[r] = r;
595                         continue;
596                 }
597
598                 ++n_used[old_reg];
599         }
600
601         block = get_nodes_block(before);
602
603         /* step1: create copies where immediately possible */
604         for (r = 0; r < n_regs; /* empty */) {
605                 ir_node *copy;
606                 ir_node *src;
607                 const arch_register_t *reg;
608                 unsigned               old_r = permutation[r];
609
610                 /* - no need to do anything for fixed points.
611                    - we can't copy if the value in the dest reg is still needed */
612                 if (old_r == r || n_used[r] > 0) {
613                         ++r;
614                         continue;
615                 }
616
617                 /* create a copy */
618                 src  = assignments[old_r].value;
619                 copy = be_new_Copy(cls, block, src);
620                 sched_add_before(before, copy);
621                 reg = arch_register_for_index(cls, r);
622                 DB((dbg, LEVEL_2, "Copy %+F (from %+F, before %+F) -> %s\n",
623                     copy, src, before, reg->name));
624                 mark_as_copy_of(copy, src);
625                 use_reg(copy, reg);
626
627                 if (live_nodes != NULL) {
628                         ir_nodeset_insert(live_nodes, copy);
629                 }
630
631                 /* old register has 1 user less, permutation is resolved */
632                 assert(arch_register_get_index(arch_get_irn_register(src)) == old_r);
633                 permutation[r] = r;
634
635                 assert(n_used[old_r] > 0);
636                 --n_used[old_r];
637                 if (n_used[old_r] == 0) {
638                         if (live_nodes != NULL) {
639                                 ir_nodeset_remove(live_nodes, src);
640                         }
641                         free_reg_of_value(src);
642                 }
643
644                 /* advance or jump back (if this copy enabled another copy) */
645                 if (old_r < r && n_used[old_r] == 0) {
646                         r = old_r;
647                 } else {
648                         ++r;
649                 }
650         }
651
652         /* at this point we only have "cycles" left which we have to resolve with
653          * perm instructions
654          * TODO: if we have free registers left, then we should really use copy
655          * instructions for any cycle longer than 2 registers...
656          * (this is probably architecture dependent, there might be archs where
657          *  copies are preferable even for 2-cycles) */
658
659         /* create perms with the rest */
660         for (r = 0; r < n_regs; /* empty */) {
661                 const arch_register_t *reg;
662                 unsigned  old_r = permutation[r];
663                 unsigned  r2;
664                 ir_node  *in[2];
665                 ir_node  *perm;
666                 ir_node  *proj0;
667                 ir_node  *proj1;
668
669                 if (old_r == r) {
670                         ++r;
671                         continue;
672                 }
673
674                 /* we shouldn't have copies from 1 value to multiple destinations left*/
675                 assert(n_used[old_r] == 1);
676
677                 /* exchange old_r and r2; after that old_r is a fixed point */
678                 r2 = permutation[old_r];
679
680                 in[0] = assignments[r2].value;
681                 in[1] = assignments[old_r].value;
682                 perm = be_new_Perm(cls, block, 2, in);
683                 sched_add_before(before, perm);
684                 DB((dbg, LEVEL_2, "Perm %+F (perm %+F,%+F, before %+F)\n",
685                     perm, in[0], in[1], before));
686
687                 proj0 = new_r_Proj(block, perm, get_irn_mode(in[0]), 0);
688                 mark_as_copy_of(proj0, in[0]);
689                 reg = arch_register_for_index(cls, old_r);
690                 use_reg(proj0, reg);
691
692                 proj1 = new_r_Proj(block, perm, get_irn_mode(in[1]), 1);
693                 mark_as_copy_of(proj1, in[1]);
694                 reg = arch_register_for_index(cls, r2);
695                 use_reg(proj1, reg);
696
697                 /* 1 value is now in the correct register */
698                 permutation[old_r] = old_r;
699                 /* the source of r changed to r2 */
700                 permutation[r] = r2;
701
702                 /* if we have reached a fixpoint update data structures */
703                 if (live_nodes != NULL) {
704                         ir_nodeset_remove(live_nodes, in[0]);
705                         ir_nodeset_remove(live_nodes, in[1]);
706                         ir_nodeset_remove(live_nodes, proj0);
707                         ir_nodeset_insert(live_nodes, proj1);
708                 }
709         }
710
711 #ifdef DEBUG_libfirm
712         /* now we should only have fixpoints left */
713         for (r = 0; r < n_regs; ++r) {
714                 assert(permutation[r] == r);
715         }
716 #endif
717 }
718
719 /**
720  * Free regs for values last used.
721  *
722  * @param live_nodes   set of live nodes, will be updated
723  * @param node         the node to consider
724  */
725 static void free_last_uses(ir_nodeset_t *live_nodes, ir_node *node)
726 {
727         allocation_info_t *info      = get_allocation_info(node);
728         const unsigned    *last_uses = &info->last_uses;
729         int                arity     = get_irn_arity(node);
730         int                i;
731         for (i = 0; i < arity; ++i) {
732                 ir_node *op;
733
734                 /* check if one operand is the last use */
735                 if (!rbitset_is_set(last_uses, i))
736                         continue;
737
738                 op = get_irn_n(node, i);
739                 free_reg_of_value(op);
740                 ir_nodeset_remove(live_nodes, op);
741         }
742 }
743
744 /**
745  * change inputs of a node to the current value (copies/perms)
746  */
747 static void rewire_inputs(ir_node *node)
748 {
749         int i;
750         int arity = get_irn_arity(node);
751
752         for (i = 0; i < arity; ++i) {
753                 ir_node           *op = get_irn_n(node, i);
754                 allocation_info_t *info;
755
756                 if (!arch_irn_consider_in_reg_alloc(cls, op))
757                         continue;
758
759                 info = get_allocation_info(op);
760                 info = get_allocation_info(info->original_value);
761                 if (info->current_value != op) {
762                         set_irn_n(node, i, info->current_value);
763                 }
764         }
765 }
766
767 /**
768  * Create a bitset of registers occupied with value living through an
769  * instruction
770  */
771 static void determine_live_through_regs(unsigned *bitset, ir_node *node)
772 {
773         const allocation_info_t *info = get_allocation_info(node);
774         unsigned r;
775         int i;
776         int arity;
777
778         /* mark all used registers as potentially live-through */
779         for (r = 0; r < n_regs; ++r) {
780                 const assignment_t *assignment = &assignments[r];
781                 if (assignment->value == NULL)
782                         continue;
783                 if (!rbitset_is_set(normal_regs, r))
784                         continue;
785
786                 rbitset_set(bitset, r);
787         }
788
789         /* remove registers of value dying at the instruction */
790         arity = get_irn_arity(node);
791         for (i = 0; i < arity; ++i) {
792                 ir_node               *op;
793                 const arch_register_t *reg;
794
795                 if (!rbitset_is_set(&info->last_uses, i))
796                         continue;
797
798                 op  = get_irn_n(node, i);
799                 reg = arch_get_irn_register(op);
800                 rbitset_clear(bitset, arch_register_get_index(reg));
801         }
802 }
803
804 /**
805  * Enforce constraints at a node by live range splits.
806  *
807  * @param  live_nodes  the set of live nodes, might be changed
808  * @param  node        the current node
809  */
810 static void enforce_constraints(ir_nodeset_t *live_nodes, ir_node *node)
811 {
812         int arity = get_irn_arity(node);
813         int i, dummy, res;
814         hungarian_problem_t *bp;
815         unsigned l, r;
816         unsigned *assignment;
817
818         /* construct a list of register occupied by live-through values */
819         unsigned *live_through_regs = NULL;
820         unsigned *output_regs       = NULL;
821
822         /* see if any use constraints are not met */
823         bool good = true;
824         for (i = 0; i < arity; ++i) {
825                 ir_node                   *op = get_irn_n(node, i);
826                 const arch_register_t     *reg;
827                 const arch_register_req_t *req;
828                 const unsigned            *limited;
829                 unsigned                  r;
830
831                 if (!arch_irn_consider_in_reg_alloc(cls, op))
832                         continue;
833
834                 /* are there any limitations for the i'th operand? */
835                 req = arch_get_register_req(node, i);
836                 if (!(req->type & arch_register_req_type_limited))
837                         continue;
838
839                 limited = req->limited;
840                 reg     = arch_get_irn_register(op);
841                 r       = arch_register_get_index(reg);
842                 if (!rbitset_is_set(limited, r)) {
843                         /* found an assignment outside the limited set */
844                         good = false;
845                         break;
846                 }
847         }
848
849         /* is any of the live-throughs using a constrained output register? */
850         if (get_irn_mode(node) == mode_T) {
851                 const ir_edge_t *edge;
852
853                 foreach_out_edge(node, edge) {
854                         ir_node *proj = get_edge_src_irn(edge);
855                         const arch_register_req_t *req;
856
857                         if (!arch_irn_consider_in_reg_alloc(cls, proj))
858                                 continue;
859
860                         req = arch_get_register_req_out(proj);
861                         if (!(req->type & arch_register_req_type_limited))
862                                 continue;
863
864                         if (live_through_regs == NULL) {
865                                 rbitset_alloca(live_through_regs, n_regs);
866                                 determine_live_through_regs(live_through_regs, node);
867
868                                 rbitset_alloca(output_regs, n_regs);
869                         }
870
871                         rbitset_or(output_regs, req->limited, n_regs);
872                         if (rbitsets_have_common(req->limited, live_through_regs, n_regs)) {
873                                 good = false;
874                         }
875                 }
876         } else {
877                 if (arch_irn_consider_in_reg_alloc(cls, node)) {
878                         const arch_register_req_t *req = arch_get_register_req_out(node);
879                         if (req->type & arch_register_req_type_limited) {
880                                 rbitset_alloca(live_through_regs, n_regs);
881                                 determine_live_through_regs(live_through_regs, node);
882                                 if (rbitsets_have_common(req->limited, live_through_regs, n_regs)) {
883                                         good = false;
884
885                                         rbitset_alloca(output_regs, n_regs);
886                                         rbitset_or(output_regs, req->limited, n_regs);
887                                 }
888                         }
889                 }
890         }
891
892         if (good)
893                 return;
894
895         /* create these arrays if we haven't yet */
896         if (output_regs == NULL) {
897                 if (live_through_regs == NULL) {
898                         rbitset_alloca(live_through_regs, n_regs);
899                 }
900                 rbitset_alloca(output_regs, n_regs);
901         }
902
903         /* at this point we have to construct a bipartite matching problem to see
904          * which values should go to which registers
905          * Note: We're building the matrix in "reverse" - source registers are
906          *       right, destinations at l because this will produce the solution
907          *       in the format required for permutate_values.
908          */
909         bp = hungarian_new(n_regs, n_regs, HUNGARIAN_MATCH_PERFECT);
910
911         /* add all combinations, then remove not allowed ones */
912         for (l = 0; l < n_regs; ++l) {
913                 if (!rbitset_is_set(normal_regs, l)) {
914                         hungarian_add(bp, l, l, 1);
915                         continue;
916                 }
917
918                 for (r = 0; r < n_regs; ++r) {
919                         if (!rbitset_is_set(normal_regs, r))
920                                 continue;
921                         /* livethrough values may not use constrainted output registers */
922                         if (rbitset_is_set(live_through_regs, l)
923                                         && rbitset_is_set(output_regs, r))
924                                 continue;
925
926                         hungarian_add(bp, r, l, l == r ? 9 : 8);
927                 }
928         }
929
930         for (i = 0; i < arity; ++i) {
931                 ir_node                   *op = get_irn_n(node, i);
932                 const arch_register_t     *reg;
933                 const arch_register_req_t *req;
934                 const unsigned            *limited;
935                 unsigned                   current_reg;
936
937                 if (!arch_irn_consider_in_reg_alloc(cls, op))
938                         continue;
939
940                 req = arch_get_register_req(node, i);
941                 if (!(req->type & arch_register_req_type_limited))
942                         continue;
943
944                 limited     = req->limited;
945                 reg         = arch_get_irn_register(op);
946                 current_reg = arch_register_get_index(reg);
947                 for (r = 0; r < n_regs; ++r) {
948                         if (rbitset_is_set(limited, r))
949                                 continue;
950                         hungarian_remv(bp, r, current_reg);
951                 }
952         }
953
954         //hungarian_print_costmatrix(bp, 1);
955         hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
956
957         assignment = ALLOCAN(unsigned, n_regs);
958         res = hungarian_solve(bp, (int*) assignment, &dummy, 0);
959         assert(res == 0);
960
961 #if 0
962         fprintf(stderr, "Swap result:");
963         for (i = 0; i < (int) n_regs; ++i) {
964                 fprintf(stderr, " %d", assignment[i]);
965         }
966         fprintf(stderr, "\n");
967 #endif
968
969         hungarian_free(bp);
970
971         permutate_values(live_nodes, node, assignment);
972 }
973
974 /** test wether a node @p n is a copy of the value of node @p of */
975 static bool is_copy_of(ir_node *value, ir_node *test_value)
976 {
977         allocation_info_t *test_info;
978         allocation_info_t *info;
979
980         if (value == test_value)
981                 return true;
982
983         info      = get_allocation_info(value);
984         test_info = get_allocation_info(test_value);
985         return test_info->original_value == info->original_value;
986 }
987
988 /**
989  * find a value in the end-assignment of a basic block
990  * @returns the index into the assignment array if found
991  *          -1 if not found
992  */
993 static int find_value_in_block_info(block_info_t *info, ir_node *value)
994 {
995         unsigned      r;
996         assignment_t *assignments = info->assignments;
997         for (r = 0; r < n_regs; ++r) {
998                 const assignment_t *assignment = &assignments[r];
999                 ir_node            *a_value    = assignment->value;
1000
1001                 if (a_value == NULL)
1002                         continue;
1003                 if (is_copy_of(a_value, value))
1004                         return (int) r;
1005         }
1006
1007         return -1;
1008 }
1009
1010 /**
1011  * Create the necessary permutations at the end of a basic block to fullfill
1012  * the register assignment for phi-nodes in the next block
1013  */
1014 static void add_phi_permutations(ir_node *block, int p)
1015 {
1016         unsigned  r;
1017         unsigned *permutation;
1018         assignment_t *old_assignments;
1019         bool      need_permutation;
1020         ir_node  *node;
1021         ir_node  *pred = get_Block_cfgpred_block(block, p);
1022
1023         block_info_t *pred_info = get_block_info(pred);
1024
1025         /* predecessor not processed yet? nothing to do */
1026         if (!pred_info->processed)
1027                 return;
1028
1029         permutation = ALLOCAN(unsigned, n_regs);
1030         for (r = 0; r < n_regs; ++r) {
1031                 permutation[r] = r;
1032         }
1033
1034         /* check phi nodes */
1035         need_permutation = false;
1036         node = sched_first(block);
1037         for ( ; is_Phi(node); node = sched_next(node)) {
1038                 const arch_register_t *reg;
1039                 int                    regn;
1040                 int                    a;
1041                 ir_node               *op;
1042
1043                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1044                         continue;
1045
1046                 op = get_Phi_pred(node, p);
1047                 if (!arch_irn_consider_in_reg_alloc(cls, op))
1048                         continue;
1049
1050                 a  = find_value_in_block_info(pred_info, op);
1051                 assert(a >= 0);
1052
1053                 reg  = arch_get_irn_register(node);
1054                 regn = arch_register_get_index(reg);
1055                 if (regn != a) {
1056                         permutation[regn] = a;
1057                         need_permutation  = true;
1058                 }
1059         }
1060
1061         if (need_permutation) {
1062                 /* permutate values at end of predecessor */
1063                 old_assignments = assignments;
1064                 assignments     = pred_info->assignments;
1065                 permutate_values(NULL, be_get_end_of_block_insertion_point(pred),
1066                                                  permutation);
1067                 assignments = old_assignments;
1068         }
1069
1070         /* change phi nodes to use the copied values */
1071         node = sched_first(block);
1072         for ( ; is_Phi(node); node = sched_next(node)) {
1073                 int      a;
1074                 ir_node *op;
1075
1076                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1077                         continue;
1078
1079                 /* we have permutated all values into the correct registers so we can
1080                    simply query which value occupies the phis register in the
1081                    predecessor */
1082                 a  = arch_register_get_index(arch_get_irn_register(node));
1083                 op = pred_info->assignments[a].value;
1084                 set_Phi_pred(node, p, op);
1085         }
1086 }
1087
1088 static void handle_phi_prefs(ir_node *phi)
1089 {
1090         int i;
1091         int arity = get_irn_arity(phi);
1092         ir_node           *block = get_nodes_block(phi);
1093         allocation_info_t *info  = get_allocation_info(phi);
1094
1095         for (i = 0; i < arity; ++i) {
1096                 ir_node               *op  = get_irn_n(phi, i);
1097                 const arch_register_t *reg = arch_get_irn_register(op);
1098                 ir_node               *pred;
1099                 float                  weight;
1100                 unsigned               r;
1101
1102                 if (reg == NULL)
1103                         continue;
1104
1105                 /* give bonus for already assigned register */
1106                 pred   = get_Block_cfgpred_block(block, i);
1107                 weight = get_block_execfreq(execfreqs, pred);
1108                 r      = arch_register_get_index(reg);
1109                 info->prefs[r] += weight * AFF_PHI;
1110         }
1111 }
1112
1113 /**
1114  * Walker: assign registers to all nodes of a block that
1115  * need registers from the currently considered register class.
1116  */
1117 static void allocate_coalesce_block(ir_node *block, void *data)
1118 {
1119         int                    i;
1120         ir_nodeset_t           live_nodes;
1121         ir_nodeset_iterator_t  iter;
1122         ir_node               *node, *start;
1123         int                    n_preds;
1124         block_info_t          *block_info;
1125         block_info_t         **pred_block_infos;
1126         ir_node              **phi_ins;
1127
1128         (void) data;
1129         DB((dbg, LEVEL_2, "* Block %+F\n", block));
1130
1131         /* clear assignments */
1132         block_info  = get_block_info(block);
1133         assignments = block_info->assignments;
1134
1135         ir_nodeset_init(&live_nodes);
1136
1137         /* gather regalloc infos of predecessor blocks */
1138         n_preds             = get_Block_n_cfgpreds(block);
1139         pred_block_infos    = ALLOCAN(block_info_t*, n_preds);
1140         for (i = 0; i < n_preds; ++i) {
1141                 ir_node      *pred      = get_Block_cfgpred_block(block, i);
1142                 block_info_t *pred_info = get_block_info(pred);
1143                 pred_block_infos[i]     = pred_info;
1144         }
1145
1146         phi_ins = ALLOCAN(ir_node*, n_preds);
1147
1148         /* collect live-in nodes and preassigned values */
1149         be_lv_foreach(lv, block, be_lv_state_in, i) {
1150                 const arch_register_t *reg;
1151                 int                    p;
1152                 bool                   need_phi = false;
1153
1154                 node = be_lv_get_irn(lv, block, i);
1155                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1156                         continue;
1157
1158                 /* check all predecessors for this value, if it is not everywhere the
1159                    same or unknown then we have to construct a phi
1160                    (we collect the potential phi inputs here) */
1161                 for (p = 0; p < n_preds; ++p) {
1162                         block_info_t *pred_info = pred_block_infos[p];
1163
1164                         if (!pred_info->processed) {
1165                                 /* use node for now, it will get fixed later */
1166                                 phi_ins[p] = node;
1167                                 need_phi   = true;
1168                         } else {
1169                                 int a = find_value_in_block_info(pred_info, node);
1170
1171                                 /* must live out of predecessor */
1172                                 assert(a >= 0);
1173                                 phi_ins[p] = pred_info->assignments[a].value;
1174                                 /* different value from last time? then we need a phi */
1175                                 if (p > 0 && phi_ins[p-1] != phi_ins[p]) {
1176                                         need_phi = true;
1177                                 }
1178                         }
1179                 }
1180
1181                 if (need_phi) {
1182                         ir_mode                   *mode = get_irn_mode(node);
1183                         const arch_register_req_t *req  = get_default_req_current_cls();
1184                         ir_node                   *phi;
1185
1186                         phi = new_r_Phi(block, n_preds, phi_ins, mode);
1187                         be_set_phi_reg_req(phi, req);
1188
1189                         DB((dbg, LEVEL_3, "Create Phi %+F (for %+F)\n", phi, node));
1190
1191                         mark_as_copy_of(phi, node);
1192                         sched_add_after(block, phi);
1193
1194                         node = phi;
1195                 } else {
1196                         allocation_info_t *info = get_allocation_info(node);
1197                         info->current_value = phi_ins[0];
1198
1199                         /* Grab 1 of the inputs we constructed (might not be the same as
1200                          * "node" as we could see the same copy of the value in all
1201                          * predecessors */
1202                         node = phi_ins[0];
1203                 }
1204
1205                 /* if the node already has a register assigned use it */
1206                 reg = arch_get_irn_register(node);
1207                 if (reg != NULL) {
1208                         /* TODO: consult pred-block infos here. The value could be copied
1209                            away in some/all predecessor blocks. We need to construct
1210                            phi-nodes in this case.
1211                            We even need to construct some Phi_0 like constructs in cases
1212                            where the predecessor allocation is not determined yet. */
1213                         use_reg(node, reg);
1214                 }
1215
1216                 /* remember that this node is live at the beginning of the block */
1217                 ir_nodeset_insert(&live_nodes, node);
1218         }
1219
1220         /* handle phis... */
1221         node = sched_first(block);
1222         for ( ; is_Phi(node); node = sched_next(node)) {
1223                 const arch_register_t *reg;
1224
1225                 if (!arch_irn_consider_in_reg_alloc(cls, node))
1226                         continue;
1227
1228                 /* fill in regs already assigned */
1229                 reg = arch_get_irn_register(node);
1230                 if (reg != NULL) {
1231                         use_reg(node, reg);
1232                 } else {
1233                         /* TODO: give boni for registers already assigned at the
1234                            predecessors */
1235                         handle_phi_prefs(node);
1236                         assign_reg(block, node);
1237                 }
1238         }
1239         start = node;
1240
1241         /* assign regs for live-in values */
1242         foreach_ir_nodeset(&live_nodes, node, iter) {
1243                 const arch_register_t *reg = arch_get_irn_register(node);
1244                 if (reg != NULL)
1245                         continue;
1246
1247                 assign_reg(block, node);
1248         }
1249
1250         /* assign instructions in the block */
1251         for (node = start; !sched_is_end(node); node = sched_next(node)) {
1252
1253                 rewire_inputs(node);
1254
1255                 /* enforce use constraints */
1256                 enforce_constraints(&live_nodes, node);
1257
1258                 rewire_inputs(node);
1259
1260                 /* free registers of values last used at this instruction */
1261                 free_last_uses(&live_nodes, node);
1262
1263                 /* assign output registers */
1264                 /* TODO: 2 phases: first: pre-assigned ones, 2nd real regs */
1265                 if (get_irn_mode(node) == mode_T) {
1266                         const ir_edge_t *edge;
1267                         foreach_out_edge(node, edge) {
1268                                 ir_node *proj = get_edge_src_irn(edge);
1269                                 if (!arch_irn_consider_in_reg_alloc(cls, proj))
1270                                         continue;
1271                                 assign_reg(block, proj);
1272                         }
1273                 } else if (arch_irn_consider_in_reg_alloc(cls, node)) {
1274                         assign_reg(block, node);
1275                 }
1276         }
1277
1278         ir_nodeset_destroy(&live_nodes);
1279         assignments = NULL;
1280
1281         block_info->processed = true;
1282
1283         /* permutate values at end of predecessor blocks in case of phi-nodes */
1284         if (n_preds > 1) {
1285                 int p;
1286                 for (p = 0; p < n_preds; ++p) {
1287                         add_phi_permutations(block, p);
1288                 }
1289         }
1290
1291         /* if we have exactly 1 successor then we might be able to produce phi
1292            copies now */
1293         if (get_irn_n_edges_kind(block, EDGE_KIND_BLOCK) == 1) {
1294                 const ir_edge_t *edge
1295                         = get_irn_out_edge_first_kind(block, EDGE_KIND_BLOCK);
1296                 ir_node      *succ      = get_edge_src_irn(edge);
1297                 int           p         = get_edge_src_pos(edge);
1298                 block_info_t *succ_info = get_block_info(succ);
1299
1300                 if (succ_info->processed) {
1301                         add_phi_permutations(succ, p);
1302                 }
1303         }
1304 }
1305
1306 /**
1307  * Run the register allocator for the current register class.
1308  */
1309 static void be_straight_alloc_cls(void)
1310 {
1311         lv = be_assure_liveness(birg);
1312         be_liveness_assure_sets(lv);
1313         be_liveness_assure_chk(lv);
1314
1315         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
1316         inc_irg_visited(irg);
1317
1318         DB((dbg, LEVEL_2, "=== Allocating registers of %s ===\n", cls->name));
1319
1320         irg_block_walk_graph(irg, NULL, analyze_block, NULL);
1321         /* we need some dominance pre-order walk to ensure we see all
1322          *  definitions/create copies before we encounter their users */
1323         dom_tree_walk_irg(irg, allocate_coalesce_block, NULL, NULL);
1324
1325         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
1326 }
1327
1328 static void dump(int mask, ir_graph *irg, const char *suffix,
1329                  void (*dumper)(ir_graph *, const char *))
1330 {
1331         if(birg->main_env->options->dump_flags & mask)
1332                 be_dump(irg, suffix, dumper);
1333 }
1334
1335 /**
1336  * Run the spiller on the current graph.
1337  */
1338 static void spill(void)
1339 {
1340         /* make sure all nodes show their real register pressure */
1341         BE_TIMER_PUSH(t_ra_constr);
1342         be_pre_spill_prepare_constr(birg, cls);
1343         BE_TIMER_POP(t_ra_constr);
1344
1345         dump(DUMP_RA, irg, "-spillprepare", dump_ir_block_graph_sched);
1346
1347         /* spill */
1348         BE_TIMER_PUSH(t_ra_spill);
1349         be_do_spill(birg, cls);
1350         BE_TIMER_POP(t_ra_spill);
1351
1352         BE_TIMER_PUSH(t_ra_spill_apply);
1353         check_for_memory_operands(irg);
1354         BE_TIMER_POP(t_ra_spill_apply);
1355
1356         dump(DUMP_RA, irg, "-spill", dump_ir_block_graph_sched);
1357 }
1358
1359 /**
1360  * The straight register allocator for a whole procedure.
1361  */
1362 static void be_straight_alloc(be_irg_t *new_birg)
1363 {
1364         const arch_env_t *arch_env = new_birg->main_env->arch_env;
1365         int   n_cls                = arch_env_get_n_reg_class(arch_env);
1366         int   c;
1367
1368         obstack_init(&obst);
1369
1370         birg      = new_birg;
1371         irg       = be_get_birg_irg(birg);
1372         execfreqs = birg->exec_freq;
1373
1374         /* TODO: extract some of the stuff from bechordal allocator, like
1375          * statistics, time measurements, etc. and use them here too */
1376
1377         for (c = 0; c < n_cls; ++c) {
1378                 cls             = arch_env_get_reg_class(arch_env, c);
1379                 default_cls_req = NULL;
1380                 if (arch_register_class_flags(cls) & arch_register_class_flag_manual_ra)
1381                         continue;
1382
1383                 stat_ev_ctx_push_str("regcls", cls->name);
1384
1385                 n_regs      = arch_register_class_n_regs(cls);
1386                 normal_regs = rbitset_malloc(n_regs);
1387                 be_abi_set_non_ignore_regs(birg->abi, cls, normal_regs);
1388
1389                 spill();
1390
1391                 /* verify schedule and register pressure */
1392                 BE_TIMER_PUSH(t_verify);
1393                 if (birg->main_env->options->vrfy_option == BE_VRFY_WARN) {
1394                         be_verify_schedule(birg);
1395                         be_verify_register_pressure(birg, cls, irg);
1396                 } else if (birg->main_env->options->vrfy_option == BE_VRFY_ASSERT) {
1397                         assert(be_verify_schedule(birg) && "Schedule verification failed");
1398                         assert(be_verify_register_pressure(birg, cls, irg)
1399                                 && "Register pressure verification failed");
1400                 }
1401                 BE_TIMER_POP(t_verify);
1402
1403                 BE_TIMER_PUSH(t_ra_color);
1404                 be_straight_alloc_cls();
1405                 BE_TIMER_POP(t_ra_color);
1406
1407                 /* we most probably constructed new Phis so liveness info is invalid
1408                  * now */
1409                 /* TODO: test liveness_introduce */
1410                 be_liveness_invalidate(lv);
1411                 free(normal_regs);
1412
1413                 stat_ev_ctx_pop("regcls");
1414         }
1415
1416         BE_TIMER_PUSH(t_ra_spill_apply);
1417         be_abi_fix_stack_nodes(birg->abi);
1418         BE_TIMER_POP(t_ra_spill_apply);
1419
1420         BE_TIMER_PUSH(t_verify);
1421         if (birg->main_env->options->vrfy_option == BE_VRFY_WARN) {
1422                 be_verify_register_allocation(birg);
1423         } else if (birg->main_env->options->vrfy_option == BE_VRFY_ASSERT) {
1424                 assert(be_verify_register_allocation(birg)
1425                                 && "Register allocation invalid");
1426         }
1427         BE_TIMER_POP(t_verify);
1428
1429         obstack_free(&obst, NULL);
1430 }
1431
1432 /**
1433  * Initializes this module.
1434  */
1435 void be_init_straight_alloc(void)
1436 {
1437         static be_ra_t be_ra_straight = {
1438                 be_straight_alloc,
1439         };
1440
1441         FIRM_DBG_REGISTER(dbg, "firm.be.straightalloc");
1442
1443         be_register_allocator("straight", &be_ra_straight);
1444 }
1445
1446 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_straight_alloc);