3 * File name: ir/opt/funccall.c
4 * Purpose: optimization of function calls
8 * Copyright: (c) 1998-2006 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 n_calls_removed_SymConst;
27 int n_calls_removed_Sel;
28 ir_node *const_call_list; /**< The list of all const function calls that will be changed. */
29 ir_node *pure_call_list; /**< The list of all pure function calls that will be changed. */
30 ir_node *proj_list; /**< The list of all potential Proj nodes that must be fixed. */
34 * Collect all calls to const and pure functions
35 * to lists. Collect of Proj nodes into a Proj list.
37 static void collect_calls(ir_node *node, void *env)
44 if (get_irn_op(node) == op_Call) {
47 /* the link will be NULL for all non-const/pure calls */
48 set_irn_link(call, NULL);
49 ptr = get_Call_ptr(call);
50 if (get_irn_op(ptr) == op_SymConst && get_SymConst_kind(ptr) == symconst_addr_ent) {
51 ent = get_SymConst_entity(ptr);
53 mode = get_entity_additional_properties(ent);
54 if ((mode & (mtp_property_const|mtp_property_pure)) == 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 const 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
68 /* note that const function are a subset of pure ones */
69 mode = mtp_property_const | mtp_property_pure;
70 for (i = 0; i < n_callees; ++i) {
71 ent = get_Call_callee(call, i);
72 if (ent == unknown_entity) {
73 /* we don't know which entity is called here */
76 mode &= get_entity_additional_properties(ent);
80 ++ctx->n_calls_removed_Sel;
85 /* ok, if we get here we found a call to a const or a pure function */
86 if (mode & mtp_property_pure) {
87 set_irn_link(call, ctx->pure_call_list);
88 ctx->pure_call_list = call;
91 set_irn_link(call, ctx->const_call_list);
92 ctx->const_call_list = call;
95 else if (get_irn_op(node) == op_Proj) {
97 * Collect all memory and exception Proj's from
100 call = get_Proj_pred(node);
101 if (get_irn_op(call) != op_Call)
104 switch (get_Proj_proj(node)) {
105 case pn_Call_M_regular:
106 case pn_Call_X_except:
107 case pn_Call_M_except:
108 set_irn_link(node, ctx->proj_list);
109 ctx->proj_list = node;
115 } /* rem_mem_from_const_fkt_calls */
118 * Fix the list of collected Calls.
120 * @param irg the graph that contained calls to pure functions
121 * @param call_list the list of all call sites of pure functions
122 * @param proj_list the list of all memory/exception Projs of this call sites
124 static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
125 ir_node *call, *next, *mem, *proj;
128 /* fix all calls by removing it's memory input */
129 for (call = call_list; call; call = next) {
130 next = get_irn_link(call);
131 mem = get_Call_mem(call);
133 set_irn_link(call, mem);
134 set_Call_mem(call, get_irg_no_mem(irg));
137 * Sorrily we cannot simply set the node to 'float'.
138 * There is a reason for that:
140 * - The call might be inside a loop/if that is NOT entered
141 * and calls a endless function. Setting the call to float
142 * would allow to move it out from the loop/if causing this
143 * function be called even if the loop/if is not entered ...
145 * This could be fixed using post-dominators for calls and Pin nodes
146 * but need some more analyzes to ensure that a call that potential
147 * never returns is not executed before some code that generates
148 * observable states...
151 /* finally, this call can float
152 set_irn_pinned(call, op_pin_state_floats); */
153 hook_func_call(irg, call);
156 /* finally fix all Proj's */
157 for (proj = proj_list; proj; proj = next) {
158 next = get_irn_link(proj);
159 call = get_Proj_pred(proj);
160 mem = get_irn_link(call);
162 /* beware of calls in the pure call list */
163 if (! mem || get_irn_op(mem) == op_Call)
165 assert(get_irn_mode(mem) == mode_M);
167 switch (get_Proj_proj(proj)) {
168 case pn_Call_M_regular: {
171 case pn_Call_X_except:
172 case pn_Call_M_except:
174 exchange(proj, get_irg_bad(irg));
181 /* changes were done ... */
182 set_irg_outs_inconsistent(irg);
183 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
186 /* ... including exception edges */
187 set_irg_doms_inconsistent(irg);
189 } /* fix_call_list */
193 * Check if a graph represents a const function.
195 * @param irg the graph
197 static int is_const_function(ir_graph *irg)
199 ir_node *end, *endbl;
202 if (get_irg_additional_properties(irg) & mtp_property_const) {
203 /* already marked as a const function */
207 end = get_irg_end(irg);
208 endbl = get_nodes_block(end);
211 /* visit every Return */
212 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
213 ir_node *node = get_Block_cfgpred(endbl, j);
214 ir_op *op = get_irn_op(node);
217 /* Bad nodes usually do NOT produce anything, so it's ok */
221 if (op == op_Return) {
222 mem = get_Return_mem(node);
224 /* Bad nodes usually do NOT produce anything, so it's ok */
228 change = mem != get_irg_initial_mem(irg);
233 /* exception found */
240 /* check, if a keep-alive exists */
241 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
242 ir_node *mem = get_End_keepalive(end, j);
244 if (mode_M != get_irn_mode(mem))
247 change = mem != get_irg_initial_mem(irg);
254 /* no memory changes found, it's a const function */
255 set_irg_additional_property(irg, mtp_property_const);
259 } /* is_const_function */
265 #define UNMARK_IRG(irg) set_irg_link((irg), NULL)
266 #define MARK_IRG(irg) set_irg_link((irg), MARK)
267 #define IS_IRG_MARKED(irg) (get_irg_link(irg) == MARK)
270 static int is_pure_function(ir_graph *irg);
272 #define UMAX(a,b) (a) > (b) ? (a) : (b)
275 * Follow the memory chain starting at node and determine
278 * @return mtp_property_const if only calls of const functions are detected
279 * mtp_property_pure if only Loads and const/pure
283 static unsigned _follow_mem(ir_node *node) {
284 unsigned m, mode = mtp_property_const;
289 if (irn_visited(node))
292 mark_irn_visited(node);
294 switch (get_irn_opcode(node)) {
296 node = get_Proj_pred(node);
305 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
306 mode &= _follow_mem(get_irn_n(node, i));
311 /* Beware volatile Loads are NOT allowed in pure functions */
312 if (get_Load_volatility(node) == volatility_is_volatile)
314 mode = mtp_property_pure;
315 node = get_Load_mem(node);
319 /* a call is only tolerable if its either constant or pure */
320 ptr = get_Call_ptr(node);
321 if (get_irn_op(ptr) == op_SymConst &&
322 get_SymConst_kind(ptr) == symconst_addr_ent) {
323 entity *ent = get_SymConst_entity(ptr);
324 ir_graph *irg = get_entity_irg(ent);
326 if (irg == current_ir_graph) {
327 /* A recursive call. The did not mode depend on this call */
329 else if (irg == NULL) {
330 m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
333 mode = UMAX(mode, m);
335 else if (irg != NULL) {
336 /* we have a graph. Check if it is already analyzed */
337 if (IS_IRG_MARKED(irg))
338 (void)is_pure_function(irg);
340 m = get_irg_additional_properties(irg) & (mtp_property_const|mtp_property_pure);
343 mode = UMAX(mode, m);
348 node = get_Call_mem(node);
358 * Follow the memory chain starting at node and determine
361 * @return mtp_property_const if only calls of const functions are detected
362 * mtp_property_pure if only Loads and const/pure
366 static unsigned follow_mem(ir_graph *irg, ir_node *node, unsigned mode) {
369 inc_irg_visited(irg);
370 /* mark the initial mem: recursion stops here */
371 mark_irn_visited(get_irg_initial_mem(irg));
372 m = _follow_mem(node);
375 return UMAX(mode, m);
379 * Check if a graph represents a pure function.
381 * @param irg the graph
383 static int is_pure_function(ir_graph *irg) {
384 ir_node *end, *endbl;
386 unsigned mode = get_irg_additional_properties(irg);
387 ir_graph *rem = current_ir_graph;
389 if (mode & mtp_property_const) {
390 /* already marked as a const function */
391 return mtp_property_const;
393 if (mode & mtp_property_pure) {
394 /* already marked as a pure function */
395 return mtp_property_const;
398 if (! IS_IRG_MARKED(irg))
402 end = get_irg_end(irg);
403 endbl = get_nodes_block(end);
404 mode = mtp_property_const;
406 current_ir_graph = irg;
408 /* visit every Return */
409 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
410 ir_node *node = get_Block_cfgpred(endbl, j);
411 ir_op *op = get_irn_op(node);
414 /* Bad nodes usually do NOT produce anything, so it's ok */
418 if (op == op_Return) {
419 mem = get_Return_mem(node);
421 /* Bad nodes usually do NOT produce anything, so it's ok */
425 if (mem != get_irg_initial_mem(irg))
426 mode = follow_mem(irg, mem, mode);
429 /* exception found. */
430 mode = follow_mem(irg, node, mode);
438 /* check, if a keep-alive exists */
439 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
440 ir_node *mem = get_End_keepalive(end, j);
442 if (mode_M != get_irn_mode(mem))
445 mode = follow_mem(irg, mem, mode);
452 set_irg_additional_property(irg, mode);
453 current_ir_graph = rem;
455 } /* is_pure_function */
458 * Handle calls to const functions.
460 static void handle_const_Calls(env_t *ctx)
464 ctx->n_calls_removed_SymConst = 0;
465 ctx->n_calls_removed_Sel = 0;
467 /* all calls of const functions can be transformed */
468 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
469 ir_graph *irg = get_irp_irg(i);
471 ctx->const_call_list = NULL;
472 ctx->pure_call_list = NULL;
473 ctx->proj_list = NULL;
474 irg_walk_graph(irg, NULL, collect_calls, ctx);
476 if (ctx->const_call_list)
477 fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
479 } /* handle_const_Calls */
482 * optimize function calls by handling const functions
484 void optimize_funccalls(int force_run)
487 unsigned num_const = 0;
488 unsigned num_pure = 0;
490 if (! get_opt_function_call())
493 /* prepare: mark all graphs as not analyzed */
494 n = get_irp_n_irgs();
495 for (i = n - 1; i >= 0; --i)
496 MARK_IRG(get_irp_irg(i));
498 /* first step: detect, which functions are const, i.e. do NOT touch any memory */
499 for (i = n - 1; i >= 0; --i) {
500 ir_graph *irg = get_irp_irg(i);
501 unsigned mode = is_pure_function(irg);
503 if (mode & mtp_property_const)
505 else if (mode & mtp_property_pure)
509 if (force_run || num_const > 0) {
512 handle_const_Calls(&ctx);
513 if (get_firm_verbosity()) {
514 printf("Detected %d graphs without side effects.\n", num_const);
515 printf("Optimizes %d(SymConst) + %d(Sel) calls to const/pure functions.\n",
516 ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
520 if (get_firm_verbosity()) {
521 printf("No graphs without side effects detected\n");
524 } /* optimize_funccalls */