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