Partially undo r20786: Emit ret $0 especially when its BB is only reached by a fallt...
[libfirm] / ir / be / beabi.c
index 728448a..f2c3b01 100644 (file)
@@ -113,9 +113,12 @@ struct _be_abi_irg_t {
 
 static heights_t *ir_heights;
 
-/* Flag: if set, try to omit the frame pointer if called by the backend */
+/** Flag: if set, try to omit the frame pointer in all routines. */
 static int be_omit_fp = 1;
 
+/** Flag: if set, try to omit the frame pointer in leaf routines only. */
+static int be_omit_leaf_fp = 1;
+
 /*
      _    ____ ___    ____      _ _ _                _
     / \  | __ )_ _|  / ___|__ _| | | |__   __ _  ___| | _____
@@ -239,7 +242,7 @@ static be_abi_call_t *be_abi_call_new(const arch_register_class_t *cls_addr)
        call->cb         = NULL;
        call->cls_addr   = cls_addr;
 
-       call->flags.bits.try_omit_fp = be_omit_fp;
+       call->flags.bits.try_omit_fp = be_omit_fp | be_omit_leaf_fp;
 
        return call;
 }
@@ -625,7 +628,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
        }
 
        /** TODO: this is not correct for cases where return values are passed
-        * on the stack, but no known ABI does this currentl...
+        * on the stack, but no known ABI does this currently...
         */
        n_reg_results = n_res;
 
@@ -733,7 +736,7 @@ static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
 
        /* kill the ProjT node */
        if (res_proj != NULL) {
-               be_kill_node(res_proj);
+               kill_node(res_proj);
        }
 
        /* Make additional projs for the caller save registers
@@ -871,10 +874,7 @@ static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp
        ir_node *new_alloc, *size, *addr, *ins[2];
        unsigned stack_alignment;
 
-       if (get_Alloc_where(alloc) != stack_alloc) {
-               assert(0);
-               return alloc;
-       }
+       assert(get_Alloc_where(alloc) == stack_alloc);
 
        block = get_nodes_block(alloc);
        irg = get_irn_irg(block);
@@ -886,7 +886,7 @@ static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp
                ir_node *irn = get_edge_src_irn(edge);
 
                assert(is_Proj(irn));
-               switch(get_Proj_proj(irn)) {
+               switch (get_Proj_proj(irn)) {
                case pn_Alloc_M:
                        alloc_mem = irn;
                        break;
@@ -909,7 +909,7 @@ static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp
        dbg = get_irn_dbg_info(alloc);
 
        /* we might need to multiply the size with the element size */
-       if(type != get_unknown_type() && get_type_size_bytes(type) != 1) {
+       if (type != firm_unknown_type && get_type_size_bytes(type) != 1) {
                tarval *tv    = new_tarval_from_long(get_type_size_bytes(type),
                                                     mode_Iu);
                ir_node *cnst = new_rd_Const(dbg, irg, block, mode_Iu, tv);
@@ -924,7 +924,7 @@ static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp
           We cannot omit it. */
        env->call->flags.bits.try_omit_fp = 0;
 
-       stack_alignment = env->arch_env->stack_alignment;
+       stack_alignment = 1 << env->arch_env->stack_alignment;
        size            = adjust_alloc_size(stack_alignment, size, irg, block, dbg);
        new_alloc       = be_new_AddSP(env->arch_env->sp, irg, block, curr_sp, size);
        set_irn_dbg_info(new_alloc, dbg);
@@ -971,10 +971,7 @@ static ir_node *adjust_free(be_abi_irg_t *env, ir_node *free, ir_node *curr_sp)
        unsigned stack_alignment;
        dbg_info *dbg;
 
-       if (get_Free_where(free) != stack_alloc) {
-               assert(0);
-               return free;
-       }
+       assert(get_Free_where(free) == stack_alloc);
 
        block = get_nodes_block(free);
        irg = get_irn_irg(block);
@@ -983,7 +980,7 @@ static ir_node *adjust_free(be_abi_irg_t *env, ir_node *free, ir_node *curr_sp)
        dbg = get_irn_dbg_info(free);
 
        /* we might need to multiply the size with the element size */
-       if(type != get_unknown_type() && get_type_size_bytes(type) != 1) {
+       if (type != firm_unknown_type && get_type_size_bytes(type) != 1) {
                tarval *tv = new_tarval_from_long(get_type_size_bytes(type), mode_Iu);
                ir_node *cnst = new_rd_Const(dbg, irg, block, mode_Iu, tv);
                ir_node *mul = new_rd_Mul(dbg, irg, block, get_Free_size(free),
@@ -993,7 +990,7 @@ static ir_node *adjust_free(be_abi_irg_t *env, ir_node *free, ir_node *curr_sp)
                size = get_Free_size(free);
        }
 
-       stack_alignment = env->arch_env->stack_alignment;
+       stack_alignment = 1 << env->arch_env->stack_alignment;
        size            = adjust_alloc_size(stack_alignment, size, irg, block, dbg);
 
        /* The stack pointer will be modified in an unknown manner.
@@ -1085,15 +1082,16 @@ static int cmp_call_dependency(const void *c1, const void *c2)
 }
 
 /**
- * Walker: links all Call/alloc/Free nodes to the Block they are contained.
+ * Walker: links all Call/Alloc/Free nodes to the Block they are contained.
+ * Clears the irg_is_leaf flag if a Call is detected.
  */
-static void link_calls_in_block_walker(ir_node *irn, void *data)
+static void link_ops_in_block_walker(ir_node *irn, void *data)
 {
        ir_opcode code = get_irn_opcode(irn);
 
        if (code == iro_Call ||
-               (code == iro_Alloc && get_Alloc_where(irn) == stack_alloc) ||
-               (code == iro_Free && get_Free_where(irn) == stack_alloc)) {
+          (code == iro_Alloc && get_Alloc_where(irn) == stack_alloc) ||
+          (code == iro_Free && get_Free_where(irn) == stack_alloc)) {
                be_abi_irg_t *env = data;
                ir_node *bl       = get_nodes_block(irn);
                void *save        = get_irn_link(bl);
@@ -1108,23 +1106,23 @@ static void link_calls_in_block_walker(ir_node *irn, void *data)
 
 /**
  * Block-walker:
- * Process all Call nodes inside a basic block.
+ * Process all Call/Alloc/Free nodes inside a basic block.
  * Note that the link field of the block must contain a linked list of all
  * Call nodes inside the Block. We first order this list according to data dependency
  * and that connect the calls together.
  */
-static void process_calls_in_block(ir_node *bl, void *data)
+static void process_ops_in_block(ir_node *bl, void *data)
 {
        be_abi_irg_t *env = data;
        ir_node *curr_sp  = env->init_sp;
        ir_node *irn;
        int n;
 
-       for(irn = get_irn_link(bl), n = 0; irn; irn = get_irn_link(irn), ++n)
+       for (irn = get_irn_link(bl), n = 0; irn; irn = get_irn_link(irn), ++n)
                obstack_ptr_grow(&env->obst, irn);
 
        /* If there were call nodes in the block. */
-       if(n > 0) {
+       if (n > 0) {
                ir_node *keep;
                ir_node **nodes;
                int i;
@@ -1134,19 +1132,25 @@ static void process_calls_in_block(ir_node *bl, void *data)
                /* order the call nodes according to data dependency */
                qsort(nodes, n, sizeof(nodes[0]), cmp_call_dependency);
 
-               for(i = n - 1; i >= 0; --i) {
+               for (i = n - 1; i >= 0; --i) {
                        ir_node *irn = nodes[i];
 
                        DBG((env->dbg, LEVEL_3, "\tprocessing call %+F\n", irn));
-                       switch(get_irn_opcode(irn)) {
+                       switch (get_irn_opcode(irn)) {
                        case iro_Call:
+                               if (! be_omit_fp) {
+                                       /* The stack pointer will be modified due to a call. */
+                                       env->call->flags.bits.try_omit_fp = 0;
+                               }
                                curr_sp = adjust_call(env, irn, curr_sp);
                                break;
                        case iro_Alloc:
-                               curr_sp = adjust_alloc(env, irn, curr_sp);
+                               if (get_Alloc_where(irn) == stack_alloc)
+                                       curr_sp = adjust_alloc(env, irn, curr_sp);
                                break;
                        case iro_Free:
-                               curr_sp = adjust_free(env, irn, curr_sp);
+                               if (get_Free_where(irn) == stack_alloc)
+                                       curr_sp = adjust_free(env, irn, curr_sp);
                                break;
                        default:
                                panic("invalid call");
@@ -1158,8 +1162,8 @@ static void process_calls_in_block(ir_node *bl, void *data)
 
                /* Keep the last stack state in the block by tying it to Keep node,
                 * the proj from calls is already kept */
-               if(curr_sp != env->init_sp
-                               && !(is_Proj(curr_sp) && be_is_Call(get_Proj_pred(curr_sp)))) {
+               if (curr_sp != env->init_sp &&
+                   !(is_Proj(curr_sp) && be_is_Call(get_Proj_pred(curr_sp)))) {
                        nodes[0] = curr_sp;
                        keep     = be_new_Keep(env->arch_env->sp->reg_class,
                                               get_irn_irg(bl), bl, 1, nodes);
@@ -1178,10 +1182,10 @@ static void process_calls(be_abi_irg_t *env)
        ir_graph *irg = env->birg->irg;
 
        env->call->flags.bits.irg_is_leaf = 1;
-       irg_walk_graph(irg, firm_clear_link, link_calls_in_block_walker, env);
+       irg_walk_graph(irg, firm_clear_link, link_ops_in_block_walker, env);
 
        ir_heights = heights_new(env->birg->irg);
-       irg_block_walk_graph(irg, NULL, process_calls_in_block, env);
+       irg_block_walk_graph(irg, NULL, process_ops_in_block, env);
        heights_free(ir_heights);
 }
 
@@ -1419,7 +1423,7 @@ static ir_node *create_be_return(be_abi_irg_t *env, ir_node *irn, ir_node *bl,
        stack = be_abi_reg_map_get(env->regs, arch_env->sp);
        if (keep) {
                stack = get_irn_n(keep, 0);
-               be_kill_node(keep);
+               kill_node(keep);
                remove_End_keepalive(get_irg_end(env->birg->irg), keep);
        }
 
@@ -1742,7 +1746,7 @@ static void modify_irg(be_abi_irg_t *env)
 
        /* value_param_base anchor is not needed anymore now */
        value_param_base = get_irg_value_param_base(irg);
-       be_kill_node(value_param_base);
+       kill_node(value_param_base);
        set_irg_value_param_base(irg, new_r_Bad(irg));
 
        env->frame = obstack_alloc(&env->obst, sizeof(env->frame[0]));
@@ -1938,7 +1942,7 @@ static void modify_irg(be_abi_irg_t *env)
 
        /* the arg proj is not needed anymore now and should be only used by the anchor */
        assert(get_irn_n_edges(arg_tuple) == 1);
-       be_kill_node(arg_tuple);
+       kill_node(arg_tuple);
        set_irg_args(irg, new_rd_Bad(irg));
 
        /* All Return nodes hang on the End node, so look for them there. */
@@ -2001,6 +2005,8 @@ void fix_call_state_inputs(be_abi_irg_t *env)
                        set_irn_n(call, inp, regnode);
                }
        }
+
+       DEL_ARR_F(stateregs);
 }
 
 /**
@@ -2123,7 +2129,8 @@ be_abi_irg_t *be_abi_introduce(be_irg_t *birg)
        optimization_state_t state;
        unsigned *limited_bitset;
 
-       be_omit_fp = birg->main_env->options->omit_fp;
+       be_omit_fp      = birg->main_env->options->omit_fp;
+       be_omit_leaf_fp = birg->main_env->options->omit_leaf_fp;
 
        obstack_init(&env->obst);
 
@@ -2217,6 +2224,27 @@ void be_abi_put_ignore_regs(be_abi_irg_t *abi, const arch_register_class_t *cls,
                        bitset_set(bs, reg->index);
 }
 
+void be_abi_set_non_ignore_regs(be_abi_irg_t *abi, const arch_register_class_t *cls, unsigned *raw_bitset)
+{
+       unsigned         i;
+       arch_register_t *reg;
+
+       for (i = 0; i < cls->n_regs; ++i) {
+               if (arch_register_type_is(&cls->regs[i], ignore))
+                       continue;
+
+               rbitset_set(raw_bitset, i);
+       }
+
+       for (reg = pset_first(abi->ignore_regs); reg != NULL;
+            reg = pset_next(abi->ignore_regs)) {
+               if (reg->reg_class != cls)
+                       continue;
+
+               rbitset_clear(raw_bitset, reg->index);
+       }
+}
+
 /* Returns the stack layout from a abi environment. */
 const be_stack_layout_t *be_abi_get_stack_layout(const be_abi_irg_t *abi) {
        return abi->frame;
@@ -2350,8 +2378,8 @@ static int process_stack_bias(be_abi_irg_t *env, ir_node *bl, int real_bias)
                                        /* patch IncSP to produce an aligned stack pointer */
                                        ir_type *between_type = env->frame->between_type;
                                        int      between_size = get_type_size_bytes(between_type);
-                                       int      alignment    = env->arch_env->stack_alignment;
-                                       int      delta        = (real_bias + ofs + between_size) % env->arch_env->stack_alignment;
+                                       int      alignment    = 1 << env->arch_env->stack_alignment;
+                                       int      delta        = (real_bias + ofs + between_size) & (alignment - 1);
                                        assert(ofs >= 0);
                                        if (delta > 0) {
                                                be_set_IncSP_offset(irn, ofs + alignment - delta);