added new licence header
[libfirm] / ir / opt / opt_frame.c
1 /*
2  * Copyright (C) 1995-2007 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  * Project:     libFIRM
22  * File name:   ir/opt/opt_frame.c
23  * Purpose:     optimize the frame type
24  * Author:      Michael Beck
25  * Created:     15.03.2006
26  * CVS-ID:      $Id$
27  * Copyright:   (c) 1998-2006 Universität Karlsruhe
28  */
29
30 /**
31  * @file opt_frame.c
32  *
33  * Optimize the frame type by removing unused type members.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "irgraph_t.h"
41 #include "type_t.h"
42 #include "irouts.h"
43 #include "iredges.h"
44 #include "opt_frame.h"
45
46 /*
47  * Optimize the frame type of an irg by removing
48  * never touched entities.
49  */
50 void opt_frame_irg(ir_graph *irg) {
51   ir_type   *frame_tp = get_irg_frame_type(irg);
52   ir_entity *ent, *list;
53   ir_node   *frame, *sel;
54   int       i, n = get_class_n_members(frame_tp);
55
56   assert(get_irg_phase_state(irg) == phase_high && "Graph must be in high phase");
57   if (n <= 0)
58     return;
59
60   /* clear all entity links */
61   for (i = n - 1; i >= 0; --i) {
62     ent = get_class_member(frame_tp, i);
63     set_entity_link(ent, NULL);
64   }
65
66   /* look for uses */
67   frame = get_irg_frame(irg);
68
69   if (edges_activated(irg)) { /* use inplace edges */
70     const ir_edge_t *edge;
71
72     /* mark all used entities */
73     foreach_out_edge(frame, edge) {
74       sel = get_edge_src_irn(edge);
75       ent = get_Sel_entity(sel);
76       set_entity_link(ent, ent);
77     }
78   }
79   else {
80     /* use traditionally out edges */
81     if (get_irg_outs_state(irg) != outs_consistent)
82       compute_irg_outs(irg);
83
84     /* mark all used entities */
85     for (i = get_irn_n_outs(frame) - 1; i >= 0; --i) {
86       sel = get_irn_out(frame, i);
87       ent = get_Sel_entity(sel);
88       set_entity_link(ent, ent);
89     }
90   }
91
92   /* link unused ones */
93   list = NULL;
94   for (i = n - 1; i >= 0; --i) {
95     ent = get_class_member(frame_tp, i);
96     if (get_entity_link(ent) == NULL) {
97       set_entity_link(ent, list);
98       list = ent;
99     }
100   }
101
102   if (list) {
103     /* delete list members */
104     for (ent = list; ent; ent = list) {
105       list = get_entity_link(ent);
106       remove_class_member(frame_tp, ent);
107     }
108     /* we changed the frame type, it's layout should be redefined */
109     set_type_state(frame_tp, layout_undefined);
110   }
111 }