Updated header
[libfirm] / ir / opt / opt_polymorphy.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  * @file
22  * @brief   Optimize polymorphic Sel and Load nodes.
23  * @author  Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  * @summary
26  *  This file subsumes optimization code from cgana.
27  */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
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 ir_type *default_firm_get_Alloc(ir_node *n) {
50         n = skip_Proj(n);
51         if (get_irn_op(n) == op_Alloc) {
52                 return get_Alloc_type(n);
53         }
54         return NULL;
55 }
56
57 typedef ir_type *(*get_Alloc_func)(ir_node *n);
58
59 /** The get_Alloc function */
60 static get_Alloc_func firm_get_Alloc = default_firm_get_Alloc;
61
62 /** Set a new get_Alloc_func and returns the old one. */
63 get_Alloc_func firm_set_Alloc_func(get_Alloc_func newf) {
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         ir_type *tp;
80
81         /* skip Cast and Confirm nodes */
82         for (;;) {
83                 ir_opcode code = get_irn_opcode(ptr);
84
85                 switch (code) {
86                 case iro_Cast:
87                         ptr = get_Cast_op(ptr);
88                         continue;
89                 case iro_Confirm:
90                         ptr = get_Confirm_value(ptr);
91                         continue;
92                 default:
93                         ;
94                 }
95                 break;
96         }
97         tp = (*firm_get_Alloc)(ptr);
98         return tp ? tp : firm_unknown_type;
99 }
100
101 /**
102  * Check, if an entity is final, i.e. is not anymore overridden.
103  */
104 static int is_final_ent(ir_entity *ent) {
105         if (is_entity_final(ent)) {
106                 /* not possible to override this entity. */
107                 return 1;
108         }
109         if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
110                 /* we have a closed world, so simply check how often it was
111                 overridden. */
112                 return 1;
113         }
114         return 0;
115 }
116
117 /*
118  * Transform Sel[method] to SymC[method] if possible.
119  */
120 ir_node *transform_node_Sel(ir_node *node) {
121         ir_node   *new_node, *ptr;
122         ir_type   *dyn_tp;
123         ir_entity *ent = get_Sel_entity(node);
124
125         if (get_irp_phase_state() == phase_building) return node;
126
127         if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
128                 return node;
129
130         if (!is_Method_type(get_entity_type(ent)))
131                 return node;
132
133         /* If the entity is a leave in the inheritance tree,
134            we can replace the Sel by a constant. */
135         if (is_final_ent(ent)) {
136                 /* In dead code, we might call a leave entity that is a description.
137                    Do not turn the Sel to a SymConst. */
138                 if (get_entity_peculiarity(ent) == peculiarity_description) {
139                         /* We could remove the Call depending on this Sel. */
140                         new_node = node;
141                 } else {
142                         ir_node *rem_block = get_cur_block();
143                         set_cur_block(get_nodes_block(node));
144                         new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
145                         set_cur_block(rem_block);
146                         DBG_OPT_POLY(node, new_node);
147                 }
148                 return new_node;
149         }
150
151         /* If we know the dynamic type, we can replace the Sel by a constant. */
152         ptr = get_Sel_ptr(node);      /* The address we select from. */
153         dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
154
155         if (dyn_tp != firm_unknown_type) {
156                 ir_entity *called_ent;
157                 ir_node *rem_block;
158
159                 /* We know which method will be called, no dispatch necessary. */
160                 called_ent = resolve_ent_polymorphy(dyn_tp, ent);
161                 /* called_ent may not be description: has no Address/Const to Call! */
162                 assert(get_entity_peculiarity(called_ent) != peculiarity_description);
163
164                 rem_block = get_cur_block();
165                 set_cur_block(get_nodes_block(node));
166                 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
167                 set_cur_block(rem_block);
168                 DBG_OPT_POLY(node, new_node);
169
170                 return new_node;
171         }
172
173         return node;
174 }
175
176 /* Transform  Load(Sel(Alloc)[constant static entity])
177  *  to Const[constant static entity value].
178  *
179  *  This function returns a node replacing the Proj(Load)[Value].
180  *  If this is actually called in transform_node, we must build
181  *  a tuple, or replace the Projs of the load.
182  *  Therefore we call this optimization in ldstopt().
183  */
184 ir_node *transform_node_Load(ir_node *n) {
185         ir_node *field_ptr, *new_node, *ptr;
186         ir_entity *ent;
187         ir_type *dyn_tp;
188
189         if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
190                 return n;
191
192         field_ptr = get_Load_ptr(n);
193
194         if (! is_Sel(field_ptr)) return n;
195
196         ent = get_Sel_entity(field_ptr);
197         if ((get_entity_allocation(ent) != allocation_static)    ||
198                 (get_entity_variability(ent) != variability_constant)  )
199                 return n;
200
201         /* If the entity is a leave in the inheritance tree,
202            we can replace the Sel by a constant. */
203         if ((get_irp_phase_state() != phase_building) && is_final_ent(ent)) {
204                 new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(ent));
205                 DBG_OPT_POLY(field_ptr, new_node);
206
207                 return new_node;
208         }
209
210         /* If we know the dynamic type, we can replace the Sel by a constant. */
211         ptr = get_Sel_ptr(field_ptr);    /* The address we select from. */
212         dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
213
214         if (dyn_tp != firm_unknown_type) {
215                 ir_entity *loaded_ent;
216
217                 /* We know which method will be called, no dispatch necessary. */
218                 loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
219                 /* called_ent may not be description: has no Address/Const to Call! */
220                 assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
221
222                 new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(loaded_ent));
223                 DBG_OPT_POLY(field_ptr, new_node);
224
225                 return new_node;
226         }
227
228         return n;
229 }