- removed useless spaces
[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         const 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 /**
156  * Compare two tr_inh_trans_tp entries.
157  */
158 static int tr_inh_trans_cmp(const void *e1, const void *e2, size_t size) {
159         const tr_inh_trans_tp *ef1 = e1;
160         const tr_inh_trans_tp *ef2 = e2;
161         (void) size;
162
163         return ef1->kind != ef2->kind;
164 }
165
166 /**
167  * calculate the hash value of an tr_inh_trans_tp
168  */
169 static INLINE unsigned int tr_inh_trans_hash(const tr_inh_trans_tp *v) {
170         return HASH_PTR(v->kind);
171 }
172
173 /* This always completes successfully. */
174 static tr_inh_trans_tp *get_firm_kind_entry(const firm_kind *k) {
175         tr_inh_trans_tp a, *found;
176         a.kind = k;
177
178         if (!tr_inh_trans_set) tr_inh_trans_set = new_set(tr_inh_trans_cmp, 128);
179
180         found = set_find(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
181         if (!found) {
182                 a.directions[d_up]   = pset_new_ptr(16);
183                 a.directions[d_down] = pset_new_ptr(16);
184                 found = set_insert(tr_inh_trans_set, &a, sizeof(a), tr_inh_trans_hash(&a));
185         }
186         return found;
187 }
188
189 static pset *get_entity_map(const ir_entity *ent, dir d) {
190         tr_inh_trans_tp *found;
191
192         assert(is_entity(ent));
193         found = get_firm_kind_entry((const firm_kind *)ent);
194         return found->directions[d];
195 }
196
197 static pset *get_type_map(const ir_type *tp, dir d) {
198         tr_inh_trans_tp *found;
199
200         assert(is_type(tp));
201         found = get_firm_kind_entry((const firm_kind *)tp);
202         return found->directions[d];
203 }
204
205
206 /**
207  * Walk over all types reachable from tp in the sub/supertype
208  * relation and compute the closure for the two downwards directed
209  * relations.
210  *
211  * The walk in the dag formed by the relation is tricky:  We must visit
212  * all subtypes before visiting the supertypes.  So we first walk down.
213  * Then we can compute the closure for this type.  Then we walk up.
214  * As we call ourselves recursive, and walk in both directions, there
215  * can be cycles.  So we have to make sure, that if we visit a node
216  * a second time (in a walk up) we do nothing.  For this we increment
217  * the master visited flag twice.
218  * If the type is marked with master_flag_visited-1 it is on the stack.
219  * If it is marked with master_flag_visited it is fully processed.
220  *
221  * Well, we still miss some candidates ... */
222 static void compute_down_closure(ir_type *tp) {
223         pset *myset, *subset;
224         int i, n_subtypes, n_members, n_supertypes;
225         unsigned long master_visited = get_master_type_visited();
226
227         assert(is_Class_type(tp));
228
229         set_type_visited(tp, master_visited-1);
230
231         /* Recursive descend. */
232         n_subtypes = get_class_n_subtypes(tp);
233         for (i = 0; i < n_subtypes; ++i) {
234                 ir_type *stp = get_class_subtype(tp, i);
235                 if (get_type_visited(stp) < master_visited-1) {
236                         compute_down_closure(stp);
237                 }
238         }
239
240         /* types */
241         myset = get_type_map(tp, d_down);
242         for (i = 0; i < n_subtypes; ++i) {
243                 ir_type *stp = get_class_subtype(tp, i);
244                 subset = get_type_map(stp, d_down);
245                 pset_insert_ptr(myset, stp);
246                 pset_insert_pset_ptr(myset, subset);
247         }
248
249         /* entities */
250         n_members = get_class_n_members(tp);
251         for (i = 0; i < n_members; ++i) {
252                 ir_entity *mem = get_class_member(tp, i);
253                 int j, n_overwrittenby = get_entity_n_overwrittenby(mem);
254
255                 myset = get_entity_map(mem, d_down);
256                 for (j = 0; j < n_overwrittenby; ++j) {
257                         ir_entity *ov = get_entity_overwrittenby(mem, j);
258                         subset = get_entity_map(ov, d_down);
259                         pset_insert_ptr(myset, ov);
260                         pset_insert_pset_ptr(myset, subset);
261                 }
262         }
263
264         mark_type_visited(tp);
265
266         /* Walk up. */
267         n_supertypes = get_class_n_supertypes(tp);
268         for (i = 0; i < n_supertypes; ++i) {
269                 ir_type *stp = get_class_supertype(tp, i);
270                 if (get_type_visited(stp) < master_visited-1) {
271                         compute_down_closure(stp);
272                 }
273         }
274 }
275
276 static void compute_up_closure(ir_type *tp) {
277         pset *myset, *subset;
278         int i, n_subtypes, n_members, n_supertypes;
279         unsigned long master_visited = get_master_type_visited();
280
281         assert(is_Class_type(tp));
282
283         set_type_visited(tp, master_visited-1);
284
285         /* Recursive descend. */
286         n_supertypes = get_class_n_supertypes(tp);
287         for (i = 0; i < n_supertypes; ++i) {
288                 ir_type *stp = get_class_supertype(tp, i);
289                 if (get_type_visited(stp) < get_master_type_visited()-1) {
290                         compute_up_closure(stp);
291                 }
292         }
293
294         /* types */
295         myset = get_type_map(tp, d_up);
296         for (i = 0; i < n_supertypes; ++i) {
297                 ir_type *stp = get_class_supertype(tp, i);
298                 subset = get_type_map(stp, d_up);
299                 pset_insert_ptr(myset, stp);
300                 pset_insert_pset_ptr(myset, subset);
301         }
302
303         /* entities */
304         n_members = get_class_n_members(tp);
305         for (i = 0; i < n_members; ++i) {
306                 ir_entity *mem = get_class_member(tp, i);
307                 int j, n_overwrites = get_entity_n_overwrites(mem);
308
309                 myset = get_entity_map(mem, d_up);
310                 for (j = 0; j < n_overwrites; ++j) {
311                         ir_entity *ov = get_entity_overwrites(mem, j);
312                         subset = get_entity_map(ov, d_up);
313                         pset_insert_pset_ptr(myset, subset);
314                         pset_insert_ptr(myset, ov);
315                 }
316         }
317
318         mark_type_visited(tp);
319
320         /* Walk down. */
321         n_subtypes = get_class_n_subtypes(tp);
322         for (i = 0; i < n_subtypes; ++i) {
323                 ir_type *stp = get_class_subtype(tp, i);
324                 if (get_type_visited(stp) < master_visited-1) {
325                         compute_up_closure(stp);
326                 }
327         }
328 }
329
330 /** Compute the transitive closure of the subclass/superclass and
331  *  overwrites/overwrittenby relation.
332  *
333  *  This function walks over the ir (O(#types+#entities)) to compute the
334  *  transitive closure.    */
335 void compute_inh_transitive_closure(void) {
336         int i, n_types = get_irp_n_types();
337         free_inh_transitive_closure();
338
339         /* The 'down' relation */
340         inc_master_type_visited();  /* Inc twice: one if on stack, second if values computed. */
341         inc_master_type_visited();
342         for (i = 0; i < n_types; ++i) {
343                 ir_type *tp = get_irp_type(i);
344                 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
345                         int j, n_subtypes = get_class_n_subtypes(tp);
346                         int has_unmarked_subtype = 0;
347
348                         assert(get_type_visited(tp) < get_master_type_visited()-1);
349                         for (j = 0; j < n_subtypes; ++j) {
350                                 ir_type *stp = get_class_subtype(tp, j);
351                                 if (type_not_visited(stp)) {
352                                         has_unmarked_subtype = 1;
353                                         break;
354                                 }
355                         }
356
357                         /* This is a good starting point. */
358                         if (!has_unmarked_subtype)
359                                 compute_down_closure(tp);
360                 }
361         }
362
363         /* The 'up' relation */
364         inc_master_type_visited();
365         inc_master_type_visited();
366         for (i = 0; i < n_types; ++i) {
367                 ir_type *tp = get_irp_type(i);
368                 if (is_Class_type(tp) && type_not_visited(tp)) { /* For others there is nothing to accumulate. */
369                         int j, n_supertypes = get_class_n_supertypes(tp);
370                         int has_unmarked_supertype = 0;
371
372                         assert(get_type_visited(tp) < get_master_type_visited()-1);
373                         for (j = 0; j < n_supertypes; ++j) {
374                                 ir_type *stp = get_class_supertype(tp, j);
375                                 if (type_not_visited(stp)) {
376                                         has_unmarked_supertype = 1;
377                                         break;
378                                 }
379                         }
380
381                         /* This is a good starting point. */
382                         if (!has_unmarked_supertype)
383                                 compute_up_closure(tp);
384                 }
385         }
386
387         irp->inh_trans_closure_state = inh_transitive_closure_valid;
388 }
389
390 /** Free memory occupied by the transitive closure information. */
391 void free_inh_transitive_closure(void) {
392         if (tr_inh_trans_set) {
393                 tr_inh_trans_tp *elt;
394                 for (elt = set_first(tr_inh_trans_set); elt; elt = set_next(tr_inh_trans_set)) {
395                         del_pset(elt->directions[d_up]);
396                         del_pset(elt->directions[d_down]);
397                 }
398                 del_set(tr_inh_trans_set);
399                 tr_inh_trans_set = NULL;
400         }
401         irp->inh_trans_closure_state = inh_transitive_closure_none;
402 }
403
404 /* - subtype ------------------------------------------------------------- */
405
406 ir_type *get_class_trans_subtype_first(const ir_type *tp) {
407         assert_valid_state();
408         return pset_first(get_type_map(tp, d_down));
409 }
410
411 ir_type *get_class_trans_subtype_next(const ir_type *tp) {
412         assert_valid_state();
413         return pset_next(get_type_map(tp, d_down));
414 }
415
416 int is_class_trans_subtype(const ir_type *tp, const ir_type *subtp) {
417         assert_valid_state();
418         return (pset_find_ptr(get_type_map(tp, d_down), subtp) != NULL);
419 }
420
421 /* - supertype ----------------------------------------------------------- */
422
423 ir_type *get_class_trans_supertype_first(const ir_type *tp) {
424         assert_valid_state();
425         return pset_first(get_type_map(tp, d_up));
426 }
427
428 ir_type *get_class_trans_supertype_next(const ir_type *tp) {
429         assert_valid_state();
430         return pset_next(get_type_map(tp, d_up));
431 }
432
433 /* - overwrittenby ------------------------------------------------------- */
434
435 ir_entity *get_entity_trans_overwrittenby_first(const ir_entity *ent) {
436         assert_valid_state();
437         return pset_first(get_entity_map(ent, d_down));
438 }
439
440 ir_entity *get_entity_trans_overwrittenby_next(const ir_entity *ent) {
441         assert_valid_state();
442         return pset_next(get_entity_map(ent, d_down));
443 }
444
445 /* - overwrites ---------------------------------------------------------- */
446
447
448 /** Iterate over all transitive overwritten entities. */
449 ir_entity *get_entity_trans_overwrites_first(const ir_entity *ent) {
450         assert_valid_state();
451         return pset_first(get_entity_map(ent, d_up));
452 }
453
454 ir_entity *get_entity_trans_overwrites_next(const ir_entity *ent) {
455         assert_valid_state();
456         return pset_next(get_entity_map(ent, d_up));
457 }
458
459
460 /* ----------------------------------------------------------------------- */
461 /* Classify pairs of types/entities in the inheritance relations.          */
462 /* ----------------------------------------------------------------------- */
463
464 /** Returns true if low is subclass of high. */
465 static int check_is_SubClass_of(ir_type *low, ir_type *high) {
466         int i, n_subtypes;
467
468         /* depth first search from high downwards. */
469         n_subtypes = get_class_n_subtypes(high);
470         for (i = 0; i < n_subtypes; i++) {
471                 ir_type *stp = get_class_subtype(high, i);
472                 if (low == stp) return 1;
473                 if (is_SubClass_of(low, stp))
474                         return 1;
475         }
476         return 0;
477 }
478
479 /* Returns true if low is subclass of high. */
480 int is_SubClass_of(ir_type *low, ir_type *high) {
481         assert(is_Class_type(low) && is_Class_type(high));
482
483         if (low == high) return 1;
484
485         if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
486                 pset *m = get_type_map(high, d_down);
487                 return pset_find_ptr(m, low) ? 1 : 0;
488         }
489         return check_is_SubClass_of(low, high);
490 }
491
492
493 /* Subclass check for pointers to classes.
494  *
495  *  Dereferences at both types the same amount of pointer types (as
496  *  many as possible).  If the remaining types are both class types
497  *  and subclasses, returns true, else false.  Can also be called with
498  *  two class types.  */
499 int is_SubClass_ptr_of(ir_type *low, ir_type *high) {
500         while (is_Pointer_type(low) && is_Pointer_type(high)) {
501                 low  = get_pointer_points_to_type(low);
502                 high = get_pointer_points_to_type(high);
503         }
504
505         if (is_Class_type(low) && is_Class_type(high))
506                 return is_SubClass_of(low, high);
507         return 0;
508 }
509
510 int is_overwritten_by(ir_entity *high, ir_entity *low) {
511         int i, n_overwrittenby;
512         assert(is_entity(low) && is_entity(high));
513
514         if (get_irp_inh_transitive_closure_state() == inh_transitive_closure_valid) {
515                 pset *m = get_entity_map(high, d_down);
516                 return pset_find_ptr(m, low) ? 1 : 0;
517         }
518
519         /* depth first search from high downwards. */
520         n_overwrittenby = get_entity_n_overwrittenby(high);
521         for (i = 0; i < n_overwrittenby; i++) {
522                 ir_entity *ov = get_entity_overwrittenby(high, i);
523                 if (low == ov) return 1;
524                 if (is_overwritten_by(low, ov))
525                         return 1;
526         }
527         return 0;
528 }
529
530 /** Resolve polymorphy in the inheritance relation.
531  *
532  * Returns the dynamically referenced entity if the static entity and the
533  * dynamic type are given.
534  * Search downwards in overwritten tree.
535  *
536  * Need two routines because I want to assert the result.
537  */
538 static ir_entity *do_resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
539         int i, n_overwrittenby;
540
541         if (get_entity_owner(static_ent) == dynamic_class) return static_ent;
542
543         n_overwrittenby = get_entity_n_overwrittenby(static_ent);
544         for (i = 0; i < n_overwrittenby; ++i) {
545                 ir_entity *ent = get_entity_overwrittenby(static_ent, i);
546                 ent = do_resolve_ent_polymorphy(dynamic_class, ent);
547                 if (ent) return ent;
548         }
549         return NULL;
550 }
551
552 /* Resolve polymorphy in the inheritance relation.
553  *
554  * Returns the dynamically referenced entity if the static entity and the
555  * dynamic type are given.
556  * Search downwards in overwritten tree. */
557 ir_entity *resolve_ent_polymorphy(ir_type *dynamic_class, ir_entity *static_ent) {
558         ir_entity *res;
559         assert(static_ent && is_entity(static_ent));
560
561         res = do_resolve_ent_polymorphy(dynamic_class, static_ent);
562         assert(res);
563
564         return res;
565 }
566
567
568
569 /* ----------------------------------------------------------------------- */
570 /* Class cast state handling.                                              */
571 /* ----------------------------------------------------------------------- */
572
573 /* - State handling. ----------------------------------------- */
574
575 void set_irg_class_cast_state(ir_graph *irg, ir_class_cast_state s) {
576         if (get_irp_class_cast_state() > s) set_irp_class_cast_state(s);
577         irg->class_cast_state = s;
578 }
579
580 ir_class_cast_state get_irg_class_cast_state(ir_graph *irg) {
581         return irg->class_cast_state;
582 }
583
584 void set_irp_class_cast_state(ir_class_cast_state s) {
585 #ifndef NDEBUG
586         int i;
587         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
588                 assert(get_irg_class_cast_state(get_irp_irg(i)) >= s);
589 #endif
590         irp->class_cast_state = s;
591 }
592
593 ir_class_cast_state get_irp_class_cast_state(void) {
594         return irp->class_cast_state;
595 }
596
597 char *get_class_cast_state_string(ir_class_cast_state s) {
598 #define X(a)    case a: return #a
599         switch(s) {
600         X(ir_class_casts_any);
601         X(ir_class_casts_transitive);
602         X(ir_class_casts_normalized);
603         X(ir_class_casts_state_max);
604         default: return "invalid class cast state";
605         }
606 #undef X
607 }
608
609 /* - State verification. ------------------------------------- */
610
611 typedef struct ccs_env {
612         ir_class_cast_state expected_state;
613         ir_class_cast_state worst_situation;
614 } ccs_env;
615
616 void verify_irn_class_cast_state(ir_node *n, void *env) {
617         ccs_env             *ccs = (ccs_env *)env;
618         ir_class_cast_state this_state = ir_class_casts_any;
619         ir_type             *fromtype, *totype;
620         int                 ref_depth = 0;
621
622         if (get_irn_op(n) != op_Cast) return;
623
624         fromtype = get_irn_typeinfo_type(get_Cast_op(n));
625         totype   = get_Cast_type(n);
626
627         while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
628                 totype   = get_pointer_points_to_type(totype);
629                 fromtype = get_pointer_points_to_type(fromtype);
630                 ref_depth++;
631         }
632
633         if (!is_Class_type(totype)) return;
634
635         if (is_SubClass_of(totype, fromtype) ||
636                 is_SubClass_of(fromtype, totype)   ) {
637                 this_state = ir_class_casts_transitive;
638                 if ((get_class_supertype_index(totype, fromtype) != -1) ||
639                     (get_class_supertype_index(fromtype, totype) != -1) ||
640                     fromtype == totype) {
641                         /*   Das ist doch alt?  Aus dem cvs aufgetaucht ...
642                         if ((get_class_supertype_index(totype, fromtype) == -1) &&
643                             (get_class_supertype_index(fromtype, totype) == -1) ) {  */
644                         this_state = ir_class_casts_normalized;
645                 }
646         }
647
648         if (!(this_state >= ccs->expected_state)) {
649                 ir_printf("  Node is %+F\n", n);
650                 ir_printf("    totype   %+F\n", totype);
651                 ir_printf("    fromtype %+F\n", fromtype);
652                 ir_printf("    this_state: %s, exp. state: %s\n",
653                         get_class_cast_state_string(this_state),
654                         get_class_cast_state_string(ccs->expected_state));
655                 assert(this_state >= ccs->expected_state &&
656                         "invalid state class cast state setting in graph");
657         }
658
659         if (this_state < ccs->worst_situation)
660                 ccs->worst_situation = this_state;
661 }
662
663 /** Verify that the graph meets requirements of state set. */
664 void verify_irg_class_cast_state(ir_graph *irg) {
665         ccs_env env;
666
667         env.expected_state  = get_irg_class_cast_state(irg);
668         env.worst_situation = ir_class_casts_normalized;
669
670         irg_walk_graph(irg, NULL, verify_irn_class_cast_state, &env);
671
672         if ((env.worst_situation > env.expected_state) && get_firm_verbosity()) {
673                 ir_printf("Note:  class cast state is set lower than reqired in graph \n\t%+F\n", irg);
674                 printf("       state is %s, reqired is %s\n",
675                         get_class_cast_state_string(env.expected_state),
676                         get_class_cast_state_string(env.worst_situation));
677         }
678 }