4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
7 * Implementation of the RA-Interface for an external, (non-SSA) register allocator.
9 * The external register allocator is a program:
10 * PROG -i INPUTFILE -o OUTPUTFILE
12 * 1) Input file defines the interference graph
13 * 2) Output file contains the instructions to perform
18 ----------------------
20 inputfile ::= regs nodes interf affinities .
22 regs ::= 'regs' regcount . // Anzahl der register (0..regcount-1), die zur Verfuegung stehen
24 nodes ::= 'nodes' '{' node* '}' . // All nodes in the graph
27 | node-info '<' reg-nr '>' . // Reg-nr is present in case of constraints
29 node-info ::= node-nr spill-costs .
31 interf ::= 'interferences' '{' i-edge* '}' . // Interference edges of the graph
33 i-edge ::= '(' node-nr ',' node-nr ')' .
35 affinities ::= 'affinities' '{' a-edge* '}' . // Affinity edges of the graph
37 a-edge ::= '(' node-nr ',' node-nr ',' weight ')' .
40 weight, regcount, node-nr ::= int32 .
41 spill-costs ::= int32 . // negative spill costs indicate unspillable
43 The output file format
44 -----------------------
46 outputfile ::= spills | allocs .
48 spills ::= 'spills' node-nr+ .
50 allocs ::= 'allocs' alloc* .
52 alloc ::= node-nr reg-nr .
55 ******** End of file format docu ********/
71 #include <libcore/lc_opts.h>
72 #include <libcore/lc_opts_enum.h>
80 #include "irprintf_t.h"
82 #include "irgraph_t.h"
84 #include "iredges_t.h"
88 #include "beraextern.h"
98 typedef struct _var_info_t var_info_t;
101 * Environment with all the needed stuff
103 typedef struct _be_raext_env_t {
104 firm_dbg_module_t *dbg;
106 const arch_register_class_t *cls;
108 dom_front_info_t *dom_info;
110 FILE *f; /**< file handle used for out- and input file */
111 set *vars; /**< contains all var_info_t */
112 int n_cls_vars; /**< length of the array cls_vars */
113 var_info_t **cls_vars; /**< only the var_infos for current cls. needed for double iterating */
118 /******************************************************************************
121 | |__| | ___| |_ __ ___ _ __ ___
122 | __ |/ _ \ | '_ \ / _ \ '__/ __|
123 | | | | __/ | |_) | __/ | \__ \
124 |_| |_|\___|_| .__/ \___|_| |___/
127 *****************************************************************************/
130 #define pset_foreach(pset, irn) for(irn=pset_first(pset); irn; irn=pset_next(pset))
131 #define set_foreach(set, e) for(e=set_first(set); e; e=set_next(set))
134 * Checks if _the_ result of the irn belongs to the
135 * current register class (raenv->cls)
136 * NOTE: Only the first result is checked.
138 #define is_res_in_reg_class(irn) arch_irn_has_reg_class(raenv->aenv, irn, -1, raenv->cls)
140 static INLINE ir_node *get_first_non_phi(pset *s) {
149 assert(0 && "There must be a non-phi-irn in this");
153 static INLINE ir_node *get_first_phi(pset *s) {
162 assert(0 && "There must be a phi in this");
166 static int get_loop_weight(ir_node *irn) {
168 ir_loop *loop = get_irn_loop(get_nodes_block(irn));
171 int d = get_loop_depth(loop);
177 #define get_const_weight(irn) (1)
179 #define get_spill_weight(irn) get_loop_weight(irn)
180 #define get_reload_weight(irn) get_loop_weight(irn)
181 #define get_affinity_weight(irn) get_loop_weight(irn)
183 /******************************************************************************
185 / ____| | | / ____| (_)
186 | | ___ _ __ ___| |_ _ __ | | ___ _ __ _ ___ ___
187 | | / _ \| '_ \/ __| __| '__| | | / _ \| '_ \| |/ _ \/ __|
188 | |___| (_) | | | \__ \ |_| | | |___| (_) | |_) | | __/\__ \
189 \_____\___/|_| |_|___/\__|_| \_____\___/| .__/|_|\___||___/
192 *****************************************************************************/
194 static void handle_constraints_walker(ir_node *irn, void *env) {
195 be_raext_env_t *raenv = env;
196 arch_register_req_t req;
199 /* handle output constraints
200 * user -> irn becomes user -> cpy -> irn
202 arch_get_register_req(raenv->aenv, &req, irn, -1);
203 if (arch_register_req_is(&req, limited)) {
204 ir_node *cpy = be_new_Copy(req.cls, raenv->irg, get_nodes_block(irn), irn);
205 const ir_edge_t *edge;
207 /* all users of the irn use the copy instead */
208 sched_add_after(irn, cpy);
209 foreach_out_edge(irn, edge)
210 set_irn_n(edge->src, edge->pos, cpy);
214 /* handle input constraints by converting them into output constraints
215 * of copies of the former argument
216 * irn -> arg becomes irn -> copy -> arg
218 for (pos = 0, max = get_irn_arity(irn); pos<max; ++pos) {
219 arch_get_register_req(raenv->aenv, &req, irn, pos);
220 if (arch_register_req_is(&req, limited)) {
221 ir_node *arg = get_irn_n(irn, pos);
222 ir_node *cpy = be_new_Copy(req.cls, raenv->irg, get_nodes_block(irn), arg);
224 /* use the copy instead */
225 sched_add_before(irn, cpy);
226 set_irn_n(irn, pos, cpy);
228 /* set an out constraint for the copy */
229 be_set_constr_limited(cpy, -1, &req);
234 static void handle_constraints(be_raext_env_t *raenv) {
235 irg_block_walk_graph(raenv->irg, NULL, handle_constraints_walker, raenv);
239 /******************************************************************************
241 / ____/ ____| /\ | __ \ | |
242 | (___| (___ / \ ______| | | | ___ ___| |_ _ __
243 \___ \\___ \ / /\ \______| | | |/ _ \/ __| __| '__|
244 ____) |___) / ____ \ | |__| | __/\__ \ |_| |
245 |_____/_____/_/ \_\ |_____/ \___||___/\__|_|
247 *****************************************************************************/
249 #define mark_as_done(irn, pos) set_irn_link(irn, INT_TO_PTR(pos+1))
250 #define has_been_done(irn, pos) (PTR_TO_INT(get_irn_link(irn)) > pos)
253 * Insert a copy for the argument of @p start_phi found at position @p pos.
254 * Also searches a phi-loop of arbitrary length to detect and resolve
255 * the class of phi-swap-problems. To search for a loop recursion is used.
257 * 1) Simplest case (phi with a non-phi arg):
258 * A single copy is inserted.
260 * 2) Phi chain (phi (with phi-arg)* with non=phi arg):
261 * Several copies are placed, each after returning from recursion.
264 * On detection a loop breaker is inserted, which is a copy of the start_phi.
265 * This copy then pretends beeing the argumnent of the last phi.
266 * Now case 2) can be used.
268 * The values of @p start_phi and @p pos never change during recursion.
270 * @p raenv Environment with all the stuff needed
271 * @p start_phi Phi node to process
272 * @p pos Argument position to insert copy/copies for
273 * @p curr_phi Phi node currently processed during recursion. Equals start_phi on initial call
275 * @return NULL If no copy is necessary
276 * NULL If the phi has already been processed at this pos
277 * Link field is used to keep track of processed positions
278 * In all other cases the ir_node *copy which was placed is returned.
280 static ir_node *insert_copies(be_raext_env_t *raenv, ir_node *start_phi, int pos, ir_node *curr_phi) {
281 ir_node *arg = get_irn_n(curr_phi, pos);
282 ir_node *arg_blk = get_nodes_block(arg);
283 ir_node *pred_blk = get_Block_cfgpred_block(get_nodes_block(curr_phi), pos);
284 ir_node *curr_cpy, *last_cpy;
286 assert(is_Phi(start_phi) && is_Phi(curr_phi));
288 if (has_been_done(start_phi, pos))
291 /* In case this is a 'normal' phi we insert into
292 * the schedule before the pred_blk irn */
295 /* If we detect a loop stop recursion. */
296 if (arg == start_phi) {
297 ir_node *loop_breaker;
298 if (start_phi == curr_phi) {
299 /* Phi directly uses itself. No copy necessary */
303 /* At least 2 phis are involved */
304 /* Insert a loop breaking copy (an additional variable T) */
305 loop_breaker = be_new_Copy(raenv->cls, raenv->irg, pred_blk, start_phi);
306 sched_add_before(pred_blk, loop_breaker);
311 /* If arg is a phi in the same block we have to continue search */
312 if (is_Phi(arg) && arg_blk == get_nodes_block(start_phi))
313 last_cpy = insert_copies(raenv, start_phi, pos, arg);
315 /* Insert copy of argument (may be the loop-breaker) */
316 curr_cpy = be_new_Copy(raenv->cls, raenv->irg, pred_blk, arg);
317 set_irn_n(curr_phi, pos, curr_cpy);
318 mark_as_done(curr_phi, pos);
319 sched_add_before(last_cpy, curr_cpy);
325 * Perform simple SSA-destruction with copies.
326 * The order of processing _must_ be
327 * for all positions {
332 * else the magic to keep track of processed phi-positions will fail in
333 * function 'insert_copies'
335 static void ssa_destr_simple_walker(ir_node *blk, void *env) {
336 be_raext_env_t *raenv = env;
340 /* for all argument positions of the phis */
341 for (pos=0, max=get_irn_arity(blk); pos<max; ++pos) {
343 /* for all phi nodes (which are scheduled first) */
344 sched_foreach(blk, phi) {
348 raenv->cls = arch_get_irn_reg_class(raenv->aenv, phi, -1);
349 insert_copies(raenv, phi, pos, phi);
355 static void ssa_destr_simple(be_raext_env_t *raenv) {
356 be_clear_links(raenv->irg);
357 irg_block_walk_graph(raenv->irg, ssa_destr_simple_walker, NULL, raenv);
361 static void ssa_destr_rastello(be_raext_env_t *raenv) {
365 phi_class_compute(raenv->irg);
366 irg_block_walk_graph(irg, ssa_destr_rastello, NULL, &raenv);
370 /******************************************************************************
372 \ \ / / | | |__ \ \ \ / /
373 \ \ / /_ _| |___ ) | \ \ / /_ _ _ __ ___
374 \ \/ / _` | / __| / / \ \/ / _` | '__/ __|
375 \ / (_| | \__ \ / /_ \ / (_| | | \__ \
376 \/ \__,_|_|___/ |____| \/ \__,_|_| |___/
377 *****************************************************************************/
380 * This struct maps a variable (nr) to the values belonging to this variable
383 int var_nr; /* the key */
384 pset *values; /* the ssa-values belonging to this variable */
387 #define SET_REMOVED -1
390 * The link field of an irn points to the var_info struct
391 * representing the corresponding variable.
393 #define set_var_info(irn, vi) set_irn_link(irn, vi)
394 #define get_var_info(irn) ((var_info_t *)get_irn_link(irn))
396 #define HASH_VAR_NR(var_nr) var_nr
398 static int compare_var_infos(const void *e1, const void *e2, size_t size) {
399 const var_info_t *v1 = e1;
400 const var_info_t *v2 = e2;
402 if (v1->var_nr == SET_REMOVED || v2->var_nr == SET_REMOVED)
405 return v1->var_nr != v2->var_nr;
408 static INLINE var_info_t *var_find(set *vars, int var_nr) {
412 return set_find(vars, &vi, sizeof(vi), HASH_VAR_NR(var_nr));
415 static INLINE var_info_t *var_find_or_insert(set *vars, int var_nr) {
416 var_info_t vi, *found;
417 memset(&vi, 0, sizeof(vi));
420 found = set_insert(vars, &vi, sizeof(vi), HASH_VAR_NR(var_nr));
423 found->values = pset_new_ptr(1);
429 * Adds a value to a variable. Sets all pointers accordingly.
431 static INLINE var_info_t *var_add_value(be_raext_env_t *raenv, int var_nr, ir_node *irn) {
432 var_info_t *vi = var_find_or_insert(raenv->vars, var_nr);
434 /* var 2 value mapping */
435 pset_insert_ptr(vi->values, irn);
437 /* value 2 var mapping */
438 set_var_info(irn, vi);
443 static INLINE pset *get_var_values(be_raext_env_t *raenv, int var_nr) {
444 var_info_t *vi = var_find(raenv->vars, var_nr);
445 assert(vi && "Variable does not exist");
450 * Define variables (numbers) for all SSA-values.
451 * All values in a phi class get assigned the same variable name.
452 * The link field maps values to the var-name
454 static void values_to_vars(ir_node *irn, void *env) {
455 be_raext_env_t *raenv = env;
459 if(arch_get_irn_reg_class(raenv->aenv, irn, -1) == NULL)
462 vals = get_phi_class(irn);
465 nr = get_irn_node_nr(get_first_phi(vals));
467 /* not a phi class member, value == var */
468 nr = get_irn_node_nr(irn);
469 vals = pset_new_ptr(1);
470 pset_insert_ptr(vals, irn);
473 /* values <--> var mapping */
474 pset_foreach(vals, irn)
475 var_add_value(raenv, nr, irn);
479 /******************************************************************************
482 | | | |_ _ _ __ ___ _ __ ___ _ __
483 | | | | | | | '_ ` _ \| '_ \ / _ \ '__|
484 | |__| | |_| | | | | | | |_) | __/ |
485 |_____/ \__,_|_| |_| |_| .__/ \___|_|
488 *****************************************************************************/
491 static void extract_vars_of_cls(be_raext_env_t *raenv) {
495 raenv->cls_vars = malloc(set_count(raenv->vars) * sizeof(*raenv->cls_vars));
496 assert(raenv->cls_vars);
498 set_foreach(raenv->vars, vi)
499 if (is_res_in_reg_class(get_first_non_phi(vi->values)))
500 raenv->cls_vars[count++] = vi;
502 raenv->cls_vars = realloc(raenv->cls_vars, count * sizeof(*raenv->cls_vars));
503 assert(raenv->cls_vars);
505 raenv->n_cls_vars = count;
510 * Check if node irn has a limited-constraint at position pos.
511 * If yes, dump it to FILE raenv->f
513 static INLINE void dump_constraint(be_raext_env_t *raenv, ir_node *irn, int pos) {
514 bitset_t *bs = bitset_alloca(raenv->cls->n_regs);
515 arch_register_req_t req;
517 arch_get_register_req(raenv->aenv, &req, irn, pos);
518 if (arch_register_req_is(&req, limited)) {
520 req.limited(req.limited_env, bs);
521 reg_nr = bitset_next_set(bs, 0);
522 fprintf(raenv->f, "<%d>", reg_nr);
523 assert(-1 == bitset_next_set(bs, reg_nr+1) && "Constraints with more than 1 possible register are not supported");
527 static INLINE unsigned int get_spill_costs(be_raext_env_t *raenv, var_info_t *vi) {
529 int c_spills=0, c_reloads=0;
531 pset_foreach(vi->values, irn) {
532 if (arch_irn_is_ignore(raenv->aenv, irn)) {
533 pset_break(vi->values);
538 /* number of reloads is the number of non-phi uses of all values of this var */
539 const ir_edge_t *edge;
540 foreach_out_edge(irn, edge)
541 if (!is_Phi(edge->src))
542 c_reloads += get_reload_weight(edge->src);
544 /* number of spills is the number of non-phi values for this var */
545 c_spills += get_spill_weight(irn);
549 return c_spills + c_reloads;
552 static void dump_nodes(be_raext_env_t *raenv) {
556 fprintf(f, "\nnodes {\n");
558 for (i=0; i<raenv->n_cls_vars; ++i) {
559 var_info_t *vi = raenv->cls_vars[i];
561 if (vi->var_nr == SET_REMOVED)
564 fprintf(f, "%d %u", vi->var_nr, get_spill_costs(raenv, vi));
565 dump_constraint(raenv, get_first_non_phi(vi->values), -1);
573 static void dump_interferences(be_raext_env_t *raenv) {
575 var_info_t *vi1, *vi2;
576 ir_node *irn1, *irn2;
579 fprintf(f, "\ninterferences {\n");
581 for (i=0; i<raenv->n_cls_vars; ++i) {
582 vi1 = raenv->cls_vars[i];
584 if (vi1->var_nr == SET_REMOVED)
587 for (o=i+1; o<raenv->n_cls_vars; ++o) {
588 vi2 = raenv->cls_vars[o];
590 if (vi2->var_nr == SET_REMOVED)
593 pset_foreach(vi1->values, irn1)
594 pset_foreach(vi2->values, irn2)
595 if (values_interfere(irn1, irn2)) {
596 pset_break(vi1->values);
597 pset_break(vi2->values);
598 fprintf(f, "(%d, %d)\n", vi1->var_nr, vi2->var_nr);
605 static void dump_affinities_walker(ir_node *irn, void *env) {
606 be_raext_env_t *raenv = env;
607 arch_register_req_t req;
609 var_info_t *vi1, *vi2;
611 if (arch_get_irn_reg_class(raenv->aenv, irn, -1) == NULL || arch_irn_is_ignore(raenv->aenv, irn))
614 vi1 = get_var_info(irn);
616 /* copies have affinities */
617 if (arch_irn_classify(raenv->aenv, irn) == arch_irn_class_copy) {
618 ir_node *other = get_irn_n(irn, 0);
620 if (! arch_irn_is_ignore(raenv->aenv, other)) {
621 vi2 = get_var_info(other);
623 fprintf(raenv->f, "(%d, %d, %d)\n", vi1->var_nr, vi2->var_nr, get_affinity_weight(irn));
628 /* should_be_equal constraints are affinites */
629 for (pos = 0, max = get_irn_arity(irn); pos<max; ++pos) {
630 arch_get_register_req(raenv->aenv, &req, irn, pos);
632 if (arch_register_req_is(&req, should_be_same) && arch_irn_is_ignore(raenv->aenv, req.other_same)) {
633 vi2 = get_var_info(req.other_same);
635 fprintf(raenv->f, "(%d, %d, %d)\n", vi1->var_nr, vi2->var_nr, get_affinity_weight(irn));
641 static void dump_affinities(be_raext_env_t *raenv) {
642 fprintf(raenv->f, "\naffinities {\n");
643 irg_walk_graph(raenv->irg, NULL, dump_affinities_walker, raenv);
644 fprintf(raenv->f, "}\n");
648 * Dump all information needed by the external
649 * register allocator to a single file.
651 static void dump_to_file(be_raext_env_t *raenv, char *filename) {
654 if (!(f = fopen(filename, "wt"))) {
655 fprintf(stderr, "Could not open file %s for writing\n", filename);
661 /* dump register info */
662 fprintf(f, "regs %d\n", arch_register_class_n_regs(raenv->cls));
664 /* dump the interference graph */
666 dump_interferences(raenv);
667 dump_affinities(raenv);
672 /******************************************************************************
675 | |__ __ _____ ___ _ _| |_ ___
676 | __| \ \/ / _ \/ __| | | | __/ _ \
677 | |____ > < __/ (__| |_| | || __/
678 |______/_/\_\___|\___|\__,_|\__\___|
679 *****************************************************************************/
682 * Execute the external register allocator specified in the
683 * firm-option firm.be.ra.ext.callee
685 static void execute(char *prog_to_call, char *out_file, char *result_file) {
689 snprintf(cmd_line, sizeof(cmd_line), "%s -i %s -o %s", prog_to_call, out_file, result_file);
691 ret_status = system(cmd_line);
692 assert(ret_status != -1 && "Invokation of external register allocator failed");
695 /******************************************************************************
698 / \ _ __ _ __ | |_ _ | |__) |___ ___ _ _| | |_
699 / /\ \ | '_ \| '_ \| | | | | | _ // _ \/ __| | | | | __|
700 / ____ \| |_) | |_) | | |_| | | | \ \ __/\__ \ |_| | | |_
701 /_/ \_\ .__/| .__/|_|\__, | |_| \_\___||___/\__,_|_|\__|
704 *****************************************************************************/
707 * Spill a variable and add reloads before all uses.
709 static INLINE void var_add_spills_and_reloads(be_raext_env_t *raenv, int var_nr) {
710 var_info_t *vi = var_find(raenv->vars, var_nr);
711 ir_node *spill=NULL, *ctx, *irn;
713 const ir_edge_t *edge, *ne;
714 pset *spills = pset_new_ptr(4); /* the spills of this variable */
715 pset *reloads = pset_new_ptr(4); /* the reloads of this variable */
716 int new_size, n_spills, n_reloads;
718 assert(vi && "Variable nr does not exist!");
719 assert(pset_count(vi->values) && "There are no values associated to this variable");
721 /* the spill context is set to an arbitrary node of the phi-class,
722 * or the node itself if it is not member of a phi class
724 if (pset_count(vi->values) == 1)
725 ctx = get_first_non_phi(vi->values);
727 ctx = get_first_phi(vi->values);
729 DBG((raenv->dbg, LEVEL_2, "Spill context: %+F\n", ctx));
731 /* for each value of this variable insert the spills */
732 pset_foreach(vi->values, irn) {
738 /* all ordinary nodes must be spilled */
739 DBG((raenv->dbg, LEVEL_2, " spilling %+F\n", irn));
740 spill = be_spill(raenv->aenv, irn, ctx);
742 /* remember the spill */
743 pset_insert_ptr(spills, spill);
746 assert(spill && "There must be at least one non-phi-node");
748 mode = get_irn_mode(get_irn_n(spill, 0));
750 /* insert reloads and wire them arbitrary*/
751 pset_foreach(vi->values, irn)
752 foreach_out_edge_safe(irn, edge, ne) {
753 ir_node *reload, *src = edge->src;
754 if (is_Phi(src) || be_is_Spill(src))
757 /* all real uses must be reloaded */
758 DBG((raenv->dbg, LEVEL_2, " reloading before %+F\n", src));
759 reload = be_reload(raenv->aenv, raenv->cls, edge->src, mode, spill);
760 set_irn_n(edge->src, edge->pos, reload);
762 /* remember the reload */
763 pset_insert_ptr(reloads, reload);
766 /* correct the reload->spill pointers... */
767 be_ssa_constr_set(raenv->dom_info, spills);
770 /****** correct the variable <--> values mapping: ******
772 * - if we had a phi class it gets split into several new variables
773 * - all reloads are new variables
775 n_spills = pset_count(spills);
776 n_reloads = pset_count(reloads);
778 /* first make room for new pointers in the cls_var array */
779 new_size = raenv->n_cls_vars + n_reloads + ((n_spills>1) ? n_spills : 0);
780 raenv->cls_vars = realloc(raenv->cls_vars, (new_size) * sizeof(*raenv->cls_vars));
781 assert(raenv->cls_vars && "Out of mem!?");
783 /* if we had a real phi-class, we must... */
784 if (pset_count(spills) > 1) {
785 /* ...remove the old variable corresponding to the phi class */
786 vi->var_nr = SET_REMOVED;
788 /* ...add new vars for each non-phi-member */
789 pset_foreach(spills, irn) {
790 ir_node *spilled = get_irn_n(irn, 0);
791 raenv->cls_vars[raenv->n_cls_vars++] = var_add_value(raenv, get_irn_node_nr(spilled), spilled);
795 /* add new variables for all reloads */
796 pset_foreach(reloads, irn)
797 raenv->cls_vars[raenv->n_cls_vars++] = var_add_value(raenv, get_irn_node_nr(irn), irn);
803 #define INVALID_FILE_FORMAT assert(0 && "Invalid file format.")
805 #define BUFCONV " %32s "
808 * Read in the actions performed by the external allocator.
809 * Apply these transformations to the irg.
810 * @return 1 if an allocation was read in. 0 otherwise.
812 static int read_and_apply_results(be_raext_env_t *raenv, char *filename) {
815 int is_allocation = 0;
817 if (!(f = fopen(filename, "rt"))) {
818 fprintf(stderr, "Could not open file %s for reading\n", filename);
824 /* read the action */
825 if (fscanf(f, BUFCONV, buf) != 1)
829 if (!strcmp(buf, "spills")) {
831 while (fscanf(f, " %d ", &var_nr) == 1)
832 var_add_spills_and_reloads(raenv, var_nr);
835 /* or do we allocate */
836 if (!strcmp(buf, "allocs")) {
840 while (fscanf(f, " %d %d ", &var_nr, ®_nr) == 2) {
842 pset *vals = get_var_values(raenv, var_nr);
844 assert(vals && "Variable nr does not exist!");
845 pset_foreach(vals, irn)
846 arch_set_irn_register(raenv->aenv, irn, arch_register_for_index(raenv->cls, reg_nr));
856 return is_allocation;
859 static void check_allocation(be_raext_env_t *raenv) {
862 for (i=0; i<raenv->n_cls_vars; ++i) {
863 var_info_t *vi1 = raenv->cls_vars[i];
865 if (vi1->var_nr == SET_REMOVED)
868 for (o=0; o<i; ++o) {
869 var_info_t *vi2 = raenv->cls_vars[o];
870 ir_node *irn1, *irn2;
872 if (vi2->var_nr == SET_REMOVED)
875 pset_foreach(vi1->values, irn1)
876 pset_foreach(vi2->values, irn2)
877 if (values_interfere(irn1, irn2)) {
878 dump_ir_block_graph_sched(raenv->irg, "ERROR");
879 ir_fprintf(stdout, "SSA values %+F and %+F interfere. They belong to varible %d and %d respectively.\n", irn1, irn2, vi1->var_nr, vi2->var_nr);
880 assert(0 && "ERROR graph dumped");
886 /******************************************************************************
890 | |\/| |/ _` | | '_ \
891 | | | | (_| | | | | |
892 |_| |_|\__,_|_|_| |_|
893 *****************************************************************************/
896 * Default values for options
898 static void (*ssa_destr)(be_raext_env_t*) = ssa_destr_simple;
899 static char callee[128] = "\"E:/user/kimohoff/ipd-registerallocator/win32/register allocator\"";
900 //static char callee[128] = "/ben/kimohoff/ipd-registerallocator/register_allocator";
904 * Allocate registers with an external program using a text-file interface.
906 * Do some computations (SSA-destruction and mapping of values--vars)
908 * Execute external program
909 * Read in results and apply them
912 static void be_ra_extern_main(const be_irg_t *bi) {
913 be_main_env_t *env = bi->main_env;
914 ir_graph *irg = bi->irg;
916 be_raext_env_t raenv;
923 raenv.aenv = env->arch_env;
924 raenv.dom_info = be_compute_dominance_frontiers(irg);
925 raenv.vars = new_set(compare_var_infos, 64);
926 raenv.dbg = firm_dbg_register("ir.be.raextern");
927 firm_dbg_set_mask(raenv.dbg, DBG_LEVEL);
929 /* Insert copies for constraints */
930 handle_constraints(&raenv);
931 dump_ir_block_graph_sched(irg, "-extern-constr");
933 /* SSA destruction respectively transformation into "Conventional SSA" */
935 dump_ir_block_graph_sched(irg, "-extern-ssadestr");
937 /* Mapping of SSA-Values <--> Variables */
938 phi_class_compute(irg);
940 irg_walk_graph(irg, values_to_vars, NULL, &raenv);
943 /* For all register classes */
944 for(clsnr = 0, clss = arch_isa_get_n_reg_class(raenv.aenv->isa); clsnr < clss; ++clsnr) {
946 char out[256], in[256];
948 raenv.cls = arch_isa_get_reg_class(raenv.aenv->isa, clsnr);
950 extract_vars_of_cls(&raenv);
953 ir_snprintf(out, sizeof(out), "%F-%s-%d.ra", irg, raenv.cls->name, round);
954 ir_snprintf(in, sizeof(in), "%F-%s-%d.ra.res", irg, raenv.cls->name, round);
958 dump_to_file(&raenv, out);
959 execute(callee, out, in);
960 done = read_and_apply_results(&raenv, in);
962 ir_snprintf(in, sizeof(in), "-extern-%s-round-%d", raenv.cls->name, round);
963 dump_ir_block_graph_sched(irg, in);
968 check_allocation(&raenv);
970 free(raenv.cls_vars);
973 dump_ir_block_graph_sched(irg, "-extern-alloc");
976 set_foreach(raenv.vars, vi)
977 del_pset(vi->values);
979 be_free_dominance_frontiers(raenv.dom_info);
982 /******************************************************************************
985 | | | |_ __ | |_ _ ___ _ __ ___
986 | | | | '_ \| __| |/ _ \| '_ \/ __|
987 | |__| | |_) | |_| | (_) | | | \__ \
988 \____/| .__/ \__|_|\___/|_| |_|___/
991 *****************************************************************************/
996 static const lc_opt_enum_func_ptr_items_t ssa_destr_items[] = {
997 { "simple", (int (*)()) ssa_destr_simple }, /* TODO make (void*) casts nicer */
998 { "rastello", (int (*)()) ssa_destr_rastello },
1002 static lc_opt_enum_func_ptr_var_t ssa_destr_var = {
1003 (int (**)()) &ssa_destr, ssa_destr_items
1006 static const lc_opt_table_entry_t be_ra_extern_options[] = {
1007 LC_OPT_ENT_ENUM_FUNC_PTR("ssa_destr", "SSA destruction flavor", &ssa_destr_var),
1008 LC_OPT_ENT_STR("callee", "The external program to call", callee, sizeof(callee)),
1012 static void be_ra_extern_register_options(lc_opt_entry_t *root) {
1013 lc_opt_entry_t *grp = lc_opt_get_grp(root, "ext");
1015 lc_opt_add_table(grp, be_ra_extern_options);
1018 #endif /* WITH_LIBCORE */
1020 const be_ra_t be_ra_external_allocator = {
1022 be_ra_extern_register_options,