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