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  * Project:     libFIRM
22  * File name:   ir/opt/opt_polymorphy
23  * Purpose:     Optimize polymorphic Sel nodes.
24  * Author:      Goetz Lindenmaier, Michael Beck
25  * Created:
26  * CVS-ID:      $Id$
27  * Copyright:   (c) 2005 Universität Karlsruhe
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "irprog_t.h"
34 #include "entity_t.h"
35 #include "type_t.h"
36 #include "irop.h"
37 #include "irnode_t.h"
38 #include "ircons.h"
39
40 #include "iropt_dbg.h"
41 #include "irflag_t.h"
42
43 /**
44  * Checks if a graph allocates new memory and returns the
45  * type of the newly allocated entity.
46  * Returns NULL if the graph did not represent an Allocation.
47  *
48  * The default implementation hecks for Alloc nodes only.
49  */
50 ir_type *default_firm_get_Alloc(ir_node *n) {
51   n = skip_Proj(n);
52   if (get_irn_op(n) == op_Alloc) {
53     return get_Alloc_type(n);
54   }
55   return NULL;
56 }
57
58 typedef ir_type *(*get_Alloc_func)(ir_node *n);
59
60 /** The get_Alloc function */
61 static get_Alloc_func firm_get_Alloc = default_firm_get_Alloc;
62
63 /** Set a new get_Alloc_func and returns the old one. */
64 get_Alloc_func firm_set_Alloc_func(get_Alloc_func newf) {
65   get_Alloc_func old = firm_get_Alloc;
66   firm_get_Alloc = newf;
67   return old;
68 }
69
70 /** Return dynamic type of ptr.
71  *
72  * If we can deduct the dynamic type from the firm nodes
73  * by a limited test we return the dynamic type.  Else
74  * we return unknown_type.
75  *
76  * If we find a dynamic type this means that the pointer always points
77  * to an object of this type during runtime.   We resolved polymorphy.
78  */
79 static ir_type *get_dynamic_type(ir_node *ptr) {
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   if (is_entity_final(ent)) {
107     /* not possible to override this entity. */
108     return 1;
109   }
110   if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
111     /* we have a closed world, so simply check how often it was
112        overridden. */
113     return 1;
114   }
115   return 0;
116 }
117
118 /*
119  * Transform Sel[method] to SymC[method] if possible.
120  */
121 ir_node *transform_node_Sel(ir_node *node)
122 {
123   ir_node   *new_node, *ptr;
124   ir_type   *dyn_tp;
125   ir_entity *ent = get_Sel_entity(node);
126
127   if (get_irp_phase_state() == phase_building) return node;
128
129   if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
130     return node;
131
132   if (!is_Method_type(get_entity_type(ent)))
133     return node;
134
135   /* If the entity is a leave in the inheritance tree,
136      we can replace the Sel by a constant. */
137   if (is_final_ent(ent)) {
138     /* In dead code, we might call a leave entity that is a description.
139        Do not turn the Sel to a SymConst. */
140     if (get_entity_peculiarity(ent) == peculiarity_description) {
141       /* We could remove the Call depending on this Sel. */
142       new_node = node;
143     } else {
144       ir_node *rem_block = get_cur_block();
145       set_cur_block(get_nodes_block(node));
146       new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
147       set_cur_block(rem_block);
148       DBG_OPT_POLY(node, new_node);
149     }
150     return new_node;
151   }
152
153   /* If we know the dynamic type, we can replace the Sel by a constant. */
154   ptr = get_Sel_ptr(node);      /* The address we select from. */
155   dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
156
157   if (dyn_tp != firm_unknown_type) {
158     ir_entity *called_ent;
159     ir_node *rem_block;
160
161     /* We know which method will be called, no dispatch necessary. */
162     called_ent = resolve_ent_polymorphy(dyn_tp, ent);
163     /* called_ent may not be description: has no Address/Const to Call! */
164     assert(get_entity_peculiarity(called_ent) != peculiarity_description);
165
166     rem_block = get_cur_block();
167     set_cur_block(get_nodes_block(node));
168     new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
169     set_cur_block(rem_block);
170     DBG_OPT_POLY(node, new_node);
171
172     return new_node;
173   }
174
175   return node;
176 }
177
178 /* Transform  Load(Sel(Alloc)[constant static entity])
179  *  to Const[constant static entity value].
180  *
181  *  This function returns a node replacing the Proj(Load)[Value].
182  *  If this is actually called in transform_node, we must build
183  *  a tuple, or replace the Projs of the load.
184  *  Therefore we call this optimization in ldstopt().
185  */
186 ir_node *transform_node_Load(ir_node *n)
187 {
188   ir_node *field_ptr, *new_node, *ptr;
189   ir_entity *ent;
190   ir_type *dyn_tp;
191
192   if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
193     return n;
194
195   field_ptr = get_Load_ptr(n);
196
197   if (! is_Sel(field_ptr)) return n;
198
199   ent = get_Sel_entity(field_ptr);
200   if ((get_entity_allocation(ent) != allocation_static)    ||
201       (get_entity_variability(ent) != variability_constant)  )
202     return n;
203
204   /* If the entity is a leave in the inheritance tree,
205      we can replace the Sel by a constant. */
206   if ((get_irp_phase_state() != phase_building) && is_final_ent(ent)) {
207     new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(ent));
208     DBG_OPT_POLY(field_ptr, new_node);
209
210     return new_node;
211   }
212
213   /* If we know the dynamic type, we can replace the Sel by a constant. */
214   ptr = get_Sel_ptr(field_ptr);    /* The address we select from. */
215   dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
216
217   if (dyn_tp != firm_unknown_type) {
218     ir_entity *loaded_ent;
219
220     /* We know which method will be called, no dispatch necessary. */
221     loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
222     /* called_ent may not be description: has no Address/Const to Call! */
223     assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
224
225     new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(loaded_ent));
226     DBG_OPT_POLY(field_ptr, new_node);
227
228     return new_node;
229   }
230
231   return n;
232 }