rename tarval to ir_tarval
[libfirm] / ir / opt / tailrec.c
index 15932a5..22b04b1 100644 (file)
@@ -52,7 +52,7 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg);
 /**
  * the environment for collecting data
  */
-typedef struct _collect_t {
+typedef struct collect_t {
        ir_node *proj_X;      /**< initial exec proj */
        ir_node *block;       /**< old first block */
        int     blk_idx;      /**< cfgpred index of the initial exec in block */
@@ -63,7 +63,8 @@ typedef struct _collect_t {
 /**
  * walker for collecting data, fills a collect_t environment
  */
-static void collect_data(ir_node *node, void *env) {
+static void collect_data(ir_node *node, void *env)
+{
        collect_t *data = env;
        ir_node *pred;
        ir_op *op;
@@ -96,7 +97,7 @@ static void collect_data(ir_node *node, void *env) {
                /*
                 * the first block has the initial exec as cfg predecessor
                 */
-               if (node != get_irg_start_block(current_ir_graph)) {
+               if (node != get_irg_start_block(get_irn_irg(node))) {
                        for (i = 0; i < n_pred; ++i) {
                                if (get_Block_cfgpred(node, i) == data->proj_X) {
                                        data->block   = node;
@@ -135,7 +136,8 @@ typedef struct tr_env {
  * @param rets          linked list of all rets
  * @param n_tail_calls  number of tail-recursion calls
  */
-static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
+static void do_opt_tail_rec(ir_graph *irg, tr_env *env)
+{
        ir_node *end_block = get_irg_end_block(irg);
        ir_node *block, *jmp, *call, *calls;
        ir_node **in;
@@ -147,9 +149,6 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
        int rem            = get_optimize();
        ir_entity *ent     = get_irg_entity(irg);
        ir_type *method_tp = get_entity_type(ent);
-       ir_graph *old      = current_ir_graph;
-
-       current_ir_graph = irg;
 
        assert(env->n_tail_calls > 0);
 
@@ -217,7 +216,7 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
 
        /* build the memory phi */
        i = 0;
-       in[i] = new_r_Proj(get_irg_start_block(irg), get_irg_start(irg), mode_M, pn_Start_M);
+       in[i] = new_r_Proj(get_irg_start(irg), mode_M, pn_Start_M);
        set_irg_initial_mem(irg, in[i]);
        ++i;
 
@@ -249,7 +248,7 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
                for (i = 0; i < n_params; ++i) {
                        ir_mode *mode = get_type_mode(get_method_param_type(method_tp, i));
 
-                       in[0] = new_r_Proj(args_bl, args, mode, i);
+                       in[0] = new_r_Proj(args, mode, i);
                        for (j = 0; j < env->n_tail_calls; ++j)
                                in[j + 1] = call_params[j][i];
 
@@ -274,7 +273,7 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
        set_irg_doms_inconsistent(irg);
        set_irg_outs_inconsistent(irg);
        set_irg_extblk_inconsistent(irg);
-       set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
+       set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
        set_trouts_inconsistent();
        set_irg_callee_info_state(irg, irg_callee_info_inconsistent);
 
@@ -305,9 +304,9 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
 
                        modes[i] = mode;
                        if (env->variants[i] == TR_ADD) {
-                               set_value(i, new_Const(get_mode_null(mode)));
+                               set_value(i, new_r_Const(irg, get_mode_null(mode)));
                        } else if (env->variants[i] == TR_MUL) {
-                               set_value(i, new_Const(get_mode_one(mode)));
+                               set_value(i, new_r_Const(irg, get_mode_one(mode)));
                        }
                }
                mature_immBlock(start_block);
@@ -329,7 +328,7 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
 
                        /* create a new jump, free of CSE */
                        set_optimize(0);
-                       jmp = new_Jmp();
+                       jmp = new_r_Jmp(block);
                        set_optimize(rem);
 
                        for (i = 0; i < env->n_ress; ++i) {
@@ -340,7 +339,7 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
                                }
                        }
                        /* create a new tuple for the return values */
-                       tuple = new_Tuple(env->n_ress, in);
+                       tuple = new_r_Tuple(block, env->n_ress, in);
 
                        turn_into_tuple(call, pn_Call_max);
                        set_Tuple_pred(call, pn_Call_M,                mem);
@@ -363,12 +362,14 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
                end_block = get_irg_end_block(irg);
                for (i = get_Block_n_cfgpreds(end_block) - 1; i >= 0; --i) {
                        ir_node *ret = get_Block_cfgpred(end_block, i);
+                       ir_node *block;
 
                        /* search all Returns of a block */
                        if (! is_Return(ret))
                                continue;
 
-                       set_cur_block(get_nodes_block(ret));
+                       block = get_nodes_block(ret);
+                       set_cur_block(block);
                        for (j = 0; j < env->n_ress; ++j) {
                                ir_node *pred = get_Return_res(ret, j);
                                ir_node *n;
@@ -379,13 +380,13 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
 
                                case TR_ADD:
                                        n = get_value(j, modes[j]);
-                                       n = new_Add(n, pred, modes[j]);
+                                       n = new_r_Add(block, n, pred, modes[j]);
                                        set_Return_res(ret, j, n);
                                        break;
 
                                case TR_MUL:
                                        n = get_value(j, modes[j]);
-                                       n = new_Mul(n, pred, modes[j]);
+                                       n = new_r_Mul(block, n, pred, modes[j]);
                                        set_Return_res(ret, j, n);
                                        break;
 
@@ -404,7 +405,6 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
                        exchange(p, bad);
                }
        }
-       current_ir_graph = old;
 }
 
 /**
@@ -416,7 +416,8 @@ static void do_opt_tail_rec(ir_graph *irg, tr_env *env) {
  *
  * @return non-zero if it's ok to do tail recursion
  */
-static int check_lifetime_of_locals(ir_graph *irg) {
+static int check_lifetime_of_locals(ir_graph *irg)
+{
        ir_node *irg_frame;
        int i;
        ir_type *frame_tp = get_irg_frame_type(irg);
@@ -441,7 +442,8 @@ static int check_lifetime_of_locals(ir_graph *irg) {
 /**
  * Examine irn and detect the recursion variant.
  */
-static tail_rec_variants find_variant(ir_node *irn, ir_node *call) {
+static tail_rec_variants find_variant(ir_node *irn, ir_node *call)
+{
        ir_node           *a, *b;
        tail_rec_variants va, vb, res;
 
@@ -453,7 +455,7 @@ static tail_rec_variants find_variant(ir_node *irn, ir_node *call) {
        case iro_Add:
                /* try additive */
                a = get_Add_left(irn);
-               if (get_irn_MacroBlock(a) != get_irn_MacroBlock(call)) {
+               if (get_nodes_block(a) != get_nodes_block(call)) {
                        /* we are outside, ignore */
                        va = TR_UNKNOWN;
                } else {
@@ -462,7 +464,7 @@ static tail_rec_variants find_variant(ir_node *irn, ir_node *call) {
                                return TR_BAD;
                }
                b = get_Add_right(irn);
-               if (get_irn_MacroBlock(b) != get_irn_MacroBlock(call)) {
+               if (get_nodes_block(b) != get_nodes_block(call)) {
                        /* we are outside, ignore */
                        vb = TR_UNKNOWN;
                } else {
@@ -489,7 +491,7 @@ static tail_rec_variants find_variant(ir_node *irn, ir_node *call) {
        case iro_Sub:
                /* try additive, but return value must be left */
                a = get_Sub_left(irn);
-               if (get_irn_MacroBlock(a) != get_irn_MacroBlock(call)) {
+               if (get_nodes_block(a) != get_nodes_block(call)) {
                        /* we are outside, ignore */
                        va = TR_UNKNOWN;
                } else {
@@ -498,7 +500,7 @@ static tail_rec_variants find_variant(ir_node *irn, ir_node *call) {
                                return TR_BAD;
                }
                b = get_Sub_right(irn);
-               if (get_irn_MacroBlock(b) != get_irn_MacroBlock(call)) {
+               if (get_nodes_block(b) != get_nodes_block(call)) {
                        /* we are outside, ignore */
                        vb = TR_UNKNOWN;
                } else {
@@ -515,7 +517,7 @@ static tail_rec_variants find_variant(ir_node *irn, ir_node *call) {
        case iro_Mul:
                /* try multiplicative */
                a = get_Mul_left(irn);
-               if (get_irn_MacroBlock(a) != get_irn_MacroBlock(call)) {
+               if (get_nodes_block(a) != get_nodes_block(call)) {
                        /* we are outside, ignore */
                        va = TR_UNKNOWN;
                } else {
@@ -524,7 +526,7 @@ static tail_rec_variants find_variant(ir_node *irn, ir_node *call) {
                                return TR_BAD;
                }
                b = get_Mul_right(irn);
-               if (get_irn_MacroBlock(b) != get_irn_MacroBlock(call)) {
+               if (get_nodes_block(b) != get_nodes_block(call)) {
                        /* we are outside, ignore */
                        vb = TR_UNKNOWN;
                } else {
@@ -568,7 +570,8 @@ static tail_rec_variants find_variant(ir_node *irn, ir_node *call) {
 /*
  * convert simple tail-calls into loops
  */
-int opt_tail_rec_irg(ir_graph *irg) {
+int opt_tail_rec_irg(ir_graph *irg)
+{
        tr_env            env;
        ir_node           *end_block;
        int               i, n_ress, n_tail_calls = 0;
@@ -576,12 +579,13 @@ int opt_tail_rec_irg(ir_graph *irg) {
        ir_type           *mtd_type, *call_type;
        ir_entity         *ent;
 
+       FIRM_DBG_REGISTER(dbg, "firm.opt.tailrec");
+
        assure_irg_outs(irg);
 
        if (! check_lifetime_of_locals(irg))
                return 0;
 
-
        ent      = get_irg_entity(irg);
        mtd_type = get_entity_type(ent);
     n_ress   = get_method_n_ress(mtd_type);
@@ -710,7 +714,8 @@ ir_graph_pass_t *opt_tail_rec_irg_pass(const char *name)
 /*
  * optimize tail recursion away
  */
-void opt_tail_recursion(void) {
+void opt_tail_recursion(void)
+{
        int i;
        int n_opt_applications = 0;
        ir_graph *irg;
@@ -720,8 +725,6 @@ void opt_tail_recursion(void) {
        for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
                irg = get_irp_irg(i);
 
-               current_ir_graph = irg;
-
                ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
                if (opt_tail_rec_irg(irg))
                        ++n_opt_applications;