3 * File name: ir/opt/opt_polymorphy
4 * Purpose: Optimize polymorphic Sel nodes.
8 * Copyright: (c) 2005 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
19 #include "iropt_dbg.h"
23 * Checks if a graph allocates new memory and returns the
24 * type of the newly allocated entity.
25 * Returns NULL if the graph did not represent an Allocation.
27 * The default implementation hecks for Alloc nodes only.
29 ir_type *default_firm_get_Alloc(ir_node *n) {
31 if (get_irn_op(n) == op_Alloc) {
32 return get_Alloc_type(n);
37 typedef ir_type *(*get_Alloc_func)(ir_node *n);
39 /** The get_Alloc function */
40 static get_Alloc_func firm_get_Alloc = default_firm_get_Alloc;
42 /** Set a new get_Alloc_func and returns the old one. */
43 get_Alloc_func firm_set_Alloc_func(get_Alloc_func newf) {
44 get_Alloc_func old = firm_get_Alloc;
45 firm_get_Alloc = newf;
49 /** Return dynamic type of ptr.
51 * If we can deduct the dynamic type from the firm nodes
52 * by a limited test we return the dynamic type. Else
53 * we return unknown_type.
55 * If we find a dynamic type this means that the pointer always points
56 * to an object of this type during runtime. We resolved polymorphy.
58 static ir_type *get_dynamic_type(ir_node *ptr) {
61 /* skip Cast and Confirm nodes */
63 opcode code = get_irn_opcode(ptr);
67 ptr = get_Cast_op(ptr);
70 ptr = get_Confirm_value(ptr);
77 tp = (*firm_get_Alloc)(ptr);
78 return tp ? tp : firm_unknown_type;
82 * Check, if a entity is final, i.e. is not anymore overridden.
84 static is_final_ent(entity *ent) {
85 if (get_entity_final(ent)) {
86 /* not possible to override this entity. */
89 if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
90 /* we have a closed world, so simply check how often it was
98 * Transform Sel[method] to SymC[method] if possible.
100 ir_node *transform_node_Sel(ir_node *node)
102 ir_node *new_node, *ptr;
104 entity *ent = get_Sel_entity(node);
106 if (get_irp_phase_state() == phase_building) return node;
108 if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
111 if (!is_Method_type(get_entity_type(ent)))
114 /* If the entity is a leave in the inheritance tree,
115 we can replace the Sel by a constant. */
116 if (is_final_ent(ent)) {
117 /* In dead code, we might call a leave entity that is a description.
118 Do not turn the Sel to a SymConst. */
119 if (get_entity_peculiarity(ent) == peculiarity_description) {
120 /* We could remove the Call depending on this Sel. */
123 ir_node *rem_block = get_cur_block();
124 set_cur_block(get_nodes_block(node));
125 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
126 set_cur_block(rem_block);
127 DBG_OPT_POLY(node, new_node);
132 /* If we know the dynamic type, we can replace the Sel by a constant. */
133 ptr = get_Sel_ptr(node); /* The address we select from. */
134 dyn_tp = get_dynamic_type(ptr); /* The runtime type of ptr. */
136 if (dyn_tp != firm_unknown_type) {
140 /* We know which method will be called, no dispatch necessary. */
141 called_ent = resolve_ent_polymorphy(dyn_tp, ent);
142 /* called_ent may not be description: has no Address/Const to Call! */
143 assert(get_entity_peculiarity(called_ent) != peculiarity_description);
145 rem_block = get_cur_block();
146 set_cur_block(get_nodes_block(node));
147 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
148 set_cur_block(rem_block);
149 DBG_OPT_POLY(node, new_node);
157 /* Transform Load(Sel(Alloc)[constant static entity])
158 * to Const[constant static entity value].
160 * This function returns a node replacing the Proj(Load)[Value].
161 * If this is actually called in transform_node, we must build
162 * a tuple, or replace the Projs of the load.
163 * Therefore we call this optimization in ldstopt().
165 ir_node *transform_node_Load(ir_node *n)
167 ir_node *field_ptr, *new_node, *ptr;
171 if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
174 field_ptr = get_Load_ptr(n);
176 if (! is_Sel(field_ptr)) return n;
178 ent = get_Sel_entity(field_ptr);
179 if ((get_entity_allocation(ent) != allocation_static) ||
180 (get_entity_variability(ent) != variability_constant) )
183 /* If the entity is a leave in the inheritance tree,
184 we can replace the Sel by a constant. */
185 if ((get_irp_phase_state() != phase_building) && (get_entity_n_overwrittenby(ent) == 0)) {
186 new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(ent));
187 DBG_OPT_POLY(field_ptr, new_node);
192 /* If we know the dynamic type, we can replace the Sel by a constant. */
193 ptr = get_Sel_ptr(field_ptr); /* The address we select from. */
194 dyn_tp = get_dynamic_type(ptr); /* The runtime type of ptr. */
196 if (dyn_tp != firm_unknown_type) {
199 /* We know which method will be called, no dispatch necessary. */
200 loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
201 /* called_ent may not be description: has no Address/Const to Call! */
202 assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
204 new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(loaded_ent));
205 DBG_OPT_POLY(field_ptr, new_node);