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 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 set_irn_link(call, NULL);
48 ptr = get_Call_ptr(call);
49 if (get_irn_op(ptr) == op_SymConst && get_SymConst_kind(ptr) == symconst_addr_ent) {
50 ent = get_SymConst_entity(ptr);
52 mode = get_entity_additional_properties(ent);
53 if (mode & (mtp_property_const|mtp_property_pure) == 0)
55 ++ctx->n_calls_removed_SymConst;
57 else if (is_Sel(ptr) &&
58 get_irg_callee_info_state(current_ir_graph) == irg_callee_info_consistent) {
59 /* If all possible callees are const functions, we can remove the memory edge. */
60 int i, n_callees = get_Call_n_callees(call);
62 /* This is kind of strange: dying code or a Call that will raise an exception
63 when executed as there is no implementation to call. So better not
67 /* note that const function are a subset of pure ones */
68 mode = mtp_property_const | mtp_property_pure;
69 for (i = 0; i < n_callees; ++i) {
70 ent = get_Call_callee(call, i);
71 if (ent == unknown_entity) {
72 /* we don't know which entity is called here */
75 mode &= get_entity_additional_properties(ent);
79 ++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;
90 set_irn_link(call, ctx->const_call_list);
91 ctx->const_call_list = call;
94 else if (get_irn_op(node) == op_Proj) {
96 * Collect all memory and exception Proj's from
99 call = get_Proj_pred(node);
100 if (get_irn_op(call) != op_Call)
103 switch (get_Proj_proj(node)) {
104 case pn_Call_M_regular:
105 case pn_Call_X_except:
106 case pn_Call_M_except:
107 set_irn_link(node, ctx->proj_list);
108 ctx->proj_list = node;
114 } /* rem_mem_from_const_fkt_calls */
117 * Fix the list of collected Calls.
119 * @param irg the graph that contained calls to pure functions
120 * @param call_list the list of all call sites of pure functions
121 * @param proj_list the list of all memory/exception Projs of this call sites
123 static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
124 ir_node *call, *next, *mem, *proj;
127 /* fix all calls by removing it's memory input */
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 analyses 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 /* finally 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);
163 switch (get_Proj_proj(proj)) {
164 case pn_Call_M_regular: {
167 case pn_Call_X_except:
168 case pn_Call_M_except:
170 exchange(proj, get_irg_bad(irg));
177 /* changes were done ... */
178 set_irg_outs_inconsistent(irg);
179 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
182 /* ... including exception edges */
183 set_irg_doms_inconsistent(irg);
185 } /* fix_call_list */
189 * Check if a graph represents a const function.
191 * @param irg the graph
193 static int is_const_function(ir_graph *irg)
195 ir_node *end, *endbl;
198 if (get_irg_additional_properties(irg) & mtp_property_const) {
199 /* already marked as a const function */
203 end = get_irg_end(irg);
204 endbl = get_nodes_block(end);
207 /* visit every Return */
208 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
209 ir_node *node = get_Block_cfgpred(endbl, j);
210 ir_op *op = get_irn_op(node);
213 /* Bad nodes usually do NOT produce anything, so it's ok */
217 if (op == op_Return) {
218 mem = get_Return_mem(node);
220 /* Bad nodes usually do NOT produce anything, so it's ok */
224 change = mem != get_irg_initial_mem(irg);
229 /* exception found */
236 /* check, if a keep-alive exists */
237 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
238 ir_node *mem = get_End_keepalive(end, j);
240 if (mode_M != get_irn_mode(mem))
243 change = mem != get_irg_initial_mem(irg);
250 /* no memory changes found, it's a const function */
251 set_irg_additional_property(irg, mtp_property_const);
255 } /* is_const_function */
261 #define UNMARK_IRG(irg) set_irg_link((irg), NULL)
262 #define MARK_IRG(irg) set_irg_link((irg), MARK)
263 #define IS_IRG_MARKED(irg) (get_irg_link(irg) == MARK)
266 static int is_pure_function(ir_graph *irg);
268 #define UMAX(a,b) (a) > (b) ? (a) : (b)
271 * Follow the memory chain starting at node and determine
274 * @return mtp_property_const if only calls of const functions are detected
275 * mtp_property_pure if only Loads and const/pure
279 static unsigned _follow_mem(ir_node *node) {
280 unsigned m, mode = mtp_property_const;
285 if (irn_visited(node))
288 mark_irn_visited(node);
290 switch (get_irn_opcode(node)) {
292 node = get_Proj_pred(node);
301 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
302 mode &= _follow_mem(get_irn_n(node, i));
307 /* Beware volatile Loads are NOT allowed in pure functions */
308 if (get_Load_volatility(node) == volatility_is_volatile)
310 mode = mtp_property_pure;
311 node = get_Load_mem(node);
315 /* a call is only tolerable if its either constant or pure */
316 ptr = get_Call_ptr(node);
317 if (get_irn_op(ptr) == op_SymConst &&
318 get_SymConst_kind(ptr) == symconst_addr_ent) {
319 entity *ent = get_SymConst_entity(ptr);
320 ir_graph *irg = get_entity_irg(ent);
322 if (irg == current_ir_graph) {
323 /* A recursive call. The did not mode depend on this call */
325 else if (irg == NULL) {
326 m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
329 mode = UMAX(mode, m);
331 else if (irg != NULL) {
332 /* we have a graph. Check if it is already analyzed */
333 if (IS_IRG_MARKED(irg))
334 (void)is_pure_function(irg);
336 m = get_irg_additional_properties(irg) & (mtp_property_const|mtp_property_pure);
339 mode = UMAX(mode, m);
344 node = get_Call_mem(node);
354 * Follow the memory chain starting at node and determine
357 * @return mtp_property_const if only calls of const functions are detected
358 * mtp_property_pure if only Loads and const/pure
362 static unsigned follow_mem(ir_graph *irg, ir_node *node, unsigned mode) {
365 inc_irg_visited(irg);
366 /* mark the initial mem: recursion stops here */
367 mark_irn_visited(get_irg_initial_mem(irg));
368 m = _follow_mem(node);
371 return UMAX(mode, m);
375 * Check if a graph represents a pure function.
377 * @param irg the graph
379 static int is_pure_function(ir_graph *irg) {
380 ir_node *end, *endbl;
382 unsigned mode = get_irg_additional_properties(irg);
383 ir_graph *rem = current_ir_graph;
385 if (mode & mtp_property_const) {
386 /* already marked as a const function */
387 return mtp_property_const;
389 if (mode & mtp_property_pure) {
390 /* already marked as a pure function */
391 return mtp_property_const;
394 if (! IS_IRG_MARKED(irg))
398 end = get_irg_end(irg);
399 endbl = get_nodes_block(end);
400 mode = mtp_property_const;
402 current_ir_graph = irg;
404 /* visit every Return */
405 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
406 ir_node *node = get_Block_cfgpred(endbl, j);
407 ir_op *op = get_irn_op(node);
410 /* Bad nodes usually do NOT produce anything, so it's ok */
414 if (op == op_Return) {
415 mem = get_Return_mem(node);
417 /* Bad nodes usually do NOT produce anything, so it's ok */
421 if (mem != get_irg_initial_mem(irg))
422 mode = follow_mem(irg, mem, mode);
425 /* exception found. */
426 mode = follow_mem(irg, mem, mode);
434 /* check, if a keep-alive exists */
435 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
436 ir_node *mem = get_End_keepalive(end, j);
438 if (mode_M != get_irn_mode(mem))
441 mode = follow_mem(irg, mem, mode);
448 set_irg_additional_property(irg, mode);
449 current_ir_graph = rem;
451 } /* is_pure_function */
454 * Handle calls to const functions.
456 static void handle_const_Calls(env_t *ctx)
460 ctx->n_calls_removed_SymConst = 0;
461 ctx->n_calls_removed_Sel = 0;
463 /* all calls of const functions can be transformed */
464 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
465 ir_graph *irg = get_irp_irg(i);
467 ctx->const_call_list = NULL;
468 ctx->pure_call_list = NULL;
469 ctx->proj_list = NULL;
470 irg_walk_graph(irg, NULL, collect_calls, ctx);
472 if (ctx->const_call_list)
473 fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
475 } /* handle_const_Calls */
478 * optimize function calls by handling const functions
480 void optimize_funccalls(int force_run)
483 unsigned num_const = 0;
484 unsigned num_pure = 0;
486 if (! get_opt_function_call())
489 /* prepare: mark all graphs as not analyzed */
490 n = get_irp_n_irgs();
491 for (i = n - 1; i >= 0; --i)
492 MARK_IRG(get_irp_irg(i));
494 /* first step: detect, which functions are const, i.e. do NOT touch any memory */
495 for (i = n - 1; i >= 0; --i) {
496 ir_graph *irg = get_irp_irg(i);
497 unsigned mode = is_pure_function(irg);
499 if (mode & mtp_property_const)
501 else if (mode & mtp_property_pure)
505 if (force_run || num_const > 0) {
508 handle_const_Calls(&ctx);
509 if (get_firm_verbosity()) {
510 printf("Detected %d graphs without side effects.\n", num_const);
511 printf("Optimizes %d(SymConst) + %d(Sel) calls to const/pure functions.\n",
512 ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
516 if (get_firm_verbosity()) {
517 printf("No graphs without side effects detected\n");
520 } /* optimize_funccalls */