When checking PhiM's, ignore Bad predecessors
[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 /**
34  * Walker: adds Call operations to a head's link list.
35  */
36 static void collect_call(ir_node * node, void *env) {
37   ir_node *head = env;
38
39   if (get_irn_op(node) == op_Call) {
40     set_irn_link(node, get_irn_link(head));
41     set_irn_link(head, node);
42   }
43 }
44
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;
48
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       }
56     }
57   }
58 }
59
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 ... */
63   int i;
64
65   if (!get_opt_dead_method_elimination()) return;
66
67   /* Mark entities that are alive.  */
68   if (n_keep > 0) {
69     entity ** marked = NEW_ARR_F(entity *, n_keep);
70     for (i = 0; i < n_keep; ++i) {
71       marked[i] = keep_arr[i];
72       set_entity_link(marked[i], MARK);
73       if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
74         printf("dead method elimination: method %s kept alive.\n", get_entity_ld_name(marked[i]));
75       }
76     }
77
78     for (i = 0; i < ARR_LEN(marked); ++i) {
79       /* check for extern methods, these don't have an IRG */
80       if (get_entity_visibility(marked[i]) != visibility_external_allocated) {
81         ir_graph * irg = get_entity_irg(marked[i]);
82         ir_node * node = get_irg_end(irg);
83
84         /* collect calls */
85         irg_walk_graph(irg, firm_clear_link, collect_call, node);
86
87         /* iterate calls */
88         for (node = get_irn_link(node); node; node = get_irn_link(node)) {
89           int i;
90           assert(get_irn_op(node) == op_Call);
91
92           for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
93             entity * ent = get_Call_callee(node, i);
94
95             if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
96               set_entity_link(ent, MARK);
97               ARR_APP1(entity *, marked, ent);
98               if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
99                 printf("dead method elimination: method %s can be called from Call %ld: kept alive.\n",
100                        get_entity_ld_name(ent), get_irn_node_nr(node));
101               }
102             }
103           }
104         }
105       }
106     }
107     DEL_ARR_F(marked);
108   }
109
110   /* clean */
111   type_walk(make_entity_to_description, NULL, MARK);
112   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
113     ir_graph * irg = get_irp_irg(i);
114     entity * ent = get_irg_entity(irg);
115     /* Removing any graph invalidates all interprocedural loop trees. */
116     if (get_irg_loopinfo_state(irg) == loopinfo_ip_consistent ||
117         get_irg_loopinfo_state(irg) == loopinfo_ip_inconsistent) {
118       free_loop_information(irg);
119     }
120     if ((get_entity_visibility(ent) == visibility_local) && (get_entity_link(ent) != MARK)) {
121       remove_irp_irg(irg);
122       set_entity_peculiarity(ent, peculiarity_description);
123       if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 1) {
124         printf("dead method elimination: freeing method %s\n", get_entity_ld_name(ent));
125       }
126     }
127     set_entity_link(ent, NULL);
128   }
129 }