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