fixed memory leak
[libfirm] / ir / be / beraextern.c
index cc4e95b..d35c2f3 100644 (file)
@@ -58,10 +58,11 @@ alloc               ::= node-nr reg-nr .
 #include "config.h"
 #endif
 
-#ifdef WIN32
-#include <malloc.h>
-#else
-#include <alloca.h>
+#ifdef HAVE_MALLOC_H
+ #include <malloc.h>
+#endif
+#ifdef HAVE_ALLOCA_H
+ #include <alloca.h>
 #endif
 
 #include <stdio.h>
@@ -86,12 +87,14 @@ alloc               ::= node-nr reg-nr .
 #include "phiclass.h"
 
 #include "beraextern.h"
+#include "beabi.h"
 #include "bearch.h"
 #include "benode_t.h"
 #include "beirgmod.h"
-#include "besched.h"
+#include "besched_t.h"
 #include "beutil.h"
 #include "belive_t.h"
+#include "beinsn_t.h"
 
 #define DBG_LEVEL 2
 
@@ -101,7 +104,6 @@ typedef struct _var_info_t var_info_t;
  * Environment with all the needed stuff
  */
 typedef struct _be_raext_env_t {
-       firm_dbg_module_t *dbg;
        arch_env_t *aenv;
        const arch_register_class_t *cls;
        ir_graph *irg;
@@ -111,6 +113,7 @@ typedef struct _be_raext_env_t {
        set *vars;                              /**< contains all var_info_t */
        int n_cls_vars;                 /**< length of the array cls_vars */
        var_info_t **cls_vars;  /**< only the var_infos for current cls. needed for double iterating */
+       DEBUG_ONLY(firm_dbg_module_t *dbg;)
 } be_raext_env_t;
 
 
@@ -191,48 +194,65 @@ static int get_loop_weight(ir_node *irn) {
                                                 |_|
  *****************************************************************************/
 
-static void handle_constraints_walker(ir_node *irn, void *env) {
-       be_raext_env_t *raenv = env;
-       arch_register_req_t req;
-       int pos, max;
+static void handle_constraints_insn(be_raext_env_t *env, be_insn_t *insn)
+{
+       ir_node *bl = get_nodes_block(insn->irn);
+       int i;
 
-       /* handle output constraints
-        * user -> irn    becomes    user -> cpy -> irn
-        */
-       arch_get_register_req(raenv->aenv, &req, irn, -1);
-       if (arch_register_req_is(&req, limited)) {
-               ir_node *cpy = be_new_Copy(req.cls, raenv->irg, get_nodes_block(irn), irn);
-               const ir_edge_t *edge;
+       for(i = 0; i < insn->use_start; ++i) {
+               be_operand_t *op = &insn->ops[i];
 
-               /* all users of the irn use the copy instead */
-               sched_add_after(irn, cpy);
-               foreach_out_edge(irn, edge)
-                       set_irn_n(edge->src, edge->pos, cpy);
+               if(op->has_constraints) {
+                       ir_node *cpy = be_new_Copy(op->req.cls, env->irg, bl, op->carrier);
+                       sched_add_before(insn->next_insn, cpy);
+                       edges_reroute(op->carrier, cpy, env->irg);
+               }
        }
 
+       for(i = insn->use_start; i < insn->n_ops; ++i) {
+               be_operand_t *op = &insn->ops[i];
 
-       /* handle input constraints by converting them into output constraints
-        * of copies of the former argument
-        * irn -> arg   becomes  irn -> copy -> arg
-     */
-       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, limited)) {
-                       ir_node *arg = get_irn_n(irn, pos);
-                       ir_node *cpy = be_new_Copy(req.cls, raenv->irg, get_nodes_block(irn), arg);
+               if(op->has_constraints) {
+                       ir_node *cpy = be_new_Copy(op->req.cls, env->irg, bl, op->carrier);
+                       sched_add_before(insn->irn, cpy);
+                       set_irn_n(insn->irn, op->pos, cpy);
+                       be_set_constr_limited(cpy, BE_OUT_POS(0), &op->req);
+               }
+       }
+}
 
-                       /* use the copy instead */
-                       sched_add_before(irn, cpy);
-                       set_irn_n(irn, pos, cpy);
+static void handle_constraints_block(ir_node *bl, void *data)
+{
+       be_raext_env_t *raenv = data;
+       int active            = bl != get_irg_start_block(raenv->irg);
 
-                       /* set an out constraint for the copy */
-                       be_set_constr_limited(cpy, -1, &req);
-               }
+       ir_node *irn;
+       be_insn_env_t ie;
+       struct obstack obst;
+
+       ie.cls           = raenv->cls;
+       ie.aenv          = raenv->aenv;
+       ie.obst          = &obst;
+       ie.ignore_colors = NULL;
+       obstack_init(&obst);
+
+       irn = sched_first(bl);
+       while(!sched_is_end(irn)) {
+               be_insn_t *insn = be_scan_insn(&ie, irn);
+
+               if(insn->has_constraints)
+                       handle_constraints_insn(raenv, insn);
+
+               if(be_is_Barrier(irn))
+                       active = !active;
+
+               irn = insn->next_insn;
+               obstack_free(&obst, insn);
        }
 }
 
 static void handle_constraints(be_raext_env_t *raenv) {
-       irg_block_walk_graph(raenv->irg, NULL, handle_constraints_walker, raenv);
+       irg_block_walk_graph(raenv->irg, NULL, handle_constraints_block, raenv);
 }
 
 
@@ -288,9 +308,10 @@ static ir_node *insert_copies(be_raext_env_t *raenv, ir_node *start_phi, int pos
        if (has_been_done(start_phi, pos))
                return NULL;
 
-       /* In case this is a 'normal' phi we insert into
-        * the schedule before the pred_blk irn */
-       last_cpy = pred_blk;
+       /* In case this is a 'normal' phi we insert at the
+        * end of the pred block before cf nodes */
+       last_cpy = sched_skip(pred_blk, 0, sched_skip_cf_predicator, raenv->aenv);
+       last_cpy = sched_next(last_cpy);
 
        /* If we detect a loop stop recursion. */
        if (arg == start_phi) {
@@ -303,7 +324,7 @@ static ir_node *insert_copies(be_raext_env_t *raenv, ir_node *start_phi, int pos
                /* At least 2 phis are involved */
                /* Insert a loop breaking copy (an additional variable T) */
                loop_breaker = be_new_Copy(raenv->cls, raenv->irg, pred_blk, start_phi);
-               sched_add_before(pred_blk, loop_breaker);
+               sched_add_before(last_cpy, loop_breaker);
 
                arg = loop_breaker;
        }
@@ -345,6 +366,9 @@ static void ssa_destr_simple_walker(ir_node *blk, void *env) {
                        if (!is_Phi(phi))
                                break;
 
+                       if (arch_irn_is(raenv->aenv, phi, ignore))
+                               continue;
+
                        raenv->cls = arch_get_irn_reg_class(raenv->aenv, phi, -1);
                        insert_copies(raenv, phi, pos, phi);
                }
@@ -494,7 +518,7 @@ static void extract_vars_of_cls(be_raext_env_t *raenv) {
        int count = 0;
        var_info_t *vi;
 
-       raenv->cls_vars = malloc(set_count(raenv->vars) * sizeof(*raenv->cls_vars));
+       raenv->cls_vars = xmalloc(set_count(raenv->vars) * sizeof(*raenv->cls_vars));
        assert(raenv->cls_vars);
 
        set_foreach(raenv->vars, vi)
@@ -533,7 +557,7 @@ static INLINE int get_spill_costs(be_raext_env_t *raenv, var_info_t *vi) {
        int c_spills=0, c_reloads=0;
 
        pset_foreach(vi->values, irn) {
-               if (arch_irn_is_ignore(raenv->aenv, irn) || be_is_Reload(irn)) {
+               if (arch_irn_is(raenv->aenv, irn, ignore) || be_is_Reload(irn)) {
                        pset_break(vi->values);
                        return UNSPILLABLE;
                }
@@ -616,16 +640,16 @@ static void dump_affinities_walker(ir_node *irn, void *env) {
        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))
+       if (arch_get_irn_reg_class(raenv->aenv, irn, -1) != raenv->cls || arch_irn_is(raenv->aenv, irn, ignore))
                return;
 
        vi1 = get_var_info(irn);
 
        /* copies have affinities */
        if (arch_irn_classify(raenv->aenv, irn) == arch_irn_class_copy) {
-               ir_node *other = get_irn_n(irn, 0);
+               ir_node *other = be_get_Copy_op(irn);
 
-               if (! arch_irn_is_ignore(raenv->aenv, other)) {
+               if (! arch_irn_is(raenv->aenv, other, ignore)) {
                        vi2 = get_var_info(other);
 
                        fprintf(raenv->f, "(%d, %d, %d)\n",  vi1->var_nr, vi2->var_nr, get_affinity_weight(irn));
@@ -637,7 +661,7 @@ 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) && arch_irn_is_ignore(raenv->aenv, req.other_same)) {
+               if (arch_register_req_is(&req, should_be_same) && arch_irn_is(raenv->aenv, req.other_same, ignore)) {
                        vi2 = get_var_info(req.other_same);
 
                        fprintf(raenv->f, "(%d, %d, %d)\n",  vi1->var_nr, vi2->var_nr, get_affinity_weight(irn));
@@ -754,7 +778,7 @@ static INLINE void var_add_spills_and_reloads(be_raext_env_t *raenv, int var_nr)
 
        assert(spill && "There must be at least one non-phi-node");
 
-       mode = get_irn_mode(get_irn_n(spill, 0));
+       mode = get_irn_mode(get_irn_n(spill, be_pos_Spill_val));
 
        /* insert reloads and wire them arbitrary*/
        pset_foreach(vi->values, irn)
@@ -796,8 +820,7 @@ static INLINE void var_add_spills_and_reloads(be_raext_env_t *raenv, int var_nr)
 
                /* ...add new vars for each non-phi-member */
                pset_foreach(spills, irn) {
-                       ir_node *spilled = get_irn_n(irn, 1);
-                       assert(get_irn_node_nr(spilled) != 1089);
+                       ir_node *spilled = get_irn_n(irn, be_pos_Spill_val);
                        raenv->cls_vars[raenv->n_cls_vars++] = var_add_value(raenv, get_irn_node_nr(spilled), spilled);
                }
        }
@@ -930,21 +953,25 @@ static void be_ra_extern_main(const be_irg_t *bi) {
        var_info_t *vi;
 
        compute_doms(irg);
+       edges_assure(irg);
 
        raenv.irg      = irg;
        raenv.aenv     = env->arch_env;
        raenv.dom_info = be_compute_dominance_frontiers(irg);
        raenv.vars     = new_set(compare_var_infos, 64);
-       raenv.dbg      = firm_dbg_register("ir.be.raextern");
-       firm_dbg_set_mask(raenv.dbg, DBG_LEVEL);
+       FIRM_DBG_REGISTER(raenv.dbg, "firm.be.raextern");
 
        /* Insert copies for constraints */
-       handle_constraints(&raenv);
-       dump_ir_block_graph_sched(irg, "-extern-constr");
+       for(clsnr = 0, clss = arch_isa_get_n_reg_class(raenv.aenv->isa); clsnr < clss; ++clsnr) {
+               raenv.cls = arch_isa_get_reg_class(raenv.aenv->isa, clsnr);
+               handle_constraints(&raenv);
+       }
+
+       be_dump(irg, "-extern-constr", dump_ir_block_graph_sched);
 
        /* SSA destruction respectively transformation into "Conventional SSA" */
        ssa_destr(&raenv);
-       dump_ir_block_graph_sched(irg, "-extern-ssadestr");
+       be_dump(irg, "-extern-ssadestr", dump_ir_block_graph_sched);
 
        /* Mapping of SSA-Values <--> Variables */
        phi_class_compute(irg);
@@ -970,9 +997,10 @@ static void be_ra_extern_main(const be_irg_t *bi) {
                        dump_to_file(&raenv, out);
                        execute(callee, out, in);
                        done = read_and_apply_results(&raenv, in);
+                       be_abi_fix_stack_nodes(bi->abi);
 
                        ir_snprintf(in, sizeof(in), "-extern-%s-round-%d", raenv.cls->name, round);
-                       dump_ir_block_graph_sched(irg, in);
+                       be_dump(irg, in, dump_ir_block_graph_sched);
 
                        round++;
                } while (!done);
@@ -982,7 +1010,7 @@ static void be_ra_extern_main(const be_irg_t *bi) {
                free(raenv.cls_vars);
        }
 
-       dump_ir_block_graph_sched(irg, "-extern-alloc");
+       be_dump(irg, "-extern-alloc", dump_ir_block_graph_sched);
 
        /* Clean up */
        set_foreach(raenv.vars, vi)