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
31 #include "irgraph_t.h"
35 #include "dbginfo_t.h"
42 * The walker environment for rem_mem_from_const_fkt_calls
44 typedef struct _env_t {
45 int n_calls_removed_SymConst;
46 int n_calls_removed_Sel;
47 ir_node *const_call_list; /**< The list of all const function calls that will be changed. */
48 ir_node *pure_call_list; /**< The list of all pure function calls that will be changed. */
49 ir_node *proj_list; /**< The list of all potential Proj nodes that must be fixed. */
53 * Collect all calls to const and pure functions
54 * to lists. Collect all Proj(Call) nodes into a Proj list.
56 static void collect_calls(ir_node *node, void *env)
66 /* set the link to NULL for all non-const/pure calls */
67 set_irn_link(call, NULL);
68 ptr = get_Call_ptr(call);
69 if (is_SymConst(ptr) && get_SymConst_kind(ptr) == symconst_addr_ent) {
70 ent = get_SymConst_entity(ptr);
72 mode = get_entity_additional_properties(ent);
73 if ((mode & (mtp_property_const|mtp_property_pure)) == 0)
75 ++ctx->n_calls_removed_SymConst;
76 } else if (get_opt_closed_world() &&
78 get_irg_callee_info_state(current_ir_graph) == irg_callee_info_consistent) {
79 /* If all possible callees are const functions, we can remove the memory edge. */
80 int i, n_callees = get_Call_n_callees(call);
82 /* This is kind of strange: dying code or a Call that will raise an exception
83 when executed as there is no implementation to call. So better not
87 /* note that const function are a subset of pure ones */
88 mode = mtp_property_const | mtp_property_pure;
89 for (i = 0; i < n_callees; ++i) {
90 ent = get_Call_callee(call, i);
91 if (ent == unknown_entity) {
92 /* we don't know which entity is called here */
95 mode &= get_entity_additional_properties(ent);
99 ++ctx->n_calls_removed_Sel;
103 /* ok, if we get here we found a call to a const or a pure function */
104 if (mode & mtp_property_pure) {
105 set_irn_link(call, ctx->pure_call_list);
106 ctx->pure_call_list = call;
108 set_irn_link(call, ctx->const_call_list);
109 ctx->const_call_list = call;
111 } else if (is_Proj(node)) {
113 * Collect all memory and exception Proj's from
116 call = get_Proj_pred(node);
120 /* collect the Proj's in the Proj list */
121 switch (get_Proj_proj(node)) {
122 case pn_Call_M_regular:
123 case pn_Call_X_except:
124 case pn_Call_M_except:
125 set_irn_link(node, ctx->proj_list);
126 ctx->proj_list = node;
132 } /* collect_calls */
135 * Fix the list of collected Calls.
137 * @param irg the graph that contained calls to pure functions
138 * @param call_list the list of all call sites of const functions
139 * @param proj_list the list of all memory/exception Proj's of this call sites
141 static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
142 ir_node *call, *next, *mem, *proj;
144 ir_graph *rem = current_ir_graph;
146 current_ir_graph = irg;
148 /* First step: fix all calls by removing it's memory input.
149 It's original memory input is preserved in their link fields. */
150 for (call = call_list; call; call = next) {
151 next = get_irn_link(call);
152 mem = get_Call_mem(call);
154 set_irn_link(call, mem);
155 set_Call_mem(call, get_irg_no_mem(irg));
158 * Sorrily we cannot simply set the node to 'float'.
159 * There is a reason for that:
161 * - The call might be inside a loop/if that is NOT entered
162 * and calls a endless function. Setting the call to float
163 * would allow to move it out from the loop/if causing this
164 * function be called even if the loop/if is not entered ...
166 * This could be fixed using post-dominators for calls and Pin nodes
167 * but need some more analyzes to ensure that a call that potential
168 * never returns is not executed before some code that generates
169 * observable states...
172 /* finally, this call can float
173 set_irn_pinned(call, op_pin_state_floats); */
174 hook_func_call(irg, call);
177 /* Second step: fix all Proj's */
178 for (proj = proj_list; proj; proj = next) {
179 next = get_irn_link(proj);
180 call = get_Proj_pred(proj);
181 mem = get_irn_link(call);
183 /* beware of calls in the pure call list */
184 if (! mem || get_irn_op(mem) == op_Call)
186 assert(get_irn_mode(mem) == mode_M);
188 switch (get_Proj_proj(proj)) {
189 case pn_Call_M_regular: {
190 /* in dead code there might be cycles where proj == mem */
194 case pn_Call_X_except:
195 case pn_Call_M_except:
197 exchange(proj, get_irg_bad(irg));
204 /* changes were done ... */
205 set_irg_outs_inconsistent(irg);
206 set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
209 /* ... including exception edges */
210 set_irg_doms_inconsistent(irg);
212 current_ir_graph = rem;
213 } /* fix_call_list */
217 * Check if a graph represents a const function.
219 * @param irg the graph
221 static int is_const_function(ir_graph *irg)
223 ir_node *end, *endbl;
226 if (get_irg_additional_properties(irg) & mtp_property_const) {
227 /* already marked as a const function */
231 end = get_irg_end(irg);
232 endbl = get_nodes_block(end);
235 /* visit every Return */
236 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
237 ir_node *node = get_Block_cfgpred(endbl, j);
238 ir_op *op = get_irn_op(node);
241 /* Bad nodes usually do NOT produce anything, so it's ok */
245 if (op == op_Return) {
246 mem = get_Return_mem(node);
248 /* Bad nodes usually do NOT produce anything, so it's ok */
252 change = mem != get_irg_initial_mem(irg);
257 /* exception found */
264 /* check, if a keep-alive exists */
265 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
266 ir_node *mem = get_End_keepalive(end, j);
268 if (mode_M != get_irn_mode(mem))
271 change = mem != get_irg_initial_mem(irg);
278 /* no memory changes found, it's a const function */
279 set_irg_additional_property(irg, mtp_property_const);
283 } /* is_const_function */
290 #define UNMARK_IRG(irg) set_irg_link((irg), NULL)
291 #define MARK_IRG(irg) set_irg_link((irg), MARK)
292 #define IS_IRG_MARKED(irg) (get_irg_link(irg) == MARK)
295 static int is_pure_function(ir_graph *irg);
297 #define UMAX(a,b) (a) > (b) ? (a) : (b)
300 * Follow the memory chain starting at node and determine
303 * @return mtp_property_const if only calls of const functions are detected
304 * mtp_property_pure if only Loads and const/pure
308 static unsigned _follow_mem(ir_node *node) {
309 unsigned m, mode = mtp_property_const;
314 if (irn_visited(node))
317 mark_irn_visited(node);
319 switch (get_irn_opcode(node)) {
321 node = get_Proj_pred(node);
330 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
331 mode &= _follow_mem(get_irn_n(node, i));
336 /* Beware volatile Loads are NOT allowed in pure functions */
337 if (get_Load_volatility(node) == volatility_is_volatile)
339 mode = mtp_property_pure;
340 node = get_Load_mem(node);
344 /* a call is only tolerable if its either constant or pure */
345 ptr = get_Call_ptr(node);
346 if (get_irn_op(ptr) == op_SymConst &&
347 get_SymConst_kind(ptr) == symconst_addr_ent) {
348 ir_entity *ent = get_SymConst_entity(ptr);
349 ir_graph *irg = get_entity_irg(ent);
351 if (irg == current_ir_graph) {
352 /* A recursive call. The did not mode depend on this call */
354 else if (irg == NULL) {
355 m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
358 mode = UMAX(mode, m);
360 else if (irg != NULL) {
361 /* we have a graph. Check if it is already analyzed */
362 if (IS_IRG_MARKED(irg))
363 (void)is_pure_function(irg);
365 m = get_irg_additional_properties(irg) & (mtp_property_const|mtp_property_pure);
368 mode = UMAX(mode, m);
373 node = get_Call_mem(node);
383 * Follow the memory chain starting at node and determine
386 * @return mtp_property_const if only calls of const functions are detected
387 * mtp_property_pure if only Loads and const/pure
391 static unsigned follow_mem(ir_graph *irg, ir_node *node, unsigned mode) {
394 inc_irg_visited(irg);
395 /* mark the initial mem: recursion stops here */
396 mark_irn_visited(get_irg_initial_mem(irg));
397 m = _follow_mem(node);
400 return UMAX(mode, m);
404 * Check if a graph represents a pure function.
406 * @param irg the graph
408 static int is_pure_function(ir_graph *irg) {
409 ir_node *end, *endbl;
411 unsigned mode = get_irg_additional_properties(irg);
412 ir_graph *rem = current_ir_graph;
414 if (mode & mtp_property_const) {
415 /* already marked as a const function */
416 return mtp_property_const;
418 if (mode & mtp_property_pure) {
419 /* already marked as a pure function */
420 return mtp_property_const;
423 if (! IS_IRG_MARKED(irg))
427 end = get_irg_end(irg);
428 endbl = get_nodes_block(end);
429 mode = mtp_property_const;
431 current_ir_graph = irg;
433 /* visit every Return */
434 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
435 ir_node *node = get_Block_cfgpred(endbl, j);
436 ir_op *op = get_irn_op(node);
439 /* Bad nodes usually do NOT produce anything, so it's ok */
443 if (op == op_Return) {
444 mem = get_Return_mem(node);
446 /* Bad nodes usually do NOT produce anything, so it's ok */
450 if (mem != get_irg_initial_mem(irg))
451 mode = follow_mem(irg, mem, mode);
454 /* exception found. */
455 mode = follow_mem(irg, node, mode);
463 /* check, if a keep-alive exists */
464 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
465 ir_node *mem = get_End_keepalive(end, j);
467 if (mode_M != get_irn_mode(mem))
470 mode = follow_mem(irg, mem, mode);
477 set_irg_additional_property(irg, mode);
478 current_ir_graph = rem;
480 } /* is_pure_function */
483 * Handle calls to const functions.
485 static void handle_const_Calls(env_t *ctx)
489 ctx->n_calls_removed_SymConst = 0;
490 ctx->n_calls_removed_Sel = 0;
492 /* all calls of const functions can be transformed */
493 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
494 ir_graph *irg = get_irp_irg(i);
496 ctx->const_call_list = NULL;
497 ctx->pure_call_list = NULL;
498 ctx->proj_list = NULL;
499 irg_walk_graph(irg, NULL, collect_calls, ctx);
501 if (ctx->const_call_list)
502 fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
504 } /* handle_const_Calls */
507 * optimize function calls by handling const functions
509 void optimize_funccalls(int force_run)
512 unsigned num_const = 0;
513 unsigned num_pure = 0;
515 if (! get_opt_function_call())
518 /* prepare: mark all graphs as not analyzed */
519 n = get_irp_n_irgs();
520 for (i = n - 1; i >= 0; --i)
521 MARK_IRG(get_irp_irg(i));
523 /* first step: detect, which functions are const, i.e. do NOT touch any memory */
524 for (i = n - 1; i >= 0; --i) {
525 ir_graph *irg = get_irp_irg(i);
526 unsigned mode = is_pure_function(irg);
528 if (mode & mtp_property_const)
530 else if (mode & mtp_property_pure)
534 if (force_run || num_const > 0) {
537 handle_const_Calls(&ctx);
538 if (get_firm_verbosity()) {
539 printf("Detected %d graphs without side effects.\n", num_const);
540 printf("Optimizes %d(SymConst) + %d(Sel) calls to const/pure functions.\n",
541 ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
545 if (get_firm_verbosity()) {
546 printf("No graphs without side effects detected\n");
549 } /* optimize_funccalls */