fixed phi spilling
[libfirm] / ir / be / bespillbelady.c
index 02f5293..b91c645 100644 (file)
@@ -6,7 +6,7 @@
  *
  */
 #ifdef HAVE_CONFIG_H
-#include <config.h>
+#include "config.h"
 #endif
 
 #ifdef HAVE_ALLOCA_H
@@ -357,14 +357,13 @@ static loc_t to_take_or_not_to_take(belady_env_t *env, ir_node* first,
        be_next_use_t next_use;
        loc_t loc;
        loc.time = USES_INFINITY;
+       loc.irn = node;
 
        if (!arch_irn_consider_in_reg_alloc(env->arch, env->cls, node)) {
                loc.time = USES_INFINITY;
                return loc;
        }
 
-       loc.irn = node;
-
        /* We have to keep nonspillable nodes in the workingset */
        if(arch_irn_get_flags(env->arch, node) & arch_irn_flags_dont_spill) {
                loc.time = 0;
@@ -378,6 +377,9 @@ static loc_t to_take_or_not_to_take(belady_env_t *env, ir_node* first,
                assert(is_Phi(node));
                loc.time = USES_INFINITY;
                DBG((dbg, DBG_START, "    %+F not taken (dead)\n", node));
+               if(is_Phi(node)) {
+                       be_spill_phi(env->senv, node);
+               }
                return loc;
        }
 
@@ -432,8 +434,6 @@ static void compute_live_ins(ir_node *block, void *data) {
                                ARR_APP1(loc_t, delayed, loc);
                        else
                                ARR_APP1(loc_t, starters, loc);
-               } else {
-                       be_spill_phi(env->senv, irn);
                }
        }
 
@@ -451,8 +451,8 @@ static void compute_live_ins(ir_node *block, void *data) {
                }
        }
 
-       assert(ARR_LEN(delayed) <= pressure);
        pressure            = be_get_loop_pressure(env->loop_ana, env->cls, loop);
+       assert(ARR_LEN(delayed) <= pressure);
        free_slots          = env->n_regs - ARR_LEN(starters);
        free_pressure_slots = env->n_regs - (pressure - ARR_LEN(delayed));
        free_slots          = MIN(free_slots, free_pressure_slots);
@@ -460,6 +460,16 @@ static void compute_live_ins(ir_node *block, void *data) {
        for (i = 0; i < ARR_LEN(delayed) && i < free_slots; ++i) {
                DBG((dbg, DBG_START, "    delayed %+F taken\n", delayed[i].irn));
                ARR_APP1(loc_t, starters, delayed[i]);
+               delayed[i].irn = NULL;
+       }
+
+       /* spill all delayed phis which didn't make it into start workset */
+       for (i = ARR_LEN(delayed) - 1; i >= 0; --i) {
+               ir_node *irn = delayed[i].irn;
+               if (irn && is_Phi(irn)) {
+                       DBG((dbg, DBG_START, "    spilling delayed phi %+F\n", irn));
+                       be_spill_phi(env->senv, irn);
+               }
        }
        DEL_ARR_F(delayed);
 
@@ -671,9 +681,6 @@ void be_spill_belady_spill_env(be_irg_t *birg, const arch_register_class_t *cls,
        belady_env_t env;
        ir_graph *irg = be_get_birg_irg(birg);
 
-       FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
-       //firm_dbg_set_mask(dbg, DBG_SPILL);
-
        be_invalidate_liveness(birg);
        be_assure_liveness(birg);
        /* construct control flow loop tree */
@@ -722,6 +729,7 @@ void be_init_spillbelady(void)
        };
 
        be_register_spiller("belady", &belady_spiller);
+       FIRM_DBG_REGISTER(dbg, "firm.be.spill.belady");
 }
 
 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);