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