+ if (is_stored(irn)) {
+ /* bad, might create an alias */
+ res = ~mtp_property_malloc;
+ goto finish;
+ }
+ }
+ }
+finish:
+ if (! old_edges)
+ edges_deactivate_kind(irg, EDGE_KIND_NORMAL);
+ return res;
+}
+
+/**
+ * Check if a graph represents a nothrow or a malloc function.
+ *
+ * @param irg the graph to check
+ * @param top if set, this is the top call
+ */
+static mtp_additional_properties check_nothrow_or_malloc(ir_graph *irg, int top)
+{
+ mtp_additional_properties curr_prop = mtp_property_malloc | mtp_property_nothrow;
+ ir_node *end_blk = get_irg_end_block(irg);
+ ir_entity *ent;
+ ir_type *mtp;
+ int i;
+
+ if (IS_IRG_READY(irg)) {
+ /* already checked */
+ return get_irg_additional_properties(irg);
+ }
+ if (IS_IRG_BUSY(irg)) {
+ /* we are still evaluate this method. Be optimistic,
+ return the best possible so far but mark the result as temporary. */
+ return mtp_temporary | mtp_property_malloc | mtp_property_nothrow;
+ }
+ SET_IRG_BUSY(irg);
+
+ ent = get_irg_entity(irg);
+ mtp = get_entity_type(ent);
+
+ if (get_method_n_ress(mtp) <= 0)
+ curr_prop &= ~mtp_property_malloc;
+
+ for (i = get_Block_n_cfgpreds(end_blk) - 1; i >= 0; --i) {
+ ir_node *pred = get_Block_cfgpred(end_blk, i);
+
+ if (is_Return(pred)) {
+ if (curr_prop & mtp_property_malloc) {
+ size_t j;
+
+ /* check, if malloc is called here */
+ for (j = get_Return_n_ress(pred); j > 0;) {
+ ir_node *res = get_Return_res(pred, --j);
+
+ /* skip Confirms and Casts */
+ res = skip_HighLevel_ops(res);
+ /* skip Proj's */
+ while (is_Proj(res))
+ res = get_Proj_pred(res);
+ if (is_malloc_call_result(res)) {
+ /* ok, this is a malloc */
+ } else if (is_Call(res)) {
+ ir_node *ptr = get_Call_ptr(res);
+
+ if (is_Global(ptr)) {
+ /* a direct call */
+ ir_entity *ent = get_Global_entity(ptr);
+ ir_graph *callee = get_entity_irg(ent);
+
+ if (callee == irg) {
+ /* A self-recursive call. The property did not depend on this call. */
+ } else if (callee != NULL) {
+ mtp_additional_properties prop = check_nothrow_or_malloc(callee, /*top=*/0);
+ curr_prop = update_property(curr_prop, prop);
+ } else {
+ curr_prop = update_property(curr_prop, get_entity_additional_properties(ent));
+ }
+ } else if (get_opt_closed_world() &&
+ is_Sel(ptr) &&
+ get_irg_callee_info_state(irg) == irg_callee_info_consistent) {
+ /* check if all possible callees are malloc functions. */
+ size_t i, n_callees = get_Call_n_callees(res);
+ if (n_callees == 0) {
+ /* This is kind of strange: dying code or a Call that will raise an exception
+ when executed as there is no implementation to call. So better not
+ optimize. */
+ curr_prop &= ~mtp_property_malloc;
+ continue;
+ }
+
+ for (i = 0; i < n_callees; ++i) {
+ ir_entity *ent = get_Call_callee(res, i);
+ if (ent == unknown_entity) {
+ /* we don't know which entity is called here */
+ curr_prop &= ~mtp_property_malloc;
+ break;
+ }
+ if ((get_entity_additional_properties(ent) & mtp_property_malloc) == 0) {
+ curr_prop &= ~mtp_property_malloc;
+ break;
+ }
+ }
+ /* if we pass the for cycle, malloc is still ok */
+ } else {
+ /* unknown call */
+ curr_prop &= ~mtp_property_malloc;
+ }
+ } else {
+ /* unknown return value */
+ curr_prop &= ~mtp_property_malloc;
+ }
+ }
+ }
+ } else if (curr_prop & mtp_property_nothrow) {
+ /* exception flow detected */
+ pred = skip_Proj(pred);
+
+ if (is_Call(pred)) {
+ ir_node *ptr = get_Call_ptr(pred);
+
+ if (is_Global(ptr)) {
+ /* a direct call */
+ ir_entity *ent = get_Global_entity(ptr);
+ ir_graph *callee = get_entity_irg(ent);
+
+ if (callee == irg) {
+ /* A self-recursive call. The property did not depend on this call. */
+ } else if (callee != NULL) {
+ /* Note: we check here for nothrow only, so do NOT reset the malloc property */
+ mtp_additional_properties prop = check_nothrow_or_malloc(callee, /*top=*/0) | mtp_property_malloc;
+ curr_prop = update_property(curr_prop, prop);
+ } else {
+ if ((get_entity_additional_properties(ent) & mtp_property_nothrow) == 0)
+ curr_prop &= ~mtp_property_nothrow;
+ }
+ } else if (get_opt_closed_world() &&
+ is_Sel(ptr) &&
+ get_irg_callee_info_state(irg) == irg_callee_info_consistent) {
+ /* check if all possible callees are nothrow functions. */
+ size_t i, n_callees = get_Call_n_callees(pred);
+ if (n_callees == 0) {
+ /* This is kind of strange: dying code or a Call that will raise an exception
+ when executed as there is no implementation to call. So better not
+ optimize. */
+ curr_prop &= ~mtp_property_nothrow;
+ continue;
+ }
+
+ for (i = 0; i < n_callees; ++i) {
+ ir_entity *ent = get_Call_callee(pred, i);
+ if (ent == unknown_entity) {
+ /* we don't know which entity is called here */
+ curr_prop &= ~mtp_property_nothrow;
+ break;
+ }
+ if ((get_entity_additional_properties(ent) & mtp_property_nothrow) == 0) {
+ curr_prop &= ~mtp_property_nothrow;
+ break;
+ }
+ }
+ /* if we pass the for cycle, nothrow is still ok */
+ } else {
+ /* unknown call */
+ curr_prop &= ~mtp_property_nothrow;
+ }
+ } else {
+ /* real exception flow possible. */
+ curr_prop &= ~mtp_property_nothrow;
+ }
+ }
+ if ((curr_prop & ~mtp_temporary) == mtp_no_property) {
+ /* no need to search further */
+ break;
+ }
+ }
+
+ if (curr_prop & mtp_property_malloc) {
+ /*
+ * Note that the malloc property means not only return newly allocated
+ * memory, but also that this memory is ALIAS FREE.
+ * To ensure that, we do NOT allow that the returned memory is somewhere
+ * stored.
+ */
+ curr_prop &= check_stored_result(irg);
+ }
+
+ if (curr_prop != mtp_no_property) {
+ if (top || (curr_prop & mtp_temporary) == 0) {
+ /* We use the temporary flag here to mark an optimistic result.
+ Set the property only if we are sure that it does NOT base on
+ temporary results OR if we are at top-level. */
+ add_irg_additional_properties(irg, curr_prop & ~mtp_temporary);
+ SET_IRG_READY(irg);
+ }
+ }
+ if (top)
+ SET_IRG_READY(irg);
+ CLEAR_IRG_BUSY(irg);
+ return curr_prop;
+} /* check_nothrow_or_malloc */
+
+/**
+ * When a function was detected as "const", it might be moved out of loops.
+ * This might be dangerous if the graph can contain endless loops.
+ */
+static void check_for_possible_endless_loops(ir_graph *irg)
+{
+ ir_loop *root_loop;
+ assure_cf_loop(irg);
+
+ root_loop = get_irg_loop(irg);
+ if (root_loop->flags & loop_outer_loop)
+ add_irg_additional_properties(irg, mtp_property_has_loop);
+}
+
+/*
+ * optimize function calls by handling const functions
+ */
+void optimize_funccalls(void)
+{
+ size_t i, n;
+ size_t last_idx;
+ env_t ctx;
+ size_t num_const = 0;
+ size_t num_pure = 0;
+ size_t num_nothrow = 0;
+ size_t num_malloc = 0;
+
+ /* prepare: mark all graphs as not analyzed */
+ last_idx = get_irp_last_idx();
+ ready_set = rbitset_malloc(last_idx);
+ busy_set = rbitset_malloc(last_idx);
+
+ /* first step: detect, which functions are nothrow or malloc */
+ DB((dbg, LEVEL_2, "Detecting nothrow and malloc properties ...\n"));
+ for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
+ ir_graph *irg = get_irp_irg(i);
+ unsigned prop = check_nothrow_or_malloc(irg, /*top=*/1);
+
+ if (prop & mtp_property_nothrow) {
+ ++num_nothrow;
+ DB((dbg, LEVEL_2, "%+F has the nothrow property\n", irg));
+ } else if (prop & mtp_property_malloc) {
+ ++num_malloc;
+ DB((dbg, LEVEL_2, "%+F has the malloc property\n", irg));
+ }
+ }
+
+ /* second step: remove exception edges: this must be done before the
+ detection of const and pure functions take place. */
+ handle_nothrow_Calls(&ctx);
+ DB((dbg, LEVEL_1, "Detected %zu nothrow graphs, %zu malloc graphs.\n", num_nothrow, num_malloc));
+ DB((dbg, LEVEL_1, "Optimizes %zu(SymConst) + %zu(Sel) calls to nothrow functions.\n",
+ ctx.n_calls_SymConst, ctx.n_calls_Sel));
+
+ rbitset_clear_all(ready_set, last_idx);
+ rbitset_clear_all(busy_set, last_idx);
+
+ /* third step: detect, which functions are const or pure */
+ DB((dbg, LEVEL_2, "Detecting const and pure properties ...\n"));
+ for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
+ ir_graph *irg = get_irp_irg(i);
+ unsigned prop = check_const_or_pure_function(irg, /*top=*/1);
+
+ if (prop & mtp_property_const) {
+ ++num_const;
+ DB((dbg, LEVEL_2, "%+F has the const property\n", irg));
+ check_for_possible_endless_loops(irg);
+ } else if (prop & mtp_property_pure) {
+ ++num_pure;
+ DB((dbg, LEVEL_2, "%+F has the pure property\n", irg));
+ }
+ }
+
+ handle_const_Calls(&ctx);
+ DB((dbg, LEVEL_1, "Detected %zu const graphs, %zu pure graphs.\n", num_const, num_pure));
+ DB((dbg, LEVEL_1, "Optimizes %u(SymConst) + %u(Sel) calls to const functions.\n",
+ ctx.n_calls_SymConst, ctx.n_calls_Sel));
+
+ xfree(busy_set);
+ xfree(ready_set);
+}
+
+/* initialize the funccall optimization */
+void firm_init_funccalls(void)
+{
+ FIRM_DBG_REGISTER(dbg, "firm.opt.funccalls");
+}
+
+/* Creates an ir_prog pass for optimize_funccalls. */
+ir_prog_pass_t *optimize_funccalls_pass(const char *name)
+{
+ return def_prog_pass(name ? name : "funccall", optimize_funccalls);