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