3 * File name: ir/opt/funccall.c
4 * Purpose: optimization of function calls
8 * Copyright: (c) 1998-2004 Universit�t Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
12 #include "irgraph_t.h"
16 #include "dbginfo_t.h"
23 * The walker environment for rem_mem_from_const_fkt_calls
25 typedef struct _env_t {
26 int changed; /**< flag, is set if a graph was changed */
27 int n_calls_removed_SymConst;
28 int n_calls_removed_Sel;
32 * remove memory from const function calls by rerouting
33 * it's ProjM and connection the call with a NoMem node.
35 * Note: By "const function" we understand a function that did neither
36 * read nor write memory. Hence its result depends solely on its
39 static void rem_mem_from_const_fkt_calls(ir_node *node, void *env)
42 ir_node *call, *ptr, *mem;
45 if (get_irn_op(node) == op_Call) {
48 set_irn_link(call, NULL);
50 ptr = get_Call_ptr(call);
51 if (get_irn_op(ptr) == op_SymConst && get_SymConst_kind(ptr) == symconst_addr_ent) {
52 ent = get_SymConst_entity(ptr);
54 if ((get_entity_additional_properties(ent) & mtp_property_const) == 0)
56 ++ctx->n_calls_removed_SymConst;
58 else if (is_Sel(ptr) &&
59 get_irg_callee_info_state(current_ir_graph) == irg_callee_info_consistent) {
60 /* If all possible callees are real functions, we can remove the memory edge. */
61 int i, n_callees = get_Call_n_callees(call);
63 /* This is kind of strange: dying code or a Call that will raise an exception
64 when executed as there is no implementation to call. So better not
67 for (i = 0; i < n_callees; ++i) {
68 ent = get_Call_callee(call, i);
69 if (ent == unknown_entity) {
70 /* we don't know which entity is called here */
73 if ((get_entity_additional_properties(ent) & mtp_property_const) == 0)
76 ++ctx->n_calls_removed_Sel;
81 /* ok, if we get here we found a call to a const function,
82 * route the NoMem node to the call */
83 mem = get_Call_mem(call);
85 set_irn_link(call, mem);
86 set_Call_mem(call, new_r_NoMem(current_ir_graph));
88 /* finally, this call can float */
89 set_irn_pinned(call, op_pin_state_floats);
91 hook_func_call(current_ir_graph, call);
95 else if (get_irn_op(node) == op_Proj) {
97 * Remove memory and exception Proj's from
98 * const function calls.
100 call = get_Proj_pred(node);
101 if ((get_irn_op(call) != op_Call) ||
102 (get_irn_op(get_Call_mem(call)) != op_NoMem))
105 switch (get_Proj_proj(node)) {
106 case pn_Call_M_regular: {
107 ir_node *old_mem = get_irn_link(call);
109 exchange(node, old_mem);
113 case pn_Call_X_except:
114 case pn_Call_M_except:
115 exchange(node, new_Bad());
124 * optimize function calls by handling const functions
126 void optimize_funccalls(int force_run)
130 unsigned num_pure = 0;
132 if (! get_opt_real_function_call())
135 /* first step: detect, which functions are const, i.e. do NOT touch any memory */
136 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
137 ir_graph *irg = get_irp_irg(i);
138 ir_node *end = get_irg_end(irg);
139 ir_node *endbl = get_nodes_block(end);
143 if (get_irg_additional_properties(irg) & mtp_property_const) {
144 /* already marked as a const function */
148 /* visit every Return */
149 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
150 ir_node *node = get_Block_cfgpred(endbl, j);
151 ir_op *op = get_irn_op(node);
154 /* Bad nodes usually do NOT produce anything, so it's ok */
158 if (op == op_Return) {
159 mem = get_Return_mem(node);
161 /* Bad nodes usually do NOT produce anything, so it's ok */
165 change = mem != get_irg_initial_mem(irg);
170 /* exception found */
177 /* check, if a keep-alive exists */
178 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
179 ir_node *mem = get_End_keepalive(end, j);
181 if (mode_M != get_irn_mode(mem))
184 change = mem != get_irg_initial_mem(irg);
191 /* no memory changes found, it's a const function */
192 set_irg_additional_property(irg, mtp_property_const);
198 if (force_run || num_pure > 0) {
201 ctx.n_calls_removed_SymConst = 0;
202 ctx.n_calls_removed_Sel = 0;
204 /* all calls of pure functions can be transformed into FuncCalls */
205 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
206 ir_graph *irg = get_irp_irg(i);
208 /* no need to do this on const functions */
209 if ((get_irg_additional_properties(irg) & mtp_property_const) == 0) {
211 irg_walk_graph(irg, NULL, rem_mem_from_const_fkt_calls, &ctx);
214 /* changes were done including exception edges */
215 set_irg_outs_inconsistent(irg);
216 set_irg_doms_inconsistent(irg);
217 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
222 if (get_firm_verbosity()) {
223 printf("Detected %d graphs without side effects.\n", num_pure);
224 printf("Optimizes %d(SymConst) + %d(Sel) calls to const functions.\n",
225 ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
229 if (get_firm_verbosity()) {
230 printf("No graphs without side effects detected\n");