Implement access functions for machine nodes with machine operands
[libfirm] / ir / be / bespill.c
index 23d2a0a..b15f46e 100644 (file)
@@ -51,15 +51,15 @@ typedef struct _spill_ctx_t {
 } spill_ctx_t;
 
 struct _spill_env_t {
-       firm_dbg_module_t *dbg;
        const arch_register_class_t *cls;
        const be_chordal_env_t *chordal_env;
        struct obstack obst;
        set *spill_ctxs;
        set *spills;                            /**< all spill_info_t's, which must be placed */
-       pset *mem_phis;                         /**< set of all special spilled phis. allocated and freed seperately */
+       pset *mem_phis;                         /**< set of all special spilled phis. allocated and freed separately */
        decide_irn_t is_mem_phi;        /**< callback func to decide if a phi needs special spilling */
        void *data;                                     /**< data passed to all callbacks */
+       DEBUG_ONLY(firm_dbg_module_t *dbg;)
 };
 
 static int cmp_spillctx(const void *a, const void *b, size_t n) {
@@ -74,15 +74,17 @@ static int cmp_spillinfo(const void *x, const void *y, size_t size) {
        return ! (xx->spilled_node == yy->spilled_node);
 }
 
-spill_env_t *be_new_spill_env(firm_dbg_module_t *dbg,
-                                                         const be_chordal_env_t *chordal_env,
-                                                         decide_irn_t is_mem_phi, void *data) {
+DEBUG_ONLY(
+void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
+       env->dbg = dbg;
+}
+)
 
+spill_env_t *be_new_spill_env(const be_chordal_env_t *chordal_env, decide_irn_t is_mem_phi, void *data) {
        spill_env_t *env = xmalloc(sizeof(env[0]));
        env->spill_ctxs  = new_set(cmp_spillctx, 1024);
        env->spills      = new_set(cmp_spillinfo, 1024);
        env->cls         = chordal_env->cls;
-       env->dbg         = dbg;
        env->is_mem_phi  = is_mem_phi;
        env->data        = data;
        env->chordal_env = chordal_env;
@@ -386,14 +388,17 @@ typedef struct _spill_slot_t {
 } spill_slot_t;
 
 typedef struct _ss_env_t {
-       firm_dbg_module_t *dbg;
        struct obstack ob;
        be_chordal_env_t *cenv;
        pmap *slots;            /* maps spill_contexts to spill_slots */
-  pmap *types;    /* maps modes to types */
+       pmap *types;    /* maps modes to types */
+       DEBUG_ONLY(firm_dbg_module_t *dbg;)
 } ss_env_t;
 
 
+/**
+ * Walker: compute the spill slots
+ */
 static void compute_spill_slots_walker(ir_node *spill, void *env) {
        ss_env_t *ssenv = env;
        ir_node *ctx;
@@ -408,28 +413,44 @@ static void compute_spill_slots_walker(ir_node *spill, void *env) {
        entry = pmap_find(ssenv->slots, ctx);
 
        if (!entry) {
+               struct _arch_env_t *arch_env     = ssenv->cenv->birg->main_env->arch_env;
+               const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, spill, be_pos_Spill_val);
+               ir_mode *largest_mode            = arch_register_class_mode(cls);
+
                /* this is a new spill context */
                ss = obstack_alloc(&ssenv->ob, sizeof(*ss));
-               ss->members = pset_new_ptr(8);
-               ss->largest_mode = get_irn_mode(get_irn_n(spill, be_pos_Spill_val));
-               ss->size = get_mode_size_bytes(ss->largest_mode);
-               ss->align = ss->size; /* TODO Assumed for now */
+               ss->members      = pset_new_ptr(8);
+               ss->largest_mode = largest_mode;
+               ss->size         = get_mode_size_bytes(ss->largest_mode);
+               ss->align        = arch_isa_get_reg_class_alignment(arch_env->isa, cls);
                pmap_insert(ssenv->slots, ctx, ss);
        } else {
-               ir_node *irn;
                /* values with the same spill_ctx must go into the same spill slot */
                ss = entry->value;
-               assert(ss->size == (unsigned)get_mode_size_bytes(get_irn_mode(get_irn_n(spill, be_pos_Spill_val))) && "Different sizes for the same spill slot are not allowed yet.");
-               for (irn = pset_first(ss->members); irn; irn = pset_next(ss->members)) {
-                       /* use values_interfere here, because it uses the dominance check,
-                          which does work for values in memory */
-                       assert(!values_interfere(spill, irn) && "Spills for the same spill slot must not interfere!");
+
+#ifndef NDEBUG
+               /* ugly mega assert :-) */
+               {
+                       ir_node *irn;
+                       struct _arch_env_t *arch_env     = ssenv->cenv->birg->main_env->arch_env;
+                       const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, spill, be_pos_Spill_val);
+                       int size = get_mode_size_bytes(arch_register_class_mode(cls));
+                       assert(ss->size == size && "Different sizes for the same spill slot are not allowed.");
+                       for (irn = pset_first(ss->members); irn; irn = pset_next(ss->members)) {
+                               /* use values_interfere here, because it uses the dominance check,
+                                        which does work for values in memory */
+                               assert(!values_interfere(spill, irn) && "Spills for the same spill slot must not interfere!");
+                       }
                }
+#endif /* NDEBUG */
        }
 
        pset_insert_ptr(ss->members, spill);
 }
 
+/**
+ * qsort compare function, sort spill slots by size.
+ */
 static int ss_sorter(const void *v1, const void *v2) {
        const spill_slot_t *ss1 = v1;
        const spill_slot_t *ss2 = v2;
@@ -520,24 +541,41 @@ interf_detected: /*nothing*/ ;
 /**
  * Returns a spill type for a mode. Keep them in a map to reduce
  * the number of types.
+ *
+ * @param types  a map containing all created types
+ * @param ss     the spill slot
+ *
+ * Note that type types should are identical for every mode.
+ * This rule might break if two different register classes return the same
+ * mode but different alignments.
  */
-static ir_type *get_spill_type(pmap *types, ir_mode *mode) {
-  pmap_entry *e = pmap_find(types, mode);
+static ir_type *get_spill_type(pmap *types, spill_slot_t *ss) {
+  pmap_entry *e = pmap_find(types, ss->largest_mode);
   ir_type *res;
 
   if (! e) {
                char buf[64];
-    snprintf(buf, sizeof(buf), "spill_slot_type_%s", get_mode_name(mode));
-    res = new_type_primitive(new_id_from_str(buf), mode);
-    pmap_insert(types, mode, res);
+    snprintf(buf, sizeof(buf), "spill_slot_type_%s", get_mode_name(ss->largest_mode));
+    res = new_type_primitive(new_id_from_str(buf), ss->largest_mode);
+               set_type_alignment_bytes(res, ss->align);
+    pmap_insert(types, ss->largest_mode, res);
   }
-  else
+  else {
     res = e->value;
+               assert(get_type_alignment_bytes(res) == (int)ss->align);
+       }
   return res;
 }
 
-static void assign_entities(ss_env_t *ssenv, int n, spill_slot_t **ss) {
-       int i, offset;
+/**
+ * Create spill slot entities on the frame type.
+ *
+ * @param ssenv   the spill environment
+ * @param n       number of spill slots
+ * @param ss      array of spill slots
+ */
+static void assign_entities(ss_env_t *ssenv, int n_slots, spill_slot_t *ss[]) {
+       int i, offset, frame_align;
        ir_type *frame = get_irg_frame_type(ssenv->cenv->irg);
 
        /* aligning by increasing frame size */
@@ -546,7 +584,7 @@ static void assign_entities(ss_env_t *ssenv, int n, spill_slot_t **ss) {
        set_type_size_bytes(frame, -1);
 
        /* create entities and assign offsets according to size and alignment*/
-       for (i=0; i<n; ++i) {
+       for (i = 0; i < n_slots; ++i) {
                char buf[64];
                ident *name;
                entity *spill_ent;
@@ -556,21 +594,22 @@ static void assign_entities(ss_env_t *ssenv, int n, spill_slot_t **ss) {
                snprintf(buf, sizeof(buf), "spill_slot_%d", i);
                name = new_id_from_str(buf);
 
-               spill_ent = new_entity(frame, name, get_spill_type(ssenv->types, ss[i]->largest_mode));
+               spill_ent = new_entity(frame, name, get_spill_type(ssenv->types, ss[i]));
 
                /* align */
                offset = round_up2(offset, ss[i]->align);
                /* set */
                set_entity_offset_bytes(spill_ent, offset);
                /* next possible offset */
-               offset += ss[i]->size;
+               offset += round_up2(ss[i]->size, ss[i]->align);
 
                pset_foreach(ss[i]->members, irn)
                        be_set_Spill_entity(irn, spill_ent);
        }
 
        /* set final size of stack frame */
-       set_type_size_bytes(frame, offset);
+       frame_align = get_type_alignment_bytes(frame);
+       set_type_size_bytes(frame, round_up2(offset, frame_align));
 }
 
 void be_compute_spill_offsets(be_chordal_env_t *cenv) {