hook_replace() added to exchange
[libfirm] / ir / ir / ircgopt.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/ircgopt.c
4  * Purpose:     Removal of unreachable methods.
5  * Author:      Hubert Schmid
6  * Modified by:
7  * Created:     09.06.2002
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * Entfernen von nicht erreichbaren (aufrufbaren) Methoden. Die Menge
15  * der nicht erreichbaren Methoden wird aus der Abschätzung der
16  * Aufrufrelation bestimmt.
17  */
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "ircgopt.h"
23
24 #include "array.h"
25 #include "irprog.h"
26 #include "irgwalk.h"
27 #include "irloop_t.h"
28 #include "irflag_t.h"
29 #include "ircons.h"
30 #include "typewalk.h"
31 #include "irtools.h"
32
33 /* Call-Operationen an die "link"-Liste von "call_head" anhängen. */
34 static void collect_call(ir_node * node, ir_node * head) {
35   if (get_irn_op(node) == op_Call) {
36     set_irn_link(node, get_irn_link(head));
37     set_irn_link(head, node);
38   }
39 }
40
41 static void make_entity_to_description(type_or_ent *tore, void *env) {
42   if (get_kind(tore) == k_entity) {
43     entity *ent = (entity *)tore;
44
45     if ((is_Method_type(get_entity_type(ent)))                        &&
46         (get_entity_peculiarity(ent) != peculiarity_description)      &&
47         (get_entity_visibility(ent)  != visibility_external_allocated)   ) {
48       entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
49       if (get_entity_link(impl) != env) {
50         set_entity_peculiarity(ent, peculiarity_description);
51       }
52     }
53   }
54 }
55
56 /* garbage collect methods: mark and remove */
57 void gc_irgs(int n_keep, entity ** keep_arr) {
58   void * MARK = &MARK; /* @@@ gefaehrlich!!! Aber wir markieren hoechstens zu viele ... */
59   int i;
60
61   if (!get_opt_dead_method_elimination()) return;
62
63   /* Mark entities that are alive.  */
64   if (n_keep > 0) {
65     entity ** marked = NEW_ARR_F(entity *, n_keep);
66     for (i = 0; i < n_keep; ++i) {
67       marked[i] = keep_arr[i];
68       set_entity_link(marked[i], MARK);
69       if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
70         printf("dead method elimination: method %s kept alive.\n", get_entity_ld_name(marked[i]));
71       }
72     }
73     for (i = 0; i < ARR_LEN(marked); ++i) {
74       /* check for extern methods, these don't have an IRG */
75       if (get_entity_visibility(marked[i]) != visibility_external_allocated) {
76         ir_graph * irg = get_entity_irg(marked[i]);
77         ir_node * node = get_irg_end(irg);
78         /* collect calls */
79         irg_walk_graph(irg, firm_clear_link, (irg_walk_func *) collect_call, node);
80         /* iterate calls */
81         for (node = get_irn_link(node); node; node = get_irn_link(node)) {
82           int i;
83           assert(get_irn_op(node) == op_Call);
84           for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
85             entity * ent = get_Call_callee(node, i);
86             if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
87               set_entity_link(ent, MARK);
88               ARR_APP1(entity *, marked, ent);
89               if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
90                 printf("dead method elimination: method %s can be called from Call %ld: kept alive.\n",
91                        get_entity_ld_name(ent), get_irn_node_nr(node));
92               }
93             }
94           }
95         }
96       }
97     }
98     DEL_ARR_F(marked);
99   }
100
101   /* clean */
102   type_walk(make_entity_to_description, NULL, MARK);
103   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
104     ir_graph * irg = get_irp_irg(i);
105     entity * ent = get_irg_entity(irg);
106     /* Removing any graph invalidates all interprocedural loop trees. */
107     if (get_irg_loopinfo_state(irg) == loopinfo_ip_consistent ||
108         get_irg_loopinfo_state(irg) == loopinfo_ip_inconsistent) {
109       free_loop_information(irg);
110     }
111     if ((get_entity_visibility(ent) == visibility_local) && (get_entity_link(ent) != MARK)) {
112       remove_irp_irg(irg);
113       set_entity_peculiarity(ent, peculiarity_description);
114       if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 1) {
115         printf("dead method elimination: freeing method %s\n", get_entity_ld_name(ent));
116       }
117     }
118     set_entity_link(ent, NULL);
119   }
120 }