/*
- * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
- *
* This file is part of libFirm.
- *
- * This file may be distributed and/or modified under the terms of the
- * GNU General Public License version 2 as published by the Free Software
- * Foundation and appearing in the file LICENSE.GPL included in the
- * packaging of this file.
- *
- * Licensees holding valid libFirm Professional Edition licenses may use
- * this file in accordance with the libFirm Commercial License.
- * Agreement provided with the Software.
- *
- * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
- * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE.
+ * Copyright (C) 2012 University of Karlsruhe.
*/
/**
* @file
* @brief Dead node elimination and Procedure Inlining.
* @author Michael Beck, Goetz Lindenmaier
- * @version $Id$
*/
#include "config.h"
#include "irtools.h"
#include "iropt_dbg.h"
#include "irpass_t.h"
-#include "irphase_t.h"
+#include "irnodemap.h"
DEBUG_ONLY(static firm_dbg_module_t *dbg;)
/* move constants into start block */
new_node = get_new_node(node);
- if (is_irn_constlike(new_node)) {
+ if (is_irn_start_block_placed(new_node)) {
ir_graph *new_irg = (ir_graph *) env;
ir_node *start_block = get_irg_start_block(new_irg);
set_nodes_block(new_node, start_block);
{
bool *allow_inline = (bool*)env;
- if (is_Sel(node)) {
+ if (is_Block(node) && get_Block_entity(node)) {
+ /**
+ * Currently we can't handle blocks whose address was taken correctly
+ * when inlining
+ */
+ *allow_inline = false;
+ } else if (is_Sel(node)) {
ir_graph *irg = current_ir_graph;
if (get_Sel_ptr(node) == get_irg_frame(irg)) {
/* access to frame */
/* access to value_type */
*allow_inline = false;
}
+ if (is_parameter_entity(ent)) {
+ *allow_inline = false;
+ }
}
} else if (is_Alloc(node) && get_Alloc_where(node) == stack_alloc) {
/* From GCC:
size_t n_params = get_method_n_params(called_type);
size_t n_arguments = get_method_n_params(call_type);
size_t n_res = get_method_n_ress(called_type);
- irg_inline_property prop = get_irg_inline_property(called_graph);
+ mtp_additional_properties props = get_entity_additional_properties(called);
size_t i;
bool res;
- if (prop == irg_inline_forbidden)
+ if (props & mtp_property_noinline)
return false;
if (n_arguments != n_params) {
for (i = 0; i < n_params; ++i) {
ir_type *p_type = get_method_param_type(call_type, i);
- if (is_compound_type(p_type))
+ if (is_compound_type(p_type) || is_Array_type(p_type))
return false;
}
for (i = 0; i < n_res; ++i) {
ir_type *r_type = get_method_res_type(call_type, i);
- if (is_compound_type(r_type))
+ if (is_compound_type(r_type) || is_Array_type(r_type))
return false;
}
ir_entity *old_ent = get_class_member(from_frame, i);
ir_entity *new_ent = copy_entity_own(old_ent, to_frame);
set_entity_link(old_ent, new_ent);
+ assert (!is_parameter_entity(old_ent));
}
}
/* Inlines a method at the given call site. */
-int inline_method(ir_node *call, ir_graph *called_graph)
+int inline_method(ir_node *const call, ir_graph *called_graph)
{
- ir_node *pre_call;
- ir_node *post_call, *post_bl;
- ir_node *in[pn_Start_max];
- ir_node *end, *end_bl, *block;
- ir_node **res_pred;
- ir_node **cf_pred;
- ir_node **args_in;
- ir_node *ret, *phi;
- int arity, n_ret, n_exc, n_res, i, j, rem_opt;
- int irn_arity, n_params;
- int n_mem_phi;
- enum exc_mode exc_handling;
- ir_type *mtp;
- ir_type *ctp;
- ir_entity *ent;
- ir_graph *rem;
- ir_graph *irg = get_irn_irg(call);
-
/* we cannot inline some types of calls */
if (! can_inline(call, called_graph))
return 0;
/* We cannot inline a recursive call. The graph must be copied before
* the call the inline_method() using create_irg_copy(). */
+ ir_graph *irg = get_irn_irg(call);
if (called_graph == irg)
return 0;
- ent = get_irg_entity(called_graph);
- mtp = get_entity_type(ent);
- ctp = get_Call_type(call);
- n_params = get_method_n_params(mtp);
- n_res = get_method_n_ress(mtp);
+ ir_entity *ent = get_irg_entity(called_graph);
+ ir_type *mtp = get_entity_type(ent);
+ ir_type *ctp = get_Call_type(call);
+ int n_params = get_method_n_params(mtp);
- rem = current_ir_graph;
+ ir_graph *rem = current_ir_graph;
current_ir_graph = irg;
DB((dbg, LEVEL_1, "Inlining %+F(%+F) into %+F\n", call, called_graph, irg));
/* optimizations can cause problems when allocating new nodes */
- rem_opt = get_opt_optimize();
+ int rem_opt = get_opt_optimize();
set_optimize(0);
/* Handle graph state */
- assert(get_irg_phase_state(irg) != phase_building);
assert(get_irg_pinned(irg) == op_pin_state_pinned);
assert(get_irg_pinned(called_graph) == op_pin_state_pinned);
- set_irg_outs_inconsistent(irg);
- set_irg_extblk_inconsistent(irg);
- set_irg_doms_inconsistent(irg);
- set_irg_loopinfo_inconsistent(irg);
+ clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
+ | IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE);
set_irg_callee_info_state(irg, irg_callee_info_inconsistent);
- set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
+ clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE);
edges_deactivate(irg);
/* here we know we WILL inline, so inform the statistics */
exc_handling:
0 There is a handler.
2 Exception handling not represented in Firm. -- */
- {
- ir_node *Xproj = NULL;
- ir_node *proj;
- for (proj = (ir_node*)get_irn_link(call); proj != NULL;
- proj = (ir_node*)get_irn_link(proj)) {
- long proj_nr = get_Proj_proj(proj);
- if (proj_nr == pn_Call_X_except) Xproj = proj;
- }
- exc_handling = Xproj != NULL ? exc_handler : exc_no_handler;
+ ir_node *Xproj = NULL;
+ for (ir_node *proj = (ir_node*)get_irn_link(call); proj != NULL;
+ proj = (ir_node*)get_irn_link(proj)) {
+ long proj_nr = get_Proj_proj(proj);
+ if (proj_nr == pn_Call_X_except) Xproj = proj;
}
+ enum exc_mode exc_handling = Xproj != NULL ? exc_handler : exc_no_handler;
/* create the argument tuple */
- args_in = ALLOCAN(ir_node*, n_params);
+ ir_node **args_in = ALLOCAN(ir_node*, n_params);
- block = get_nodes_block(call);
- for (i = n_params - 1; i >= 0; --i) {
+ ir_node *block = get_nodes_block(call);
+ for (int i = n_params - 1; i >= 0; --i) {
ir_node *arg = get_Call_param(call, i);
ir_type *param_tp = get_method_param_type(mtp, i);
ir_mode *mode = get_type_mode(param_tp);
/* the procedure and later replaces the Start node of the called graph.
* Post_call is the old Call node and collects the results of the called
* graph. Both will end up being a tuple. */
- post_bl = get_nodes_block(call);
+ ir_node *post_bl = get_nodes_block(call);
/* XxMxPxPxPxT of Start + parameter of Call */
+ ir_node *in[pn_Start_max+1];
in[pn_Start_M] = get_Call_mem(call);
in[pn_Start_X_initial_exec] = new_r_Jmp(post_bl);
in[pn_Start_P_frame_base] = get_irg_frame(irg);
in[pn_Start_T_args] = new_r_Tuple(post_bl, n_params, args_in);
- pre_call = new_r_Tuple(post_bl, pn_Start_max, in);
- post_call = call;
+ ir_node *pre_call = new_r_Tuple(post_bl, pn_Start_max+1, in);
/* --
The new block gets the ins of the old block, pre_call and all its
* node, similar for singleton nodes like NoMem and Bad.
* Note: this will prohibit predecessors to be copied - only do it for
* nodes without predecessors */
- {
- ir_node *start_block;
- ir_node *start;
- ir_node *nomem;
-
- start_block = get_irg_start_block(called_graph);
- set_new_node(start_block, get_nodes_block(pre_call));
- mark_irn_visited(start_block);
-
- start = get_irg_start(called_graph);
- set_new_node(start, pre_call);
- mark_irn_visited(start);
-
- nomem = get_irg_no_mem(called_graph);
- set_new_node(nomem, get_irg_no_mem(irg));
- mark_irn_visited(nomem);
- }
+ ir_node *start_block = get_irg_start_block(called_graph);
+ set_new_node(start_block, get_nodes_block(pre_call));
+ mark_irn_visited(start_block);
+
+ ir_node *start = get_irg_start(called_graph);
+ set_new_node(start, pre_call);
+ mark_irn_visited(start);
+
+ ir_node *nomem = get_irg_no_mem(called_graph);
+ set_new_node(nomem, get_irg_no_mem(irg));
+ mark_irn_visited(nomem);
/* entitiy link is used to link entities on old stackframe to the
* new stackframe */
- irp_reserve_resources(irp, IR_RESOURCE_ENTITY_LINK);
+ irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
/* copy entities and nodes */
assert(!irn_visited(get_irg_end(called_graph)));
irg_walk_core(get_irg_end(called_graph), copy_node_inline, set_preds_inline,
irg);
- irp_free_resources(irp, IR_RESOURCE_ENTITY_LINK);
+ irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
/* -- Merge the end of the inlined procedure with the call site -- */
/* We will turn the old Call node into a Tuple with the following
*/
/* Precompute some values */
- end_bl = get_new_node(get_irg_end_block(called_graph));
- end = get_new_node(get_irg_end(called_graph));
- arity = get_Block_n_cfgpreds(end_bl); /* arity = n_exc + n_ret */
- n_res = get_method_n_ress(get_Call_type(call));
+ ir_node *end_bl = get_new_node(get_irg_end_block(called_graph));
+ ir_node *end = get_new_node(get_irg_end(called_graph));
+ int arity = get_Block_n_cfgpreds(end_bl); /* arity = n_exc + n_ret */
+ int n_res = get_method_n_ress(get_Call_type(call));
- res_pred = XMALLOCN(ir_node*, n_res);
- cf_pred = XMALLOCN(ir_node*, arity);
+ ir_node **res_pred = XMALLOCN(ir_node*, n_res);
+ ir_node **cf_pred = XMALLOCN(ir_node*, arity);
/* archive keepalives */
- irn_arity = get_irn_arity(end);
- for (i = 0; i < irn_arity; i++) {
+ int irn_arity = get_irn_arity(end);
+ for (int i = 0; i < irn_arity; i++) {
ir_node *ka = get_End_keepalive(end, i);
if (! is_Bad(ka))
add_End_keepalive(get_irg_end(irg), ka);
}
/* replace Return nodes by Jump nodes */
- n_ret = 0;
- for (i = 0; i < arity; i++) {
- ir_node *ret;
- ret = get_Block_cfgpred(end_bl, i);
+ int n_ret = 0;
+ for (int i = 0; i < arity; i++) {
+ ir_node *ret = get_Block_cfgpred(end_bl, i);
if (is_Return(ret)) {
ir_node *block = get_nodes_block(ret);
cf_pred[n_ret] = new_r_Jmp(block);
/* build a Tuple for all results of the method.
* add Phi node if there was more than one Return. */
- turn_into_tuple(post_call, pn_Call_max);
/* First the Memory-Phi */
- n_mem_phi = 0;
- for (i = 0; i < arity; i++) {
- ret = get_Block_cfgpred(end_bl, i);
+ int n_mem_phi = 0;
+ for (int i = 0; i < arity; i++) {
+ ir_node *ret = get_Block_cfgpred(end_bl, i);
if (is_Return(ret)) {
cf_pred[n_mem_phi++] = get_Return_mem(ret);
}
cf_pred[n_mem_phi++] = new_r_Proj(ret, mode_M, 1);
}
}
- phi = new_r_Phi(post_bl, n_mem_phi, cf_pred, mode_M);
- set_Tuple_pred(call, pn_Call_M, phi);
+ ir_node *const call_mem = new_r_Phi(post_bl, n_mem_phi, cf_pred, mode_M);
/* Conserve Phi-list for further inlinings -- but might be optimized */
- if (get_nodes_block(phi) == post_bl) {
- set_irn_link(phi, get_irn_link(post_bl));
- set_irn_link(post_bl, phi);
+ if (get_nodes_block(call_mem) == post_bl) {
+ set_irn_link(call_mem, get_irn_link(post_bl));
+ set_irn_link(post_bl, call_mem);
}
/* Now the real results */
+ ir_node *call_res;
if (n_res > 0) {
- ir_node *result_tuple;
- for (j = 0; j < n_res; j++) {
+ for (int j = 0; j < n_res; j++) {
ir_type *res_type = get_method_res_type(ctp, j);
ir_mode *res_mode = get_type_mode(res_type);
- n_ret = 0;
- for (i = 0; i < arity; i++) {
- ret = get_Block_cfgpred(end_bl, i);
+ int n_ret = 0;
+ for (int i = 0; i < arity; i++) {
+ ir_node *ret = get_Block_cfgpred(end_bl, i);
if (is_Return(ret)) {
ir_node *res = get_Return_res(ret, j);
if (get_irn_mode(res) != res_mode) {
n_ret++;
}
}
- if (n_ret > 0) {
- ir_mode *mode = get_irn_mode(cf_pred[0]);
- phi = new_r_Phi(post_bl, n_ret, cf_pred, mode);
- } else {
- ir_mode *mode = get_irn_mode(cf_pred[0]);
- phi = new_r_Bad(irg, mode);
- }
+ ir_node *const phi = n_ret > 0
+ ? new_r_Phi(post_bl, n_ret, cf_pred, res_mode)
+ : new_r_Bad(irg, res_mode);
res_pred[j] = phi;
/* Conserve Phi-list for further inlinings -- but might be optimized */
if (get_nodes_block(phi) == post_bl) {
set_Block_phis(post_bl, phi);
}
}
- result_tuple = new_r_Tuple(post_bl, n_res, res_pred);
- set_Tuple_pred(call, pn_Call_T_result, result_tuple);
+ call_res = new_r_Tuple(post_bl, n_res, res_pred);
} else {
- set_Tuple_pred(call, pn_Call_T_result, new_r_Bad(irg, mode_T));
+ call_res = new_r_Bad(irg, mode_T);
}
/* handle the regular call */
- set_Tuple_pred(call, pn_Call_X_regular, new_r_Jmp(post_bl));
+ ir_node *const call_x_reg = new_r_Jmp(post_bl);
/* Finally the exception control flow.
We have two possible situations:
Second: There is no exception edge. Just add all inlined exception
branches to the End node.
*/
+ ir_node *call_x_exc;
if (exc_handling == exc_handler) {
- n_exc = 0;
- for (i = 0; i < arity; i++) {
- ir_node *ret, *irn;
- ret = get_Block_cfgpred(end_bl, i);
- irn = skip_Proj(ret);
+ int n_exc = 0;
+ for (int i = 0; i < arity; i++) {
+ ir_node *ret = get_Block_cfgpred(end_bl, i);
+ ir_node *irn = skip_Proj(ret);
if (is_fragile_op(irn) || is_Raise(irn)) {
cf_pred[n_exc] = ret;
++n_exc;
if (n_exc > 0) {
if (n_exc == 1) {
/* simple fix */
- set_Tuple_pred(call, pn_Call_X_except, cf_pred[0]);
+ call_x_exc = cf_pred[0];
} else {
ir_node *block = new_r_Block(irg, n_exc, cf_pred);
- set_Tuple_pred(call, pn_Call_X_except, new_r_Jmp(block));
+ call_x_exc = new_r_Jmp(block);
}
} else {
- set_Tuple_pred(call, pn_Call_X_except, new_r_Bad(irg, mode_X));
+ call_x_exc = new_r_Bad(irg, mode_X);
}
} else {
- ir_node *main_end_bl;
- int main_end_bl_arity;
- ir_node **end_preds;
-
/* assert(exc_handling == 1 || no exceptions. ) */
- n_exc = 0;
- for (i = 0; i < arity; i++) {
+ int n_exc = 0;
+ for (int i = 0; i < arity; i++) {
ir_node *ret = get_Block_cfgpred(end_bl, i);
ir_node *irn = skip_Proj(ret);
n_exc++;
}
}
- main_end_bl = get_irg_end_block(irg);
- main_end_bl_arity = get_irn_arity(main_end_bl);
- end_preds = XMALLOCN(ir_node*, n_exc + main_end_bl_arity);
+ ir_node *main_end_bl = get_irg_end_block(irg);
+ int main_end_bl_arity = get_irn_arity(main_end_bl);
+ ir_node **end_preds = XMALLOCN(ir_node*, n_exc+main_end_bl_arity);
- for (i = 0; i < main_end_bl_arity; ++i)
+ for (int i = 0; i < main_end_bl_arity; ++i)
end_preds[i] = get_irn_n(main_end_bl, i);
- for (i = 0; i < n_exc; ++i)
+ for (int i = 0; i < n_exc; ++i)
end_preds[main_end_bl_arity + i] = cf_pred[i];
set_irn_in(main_end_bl, n_exc + main_end_bl_arity, end_preds);
- set_Tuple_pred(call, pn_Call_X_except, new_r_Bad(irg, mode_X));
+ call_x_exc = new_r_Bad(irg, mode_X);
free(end_preds);
}
free(res_pred);
free(cf_pred);
+ ir_node *const call_in[] = {
+ [pn_Call_M] = call_mem,
+ [pn_Call_T_result] = call_res,
+ [pn_Call_X_regular] = call_x_reg,
+ [pn_Call_X_except] = call_x_exc,
+ };
+ turn_into_tuple(call, ARRAY_SIZE(call_in), call_in);
+
/* -- Turn CSE back on. -- */
set_optimize(rem_opt);
current_ir_graph = rem;
unsigned all_const:1; /**< Set if this call has only constant parameters. */
} call_entry;
-/**
- * environment for inlining small irgs
- */
-typedef struct inline_env_t {
- struct obstack obst; /**< An obstack where call_entries are allocated on. */
- list_head calls; /**< The call entry list. */
-} inline_env_t;
-
/**
* Returns the irg called from a Call node. If the irg is not
* known, NULL is returned.
ir_node *addr;
addr = get_Call_ptr(call);
- if (is_Global(addr)) {
- ir_entity *ent = get_Global_entity(addr);
+ if (is_SymConst_addr_ent(addr)) {
+ ir_entity *ent = get_SymConst_entity(addr);
/* we don't know which function gets finally bound to a weak symbol */
if (get_entity_linkage(ent) & IR_LINKAGE_WEAK)
return NULL;
return NULL;
}
-/**
- * Walker: Collect all calls to known graphs inside a graph.
- */
-static void collect_calls(ir_node *call, void *env)
-{
- (void) env;
- if (is_Call(call)) {
- ir_graph *called_irg = get_call_called_irg(call);
-
- if (called_irg != NULL) {
- /* The Call node calls a locally defined method. Remember to inline. */
- inline_env_t *ienv = (inline_env_t*)env;
- call_entry *entry = OALLOC(&ienv->obst, call_entry);
- entry->call = call;
- entry->callee = called_irg;
- entry->loop_depth = 0;
- entry->benefice = 0;
- entry->local_adr = 0;
- entry->all_const = 0;
-
- list_add_tail(&entry->list, &ienv->calls);
- }
- }
-}
-
-/**
- * Inlines all small methods at call sites where the called address comes
- * from a Const node that references the entity representing the called
- * method.
- * The size argument is a rough measure for the code size of the method:
- * Methods where the obstack containing the firm graph is smaller than
- * size are inlined.
- */
-void inline_small_irgs(ir_graph *irg, int size)
-{
- ir_graph *rem = current_ir_graph;
- inline_env_t env;
- call_entry *entry;
-
- current_ir_graph = irg;
- /* Handle graph state */
- assert(get_irg_phase_state(irg) != phase_building);
- free_callee_info(irg);
-
- /* Find Call nodes to inline.
- (We can not inline during a walk of the graph, as inlining the same
- method several times changes the visited flag of the walked graph:
- after the first inlining visited of the callee equals visited of
- the caller. With the next inlining both are increased.) */
- obstack_init(&env.obst);
- INIT_LIST_HEAD(&env.calls);
- irg_walk_graph(irg, NULL, collect_calls, &env);
-
- if (! list_empty(&env.calls)) {
- /* There are calls to inline */
- ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
- collect_phiprojs(irg);
-
- list_for_each_entry(call_entry, entry, &env.calls, list) {
- ir_graph *callee = entry->callee;
- irg_inline_property prop = get_irg_inline_property(callee);
-
- if (prop == irg_inline_forbidden) {
- continue;
- }
-
- if (prop >= irg_inline_forced ||
- _obstack_memory_used(callee->obst) - (int)obstack_room(callee->obst) < size) {
- inline_method(entry->call, callee);
- }
- }
- ir_free_resources(irg, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
- }
- obstack_free(&env.obst, NULL);
- current_ir_graph = rem;
-}
-
-typedef struct inline_small_irgs_pass_t {
- ir_graph_pass_t pass;
- int size;
-} inline_small_irgs_pass_t;
-
-/**
- * Wrapper to run inline_small_irgs() as a pass.
- */
-static int inline_small_irgs_wrapper(ir_graph *irg, void *context)
-{
- inline_small_irgs_pass_t *pass = (inline_small_irgs_pass_t*)context;
-
- inline_small_irgs(irg, pass->size);
- return 0;
-}
-
-/* create a pass for inline_small_irgs() */
-ir_graph_pass_t *inline_small_irgs_pass(const char *name, int size)
-{
- inline_small_irgs_pass_t *pass = XMALLOCZ(inline_small_irgs_pass_t);
-
- pass->size = size;
- return def_graph_pass_constructor(
- &pass->pass, name ? name : "inline_small_irgs", inline_small_irgs_wrapper);
-}
-
/**
* Environment for inlining irgs.
*/
if (env->ignore_runtime) {
ir_node *symc = get_Call_ptr(call);
- if (is_Global(symc)) {
- ir_entity *ent = get_Global_entity(symc);
+ if (is_SymConst_addr_ent(symc)) {
+ ir_entity *ent = get_SymConst_entity(symc);
if (get_entity_additional_properties(ent) & mtp_property_runtime)
return;
}
}
-/**
- * Returns TRUE if the number of callers is 0 in the irg's environment,
- * hence this irg is a leave.
- */
-inline static int is_leave(ir_graph *irg)
-{
- inline_irg_env *env = (inline_irg_env*)get_irg_link(irg);
- return env->n_call_nodes == 0;
-}
-
-/**
- * Returns TRUE if the number of nodes in the callee is
- * smaller then size in the irg's environment.
- */
-inline static int is_smaller(ir_graph *callee, unsigned size)
-{
- inline_irg_env *env = (inline_irg_env*)get_irg_link(callee);
- return env->n_nodes < size;
-}
-
/**
* Duplicate a call entry.
*
return nentry;
}
-/**
- * Append all call nodes of the source environment to the nodes of in the destination
- * environment.
- *
- * @param dst destination environment
- * @param src source environment
- * @param loop_depth the loop depth of the call that is replaced by the src list
- */
-static void append_call_list(inline_irg_env *dst, inline_irg_env *src, int loop_depth)
-{
- call_entry *entry, *nentry;
-
- /* Note that the src list points to Call nodes in the inlined graph, but
- we need Call nodes in our graph. Luckily the inliner leaves this information
- in the link field. */
- list_for_each_entry(call_entry, entry, &src->calls, list) {
- nentry = duplicate_call_entry(entry, (ir_node*)get_irn_link(entry->call), loop_depth);
- list_add_tail(&nentry->list, &dst->calls);
- }
- dst->n_call_nodes += src->n_call_nodes;
- dst->n_nodes += src->n_nodes;
-}
-
-/*
- * Inlines small leave methods at call sites where the called address comes
- * from a Const node that references the entity representing the called
- * method.
- * The size argument is a rough measure for the code size of the method:
- * Methods where the obstack containing the firm graph is smaller than
- * size are inlined.
- */
-void inline_leave_functions(unsigned maxsize, unsigned leavesize,
- unsigned size, int ignore_runtime)
-{
- inline_irg_env *env;
- ir_graph *irg;
- size_t i, n_irgs;
- ir_graph *rem;
- int did_inline;
- wenv_t wenv;
- call_entry *entry, *next;
- const call_entry *centry;
- pmap *copied_graphs;
- pmap_entry *pm_entry;
-
- rem = current_ir_graph;
- obstack_init(&temp_obst);
-
- /* a map for the copied graphs, used to inline recursive calls */
- copied_graphs = pmap_create();
-
- /* extend all irgs by a temporary data structure for inlining. */
- n_irgs = get_irp_n_irgs();
- for (i = 0; i < n_irgs; ++i)
- set_irg_link(get_irp_irg(i), alloc_inline_irg_env());
-
- /* Pre-compute information in temporary data structure. */
- wenv.ignore_runtime = ignore_runtime;
- wenv.ignore_callers = 0;
- for (i = 0; i < n_irgs; ++i) {
- ir_graph *irg = get_irp_irg(i);
-
- assert(get_irg_phase_state(irg) != phase_building);
- free_callee_info(irg);
-
- assure_cf_loop(irg);
- wenv.x = (inline_irg_env*)get_irg_link(irg);
- irg_walk_graph(irg, NULL, collect_calls2, &wenv);
- }
-
- /* -- and now inline. -- */
-
- /* Inline leaves recursively -- we might construct new leaves. */
- do {
- did_inline = 0;
-
- for (i = 0; i < n_irgs; ++i) {
- ir_node *call;
- int phiproj_computed = 0;
-
- current_ir_graph = get_irp_irg(i);
- env = (inline_irg_env*)get_irg_link(current_ir_graph);
-
- ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
- list_for_each_entry_safe(call_entry, entry, next, &env->calls, list) {
- ir_graph *callee;
- irg_inline_property prop;
-
- if (env->n_nodes > maxsize)
- break;
-
- call = entry->call;
- callee = entry->callee;
-
- prop = get_irg_inline_property(callee);
- if (prop == irg_inline_forbidden) {
- continue;
- }
-
- if (is_leave(callee) && (
- is_smaller(callee, leavesize) || prop >= irg_inline_forced)) {
- if (!phiproj_computed) {
- phiproj_computed = 1;
- collect_phiprojs(current_ir_graph);
- }
- did_inline = inline_method(call, callee);
-
- if (did_inline) {
- inline_irg_env *callee_env = (inline_irg_env*)get_irg_link(callee);
-
- /* call was inlined, Phi/Projs for current graph must be recomputed */
- phiproj_computed = 0;
-
- /* Do some statistics */
- env->got_inline = 1;
- --env->n_call_nodes;
- env->n_nodes += callee_env->n_nodes;
- --callee_env->n_callers;
-
- /* remove this call from the list */
- list_del(&entry->list);
- continue;
- }
- }
- }
- ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
- }
- } while (did_inline);
-
- /* inline other small functions. */
- for (i = 0; i < n_irgs; ++i) {
- ir_node *call;
- int phiproj_computed = 0;
-
- current_ir_graph = get_irp_irg(i);
- env = (inline_irg_env*)get_irg_link(current_ir_graph);
-
- ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
-
- /* note that the list of possible calls is updated during the process */
- list_for_each_entry_safe(call_entry, entry, next, &env->calls, list) {
- irg_inline_property prop;
- ir_graph *callee;
- pmap_entry *e;
-
- call = entry->call;
- callee = entry->callee;
-
- prop = get_irg_inline_property(callee);
- if (prop == irg_inline_forbidden) {
- continue;
- }
-
- e = pmap_find(copied_graphs, callee);
- if (e != NULL) {
- /*
- * Remap callee if we have a copy.
- * FIXME: Should we do this only for recursive Calls ?
- */
- callee = (ir_graph*)e->value;
- }
-
- if (prop >= irg_inline_forced ||
- (is_smaller(callee, size) && env->n_nodes < maxsize) /* small function */) {
- if (current_ir_graph == callee) {
- /*
- * Recursive call: we cannot directly inline because we cannot walk
- * the graph and change it. So we have to make a copy of the graph
- * first.
- */
-
- inline_irg_env *callee_env;
- ir_graph *copy;
-
- ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
-
- /*
- * No copy yet, create one.
- * Note that recursive methods are never leaves, so it is sufficient
- * to test this condition here.
- */
- copy = create_irg_copy(callee);
-
- /* create_irg_copy() destroys the Proj links, recompute them */
- phiproj_computed = 0;
-
- ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
-
- /* allocate new environment */
- callee_env = alloc_inline_irg_env();
- set_irg_link(copy, callee_env);
-
- assure_cf_loop(copy);
- wenv.x = callee_env;
- wenv.ignore_callers = 1;
- irg_walk_graph(copy, NULL, collect_calls2, &wenv);
-
- /*
- * Enter the entity of the original graph. This is needed
- * for inline_method(). However, note that ent->irg still points
- * to callee, NOT to copy.
- */
- set_irg_entity(copy, get_irg_entity(callee));
-
- pmap_insert(copied_graphs, callee, copy);
- callee = copy;
-
- /* we have only one caller: the original graph */
- callee_env->n_callers = 1;
- callee_env->n_callers_orig = 1;
- }
- if (! phiproj_computed) {
- phiproj_computed = 1;
- collect_phiprojs(current_ir_graph);
- }
- did_inline = inline_method(call, callee);
- if (did_inline) {
- inline_irg_env *callee_env = (inline_irg_env *)get_irg_link(callee);
-
- /* call was inlined, Phi/Projs for current graph must be recomputed */
- phiproj_computed = 0;
-
- /* callee was inline. Append its call list. */
- env->got_inline = 1;
- --env->n_call_nodes;
- append_call_list(env, callee_env, entry->loop_depth);
- --callee_env->n_callers;
-
- /* after we have inlined callee, all called methods inside callee
- are now called once more */
- list_for_each_entry(call_entry, centry, &callee_env->calls, list) {
- inline_irg_env *penv = (inline_irg_env*)get_irg_link(centry->callee);
- ++penv->n_callers;
- }
-
- /* remove this call from the list */
- list_del(&entry->list);
- continue;
- }
- }
- }
- ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_LINK|IR_RESOURCE_PHI_LIST);
- }
-
- for (i = 0; i < n_irgs; ++i) {
- irg = get_irp_irg(i);
- env = (inline_irg_env*)get_irg_link(irg);
-
- if (env->got_inline) {
- optimize_graph_df(irg);
- optimize_cf(irg);
- }
- if (env->got_inline || (env->n_callers_orig != env->n_callers)) {
- DB((dbg, LEVEL_1, "Nodes:%3d ->%3d, calls:%3d ->%3d, callers:%3d ->%3d, -- %s\n",
- env->n_nodes_orig, env->n_nodes, env->n_call_nodes_orig, env->n_call_nodes,
- env->n_callers_orig, env->n_callers,
- get_entity_name(get_irg_entity(irg))));
- }
- }
-
- /* kill the copied graphs: we don't need them anymore */
- foreach_pmap(copied_graphs, pm_entry) {
- ir_graph *copy = (ir_graph*)pm_entry->value;
-
- /* reset the entity, otherwise it will be deleted in the next step ... */
- set_irg_entity(copy, NULL);
- free_ir_graph(copy);
- }
- pmap_destroy(copied_graphs);
-
- obstack_free(&temp_obst, NULL);
- current_ir_graph = rem;
-}
-
-typedef struct inline_leave_functions_pass_t {
- ir_prog_pass_t pass;
- unsigned maxsize;
- unsigned leavesize;
- unsigned size;
- int ignore_runtime;
-} inline_leave_functions_pass_t;
-
-/**
- * Wrapper to run inline_leave_functions() as a ir_prog pass.
- */
-static int inline_leave_functions_wrapper(ir_prog *irp, void *context)
-{
- inline_leave_functions_pass_t *pass = (inline_leave_functions_pass_t*)context;
-
- (void)irp;
- inline_leave_functions(
- pass->maxsize, pass->leavesize,
- pass->size, pass->ignore_runtime);
- return 0;
-}
-
-/* create a pass for inline_leave_functions() */
-ir_prog_pass_t *inline_leave_functions_pass(
- const char *name, unsigned maxsize, unsigned leavesize,
- unsigned size, int ignore_runtime)
-{
- inline_leave_functions_pass_t *pass = XMALLOCZ(inline_leave_functions_pass_t);
-
- pass->maxsize = maxsize;
- pass->leavesize = leavesize;
- pass->size = size;
- pass->ignore_runtime = ignore_runtime;
-
- return def_prog_pass_constructor(
- &pass->pass,
- name ? name : "inline_leave_functions",
- inline_leave_functions_wrapper);
-}
-
/**
* Calculate the parameter weights for transmitting the address of a local variable.
*/
static unsigned calc_method_local_weight(ir_node *arg)
{
- int i, j, k;
+ int j;
unsigned v, weight = 0;
- for (i = get_irn_n_outs(arg) - 1; i >= 0; --i) {
+ for (unsigned i = get_irn_n_outs(arg); i-- > 0; ) {
ir_node *succ = get_irn_out(arg, i);
switch (get_irn_opcode(succ)) {
ir_node *pred = get_Tuple_pred(succ, j);
if (pred == arg) {
/* look for Proj(j) */
- for (k = get_irn_n_outs(succ) - 1; k >= 0; --k) {
+ for (unsigned k = get_irn_n_outs(succ); k-- > 0; ) {
ir_node *succ_succ = get_irn_out(succ, k);
if (is_Proj(succ_succ)) {
if (get_Proj_proj(succ_succ) == j) {
ir_entity *ent = get_irg_entity(irg);
ir_type *mtp;
size_t nparams;
- int i;
long proj_nr;
ir_node *irg_args, *arg;
assure_irg_outs(irg);
irg_args = get_irg_args(irg);
- for (i = get_irn_n_outs(irg_args) - 1; i >= 0; --i) {
+ for (unsigned i = get_irn_n_outs(irg_args); i-- > 0; ) {
arg = get_irn_out(irg_args, i);
proj_nr = get_Proj_proj(arg);
env->local_weights[proj_nr] = calc_method_local_weight(arg);
{
ir_node *call = entry->call;
ir_entity *ent = get_irg_entity(callee);
+ ir_type *callee_frame;
+ size_t i, n_members, n_params;
ir_node *frame_ptr;
ir_type *mtp;
int weight = 0;
- int i, n_params, all_const;
+ int all_const;
unsigned cc, v;
- irg_inline_property prop;
inline_irg_env *callee_env;
- prop = get_irg_inline_property(callee);
- if (prop == irg_inline_forbidden) {
+ mtp_additional_properties props = get_entity_additional_properties(ent);
+ if (props & mtp_property_noinline) {
DB((dbg, LEVEL_2, "In %+F Call to %+F: inlining forbidden\n",
call, callee));
return entry->benefice = INT_MIN;
}
- if (get_irg_additional_properties(callee) & mtp_property_noreturn) {
+ callee_frame = get_irg_frame_type(callee);
+ n_members = get_class_n_members(callee_frame);
+ for (i = 0; i < n_members; ++i) {
+ ir_entity *frame_ent = get_class_member(callee_frame, i);
+ if (is_parameter_entity(frame_ent)) {
+ // TODO inliner should handle parameter entities by inserting Store operations
+ DB((dbg, LEVEL_2, "In %+F Call to %+F: inlining forbidden due to parameter entity\n", call, callee));
+ add_entity_additional_properties(ent, mtp_property_noinline);
+ return entry->benefice = INT_MIN;
+ }
+ }
+
+ if (props & mtp_property_noreturn) {
DB((dbg, LEVEL_2, "In %+F Call to %+F: not inlining noreturn or weak\n",
call, callee));
return entry->benefice = INT_MIN;
cc = get_method_calling_convention(mtp);
if (cc & cc_reg_param) {
/* register parameter, smaller costs for register parameters */
- int max_regs = cc & ~cc_bits;
+ size_t max_regs = cc & ~cc_bits;
if (max_regs < n_params)
weight += max_regs * 2 + (n_params - max_regs) * 5;
if (callee_env->n_nodes < 30 && !callee_env->recursive)
weight += 2000;
- /* and finally for leaves: they do not increase the register pressure
+ /* and finally for leafs: they do not increase the register pressure
because of callee safe registers */
if (callee_env->n_call_nodes == 0)
weight += 400;
callgraph_walk(NULL, callgraph_walker, &env);
assert(n_irgs == env.last_irg);
+ free_callgraph();
+
return env.irgs;
}
static void maybe_push_call(pqueue_t *pqueue, call_entry *call,
int inline_threshold)
{
- ir_graph *callee = call->callee;
- irg_inline_property prop = get_irg_inline_property(callee);
- int benefice = calc_inline_benefice(call, callee);
+ ir_graph *callee = call->callee;
+ int benefice = calc_inline_benefice(call, callee);
DB((dbg, LEVEL_2, "In %+F Call %+F to %+F has benefice %d\n",
get_irn_irg(call->call), call->call, callee, benefice));
- if (prop < irg_inline_forced && benefice < inline_threshold) {
+ ir_entity *ent = get_irg_entity(callee);
+ mtp_additional_properties props = get_entity_additional_properties(ent);
+ if (!(props & mtp_property_always_inline) && benefice < inline_threshold) {
return;
}
{
int phiproj_computed = 0;
inline_irg_env *env = (inline_irg_env*)get_irg_link(irg);
- call_entry *curr_call;
wenv_t wenv;
pqueue_t *pqueue;
ir_graph *callee = curr_call->callee;
ir_node *call_node = curr_call->call;
inline_irg_env *callee_env = (inline_irg_env*)get_irg_link(callee);
- irg_inline_property prop = get_irg_inline_property(callee);
+ ir_entity *ent = get_irg_entity(callee);
+ mtp_additional_properties props
+ = get_entity_additional_properties(ent);
+ ir_graph *calleee;
int loop_depth;
- const call_entry *centry;
- pmap_entry *e;
- if ((prop < irg_inline_forced) && env->n_nodes + callee_env->n_nodes > maxsize) {
+ if (!(props & mtp_property_always_inline)
+ && env->n_nodes + callee_env->n_nodes > maxsize) {
DB((dbg, LEVEL_2, "%+F: too big (%d) + %+F (%d)\n", irg,
env->n_nodes, callee, callee_env->n_nodes));
continue;
}
- e = pmap_find(copied_graphs, callee);
- if (e != NULL) {
+ calleee = pmap_get(ir_graph, copied_graphs, callee);
+ if (calleee != NULL) {
int benefice = curr_call->benefice;
/*
* Reduce the weight for recursive function IFF not all arguments are const.
/*
* Remap callee if we have a copy.
*/
- callee = (ir_graph*)e->value;
+ callee = calleee;
callee_env = (inline_irg_env*)get_irg_link(callee);
}
/*
* No copy yet, create one.
- * Note that recursive methods are never leaves, so it is
+ * Note that recursive methods are never leafs, so it is
* sufficient to test this condition here.
*/
copy = create_irg_copy(callee);
callee_env = alloc_inline_irg_env();
set_irg_link(copy, callee_env);
- assure_cf_loop(copy);
+ assure_irg_properties(copy, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
memset(&wenv, 0, sizeof(wenv));
wenv.x = callee_env;
wenv.ignore_callers = 1;
* but we need Call nodes in our graph. Luckily the inliner leaves
* this information in the link field. */
new_call = (ir_node*)get_irn_link(centry->call);
+ if (get_irn_irg(new_call) != irg) {
+ /* centry->call has not been copied, which means it is dead.
+ * This might happen during inlining, if a const function,
+ * which cannot be inlined is only used as an unused argument
+ * of another function, which is inlined. */
+ continue;
+ }
assert(is_Call(new_call));
new_entry = duplicate_call_entry(centry, new_call, loop_depth);
free_callee_info(irg);
wenv.x = (inline_irg_env*)get_irg_link(irg);
- assure_cf_loop(irg);
+ assure_loopinfo(irg);
irg_walk_graph(irg, NULL, collect_calls2, &wenv);
}