+ * @param call_list the list of all call sites of const functions
+ * @param proj_list the list of all memory/exception Proj's of this call sites
+ */
+static void fix_nothrow_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list)
+{
+ ir_node *call, *next, *proj;
+ int exc_changed = 0;
+
+ /* First step: go through the list of calls and mark them. */
+ for (call = call_list; call; call = next) {
+ next = (ir_node*)get_irn_link(call);
+
+ /* current_ir_graph is in memory anyway, so it's a good marker */
+ set_irn_link(call, ¤t_ir_graph);
+ hook_func_call(irg, call);
+ }
+
+ /* Second step: Remove all exception Proj's */
+ for (proj = proj_list; proj; proj = next) {
+ next = (ir_node*)get_irn_link(proj);
+ call = get_Proj_pred(proj);
+
+ /* handle only marked calls */
+ if (get_irn_link(call) != ¤t_ir_graph)
+ continue;
+
+ /* kill any exception flow */
+ switch (get_Proj_proj(proj)) {
+ case pn_Call_X_except:
+ exc_changed = 1;
+ exchange(proj, new_r_Bad(irg, mode_X));
+ break;
+ case pn_Call_X_regular: {
+ ir_node *block = get_nodes_block(call);
+ exc_changed = 1;
+ exchange(proj, new_r_Jmp(block));
+ break;
+ }
+ default:
+ break;
+ }
+ }
+
+ /* changes were done ... */
+ set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
+
+ if (exc_changed) {
+ /* ... including exception edges */
+ set_irg_doms_inconsistent(irg);
+ }
+} /* fix_nothrow_call_list */
+
+/* marking */
+#define SET_IRG_READY(irg) rbitset_set(ready_set, get_irg_idx(irg))
+#define IS_IRG_READY(irg) rbitset_is_set(ready_set, get_irg_idx(irg))
+#define SET_IRG_BUSY(irg) rbitset_set(busy_set, get_irg_idx(irg))
+#define CLEAR_IRG_BUSY(irg) rbitset_clear(busy_set, get_irg_idx(irg))
+#define IS_IRG_BUSY(irg) rbitset_is_set(busy_set, get_irg_idx(irg))
+
+/* forward */
+static mtp_additional_properties check_const_or_pure_function(ir_graph *irg, int top);
+
+/**
+ * Calculate the bigger property of two. Handle the temporary flag right.
+ */
+static mtp_additional_properties max_property(mtp_additional_properties a,
+ mtp_additional_properties b)
+{
+ mtp_additional_properties r;
+ mtp_additional_properties t = (a | b) & mtp_temporary;
+ a &= ~mtp_temporary;
+ b &= ~mtp_temporary;
+
+ if (a == mtp_no_property || b == mtp_no_property)
+ return mtp_no_property;
+ r = a > b ? a : b;
+ return r | t;
+} /* max_property */
+
+/**
+ * Follow the memory chain starting at node and determine
+ * the mtp_property.
+ *
+ * @return mtp_property_const if only calls of const functions are detected
+ * mtp_property_pure if only Loads and const/pure calls detected
+ * mtp_no_property else
+ */
+static mtp_additional_properties follow_mem_(ir_node *node)
+{
+ mtp_additional_properties mode = mtp_property_const;
+ mtp_additional_properties m;
+ ir_node *ptr;
+ int i;
+
+ for (;;) {
+ if (mode == mtp_no_property)
+ return mtp_no_property;
+
+ if (irn_visited_else_mark(node))
+ return mode;
+
+ switch (get_irn_opcode(node)) {
+ case iro_Proj:
+ node = get_Proj_pred(node);
+ break;
+
+ case iro_NoMem:
+ /* finish here */
+ return mode;
+
+ case iro_Phi:
+ case iro_Sync:
+ /* do a dfs search */
+ for (i = get_irn_arity(node) - 1; i >= 0; --i) {
+ m = follow_mem_(get_irn_n(node, i));
+ mode = max_property(mode, m);
+ if (mode == mtp_no_property)
+ return mtp_no_property;
+ }
+ return mode;
+
+ case iro_Load:
+ /* Beware volatile Loads are NOT allowed in pure functions. */
+ if (get_Load_volatility(node) == volatility_is_volatile)
+ return mtp_no_property;
+ mode = max_property(mode, mtp_property_pure);
+ node = get_Load_mem(node);
+ break;
+
+ case iro_Call:
+ /* A call is only tolerable if its either constant or pure. */
+ ptr = get_Call_ptr(node);
+ if (is_SymConst_addr_ent(ptr)) {
+ ir_entity *ent = get_SymConst_entity(ptr);
+ ir_graph *irg = get_entity_irg(ent);
+
+ if (irg == NULL) {
+ m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
+ mode = max_property(mode, m);
+ } else {
+ /* we have a graph, analyze it. */
+ m = check_const_or_pure_function(irg, /*top=*/0);
+ mode = max_property(mode, m);
+ }
+ } else
+ return mtp_no_property;
+ node = get_Call_mem(node);
+ break;
+
+ default:
+ return mtp_no_property;
+ }
+ }
+}
+
+/**
+ * Follow the memory chain starting at node and determine
+ * the mtp_property.
+ *
+ * @return mtp_property_const if only calls of const functions are detected
+ * mtp_property_pure if only Loads and const/pure calls detected
+ * mtp_no_property else
+ */
+static mtp_additional_properties follow_mem(ir_node *node, mtp_additional_properties mode)
+{
+ mtp_additional_properties m = follow_mem_(node);
+ return max_property(mode, m);
+}
+
+/**
+ * Check if a graph represents a const or a pure function.
+ *
+ * @param irg the graph to check
+ * @param top if set, this is the top call
+ */
+static mtp_additional_properties check_const_or_pure_function(ir_graph *irg, int top)
+{
+ ir_node *end, *endbl;
+ int j;
+ mtp_additional_properties prop = get_irg_additional_properties(irg);
+
+ if (prop & mtp_property_const) {
+ /* already marked as a const function */
+ return mtp_property_const;
+ }
+ if (prop & mtp_property_pure) {
+ /* already marked as a pure function */
+ return mtp_property_pure;
+ }
+
+ if (IS_IRG_READY(irg)) {
+ /* already checked */
+ return mtp_no_property;
+ }
+ if (IS_IRG_BUSY(irg)) {
+ /* We are still evaluate this method.
+ * The function (indirectly) calls itself and thus may not terminate.
+ */
+ return mtp_no_property;
+ }
+ SET_IRG_BUSY(irg);
+
+ end = get_irg_end(irg);
+ endbl = get_nodes_block(end);
+ prop = mtp_property_const;
+
+ ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
+ inc_irg_visited(irg);
+ /* mark the initial mem: recursion of follow_mem() stops here */
+ mark_irn_visited(get_irg_initial_mem(irg));
+
+ /* visit every Return */
+ for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
+ ir_node *node = get_Block_cfgpred(endbl, j);
+ unsigned code = get_irn_opcode(node);
+ ir_node *mem;
+
+ /* Bad nodes usually do NOT produce anything, so it's ok */
+ if (code == iro_Bad)
+ continue;
+
+ if (code == iro_Return) {
+ mem = get_Return_mem(node);
+
+ /* Bad nodes usually do NOT produce anything, so it's ok */
+ if (is_Bad(mem))
+ continue;
+
+ if (mem != get_irg_initial_mem(irg))
+ prop = max_property(prop, follow_mem(mem, prop));
+ } else {
+ /* Exception found. Cannot be const or pure. */
+ prop = mtp_no_property;
+ break;
+ }
+ if (prop == mtp_no_property)
+ break;
+ }
+
+ if (prop != mtp_no_property) {
+ /* check, if a keep-alive exists */
+ for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
+ ir_node *kept = get_End_keepalive(end, j);
+
+ if (is_Block(kept)) {
+ prop = mtp_no_property;
+ break;
+ }
+
+ if (mode_M != get_irn_mode(kept))
+ continue;
+
+ prop = max_property(prop, follow_mem(kept, prop));
+ if (prop == mtp_no_property)
+ break;
+ }
+ }
+
+ if (top) {
+ /* Set the property only if we are at top-level. */
+ if (prop != mtp_no_property) {
+ add_irg_additional_properties(irg, prop);
+ }
+ SET_IRG_READY(irg);
+ }
+ CLEAR_IRG_BUSY(irg);
+ ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
+ return prop;
+} /* check_const_or_pure_function */
+
+/**
+ * Handle calls to const functions.
+ *
+ * @param ctx context
+ */
+static void handle_const_Calls(env_t *ctx)
+{
+ size_t i, n;
+
+ ctx->n_calls_SymConst = 0;
+ ctx->n_calls_Sel = 0;
+
+ /* all calls of const functions can be transformed */
+ for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
+ ir_graph *irg = get_irp_irg(i);
+
+ ctx->float_const_call_list = NULL;
+ ctx->nonfloat_const_call_list = NULL;
+ ctx->pure_call_list = NULL;
+ ctx->proj_list = NULL;
+
+ ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
+ irg_walk_graph(irg, NULL, collect_const_and_pure_calls, ctx);
+
+ if (ctx->float_const_call_list != NULL)
+ fix_const_call_lists(irg, ctx);
+ ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
+ }
+} /* handle_const_Calls */
+
+/**
+ * Handle calls to nothrow functions.
+ *
+ * @param ctx context
+ */
+static void handle_nothrow_Calls(env_t *ctx)
+{
+ size_t i, n;
+
+ ctx->n_calls_SymConst = 0;
+ ctx->n_calls_Sel = 0;
+
+ /* all calls of const functions can be transformed */
+ for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
+ ir_graph *irg = get_irp_irg(i);
+
+ ctx->nothrow_call_list = NULL;
+ ctx->proj_list = NULL;
+
+ ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
+ irg_walk_graph(irg, NULL, collect_nothrow_calls, ctx);
+
+ if (ctx->nothrow_call_list)
+ fix_nothrow_call_list(irg, ctx->nothrow_call_list, ctx->proj_list);
+ ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
+ }
+}
+
+/**
+ * Check, whether a given node represents a return value of
+ * a malloc like function (ie, new heap allocated memory).
+ *
+ * @param node the node to check