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 (get_irn_op(ptr) == op_Sel &&
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 ((get_entity_additional_properties(ent) & mtp_property_const) == 0)
72 ++ctx->n_calls_removed_Sel;
77 /* ok, if we get here we found a call to a const function,
78 * route the NoMem node to the call */
79 mem = get_Call_mem(call);
81 set_irn_link(call, mem);
82 set_Call_mem(call, new_r_NoMem(current_ir_graph));
84 /* finally, this call can float */
85 set_irn_pinned(call, op_pin_state_floats);
87 hook_func_call(current_ir_graph, call);
91 else if (get_irn_op(node) == op_Proj) {
93 * Remove memory and exception Proj's from
94 * const function calls.
96 call = get_Proj_pred(node);
97 if ((get_irn_op(call) != op_Call) ||
98 (get_irn_op(get_Call_mem(call)) != op_NoMem))
101 switch (get_Proj_proj(node)) {
102 case pn_Call_M_regular: {
103 ir_node *old_mem = get_irn_link(call);
105 exchange(node, old_mem);
107 case pn_Call_X_except:
108 case pn_Call_M_except:
109 exchange(node, new_Bad());
117 * optimize function calls by handling const functions
119 void optimize_funccalls(int force_run)
123 unsigned num_pure = 0;
125 if (! get_opt_real_func_call())
128 /* first step: detect, which functions are const, i.e. do NOT touch any memory */
129 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
130 ir_graph *irg = get_irp_irg(i);
131 ir_node *end = get_irg_end(irg);
132 ir_node *endbl = get_nodes_block(end);
136 if (get_irg_additional_properties(irg) & mtp_property_const) {
137 /* already marked as a const function */
141 /* visit every Return */
142 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
143 ir_node *node = get_Block_cfgpred(endbl, j);
144 ir_op *op = get_irn_op(node);
147 /* Bad nodes usually do NOT produce anything, so it's ok */
151 if (op == op_Return) {
152 mem = get_Return_mem(node);
154 /* Bad nodes usually do NOT produce anything, so it's ok */
158 change = mem != get_irg_initial_mem(irg);
163 /* exception found */
170 /* check, if a keep-alive exists */
171 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
172 ir_node *mem = get_End_keepalive(end, j);
174 if (mode_M != get_irn_mode(mem))
177 change = mem != get_irg_initial_mem(irg);
184 /* no memory changes found, it's a const function */
185 set_irg_additional_property(irg, mtp_property_const);
191 if (force_run || num_pure > 0) {
194 ctx.n_calls_removed_SymConst = 0;
195 ctx.n_calls_removed_Sel = 0;
197 /* all calls of pure functions can be transformed into FuncCalls */
198 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
199 ir_graph *irg = get_irp_irg(i);
201 /* no need to do this on const functions */
202 if ((get_irg_additional_properties(irg) & mtp_property_const) == 0) {
204 irg_walk_graph(irg, NULL, rem_mem_from_const_fkt_calls, &ctx);
207 /* changes were done */
208 set_irg_outs_inconsistent(irg);
209 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
214 if (get_firm_verbosity()) {
215 printf("Detected %d graphs without side effects.\n", num_pure);
216 printf("Optimizes %d(SymConst) + %d(Sel) calls to const functions.\n",
217 ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
221 if (get_firm_verbosity()) {
222 printf("No graphs without side effects detected\n");