3 * File name: ir/ir/ircgopt.c
4 * Purpose: Removal of unreachable methods.
5 * Author: Hubert Schmid
9 * Copyright: (c) 2002-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
14 * Entfernen von nicht erreichbaren (aufrufbaren) Methoden. Die Menge
15 * der nicht erreichbaren Methoden wird aus der Abschätzung der
16 * Aufrufrelation bestimmt.
32 static void clear_link(ir_node * node, void * env) {
33 set_irn_link(node, NULL);
37 /* Call-Operationen an die "link"-Liste von "call_head" anhängen. */
38 static void collect_call(ir_node * node, ir_node * head) {
39 if (get_irn_op(node) == op_Call) {
40 set_irn_link(node, get_irn_link(head));
41 set_irn_link(head, node);
45 static void make_entity_to_description(type_or_ent *tore, void *env) {
46 if (get_kind(tore) == k_entity) {
47 entity *ent = (entity *)tore;
49 if ((is_Method_type(get_entity_type(ent))) &&
50 (get_entity_peculiarity(ent) != peculiarity_description) &&
51 (get_entity_visibility(ent) != visibility_external_allocated) ) {
52 entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
53 if (get_entity_link(impl) != env) {
54 set_entity_peculiarity(ent, peculiarity_description);
55 //set_atomic_ent_value(ent, new_r_Const(get_const_code_irg(), get_irg_start_block(get_const_code_irg()),
56 // mode_P, get_tarval_null(mode_P)));
62 /* garbage collect methods: mark and remove */
63 void gc_irgs(int n_keep, entity ** keep_arr) {
64 void * MARK = &MARK; /* @@@ gefaehrlich!!! Aber wir markieren hoechstens zu viele ... */
67 if (!get_opt_dead_method_elimination()) return;
69 /* Mark entities that are alive. */
71 entity ** marked = NEW_ARR_F(entity *, n_keep);
72 for (i = 0; i < n_keep; ++i) {
73 marked[i] = keep_arr[i];
74 set_entity_link(marked[i], MARK);
75 if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
76 printf("dead method elimination: method %s kept alive.\n", get_entity_ld_name(marked[i]));
79 for (i = 0; i < ARR_LEN(marked); ++i) {
80 /* check for extern methods, these don't have an IRG */
81 if (get_entity_visibility(marked[i]) != visibility_external_allocated) {
82 ir_graph * irg = get_entity_irg(marked[i]);
83 ir_node * node = get_irg_end(irg);
85 irg_walk_graph(irg, clear_link, (irg_walk_func *) collect_call, node);
87 for (node = get_irn_link(node); node; node = get_irn_link(node)) {
89 assert(get_irn_op(node) == op_Call);
90 for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
91 entity * ent = get_Call_callee(node, i);
92 if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
93 set_entity_link(ent, MARK);
94 ARR_APP1(entity *, marked, ent);
95 if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
96 printf("dead method elimination: method %s can be called from Call %ld: kept alive.\n",
97 get_entity_ld_name(ent), get_irn_node_nr(node));
108 type_walk(make_entity_to_description, NULL, MARK);
109 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
110 ir_graph * irg = get_irp_irg(i);
111 entity * ent = get_irg_entity(irg);
112 /* Removing any graph invalidates all interprocedural loop trees. */
113 if (get_irg_loopinfo_state(irg) == loopinfo_ip_consistent ||
114 get_irg_loopinfo_state(irg) == loopinfo_ip_inconsistent) {
115 free_loop_information(irg);
117 if ((get_entity_visibility(ent) == visibility_local) && (get_entity_link(ent) != MARK)) {
119 set_entity_peculiarity(ent, peculiarity_description);
120 if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 1) {
121 printf("dead method elimination: freeing method %s\n", get_entity_ld_name(ent));
124 set_entity_link(ent, NULL);