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 all Proj(Call) nodes into a Proj list.
37 static void collect_calls(ir_node *node, void *env)
47 /* set the link to 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;
57 } else if (get_opt_closed_world() &&
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;
84 /* ok, if we get here we found a call to a const or a pure function */
85 if (mode & mtp_property_pure) {
86 set_irn_link(call, ctx->pure_call_list);
87 ctx->pure_call_list = call;
89 set_irn_link(call, ctx->const_call_list);
90 ctx->const_call_list = call;
92 } else if (is_Proj(node)) {
94 * Collect all memory and exception Proj's from
97 call = get_Proj_pred(node);
101 /* collect the Proj's in the Proj list */
102 switch (get_Proj_proj(node)) {
103 case pn_Call_M_regular:
104 case pn_Call_X_except:
105 case pn_Call_M_except:
106 set_irn_link(node, ctx->proj_list);
107 ctx->proj_list = node;
113 } /* collect_calls */
116 * Fix the list of collected Calls.
118 * @param irg the graph that contained calls to pure functions
119 * @param call_list the list of all call sites of const functions
120 * @param proj_list the list of all memory/exception Proj's of this call sites
122 static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
123 ir_node *call, *next, *mem, *proj;
126 /* First step: fix all calls by removing it's memory input.
127 It's original memory input is preserved in their link fields. */
128 for (call = call_list; call; call = next) {
129 next = get_irn_link(call);
130 mem = get_Call_mem(call);
132 set_irn_link(call, mem);
133 set_Call_mem(call, get_irg_no_mem(irg));
136 * Sorrily we cannot simply set the node to 'float'.
137 * There is a reason for that:
139 * - The call might be inside a loop/if that is NOT entered
140 * and calls a endless function. Setting the call to float
141 * would allow to move it out from the loop/if causing this
142 * function be called even if the loop/if is not entered ...
144 * This could be fixed using post-dominators for calls and Pin nodes
145 * but need some more analyzes to ensure that a call that potential
146 * never returns is not executed before some code that generates
147 * observable states...
150 /* finally, this call can float
151 set_irn_pinned(call, op_pin_state_floats); */
152 hook_func_call(irg, call);
155 /* Second step: fix all Proj's */
156 for (proj = proj_list; proj; proj = next) {
157 next = get_irn_link(proj);
158 call = get_Proj_pred(proj);
159 mem = get_irn_link(call);
161 /* beware of calls in the pure call list */
162 if (! mem || get_irn_op(mem) == op_Call)
164 assert(get_irn_mode(mem) == mode_M);
166 switch (get_Proj_proj(proj)) {
167 case pn_Call_M_regular: {
168 /* in dead code there might be cycles where proj == mem */
172 case pn_Call_X_except:
173 case pn_Call_M_except:
175 exchange(proj, get_irg_bad(irg));
182 /* changes were done ... */
183 set_irg_outs_inconsistent(irg);
184 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
187 /* ... including exception edges */
188 set_irg_doms_inconsistent(irg);
190 } /* fix_call_list */
194 * Check if a graph represents a const function.
196 * @param irg the graph
198 static int is_const_function(ir_graph *irg)
200 ir_node *end, *endbl;
203 if (get_irg_additional_properties(irg) & mtp_property_const) {
204 /* already marked as a const function */
208 end = get_irg_end(irg);
209 endbl = get_nodes_block(end);
212 /* visit every Return */
213 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
214 ir_node *node = get_Block_cfgpred(endbl, j);
215 ir_op *op = get_irn_op(node);
218 /* Bad nodes usually do NOT produce anything, so it's ok */
222 if (op == op_Return) {
223 mem = get_Return_mem(node);
225 /* Bad nodes usually do NOT produce anything, so it's ok */
229 change = mem != get_irg_initial_mem(irg);
234 /* exception found */
241 /* check, if a keep-alive exists */
242 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
243 ir_node *mem = get_End_keepalive(end, j);
245 if (mode_M != get_irn_mode(mem))
248 change = mem != get_irg_initial_mem(irg);
255 /* no memory changes found, it's a const function */
256 set_irg_additional_property(irg, mtp_property_const);
260 } /* is_const_function */
267 #define UNMARK_IRG(irg) set_irg_link((irg), NULL)
268 #define MARK_IRG(irg) set_irg_link((irg), MARK)
269 #define IS_IRG_MARKED(irg) (get_irg_link(irg) == MARK)
272 static int is_pure_function(ir_graph *irg);
274 #define UMAX(a,b) (a) > (b) ? (a) : (b)
277 * Follow the memory chain starting at node and determine
280 * @return mtp_property_const if only calls of const functions are detected
281 * mtp_property_pure if only Loads and const/pure
285 static unsigned _follow_mem(ir_node *node) {
286 unsigned m, mode = mtp_property_const;
291 if (irn_visited(node))
294 mark_irn_visited(node);
296 switch (get_irn_opcode(node)) {
298 node = get_Proj_pred(node);
307 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
308 mode &= _follow_mem(get_irn_n(node, i));
313 /* Beware volatile Loads are NOT allowed in pure functions */
314 if (get_Load_volatility(node) == volatility_is_volatile)
316 mode = mtp_property_pure;
317 node = get_Load_mem(node);
321 /* a call is only tolerable if its either constant or pure */
322 ptr = get_Call_ptr(node);
323 if (get_irn_op(ptr) == op_SymConst &&
324 get_SymConst_kind(ptr) == symconst_addr_ent) {
325 entity *ent = get_SymConst_entity(ptr);
326 ir_graph *irg = get_entity_irg(ent);
328 if (irg == current_ir_graph) {
329 /* A recursive call. The did not mode depend on this call */
331 else if (irg == NULL) {
332 m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
335 mode = UMAX(mode, m);
337 else if (irg != NULL) {
338 /* we have a graph. Check if it is already analyzed */
339 if (IS_IRG_MARKED(irg))
340 (void)is_pure_function(irg);
342 m = get_irg_additional_properties(irg) & (mtp_property_const|mtp_property_pure);
345 mode = UMAX(mode, m);
350 node = get_Call_mem(node);
360 * Follow the memory chain starting at node and determine
363 * @return mtp_property_const if only calls of const functions are detected
364 * mtp_property_pure if only Loads and const/pure
368 static unsigned follow_mem(ir_graph *irg, ir_node *node, unsigned mode) {
371 inc_irg_visited(irg);
372 /* mark the initial mem: recursion stops here */
373 mark_irn_visited(get_irg_initial_mem(irg));
374 m = _follow_mem(node);
377 return UMAX(mode, m);
381 * Check if a graph represents a pure function.
383 * @param irg the graph
385 static int is_pure_function(ir_graph *irg) {
386 ir_node *end, *endbl;
388 unsigned mode = get_irg_additional_properties(irg);
389 ir_graph *rem = current_ir_graph;
391 if (mode & mtp_property_const) {
392 /* already marked as a const function */
393 return mtp_property_const;
395 if (mode & mtp_property_pure) {
396 /* already marked as a pure function */
397 return mtp_property_const;
400 if (! IS_IRG_MARKED(irg))
404 end = get_irg_end(irg);
405 endbl = get_nodes_block(end);
406 mode = mtp_property_const;
408 current_ir_graph = irg;
410 /* visit every Return */
411 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
412 ir_node *node = get_Block_cfgpred(endbl, j);
413 ir_op *op = get_irn_op(node);
416 /* Bad nodes usually do NOT produce anything, so it's ok */
420 if (op == op_Return) {
421 mem = get_Return_mem(node);
423 /* Bad nodes usually do NOT produce anything, so it's ok */
427 if (mem != get_irg_initial_mem(irg))
428 mode = follow_mem(irg, mem, mode);
431 /* exception found. */
432 mode = follow_mem(irg, node, mode);
440 /* check, if a keep-alive exists */
441 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
442 ir_node *mem = get_End_keepalive(end, j);
444 if (mode_M != get_irn_mode(mem))
447 mode = follow_mem(irg, mem, mode);
454 set_irg_additional_property(irg, mode);
455 current_ir_graph = rem;
457 } /* is_pure_function */
460 * Handle calls to const functions.
462 static void handle_const_Calls(env_t *ctx)
466 ctx->n_calls_removed_SymConst = 0;
467 ctx->n_calls_removed_Sel = 0;
469 /* all calls of const functions can be transformed */
470 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
471 ir_graph *irg = get_irp_irg(i);
473 ctx->const_call_list = NULL;
474 ctx->pure_call_list = NULL;
475 ctx->proj_list = NULL;
476 irg_walk_graph(irg, NULL, collect_calls, ctx);
478 if (ctx->const_call_list)
479 fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
481 } /* handle_const_Calls */
484 * optimize function calls by handling const functions
486 void optimize_funccalls(int force_run)
489 unsigned num_const = 0;
490 unsigned num_pure = 0;
492 if (! get_opt_function_call())
495 /* prepare: mark all graphs as not analyzed */
496 n = get_irp_n_irgs();
497 for (i = n - 1; i >= 0; --i)
498 MARK_IRG(get_irp_irg(i));
500 /* first step: detect, which functions are const, i.e. do NOT touch any memory */
501 for (i = n - 1; i >= 0; --i) {
502 ir_graph *irg = get_irp_irg(i);
503 unsigned mode = is_pure_function(irg);
505 if (mode & mtp_property_const)
507 else if (mode & mtp_property_pure)
511 if (force_run || num_const > 0) {
514 handle_const_Calls(&ctx);
515 if (get_firm_verbosity()) {
516 printf("Detected %d graphs without side effects.\n", num_const);
517 printf("Optimizes %d(SymConst) + %d(Sel) calls to const/pure functions.\n",
518 ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
522 if (get_firm_verbosity()) {
523 printf("No graphs without side effects detected\n");
526 } /* optimize_funccalls */