simplify confusing entity/owner interfaces. There is no public way anymore to add...
[libfirm] / ir / opt / opt_frame.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Optimize the frame type.
23  * @date    15.03.2006
24  * @author  Michael Beck
25  * @version $Id$
26  * @brief
27  *   Optimize the frame type by removing unused type members.
28  */
29 #include "config.h"
30
31 #include "iroptimize.h"
32 #include "irgraph_t.h"
33 #include "type_t.h"
34 #include "irouts.h"
35 #include "iredges.h"
36 #include "irpass.h"
37
38 /*
39  * Optimize the frame type of an irg by removing
40  * never touched entities.
41  */
42 void opt_frame_irg(ir_graph *irg)
43 {
44         ir_type   *frame_tp = get_irg_frame_type(irg);
45         ir_entity *ent, *list;
46         ir_node   *frame, *sel;
47         int       i, n = get_class_n_members(frame_tp);
48
49         if (n <= 0)
50                 return;
51
52         irp_reserve_resources(irp, IR_RESOURCE_ENTITY_LINK);
53
54         /* clear all entity links */
55         for (i = n - 1; i >= 0; --i) {
56                 ent = get_class_member(frame_tp, i);
57                 set_entity_link(ent, NULL);
58         }
59
60         /* look for uses */
61         frame = get_irg_frame(irg);
62
63         if (edges_activated(irg)) { /* use inplace edges */
64                 const ir_edge_t *edge;
65
66                 /* mark all used entities */
67                 foreach_out_edge(frame, edge) {
68                         sel = get_edge_src_irn(edge);
69                         if (is_Sel(sel)) {
70                                 ent = get_Sel_entity(sel);
71                                 set_entity_link(ent, ent);
72                         }
73                 }
74         } else {
75                 /* use traditionally out edges */
76                 assure_irg_outs(irg);
77
78                 /* mark all used entities */
79                 for (i = get_irn_n_outs(frame) - 1; i >= 0; --i) {
80                         sel = get_irn_out(frame, i);
81                         if (is_Sel(sel)) {
82                                 ent = get_Sel_entity(sel);
83                                 /* only entities on the frame */
84                                 if (get_entity_owner(ent) == frame_tp)
85                                         set_entity_link(ent, ent);
86                         }
87                 }
88         }
89
90         /* link unused ones */
91         list = NULL;
92         for (i = n - 1; i >= 0; --i) {
93                 ent = get_class_member(frame_tp, i);
94                 /* beware of inner functions: those are NOT unused */
95                 if (get_entity_link(ent) == NULL && !is_method_entity(ent)) {
96                         set_entity_link(ent, list);
97                         list = ent;
98                 }
99         }
100
101         if (list != NULL) {
102                 /* delete list members */
103                 for (ent = list; ent; ent = list) {
104                         list = get_entity_link(ent);
105                         free_entity(ent);
106                 }
107                 /* we changed the frame type, it's layout should be redefined */
108                 set_type_state(frame_tp, layout_undefined);
109         }
110         irp_free_resources(irp, IR_RESOURCE_ENTITY_LINK);
111 }
112
113 ir_graph_pass_t *opt_frame_irg_pass(const char *name)
114 {
115         return def_graph_pass(name ? name : "opt_frame_irg", opt_frame_irg);
116 }