2 * Copyright (C) 1995-2011 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
21 * @file tr_inheritance.c
22 * @brief Utility routines for inheritance representation
23 * @author Goetz Lindenmaier
29 #include "irgraph_t.h"
37 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
39 /* ----------------------------------------------------------------------- */
40 /* Resolve implicit inheritance. */
41 /* ----------------------------------------------------------------------- */
43 ident *default_mangle_inherited_name(const ir_entity *super, const ir_type *clss)
45 return id_mangle_u(new_id_from_str("inh"), id_mangle_u(get_class_ident(clss), get_entity_ident(super)));
48 /** Replicates all entities in all super classes that are not overwritten
49 by an entity of this class. */
50 static void copy_entities_from_superclass(ir_type *clss, void *env)
58 ir_entity *inhent, *thisent;
59 mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
61 for (i = 0; i < get_class_n_supertypes(clss); i++) {
62 super = get_class_supertype(clss, i);
63 assert(is_Class_type(super) && "not a class");
64 for (j = 0; j < get_class_n_members(super); j++) {
65 inhent = get_class_member(super, j);
66 /* check whether inhent is already overwritten */
68 for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
69 thisent = get_class_member(clss, k);
70 for (l = 0; l < get_entity_n_overwrites(thisent); l++) {
71 if (inhent == get_entity_overwrites(thisent, l)) {
72 /* overwritten - do not copy */
80 thisent = copy_entity_own(inhent, clss);
81 add_entity_overwrites(thisent, inhent);
82 if (get_entity_peculiarity(inhent) == peculiarity_existent)
83 set_entity_peculiarity(thisent, peculiarity_inherited);
84 set_entity_ld_ident(thisent, mfunc(inhent, clss));
85 if (get_entity_linkage(inhent) & IR_LINKAGE_CONSTANT) {
86 assert(is_atomic_entity(inhent) && /* @@@ */
87 "Inheritance of constant, compound entities not implemented");
88 add_entity_linkage(thisent, IR_LINKAGE_CONSTANT);
89 set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
96 /* Resolve implicit inheritance.
98 * Resolves the implicit inheritance supplied by firm.
100 void resolve_inheritance(mangle_inherited_name_func *mfunc)
103 mfunc = default_mangle_inherited_name;
104 class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
108 /* ----------------------------------------------------------------------- */
109 /* The transitive closure of the subclass/superclass and */
110 /* overwrites/overwrittenby relation. */
112 /* A walk over the ir (O(#types+#entities)) computes the transitive */
113 /* closure. Adding a new type/entity or changing the basic relations in */
114 /* some other way invalidates the transitive closure, i.e., it is not */
115 /* updated by the basic functions. */
117 /* All functions are named as their counterparts for the basic relations, */
118 /* adding the infix 'trans_'. */
119 /* ----------------------------------------------------------------------- */
121 void set_irp_inh_transitive_closure_state(inh_transitive_closure_state s)
123 irp->inh_trans_closure_state = s;
125 void invalidate_irp_inh_transitive_closure_state(void)
127 if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
128 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
130 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void)
132 return irp->inh_trans_closure_state;
135 static void assert_valid_state(void)
137 assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
138 irp->inh_trans_closure_state == inh_transitive_closure_invalid);
141 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
142 /* There is a set that extends each entity/type with two new */
143 /* fields: one for the upwards directed relation: 'up' (supertype, */
144 /* overwrites) and one for the downwards directed relation: 'down' (sub- */
145 /* type, overwrittenby. These fields contain psets (and maybe later */
146 /* arrays) listing all subtypes... */
147 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
155 const firm_kind *kind; /**< An entity or type. */
159 /* We use this set for all types and entities. */
160 static set *tr_inh_trans_set = NULL;
163 * Compare two tr_inh_trans_tp entries.
165 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size)
167 const tr_inh_trans_tp *ef1 = (const tr_inh_trans_tp*)e1;
168 const tr_inh_trans_tp *ef2 = (const tr_inh_trans_tp*)e2;
171 return ef1->kind != ef2->kind;
175 * calculate the hash value of an tr_inh_trans_tp
177 static inline unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v)
179 return HASH_PTR(v->kind);
182 /* This always completes successfully. */
183 static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k)
185 tr_inh_trans_tp a, *found;
188 if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
190 found = (tr_inh_trans_tp*)set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
192 a.directions[d_up] = pset_new_ptr(16);
193 a.directions[d_down] = pset_new_ptr(16);
194 found = (tr_inh_trans_tp*)set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
199 static pset *get_entity_map(const ir_entity *ent, dir d)
201 tr_inh_trans_tp *found;
203 assert(is_entity(ent));
204 found = get_firm_kind_entry((const firm_kind *)ent);
205 return found->directions[d];
208 static pset *get_type_map(const ir_type *tp, dir d)
210 tr_inh_trans_tp *found;
213 found = get_firm_kind_entry((const firm_kind *)tp);
214 return found->directions[d];
219 * Walk over all types reachable from tp in the sub/supertype
220 * relation and compute the closure for the two downwards directed
223 * The walk in the dag formed by the relation is tricky: We must visit
224 * all subtypes before visiting the supertypes. So we first walk down.
225 * Then we can compute the closure for this type. Then we walk up.
226 * As we call ourselves recursive, and walk in both directions, there
227 * can be cycles. So we have to make sure, that if we visit a node
228 * a second time (in a walk up) we do nothing. For this we increment
229 * the master visited flag twice.
230 * If the type is marked with master_flag_visited-1 it is on the stack.
231 * If it is marked with master_flag_visited it is fully processed.
233 * Well, we still miss some candidates ... */
234 static void compute_down_closure(ir_type *tp)
236 pset *myset, *subset;
237 size_t i, n_subtypes, n_members, n_supertypes;
238 ir_visited_t master_visited = get_master_type_visited();
240 assert(is_Class_type(tp));
242 set_type_visited(tp, master_visited-1);
244 /* Recursive descend. */
245 n_subtypes = get_class_n_subtypes(tp);
246 for (i = 0; i < n_subtypes; ++i) {
247 ir_type *stp = get_class_subtype(tp, i);
248 if (get_type_visited(stp) < master_visited-1) {
249 compute_down_closure(stp);
254 myset = get_type_map(tp, d_down);
255 for (i = 0; i < n_subtypes; ++i) {
256 ir_type *stp = get_class_subtype(tp, i);
257 subset = get_type_map(stp, d_down);
258 pset_insert_ptr(myset, stp);
259 pset_insert_pset_ptr(myset, subset);
263 n_members = get_class_n_members(tp);
264 for (i = 0; i < n_members; ++i) {
265 ir_entity *mem = get_class_member(tp, i);
266 size_t j, n_overwrittenby = get_entity_n_overwrittenby(mem);
268 myset = get_entity_map(mem, d_down);
269 for (j = 0; j < n_overwrittenby; ++j) {
270 ir_entity *ov = get_entity_overwrittenby(mem, j);
271 subset = get_entity_map(ov, d_down);
272 pset_insert_ptr(myset, ov);
273 pset_insert_pset_ptr(myset, subset);
277 mark_type_visited(tp);
280 n_supertypes = get_class_n_supertypes(tp);
281 for (i = 0; i < n_supertypes; ++i) {
282 ir_type *stp = get_class_supertype(tp, i);
283 if (get_type_visited(stp) < master_visited-1) {
284 compute_down_closure(stp);
289 static void compute_up_closure(ir_type *tp)
291 pset *myset, *subset;
292 size_t i, n_subtypes, n_members, n_supertypes;
293 ir_visited_t master_visited = get_master_type_visited();
295 assert(is_Class_type(tp));
297 set_type_visited(tp, master_visited-1);
299 /* Recursive descend. */
300 n_supertypes = get_class_n_supertypes(tp);
301 for (i = 0; i < n_supertypes; ++i) {
302 ir_type *stp = get_class_supertype(tp, i);
303 if (get_type_visited(stp) < get_master_type_visited()-1) {
304 compute_up_closure(stp);
309 myset = get_type_map(tp, d_up);
310 for (i = 0; i < n_supertypes; ++i) {
311 ir_type *stp = get_class_supertype(tp, i);
312 subset = get_type_map(stp, d_up);
313 pset_insert_ptr(myset, stp);
314 pset_insert_pset_ptr(myset, subset);
318 n_members = get_class_n_members(tp);
319 for (i = 0; i < n_members; ++i) {
320 ir_entity *mem = get_class_member(tp, i);
321 size_t j, n_overwrites = get_entity_n_overwrites(mem);
323 myset = get_entity_map(mem, d_up);
324 for (j = 0; j < n_overwrites; ++j) {
325 ir_entity *ov = get_entity_overwrites(mem, j);
326 subset = get_entity_map(ov, d_up);
327 pset_insert_pset_ptr(myset, subset);
328 pset_insert_ptr(myset, ov);
332 mark_type_visited(tp);
335 n_subtypes = get_class_n_subtypes(tp);
336 for (i = 0; i < n_subtypes; ++i) {
337 ir_type *stp = get_class_subtype(tp, i);
338 if (get_type_visited(stp) < master_visited-1) {
339 compute_up_closure(stp);
344 /** Compute the transitive closure of the subclass/superclass and
345 * overwrites/overwrittenby relation.
347 * This function walks over the ir (O(#types+#entities)) to compute the
348 * transitive closure. */
349 void compute_inh_transitive_closure(void)
351 size_t i, n_types = get_irp_n_types();
352 free_inh_transitive_closure();
354 /* The 'down' relation */
355 irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
356 inc_master_type_visited(); /* Inc twice: one if on stack, second if values computed. */
357 inc_master_type_visited();
358 for (i = 0; i < n_types; ++i) {
359 ir_type *tp = get_irp_type(i);
360 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
361 size_t j, n_subtypes = get_class_n_subtypes(tp);
362 int has_unmarked_subtype = 0;
364 assert(get_type_visited(tp) < get_master_type_visited()-1);
365 for (j = 0; j < n_subtypes; ++j) {
366 ir_type *stp = get_class_subtype(tp, j);
367 if (type_not_visited(stp)) {
368 has_unmarked_subtype = 1;
373 /* This is a good starting point. */
374 if (!has_unmarked_subtype)
375 compute_down_closure(tp);
379 /* The 'up' relation */
380 inc_master_type_visited();
381 inc_master_type_visited();
382 for (i = 0; i < n_types; ++i) {
383 ir_type *tp = get_irp_type(i);
384 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
385 size_t j, n_supertypes = get_class_n_supertypes(tp);
386 int has_unmarked_supertype = 0;
388 assert(get_type_visited(tp) < get_master_type_visited()-1);
389 for (j = 0; j < n_supertypes; ++j) {
390 ir_type *stp = get_class_supertype(tp, j);
391 if (type_not_visited(stp)) {
392 has_unmarked_supertype = 1;
397 /* This is a good starting point. */
398 if (!has_unmarked_supertype)
399 compute_up_closure(tp);
403 irp->inh_trans_closure_state = inh_transitive_closure_valid;
404 irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
407 /** Free memory occupied by the transitive closure information. */
408 void free_inh_transitive_closure(void)
410 if (tr_inh_trans_set) {
411 tr_inh_trans_tp *elt;
412 for (elt = (tr_inh_trans_tp*)set_first(tr_inh_trans_set); elt != NULL;
413 elt = (tr_inh_trans_tp*)set_next(tr_inh_trans_set)) {
414 del_pset(elt->directions[d_up]);
415 del_pset(elt->directions[d_down]);
417 del_set(tr_inh_trans_set);
418 tr_inh_trans_set = NULL;
420 irp->inh_trans_closure_state = inh_transitive_closure_none;
423 /* - subtype ------------------------------------------------------------- */
425 ir_type *get_class_trans_subtype_first(const ir_type *tp)
427 assert_valid_state();
428 return (ir_type*)pset_first(get_type_map(tp, d_down));
431 ir_type *get_class_trans_subtype_next(const ir_type *tp)
433 assert_valid_state();
434 return (ir_type*)pset_next(get_type_map(tp, d_down));
437 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp)
439 assert_valid_state();
440 return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
443 /* - supertype ----------------------------------------------------------- */
445 ir_type *get_class_trans_supertype_first(const ir_type *tp)
447 assert_valid_state();
448 return (ir_type*)pset_first(get_type_map(tp, d_up));
451 ir_type *get_class_trans_supertype_next(const ir_type *tp)
453 assert_valid_state();
454 return (ir_type*)pset_next(get_type_map(tp, d_up));
457 /* - overwrittenby ------------------------------------------------------- */
459 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent)
461 assert_valid_state();
462 return (ir_entity*)pset_first(get_entity_map(ent, d_down));
465 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent)
467 assert_valid_state();
468 return (ir_entity*)pset_next(get_entity_map(ent, d_down));
471 /* - overwrites ---------------------------------------------------------- */
474 /** Iterate over all transitive overwritten entities. */
475 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent)
477 assert_valid_state();
478 return (ir_entity*)pset_first(get_entity_map(ent, d_up));
481 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent)
483 assert_valid_state();
484 return (ir_entity*)pset_next(get_entity_map(ent, d_up));
488 /* ----------------------------------------------------------------------- */
489 /* Classify pairs of types/entities in the inheritance relations. */
490 /* ----------------------------------------------------------------------- */
492 /** Returns true if low is subclass of high. */
493 static int check_is_SubClass_of(ir_type *low, ir_type *high)
495 size_t i, n_subtypes;
497 /* depth first search from high downwards. */
498 n_subtypes = get_class_n_subtypes(high);
499 for (i = 0; i < n_subtypes; i++) {
500 ir_type *stp = get_class_subtype(high, i);
501 if (low == stp) return 1;
502 if (is_SubClass_of(low, stp))
508 /* Returns true if low is subclass of high. */
509 int is_SubClass_of(ir_type *low, ir_type *high)
511 assert(is_Class_type(low) && is_Class_type(high));
513 if (low == high) return 1;
515 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
516 pset *m = get_type_map(high, d_down);
517 return pset_find_ptr(m, low) ? 1 : 0;
519 return check_is_SubClass_of(low, high);
523 /* Subclass check for pointers to classes.
525 * Dereferences at both types the same amount of pointer types (as
526 * many as possible). If the remaining types are both class types
527 * and subclasses, returns true, else false. Can also be called with
528 * two class types. */
529 int is_SubClass_ptr_of(ir_type *low, ir_type *high)
531 while (is_Pointer_type(low) && is_Pointer_type(high)) {
532 low = get_pointer_points_to_type(low);
533 high = get_pointer_points_to_type(high);
536 if (is_Class_type(low) && is_Class_type(high))
537 return is_SubClass_of(low, high);
541 int is_overwritten_by(ir_entity *high, ir_entity *low)
543 size_t i, n_overwrittenby;
544 assert(is_entity(low) && is_entity(high));
546 if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
547 pset *m = get_entity_map(high, d_down);
548 return pset_find_ptr(m, low) ? 1 : 0;
551 /* depth first search from high downwards. */
552 n_overwrittenby = get_entity_n_overwrittenby(high);
553 for (i = 0; i < n_overwrittenby; i++) {
554 ir_entity *ov = get_entity_overwrittenby(high, i);
555 if (low == ov) return 1;
556 if (is_overwritten_by(low, ov))
562 /** Resolve polymorphy in the inheritance relation.
564 * Returns the dynamically referenced entity if the static entity and the
565 * dynamic type are given.
566 * Search downwards in overwritten tree.
568 * Need two routines because I want to assert the result.
570 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
572 size_t i, n_overwrittenby;
574 ir_type *owner = get_entity_owner(static_ent);
575 if (owner == dynamic_class) return static_ent;
577 // if the owner of the static_ent already is more special than the dynamic
578 // type to check against - stop here.
579 if (! is_SubClass_of(dynamic_class, owner)) return NULL;
581 n_overwrittenby = get_entity_n_overwrittenby(static_ent);
582 for (i = 0; i < n_overwrittenby; ++i) {
583 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
584 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
588 // No further specialization of static_ent has been found
592 /* Resolve polymorphy in the inheritance relation.
594 * Returns the dynamically referenced entity if the static entity and the
595 * dynamic type are given.
596 * Search downwards in overwritten tree.
598 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent)
601 assert(static_ent && is_entity(static_ent));
603 res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
611 /* ----------------------------------------------------------------------- */
612 /* Class cast state handling. */
613 /* ----------------------------------------------------------------------- */
615 /* - State handling. ----------------------------------------- */
617 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s)
619 if (get_irp_class_cast_state() > s)
620 set_irp_class_cast_state(s);
621 irg->class_cast_state = s;
624 ir_class_cast_state get_irg_class_cast_state(const ir_graph *irg)
626 return irg->class_cast_state;
629 void set_irp_class_cast_state(ir_class_cast_state s)
633 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
634 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
636 irp->class_cast_state = s;
639 ir_class_cast_state get_irp_class_cast_state(void)
641 return irp->class_cast_state;
644 const char *get_class_cast_state_string(ir_class_cast_state s)
646 #define X(a) case a: return #a
648 X(ir_class_casts_any);
649 X(ir_class_casts_transitive);
650 X(ir_class_casts_normalized);
651 X(ir_class_casts_state_max);
652 default: return "invalid class cast state";
657 /* - State verification. ------------------------------------- */
659 typedef struct ccs_env {
660 ir_class_cast_state expected_state;
661 ir_class_cast_state worst_situation;
665 * Walker: check Casts.
667 static void verify_irn_class_cast_state(ir_node *n, void *env)
669 ccs_env *ccs = (ccs_env *)env;
670 ir_class_cast_state this_state = ir_class_casts_any;
671 ir_type *fromtype, *totype;
673 if (!is_Cast(n)) return;
675 fromtype = get_irn_typeinfo_type(get_Cast_op(n));
676 totype = get_Cast_type(n);
678 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
679 totype = get_pointer_points_to_type(totype);
680 fromtype = get_pointer_points_to_type(fromtype);
683 if (!is_Class_type(totype)) return;
685 if (is_SubClass_of(totype, fromtype) ||
686 is_SubClass_of(fromtype, totype)) {
687 this_state = ir_class_casts_transitive;
688 if ((get_class_supertype_index(totype, fromtype) != (size_t)-1) ||
689 (get_class_supertype_index(fromtype, totype) != (size_t)-1) ||
690 fromtype == totype) {
691 this_state = ir_class_casts_normalized;
695 if (!(this_state >= ccs->expected_state)) {
696 ir_printf(" Node is %+F\n", n);
697 ir_printf(" totype %+F\n", totype);
698 ir_printf(" fromtype %+F\n", fromtype);
699 ir_printf(" this_state: %s, exp. state: %s\n",
700 get_class_cast_state_string(this_state),
701 get_class_cast_state_string(ccs->expected_state));
702 assert(this_state >= ccs->expected_state &&
703 "invalid state class cast state setting in graph");
706 if (this_state < ccs->worst_situation)
707 ccs->worst_situation = this_state;
710 /** Verify that the graph meets requirements of state set. */
711 void verify_irg_class_cast_state(ir_graph *irg)
715 FIRM_DBG_REGISTER(dbg, "firm.tr.inheritance");
717 env.expected_state = get_irg_class_cast_state(irg);
718 env.worst_situation = ir_class_casts_normalized;
720 irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
722 if ((env.worst_situation > env.expected_state)) {
723 DB((dbg, LEVEL_1, "Note: class cast state is set lower than reqired "
724 "in graph \n\t%+F\n", irg));
725 DB((dbg, LEVEL_1, " state is %s, reqired is %s\n",
726 get_class_cast_state_string(env.expected_state),
727 get_class_cast_state_string(env.worst_situation)));