changed attribute structure
[libfirm] / ir / be / beraextern.c
index 9f2e628..ac38070 100644 (file)
@@ -28,15 +28,17 @@ node                ::= node-info
 
 node-info      ::= node-nr spill-costs .
 
-interf         ::= 'interferences' '{' edge* '}' .                     // Interference edges of the graph
+interf         ::= 'interferences' '{' i-edge* '}' .           // Interference edges of the graph
 
-affinities     ::= 'affinities' '{' edge* '}' .                        // Affinity edges of the graph
+i-edge         ::= '(' node-nr ',' node-nr ')' .
 
-edge           ::= '(' node-nr ',' node-nr ')' .
+affinities     ::= 'affinities' '{' a-edge* '}' .                      // Affinity edges of the graph
 
+a-edge         ::= '(' node-nr ',' node-nr ',' weight ')' .
 
-regcount, node-nr ::= int32 .
-spill-costs ::= uint32 .
+
+weight, regcount, node-nr ::= int32 .
+spill-costs ::= int32 .                                                                        // negative spill costs indicate unspillable
 
 The output file format
 -----------------------
@@ -161,6 +163,23 @@ static INLINE ir_node *get_first_phi(pset *s) {
        return NULL;
 }
 
+static int get_loop_weight(ir_node *irn) {
+       int cost = 0;
+       ir_loop *loop = get_irn_loop(get_nodes_block(irn));
+
+       if (loop) {
+               int d = get_loop_depth(loop);
+               cost = d*d;
+       }
+       return cost+1;
+}
+
+#define get_const_weight(irn) (1)
+
+#define get_spill_weight(irn)    get_loop_weight(irn)
+#define get_reload_weight(irn)   get_loop_weight(irn)
+#define get_affinity_weight(irn) get_loop_weight(irn)
+
 /******************************************************************************
     _____                _            _____            _
    / ____|              | |          / ____|          (_)
@@ -507,12 +526,12 @@ static INLINE void dump_constraint(be_raext_env_t *raenv, ir_node *irn, int pos)
 
 static INLINE unsigned int get_spill_costs(be_raext_env_t *raenv, var_info_t *vi) {
        ir_node *irn;
-       int n_spills=0, n_reloads=0;
+       int c_spills=0, c_reloads=0;
 
        pset_foreach(vi->values, irn) {
                if (arch_irn_is_ignore(raenv->aenv, irn)) {
                        pset_break(vi->values);
-                       return UINT_MAX;
+                       return -1;
                }
 
                if (is_Phi(irn)) {
@@ -520,14 +539,14 @@ static INLINE unsigned int get_spill_costs(be_raext_env_t *raenv, var_info_t *vi
                        const ir_edge_t *edge;
                        foreach_out_edge(irn, edge)
                                if (!is_Phi(edge->src))
-                                       n_reloads++;
+                                       c_reloads += get_reload_weight(edge->src);
                } else {
                        /* number of spills is the number of non-phi values for this var */
-                       n_spills++;
+                       c_spills += get_spill_weight(irn);
                }
        }
 
-       return n_spills + n_reloads;
+       return c_spills + c_reloads;
 }
 
 static void dump_nodes(be_raext_env_t *raenv) {
@@ -583,21 +602,26 @@ static void dump_interferences(be_raext_env_t *raenv) {
        fprintf(f, "}\n");
 }
 
-
 static void dump_affinities_walker(ir_node *irn, void *env) {
        be_raext_env_t *raenv = env;
        arch_register_req_t req;
        int pos, max;
        var_info_t *vi1, *vi2;
 
+       if (arch_get_irn_reg_class(raenv->aenv, irn, -1) == NULL || arch_irn_is_ignore(raenv->aenv, irn))
+               return;
+
        vi1 = get_var_info(irn);
 
        /* copies have affinities */
-       /* TODO? remove this case by adding should_be_equal requirements */
        if (arch_irn_classify(raenv->aenv, irn) == arch_irn_class_copy) {
-               vi2 = get_var_info(get_irn_n(irn, 0));
+               ir_node *other = get_irn_n(irn, 0);
+
+               if (! arch_irn_is_ignore(raenv->aenv, other)) {
+                       vi2 = get_var_info(other);
 
-               fprintf(raenv->f, "(%d, %d)\n",  vi1->var_nr, vi2->var_nr);
+                       fprintf(raenv->f, "(%d, %d, %d)\n",  vi1->var_nr, vi2->var_nr, get_affinity_weight(irn));
+               }
        }
 
 
@@ -605,10 +629,10 @@ static void dump_affinities_walker(ir_node *irn, void *env) {
        for (pos = 0, max = get_irn_arity(irn); pos<max; ++pos) {
                arch_get_register_req(raenv->aenv, &req, irn, pos);
 
-               if (arch_register_req_is(&req, should_be_same)) {
+               if (arch_register_req_is(&req, should_be_same) && arch_irn_is_ignore(raenv->aenv, req.other_same)) {
                        vi2 = get_var_info(req.other_same);
 
-                       fprintf(raenv->f, "(%d, %d)\n",  vi1->var_nr, vi2->var_nr);
+                       fprintf(raenv->f, "(%d, %d, %d)\n",  vi1->var_nr, vi2->var_nr, get_affinity_weight(irn));
                }
        }
 }
@@ -802,7 +826,6 @@ static int read_and_apply_results(be_raext_env_t *raenv, char *filename) {
                int var_nr;
                while (fscanf(f, " %d ", &var_nr) == 1)
                        var_add_spills_and_reloads(raenv, var_nr);
-               be_liveness(raenv->irg);
        } else
 
        /* or do we allocate */
@@ -829,6 +852,33 @@ static int read_and_apply_results(be_raext_env_t *raenv, char *filename) {
        return is_allocation;
 }
 
+static void check_allocation(be_raext_env_t *raenv) {
+       int i, o;
+
+       for (i=0; i<raenv->n_cls_vars; ++i) {
+               var_info_t *vi1 = raenv->cls_vars[i];
+
+               if (vi1->var_nr == SET_REMOVED)
+                       continue;
+
+               for (o=0; o<i; ++o) {
+                       var_info_t *vi2 = raenv->cls_vars[o];
+                       ir_node *irn1, *irn2;
+
+                       if (vi2->var_nr == SET_REMOVED)
+                               continue;
+
+                       pset_foreach(vi1->values, irn1)
+                               pset_foreach(vi2->values, irn2)
+                                       if (values_interfere(irn1, irn2)) {
+                                               dump_ir_block_graph_sched(raenv->irg, "ERROR");
+                                               ir_fprintf(stdout, "SSA values %+F and %+F interfere. They belong to varible %d and %d respectively.\n", irn1, irn2, vi1->var_nr, vi2->var_nr);
+                                               assert(0 && "ERROR graph dumped");
+                                       }
+               }
+       }
+}
+
 /******************************************************************************
     __  __       _
    |  \/  |     (_)
@@ -884,7 +934,7 @@ static void be_ra_extern_main(const be_irg_t *bi) {
        phi_class_compute(irg);
        be_clear_links(irg);
        irg_walk_graph(irg, values_to_vars, NULL, &raenv);
-       be_liveness(irg);
+
 
        /* For all register classes */
        for(clsnr = 0, clss = arch_isa_get_n_reg_class(raenv.aenv->isa); clsnr < clss; ++clsnr) {
@@ -899,6 +949,8 @@ static void be_ra_extern_main(const be_irg_t *bi) {
                        ir_snprintf(out, sizeof(out), "%F-%s-%d.ra", irg, raenv.cls->name, round);
                        ir_snprintf(in, sizeof(in), "%F-%s-%d.ra.res", irg, raenv.cls->name, round);
 
+                       be_liveness(irg);
+
                        dump_to_file(&raenv, out);
                        execute(callee, out, in);
                        done = read_and_apply_results(&raenv, in);
@@ -909,6 +961,8 @@ static void be_ra_extern_main(const be_irg_t *bi) {
                        round++;
                } while (!done);
 
+               check_allocation(&raenv);
+
                free(raenv.cls_vars);
        }