updated
[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  * @summary
27  *   Optimize the frame type by removing unused type members.
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "iroptimize.h"
34 #include "irgraph_t.h"
35 #include "type_t.h"
36 #include "irouts.h"
37 #include "iredges.h"
38
39 /*
40  * Optimize the frame type of an irg by removing
41  * never touched entities.
42  */
43 void opt_frame_irg(ir_graph *irg) {
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         /* clear all entity links */
53         for (i = n - 1; i >= 0; --i) {
54                 ent = get_class_member(frame_tp, i);
55                 set_entity_link(ent, NULL);
56         }
57
58         /* look for uses */
59         frame = get_irg_frame(irg);
60
61         if (edges_activated(irg)) { /* use inplace edges */
62                 const ir_edge_t *edge;
63
64                 /* mark all used entities */
65                 foreach_out_edge(frame, edge) {
66                         sel = get_edge_src_irn(edge);
67                         ent = get_Sel_entity(sel);
68                         set_entity_link(ent, ent);
69                 }
70         }
71         else {
72                 /* use traditionally out edges */
73                 if (get_irg_outs_state(irg) != outs_consistent)
74                         compute_irg_outs(irg);
75
76                 /* mark all used entities */
77                 for (i = get_irn_n_outs(frame) - 1; i >= 0; --i) {
78                         sel = get_irn_out(frame, i);
79                         ent = get_Sel_entity(sel);
80                         set_entity_link(ent, ent);
81                 }
82         }
83
84         /* link unused ones */
85         list = NULL;
86         for (i = n - 1; i >= 0; --i) {
87                 ent = get_class_member(frame_tp, i);
88                 if (get_entity_link(ent) == NULL) {
89                         set_entity_link(ent, list);
90                         list = ent;
91                 }
92         }
93
94         if (list != NULL) {
95                 /* delete list members */
96                 for (ent = list; ent; ent = list) {
97                         list = get_entity_link(ent);
98                         remove_class_member(frame_tp, ent);
99                 }
100                 /* we changed the frame type, it's layout should be redefined */
101                 set_type_state(frame_tp, layout_undefined);
102         }
103 }