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