fix trailing whitespaces and tabulators in the middle of a line
[libfirm] / ir / opt / opt_polymorphy.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 polymorphic Sel and Load nodes.
23  * @author  Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  * @brief
26  *  This file subsumes optimization code from cgana.
27  */
28 #include "config.h"
29
30 #include "opt_polymorphy.h"
31 #include "iroptimize.h"
32 #include "irprog_t.h"
33 #include "entity_t.h"
34 #include "type_t.h"
35 #include "irop.h"
36 #include "irnode_t.h"
37 #include "ircons.h"
38
39 #include "iropt_dbg.h"
40 #include "irflag_t.h"
41
42 /**
43  * Checks if a graph allocates new memory and returns the
44  * type of the newly allocated entity.
45  * Returns NULL if the graph did not represent an Allocation.
46  *
47  * The default implementation hecks for Alloc nodes only.
48  */
49 static ir_type *default_firm_get_Alloc(ir_node *n)
50 {
51         n = skip_Proj(n);
52         if (is_Alloc(n)) {
53                 return get_Alloc_type(n);
54         }
55         return NULL;
56 }
57
58 /** The get_Alloc function */
59 static get_Alloc_func firm_get_Alloc = default_firm_get_Alloc;
60
61 /** Set a new get_Alloc_func and returns the old one. */
62 get_Alloc_func firm_set_Alloc_func(get_Alloc_func newf)
63 {
64         get_Alloc_func old = firm_get_Alloc;
65         firm_get_Alloc = newf;
66         return old;
67 }
68
69 /** Return dynamic type of ptr.
70  *
71  * If we can deduct the dynamic type from the firm nodes
72  * by a limited test we return the dynamic type.  Else
73  * we return unknown_type.
74  *
75  * If we find a dynamic type this means that the pointer always points
76  * to an object of this type during runtime.   We resolved polymorphy.
77  */
78 static ir_type *get_dynamic_type(ir_node *ptr)
79 {
80         ir_type *tp;
81
82         /* skip Cast and Confirm nodes */
83         for (;;) {
84                 ir_opcode code = get_irn_opcode(ptr);
85
86                 switch (code) {
87                 case iro_Cast:
88                         ptr = get_Cast_op(ptr);
89                         continue;
90                 case iro_Confirm:
91                         ptr = get_Confirm_value(ptr);
92                         continue;
93                 default:
94                         ;
95                 }
96                 break;
97         }
98         tp = (*firm_get_Alloc)(ptr);
99         return tp ? tp : firm_unknown_type;
100 }
101
102 /**
103  * Check, if an entity is final, i.e. is not anymore overridden.
104  */
105 static int is_final_ent(ir_entity *ent)
106 {
107         if (is_entity_final(ent)) {
108                 /* not possible to override this entity. */
109                 return 1;
110         }
111         if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
112                 /* we have a closed world, so simply check how often it was
113                 overridden. */
114                 return 1;
115         }
116         return 0;
117 }
118
119 /*
120  * Transform Sel[method] to SymC[method] if possible.
121  */
122 ir_node *transform_node_Sel(ir_node *node)
123 {
124         ir_node   *new_node, *ptr;
125         ir_type   *dyn_tp;
126         ir_entity *ent = get_Sel_entity(node);
127
128         if (get_irp_phase_state() == phase_building) return node;
129
130         if (!get_opt_dyn_meth_dispatch())
131                 return node;
132
133         if (!is_Method_type(get_entity_type(ent)))
134                 return node;
135
136         /* If the entity is a leave in the inheritance tree,
137            we can replace the Sel by a constant. */
138         if (is_final_ent(ent)) {
139                 /* In dead code, we might call a leave entity that is a description.
140                    Do not turn the Sel to a SymConst. */
141                 if (get_entity_peculiarity(ent) == peculiarity_description) {
142                         /* We could remove the Call depending on this Sel. */
143                         new_node = node;
144                 } else {
145                         ir_node *rem_block = get_cur_block();
146                         set_cur_block(get_nodes_block(node));
147                         new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
148                         set_cur_block(rem_block);
149                         DBG_OPT_POLY(node, new_node);
150                 }
151                 return new_node;
152         }
153
154         /* If we know the dynamic type, we can replace the Sel by a constant. */
155         ptr = get_Sel_ptr(node);      /* The address we select from. */
156         dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
157
158         if (dyn_tp != firm_unknown_type) {
159                 ir_entity *called_ent;
160                 ir_node *rem_block;
161
162                 /* We know which method will be called, no dispatch necessary. */
163                 called_ent = resolve_ent_polymorphy(dyn_tp, ent);
164
165                 rem_block = get_cur_block();
166                 set_cur_block(get_nodes_block(node));
167                 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
168                 set_cur_block(rem_block);
169                 DBG_OPT_POLY(node, new_node);
170
171                 return new_node;
172         }
173
174         return node;
175 }
176
177 /* Transform  Load(Sel(Alloc)[constant static entity])
178  *  to Const[constant static entity value].
179  *
180  *  This function returns a node replacing the Proj(Load)[Value].
181  *  If this is actually called in transform_node, we must build
182  *  a tuple, or replace the Projs of the load.
183  *  Therefore we call this optimization in ldstopt().
184  */
185 ir_node *transform_polymorph_Load(ir_node *load)
186 {
187         ir_node *new_node = NULL;
188         ir_node *field_ptr, *ptr;
189         ir_entity *ent;
190         ir_type *dyn_tp;
191
192         if (!get_opt_dyn_meth_dispatch())
193                 return load;
194
195         field_ptr = get_Load_ptr(load);
196
197         if (! is_Sel(field_ptr)) return load;
198
199         ent = get_Sel_entity(field_ptr);
200         if ( !(get_entity_linkage(ent) & IR_LINKAGE_CONSTANT) )
201                 return load;
202
203         /* If the entity is a leave in the inheritance tree,
204            we can replace the Sel by a constant. */
205         if ((get_irp_phase_state() != phase_building) && is_final_ent(ent)) {
206                 new_node = get_atomic_ent_value(ent);
207         } else {
208                 /* If we know the dynamic type, we can replace the Sel by a constant. */
209                 ptr = get_Sel_ptr(field_ptr);    /* The address we select from. */
210                 dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
211
212                 if (dyn_tp != firm_unknown_type) {
213                         ir_entity *loaded_ent;
214
215                         /* We know which method will be called, no dispatch necessary. */
216                         loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
217                         new_node = get_atomic_ent_value(loaded_ent);
218                 }
219         }
220         if (new_node != NULL) {
221                 new_node = can_replace_load_by_const(load, new_node);
222                 if (new_node != NULL) {
223                         DBG_OPT_POLY(field_ptr, new_node);
224
225                         return new_node;
226                 }
227         }
228         return load;
229 }