fix missing PTR_TO_INT macros in release mode
[libfirm] / ir / ir / ircons.c
index 438e82e..290e1c4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
@@ -87,8 +87,8 @@ ir_node *new_rd_ASM(dbg_info *db, ir_node *block, int arity, ir_node *in[],
        memcpy(res->attr.assem.output_constraints, outputs, sizeof(outputs[0]) * n_outs);
        memcpy(res->attr.assem.clobbers, clobber, sizeof(clobber[0]) * n_clobber);
 
-       res = optimize_node(res);
        irn_verify_irg(res, irg);
+       res = optimize_node(res);
        return res;
 }
 
@@ -106,8 +106,8 @@ ir_node *new_rd_SymConst(dbg_info *db, ir_graph *irg, ir_mode *mode,
        res->attr.symc.kind = symkind;
        res->attr.symc.sym  = value;
 
-       res = optimize_node(res);
        irn_verify_irg(res, irg);
+       res = optimize_node(res);
        return res;
 }
 
@@ -185,6 +185,42 @@ static inline ir_node *new_rd_Phi0(dbg_info *dbgi, ir_node *block,
 
 static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode);
 
+static void try_remove_unnecessary_phi(ir_node *phi)
+{
+       ir_node *phi_value = NULL;
+       int      arity     = get_irn_arity(phi);
+       int      i;
+
+       /* see if all inputs are either pointing to a single value or
+        * are self references */
+       for (i = 0; i < arity; ++i) {
+               ir_node *in = get_irn_n(phi, i);
+               if (in == phi)
+                       continue;
+               if (in == phi_value)
+                       continue;
+               /** found a different value from the one we already found, can't remove
+                * the phi (yet) */
+               if (phi_value != NULL)
+                       return;
+               phi_value = in;
+       }
+       if (phi_value == NULL)
+               return;
+
+       /* if we're here then all phi inputs have been either phi_value
+        * or self-references, we can replace the phi by phi_value.
+        * We do this with an Id-node */
+       exchange(phi, phi_value);
+
+       /* recursively check phi_value, because it could be that we were the last
+        * phi-node in a loop-body. Then our arguments is an unnecessary phi in
+        * the loop header which can be eliminated now */
+       if (is_Phi(phi_value)) {
+               try_remove_unnecessary_phi(phi_value);
+       }
+}
+
 /**
  * Computes the predecessors for the real phi node, and then
  * allocates and returns this node.  The routine called to allocate the
@@ -198,8 +234,6 @@ static ir_node *set_phi_arguments(ir_node *phi, int pos)
        int       arity        = get_irn_arity(block);
        ir_node **in           = ALLOCAN(ir_node*, arity);
        ir_mode  *mode         = get_irn_mode(phi);
-       ir_node  *phi_value    = NULL;
-       bool      no_phi_value = false;
        int       i;
 
        /* This loop goes to all predecessor blocks of the block the Phi node
@@ -209,37 +243,27 @@ static ir_node *set_phi_arguments(ir_node *phi, int pos)
                ir_node *cfgpred = get_Block_cfgpred_block(block, i);
                ir_node *value;
                if (is_Bad(cfgpred)) {
-                       value = new_r_Bad(irg);
+                       value = new_r_Bad(irg, mode);
                } else {
+                       inc_irg_visited(irg);
+
                        value = get_r_value_internal(cfgpred, pos, mode);
                }
-               if (!no_phi_value && value != phi) {
-                       if (phi_value == NULL) {
-                               phi_value = value;
-                       } else if (value != phi_value) {
-                               no_phi_value = true;
-                               phi_value = NULL;
-                       }
-               }
                in[i] = value;
        }
 
-       if (phi_value != NULL && !no_phi_value) {
-               exchange(phi, phi_value);
-               return phi_value;
-       }
-
        phi->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);
        set_irn_in(phi, arity, in);
        set_irn_op(phi, op_Phi);
 
-       phi = optimize_in_place_2(phi);
        irn_verify_irg(phi, irg);
 
        /* Memory Phis in endless loops must be kept alive.
           As we can't distinguish these easily we keep all of them alive. */
        if (is_Phi(phi) && mode == mode_M)
                add_End_keepalive(get_irg_end(irg), phi);
+
+       try_remove_unnecessary_phi(phi);
        return phi;
 }
 
@@ -255,16 +279,22 @@ static ir_node *set_phi_arguments(ir_node *phi, int pos)
  */
 static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode)
 {
-       ir_node *res = block->attr.block.graph_arr[pos];
+       ir_node  *res = block->attr.block.graph_arr[pos];
+       ir_graph *irg = get_irn_irg(block);
        if (res != NULL)
                return res;
 
-       /* in a matured block we can immediated determine the phi arguments */
-       if (block->attr.block.is_matured) {
+       /* We ran into a cycle. This may happen in unreachable loops. */
+       if (irn_visited_else_mark(block)) {
+               /* Since the loop is unreachable, return a Bad. */
+               return new_r_Bad(irg, mode);
+       }
+
+       /* in a matured block we can immediately determine the phi arguments */
+       if (get_Block_matured(block)) {
                int arity = get_irn_arity(block);
                /* no predecessors: use unknown value */
                if (arity == 0 && block == get_irg_start_block(get_irn_irg(block))) {
-                       ir_graph *irg = get_irn_irg(block);
                        if (default_initialize_local_variable != NULL) {
                                ir_node *rem = get_r_cur_block(irg);
                                set_r_cur_block(irg, block);
@@ -275,11 +305,12 @@ static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode)
                        }
                /* one predecessor just use its value */
                } else if (arity == 1) {
-                       ir_node *cfgpred = get_Block_cfgpred_block(block, 0);
+                       ir_node *cfgpred = get_Block_cfgpred(block, 0);
                        if (is_Bad(cfgpred)) {
-                               res = cfgpred;
+                               res = new_r_Bad(irg, mode);
                        } else {
-                               res = get_r_value_internal(cfgpred, pos, mode);
+                               ir_node *cfgpred_block = get_nodes_block(cfgpred);
+                               res = get_r_value_internal(cfgpred_block, pos, mode);
                        }
                /* multiple predecessors construct Phi */
                } else {
@@ -308,7 +339,7 @@ static ir_node *get_r_value_internal(ir_node *block, int pos, ir_mode *mode)
  */
 void mature_immBlock(ir_node *block)
 {
-       int       n_preds;
+       size_t   n_preds;
        ir_node  *next;
        ir_node  *phi;
        ir_graph *irg;
@@ -335,7 +366,7 @@ void mature_immBlock(ir_node *block)
                }
        }
 
-       block->attr.block.is_matured = 1;
+       set_Block_matured(block, 1);
 
        /* Now, as the block is a finished Firm node, we can optimize it.
           Since other nodes have been allocated since the block was created
@@ -345,8 +376,8 @@ void mature_immBlock(ir_node *block)
           nodes refer to the unoptimized node.
           We can call optimize_in_place_2(), as global cse has no effect on blocks.
         */
-       block = optimize_in_place_2(block);
        irn_verify_irg(block, irg);
+       block = optimize_in_place_2(block);
 }
 
 ir_node *new_d_Const_long(dbg_info *db, ir_mode *mode, long value)
@@ -401,8 +432,8 @@ ir_node *new_rd_strictConv(dbg_info *dbgi, ir_node *block, ir_node * irn_op, ir_
 
        res = new_ir_node(dbgi, irg, block, op_Conv, mode, 1, in);
        res->attr.conv.strict = 1;
-       res = optimize_node(res);
        irn_verify_irg(res, irg);
+       res = optimize_node(res);
        return res;
 }
 
@@ -435,11 +466,11 @@ ir_node *new_rd_DivRL(dbg_info *dbgi, ir_node *block, ir_node * irn_mem, ir_node
        in[2] = irn_right;
 
        res = new_ir_node(dbgi, irg, block, op_Div, mode_T, 3, in);
-       res->attr.divmod.resmode = resmode;
-       res->attr.divmod.no_remainder = 1;
-       res->attr.divmod.exc.pin_state = pin_state;
-       res = optimize_node(res);
+       res->attr.div.resmode = resmode;
+       res->attr.div.no_remainder = 1;
+       res->attr.div.exc.pin_state = pin_state;
        irn_verify_irg(res, irg);
+       res = optimize_node(res);
        return res;
 }
 
@@ -469,14 +500,12 @@ ir_node *new_rd_immBlock(dbg_info *dbgi, ir_graph *irg)
        /* creates a new dynamic in-array as length of in is -1 */
        res = new_ir_node(dbgi, irg, NULL, op_Block, mode_BB, -1, NULL);
 
-       res->attr.block.is_matured  = 0;
-       res->attr.block.is_dead     = 0;
+       set_Block_matured(res, 0);
        res->attr.block.irg.irg     = irg;
        res->attr.block.backedge    = NULL;
        res->attr.block.in_cg       = NULL;
        res->attr.block.cg_backedge = NULL;
        res->attr.block.extblk      = NULL;
-       res->attr.block.region      = NULL;
        res->attr.block.entity      = NULL;
 
        set_Block_block_visited(res, 0);
@@ -511,7 +540,7 @@ void add_immBlock_pred(ir_node *block, ir_node *jmp)
        int n = ARR_LEN(block->in) - 1;
 
        assert(is_Block(block) && "Error: Must be a Block");
-       assert(!block->attr.block.is_matured && "Error: Block already matured!\n");
+       assert(!get_Block_matured(block) && "Error: Block already matured!\n");
        assert(is_ir_node(jmp));
 
        ARR_APP1(ir_node *, block->in, jmp);
@@ -521,11 +550,13 @@ void add_immBlock_pred(ir_node *block, ir_node *jmp)
 
 void set_cur_block(ir_node *target)
 {
-       current_ir_graph->current_block = target;
+       set_r_cur_block(current_ir_graph, target);
 }
 
 void set_r_cur_block(ir_graph *irg, ir_node *target)
 {
+       assert(target == NULL || get_irn_mode(target) == mode_BB);
+       assert(target == NULL || get_irn_irg(target)  == irg);
        irg->current_block = target;
 }
 
@@ -543,6 +574,7 @@ ir_node *get_r_value(ir_graph *irg, int pos, ir_mode *mode)
 {
        assert(get_irg_phase_state(irg) == phase_building);
        assert(pos >= 0);
+       inc_irg_visited(irg);
 
        return get_r_value_internal(irg->current_block, pos + 1, mode);
 }
@@ -562,9 +594,8 @@ static ir_mode *guess_recursively(ir_node *block, int pos)
        int      n_preds;
        int      i;
 
-       if (irn_visited(block))
+       if (irn_visited_else_mark(block))
                return NULL;
-       mark_irn_visited(block);
 
        /* already have a defintion -> we can simply look at its mode */
        value = block->attr.block.graph_arr[pos];
@@ -623,12 +654,13 @@ void set_value(int pos, ir_node *value)
 
 int r_find_value(ir_graph *irg, ir_node *value)
 {
-       int i;
+       size_t i;
        ir_node *bl = irg->current_block;
 
-       for (i = ARR_LEN(bl->attr.block.graph_arr) - 1; i >= 1; --i)
-               if (bl->attr.block.graph_arr[i] == value)
+       for (i = ARR_LEN(bl->attr.block.graph_arr); i > 1;) {
+               if (bl->attr.block.graph_arr[--i] == value)
                        return i - 1;
+       }
        return -1;
 }
 
@@ -640,6 +672,7 @@ int find_value(ir_node *value)
 ir_node *get_r_store(ir_graph *irg)
 {
        assert(get_irg_phase_state(irg) == phase_building);
+       inc_irg_visited(irg);
        return get_r_value_internal(irg->current_block, 0, mode_M);
 }
 
@@ -691,14 +724,10 @@ void set_store(ir_node *store)
        set_r_store(current_ir_graph, store);
 }
 
-void r_keep_alive(ir_graph *irg, ir_node *ka)
-{
-       add_End_keepalive(get_irg_end(irg), ka);
-}
-
 void keep_alive(ir_node *ka)
 {
-       r_keep_alive(current_ir_graph, ka);
+       ir_graph *irg = get_irn_irg(ka);
+       add_End_keepalive(get_irg_end(irg), ka);
 }
 
 void ir_set_uninitialized_local_variable_func(
@@ -714,8 +743,8 @@ void irg_finalize_cons(ir_graph *irg)
 
 void irp_finalize_cons(void)
 {
-       int i;
-       for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
+       size_t i, n;
+       for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
                irg_finalize_cons(get_irp_irg(i));
        }
        irp->phase_state = phase_high;