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