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