3 * File name: ir/opt/opt_polymorphy
4 * Purpose: Optimize polymorphic Sel nodes.
5 * Author: Goetz Lindenmaier, Michael Beck
8 * Copyright: (c) 2005 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 #include "iropt_dbg.h"
26 * Checks if a graph allocates new memory and returns the
27 * type of the newly allocated entity.
28 * Returns NULL if the graph did not represent an Allocation.
30 * The default implementation hecks for Alloc nodes only.
32 ir_type *default_firm_get_Alloc(ir_node *n) {
34 if (get_irn_op(n) == op_Alloc) {
35 return get_Alloc_type(n);
40 typedef ir_type *(*get_Alloc_func)(ir_node *n);
42 /** The get_Alloc function */
43 static get_Alloc_func firm_get_Alloc = default_firm_get_Alloc;
45 /** Set a new get_Alloc_func and returns the old one. */
46 get_Alloc_func firm_set_Alloc_func(get_Alloc_func newf) {
47 get_Alloc_func old = firm_get_Alloc;
48 firm_get_Alloc = newf;
52 /** Return dynamic type of ptr.
54 * If we can deduct the dynamic type from the firm nodes
55 * by a limited test we return the dynamic type. Else
56 * we return unknown_type.
58 * If we find a dynamic type this means that the pointer always points
59 * to an object of this type during runtime. We resolved polymorphy.
61 static ir_type *get_dynamic_type(ir_node *ptr) {
64 /* skip Cast and Confirm nodes */
66 ir_opcode code = get_irn_opcode(ptr);
70 ptr = get_Cast_op(ptr);
73 ptr = get_Confirm_value(ptr);
80 tp = (*firm_get_Alloc)(ptr);
81 return tp ? tp : firm_unknown_type;
85 * Check, if an entity is final, i.e. is not anymore overridden.
87 static int is_final_ent(ir_entity *ent) {
88 if (is_entity_final(ent)) {
89 /* not possible to override this entity. */
92 if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
93 /* we have a closed world, so simply check how often it was
101 * Transform Sel[method] to SymC[method] if possible.
103 ir_node *transform_node_Sel(ir_node *node)
105 ir_node *new_node, *ptr;
107 ir_entity *ent = get_Sel_entity(node);
109 if (get_irp_phase_state() == phase_building) return node;
111 if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
114 if (!is_Method_type(get_entity_type(ent)))
117 /* If the entity is a leave in the inheritance tree,
118 we can replace the Sel by a constant. */
119 if (is_final_ent(ent)) {
120 /* In dead code, we might call a leave entity that is a description.
121 Do not turn the Sel to a SymConst. */
122 if (get_entity_peculiarity(ent) == peculiarity_description) {
123 /* We could remove the Call depending on this Sel. */
126 ir_node *rem_block = get_cur_block();
127 set_cur_block(get_nodes_block(node));
128 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
129 set_cur_block(rem_block);
130 DBG_OPT_POLY(node, new_node);
135 /* If we know the dynamic type, we can replace the Sel by a constant. */
136 ptr = get_Sel_ptr(node); /* The address we select from. */
137 dyn_tp = get_dynamic_type(ptr); /* The runtime type of ptr. */
139 if (dyn_tp != firm_unknown_type) {
140 ir_entity *called_ent;
143 /* We know which method will be called, no dispatch necessary. */
144 called_ent = resolve_ent_polymorphy(dyn_tp, ent);
145 /* called_ent may not be description: has no Address/Const to Call! */
146 assert(get_entity_peculiarity(called_ent) != peculiarity_description);
148 rem_block = get_cur_block();
149 set_cur_block(get_nodes_block(node));
150 new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
151 set_cur_block(rem_block);
152 DBG_OPT_POLY(node, new_node);
160 /* Transform Load(Sel(Alloc)[constant static entity])
161 * to Const[constant static entity value].
163 * This function returns a node replacing the Proj(Load)[Value].
164 * If this is actually called in transform_node, we must build
165 * a tuple, or replace the Projs of the load.
166 * Therefore we call this optimization in ldstopt().
168 ir_node *transform_node_Load(ir_node *n)
170 ir_node *field_ptr, *new_node, *ptr;
174 if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
177 field_ptr = get_Load_ptr(n);
179 if (! is_Sel(field_ptr)) return n;
181 ent = get_Sel_entity(field_ptr);
182 if ((get_entity_allocation(ent) != allocation_static) ||
183 (get_entity_variability(ent) != variability_constant) )
186 /* If the entity is a leave in the inheritance tree,
187 we can replace the Sel by a constant. */
188 if ((get_irp_phase_state() != phase_building) && is_final_ent(ent)) {
189 new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(ent));
190 DBG_OPT_POLY(field_ptr, new_node);
195 /* If we know the dynamic type, we can replace the Sel by a constant. */
196 ptr = get_Sel_ptr(field_ptr); /* The address we select from. */
197 dyn_tp = get_dynamic_type(ptr); /* The runtime type of ptr. */
199 if (dyn_tp != firm_unknown_type) {
200 ir_entity *loaded_ent;
202 /* We know which method will be called, no dispatch necessary. */
203 loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
204 /* called_ent may not be description: has no Address/Const to Call! */
205 assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
207 new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(loaded_ent));
208 DBG_OPT_POLY(field_ptr, new_node);