remove #if 1
[libfirm] / ir / opt / opt_frame.c
1 /*
2  * Copyright (C) 1995-2011 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  * @brief
26  *   Optimize the frame type by removing unused type members.
27  */
28 #include "config.h"
29
30 #include "iroptimize.h"
31 #include "irgraph_t.h"
32 #include "type_t.h"
33 #include "irouts.h"
34 #include "iredges.h"
35 #include "irpass.h"
36
37 /*
38  * Optimize the frame type of an irg by removing
39  * never touched entities.
40  */
41 void opt_frame_irg(ir_graph *irg)
42 {
43         ir_type   *frame_tp = get_irg_frame_type(irg);
44         ir_entity *ent, *list;
45         ir_node   *frame, *sel;
46         size_t    i, n = get_class_n_members(frame_tp);
47         int       o;
48
49         if (n <= 0)
50                 return;
51
52         assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
53
54         irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
55
56         /* clear all entity links */
57         for (i = n; i > 0;) {
58                 ent = get_class_member(frame_tp, --i);
59                 set_entity_link(ent, NULL);
60         }
61
62         /* look for uses */
63         frame = get_irg_frame(irg);
64
65         /* mark all used entities */
66         for (o = get_irn_n_outs(frame) - 1; o >= 0; --o) {
67                 sel = get_irn_out(frame, o);
68                 if (is_Sel(sel)) {
69                         ent = get_Sel_entity(sel);
70                         /* only entities on the frame */
71                         if (get_entity_owner(ent) == frame_tp)
72                                 set_entity_link(ent, ent);
73                 }
74         }
75
76         /* link unused ones */
77         list = NULL;
78         for (i = n; i > 0;) {
79                 ent = get_class_member(frame_tp, --i);
80                 /* beware of inner functions: those are NOT unused */
81                 if (get_entity_link(ent) == NULL && !is_method_entity(ent)) {
82                         set_entity_link(ent, list);
83                         list = ent;
84                 }
85         }
86
87         if (list != NULL) {
88                 /* delete list members */
89                 for (ent = list; ent; ent = list) {
90                         list = (ir_entity*)get_entity_link(ent);
91                         free_entity(ent);
92                 }
93                 /* we changed the frame type, its layout should be redefined */
94                 set_type_state(frame_tp, layout_undefined);
95         }
96         irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
97
98         /* we changed the type, this affects none of the currently known graph
99          * properties, but I don't use ALL because I don't know if someone adds
100          * type-based properties at some point */
101         confirm_irg_properties(irg,
102                 IR_GRAPH_PROPERTIES_CONTROL_FLOW
103                 | IR_GRAPH_PROPERTY_NO_BADS
104                 | IR_GRAPH_PROPERTY_NO_TUPLES
105                 | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
106                 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS
107                 | IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE
108                 | IR_GRAPH_PROPERTY_MANY_RETURNS);
109 }
110
111 ir_graph_pass_t *opt_frame_irg_pass(const char *name)
112 {
113         return def_graph_pass(name ? name : "opt_frame_irg", opt_frame_irg);
114 }