condeval is called jump threading now
[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  * @summary
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 typedef ir_type *(*get_Alloc_func)(ir_node *n);
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         get_Alloc_func old = firm_get_Alloc;
64         firm_get_Alloc = newf;
65         return old;
66 }
67
68 /** Return dynamic type of ptr.
69  *
70  * If we can deduct the dynamic type from the firm nodes
71  * by a limited test we return the dynamic type.  Else
72  * we return unknown_type.
73  *
74  * If we find a dynamic type this means that the pointer always points
75  * to an object of this type during runtime.   We resolved polymorphy.
76  */
77 static ir_type *get_dynamic_type(ir_node *ptr) {
78         ir_type *tp;
79
80         /* skip Cast and Confirm nodes */
81         for (;;) {
82                 ir_opcode code = get_irn_opcode(ptr);
83
84                 switch (code) {
85                 case iro_Cast:
86                         ptr = get_Cast_op(ptr);
87                         continue;
88                 case iro_Confirm:
89                         ptr = get_Confirm_value(ptr);
90                         continue;
91                 default:
92                         ;
93                 }
94                 break;
95         }
96         tp = (*firm_get_Alloc)(ptr);
97         return tp ? tp : firm_unknown_type;
98 }
99
100 /**
101  * Check, if an entity is final, i.e. is not anymore overridden.
102  */
103 static int is_final_ent(ir_entity *ent) {
104         if (is_entity_final(ent)) {
105                 /* not possible to override this entity. */
106                 return 1;
107         }
108         if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
109                 /* we have a closed world, so simply check how often it was
110                 overridden. */
111                 return 1;
112         }
113         return 0;
114 }
115
116 /*
117  * Transform Sel[method] to SymC[method] if possible.
118  */
119 ir_node *transform_node_Sel(ir_node *node) {
120         ir_node   *new_node, *ptr;
121         ir_type   *dyn_tp;
122         ir_entity *ent = get_Sel_entity(node);
123
124         if (get_irp_phase_state() == phase_building) return node;
125
126         if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
127                 return node;
128
129         if (!is_Method_type(get_entity_type(ent)))
130                 return node;
131
132         /* If the entity is a leave in the inheritance tree,
133            we can replace the Sel by a constant. */
134         if (is_final_ent(ent)) {
135                 /* In dead code, we might call a leave entity that is a description.
136                    Do not turn the Sel to a SymConst. */
137                 if (get_entity_peculiarity(ent) == peculiarity_description) {
138                         /* We could remove the Call depending on this Sel. */
139                         new_node = node;
140                 } else {
141                         ir_node *rem_block = get_cur_block();
142                         set_cur_block(get_nodes_block(node));
143                         new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
144                         set_cur_block(rem_block);
145                         DBG_OPT_POLY(node, new_node);
146                 }
147                 return new_node;
148         }
149
150         /* If we know the dynamic type, we can replace the Sel by a constant. */
151         ptr = get_Sel_ptr(node);      /* The address we select from. */
152         dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
153
154         if (dyn_tp != firm_unknown_type) {
155                 ir_entity *called_ent;
156                 ir_node *rem_block;
157
158                 /* We know which method will be called, no dispatch necessary. */
159                 called_ent = resolve_ent_polymorphy(dyn_tp, ent);
160                 /* called_ent may not be description: has no Address/Const to Call! */
161                 assert(get_entity_peculiarity(called_ent) != peculiarity_description);
162
163                 rem_block = get_cur_block();
164                 set_cur_block(get_nodes_block(node));
165                 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
166                 set_cur_block(rem_block);
167                 DBG_OPT_POLY(node, new_node);
168
169                 return new_node;
170         }
171
172         return node;
173 }
174
175 /* Transform  Load(Sel(Alloc)[constant static entity])
176  *  to Const[constant static entity value].
177  *
178  *  This function returns a node replacing the Proj(Load)[Value].
179  *  If this is actually called in transform_node, we must build
180  *  a tuple, or replace the Projs of the load.
181  *  Therefore we call this optimization in ldstopt().
182  */
183 ir_node *transform_polymorph_Load(ir_node *load) {
184         ir_node *new_node = NULL;
185         ir_node *field_ptr, *ptr;
186         ir_entity *ent;
187         ir_type *dyn_tp;
188
189         if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
190                 return load;
191
192         field_ptr = get_Load_ptr(load);
193
194         if (! is_Sel(field_ptr)) return load;
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 load;
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 = get_atomic_ent_value(ent);
205         } else {
206                 /* If we know the dynamic type, we can replace the Sel by a constant. */
207                 ptr = get_Sel_ptr(field_ptr);    /* The address we select from. */
208                 dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
209
210                 if (dyn_tp != firm_unknown_type) {
211                         ir_entity *loaded_ent;
212
213                         /* We know which method will be called, no dispatch necessary. */
214                         loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
215                         /* called_ent may not be description: has no Address/Const to Call! */
216                         assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
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 }