unified main comments
[libfirm] / ir / be / beraextern.c
1 /*
2  * Copyright (C) 1995-2007 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       Implementation of the RA-Interface for an external, (non-SSA) register allocator.
23  * @author      Daniel Grund
24  * @date        17.01.2006
25  * @version     $Id$
26  *
27  * The external register allocator is a program:
28  *    PROG -i INPUTFILE -o OUTPUTFILE
29  *
30  *   1) Input file defines the interference graph
31  *   2) Output file contains the instructions to perform
32  *
33
34
35 The input file format
36 ----------------------
37
38 inputfile       ::= regs nodes interf affinities .
39
40 regs            ::= 'regs' regcount .                                           // Anzahl der register (0..regcount-1), die zur Verfuegung stehen
41
42 nodes           ::= 'nodes' '{' node* '}' .                                     // All nodes in the graph
43
44 node            ::= node-info
45                           | node-info '<' reg-nr '>' .                          // Reg-nr is present in case of constraints
46
47 node-info       ::= node-nr spill-costs .
48
49 interf          ::= 'interferences' '{' i-edge* '}' .           // Interference edges of the graph
50
51 i-edge          ::= '(' node-nr ',' node-nr ')' .
52
53 affinities      ::= 'affinities' '{' a-edge* '}' .                      // Affinity edges of the graph
54
55 a-edge          ::= '(' node-nr ',' node-nr ',' weight ')' .
56
57
58 weight, regcount, node-nr ::= int32 .
59 spill-costs ::= int32 .                                                                 // negative spill costs indicate unspillable
60
61 The output file format
62 -----------------------
63
64 outputfile      ::= spills | allocs .
65
66 spills          ::= 'spills' node-nr+ .
67
68 allocs          ::= 'allocs' alloc* .
69
70 alloc           ::= node-nr reg-nr .
71
72 */
73 #ifdef NOT_PORTED
74 #ifdef HAVE_CONFIG_H
75 #include "config.h"
76 #endif
77
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <limits.h>
81 #include <libcore/lc_opts.h>
82 #include <libcore/lc_opts_enum.h>
83
84 #include "set.h"
85 #include "pset.h"
86 #include "pmap.h"
87 #include "bitset.h"
88 #include "raw_bitset.h"
89 #include "xmalloc.h"
90
91 #include "irprintf_t.h"
92 #include "irnode_t.h"
93 #include "irgraph_t.h"
94 #include "irgwalk.h"
95 #include "iredges_t.h"
96 #include "irdom_t.h"
97 #include "phiclass.h"
98
99 #include "bemodule.h"
100 #include "beraextern.h"
101 #include "beabi.h"
102 #include "bearch_t.h"
103 #include "benode_t.h"
104 #include "beirgmod.h"
105 #include "besched_t.h"
106 #include "beutil.h"
107 #include "belive_t.h"
108 #include "beinsn_t.h"
109
110 #include "bessadestrsimple.h"
111
112 #define DBG_LEVEL 2
113
114 /**
115  * Environment with all the needed stuff
116  */
117 typedef struct _be_raext_env_t {
118         arch_env_t *aenv;
119         const arch_register_class_t *cls;
120         be_irg_t *birg;
121         ir_graph *irg;
122
123         FILE *f;                                /**< file handle used for out- and input file */
124         set *vars;                              /**< contains all be_var_info_t */
125         int n_cls_vars;                 /**< length of the array cls_vars */
126         be_var_info_t **cls_vars;       /**< only the var_infos for current cls. needed for double iterating */
127         DEBUG_ONLY(firm_dbg_module_t *dbg;)
128 } be_raext_env_t;
129
130
131
132 /******************************************************************************
133     _    _      _
134    | |  | |    | |
135    | |__| | ___| |_ __   ___ _ __ ___
136    |  __  |/ _ \ | '_ \ / _ \ '__/ __|
137    | |  | |  __/ | |_) |  __/ |  \__ \
138    |_|  |_|\___|_| .__/ \___|_|  |___/
139                  | |
140                  |_|
141  *****************************************************************************/
142
143
144 #define pset_foreach(pset, irn)  for(irn=pset_first(pset); irn; irn=pset_next(pset))
145 #define set_foreach(set, e)  for(e=set_first(set); e; e=set_next(set))
146
147 /**
148  * Checks if _the_ result of the irn belongs to the
149  * current register class (raenv->cls)
150  * NOTE: Only the first result is checked.
151  */
152 #define is_res_in_reg_class(irn) arch_irn_has_reg_class(raenv->aenv, irn, -1, raenv->cls)
153
154 static INLINE ir_node *get_first_non_phi(pset *s) {
155         ir_node *irn;
156
157         pset_foreach(s, irn)
158                 if (!is_Phi(irn)) {
159                         pset_break(s);
160                         return irn;
161                 }
162
163         assert(0 && "There must be a non-phi-irn in this");
164         return NULL;
165 }
166
167 static INLINE ir_node *get_first_phi(pset *s) {
168         ir_node *irn;
169
170         pset_foreach(s, irn)
171                 if (is_Phi(irn)) {
172                         pset_break(s);
173                         return irn;
174                 }
175
176         assert(0 && "There must be a phi in this");
177         return NULL;
178 }
179
180 static int get_loop_weight(ir_node *irn) {
181         int cost = 0;
182         ir_loop *loop = get_irn_loop(get_nodes_block(irn));
183
184         if (loop) {
185                 int d = get_loop_depth(loop);
186                 cost = d*d;
187         }
188         return cost+1;
189 }
190
191 #define get_const_weight(irn) (1)
192
193 #define get_spill_weight(irn)    get_loop_weight(irn)
194 #define get_reload_weight(irn)   get_loop_weight(irn)
195 #define get_affinity_weight(irn) get_loop_weight(irn)
196
197 /******************************************************************************
198     _____                _            _____            _
199    / ____|              | |          / ____|          (_)
200   | |     ___  _ __  ___| |_ _ __   | |     ___  _ __  _  ___  ___
201   | |    / _ \| '_ \/ __| __| '__|  | |    / _ \| '_ \| |/ _ \/ __|
202   | |___| (_) | | | \__ \ |_| |     | |___| (_) | |_) | |  __/\__ \
203    \_____\___/|_| |_|___/\__|_|      \_____\___/| .__/|_|\___||___/
204                                                 | |
205                                                 |_|
206  *****************************************************************************/
207
208 static void handle_constraints_insn(be_raext_env_t *env, be_insn_t *insn)
209 {
210         ir_node *bl = get_nodes_block(insn->irn);
211         int i;
212
213         for(i = 0; i < insn->use_start; ++i) {
214                 be_operand_t *op = &insn->ops[i];
215
216                 if(op->has_constraints) {
217                         ir_node *cpy = be_new_Copy(op->req->cls, env->irg, bl, op->carrier);
218                         sched_add_before(insn->next_insn, cpy);
219                         edges_reroute(op->carrier, cpy, env->irg);
220                 }
221         }
222
223         for(i = insn->use_start; i < insn->n_ops; ++i) {
224                 be_operand_t *op = &insn->ops[i];
225
226                 if(op->has_constraints) {
227                         ir_node *cpy = be_new_Copy(op->req->cls, env->irg, bl, op->carrier);
228                         sched_add_before(insn->irn, cpy);
229                         set_irn_n(insn->irn, op->pos, cpy);
230                         be_set_constr_limited(cpy, BE_OUT_POS(0), op->req);
231                 }
232         }
233 }
234
235 static void handle_constraints_block(ir_node *bl, void *data)
236 {
237         be_raext_env_t *raenv = data;
238         int active            = bl != get_irg_start_block(raenv->irg);
239
240         ir_node *irn;
241         be_insn_env_t ie;
242         struct obstack obst;
243
244         ie.cls           = raenv->cls;
245         ie.aenv          = raenv->aenv;
246         ie.obst          = &obst;
247         ie.ignore_colors = NULL;
248         obstack_init(&obst);
249
250         irn = sched_first(bl);
251         while(!sched_is_end(irn)) {
252                 be_insn_t *insn = be_scan_insn(&ie, irn);
253
254                 if(insn->has_constraints)
255                         handle_constraints_insn(raenv, insn);
256
257                 if(be_is_Barrier(irn))
258                         active = !active;
259
260                 irn = insn->next_insn;
261                 obstack_free(&obst, insn);
262         }
263 }
264
265 static void handle_constraints(be_raext_env_t *raenv) {
266         irg_block_walk_graph(raenv->irg, NULL, handle_constraints_block, raenv);
267 }
268
269
270
271
272 /******************************************************************************
273     _____
274    |  __ \
275    | |  | |_   _ _ __ ___  _ __   ___ _ __
276    | |  | | | | | '_ ` _ \| '_ \ / _ \ '__|
277    | |__| | |_| | | | | | | |_) |  __/ |
278    |_____/ \__,_|_| |_| |_| .__/ \___|_|
279                           | |
280                           |_|
281  *****************************************************************************/
282
283
284 static void extract_vars_of_cls(be_raext_env_t *raenv) {
285         int count = 0;
286         be_var_info_t *vi;
287
288         raenv->cls_vars = xmalloc(set_count(raenv->vars) * sizeof(*raenv->cls_vars));
289         assert(raenv->cls_vars);
290
291         set_foreach(raenv->vars, vi)
292                 if (is_res_in_reg_class(get_first_non_phi(vi->values)))
293                         raenv->cls_vars[count++] = vi;
294
295         raenv->cls_vars = realloc(raenv->cls_vars, count * sizeof(*raenv->cls_vars));
296         assert(raenv->cls_vars);
297
298         raenv->n_cls_vars = count;
299 }
300
301
302 /**
303  * Check if node irn has a limited-constraint at position pos.
304  * If yes, dump it to FILE raenv->f
305  */
306 static INLINE void dump_constraint(be_raext_env_t *raenv, ir_node *irn, int pos) {
307         const arch_register_req_t *req;
308
309         req = arch_get_register_req(raenv->aenv, irn, pos);
310         if (arch_register_req_is(req, limited)) {
311                 unsigned reg_nr;
312
313                 reg_nr = rbitset_next(req->limited, 0, 1);
314                 fprintf(raenv->f, "<%d>", reg_nr);
315                 assert(rbitset_popcnt(req->limited, raenv->cls->n_regs) <= 1
316                                 && "Constraints with more than 1 possible register are not supported");
317         }
318 }
319
320 #define UNSPILLABLE -1
321
322 static INLINE int get_spill_costs(be_raext_env_t *raenv, be_var_info_t *vi) {
323         ir_node *irn;
324         int c_spills=0, c_reloads=0;
325
326         pset_foreach(vi->values, irn) {
327                 if (arch_irn_is(raenv->aenv, irn, ignore) || be_is_Reload(irn)) {
328                         pset_break(vi->values);
329                         return UNSPILLABLE;
330                 }
331
332                 if (is_Phi(irn)) {
333                         /* number of reloads is the number of non-phi uses of all values of this var */
334                         const ir_edge_t *edge;
335                         foreach_out_edge(irn, edge)
336                                 if (!is_Phi(edge->src))
337                                         c_reloads += get_reload_weight(edge->src);
338                 } else {
339                         /* number of spills is the number of non-phi values for this var */
340                         c_spills += get_spill_weight(irn);
341                 }
342         }
343
344         return c_spills + c_reloads;
345 }
346
347 static void dump_nodes(be_raext_env_t *raenv) {
348         FILE *f = raenv->f;
349         int i;
350
351         fprintf(f, "\nnodes {\n");
352
353         for (i=0; i<raenv->n_cls_vars; ++i) {
354                 be_var_info_t *vi = raenv->cls_vars[i];
355
356                 if (vi->var_nr == SET_REMOVED)
357                         continue;
358
359                 fprintf(f, "%d %d", vi->var_nr, get_spill_costs(raenv, vi));
360                 dump_constraint(raenv, get_first_non_phi(vi->values), -1);
361                 fprintf(f, "\n");
362         }
363
364         fprintf(f, "}\n");
365         fflush(f);
366 }
367
368
369 static void dump_interferences(be_raext_env_t *raenv) {
370         int i,o;
371         be_var_info_t *vi1, *vi2;
372         ir_node *irn1, *irn2;
373         FILE *f = raenv->f;
374         be_lv_t *lv = raenv->birg->lv;
375
376         fprintf(f, "\ninterferences {\n");
377
378         for (i=0; i<raenv->n_cls_vars; ++i) {
379                 vi1 = raenv->cls_vars[i];
380
381                 if (vi1->var_nr == SET_REMOVED)
382                         continue;
383
384                 for (o=i+1; o<raenv->n_cls_vars; ++o) {
385                         vi2 = raenv->cls_vars[o];
386
387                         if (vi2->var_nr == SET_REMOVED)
388                                 continue;
389
390                         pset_foreach(vi1->values, irn1)
391                                 pset_foreach(vi2->values, irn2)
392                                         if (values_interfere(lv, irn1, irn2)) {
393                                                 pset_break(vi1->values);
394                                                 pset_break(vi2->values);
395                                                 fprintf(f, "(%d, %d)\n", vi1->var_nr, vi2->var_nr);
396                                                 goto NextVar;
397                                         }
398
399 NextVar: ;
400                 }
401         }
402         fprintf(f, "}\n");
403 }
404
405 static void dump_affinities_walker(ir_node *irn, void *env) {
406         be_raext_env_t *raenv = env;
407         const arch_register_req_t *req;
408         int pos, max;
409         be_var_info_t *vi1, *vi2;
410
411         if (arch_get_irn_reg_class(raenv->aenv, irn, -1) != raenv->cls || arch_irn_is(raenv->aenv, irn, ignore))
412                 return;
413
414         vi1 = be_get_var_info(irn);
415
416         /* copies have affinities */
417         if (arch_irn_class_is(raenv->aenv, irn, copy)) {
418                 ir_node *other = be_get_Copy_op(irn);
419
420                 if (! arch_irn_is(raenv->aenv, other, ignore)) {
421                         vi2 = be_get_var_info(other);
422
423                         fprintf(raenv->f, "(%d, %d, %d)\n",  vi1->var_nr, vi2->var_nr, get_affinity_weight(irn));
424                 }
425         }
426
427
428         /* should_be_equal constraints are affinites */
429         for (pos = 0, max = get_irn_arity(irn); pos<max; ++pos) {
430                 req = arch_get_register_req(raenv->aenv, irn, pos);
431
432                 if (arch_register_req_is(req, should_be_same)) {
433                         ir_node *other = get_irn_n(irn, req->other_same);
434                         if(arch_irn_is(raenv->aenv, other, ignore)) {
435                                 vi2 = be_get_var_info(other);
436
437                                 fprintf(raenv->f, "(%d, %d, %d)\n",  vi1->var_nr, vi2->var_nr, get_affinity_weight(irn));
438                         }
439                 }
440         }
441 }
442
443
444 static void dump_affinities(be_raext_env_t *raenv) {
445         fprintf(raenv->f, "\naffinities {\n");
446         irg_walk_graph(raenv->irg, NULL, dump_affinities_walker, raenv);
447         fprintf(raenv->f, "}\n");
448 }
449
450 /**
451  * Dump all information needed by the external
452  * register allocator to a single file.
453  */
454 static void dump_to_file(be_raext_env_t *raenv, char *filename) {
455         FILE *f;
456
457         if (!(f = fopen(filename, "wt"))) {
458                 fprintf(stderr, "Could not open file %s for writing\n", filename);
459                 assert(0);
460                 exit(0xdeadbeef);
461         }
462         raenv->f = f;
463
464         /* dump register info */
465         fprintf(f, "regs %d\n", arch_register_class_n_regs(raenv->cls));
466
467         /* dump the interference graph */
468         dump_nodes(raenv);
469         dump_interferences(raenv);
470         dump_affinities(raenv);
471
472         fclose(f);
473 }
474
475 /******************************************************************************
476     ______                     _
477    |  ____|                   | |
478    | |__  __  _____  ___ _   _| |_ ___
479    |  __| \ \/ / _ \/ __| | | | __/ _ \
480    | |____ >  <  __/ (__| |_| | ||  __/
481    |______/_/\_\___|\___|\__,_|\__\___|
482  *****************************************************************************/
483
484 /**
485  * Execute the external register allocator specified in the
486  * firm-option firm.be.ra.ext.callee
487  */
488 static void execute(char *prog_to_call, char *out_file, char *result_file) {
489         char cmd_line[1024];
490         int ret_status;
491
492         snprintf(cmd_line, sizeof(cmd_line), "%s -i %s -o %s", prog_to_call, out_file, result_file);
493         cmd_line[sizeof(cmd_line) - 1] = '\0';
494
495         ret_status = system(cmd_line);
496         assert(ret_status != -1 && "Invokation of external register allocator failed");
497         assert(ret_status == 0 && "External register allocator is unhappy with sth.");
498 }
499
500 /******************************************************************************
501                          _         _____                 _ _
502        /\               | |       |  __ \               | | |
503       /  \   _ __  _ __ | |_   _  | |__) |___  ___ _   _| | |_
504      / /\ \ | '_ \| '_ \| | | | | |  _  // _ \/ __| | | | | __|
505     / ____ \| |_) | |_) | | |_| | | | \ \  __/\__ \ |_| | | |_
506    /_/    \_\ .__/| .__/|_|\__, | |_|  \_\___||___/\__,_|_|\__|
507             | |   | |       __/ |
508             |_|   |_|      |___/
509  *****************************************************************************/
510
511 /**
512  * Spill a variable and add reloads before all uses.
513  */
514 static INLINE void var_add_spills_and_reloads(be_raext_env_t *raenv, int var_nr) {
515         be_var_info_t *vi = be_var_find(raenv->vars, var_nr);
516         ir_node *spill=NULL, *ctx, *irn;
517         ir_mode *mode;
518         const ir_edge_t *edge, *ne;
519         pset *spills  = pset_new_ptr(4);        /* the spills of this variable */
520         pset *reloads = pset_new_ptr(4);        /* the reloads of this variable */
521         be_lv_t *lv = raenv->birg->lv;
522         be_dom_front_info_t *dom_front = raenv->birg->dom_front;
523         int new_size, n_spills, n_reloads;
524
525         assert(vi && "Variable nr does not exist!");
526         assert(pset_count(vi->values) && "There are no values associated to this variable");
527
528         /* the spill context is set to an arbitrary node of the phi-class,
529          * or the node itself if it is not member of a phi class
530          */
531         if (pset_count(vi->values) == 1)
532                 ctx = get_first_non_phi(vi->values);
533         else
534                 ctx = get_first_phi(vi->values);
535
536         DBG((raenv->dbg, LEVEL_2, "Spill context: %+F\n", ctx));
537
538         /* for each value of this variable insert the spills */
539         pset_foreach(vi->values, irn) {
540                 if (is_Phi(irn)) {
541                         sched_remove(irn);
542                         continue;
543                 }
544
545                 /* all ordinary nodes must be spilled */
546                 DBG((raenv->dbg, LEVEL_2, "  spilling %+F\n", irn));
547                 spill = be_spill(raenv->aenv, irn);
548
549                 /* remember the spill */
550                 pset_insert_ptr(spills, spill);
551         }
552
553         assert(spill && "There must be at least one non-phi-node");
554
555         mode = get_irn_mode(get_irn_n(spill, be_pos_Spill_val));
556
557         /* insert reloads and wire them arbitrary*/
558         pset_foreach(vi->values, irn) {
559                 foreach_out_edge_safe(irn, edge, ne) {
560                         ir_node *reload, *src = edge->src;
561                         if (is_Phi(src) || be_is_Spill(src))
562                                 continue;
563
564                         /* all real uses must be reloaded */
565                         DBG((raenv->dbg, LEVEL_2, "  reloading before %+F\n", src));
566                         reload = be_reload(raenv->aenv, raenv->cls, edge->src, mode, spill);
567                         set_irn_n(edge->src, edge->pos, reload);
568
569                         /* remember the reload */
570                         pset_insert_ptr(reloads, reload);
571                 }
572         }
573
574         /* correct the reload->spill pointers... */
575         be_ssa_constr_set_ignore(dom_front, lv, spills, NULL);
576
577
578         /****** correct the variable <--> values mapping: ******
579          *
580          *  - if we had a phi class it gets split into several new variables
581          *  - all reloads are new variables
582          */
583         n_spills = pset_count(spills);
584         n_reloads = pset_count(reloads);
585
586         /* first make room for new pointers in the cls_var array */
587         new_size = raenv->n_cls_vars + n_reloads + ((n_spills>1) ? n_spills : 0);
588         raenv->cls_vars = realloc(raenv->cls_vars, (new_size) * sizeof(*raenv->cls_vars));
589         assert(raenv->cls_vars && "Out of mem!?");
590
591         /* if we had a real phi-class, we must... */
592         if (pset_count(spills) > 1) {
593                 /* ...remove the old variable corresponding to the phi class */
594                 vi->var_nr = SET_REMOVED;
595
596                 /* ...add new vars for each non-phi-member */
597                 pset_foreach(spills, irn) {
598                         ir_node *spilled = get_irn_n(irn, be_pos_Spill_val);
599                         raenv->cls_vars[raenv->n_cls_vars++] = be_var_add_value(raenv->vars, get_irn_node_nr(spilled), spilled);
600                 }
601         }
602
603         /* add new variables for all reloads */
604         pset_foreach(reloads, irn) {
605                 assert(get_irn_node_nr(irn) != 1089);
606                 raenv->cls_vars[raenv->n_cls_vars++] = be_var_add_value(raenv->vars, get_irn_node_nr(irn), irn);
607         }
608
609         del_pset(spills);
610         del_pset(reloads);
611 }
612
613 #define INVALID_FILE_FORMAT assert(0 && "Invalid file format.")
614 #define BUFLEN 32
615 #define BUFCONV " %32s "
616
617 /**
618  * Read in the actions performed by the external allocator.
619  * Apply these transformations to the irg.
620  * @return 1 if an allocation was read in. 0 otherwise.
621  */
622 static int read_and_apply_results(be_raext_env_t *raenv, char *filename) {
623         FILE *f;
624         char buf[BUFLEN];
625         int is_allocation = 0;
626
627         if (!(f = fopen(filename, "rt"))) {
628                 fprintf(stderr, "Could not open file %s for reading\n", filename);
629                 assert(0);
630                 exit(0xdeadbeef);
631         }
632         raenv->f = f;
633
634         /* read the action */
635         if (fscanf(f, BUFCONV, buf) != 1)
636                 INVALID_FILE_FORMAT;
637
638         /* do we spill */
639         if (!strcmp(buf, "spills")) {
640                 int var_nr;
641                 while (fscanf(f, " %d ", &var_nr) == 1)
642                         var_add_spills_and_reloads(raenv, var_nr);
643         } else
644
645         /* or do we allocate */
646         if (!strcmp(buf, "allocs")) {
647                 int var_nr, reg_nr;
648
649                 is_allocation = 1;
650                 while (fscanf(f, " %d %d ", &var_nr, &reg_nr) == 2) {
651                         ir_node *irn;
652                         pset *vals = be_get_var_values(raenv->vars, var_nr);
653
654                         assert(vals && "Variable nr does not exist!");
655                         pset_foreach(vals, irn)
656                                 arch_set_irn_register(raenv->aenv, irn, arch_register_for_index(raenv->cls, reg_nr));
657                 }
658         } else
659                 INVALID_FILE_FORMAT;
660
661         if (!feof(f))
662                 INVALID_FILE_FORMAT;
663
664         fclose(f);
665
666         return is_allocation;
667 }
668
669 static void check_allocation(be_raext_env_t *raenv) {
670         int i, o;
671         be_lv_t *lv = raenv->birg->lv;
672
673         for (i=0; i<raenv->n_cls_vars; ++i) {
674                 be_var_info_t *vi1 = raenv->cls_vars[i];
675
676                 if (vi1->var_nr == SET_REMOVED)
677                         continue;
678
679                 for (o=0; o<i; ++o) {
680                         be_var_info_t *vi2 = raenv->cls_vars[o];
681                         ir_node *irn1, *irn2;
682
683                         if (vi2->var_nr == SET_REMOVED)
684                                 continue;
685
686                         pset_foreach(vi1->values, irn1)
687                                 pset_foreach(vi2->values, irn2)
688                                         if (values_interfere(lv, irn1, irn2) && arch_get_irn_register(raenv->aenv, irn1) == arch_get_irn_register(raenv->aenv, irn2)) {
689                                                 dump_ir_block_graph_sched(raenv->irg, "ERROR");
690                                                 ir_fprintf(stdout, "SSA values %+F and %+F interfere. They belong to variable %d and %d respectively.\n", irn1, irn2, vi1->var_nr, vi2->var_nr);
691                                                 assert(0 && "ERROR graph dumped");
692                                         }
693                 }
694         }
695 }
696
697 /******************************************************************************
698     __  __       _
699    |  \/  |     (_)
700    | \  / | __ _ _ _ __
701    | |\/| |/ _` | | '_ \
702    | |  | | (_| | | | | |
703    |_|  |_|\__,_|_|_| |_|
704  *****************************************************************************/
705
706 /**
707  * Default values for options
708  */
709 static char callee[128] = "\"E:/user/kimohoff/public/register allocator\"";
710 //static char callee[128] = "/ben/kimohoff/ipd-registerallocator/register_allocator";
711
712
713 /**
714  * Allocate registers with an external program using a text-file interface.
715  *
716  * Do some computations (SSA-destruction and mapping of values--vars)
717  * Write file
718  * Execute external program
719  * Read in results and apply them
720  *
721  */
722 static void be_ra_extern_main(be_irg_t *birg) {
723         be_main_env_t *env = birg->main_env;
724         ir_graph *irg = birg->irg;
725
726         be_raext_env_t raenv;
727         int clsnr, clss;
728
729         be_assure_dom_front(birg);
730         be_assure_liveness(birg);
731         edges_assure(irg);
732
733         raenv.irg      = irg;
734         raenv.birg     = birg;
735         raenv.aenv     = env->arch_env;
736         FIRM_DBG_REGISTER(raenv.dbg, "firm.be.raextern");
737
738         /* Insert copies for constraints */
739         for(clsnr = 0, clss = arch_isa_get_n_reg_class(raenv.aenv->isa); clsnr < clss; ++clsnr) {
740                 raenv.cls = arch_isa_get_reg_class(raenv.aenv->isa, clsnr);
741                 handle_constraints(&raenv);
742         }
743
744         be_dump(irg, "-extern-constr", dump_ir_block_graph_sched);
745
746         /* SSA destruction respectively transformation into "Conventional SSA" */
747         raenv.vars = be_ssa_destr_simple(irg, env->arch_env);
748         be_dump(irg, "-extern-ssadestr", dump_ir_block_graph_sched);
749
750
751         /* For all register classes */
752         for(clsnr = 0, clss = arch_isa_get_n_reg_class(raenv.aenv->isa); clsnr < clss; ++clsnr) {
753                 int done, round = 1;
754                 char out[256], in[256];
755
756                 raenv.cls = arch_isa_get_reg_class(raenv.aenv->isa, clsnr);
757
758                 extract_vars_of_cls(&raenv);
759
760                 do {
761                         ir_snprintf(out, sizeof(out), "%F-%s-%d.ra", irg, raenv.cls->name, round);
762                         ir_snprintf(in, sizeof(in), "%F-%s-%d.ra.res", irg, raenv.cls->name, round);
763
764                         be_liveness(irg);
765
766                         dump_to_file(&raenv, out);
767                         execute(callee, out, in);
768                         done = read_and_apply_results(&raenv, in);
769                         be_abi_fix_stack_nodes(birg->abi);
770
771                         ir_snprintf(in, sizeof(in), "-extern-%s-round-%d", raenv.cls->name, round);
772                         be_dump(irg, in, dump_ir_block_graph_sched);
773
774                         round++;
775                 } while (!done);
776
777                 check_allocation(&raenv);
778
779                 free(raenv.cls_vars);
780         }
781
782         be_dump(irg, "-extern-alloc", dump_ir_block_graph_sched);
783
784         /* Clean up */
785         free_ssa_destr_simple(raenv.vars);
786
787         be_invalidate_liveness(birg);
788 }
789
790 /******************************************************************************
791      ____        _   _
792     / __ \      | | (_)
793    | |  | |_ __ | |_ _  ___  _ __  ___
794    | |  | | '_ \| __| |/ _ \| '_ \/ __|
795    | |__| | |_) | |_| | (_) | | | \__ \
796     \____/| .__/ \__|_|\___/|_| |_|___/
797           | |
798           |_|
799  *****************************************************************************/
800
801 static const lc_opt_enum_func_ptr_items_t ssa_destr_items[] = {
802         { "simple",     (int (*)(void)) be_ssa_destr_simple }, /* TODO make (void*) casts nicer */
803         { NULL,      NULL }
804 };
805
806 static set* (*ssa_destr)(ir_graph*,const arch_env_t*) = be_ssa_destr_simple;
807
808 static lc_opt_enum_func_ptr_var_t ssa_destr_var = {
809          (int (**)(void)) &ssa_destr, ssa_destr_items
810 };
811
812 static const lc_opt_table_entry_t be_ra_extern_options[] = {
813         LC_OPT_ENT_ENUM_FUNC_PTR("ssa_destr", "SSA destruction flavor", &ssa_destr_var),
814         LC_OPT_ENT_STR("callee", "The external program to call", callee, sizeof(callee)),
815         { NULL }
816 };
817
818 static be_ra_t be_ra_external_allocator = {
819         be_ra_extern_main
820 };
821
822 void be_init_raextern(void) {
823         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
824         lc_opt_entry_t *blocksched_grp = lc_opt_get_grp(be_grp, "ra");
825         lc_opt_entry_t *ext_grp = lc_opt_get_grp(blocksched_grp, "ext");
826
827         lc_opt_add_table(ext_grp, be_ra_extern_options);
828
829         be_register_allocator("ext", &be_ra_external_allocator);
830 }
831 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_raextern);
832
833 #endif