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