Add minimal fixpoint VRP.
[libfirm] / ir / ana / irconsconfirm.c
index 1198034..9ec685e 100644 (file)
@@ -26,6 +26,8 @@
  */
 #include "config.h"
 
+#include "irconsconfirm.h"
+
 #include "irgraph_t.h"
 #include "irnode_t.h"
 #include "ircons_t.h"
@@ -63,7 +65,8 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg;)
  *
  * This handles correctly Phi nodes.
  */
-static ir_node *get_effective_use_block(ir_node *node, int pos) {
+static ir_node *get_effective_use_block(ir_node *node, int pos)
+{
        if (is_Phi(node)) {
                /* the effective use of a Phi argument is in its predecessor block */
                node = get_nodes_block(node);
@@ -83,7 +86,8 @@ static ir_node *get_effective_use_block(ir_node *node, int pos) {
  * Branch labels are a simple case. We can replace the value
  * by a Const with the branch label.
  */
-static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env) {
+static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env)
+{
        const ir_edge_t *edge, *next;
        ir_node *c = NULL;
 
@@ -129,8 +133,9 @@ static void handle_case(ir_node *block, ir_node *irn, long nr, env_t *env) {
  * @param pnc       the true/false condition branch
  * @param env       statistical environment
  */
-static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *env) {
-       ir_node *cond, *old, *cond_block = NULL, *other_blk = NULL, *con = NULL;
+static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *env)
+{
+       ir_node *cond, *old, *other_blk = NULL, *con = NULL;
        ir_node *c_b = NULL, *c_o = NULL;
        const ir_edge_t *edge, *next;
 
@@ -163,7 +168,6 @@ static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *
                        if (other_blk == NULL) {
                                /* we have already tested, that block has only ONE Cond predecessor */
                                cond = get_Proj_pred(get_Block_cfgpred(block, 0));
-                               cond_block = get_nodes_block(cond);
                                foreach_out_edge(cond, edge) {
                                        ir_node *proj = get_edge_src_irn(edge);
                                        if (get_Proj_proj(proj) == (long)pnc)
@@ -210,6 +214,7 @@ static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *
                                if (c_b == NULL) {
                                        ir_node *c_true  = new_Const(tarval_b_true);
                                        ir_node *c_false = new_Const(tarval_b_false);
+                                       env->num_consts += 2;
                                        if (pnc == pn_Cond_true) {
                                                c_b = c_true;
                                                c_o = c_false;
@@ -228,6 +233,7 @@ static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *
                                }
                                phi = new_r_Phi(user_blk, n, in, mode_b);
                                set_irn_n(user, pos, phi);
+                               env->num_eq += 1;
                        }
                }
        }
@@ -241,7 +247,8 @@ static void handle_modeb(ir_node *block, ir_node *selector, pn_Cond pnc, env_t *
  * @param pnc     the Compare relation for taking this branch
  * @param env     statistical environment
  */
-static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
+static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env)
+{
        ir_node *left  = get_Cmp_left(cmp);
        ir_node *right = get_Cmp_right(cmp);
        ir_node *cond_block;
@@ -293,39 +300,40 @@ static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
                                DB((dbg, LEVEL_2, "Replacing input %d of node %+F with %+F\n", pos, user, right));
 
                                env->num_eq += 1;
-                       } else if (block_dominates(blk, cond_block)) {
-                               if (is_Const(right) && get_irn_pinned(user) == op_pin_state_floats) {
-                                       /*
-                                        * left == Const and we found a movable user of left in a
-                                        * dominator of the Cond block
-                                        */
-                                       const ir_edge_t *edge, *next;
-                                       for (edge = get_irn_out_edge_first(user); edge; edge = next) {
-                                               ir_node *usr_of_usr = get_edge_src_irn(edge);
-                                               int      npos = get_edge_src_pos(edge);
-                                               ir_node *blk  = get_effective_use_block(usr_of_usr, npos);
-
-                                               next = get_irn_out_edge_next(user, edge);
-                                               if (block_dominates(block, blk)) {
-                                                       /*
-                                                        * The user of the user is dominated by our true/false
-                                                        * block. So, create a copy of user WITH the constant
-                                                        * replacing it's pos'th input.
-                                                        *
-                                                        * This is always good for unop's and might be good
-                                                        * for binops.
-                                                        *
-                                                        * If user has other user in the false/true block, code
-                                                        * placement will move it down.
-                                                        * If there are users in cond block or upper, we create
-                                                        * "redundant ops", because one will have a const op,
-                                                        * the other no const ...
-                                                        */
-                                                       ir_node *new_op = exact_copy(user);
-                                                       set_nodes_block(new_op, block);
-                                                       set_irn_n(new_op, pos, right);
-                                                       set_irn_n(usr_of_usr, npos, new_op);
-                                               }
+                       } else if (block_dominates(blk, cond_block)
+                                       && is_Const(right)
+                                       && get_irn_pinned(user) == op_pin_state_floats) {
+                               /*
+                                * left == Const and we found a movable user of left in a
+                                * dominator of the Cond block
+                                */
+                               const ir_edge_t *edge, *next;
+                               for (edge = get_irn_out_edge_first(user); edge; edge = next) {
+                                       ir_node *usr_of_usr = get_edge_src_irn(edge);
+                                       int      npos = get_edge_src_pos(edge);
+                                       ir_node *blk  = get_effective_use_block(usr_of_usr, npos);
+
+                                       next = get_irn_out_edge_next(user, edge);
+                                       if (block_dominates(block, blk)) {
+                                               /*
+                                                * The user of the user is dominated by our true/false
+                                                * block. So, create a copy of user WITH the constant
+                                                * replacing it's pos'th input.
+                                                *
+                                                * This is always good for unop's and might be good
+                                                * for binops.
+                                                *
+                                                * If user has other user in the false/true block, code
+                                                * placement will move it down.
+                                                * If there are users in cond block or upper, we create
+                                                * "redundant ops", because one will have a const op,
+                                                * the other no const ...
+                                                */
+                                               ir_node *new_op = exact_copy(user);
+                                               set_nodes_block(new_op, block);
+                                               set_irn_n(new_op, pos, right);
+                                               set_irn_n(usr_of_usr, npos, new_op);
+                                               env->num_eq += 1;
                                        }
                                }
                        }
@@ -394,7 +402,8 @@ static void handle_if(ir_node *block, ir_node *cmp, pn_Cmp pnc, env_t *env) {
 /**
  * Pre-block-walker: Called for every block to insert Confirm nodes
  */
-static void insert_Confirm_in_block(ir_node *block, void *env) {
+static void insert_Confirm_in_block(ir_node *block, void *env)
+{
        ir_node *cond, *proj, *selector;
        ir_mode *mode;
 
@@ -454,7 +463,8 @@ static void insert_Confirm_in_block(ir_node *block, void *env) {
 /**
  * Checks if a node is a non-null Confirm.
  */
-static int is_non_null_Confirm(const ir_node *ptr) {
+static int is_non_null_Confirm(const ir_node *ptr)
+{
        for (;;) {
                if (! is_Confirm(ptr))
                        break;
@@ -483,7 +493,8 @@ static int is_non_null_Confirm(const ir_node *ptr) {
  * @param block  the block of the dereferencing instruction
  * @param env    environment
  */
-static void insert_non_null(ir_node *ptr, ir_node *block, env_t *env) {
+static void insert_non_null(ir_node *ptr, ir_node *block, env_t *env)
+{
        const ir_edge_t *edge, *next;
        ir_node         *c = NULL;
 
@@ -525,7 +536,8 @@ static void insert_non_null(ir_node *ptr, ir_node *block, env_t *env) {
 /**
  * Pre-walker: Called for every node to insert Confirm nodes
  */
-static void insert_Confirm(ir_node *node, void *env) {
+static void insert_Confirm(ir_node *node, void *env)
+{
        ir_node *ptr;
 
        switch (get_irn_opcode(node)) {
@@ -550,11 +562,11 @@ static void insert_Confirm(ir_node *node, void *env) {
 /*
  * Construct Confirm nodes
  */
-void construct_confirms(ir_graph *irg) {
+void construct_confirms(ir_graph *irg)
+{
        env_t env;
        int edges_active = edges_activated(irg);
 
-
        FIRM_DBG_REGISTER(dbg, "firm.ana.confirm");
 
        remove_critical_cf_edges(irg);
@@ -602,7 +614,8 @@ void construct_confirms(ir_graph *irg) {
 }  /* construct_confirms */
 
 /* Construct a pass. */
-ir_graph_pass_t *construct_confirms_pass(const char *name) {
+ir_graph_pass_t *construct_confirms_pass(const char *name)
+{
        return def_graph_pass(name ? name : "confirm", construct_confirms);
 }  /* construct_confirms_pass */
 
@@ -610,7 +623,8 @@ ir_graph_pass_t *construct_confirms_pass(const char *name) {
 /**
  * Post-walker: Remove Confirm nodes
  */
-static void rem_Confirm(ir_node *n, void *env) {
+static void rem_Confirm(ir_node *n, void *env)
+{
        (void) env;
        if (is_Confirm(n)) {
                ir_node *value = get_Confirm_value(n);
@@ -630,7 +644,8 @@ static void rem_Confirm(ir_node *n, void *env) {
 /*
  * Remove all Confirm nodes from a graph.
  */
-void remove_confirms(ir_graph *irg) {
+void remove_confirms(ir_graph *irg)
+{
        int rem = get_opt_remove_confirm();
 
        set_opt_remove_confirm(1);
@@ -639,6 +654,7 @@ void remove_confirms(ir_graph *irg) {
 }  /* remove_confirms */
 
 /* Construct a pass. */
-ir_graph_pass_t *remove_confirms_pass(const char *name) {
+ir_graph_pass_t *remove_confirms_pass(const char *name)
+{
        return def_graph_pass(name ? name : "rem_confirm", remove_confirms);
 }  /* remove_confirms_pass */