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 * Transform Sel[method] to SymC[method] if possible.
84 ir_node *transform_node_Sel(ir_node *node)
86 ir_node *new_node, *ptr;
88 entity *ent = get_Sel_entity(node);
90 if (get_irp_phase_state() == phase_building) return node;
92 if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
95 if (!is_Method_type(get_entity_type(ent)))
98 /* If the entity is a leave in the inheritance tree,
99 we can replace the Sel by a constant. */
100 if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
101 /* In dead code, we might call a leave entity that is a description.
102 Do not turn the Sel to a SymConst. */
103 if (get_entity_peculiarity(ent) == peculiarity_description) {
104 /* We could remove the Call depending on this Sel. */
107 ir_node *rem_block = get_cur_block();
108 set_cur_block(get_nodes_block(node));
109 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
110 set_cur_block(rem_block);
111 DBG_OPT_POLY(node, new_node);
117 /* If we know the dynamic type, we can replace the Sel by a constant. */
118 ptr = get_Sel_ptr(node); /* The address we select from. */
119 dyn_tp = get_dynamic_type(ptr); /* The runtime type of ptr. */
121 if (dyn_tp != firm_unknown_type) {
125 /* We know which method will be called, no dispatch necessary. */
126 called_ent = resolve_ent_polymorphy(dyn_tp, ent);
127 /* called_ent may not be description: has no Address/Const to Call! */
128 assert(get_entity_peculiarity(called_ent) != peculiarity_description);
130 rem_block = get_cur_block();
131 set_cur_block(get_nodes_block(node));
132 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
133 set_cur_block(rem_block);
134 DBG_OPT_POLY(node, new_node);
142 /* Transform Load(Sel(Alloc)[constant static entity])
143 * to Const[constant static entity value].
145 * This function returns a node replacing the Proj(Load)[Value].
146 * If this is actually called in transform_node, we must build
147 * a tuple, or replace the Projs of the load.
148 * Therefore we call this optimization in ldstopt().
150 ir_node *transform_node_Load(ir_node *n)
152 ir_node *field_ptr, *new_node, *ptr;
156 if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
159 field_ptr = get_Load_ptr(n);
161 if (! is_Sel(field_ptr)) return n;
163 ent = get_Sel_entity(field_ptr);
164 if ((get_entity_allocation(ent) != allocation_static) ||
165 (get_entity_variability(ent) != variability_constant) )
168 /* If the entity is a leave in the inheritance tree,
169 we can replace the Sel by a constant. */
170 if ((get_irp_phase_state() != phase_building) && (get_entity_n_overwrittenby(ent) == 0)) {
171 new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(ent));
172 DBG_OPT_POLY(field_ptr, new_node);
177 /* If we know the dynamic type, we can replace the Sel by a constant. */
178 ptr = get_Sel_ptr(field_ptr); /* The address we select from. */
179 dyn_tp = get_dynamic_type(ptr); /* The runtime type of ptr. */
181 if (dyn_tp != firm_unknown_type) {
184 /* We know which method will be called, no dispatch necessary. */
185 loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
186 /* called_ent may not be description: has no Address/Const to Call! */
187 assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
189 new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(loaded_ent));
190 DBG_OPT_POLY(field_ptr, new_node);