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 * @brief Optimization of function calls.
23 * @author Michael Beck
30 #include "iroptimize.h"
33 #include "irgraph_t.h"
37 #include "dbginfo_t.h"
43 * The walker environment for rem_mem_from_const_fkt_calls
45 typedef struct _env_t {
46 int n_calls_removed_SymConst;
47 int n_calls_removed_Sel;
48 ir_node *const_call_list; /**< The list of all const function calls that will be changed. */
49 ir_node *pure_call_list; /**< The list of all pure function calls that will be changed. */
50 ir_node *proj_list; /**< The list of all potential Proj nodes that must be fixed. */
54 * Collect all calls to const and pure functions
55 * to lists. Collect all Proj(Call) nodes into a Proj list.
57 static void collect_calls(ir_node *node, void *env)
67 /* set the link to NULL for all non-const/pure calls */
68 set_irn_link(call, NULL);
69 ptr = get_Call_ptr(call);
70 if (is_SymConst(ptr) && get_SymConst_kind(ptr) == symconst_addr_ent) {
71 ent = get_SymConst_entity(ptr);
73 mode = get_entity_additional_properties(ent);
74 if ((mode & (mtp_property_const|mtp_property_pure)) == 0)
76 ++ctx->n_calls_removed_SymConst;
77 } else if (get_opt_closed_world() &&
79 get_irg_callee_info_state(current_ir_graph) == irg_callee_info_consistent) {
80 /* If all possible callees are const functions, we can remove the memory edge. */
81 int i, n_callees = get_Call_n_callees(call);
83 /* This is kind of strange: dying code or a Call that will raise an exception
84 when executed as there is no implementation to call. So better not
88 /* note that const function are a subset of pure ones */
89 mode = mtp_property_const | mtp_property_pure;
90 for (i = 0; i < n_callees; ++i) {
91 ent = get_Call_callee(call, i);
92 if (ent == unknown_entity) {
93 /* we don't know which entity is called here */
96 mode &= get_entity_additional_properties(ent);
100 ++ctx->n_calls_removed_Sel;
104 /* ok, if we get here we found a call to a const or a pure function */
105 if (mode & mtp_property_pure) {
106 set_irn_link(call, ctx->pure_call_list);
107 ctx->pure_call_list = call;
109 set_irn_link(call, ctx->const_call_list);
110 ctx->const_call_list = call;
112 } else if (is_Proj(node)) {
114 * Collect all memory and exception Proj's from
117 call = get_Proj_pred(node);
121 /* collect the Proj's in the Proj list */
122 switch (get_Proj_proj(node)) {
123 case pn_Call_M_regular:
124 case pn_Call_X_except:
125 case pn_Call_M_except:
126 set_irn_link(node, ctx->proj_list);
127 ctx->proj_list = node;
133 } /* collect_calls */
136 * Fix the list of collected Calls.
138 * @param irg the graph that contained calls to pure functions
139 * @param call_list the list of all call sites of const functions
140 * @param proj_list the list of all memory/exception Proj's of this call sites
142 static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
143 ir_node *call, *next, *mem, *proj;
145 ir_graph *rem = current_ir_graph;
147 current_ir_graph = irg;
149 /* First step: fix all calls by removing it's memory input.
150 It's original memory input is preserved in their link fields. */
151 for (call = call_list; call; call = next) {
152 next = get_irn_link(call);
153 mem = get_Call_mem(call);
155 set_irn_link(call, mem);
156 set_Call_mem(call, get_irg_no_mem(irg));
159 * Sorrily we cannot simply set the node to 'float'.
160 * There is a reason for that:
162 * - The call might be inside a loop/if that is NOT entered
163 * and calls a endless function. Setting the call to float
164 * would allow to move it out from the loop/if causing this
165 * function be called even if the loop/if is not entered ...
167 * This could be fixed using post-dominators for calls and Pin nodes
168 * but need some more analyzes to ensure that a call that potential
169 * never returns is not executed before some code that generates
170 * observable states...
173 /* finally, this call can float
174 set_irn_pinned(call, op_pin_state_floats); */
175 hook_func_call(irg, call);
178 /* Second step: fix all Proj's */
179 for (proj = proj_list; proj; proj = next) {
180 next = get_irn_link(proj);
181 call = get_Proj_pred(proj);
182 mem = get_irn_link(call);
184 /* beware of calls in the pure call list */
185 if (! mem || get_irn_op(mem) == op_Call)
187 assert(get_irn_mode(mem) == mode_M);
189 switch (get_Proj_proj(proj)) {
190 case pn_Call_M_regular: {
191 /* in dead code there might be cycles where proj == mem */
195 case pn_Call_X_except:
196 case pn_Call_M_except:
198 exchange(proj, get_irg_bad(irg));
205 /* changes were done ... */
206 set_irg_outs_inconsistent(irg);
207 set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
210 /* ... including exception edges */
211 set_irg_doms_inconsistent(irg);
213 current_ir_graph = rem;
214 } /* fix_call_list */
218 * Check if a graph represents a const function.
220 * @param irg the graph
222 static int is_const_function(ir_graph *irg)
224 ir_node *end, *endbl;
227 if (get_irg_additional_properties(irg) & mtp_property_const) {
228 /* already marked as a const function */
232 end = get_irg_end(irg);
233 endbl = get_nodes_block(end);
236 /* visit every Return */
237 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
238 ir_node *node = get_Block_cfgpred(endbl, j);
239 ir_op *op = get_irn_op(node);
242 /* Bad nodes usually do NOT produce anything, so it's ok */
246 if (op == op_Return) {
247 mem = get_Return_mem(node);
249 /* Bad nodes usually do NOT produce anything, so it's ok */
253 change = mem != get_irg_initial_mem(irg);
258 /* exception found */
265 /* check, if a keep-alive exists */
266 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
267 ir_node *mem = get_End_keepalive(end, j);
269 if (mode_M != get_irn_mode(mem))
272 change = mem != get_irg_initial_mem(irg);
279 /* no memory changes found, it's a const function */
280 set_irg_additional_property(irg, mtp_property_const);
284 } /* is_const_function */
291 #define UNMARK_IRG(irg) set_irg_link((irg), NULL)
292 #define MARK_IRG(irg) set_irg_link((irg), MARK)
293 #define IS_IRG_MARKED(irg) (get_irg_link(irg) == MARK)
296 static int is_pure_function(ir_graph *irg);
298 #define UMAX(a,b) (a) > (b) ? (a) : (b)
301 * Follow the memory chain starting at node and determine
304 * @return mtp_property_const if only calls of const functions are detected
305 * mtp_property_pure if only Loads and const/pure
309 static unsigned _follow_mem(ir_node *node) {
310 unsigned m, mode = mtp_property_const;
315 if (irn_visited(node))
318 mark_irn_visited(node);
320 switch (get_irn_opcode(node)) {
322 node = get_Proj_pred(node);
331 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
332 mode &= _follow_mem(get_irn_n(node, i));
337 /* Beware volatile Loads are NOT allowed in pure functions */
338 if (get_Load_volatility(node) == volatility_is_volatile)
340 mode = mtp_property_pure;
341 node = get_Load_mem(node);
345 /* a call is only tolerable if its either constant or pure */
346 ptr = get_Call_ptr(node);
347 if (get_irn_op(ptr) == op_SymConst &&
348 get_SymConst_kind(ptr) == symconst_addr_ent) {
349 ir_entity *ent = get_SymConst_entity(ptr);
350 ir_graph *irg = get_entity_irg(ent);
352 if (irg == current_ir_graph) {
353 /* A recursive call. The did not mode depend on this call */
355 else if (irg == NULL) {
356 m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
359 mode = UMAX(mode, m);
361 else if (irg != NULL) {
362 /* we have a graph. Check if it is already analyzed */
363 if (IS_IRG_MARKED(irg))
364 (void)is_pure_function(irg);
366 m = get_irg_additional_properties(irg) & (mtp_property_const|mtp_property_pure);
369 mode = UMAX(mode, m);
374 node = get_Call_mem(node);
384 * Follow the memory chain starting at node and determine
387 * @return mtp_property_const if only calls of const functions are detected
388 * mtp_property_pure if only Loads and const/pure
392 static unsigned follow_mem(ir_graph *irg, ir_node *node, unsigned mode) {
395 inc_irg_visited(irg);
396 /* mark the initial mem: recursion stops here */
397 mark_irn_visited(get_irg_initial_mem(irg));
398 m = _follow_mem(node);
401 return UMAX(mode, m);
405 * Check if a graph represents a pure function.
407 * @param irg the graph
409 static int is_pure_function(ir_graph *irg) {
410 ir_node *end, *endbl;
412 unsigned mode = get_irg_additional_properties(irg);
413 ir_graph *rem = current_ir_graph;
415 if (mode & mtp_property_const) {
416 /* already marked as a const function */
417 return mtp_property_const;
419 if (mode & mtp_property_pure) {
420 /* already marked as a pure function */
421 return mtp_property_const;
424 if (! IS_IRG_MARKED(irg))
428 end = get_irg_end(irg);
429 endbl = get_nodes_block(end);
430 mode = mtp_property_const;
432 current_ir_graph = irg;
434 /* visit every Return */
435 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
436 ir_node *node = get_Block_cfgpred(endbl, j);
437 ir_op *op = get_irn_op(node);
440 /* Bad nodes usually do NOT produce anything, so it's ok */
444 if (op == op_Return) {
445 mem = get_Return_mem(node);
447 /* Bad nodes usually do NOT produce anything, so it's ok */
451 if (mem != get_irg_initial_mem(irg))
452 mode = follow_mem(irg, mem, mode);
455 /* exception found. */
456 mode = follow_mem(irg, node, mode);
464 /* check, if a keep-alive exists */
465 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
466 ir_node *mem = get_End_keepalive(end, j);
468 if (mode_M != get_irn_mode(mem))
471 mode = follow_mem(irg, mem, mode);
478 set_irg_additional_property(irg, mode);
479 current_ir_graph = rem;
481 } /* is_pure_function */
484 * Handle calls to const functions.
486 static void handle_const_Calls(env_t *ctx)
490 ctx->n_calls_removed_SymConst = 0;
491 ctx->n_calls_removed_Sel = 0;
493 /* all calls of const functions can be transformed */
494 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
495 ir_graph *irg = get_irp_irg(i);
497 ctx->const_call_list = NULL;
498 ctx->pure_call_list = NULL;
499 ctx->proj_list = NULL;
500 irg_walk_graph(irg, NULL, collect_calls, ctx);
502 if (ctx->const_call_list)
503 fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
505 } /* handle_const_Calls */
508 * optimize function calls by handling const functions
510 void optimize_funccalls(int force_run)
513 unsigned num_const = 0;
514 unsigned num_pure = 0;
516 if (! get_opt_function_call())
519 /* prepare: mark all graphs as not analyzed */
520 n = get_irp_n_irgs();
521 for (i = n - 1; i >= 0; --i)
522 MARK_IRG(get_irp_irg(i));
524 /* first step: detect, which functions are const, i.e. do NOT touch any memory */
525 for (i = n - 1; i >= 0; --i) {
526 ir_graph *irg = get_irp_irg(i);
527 unsigned mode = is_pure_function(irg);
529 if (mode & mtp_property_const)
531 else if (mode & mtp_property_pure)
535 if (force_run || num_const > 0) {
538 handle_const_Calls(&ctx);
539 if (get_firm_verbosity()) {
540 printf("Detected %d graphs without side effects.\n", num_const);
541 printf("Optimizes %d(SymConst) + %d(Sel) calls to const/pure functions.\n",
542 ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
546 if (get_firm_verbosity()) {
547 printf("No graphs without side effects detected\n");
550 } /* optimize_funccalls */