make use of generated proj nums
[libfirm] / ir / be / beraextern.c
index cbd55d7..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>
@@ -93,6 +94,7 @@ alloc         ::= node-nr reg-nr .
 #include "besched_t.h"
 #include "beutil.h"
 #include "belive_t.h"
+#include "beinsn_t.h"
 
 #define DBG_LEVEL 2
 
@@ -102,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;
@@ -112,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;
 
 
@@ -192,46 +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);
+       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);
-               edges_reroute(irn, cpy, raenv->irg);
+               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_walk_graph(raenv->irg, NULL, handle_constraints_walker, raenv);
+       irg_block_walk_graph(raenv->irg, NULL, handle_constraints_block, raenv);
 }
 
 
@@ -345,7 +366,7 @@ static void ssa_destr_simple_walker(ir_node *blk, void *env) {
                        if (!is_Phi(phi))
                                break;
 
-                       if (arch_irn_is_ignore(raenv->aenv, phi))
+                       if (arch_irn_is(raenv->aenv, phi, ignore))
                                continue;
 
                        raenv->cls = arch_get_irn_reg_class(raenv->aenv, phi, -1);
@@ -497,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)
@@ -536,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;
                }
@@ -619,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) != raenv->cls || 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, be_pos_Copy_orig);
+               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));
@@ -640,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));
@@ -938,11 +959,14 @@ static void be_ra_extern_main(const be_irg_t *bi) {
        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);
+       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" */