- ir_type *fromtype = get_irn_typeinfo_type(pred);
- ir_node *new_cast = pred;
- int ref_depth = 0;
-
- if (totype == fromtype) return pred; /* Case for optimization! */
-
- while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
- totype = get_pointer_points_to_type(totype);
- fromtype = get_pointer_points_to_type(fromtype);
- ref_depth++;
- }
-
- if (!is_Class_type(totype)) return pred;
- if (!is_Class_type(fromtype)) return pred;
-
- if ((get_class_supertype_index(totype, fromtype) != -1) ||
- (get_class_supertype_index(fromtype, totype) != -1) ) {
- /* It's just what we want ... */
- return pred;
- }
-
- set_cur_block(get_nodes_block(pred));
-
- if (is_SubClass_of(totype, fromtype)) {
- /* downcast */
- while (get_class_subtype_index(fromtype, totype) == -1) {
- /* Insert a cast to a subtype of fromtype. */
- ir_type *new_type = NULL;
- ir_node *new_cast;
- int i, n_subtypes = get_class_n_subtypes(fromtype);
- for (i = 0; i < n_subtypes && !new_type; ++i) {
- ir_type *new_sub = get_class_subtype(fromtype, i);
- if (is_SuperClass_of(new_sub, totype))
- new_type = new_sub;
- }
- assert(new_type);
- fromtype = new_type;
- new_type = pointerize_type(new_type, ref_depth);
- new_cast = new_Cast(pred, new_type);
- pred = new_cast;
- n_casts_normalized ++;
- set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
- if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
- }
- }
- else {
- assert(is_SuperClass_of(totype, fromtype));
- /* upcast */
- while (get_class_supertype_index(fromtype, totype) == -1) {
- /* Insert a cast to a supertype of fromtype. */
- ir_type *new_type = NULL;
- int i, n_supertypes = get_class_n_supertypes(fromtype);
- for (i = 0; i < n_supertypes && !new_type; ++i) {
- ir_type *new_super = get_class_supertype(fromtype, i);
- if (is_SubClass_of(new_super, totype))
- new_type = new_super;
- }
- assert(new_type);
- fromtype = new_type;
- new_type = pointerize_type(new_type, ref_depth);
- new_cast = new_Cast(pred, new_type);
- pred = new_cast;
- n_casts_normalized ++;
- set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
- if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
- }
- }
- return new_cast;
+ ir_type *fromtype = get_irn_typeinfo_type(pred);
+ ir_node *new_cast = pred;
+ int ref_depth = 0;
+
+ if (totype == fromtype) return pred; /* Case for optimization! */
+
+ while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
+ totype = get_pointer_points_to_type(totype);
+ fromtype = get_pointer_points_to_type(fromtype);
+ ref_depth++;
+ }
+
+ if (!is_Class_type(totype)) return pred;
+ if (!is_Class_type(fromtype)) return pred;
+
+ if ((get_class_supertype_index(totype, fromtype) != -1) ||
+ (get_class_supertype_index(fromtype, totype) != -1) ) {
+ /* It's just what we want ... */
+ return pred;
+ }
+
+ set_cur_block(get_nodes_block(pred));
+
+ if (is_SubClass_of(totype, fromtype)) {
+ /* downcast */
+ while (get_class_subtype_index(fromtype, totype) == -1) {
+ /* Insert a cast to a subtype of fromtype. */
+ ir_type *new_type = NULL;
+ ir_node *new_cast;
+ int i, n_subtypes = get_class_n_subtypes(fromtype);
+ for (i = 0; i < n_subtypes && !new_type; ++i) {
+ ir_type *new_sub = get_class_subtype(fromtype, i);
+ if (is_SuperClass_of(new_sub, totype))
+ new_type = new_sub;
+ }
+ assert(new_type);
+ fromtype = new_type;
+ new_type = pointerize_type(new_type, ref_depth);
+ new_cast = new_Cast(pred, new_type);
+ pred = new_cast;
+ n_casts_normalized ++;
+ set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
+ if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
+ }
+ } else {
+ assert(is_SuperClass_of(totype, fromtype));
+ /* upcast */
+ while (get_class_supertype_index(fromtype, totype) == -1) {
+ /* Insert a cast to a supertype of fromtype. */
+ ir_type *new_type = NULL;
+ int i, n_supertypes = get_class_n_supertypes(fromtype);
+ for (i = 0; i < n_supertypes && !new_type; ++i) {
+ ir_type *new_super = get_class_supertype(fromtype, i);
+ if (is_SubClass_of(new_super, totype))
+ new_type = new_super;
+ }
+ assert(new_type);
+ fromtype = new_type;
+ new_type = pointerize_type(new_type, ref_depth);
+ new_cast = new_Cast(pred, new_type);
+ pred = new_cast;
+ n_casts_normalized ++;
+ set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
+ if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
+ }
+ }
+ return new_cast;