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 "funccall_t.h"
33 #include "irgraph_t.h"
37 #include "dbginfo_t.h"
43 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
46 * The walker environment for rem_mem_from_const_fkt_calls
48 typedef struct _env_t {
49 unsigned n_calls_SymConst;
51 ir_node *const_call_list; /**< The list of all const function calls that will be changed. */
52 ir_node *pure_call_list; /**< The list of all pure function calls that will be changed. */
53 ir_node *proj_list; /**< The list of all potential Proj nodes that must be fixed. */
57 * Collect all calls to const and pure functions
58 * to lists. Collect all Proj(Call) nodes into a Proj list.
60 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_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
91 /* note that const function are a subset of pure ones */
92 mode = mtp_property_const | mtp_property_pure;
93 for (i = 0; i < n_callees; ++i) {
94 ent = get_Call_callee(call, i);
95 if (ent == unknown_entity) {
96 /* we don't know which entity is called here */
99 mode &= get_entity_additional_properties(ent);
107 /* ok, if we get here we found a call to a const or a pure function */
108 if (mode & mtp_property_pure) {
109 set_irn_link(call, ctx->pure_call_list);
110 ctx->pure_call_list = call;
112 set_irn_link(call, ctx->const_call_list);
113 ctx->const_call_list = call;
115 } else if (is_Proj(node)) {
117 * Collect all memory and exception Proj's from
120 call = get_Proj_pred(node);
124 /* collect the Proj's in the Proj list */
125 switch (get_Proj_proj(node)) {
126 case pn_Call_M_regular:
127 case pn_Call_X_except:
128 case pn_Call_M_except:
129 set_irn_link(node, ctx->proj_list);
130 ctx->proj_list = node;
136 } /* collect_calls */
139 * Fix the list of collected Calls.
141 * @param irg the graph that contained calls to pure functions
142 * @param call_list the list of all call sites of const functions
143 * @param proj_list the list of all memory/exception Proj's of this call sites
145 static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
146 ir_node *call, *next, *mem, *proj;
148 ir_graph *rem = current_ir_graph;
150 current_ir_graph = irg;
152 /* First step: fix all calls by removing it's memory input.
153 It's original memory input is preserved in their link fields. */
154 for (call = call_list; call; call = next) {
155 next = get_irn_link(call);
156 mem = get_Call_mem(call);
158 set_irn_link(call, mem);
159 set_Call_mem(call, get_irg_no_mem(irg));
162 * Sorrily we cannot simply set the node to 'float'.
163 * There is a reason for that:
165 * - The call might be inside a loop/if that is NOT entered
166 * and calls a endless function. Setting the call to float
167 * would allow to move it out from the loop/if causing this
168 * function be called even if the loop/if is not entered ...
170 * This could be fixed using post-dominators for calls and Pin nodes
171 * but need some more analyzes to ensure that a call that potential
172 * never returns is not executed before some code that generates
173 * observable states...
176 /* finally, this call can float
177 set_irn_pinned(call, op_pin_state_floats); */
178 hook_func_call(irg, call);
181 /* Second step: fix all Proj's */
182 for (proj = proj_list; proj; proj = next) {
183 next = get_irn_link(proj);
184 call = get_Proj_pred(proj);
185 mem = get_irn_link(call);
187 /* beware of calls in the pure call list */
188 if (! mem || get_irn_op(mem) == op_Call)
190 assert(get_irn_mode(mem) == mode_M);
192 switch (get_Proj_proj(proj)) {
193 case pn_Call_M_regular: {
194 /* in dead code there might be cycles where proj == mem */
198 case pn_Call_X_except:
199 case pn_Call_M_except:
201 exchange(proj, get_irg_bad(irg));
208 /* changes were done ... */
209 set_irg_outs_inconsistent(irg);
210 set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
213 /* ... including exception edges */
214 set_irg_doms_inconsistent(irg);
216 current_ir_graph = rem;
217 } /* fix_call_list */
223 #define UNMARK_IRG(irg) set_irg_link((irg), NULL)
224 #define MARK_IRG(irg) set_irg_link((irg), MARK)
225 #define IS_IRG_MARKED(irg) (get_irg_link(irg) == MARK)
228 static unsigned check_const_or_pure_function(ir_graph *irg);
230 #define UMAX(a,b) (a) > (b) ? (a) : (b)
233 * Follow the memory chain starting at node and determine
236 * @return mtp_property_const if only calls of const functions are detected
237 * mtp_property_pure if only Loads and const/pure
241 static unsigned _follow_mem(ir_node *node) {
242 unsigned m, mode = mtp_property_const;
247 if (irn_visited(node))
250 mark_irn_visited(node);
252 switch (get_irn_opcode(node)) {
254 node = get_Proj_pred(node);
263 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
264 mode &= _follow_mem(get_irn_n(node, i));
269 /* Beware volatile Loads are NOT allowed in pure functions */
270 if (get_Load_volatility(node) == volatility_is_volatile)
272 mode = mtp_property_pure;
273 node = get_Load_mem(node);
277 /* a call is only tolerable if its either constant or pure */
278 ptr = get_Call_ptr(node);
279 if (get_irn_op(ptr) == op_SymConst &&
280 get_SymConst_kind(ptr) == symconst_addr_ent) {
281 ir_entity *ent = get_SymConst_entity(ptr);
282 ir_graph *irg = get_entity_irg(ent);
284 if (irg == current_ir_graph) {
285 /* A recursive call. The did not mode depend on this call */
286 } else if (irg == NULL) {
287 m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
290 mode = UMAX(mode, m);
291 } else if (irg != NULL) {
292 /* we have a graph. Check if it is already analyzed */
293 if (IS_IRG_MARKED(irg))
294 (void)check_const_or_pure_function(irg);
296 m = get_irg_additional_properties(irg) & (mtp_property_const|mtp_property_pure);
299 mode = UMAX(mode, m);
303 node = get_Call_mem(node);
313 * Follow the memory chain starting at node and determine
316 * @return mtp_property_const if only calls of const functions are detected
317 * mtp_property_pure if only Loads and const/pure
321 static unsigned follow_mem(ir_graph *irg, ir_node *node, unsigned mode) {
324 inc_irg_visited(irg);
325 /* mark the initial mem: recursion stops here */
326 mark_irn_visited(get_irg_initial_mem(irg));
327 m = _follow_mem(node);
330 return UMAX(mode, m);
334 * Check if a graph represents a const function.
336 * @param irg the graph
338 static unsigned check_const_or_pure_function(ir_graph *irg) {
339 ir_node *end, *endbl;
341 unsigned mode = get_irg_additional_properties(irg);
342 ir_graph *rem = current_ir_graph;
344 if (mode & mtp_property_const) {
345 /* already marked as a const function */
346 return mtp_property_const;
348 if (mode & mtp_property_pure) {
349 /* already marked as a pure function */
350 return mtp_property_pure;
353 if (! IS_IRG_MARKED(irg))
357 end = get_irg_end(irg);
358 endbl = get_nodes_block(end);
359 mode = mtp_property_const;
361 current_ir_graph = irg;
363 /* visit every Return */
364 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
365 ir_node *node = get_Block_cfgpred(endbl, j);
366 ir_op *op = get_irn_op(node);
369 /* Bad nodes usually do NOT produce anything, so it's ok */
373 if (op == op_Return) {
374 mem = get_Return_mem(node);
376 /* Bad nodes usually do NOT produce anything, so it's ok */
380 if (mem != get_irg_initial_mem(irg))
381 mode = follow_mem(irg, mem, mode);
383 /* exception found. */
384 mode = follow_mem(irg, node, mode);
392 /* check, if a keep-alive exists */
393 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
394 ir_node *mem = get_End_keepalive(end, j);
396 if (mode_M != get_irn_mode(mem))
399 mode = follow_mem(irg, mem, mode);
406 set_irg_additional_property(irg, mode);
407 current_ir_graph = rem;
409 } /* check_const_or_pure_function */
412 * Handle calls to const functions.
414 static void handle_const_Calls(env_t *ctx) {
417 ctx->n_calls_SymConst = 0;
418 ctx->n_calls_Sel = 0;
420 /* all calls of const functions can be transformed */
421 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
422 ir_graph *irg = get_irp_irg(i);
424 ctx->const_call_list = NULL;
425 ctx->pure_call_list = NULL;
426 ctx->proj_list = NULL;
427 irg_walk_graph(irg, NULL, collect_calls, ctx);
429 if (ctx->const_call_list)
430 fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
432 } /* handle_const_Calls */
435 * optimize function calls by handling const functions
437 void optimize_funccalls(int force_run)
440 unsigned num_const = 0;
441 unsigned num_pure = 0;
443 /* prepare: mark all graphs as not analyzed */
444 n = get_irp_n_irgs();
445 for (i = n - 1; i >= 0; --i)
446 MARK_IRG(get_irp_irg(i));
448 /* first step: detect, which functions are const, i.e. do NOT touch any memory */
449 DBG((dbg, LEVEL_2, "Detecting const and pure properties ...\n"));
450 for (i = n - 1; i >= 0; --i) {
451 ir_graph *irg = get_irp_irg(i);
452 unsigned mode = check_const_or_pure_function(irg);
454 if (mode & mtp_property_const) {
456 DBG((dbg, LEVEL_2, "%+F has the const property\n", irg));
457 } else if (mode & mtp_property_pure) {
459 DBG((dbg, LEVEL_2, "%+F has the pure property\n", irg));
463 if (force_run || num_const > 0) {
466 handle_const_Calls(&ctx);
467 if (get_firm_verbosity()) {
468 DBG((dbg, LEVEL_1, "Detected %u const graphs, %u pure graphs.\n", num_const, num_pure));
469 DBG((dbg, LEVEL_1, "Optimizes %u(SymConst) + %u(Sel) calls to const functions.\n",
470 ctx.n_calls_SymConst, ctx.n_calls_Sel));
473 DBG((dbg, LEVEL_1, "No graphs without side effects detected\n"));
475 } /* optimize_funccalls */
477 /* initialize the funccall optimization */
478 void firm_init_funccalls(void)
480 FIRM_DBG_REGISTER(dbg, "firm.opt.funccalls");
481 } /* firm_init_funccalls */