2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * File name: ir/opt/funccall.c
23 * Purpose: optimization of function calls
24 * Author: Michael Beck
27 * Copyright: (c) 1998-2006 Universität Karlsruhe
34 #include "irgraph_t.h"
38 #include "dbginfo_t.h"
45 * The walker environment for rem_mem_from_const_fkt_calls
47 typedef struct _env_t {
48 int n_calls_removed_SymConst;
49 int n_calls_removed_Sel;
50 ir_node *const_call_list; /**< The list of all const function calls that will be changed. */
51 ir_node *pure_call_list; /**< The list of all pure function calls that will be changed. */
52 ir_node *proj_list; /**< The list of all potential Proj nodes that must be fixed. */
56 * Collect all calls to const and pure functions
57 * to lists. Collect all Proj(Call) nodes into a Proj list.
59 static void collect_calls(ir_node *node, void *env)
69 /* set the link to NULL for all non-const/pure calls */
70 set_irn_link(call, NULL);
71 ptr = get_Call_ptr(call);
72 if (is_SymConst(ptr) && get_SymConst_kind(ptr) == symconst_addr_ent) {
73 ent = get_SymConst_entity(ptr);
75 mode = get_entity_additional_properties(ent);
76 if ((mode & (mtp_property_const|mtp_property_pure)) == 0)
78 ++ctx->n_calls_removed_SymConst;
79 } else if (get_opt_closed_world() &&
81 get_irg_callee_info_state(current_ir_graph) == irg_callee_info_consistent) {
82 /* If all possible callees are const functions, we can remove the memory edge. */
83 int i, n_callees = get_Call_n_callees(call);
85 /* This is kind of strange: dying code or a Call that will raise an exception
86 when executed as there is no implementation to call. So better not
90 /* note that const function are a subset of pure ones */
91 mode = mtp_property_const | mtp_property_pure;
92 for (i = 0; i < n_callees; ++i) {
93 ent = get_Call_callee(call, i);
94 if (ent == unknown_entity) {
95 /* we don't know which entity is called here */
98 mode &= get_entity_additional_properties(ent);
102 ++ctx->n_calls_removed_Sel;
106 /* ok, if we get here we found a call to a const or a pure function */
107 if (mode & mtp_property_pure) {
108 set_irn_link(call, ctx->pure_call_list);
109 ctx->pure_call_list = call;
111 set_irn_link(call, ctx->const_call_list);
112 ctx->const_call_list = call;
114 } else if (is_Proj(node)) {
116 * Collect all memory and exception Proj's from
119 call = get_Proj_pred(node);
123 /* collect the Proj's in the Proj list */
124 switch (get_Proj_proj(node)) {
125 case pn_Call_M_regular:
126 case pn_Call_X_except:
127 case pn_Call_M_except:
128 set_irn_link(node, ctx->proj_list);
129 ctx->proj_list = node;
135 } /* collect_calls */
138 * Fix the list of collected Calls.
140 * @param irg the graph that contained calls to pure functions
141 * @param call_list the list of all call sites of const functions
142 * @param proj_list the list of all memory/exception Proj's of this call sites
144 static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
145 ir_node *call, *next, *mem, *proj;
147 ir_graph *rem = current_ir_graph;
149 current_ir_graph = irg;
151 /* First step: fix all calls by removing it's memory input.
152 It's original memory input is preserved in their link fields. */
153 for (call = call_list; call; call = next) {
154 next = get_irn_link(call);
155 mem = get_Call_mem(call);
157 set_irn_link(call, mem);
158 set_Call_mem(call, get_irg_no_mem(irg));
161 * Sorrily we cannot simply set the node to 'float'.
162 * There is a reason for that:
164 * - The call might be inside a loop/if that is NOT entered
165 * and calls a endless function. Setting the call to float
166 * would allow to move it out from the loop/if causing this
167 * function be called even if the loop/if is not entered ...
169 * This could be fixed using post-dominators for calls and Pin nodes
170 * but need some more analyzes to ensure that a call that potential
171 * never returns is not executed before some code that generates
172 * observable states...
175 /* finally, this call can float
176 set_irn_pinned(call, op_pin_state_floats); */
177 hook_func_call(irg, call);
180 /* Second step: fix all Proj's */
181 for (proj = proj_list; proj; proj = next) {
182 next = get_irn_link(proj);
183 call = get_Proj_pred(proj);
184 mem = get_irn_link(call);
186 /* beware of calls in the pure call list */
187 if (! mem || get_irn_op(mem) == op_Call)
189 assert(get_irn_mode(mem) == mode_M);
191 switch (get_Proj_proj(proj)) {
192 case pn_Call_M_regular: {
193 /* in dead code there might be cycles where proj == mem */
197 case pn_Call_X_except:
198 case pn_Call_M_except:
200 exchange(proj, get_irg_bad(irg));
207 /* changes were done ... */
208 set_irg_outs_inconsistent(irg);
209 set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
212 /* ... including exception edges */
213 set_irg_doms_inconsistent(irg);
215 current_ir_graph = rem;
216 } /* fix_call_list */
220 * Check if a graph represents a const function.
222 * @param irg the graph
224 static int is_const_function(ir_graph *irg)
226 ir_node *end, *endbl;
229 if (get_irg_additional_properties(irg) & mtp_property_const) {
230 /* already marked as a const function */
234 end = get_irg_end(irg);
235 endbl = get_nodes_block(end);
238 /* visit every Return */
239 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
240 ir_node *node = get_Block_cfgpred(endbl, j);
241 ir_op *op = get_irn_op(node);
244 /* Bad nodes usually do NOT produce anything, so it's ok */
248 if (op == op_Return) {
249 mem = get_Return_mem(node);
251 /* Bad nodes usually do NOT produce anything, so it's ok */
255 change = mem != get_irg_initial_mem(irg);
260 /* exception found */
267 /* check, if a keep-alive exists */
268 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
269 ir_node *mem = get_End_keepalive(end, j);
271 if (mode_M != get_irn_mode(mem))
274 change = mem != get_irg_initial_mem(irg);
281 /* no memory changes found, it's a const function */
282 set_irg_additional_property(irg, mtp_property_const);
286 } /* is_const_function */
293 #define UNMARK_IRG(irg) set_irg_link((irg), NULL)
294 #define MARK_IRG(irg) set_irg_link((irg), MARK)
295 #define IS_IRG_MARKED(irg) (get_irg_link(irg) == MARK)
298 static int is_pure_function(ir_graph *irg);
300 #define UMAX(a,b) (a) > (b) ? (a) : (b)
303 * Follow the memory chain starting at node and determine
306 * @return mtp_property_const if only calls of const functions are detected
307 * mtp_property_pure if only Loads and const/pure
311 static unsigned _follow_mem(ir_node *node) {
312 unsigned m, mode = mtp_property_const;
317 if (irn_visited(node))
320 mark_irn_visited(node);
322 switch (get_irn_opcode(node)) {
324 node = get_Proj_pred(node);
333 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
334 mode &= _follow_mem(get_irn_n(node, i));
339 /* Beware volatile Loads are NOT allowed in pure functions */
340 if (get_Load_volatility(node) == volatility_is_volatile)
342 mode = mtp_property_pure;
343 node = get_Load_mem(node);
347 /* a call is only tolerable if its either constant or pure */
348 ptr = get_Call_ptr(node);
349 if (get_irn_op(ptr) == op_SymConst &&
350 get_SymConst_kind(ptr) == symconst_addr_ent) {
351 ir_entity *ent = get_SymConst_entity(ptr);
352 ir_graph *irg = get_entity_irg(ent);
354 if (irg == current_ir_graph) {
355 /* A recursive call. The did not mode depend on this call */
357 else if (irg == NULL) {
358 m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
361 mode = UMAX(mode, m);
363 else if (irg != NULL) {
364 /* we have a graph. Check if it is already analyzed */
365 if (IS_IRG_MARKED(irg))
366 (void)is_pure_function(irg);
368 m = get_irg_additional_properties(irg) & (mtp_property_const|mtp_property_pure);
371 mode = UMAX(mode, m);
376 node = get_Call_mem(node);
386 * Follow the memory chain starting at node and determine
389 * @return mtp_property_const if only calls of const functions are detected
390 * mtp_property_pure if only Loads and const/pure
394 static unsigned follow_mem(ir_graph *irg, ir_node *node, unsigned mode) {
397 inc_irg_visited(irg);
398 /* mark the initial mem: recursion stops here */
399 mark_irn_visited(get_irg_initial_mem(irg));
400 m = _follow_mem(node);
403 return UMAX(mode, m);
407 * Check if a graph represents a pure function.
409 * @param irg the graph
411 static int is_pure_function(ir_graph *irg) {
412 ir_node *end, *endbl;
414 unsigned mode = get_irg_additional_properties(irg);
415 ir_graph *rem = current_ir_graph;
417 if (mode & mtp_property_const) {
418 /* already marked as a const function */
419 return mtp_property_const;
421 if (mode & mtp_property_pure) {
422 /* already marked as a pure function */
423 return mtp_property_const;
426 if (! IS_IRG_MARKED(irg))
430 end = get_irg_end(irg);
431 endbl = get_nodes_block(end);
432 mode = mtp_property_const;
434 current_ir_graph = irg;
436 /* visit every Return */
437 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
438 ir_node *node = get_Block_cfgpred(endbl, j);
439 ir_op *op = get_irn_op(node);
442 /* Bad nodes usually do NOT produce anything, so it's ok */
446 if (op == op_Return) {
447 mem = get_Return_mem(node);
449 /* Bad nodes usually do NOT produce anything, so it's ok */
453 if (mem != get_irg_initial_mem(irg))
454 mode = follow_mem(irg, mem, mode);
457 /* exception found. */
458 mode = follow_mem(irg, node, mode);
466 /* check, if a keep-alive exists */
467 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
468 ir_node *mem = get_End_keepalive(end, j);
470 if (mode_M != get_irn_mode(mem))
473 mode = follow_mem(irg, mem, mode);
480 set_irg_additional_property(irg, mode);
481 current_ir_graph = rem;
483 } /* is_pure_function */
486 * Handle calls to const functions.
488 static void handle_const_Calls(env_t *ctx)
492 ctx->n_calls_removed_SymConst = 0;
493 ctx->n_calls_removed_Sel = 0;
495 /* all calls of const functions can be transformed */
496 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
497 ir_graph *irg = get_irp_irg(i);
499 ctx->const_call_list = NULL;
500 ctx->pure_call_list = NULL;
501 ctx->proj_list = NULL;
502 irg_walk_graph(irg, NULL, collect_calls, ctx);
504 if (ctx->const_call_list)
505 fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
507 } /* handle_const_Calls */
510 * optimize function calls by handling const functions
512 void optimize_funccalls(int force_run)
515 unsigned num_const = 0;
516 unsigned num_pure = 0;
518 if (! get_opt_function_call())
521 /* prepare: mark all graphs as not analyzed */
522 n = get_irp_n_irgs();
523 for (i = n - 1; i >= 0; --i)
524 MARK_IRG(get_irp_irg(i));
526 /* first step: detect, which functions are const, i.e. do NOT touch any memory */
527 for (i = n - 1; i >= 0; --i) {
528 ir_graph *irg = get_irp_irg(i);
529 unsigned mode = is_pure_function(irg);
531 if (mode & mtp_property_const)
533 else if (mode & mtp_property_pure)
537 if (force_run || num_const > 0) {
540 handle_const_Calls(&ctx);
541 if (get_firm_verbosity()) {
542 printf("Detected %d graphs without side effects.\n", num_const);
543 printf("Optimizes %d(SymConst) + %d(Sel) calls to const/pure functions.\n",
544 ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
548 if (get_firm_verbosity()) {
549 printf("No graphs without side effects detected\n");
552 } /* optimize_funccalls */