Improve the AM folding heuristics: Do not fold AM if at least one of the AM operands...
[libfirm] / ir / be / ia32 / ia32_address_mode.c
index 7ab3dee..ac876fc 100644 (file)
@@ -45,6 +45,7 @@
 /* gas/ld don't support negative symconsts :-( */
 #undef SUPPORT_NEGATIVE_SYMCONSTS
 
+static be_lv_t  *lv;
 static bitset_t *non_address_mode_nodes;
 
 /**
@@ -291,21 +292,10 @@ static int eat_shl(ia32_address_t *addr, ir_node *node)
 #endif
 
        addr->scale = val;
-       addr->index = eat_immediates(addr, shifted_val, 0);
+       addr->index = shifted_val;
        return 1;
 }
 
-/**
- * Returns non-zero if a value of a given mode can be stored in GP registers.
- */
-static INLINE int mode_needs_gp_reg(ir_mode *mode) {
-       if(mode == mode_fpcw)
-               return 0;
-       if(get_mode_size_bits(mode) > 32)
-               return 0;
-       return mode_is_int(mode) || mode_is_reference(mode) || mode == mode_b;
-}
-
 /* Create an address mode for a given node. */
 void ia32_create_address_mode(ia32_address_t *addr, ir_node *node, int force)
 {
@@ -350,8 +340,9 @@ void ia32_create_address_mode(ia32_address_t *addr, ir_node *node, int force)
        }
 
        /* starting point Add, Sub or Shl, FrameAddr */
-       if(is_Shl(node)) { /* we don't want to eat add x, x as shl here, so only
-                             test for real Shl instructions */
+       if(is_Shl(node)) {
+               /* We don't want to eat add x, x as shl here, so only test for real Shl
+                * instructions, because we want the former as Lea x, x, not Shl x, 1 */
                if(eat_shl(addr, node))
                        return;
        } else if(is_immediate(addr, node, 0)) {
@@ -434,12 +425,12 @@ void ia32_mark_non_am(ir_node *node)
 static void mark_non_address_nodes(ir_node *node, void *env)
 {
        int i, arity;
-       ir_node *ptr;
-       ir_node *mem;
        ir_node *val;
+       ir_node *block;
        ir_node *left;
        ir_node *right;
        ir_mode *mode;
+       const ir_edge_t *edge;
        (void) env;
 
        mode = get_irn_mode(node);
@@ -448,38 +439,57 @@ static void mark_non_address_nodes(ir_node *node, void *env)
 
        switch(get_irn_opcode(node)) {
        case iro_Load:
-               ptr = get_Load_ptr(node);
-               mem = get_Load_mem(node);
-
-               bitset_set(non_address_mode_nodes, get_irn_idx(mem));
+               /* Nothing to do. especially do not mark the pointer, because we want to
+                * turn it into AM. */
                break;
 
        case iro_Store:
+               /* Do not mark the pointer, because we want to turn it into AM. */
                val = get_Store_value(node);
-               ptr = get_Store_ptr(node);
-               mem = get_Store_mem(node);
-
                bitset_set(non_address_mode_nodes, get_irn_idx(val));
-               bitset_set(non_address_mode_nodes, get_irn_idx(mem));
                break;
 
+       case iro_Shl:
        case iro_Add:
-               left  = get_Add_left(node);
-               right = get_Add_right(node);
-#if 0
-               /* if we can do source address mode then we will never fold the add
-                * into address mode */
-               if((is_immediate_simple(right)) ||
-                        (!ia32_use_source_address_mode(get_nodes_block(node), left, right)
-                    && !ia32_use_source_address_mode(get_nodes_block(node), right, left)))
-               {
-                   break;
+               /* only 1 user: AM folding is always beneficial */
+               if(get_irn_n_edges(node) <= 1)
+                       break;
+
+               /* for adds and shls with multiple users we use this heuristic:
+                * we do not fold them into address mode if their operands don't live
+                * out of the block, because in this case we will reduce register
+                * pressure. Otherwise we fold them in aggressively in the hope, that
+                * the node itself doesn't exist anymore and we were able to save the
+                * register for the result */
+               block = get_nodes_block(node);
+               left  = get_binop_left(node);
+               right = get_binop_right(node);
+
+               /* If both are live end not folding AM costs a register */
+               if(be_is_live_end(lv, block, left) && be_is_live_end(lv, block, right))
+                       return;
+
+               /* if multiple nodes in this block use left/right values, then we
+                * can't really decide wether the values will die after node.
+                * We use aggressive mode then, since it's usually just multiple address
+                * calculations. */
+               foreach_out_edge(left, edge) {
+                       ir_node *user = get_edge_src_irn(edge);
+                       if(user != node && get_nodes_block(user) == block) {
+                               foreach_out_edge(right, edge) {
+                                       ir_node *user = get_edge_src_irn(edge);
+                                       if(user != node && get_nodes_block(user) == block)
+                                               return;
+                               }
+                               break;
+                       }
                }
+
+               /* At least one of left and right are not used by anyone else, so it is
+                * beneficial for the register pressure (if both are unused otherwise,
+                * else neutral) and ALU use to not fold AM. */
                bitset_set(non_address_mode_nodes, get_irn_idx(node));
-#else
                break;
-#endif
-               /* fallthrough */
 
        default:
                arity = get_irn_arity(node);
@@ -492,8 +502,11 @@ static void mark_non_address_nodes(ir_node *node, void *env)
        }
 }
 
-void ia32_calculate_non_address_mode_nodes(ir_graph *irg)
+void ia32_calculate_non_address_mode_nodes(be_irg_t *birg)
 {
+       ir_graph *irg = be_get_birg_irg(birg);
+
+       lv                     = be_assure_liveness(birg);
        non_address_mode_nodes = bitset_malloc(get_irg_last_idx(irg));
 
        irg_walk_graph(irg, NULL, mark_non_address_nodes, NULL);