Several BugFixes and updates:
[libfirm] / ir / be / bespillslots.c
index e00e164..815119f 100644 (file)
@@ -1,8 +1,28 @@
 /*
- * Author:      Matthias Braun
- * Date:               26.7.06
- * Copyright:   (c) Universitaet Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
+/**
+ * @file
+ * @brief       Spillslot coalescer.
+ * @author      Matthias Braun
+ * @date        26.07.2006
+ * @version     $Id$
  */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -11,7 +31,7 @@
 #include <stdlib.h>
 
 #include "set.h"
-
+#include "array.h"
 #include "irgwalk.h"
 #include "ircons.h"
 #include "irprintf.h"
 #include "benodesets.h"
 #include "bestatevent.h"
 #include "bespilloptions.h"
+#include "bemodule.h"
+#include "bera.h"
+#include "beirg_t.h"
+#include "bearch_t.h"
 
 #define DBG_COALESCING         1
 #define DBG_INTERFERENCES      2
@@ -236,47 +260,6 @@ static int merge_interferences(be_fec_env_t *env, bitset_t** interferences,
        return res;
 }
 
-#if 0
-static void dump_interference_graph(be_fec_env_t *env, bitset_t **interferences, const char* suffix) {
-       char name[256];
-       int i;
-       int spillcount;
-       spill_t *spill;
-       FILE *f;
-       static int cnt = 0;
-
-       snprintf(name, sizeof(name), "%d-%s-spillslots-%s.vcg", cnt++, get_irg_dump_name(env->chordal_env->birg->irg), suffix);
-
-       f = fopen(name, "w");
-       assert(f != NULL);
-
-       fprintf(f, "graph: {\n");
-
-       spillcount = set_count(env->spills);
-       for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
-               int slotid = spill->spillslot;
-               fprintf(f, "\tnode: { title: \"n%d\" label: \"%d\" }\n", i, slotid);
-       }
-
-       for(i = 0; i < ARR_LEN(env->affinity_edges); ++i) {
-               affinity_edge_t *edge = env->affinity_edges[i];
-               fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: green }\n", edge->slot1, edge->slot2);
-       }
-
-       for(i = 0; i < spillcount; ++i) {
-               int i2;
-               for(i2 = 0; i2 < spillcount; ++i2) {
-                       if(bitset_is_set(interferences[i], i2)) {
-                               fprintf(f, "\tedge: { sourcename: \"n%d\" targetname: \"n%d\" color: red }\n", i, i2);
-                       }
-               }
-       }
-
-       fprintf(f, "}\n");
-       fclose(f);
-}
-#endif
-
 /**
  * A greedy coalescing algorithm for spillslots:
  *  1. Sort the list of affinity edges
@@ -319,10 +302,20 @@ static void do_greedy_coalescing(be_fec_env_t *env)
                interferences[i] = bitset_alloca(spillcount);
        }
 
-       // construct interferences
-       for(i = 0; i < spillcount; ++i) {
+       /* construct interferences */
+       for (i = 0; i < spillcount; ++i) {
+               ir_node *spill1 = spilllist[i]->spill;
+
+               if (is_NoMem(spill1))
+                       continue;
+
                for(i2 = i+1; i2 < spillcount; ++i2) {
-                       if(values_interfere(lv, spilllist[i]->spill, spilllist[i2]->spill)) {
+                       ir_node *spill2 = spilllist[i2]->spill;
+
+                       if (is_NoMem(spill2))
+                               continue;
+
+                       if (values_interfere(lv, spill1, spill2)) {
                                DBG((dbg, DBG_INTERFERENCES, "Slot %d and %d interfere\n", i, i2));
                                bitset_set(interferences[i], i2);
                                bitset_set(interferences[i2], i);
@@ -330,20 +323,20 @@ static void do_greedy_coalescing(be_fec_env_t *env)
                }
        }
 
-       // sort affinity edges
+       /* sort affinity edges */
        affinity_edge_count = ARR_LEN(env->affinity_edges);
        qsort(env->affinity_edges, affinity_edge_count, sizeof(env->affinity_edges[0]), cmp_affinity);
 
        //dump_interference_graph(env, interferences, "before");
 
-       // try to merge affine nodes
+       /* try to merge affine nodes */
        for(i = 0; i < affinity_edge_count; ++i) {
                const affinity_edge_t *edge = env->affinity_edges[i];
                int s1 = uf_find(spillslot_unionfind, edge->slot1);
                int s2 = uf_find(spillslot_unionfind, edge->slot2);
 
                /* test if values interfere */
-               if(bitset_is_set(interferences[s1], s2)) {
+               if (bitset_is_set(interferences[s1], s2)) {
                        assert(bitset_is_set(interferences[s2], s1));
                        continue;
                }
@@ -454,7 +447,7 @@ static ir_entity* create_stack_entity(be_fec_env_t *env, spill_slot_t *slot)
        ir_type *frame = get_irg_frame_type(irg);
        ir_entity *res = frame_alloc_area(frame, slot->size, slot->align, 0);
 
-       // adjust size of the entity type...
+       /* adjust size of the entity type... */
        ir_type *enttype = get_entity_type(res);
        set_type_size_bytes(enttype, slot->size);
 
@@ -563,7 +556,8 @@ static void assign_spillslots(be_fec_env_t *env)
                                }
                        }
                } else {
-                       arch_set_frame_entity(arch_env, node, slot->entity);
+                       if(!is_NoMem(node))
+                               arch_set_frame_entity(arch_env, node, slot->entity);
                }
        }
 
@@ -669,19 +663,17 @@ static int count_spillslots(const be_fec_env_t *env)
 be_fec_env_t *be_new_frame_entity_coalescer(be_irg_t *birg)
 {
        const arch_env_t *arch_env = birg->main_env->arch_env;
-
-       be_fec_env_t *env = xmalloc(sizeof(env[0]));
+       be_fec_env_t     *env      = xmalloc(sizeof(env[0]));
 
        be_assure_liveness(birg);
 
        obstack_init(&env->obst);
-       env->arch_env = arch_env;
-       env->birg = birg;
-       env->spills = new_set(cmp_spill, 10);
-       env->reloads = NEW_ARR_F(ir_node*, 0);
+       env->arch_env       = arch_env;
+       env->birg           = birg;
+       env->spills         = new_set(cmp_spill, 10);
+       env->reloads        = NEW_ARR_F(ir_node*, 0);
        env->affinity_edges = NEW_ARR_F(affinity_edge_t*, 0);
-       env->memperms = new_set(cmp_memperm, 10);
-       FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
+       env->memperms       = new_set(cmp_memperm, 10);
 
        return env;
 }
@@ -737,21 +729,28 @@ static void collect_spills_walker(ir_node *node, void *data)
        if (!arch_irn_class_is(arch_env, node, reload))
                return;
 
-       mode = get_irn_mode(node);
-       cls = arch_get_irn_reg_class(arch_env, node, -1);
+       mode  = get_irn_mode(node);
+       cls   = arch_get_irn_reg_class(arch_env, node, -1);
        align = arch_isa_get_reg_class_alignment(arch_env_get_isa(arch_env), cls);
 
        be_node_needs_frame_entity(env, node, mode, align);
 }
 
-void be_coalesce_spillslots(const be_chordal_env_t *chordal_env)
+void be_coalesce_spillslots(be_irg_t *birg)
 {
-       be_fec_env_t *env = be_new_frame_entity_coalescer(chordal_env->birg);
+       be_fec_env_t *env = be_new_frame_entity_coalescer(birg);
 
        /* collect reloads */
-       irg_walk_graph(chordal_env->irg, NULL, collect_spills_walker, env);
+       irg_walk_graph(birg->irg, NULL, collect_spills_walker, env);
 
        be_assign_entities(env);
 
        be_free_frame_entity_coalescer(env);
 }
+
+void be_init_spillslots(void)
+{
+       FIRM_DBG_REGISTER(dbg, "firm.be.spillslots");
+}
+
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillslots);