2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Optimize polymorphic Sel and Load nodes.
23 * @author Goetz Lindenmaier, Michael Beck
26 * This file subsumes optimization code from cgana.
30 #include "iroptimize.h"
38 #include "iropt_dbg.h"
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.
46 * The default implementation hecks for Alloc nodes only.
48 ir_type *default_firm_get_Alloc(ir_node *n) {
51 return get_Alloc_type(n);
56 /** The get_Alloc function */
57 static get_Alloc_func firm_get_Alloc = default_firm_get_Alloc;
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;
66 /** Return dynamic type of ptr.
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.
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.
75 static ir_type *get_dynamic_type(ir_node *ptr) {
78 /* skip Cast and Confirm nodes */
80 ir_opcode code = get_irn_opcode(ptr);
84 ptr = get_Cast_op(ptr);
87 ptr = get_Confirm_value(ptr);
94 tp = (*firm_get_Alloc)(ptr);
95 return tp ? tp : firm_unknown_type;
99 * Check, if an entity is final, i.e. is not anymore overridden.
101 static int is_final_ent(ir_entity *ent) {
102 if (is_entity_final(ent)) {
103 /* not possible to override this entity. */
106 if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
107 /* we have a closed world, so simply check how often it was
115 * Transform Sel[method] to SymC[method] if possible.
117 ir_node *transform_node_Sel(ir_node *node) {
118 ir_node *new_node, *ptr;
120 ir_entity *ent = get_Sel_entity(node);
122 if (get_irp_phase_state() == phase_building) return node;
124 if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
127 if (!is_Method_type(get_entity_type(ent)))
130 /* If the entity is a leave in the inheritance tree,
131 we can replace the Sel by a constant. */
132 if (is_final_ent(ent)) {
133 /* In dead code, we might call a leave entity that is a description.
134 Do not turn the Sel to a SymConst. */
135 if (get_entity_peculiarity(ent) == peculiarity_description) {
136 /* We could remove the Call depending on this Sel. */
139 ir_node *rem_block = get_cur_block();
140 set_cur_block(get_nodes_block(node));
141 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
142 set_cur_block(rem_block);
143 DBG_OPT_POLY(node, new_node);
148 /* If we know the dynamic type, we can replace the Sel by a constant. */
149 ptr = get_Sel_ptr(node); /* The address we select from. */
150 dyn_tp = get_dynamic_type(ptr); /* The runtime type of ptr. */
152 if (dyn_tp != firm_unknown_type) {
153 ir_entity *called_ent;
156 /* We know which method will be called, no dispatch necessary. */
157 called_ent = resolve_ent_polymorphy(dyn_tp, ent);
158 /* called_ent may not be description: has no Address/Const to Call! */
159 assert(get_entity_peculiarity(called_ent) != peculiarity_description);
161 rem_block = get_cur_block();
162 set_cur_block(get_nodes_block(node));
163 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
164 set_cur_block(rem_block);
165 DBG_OPT_POLY(node, new_node);
173 /* Transform Load(Sel(Alloc)[constant static entity])
174 * to Const[constant static entity value].
176 * This function returns a node replacing the Proj(Load)[Value].
177 * If this is actually called in transform_node, we must build
178 * a tuple, or replace the Projs of the load.
179 * Therefore we call this optimization in ldstopt().
181 ir_node *transform_polymorph_Load(ir_node *load) {
182 ir_node *new_node = NULL;
183 ir_node *field_ptr, *ptr;
187 if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
190 field_ptr = get_Load_ptr(load);
192 if (! is_Sel(field_ptr)) return load;
194 ent = get_Sel_entity(field_ptr);
195 if ((get_entity_allocation(ent) != allocation_static) ||
196 (get_entity_variability(ent) != variability_constant) )
199 /* If the entity is a leave in the inheritance tree,
200 we can replace the Sel by a constant. */
201 if ((get_irp_phase_state() != phase_building) && is_final_ent(ent)) {
202 new_node = get_atomic_ent_value(ent);
204 /* If we know the dynamic type, we can replace the Sel by a constant. */
205 ptr = get_Sel_ptr(field_ptr); /* The address we select from. */
206 dyn_tp = get_dynamic_type(ptr); /* The runtime type of ptr. */
208 if (dyn_tp != firm_unknown_type) {
209 ir_entity *loaded_ent;
211 /* We know which method will be called, no dispatch necessary. */
212 loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
213 /* called_ent may not be description: has no Address/Const to Call! */
214 assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
215 new_node = get_atomic_ent_value(loaded_ent);
218 if (new_node != NULL) {
219 new_node = can_replace_load_by_const(load, new_node);
220 if (new_node != NULL) {
221 DBG_OPT_POLY(field_ptr, new_node);