moved external headers into include dir
[libfirm] / ir / tr / tr_inheritance.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file    tr_inheritance.c
22  * @brief   Utility routines for inheritance representation
23  * @author  Goetz Lindenmaier
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "typerep.h"
31 #include "irgraph_t.h"
32 #include "irprog_t.h"
33 #include "irprintf.h"
34 #include "pset.h"
35 #include "set.h"
36 #include "irgwalk.h"
37 #include "irflag.h"
38
39
40 /* ----------------------------------------------------------------------- */
41 /* Resolve implicit inheritance.                                           */
42 /* ----------------------------------------------------------------------- */
43
44 ident *default_mangle_inherited_name(ir_entity *super, ir_type *clss) {
45         return mangle_u(new_id_from_str("inh"), mangle_u(get_type_ident(clss), get_entity_ident(super)));
46 }
47
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)
51 {
52         int i, j, k, l;
53         int overwritten;
54         ir_type *super, *inhenttype;
55         ir_entity *inhent, *thisent;
56         mangle_inherited_name_func *mfunc = *(mangle_inherited_name_func **)env;
57
58         for(i = 0; i < get_class_n_supertypes(clss); i++) {
59                 super = get_class_supertype(clss, i);
60                 assert(is_Class_type(super) && "not a class");
61                 for(j = 0; j < get_class_n_members(super); j++) {
62                         inhent = get_class_member(super, j);
63                         inhenttype = get_entity_type(inhent);
64                         /* check whether inhent is already overwritten */
65                         overwritten = 0;
66                         for (k = 0; (k < get_class_n_members(clss)) && (overwritten == 0); k++) {
67                                 thisent = get_class_member(clss, k);
68                                 for(l = 0; l < get_entity_n_overwrites(thisent); l++) {
69                                         if(inhent == get_entity_overwrites(thisent, l)) {
70                                                 /* overwritten - do not copy */
71                                                 overwritten = 1;
72                                                 break;
73                                         }
74                                 }
75                         }
76                         /* Inherit entity */
77                         if (!overwritten) {
78                                 thisent = copy_entity_own(inhent, clss);
79                                 add_entity_overwrites(thisent, inhent);
80                                 if (get_entity_peculiarity(inhent) == peculiarity_existent)
81                                         set_entity_peculiarity(thisent, peculiarity_inherited);
82                                 set_entity_ld_ident(thisent, mfunc(inhent, clss));
83                                 if (get_entity_variability(inhent) == variability_constant) {
84                                         assert(is_atomic_entity(inhent) &&  /* @@@ */
85                                                 "Inheritance of constant, compound entities not implemented");
86                                         set_entity_variability(thisent, variability_constant);
87                                         set_atomic_ent_value(thisent, get_atomic_ent_value(inhent));
88                                 }
89                         }
90                 }
91         }
92 }
93
94 /* Resolve implicit inheritance.
95  *
96  *  Resolves the implicit inheritance supplied by firm.
97  */
98 void resolve_inheritance(mangle_inherited_name_func *mfunc) {
99         if (!mfunc)
100                 mfunc = default_mangle_inherited_name;
101         class_walk_super2sub(copy_entities_from_superclass, NULL, (void *)&mfunc);
102 }
103
104
105 /* ----------------------------------------------------------------------- */
106 /* The transitive closure of the subclass/superclass and                   */
107 /* overwrites/overwrittenby relation.                                      */
108 /*                                                                         */
109 /* A walk over the ir (O(#types+#entities)) computes the transitive        */
110 /* closure.  Adding a new type/entity or changing the basic relations in   */
111 /* some other way invalidates the transitive closure, i.e., it is not      */
112 /* updated by the basic functions.                                         */
113 /*                                                                         */
114 /* All functions are named as their counterparts for the basic relations,  */
115 /* adding the infix 'trans_'.                                              */
116 /* ----------------------------------------------------------------------- */
117
118 void                        set_irp_inh_transitive_closure_state(inh_transitive_closure_state s) {
119         irp->inh_trans_closure_state = s;
120 }
121 void                        invalidate_irp_inh_transitive_closure_state(void) {
122         if (irp->inh_trans_closure_state == inh_transitive_closure_valid)
123                 irp->inh_trans_closure_state = inh_transitive_closure_invalid;
124 }
125 inh_transitive_closure_state get_irp_inh_transitive_closure_state(void) {
126         return irp->inh_trans_closure_state;
127 }
128
129 static void assert_valid_state(void) {
130         assert(irp->inh_trans_closure_state == inh_transitive_closure_valid ||
131                irp->inh_trans_closure_state == inh_transitive_closure_invalid);
132 }
133
134 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
135 /* There is a set that extends each entity/type with two new               */
136 /* fields:  one for the upwards directed relation: 'up' (supertype,        */
137 /* overwrites) and one for the downwards directed relation: 'down' (sub-   */
138 /* type, overwrittenby.  These fields contain psets (and maybe later       */
139 /* arrays) listing all subtypes...                                         */
140 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
141
142 typedef enum {
143         d_up   = 0,
144         d_down = 1,
145 } dir;
146
147 typedef struct {
148         firm_kind *kind;   /* An entity or type. */
149         pset *directions[2];
150 } tr_inh_trans_tp;
151
152 /* We use this set for all types and entities.  */
153 static set *tr_inh_trans_set = NULL;
154
155 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
156         tr_inh_trans_tp *ef1 = (tr_inh_trans_tp *)e1;
157         tr_inh_trans_tp *ef2 = (tr_inh_trans_tp *)e2;
158         return (ef1->kind != ef2->kind);
159 }
160
161 static INLINE unsigned int tr_inh_trans_hash(void *e) {
162         tr_inh_trans_tp *v = e;
163         return HASH_PTR(v->kind);
164 }
165
166 /* This always completes successfully. */
167 static tr_inh_trans_tp* get_firm_kind_entry(firm_kind *k) {
168         tr_inh_trans_tp a, *found;
169         a.kind = k;
170
171         if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
172
173         found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
174         if (!found) {
175                 a.directions[d_up]   = pset_new_ptr(16);
176                 a.directions[d_down] = pset_new_ptr(16);
177                 found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
178         }
179         return found;
180 }
181
182 static pset *get_entity_map(ir_entity *ent, dir d) {
183         tr_inh_trans_tp *found;
184
185         assert(is_entity(ent));
186         found = get_firm_kind_entry((firm_kind *)ent);
187         return found->directions[d];
188 }
189 /*
190 static void  add_entity_map(ir_entity *ent, dir d, ir_entity *new) {
191         tr_inh_trans_tp *found;
192
193         assert(is_entity(ent) && is_entity(new));
194         tr_inh_trans_tp *found = get_firm_kind_entry((firm_kind *)ent);
195         pset_insert_ptr(found->directions[d], new);
196 }
197 */
198 static pset *get_type_map(ir_type *tp, dir d) {
199         tr_inh_trans_tp *found;
200
201         assert(is_type(tp));
202         found = get_firm_kind_entry((firm_kind *)tp);
203         return found->directions[d];
204 }
205 /*
206 static void  add_type_map(ir_type *tp, dir d, type *new) {
207         tr_inh_trans_tp *found;
208
209         assert(is_type(tp) && is_type(new));
210         found = get_firm_kind_entry((firm_kind *)tp);
211         pset_insert_ptr(found->directions[d], new);
212 }
213 */
214
215
216 /**
217  * Walk over all types reachable from tp in the sub/supertype
218  * relation and compute the closure for the two downwards directed
219  * relations.
220  *
221  * The walk in the dag formed by the relation is tricky:  We must visit
222  * all subtypes before visiting the supertypes.  So we first walk down.
223  * Then we can compute the closure for this type.  Then we walk up.
224  * As we call ourselves recursive, and walk in both directions, there
225  * can be cycles.  So we have to make sure, that if we visit a node
226  * a second time (in a walk up) we do nothing.  For this we increment
227  * the master visited flag twice.
228  * If the type is marked with master_flag_visited-1 it is on the stack.
229  * If it is marked with master_flag_visited it is fully processed.
230  *
231  * Well, we still miss some candidates ... */
232 static void compute_down_closure(ir_type *tp) {
233         pset *myset, *subset;
234         int i, n_subtypes, n_members, n_supertypes;
235         unsigned long master_visited = get_master_type_visited();
236
237         assert(is_Class_type(tp));
238
239         set_type_visited(tp, master_visited-1);
240
241         /* Recursive descend. */
242         n_subtypes = get_class_n_subtypes(tp);
243         for (i = 0; i < n_subtypes; ++i) {
244                 ir_type *stp = get_class_subtype(tp, i);
245                 if (get_type_visited(stp) < master_visited-1) {
246                         compute_down_closure(stp);
247                 }
248         }
249
250         /* types */
251         myset = get_type_map(tp, d_down);
252         for (i = 0; i < n_subtypes; ++i) {
253                 ir_type *stp = get_class_subtype(tp, i);
254                 subset = get_type_map(stp, d_down);
255                 pset_insert_ptr(myset, stp);
256                 pset_insert_pset_ptr(myset, subset);
257         }
258
259         /* entities */
260         n_members = get_class_n_members(tp);
261         for (i = 0; i < n_members; ++i) {
262                 ir_entity *mem = get_class_member(tp, i);
263                 int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
264
265                 myset = get_entity_map(mem, d_down);
266                 for (j = 0; j < n_overwrittenby; ++j) {
267                         ir_entity *ov = get_entity_overwrittenby(mem, j);
268                         subset = get_entity_map(ov, d_down);
269                         pset_insert_ptr(myset, ov);
270                         pset_insert_pset_ptr(myset, subset);
271                 }
272         }
273
274         mark_type_visited(tp);
275
276         /* Walk up. */
277         n_supertypes = get_class_n_supertypes(tp);
278         for (i = 0; i < n_supertypes; ++i) {
279                 ir_type *stp = get_class_supertype(tp, i);
280                 if (get_type_visited(stp) < master_visited-1) {
281                         compute_down_closure(stp);
282                 }
283         }
284 }
285
286 static void compute_up_closure(ir_type *tp) {
287         pset *myset, *subset;
288         int i, n_subtypes, n_members, n_supertypes;
289         unsigned long master_visited = get_master_type_visited();
290
291         assert(is_Class_type(tp));
292
293         set_type_visited(tp, master_visited-1);
294
295         /* Recursive descend. */
296         n_supertypes = get_class_n_supertypes(tp);
297         for (i = 0; i < n_supertypes; ++i) {
298                 ir_type *stp = get_class_supertype(tp, i);
299                 if (get_type_visited(stp) < get_master_type_visited()-1) {
300                         compute_up_closure(stp);
301                 }
302         }
303
304         /* types */
305         myset = get_type_map(tp, d_up);
306         for (i = 0; i < n_supertypes; ++i) {
307                 ir_type *stp = get_class_supertype(tp, i);
308                 subset = get_type_map(stp, d_up);
309                 pset_insert_ptr(myset, stp);
310                 pset_insert_pset_ptr(myset, subset);
311         }
312
313         /* entities */
314         n_members = get_class_n_members(tp);
315         for (i = 0; i < n_members; ++i) {
316                 ir_entity *mem = get_class_member(tp, i);
317                 int j, n_overwrites = get_entity_n_overwrites(mem);
318
319                 myset = get_entity_map(mem, d_up);
320                 for (j = 0; j < n_overwrites; ++j) {
321                         ir_entity *ov = get_entity_overwrites(mem, j);
322                         subset = get_entity_map(ov, d_up);
323                         pset_insert_pset_ptr(myset, subset);
324                         pset_insert_ptr(myset, ov);
325                 }
326         }
327
328         mark_type_visited(tp);
329
330         /* Walk down. */
331         n_subtypes = get_class_n_subtypes(tp);
332         for (i = 0; i < n_subtypes; ++i) {
333                 ir_type *stp = get_class_subtype(tp, i);
334                 if (get_type_visited(stp) < master_visited-1) {
335                         compute_up_closure(stp);
336                 }
337         }
338 }
339
340 /** Compute the transitive closure of the subclass/superclass and
341  *  overwrites/overwrittenby relation.
342  *
343  *  This function walks over the ir (O(#types+#entities)) to compute the
344  *  transitive closure.    */
345 void compute_inh_transitive_closure(void) {
346         int i, n_types = get_irp_n_types();
347         free_inh_transitive_closure();
348
349         /* The 'down' relation */
350         inc_master_type_visited();  /* Inc twice: one if on stack, second if values computed. */
351         inc_master_type_visited();
352         for (i = 0; i < n_types; ++i) {
353                 ir_type *tp = get_irp_type(i);
354                 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
355                         int j, n_subtypes = get_class_n_subtypes(tp);
356                         int has_unmarked_subtype = 0;
357
358                         assert(get_type_visited(tp) < get_master_type_visited()-1);
359                         for (j = 0; j < n_subtypes; ++j) {
360                                 ir_type *stp = get_class_subtype(tp, j);
361                                 if (type_not_visited(stp)) {
362                                         has_unmarked_subtype = 1;
363                                         break;
364                                 }
365                         }
366
367                         /* This is a good starting point. */
368                         if (!has_unmarked_subtype)
369                                 compute_down_closure(tp);
370                 }
371         }
372
373         /* The 'up' relation */
374         inc_master_type_visited();
375         inc_master_type_visited();
376         for (i = 0; i < n_types; ++i) {
377                 ir_type *tp = get_irp_type(i);
378                 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
379                         int j, n_supertypes = get_class_n_supertypes(tp);
380                         int has_unmarked_supertype = 0;
381
382                         assert(get_type_visited(tp) < get_master_type_visited()-1);
383                         for (j = 0; j < n_supertypes; ++j) {
384                                 ir_type *stp = get_class_supertype(tp, j);
385                                 if (type_not_visited(stp)) {
386                                         has_unmarked_supertype = 1;
387                                         break;
388                                 }
389                         }
390
391                         /* This is a good starting point. */
392                         if (!has_unmarked_supertype)
393                                 compute_up_closure(tp);
394                 }
395         }
396
397         irp->inh_trans_closure_state = inh_transitive_closure_valid;
398 }
399
400 /** Free memory occupied by the transitive closure information. */
401 void free_inh_transitive_closure(void) {
402         if (tr_inh_trans_set) {
403                 tr_inh_trans_tp *elt;
404                 for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
405                         del_pset(elt->directions[d_up]);
406                         del_pset(elt->directions[d_down]);
407                 }
408                 del_set(tr_inh_trans_set);
409                 tr_inh_trans_set = NULL;
410         }
411         irp->inh_trans_closure_state = inh_transitive_closure_none;
412 }
413
414 /* - subtype ------------------------------------------------------------- */
415
416 ir_type *get_class_trans_subtype_first(ir_type *tp) {
417         assert_valid_state();
418         return pset_first(get_type_map(tp, d_down));
419 }
420
421 ir_type *get_class_trans_subtype_next (ir_type *tp) {
422         assert_valid_state();
423         return pset_next(get_type_map(tp, d_down));
424 }
425
426 int is_class_trans_subtype (ir_type *tp, ir_type *subtp) {
427         assert_valid_state();
428         return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
429 }
430
431 /* - supertype ----------------------------------------------------------- */
432
433 ir_type *get_class_trans_supertype_first(ir_type *tp) {
434         assert_valid_state();
435         return pset_first(get_type_map(tp, d_up));
436 }
437
438 ir_type *get_class_trans_supertype_next (ir_type *tp) {
439         assert_valid_state();
440         return pset_next(get_type_map(tp, d_up));
441 }
442
443 /* - overwrittenby ------------------------------------------------------- */
444
445 ir_entity *get_entity_trans_overwrittenby_first(ir_entity *ent) {
446         assert_valid_state();
447         return pset_first(get_entity_map(ent, d_down));
448 }
449
450 ir_entity *get_entity_trans_overwrittenby_next (ir_entity *ent) {
451         assert_valid_state();
452         return pset_next(get_entity_map(ent, d_down));
453 }
454
455 /* - overwrites ---------------------------------------------------------- */
456
457
458 /** Iterate over all transitive overwritten entities. */
459 ir_entity *get_entity_trans_overwrites_first(ir_entity *ent) {
460         assert_valid_state();
461         return pset_first(get_entity_map(ent, d_up));
462 }
463
464 ir_entity *get_entity_trans_overwrites_next (ir_entity *ent) {
465         assert_valid_state();
466         return pset_next(get_entity_map(ent, d_up));
467 }
468
469
470
471
472
473 /* ----------------------------------------------------------------------- */
474 /* Classify pairs of types/entities in the inheritance relations.          */
475 /* ----------------------------------------------------------------------- */
476
477 /** Returns true if low is subclass of high. */
478 static int check_is_SubClass_of(ir_type *low, ir_type *high) {
479         int i, n_subtypes;
480
481         /* depth first search from high downwards. */
482         n_subtypes = get_class_n_subtypes(high);
483         for (i = 0; i < n_subtypes; i++) {
484                 ir_type *stp = get_class_subtype(high, i);
485                 if (low == stp) return 1;
486                 if (is_SubClass_of(low, stp))
487                         return 1;
488         }
489         return 0;
490 }
491
492 /* Returns true if low is subclass of high. */
493 int is_SubClass_of(ir_type *low, ir_type *high) {
494         assert(is_Class_type(low) && is_Class_type(high));
495
496         if (low == high) return 1;
497
498         if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
499                 pset *m = get_type_map(high, d_down);
500                 return pset_find_ptr(m, low) ? 1 : 0;
501         }
502         return check_is_SubClass_of(low, high);
503 }
504
505
506 /* Subclass check for pointers to classes.
507  *
508  *  Dereferences at both types the same amount of pointer types (as
509  *  many as possible).  If the remaining types are both class types
510  *  and subclasses, returns true, else false.  Can also be called with
511  *  two class types.  */
512 int is_SubClass_ptr_of(ir_type *low, ir_type *high) {
513         while (is_Pointer_type(low) && is_Pointer_type(high)) {
514                 low  = get_pointer_points_to_type(low);
515                 high = get_pointer_points_to_type(high);
516         }
517
518         if (is_Class_type(low) && is_Class_type(high))
519                 return is_SubClass_of(low, high);
520         return 0;
521 }
522
523 int is_overwritten_by(ir_entity *high, ir_entity *low) {
524         int i, n_overwrittenby;
525         assert(is_entity(low) && is_entity(high));
526
527         if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
528                 pset *m = get_entity_map(high, d_down);
529                 return pset_find_ptr(m, low) ? 1 : 0;
530         }
531
532         /* depth first search from high downwards. */
533         n_overwrittenby = get_entity_n_overwrittenby(high);
534         for (i = 0; i < n_overwrittenby; i++) {
535                 ir_entity *ov = get_entity_overwrittenby(high, i);
536                 if (low == ov) return 1;
537                 if (is_overwritten_by(low, ov))
538                         return 1;
539         }
540         return 0;
541 }
542
543 /** Resolve polymorphy in the inheritance relation.
544  *
545  * Returns the dynamically referenced entity if the static entity and the
546  * dynamic type are given.
547  * Search downwards in overwritten tree.
548  *
549  * Need two routines because I want to assert the result.
550  */
551 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
552         int i, n_overwrittenby;
553
554         if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
555
556         n_overwrittenby = get_entity_n_overwrittenby(static_ent);
557         for (i = 0; i < n_overwrittenby; ++i) {
558                 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
559                 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
560                 if (ent) return ent;
561         }
562         return NULL;
563 }
564
565 /* Resolve polymorphy in the inheritance relation.
566  *
567  * Returns the dynamically referenced entity if the static entity and the
568  * dynamic type are given.
569  * Search downwards in overwritten tree. */
570 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
571         ir_entity *res;
572         assert(static_ent && is_entity(static_ent));
573
574         res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
575         assert(res);
576
577         return res;
578 }
579
580
581
582 /* ----------------------------------------------------------------------- */
583 /* Class cast state handling.                                              */
584 /* ----------------------------------------------------------------------- */
585
586 /* - State handling. ----------------------------------------- */
587
588 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) {
589         if (get_irp_class_cast_state() > s) set_irp_class_cast_state(s);
590         irg->class_cast_state = s;
591 }
592
593 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg) {
594         return irg->class_cast_state;
595 }
596
597 void set_irp_class_cast_state(ir_class_cast_state s) {
598         int i;
599         for (i = 0; i < get_irp_n_irgs(); ++i)
600                 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
601         irp->class_cast_state = s;
602 }
603
604 ir_class_cast_state get_irp_class_cast_state(void) {
605         return irp->class_cast_state;
606 }
607
608 char *get_class_cast_state_string(ir_class_cast_state s) {
609 #define X(a)    case a: return #a
610         switch(s) {
611         X(ir_class_casts_any);
612         X(ir_class_casts_transitive);
613         X(ir_class_casts_normalized);
614         X(ir_class_casts_state_max);
615         default: return "invalid class cast state";
616         }
617 #undef X
618 }
619
620 /* - State verification. ------------------------------------- */
621
622 typedef struct ccs_env {
623         ir_class_cast_state expected_state;
624         ir_class_cast_state worst_situation;
625 } ccs_env;
626
627 void verify_irn_class_cast_state(ir_node *n, void *env) {
628         ccs_env             *ccs = (ccs_env *)env;
629         ir_class_cast_state this_state = ir_class_casts_any;
630         ir_type             *fromtype, *totype;
631         int                 ref_depth = 0;
632
633         if (get_irn_op(n) != op_Cast) return;
634
635         fromtype = get_irn_typeinfo_type(get_Cast_op(n));
636         totype   = get_Cast_type(n);
637
638         while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
639                 totype   = get_pointer_points_to_type(totype);
640                 fromtype = get_pointer_points_to_type(fromtype);
641                 ref_depth++;
642         }
643
644         if (!is_Class_type(totype)) return;
645
646         if (is_SubClass_of(totype, fromtype) ||
647                 is_SubClass_of(fromtype, totype)   ) {
648                 this_state = ir_class_casts_transitive;
649                 if ((get_class_supertype_index(totype, fromtype) != -1) ||
650                     (get_class_supertype_index(fromtype, totype) != -1) ||
651                     fromtype == totype) {
652                         /*   Das ist doch alt?  Aus dem cvs aufgetaucht ...
653                         if ((get_class_supertype_index(totype, fromtype) == -1) &&
654                             (get_class_supertype_index(fromtype, totype) == -1) ) {  */
655                         this_state = ir_class_casts_normalized;
656                 }
657         }
658
659         if (!(this_state >= ccs->expected_state)) {
660                 ir_printf("  Node is %+F\n", n);
661                 ir_printf("    totype   %+F\n", totype);
662                 ir_printf("    fromtype %+F\n", fromtype);
663                 ir_printf("    this_state: %s, exp. state: %s\n",
664                         get_class_cast_state_string(this_state),
665                         get_class_cast_state_string(ccs->expected_state));
666                 assert(this_state >= ccs->expected_state &&
667                         "invalid state class cast state setting in graph");
668         }
669
670         if (this_state < ccs->worst_situation)
671                 ccs->worst_situation = this_state;
672 }
673
674
675 /** Verify that the graph meets requirements of state set. */
676 void verify_irg_class_cast_state(ir_graph *irg) {
677         ccs_env env;
678
679         env.expected_state  = get_irg_class_cast_state(irg);
680         env.worst_situation = ir_class_casts_normalized;
681
682         irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
683
684         if ((env.worst_situation > env.expected_state) && get_firm_verbosity()) {
685                 ir_printf("Note:  class cast state is set lower than reqired in graph \n\t%+F\n", irg);
686                 printf("       state is %s, reqired is %s\n",
687                         get_class_cast_state_string(env.expected_state),
688                         get_class_cast_state_string(env.worst_situation));
689         }
690 }