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.
34 * Walker: adds Call operations to a head's link list.
36 static void collect_call(ir_node * node, void *env) {
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);
60 /* garbage collect methods: mark and remove */
61 void gc_irgs(int n_keep, entity ** keep_arr) {
62 void * MARK = &MARK; /* @@@ gefaehrlich!!! Aber wir markieren hoechstens zu viele ... */
65 if (!get_opt_dead_method_elimination()) return;
67 if (n_keep >= get_irp_n_irgs()) {
68 /* Shortcut. Obviously we have to keep all methods. */
72 /* Mark entities that are alive. */
74 entity ** marked = NEW_ARR_F(entity *, n_keep);
75 for (i = 0; i < n_keep; ++i) {
76 marked[i] = keep_arr[i];
77 set_entity_link(marked[i], MARK);
78 if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
79 printf("dead method elimination: method %s kept alive.\n", get_entity_ld_name(marked[i]));
83 for (i = 0; i < ARR_LEN(marked); ++i) {
84 /* check for extern methods, these don't have an IRG */
85 if (get_entity_visibility(marked[i]) != visibility_external_allocated) {
86 ir_graph * irg = get_entity_irg(marked[i]);
87 ir_node * node = get_irg_end(irg);
90 irg_walk_graph(irg, firm_clear_link, collect_call, node);
93 for (node = get_irn_link(node); node; node = get_irn_link(node)) {
95 assert(get_irn_op(node) == op_Call);
97 for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
98 entity * ent = get_Call_callee(node, i);
100 if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
101 set_entity_link(ent, MARK);
102 ARR_APP1(entity *, marked, ent);
103 if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
104 printf("dead method elimination: method %s can be called from Call %ld: kept alive.\n",
105 get_entity_ld_name(ent), get_irn_node_nr(node));
116 type_walk(make_entity_to_description, NULL, MARK);
117 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
118 ir_graph * irg = get_irp_irg(i);
119 entity * ent = get_irg_entity(irg);
120 /* Removing any graph invalidates all interprocedural loop trees. */
121 if (get_irg_loopinfo_state(irg) == loopinfo_ip_consistent ||
122 get_irg_loopinfo_state(irg) == loopinfo_ip_inconsistent) {
123 free_loop_information(irg);
125 if ((get_entity_visibility(ent) == visibility_local) && (get_entity_link(ent) != MARK)) {
127 set_entity_peculiarity(ent, peculiarity_description);
128 if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 1) {
129 printf("dead method elimination: freeing method %s\n", get_entity_ld_name(ent));
132 set_entity_link(ent, NULL);