ensure that the end block is always the last in the block schedule
[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               //set_atomic_ent_value(ent, new_r_Const(get_const_code_irg(), get_irg_start_block(get_const_code_irg()),
52               //                                      mode_P, get_tarval_null(mode_P)));
53       }
54     }
55   }
56 }
57
58 /* garbage collect methods: mark and remove */
59 void gc_irgs(int n_keep, entity ** keep_arr) {
60   void * MARK = &MARK; /* @@@ gefaehrlich!!! Aber wir markieren hoechstens zu viele ... */
61   int i;
62
63   if (!get_opt_dead_method_elimination()) return;
64
65   /* Mark entities that are alive.  */
66   if (n_keep > 0) {
67     entity ** marked = NEW_ARR_F(entity *, n_keep);
68     for (i = 0; i < n_keep; ++i) {
69       marked[i] = keep_arr[i];
70       set_entity_link(marked[i], MARK);
71       if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
72         printf("dead method elimination: method %s kept alive.\n", get_entity_ld_name(marked[i]));
73       }
74     }
75     for (i = 0; i < ARR_LEN(marked); ++i) {
76       /* check for extern methods, these don't have an IRG */
77       if (get_entity_visibility(marked[i]) != visibility_external_allocated) {
78         ir_graph * irg = get_entity_irg(marked[i]);
79         ir_node * node = get_irg_end(irg);
80         /* collect calls */
81         irg_walk_graph(irg, firm_clear_link, (irg_walk_func *) collect_call, node);
82         /* iterate calls */
83         for (node = get_irn_link(node); node; node = get_irn_link(node)) {
84           int i;
85           assert(get_irn_op(node) == op_Call);
86           for (i = get_Call_n_callees(node) - 1; i >= 0; --i) {
87             entity * ent = get_Call_callee(node, i);
88             if (get_entity_irg(ent) && get_entity_link(ent) != MARK) {
89               set_entity_link(ent, MARK);
90               ARR_APP1(entity *, marked, ent);
91               if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 2) {
92                 printf("dead method elimination: method %s can be called from Call %ld: kept alive.\n",
93                        get_entity_ld_name(ent), get_irn_node_nr(node));
94               }
95             }
96           }
97         }
98       }
99     }
100     DEL_ARR_F(marked);
101   }
102
103   /* clean */
104   type_walk(make_entity_to_description, NULL, MARK);
105   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
106     ir_graph * irg = get_irp_irg(i);
107     entity * ent = get_irg_entity(irg);
108     /* Removing any graph invalidates all interprocedural loop trees. */
109     if (get_irg_loopinfo_state(irg) == loopinfo_ip_consistent ||
110         get_irg_loopinfo_state(irg) == loopinfo_ip_inconsistent) {
111       free_loop_information(irg);
112     }
113     if ((get_entity_visibility(ent) == visibility_local) && (get_entity_link(ent) != MARK)) {
114       remove_irp_irg(irg);
115       set_entity_peculiarity(ent, peculiarity_description);
116       if (get_opt_dead_method_elimination_verbose() && get_firm_verbosity() > 1) {
117         printf("dead method elimination: freeing method %s\n", get_entity_ld_name(ent));
118       }
119     }
120     set_entity_link(ent, NULL);
121   }
122 }