-/* Collect the entity representing the implementation of this
- entity (not the same if inherited) and all entities for overwriting
- implementations in "set".
- If the implementation of the method is not included in the
- compilation unit "open" is set to true.
- A recursive descend in the overwritten relation.
- Cycle-free, therefore must terminate. */
-void collect_impls(entity *method, eset *set, int *size, bool *open) {
- int i;
- if (get_entity_peculiarity(method) == existent) {
- if (get_entity_visibility(method) == external_allocated) {
- assert(get_entity_irg(method) == NULL);
- *open = true;
- } else {
- assert(get_entity_irg(method) != NULL);
- if (!eset_contains(set, method)) {
- eset_insert(set, method);
- ++(*size);
- }
- }
- }
- if (get_entity_peculiarity(method) == inherited) {
- entity *impl_ent = get_inherited_methods_implementation(method);
- assert(impl_ent && "no implementation for inherited entity");
- if (get_entity_visibility(impl_ent) == external_allocated) {
- assert(get_entity_irg(impl_ent) == NULL);
- *open = true;
- } else {
- assert(get_entity_irg(impl_ent) != NULL);
- if (!eset_contains(set, impl_ent)) {
- eset_insert(set, impl_ent);
- ++(*size);
- }
- }
- }
- /** recursive descend **/
- for (i = get_entity_n_overwrittenby(method) - 1; i >= 0; --i)
- collect_impls(get_entity_overwrittenby(method, i), set, size, open);
+/** Analyze address computations.
+ *
+ * Compute for all Sel nodes the set of methods that can be selected.
+ * For each entity we store the set of subentities in the link field.
+ *
+ * Further do some optimizations:
+ * - Call standard optimizations for Sel nodes: this removes polymorphic
+ * calls.
+ * - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
+ * For this we precomputed a map name->entity. Nowadays, we no more support
+ * this and assert.
+ * - If the node is a Sel:
+ * If we found only a single method that can be called, replace the Sel
+ * by a SymConst. This is more powerful than the analysis in opt_polymorphy,
+ * as here we walk the type graph. In opt_polymorphy we only apply a local
+ * pattern.
+ *
+ * @param node The node to analyze
+ * @param env A map that maps names of entities to the entities.
+ */
+static void sel_methods_walker(ir_node *node, void *env) {
+ pmap *ldname_map = env;
+ ir_entity **arr;
+
+ /* Call standard optimizations */
+ if (is_Sel(node)) {
+ ir_node *new_node = optimize_in_place(node);
+ if (node != new_node) {
+ exchange(node, new_node);
+ node = new_node;
+ }
+ }
+
+ /* replace SymConst(name)-operations by SymConst(ent) */
+ if (is_SymConst(node)) {
+ if (get_SymConst_kind(node) == symconst_addr_name) {
+ pmap_entry *entry = pmap_find(ldname_map, get_SymConst_name(node));
+ if (entry != NULL) { /* Method is declared in the compiled code */
+ assert(!"There should not be a SymConst[addr_name] addressing a method with an implementation"
+ "in this compilation unit. Use a SymConst[addr_ent].");
+ }
+ }
+ } else if (is_Sel(node) && is_Method_type(get_entity_type(get_Sel_entity(node)))) {
+ ir_entity *ent = get_SymConst_entity(get_atomic_ent_value(get_Sel_entity(node)));
+ assert(get_entity_peculiarity(ent) != peculiarity_inherited);
+
+ if (!eset_contains(entities, ent)) {
+ /* Entity not yet handled. Find all (internal or external)
+ * implemented methods that overwrites this entity.
+ * This set is stored in the entity link. */
+ set_entity_link(ent, get_impl_methods(ent));
+ eset_insert(entities, ent);
+ }
+
+ /* -- As an add on we get an optimization that removes polymorphic calls.
+ This optimization is more powerful than that in transform_node_Sel(). -- */
+ arr = get_entity_link(ent);
+ if (arr == NULL) {
+ /*
+ * The Sel node never returns a pointer to a usable method.
+ * We could not call it, but it may be description:
+ * We call a method in a dead part of the program.
+ */
+ assert(get_entity_peculiarity(ent) == peculiarity_description);
+ }
+ else if (get_opt_closed_world() && get_opt_dyn_meth_dispatch() &&
+ (ARR_LEN(arr) == 1 && arr[0] != NULL)) {
+ ir_node *new_node;
+
+ /*
+ * The Sel node returns only one possible method.
+ * So we could replace the Sel node by a SymConst.
+ * This method must exists.
+ */
+ set_irg_current_block(current_ir_graph, get_nodes_block(node));
+ assert(get_entity_peculiarity(get_SymConst_entity(get_atomic_ent_value(arr[0]))) ==
+ peculiarity_existent);
+ new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(arr[0]));
+ DBG_OPT_POLY(node, new_node);
+ exchange(node, new_node);
+ }
+ }