2 * Copyright (C) 1995-2008 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 <adt/raw_bitset.h>
32 #include "funccall_t.h"
35 #include "irgraph_t.h"
39 #include "dbginfo_t.h"
42 #include "iredges_t.h"
43 #include "analyze_irg_args.h"
47 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
50 * The walker environment for updating function calls.
52 typedef struct _env_t {
53 unsigned n_calls_SymConst;
55 ir_node *const_call_list; /**< The list of all const function calls that will be changed. */
56 ir_node *pure_call_list; /**< The list of all pure function calls that will be changed. */
57 ir_node *nothrow_call_list; /**< The list of all nothrow function calls that will be changed. */
58 ir_node *proj_list; /**< The list of all potential Proj nodes that must be fixed. */
61 /** If non-null, evaluates entities for being a heap alloc. */
62 static check_alloc_entity_func is_alloc_entity = NULL;
64 /** Ready IRG's are marked in the ready set. */
65 static unsigned *ready_set;
67 /** IRG's that are in progress are marked here. */
68 static unsigned *busy_set;
71 * We misuse the mtp_property_inherited flag as temporary here.
72 * The is ok, as we cannot set or get it anyway using the
73 * get_addtional_properties API.
75 #define mtp_temporary mtp_property_inherited
78 * Walker: Collect all calls to const and pure functions
79 * to lists. Collect all Proj(Call) nodes into a Proj list.
81 static void collect_const_and_pure_calls(ir_node *node, void *env) {
90 /* set the link to NULL for all non-const/pure calls */
91 set_irn_link(call, NULL);
92 ptr = get_Call_ptr(call);
94 ent = get_Global_entity(ptr);
96 prop = get_entity_additional_properties(ent);
97 if ((prop & (mtp_property_const|mtp_property_pure)) == 0)
99 ++ctx->n_calls_SymConst;
100 } else if (get_opt_closed_world() &&
102 get_irg_callee_info_state(current_ir_graph) == irg_callee_info_consistent) {
103 /* If all possible callees are const functions, we can remove the memory edge. */
104 int i, n_callees = get_Call_n_callees(call);
105 if (n_callees == 0) {
106 /* This is kind of strange: dying code or a Call that will raise an exception
107 when executed as there is no implementation to call. So better not
112 /* note that const function are a subset of pure ones */
113 prop = mtp_property_const | mtp_property_pure;
114 for (i = 0; i < n_callees; ++i) {
115 ent = get_Call_callee(call, i);
116 if (ent == unknown_entity) {
117 /* we don't know which entity is called here */
120 prop &= get_entity_additional_properties(ent);
121 if (prop == mtp_no_property)
128 /* ok, if we get here we found a call to a const or a pure function */
129 if (prop & mtp_property_pure) {
130 set_irn_link(call, ctx->pure_call_list);
131 ctx->pure_call_list = call;
133 set_irn_link(call, ctx->const_call_list);
134 ctx->const_call_list = call;
136 } else if (is_Proj(node)) {
138 * Collect all memory and exception Proj's from
141 call = get_Proj_pred(node);
145 /* collect the Proj's in the Proj list */
146 switch (get_Proj_proj(node)) {
147 case pn_Call_M_regular:
148 case pn_Call_X_except:
149 case pn_Call_X_regular:
150 case pn_Call_M_except:
151 set_irn_link(node, ctx->proj_list);
152 ctx->proj_list = node;
158 } /* collect_const_and_pure_calls */
161 * Fix the list of collected Calls.
163 * @param irg the graph that contained calls to pure functions
164 * @param call_list the list of all call sites of const functions
165 * @param proj_list the list of all memory/exception Proj's of this call sites
167 static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
168 ir_node *call, *next, *mem, *proj;
170 ir_graph *rem = current_ir_graph;
172 current_ir_graph = irg;
174 /* First step: fix all calls by removing it's memory input.
175 It's original memory input is preserved in their link fields. */
176 for (call = call_list; call; call = next) {
177 next = get_irn_link(call);
178 mem = get_Call_mem(call);
180 set_irn_link(call, mem);
181 set_Call_mem(call, get_irg_no_mem(irg));
184 * Sorrily we cannot simply set the node to 'float'.
185 * There is a reason for that:
187 * - The call might be inside a loop/if that is NOT entered
188 * and calls a endless function. Setting the call to float
189 * would allow to move it out from the loop/if causing this
190 * function be called even if the loop/if is not entered ...
192 * This could be fixed using post-dominators for calls and Pin nodes
193 * but need some more analyzes to ensure that a call that potential
194 * never returns is not executed before some code that generates
195 * observable states...
198 /* finally, this call can float
199 set_irn_pinned(call, op_pin_state_floats); */
200 hook_func_call(irg, call);
203 /* Second step: fix all Proj's */
204 for (proj = proj_list; proj; proj = next) {
205 next = get_irn_link(proj);
206 call = get_Proj_pred(proj);
207 mem = get_irn_link(call);
209 /* beware of calls in the pure call list */
210 if (!mem || is_Call(mem))
212 assert(get_irn_mode(mem) == mode_M);
214 switch (get_Proj_proj(proj)) {
215 case pn_Call_M_regular: {
216 /* in dead code there might be cycles where proj == mem */
221 case pn_Call_X_except:
222 case pn_Call_M_except:
224 exchange(proj, get_irg_bad(irg));
226 case pn_Call_X_regular: {
227 ir_node *block = get_nodes_block(call);
229 exchange(proj, new_r_Jmp(irg, block));
237 /* changes were done ... */
238 set_irg_outs_inconsistent(irg);
239 set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
242 /* ... including exception edges */
243 set_irg_doms_inconsistent(irg);
245 current_ir_graph = rem;
246 } /* fix_const_call_list */
249 * Walker: Collect all calls to nothrow functions
250 * to lists. Collect all Proj(Call) nodes into a Proj list.
252 static void collect_nothrow_calls(ir_node *node, void *env) {
261 /* set the link to NULL for all non-const/pure calls */
262 set_irn_link(call, NULL);
263 ptr = get_Call_ptr(call);
264 if (is_Global(ptr)) {
265 ent = get_Global_entity(ptr);
267 prop = get_entity_additional_properties(ent);
268 if ((prop & mtp_property_nothrow) == 0)
270 ++ctx->n_calls_SymConst;
271 } else if (get_opt_closed_world() &&
273 get_irg_callee_info_state(current_ir_graph) == irg_callee_info_consistent) {
274 /* If all possible callees are nothrow functions, we can remove the exception edge. */
275 int i, n_callees = get_Call_n_callees(call);
276 if (n_callees == 0) {
277 /* This is kind of strange: dying code or a Call that will raise an exception
278 when executed as there is no implementation to call. So better not
283 /* note that const function are a subset of pure ones */
284 prop = mtp_property_nothrow;
285 for (i = 0; i < n_callees; ++i) {
286 ent = get_Call_callee(call, i);
287 if (ent == unknown_entity) {
288 /* we don't know which entity is called here */
291 prop &= get_entity_additional_properties(ent);
292 if (prop == mtp_no_property)
299 /* ok, if we get here we found a call to a nothrow function */
300 set_irn_link(call, ctx->nothrow_call_list);
301 ctx->nothrow_call_list = call;
302 } else if (is_Proj(node)) {
304 * Collect all memory and exception Proj's from
307 call = get_Proj_pred(node);
311 /* collect the Proj's in the Proj list */
312 switch (get_Proj_proj(node)) {
313 case pn_Call_M_regular:
314 case pn_Call_X_except:
315 case pn_Call_X_regular:
316 case pn_Call_M_except:
317 set_irn_link(node, ctx->proj_list);
318 ctx->proj_list = node;
324 } /* collect_nothrow_calls */
327 * Fix the list of collected nothrow Calls.
329 * @param irg the graph that contained calls to pure functions
330 * @param call_list the list of all call sites of const functions
331 * @param proj_list the list of all memory/exception Proj's of this call sites
333 static void fix_nothrow_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
334 ir_node *call, *next, *proj;
336 ir_graph *rem = current_ir_graph;
338 current_ir_graph = irg;
340 /* First step: go through the list of calls and mark them. */
341 for (call = call_list; call; call = next) {
342 next = get_irn_link(call);
344 /* current_ir_graph is in memory anyway, so it's a good marker */
345 set_irn_link(call, ¤t_ir_graph);
346 hook_func_call(irg, call);
349 /* Second step: Remove all exception Proj's */
350 for (proj = proj_list; proj; proj = next) {
351 next = get_irn_link(proj);
352 call = get_Proj_pred(proj);
354 /* handle only marked calls */
355 if (get_irn_link(call) != ¤t_ir_graph)
358 /* kill any exception flow */
359 switch (get_Proj_proj(proj)) {
360 case pn_Call_X_except:
361 case pn_Call_M_except:
363 exchange(proj, get_irg_bad(irg));
365 case pn_Call_X_regular: {
366 ir_node *block = get_nodes_block(call);
368 exchange(proj, new_r_Jmp(irg, block));
376 /* changes were done ... */
377 set_irg_outs_inconsistent(irg);
378 set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
381 /* ... including exception edges */
382 set_irg_doms_inconsistent(irg);
384 current_ir_graph = rem;
385 } /* fix_nothrow_call_list */
388 #define SET_IRG_READY(irg) rbitset_set(ready_set, get_irg_idx(irg))
389 #define IS_IRG_READY(irg) rbitset_is_set(ready_set, get_irg_idx(irg))
390 #define SET_IRG_BUSY(irg) rbitset_set(busy_set, get_irg_idx(irg))
391 #define CLEAR_IRG_BUSY(irg) rbitset_clear(busy_set, get_irg_idx(irg))
392 #define IS_IRG_BUSY(irg) rbitset_is_set(busy_set, get_irg_idx(irg))
395 static unsigned check_const_or_pure_function(ir_graph *irg, int top);
396 static unsigned check_nothrow_or_malloc(ir_graph *irg, int top);
399 * Calculate the bigger property of two. Handle the temporary flag right.
401 static unsigned max_property(unsigned a, unsigned b) {
402 unsigned r, t = (a | b) & mtp_temporary;
406 if (a == mtp_no_property || b == mtp_no_property)
407 return mtp_no_property;
413 * Follow the memory chain starting at node and determine
416 * @return mtp_property_const if only calls of const functions are detected
417 * mtp_property_pure if only Loads and const/pure
419 * mtp_no_property else
421 static unsigned _follow_mem(ir_node *node) {
422 unsigned m, mode = mtp_property_const;
427 if (mode == mtp_no_property)
428 return mtp_no_property;
430 if (irn_visited(node))
433 mark_irn_visited(node);
435 switch (get_irn_opcode(node)) {
437 node = get_Proj_pred(node);
446 /* do a dfs search */
447 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
448 m = _follow_mem(get_irn_n(node, i));
449 mode = max_property(mode, m);
450 if (mode == mtp_no_property)
451 return mtp_no_property;
456 /* Beware volatile Loads are NOT allowed in pure functions. */
457 if (get_Load_volatility(node) == volatility_is_volatile)
458 return mtp_no_property;
459 mode = max_property(mode, mtp_property_pure);
460 node = get_Load_mem(node);
464 /* A call is only tolerable if its either constant or pure. */
465 ptr = get_Call_ptr(node);
466 if (is_SymConst(ptr) && get_SymConst_kind(ptr) == symconst_addr_ent) {
467 ir_entity *ent = get_SymConst_entity(ptr);
468 ir_graph *irg = get_entity_irg(ent);
470 if (irg == current_ir_graph) {
471 /* A self-recursive call. The property did not depend on this call. */
472 } else if (irg == NULL) {
473 m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
474 mode = max_property(mode, m);
475 } else if (irg != NULL) {
476 /* we have a graph, analyze it. */
477 m = check_const_or_pure_function(irg, /*top=*/0);
478 mode = max_property(mode, m);
481 return mtp_no_property;
482 node = get_Call_mem(node);
486 return mtp_no_property;
492 * Follow the memory chain starting at node and determine
495 * @return mtp_property_const if only calls of const functions are detected
496 * mtp_property_pure if only Loads and const/pure calls detected
497 * mtp_no_property else
499 static unsigned follow_mem(ir_node *node, unsigned mode) {
502 m = _follow_mem(node);
503 return max_property(mode, m);
507 * Check if a graph represents a const or a pure function.
509 * @param irg the graph to check
510 * @param top if set, this is the top call
512 static unsigned check_const_or_pure_function(ir_graph *irg, int top) {
513 ir_node *end, *endbl;
515 unsigned prop = get_irg_additional_properties(irg);
516 ir_graph *rem = current_ir_graph;
518 if (prop & mtp_property_const) {
519 /* already marked as a const function */
520 return mtp_property_const;
522 if (prop & mtp_property_pure) {
523 /* already marked as a pure function */
524 return mtp_property_pure;
527 if (IS_IRG_READY(irg)) {
528 /* already checked */
529 return mtp_no_property;
531 if (IS_IRG_BUSY(irg)) {
532 /* we are still evaluate this method. Be optimistic,
533 return the best possible so far but mark the result as temporary. */
534 return mtp_temporary | mtp_property_const;
538 end = get_irg_end(irg);
539 endbl = get_nodes_block(end);
540 prop = mtp_property_const;
542 current_ir_graph = irg;
544 inc_irg_visited(irg);
545 /* mark the initial mem: recursion of follow_mem stops here */
546 mark_irn_visited(get_irg_initial_mem(irg));
548 /* visit every Return */
549 for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
550 ir_node *node = get_Block_cfgpred(endbl, j);
551 ir_opcode code = get_irn_opcode(node);
554 /* Bad nodes usually do NOT produce anything, so it's ok */
558 if (code == iro_Return) {
559 mem = get_Return_mem(node);
561 /* Bad nodes usually do NOT produce anything, so it's ok */
565 if (mem != get_irg_initial_mem(irg))
566 prop = max_property(prop, follow_mem(mem, prop));
568 /* Exception found. Cannot be const or pure. */
569 prop = mtp_no_property;
572 if (prop == mtp_no_property)
576 if (prop != mtp_no_property) {
577 /* check, if a keep-alive exists */
578 for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
579 ir_node *mem = get_End_keepalive(end, j);
581 if (mode_M != get_irn_mode(mem))
584 prop = max_property(prop, follow_mem(mem, prop));
585 if (prop == mtp_no_property)
590 if (prop != mtp_no_property) {
591 if (top || (prop & mtp_temporary) == 0) {
592 /* We use the temporary flag here to mark optimistic result.
593 Set the property only if we are sure that it does NOT base on
594 temporary results OR if we are at top-level. */
595 set_irg_additional_property(irg, prop & ~mtp_temporary);
602 current_ir_graph = rem;
604 } /* check_const_or_pure_function */
607 * Handle calls to const functions.
611 static void handle_const_Calls(env_t *ctx) {
614 ctx->n_calls_SymConst = 0;
615 ctx->n_calls_Sel = 0;
617 /* all calls of const functions can be transformed */
618 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
619 ir_graph *irg = get_irp_irg(i);
621 ctx->const_call_list = NULL;
622 ctx->pure_call_list = NULL;
623 ctx->proj_list = NULL;
624 irg_walk_graph(irg, NULL, collect_const_and_pure_calls, ctx);
626 if (ctx->const_call_list) {
627 fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
629 /* this graph was changed, invalidate analysis info */
630 set_irg_outs_inconsistent(irg);
631 set_irg_doms_inconsistent(irg);
634 } /* handle_const_Calls */
637 * Handle calls to nothrow functions.
641 static void handle_nothrow_Calls(env_t *ctx) {
644 ctx->n_calls_SymConst = 0;
645 ctx->n_calls_Sel = 0;
647 /* all calls of const functions can be transformed */
648 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
649 ir_graph *irg = get_irp_irg(i);
651 ctx->nothrow_call_list = NULL;
652 ctx->proj_list = NULL;
653 irg_walk_graph(irg, NULL, collect_nothrow_calls, ctx);
655 if (ctx->nothrow_call_list) {
656 fix_nothrow_call_list(irg, ctx->nothrow_call_list, ctx->proj_list);
658 /* this graph was changed, invalidate analysis info */
659 set_irg_outs_inconsistent(irg);
660 set_irg_doms_inconsistent(irg);
666 * Check, whether a given node represents a return value of
667 * a malloc like function (ie, new heap allocated memory).
669 * @param node the node to check
671 static int is_malloc_call_result(const ir_node *node) {
672 if (is_Alloc(node) && get_Alloc_where(node) == heap_alloc) {
673 /* Firm style high-level allocation */
676 if (is_alloc_entity != NULL && is_Call(node)) {
677 ir_node *ptr = get_Call_ptr(node);
679 if (is_Global(ptr)) {
680 ir_entity *ent = get_Global_entity(ptr);
681 return is_alloc_entity(ent);
685 } /* is_malloc_call_result */
688 * Update a property depending on a call property.
690 static unsigned update_property(unsigned orig_prop, unsigned call_prop) {
691 unsigned t = (orig_prop | call_prop) & mtp_temporary;
692 unsigned r = orig_prop & call_prop;
694 } /** update_property */
697 * Check if a node is stored.
699 static int is_stored(const ir_node *n) {
700 const ir_edge_t *edge;
703 foreach_out_edge(n, edge) {
704 const ir_node *succ = get_edge_src_irn(edge);
706 switch (get_irn_opcode(succ)) {
713 if (get_Store_value(succ) == n)
715 /* ok if its only the address input */
724 ptr = get_Call_ptr(succ);
725 if (is_Global(ptr)) {
726 ir_entity *ent = get_Global_entity(ptr);
729 /* we know the called entity */
730 for (i = get_Call_n_params(succ) - 1; i >= 0; --i) {
731 if (get_Call_param(succ, i) == n) {
732 /* n is the i'th param of the call */
733 if (get_method_param_access(ent, i) & ptr_access_store) {
734 /* n is store in ent */
740 /* unknown call address */
745 /* bad, potential alias */
753 * Check that the return value of an irg is not stored anywhere.
755 * return ~mtp_property_malloc if return values are stored, ~0 else
757 static unsigned check_stored_result(ir_graph *irg) {
758 ir_node *end_blk = get_irg_end_block(irg);
761 int old_edges = edges_assure_kind(irg, EDGE_KIND_NORMAL);
763 for (i = get_Block_n_cfgpreds(end_blk) - 1; i >= 0; --i) {
764 ir_node *pred = get_Block_cfgpred(end_blk, i);
766 if (! is_Return(pred))
768 for (j = get_Return_n_ress(pred) - 1; j >= 0; --j) {
769 const ir_node *irn = get_Return_res(pred, j);
771 if (is_stored(irn)) {
772 /* bad, might create an alias */
773 res = ~mtp_property_malloc;
780 edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
782 } /* check_stored_result */
785 * Check if a graph represents a nothrow or a malloc function.
787 * @param irg the graph to check
788 * @param top if set, this is the top call
790 static unsigned check_nothrow_or_malloc(ir_graph *irg, int top) {
791 ir_node *end_blk = get_irg_end_block(irg);
795 unsigned curr_prop = mtp_property_malloc | mtp_property_nothrow;
797 if (IS_IRG_READY(irg)) {
798 /* already checked */
799 return get_irg_additional_properties(irg);
801 if (IS_IRG_BUSY(irg)) {
802 /* we are still evaluate this method. Be optimistic,
803 return the best possible so far but mark the result as temporary. */
804 return mtp_temporary | mtp_property_malloc | mtp_property_nothrow;
808 ent = get_irg_entity(irg);
809 mtp = get_entity_type(ent);
811 if (get_method_n_ress(mtp) <= 0)
812 curr_prop &= ~mtp_property_malloc;
814 for (i = get_Block_n_cfgpreds(end_blk) - 1; i >= 0; --i) {
815 ir_node *pred = get_Block_cfgpred(end_blk, i);
817 if (is_Return(pred)) {
818 if (curr_prop & mtp_property_malloc) {
819 /* check, if malloc is called here */
820 for (j = get_Return_n_ress(pred) - 1; j >= 0; --j) {
821 ir_node *res = get_Return_res(pred, j);
823 /* skip Confirms and Casts */
824 res = skip_HighLevel_ops(res);
827 res = get_Proj_pred(res);
828 if (is_malloc_call_result(res)) {
829 /* ok, this is a malloc */
830 } else if (is_Call(res)) {
831 ir_node *ptr = get_Call_ptr(res);
833 if (is_Global(ptr)) {
835 ir_entity *ent = get_Global_entity(ptr);
836 ir_graph *callee = get_entity_irg(ent);
839 /* A self-recursive call. The property did not depend on this call. */
840 } else if (callee != NULL) {
841 unsigned prop = check_nothrow_or_malloc(callee, /*top=*/0);
842 curr_prop = update_property(curr_prop, prop);
844 curr_prop = update_property(curr_prop, get_entity_additional_properties(ent));
846 } else if (get_opt_closed_world() &&
848 get_irg_callee_info_state(irg) == irg_callee_info_consistent) {
849 /* check if all possible callees are malloc functions. */
850 int i, n_callees = get_Call_n_callees(res);
851 if (n_callees == 0) {
852 /* This is kind of strange: dying code or a Call that will raise an exception
853 when executed as there is no implementation to call. So better not
855 curr_prop &= ~mtp_property_malloc;
859 for (i = 0; i < n_callees; ++i) {
860 ir_entity *ent = get_Call_callee(res, i);
861 if (ent == unknown_entity) {
862 /* we don't know which entity is called here */
863 curr_prop &= ~mtp_property_malloc;
866 if ((get_entity_additional_properties(ent) & mtp_property_malloc) == 0) {
867 curr_prop &= ~mtp_property_malloc;
871 /* if we pass the for cycle, malloc is still ok */
874 curr_prop &= ~mtp_property_malloc;
877 /* unknown return value */
878 curr_prop &= ~mtp_property_malloc;
882 } else if (curr_prop & mtp_property_nothrow) {
883 /* exception flow detected */
884 pred = skip_Proj(pred);
887 ir_node *ptr = get_Call_ptr(pred);
889 if (is_Global(ptr)) {
891 ir_entity *ent = get_Global_entity(ptr);
892 ir_graph *callee = get_entity_irg(ent);
895 /* A self-recursive call. The property did not depend on this call. */
896 } else if (callee != NULL) {
897 /* Note: we check here for nothrow only, so do NOT reset the malloc property */
898 unsigned prop = check_nothrow_or_malloc(callee, /*top=*/0) | mtp_property_malloc;
899 curr_prop = update_property(curr_prop, prop);
901 if ((get_entity_additional_properties(ent) & mtp_property_nothrow) == 0)
902 curr_prop &= ~mtp_property_nothrow;
904 } else if (get_opt_closed_world() &&
906 get_irg_callee_info_state(irg) == irg_callee_info_consistent) {
907 /* check if all possible callees are nothrow functions. */
908 int i, n_callees = get_Call_n_callees(pred);
909 if (n_callees == 0) {
910 /* This is kind of strange: dying code or a Call that will raise an exception
911 when executed as there is no implementation to call. So better not
913 curr_prop &= ~mtp_property_nothrow;
917 for (i = 0; i < n_callees; ++i) {
918 ir_entity *ent = get_Call_callee(pred, i);
919 if (ent == unknown_entity) {
920 /* we don't know which entity is called here */
921 curr_prop &= ~mtp_property_nothrow;
924 if ((get_entity_additional_properties(ent) & mtp_property_nothrow) == 0) {
925 curr_prop &= ~mtp_property_nothrow;
929 /* if we pass the for cycle, nothrow is still ok */
932 curr_prop &= ~mtp_property_nothrow;
935 /* real exception flow possible. */
936 curr_prop &= ~mtp_property_nothrow;
939 if ((curr_prop & ~mtp_temporary) == mtp_no_property) {
940 /* no need to search further */
945 if (curr_prop & mtp_property_malloc) {
947 * Note that the malloc property means not only return newly allocated
948 * memory, but also that this memory is ALIAS FREE.
949 * To ensure that, we do NOT allow that the returned memory is somewhere
952 curr_prop &= check_stored_result(irg);
955 if (curr_prop != mtp_no_property) {
956 if (top || (curr_prop & mtp_temporary) == 0) {
957 /* We use the temporary flag here to mark an optimistic result.
958 Set the property only if we are sure that it does NOT base on
959 temporary results OR if we are at top-level. */
960 set_irg_additional_property(irg, curr_prop & ~mtp_temporary);
968 } /* check_nothrow_or_malloc */
971 * optimize function calls by handling const functions
973 void optimize_funccalls(int force_run, check_alloc_entity_func callback)
976 unsigned num_const = 0;
977 unsigned num_pure = 0;
978 unsigned num_nothrow = 0;
979 unsigned num_malloc = 0;
981 is_alloc_entity = callback;
983 /* prepare: mark all graphs as not analyzed */
984 last_idx = get_irp_last_idx();
985 ready_set = rbitset_malloc(last_idx);
986 busy_set = rbitset_malloc(last_idx);
988 /* first step: detect, which functions are nothrow or malloc */
989 DB((dbg, LEVEL_2, "Detecting nothrow and malloc properties ...\n"));
990 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
991 ir_graph *irg = get_irp_irg(i);
992 unsigned prop = check_nothrow_or_malloc(irg, /*top=*/1);
994 if (prop & mtp_property_nothrow) {
996 DB((dbg, LEVEL_2, "%+F has the nothrow property\n", irg));
997 } else if (prop & mtp_property_malloc) {
999 DB((dbg, LEVEL_2, "%+F has the malloc property\n", irg));
1003 /* second step: remove exception edges: this must be done before the
1004 detection of const and pure functions take place. */
1005 if (force_run || num_nothrow > 0) {
1008 handle_nothrow_Calls(&ctx);
1009 DB((dbg, LEVEL_1, "Detected %u nothrow graphs, %u malloc graphs.\n", num_nothrow, num_malloc));
1010 DB((dbg, LEVEL_1, "Optimizes %u(SymConst) + %u(Sel) calls to nothrow functions.\n",
1011 ctx.n_calls_SymConst, ctx.n_calls_Sel));
1013 DB((dbg, LEVEL_1, "No graphs without side effects detected\n"));
1016 rbitset_clear_all(ready_set, last_idx);
1017 rbitset_clear_all(busy_set, last_idx);
1019 /* third step: detect, which functions are const or pure */
1020 DB((dbg, LEVEL_2, "Detecting const and pure properties ...\n"));
1021 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
1022 ir_graph *irg = get_irp_irg(i);
1023 unsigned prop = check_const_or_pure_function(irg, /*top=*/1);
1025 if (prop & mtp_property_const) {
1027 DB((dbg, LEVEL_2, "%+F has the const property\n", irg));
1028 } else if (prop & mtp_property_pure) {
1030 DB((dbg, LEVEL_2, "%+F has the pure property\n", irg));
1034 if (force_run || num_const > 0) {
1037 handle_const_Calls(&ctx);
1038 DB((dbg, LEVEL_1, "Detected %u const graphs, %u pure graphs.\n", num_const, num_pure));
1039 DB((dbg, LEVEL_1, "Optimizes %u(SymConst) + %u(Sel) calls to const functions.\n",
1040 ctx.n_calls_SymConst, ctx.n_calls_Sel));
1042 DB((dbg, LEVEL_1, "No graphs without side effects detected\n"));
1046 } /* optimize_funccalls */
1048 /* initialize the funccall optimization */
1049 void firm_init_funccalls(void) {
1050 FIRM_DBG_REGISTER(dbg, "firm.opt.funccalls");
1051 // firm_dbg_set_mask(dbg, -1);
1052 } /* firm_init_funccalls */