6 * File name: ir/opt/tropt.h
7 * Purpose: Optimize the type representation.
8 * Author: Goetz Lindenmaier
12 * Copyright: (c) 2005 Universität Karlsruhe
13 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * Perform optimizations of the type representation.
27 #include "tr_inheritance.h"
28 #include "irtypeinfo.h"
30 #include "irsimpletype.h"
36 /* - statistics ---------------------------------------------- */
38 static int n_casts_normalized = 0;
39 static int n_casts_removed = 0;
40 static int n_sels_concretized = 0;
42 /* - Cast normalization. ------------------------------------- */
44 static type *default_gen_pointer_type_to(type *tp);
46 #define PTR_TYPE_SUFFIX "cc_ptr_tp" /* class cast pointer type. */
47 static ident *ptr_type_suffix = NULL;
48 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
50 static type *default_gen_pointer_type_to(type *tp) {
52 if (get_trouts_state() == outs_consistent) {
53 if (get_type_n_pointertypes_to(tp) > 0) {
54 res = get_type_pointertype_to(tp, 0);
56 res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp);
57 /* Update trout for pointertypes, so we can use it in next call. */
58 add_type_pointertype_to(tp, res);
61 res = find_pointer_type_to_type(tp);
62 if (res == firm_unknown_type)
63 res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp);
69 /* Return a type that is a depth times pointer to type. */
70 static type *pointerize_type(type *tp, int depth) {
71 for (; depth > 0; --depth) {
72 tp = gen_pointer_type_to(tp);
77 static void normalize_irn_class_cast(ir_node *n, void *env) {
78 if (get_irn_op(n) != op_Cast) return;
81 type *fromtype = get_irn_typeinfo_type(get_Cast_op(n));
82 type *totype = get_Cast_type(n);
85 if (totype == fromtype) return; /* Case for optimization! */
87 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
88 totype = get_pointer_points_to_type(totype);
89 fromtype = get_pointer_points_to_type(fromtype);
93 if (!is_Class_type(totype)) return;
94 if (!is_Class_type(fromtype)) return;
96 if ((get_class_supertype_index(totype, fromtype) != -1) ||
97 (get_class_supertype_index(fromtype, totype) != -1) ) {
98 /* It's just what we want ... */
102 set_cur_block(get_nodes_block(n));
104 if (is_subclass_of(totype, fromtype)) {
106 while (get_class_subtype_index(fromtype, totype) == -1) {
107 /* Insert a cast to a subtype of fromtype. */
108 type *new_type = NULL;
110 int i, n_subtypes = get_class_n_subtypes(fromtype);
111 for (i = 0; i < n_subtypes && !new_type; ++i) {
112 type *new_sub = get_class_subtype(fromtype, i);
113 if (is_superclass_of(new_sub, totype))
118 new_type = pointerize_type(new_type, ref_depth);
119 new_cast = new_Cast(get_Cast_op(n), new_type);
120 n_casts_normalized ++;
121 set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
122 set_Cast_op(n, new_cast);
123 if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
127 assert(is_superclass_of(totype, fromtype));
129 while (get_class_supertype_index(fromtype, totype) == -1) {
130 /* Insert a cast to a supertype of fromtype. */
131 type *new_type = NULL;
133 int i, n_supertypes = get_class_n_supertypes(fromtype);
134 for (i = 0; i < n_supertypes && !new_type; ++i) {
135 type *new_super = get_class_supertype(fromtype, i);
136 if (is_subclass_of(new_super, totype))
137 new_type = new_super;
141 new_type = pointerize_type(new_type, ref_depth);
142 new_cast = new_Cast(get_Cast_op(n), new_type);
143 n_casts_normalized ++;
144 set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
145 if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
146 set_Cast_op(n, new_cast);
152 static void pure_normalize_irg_class_casts(ir_graph *irg) {
153 assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
154 "Cannot normalize irregular casts.");
155 if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
156 verify_irg_class_cast_state(irg);
160 irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
161 set_irg_class_cast_state(irg, ir_class_casts_normalized);
165 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct) {
166 assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
168 if (gppt_fct) gen_pointer_type_to = gppt_fct;
170 if (!ptr_type_suffix)
171 ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
173 pure_normalize_irg_class_casts(irg);
175 gen_pointer_type_to = default_gen_pointer_type_to;
178 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct) {
179 int i, n_irgs = get_irp_n_irgs();
180 if (gppt_fct) gen_pointer_type_to = gppt_fct;
182 if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
183 simple_analyse_types();
185 for (i = 0; i < n_irgs; ++i) {
186 pure_normalize_irg_class_casts(get_irp_irg(i));
189 set_irp_class_cast_state(ir_class_casts_normalized);
190 gen_pointer_type_to = default_gen_pointer_type_to;
192 if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
193 printf(" Cast normalization: %d Casts inserted.\n", n_casts_normalized);
199 /* - Cast optimization. ------------------------------------------- */
201 static void cancel_out_casts(ir_node *cast) {
202 ir_node *orig, *pred = get_Cast_op(cast);
203 type *tp_cast, *tp_pred, *tp_orig;
206 if (get_irn_op(pred) != op_Cast) return;
207 orig = get_Cast_op(pred);
209 tp_cast = get_Cast_type(cast);
210 tp_pred = get_Cast_type(pred);
211 tp_orig = get_irn_typeinfo_type(orig);
213 while (is_Pointer_type(tp_cast) &&
214 is_Pointer_type(tp_pred) &&
215 is_Pointer_type(tp_orig) ) {
216 tp_cast = get_pointer_points_to_type(tp_cast);
217 tp_pred = get_pointer_points_to_type(tp_pred);
218 tp_orig = get_pointer_points_to_type(tp_orig);
222 if (!is_Class_type(tp_cast)) return;
223 if (!is_Class_type(tp_pred)) return;
224 if (!is_Class_type(tp_orig)) return;
226 if (is_subclass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
229 if (tp_cast == tp_orig) {
230 exchange(cast, orig);
231 n_casts_removed += 2;
235 if (!(is_subclass_of (tp_cast, tp_orig) || is_subclass_of (tp_orig, tp_cast)))
236 /* Avoid (B2)(A)(new B1()) --> (B2)(new B1()) */
239 if ((is_subclass_of (tp_cast, tp_pred) && is_superclass_of(tp_pred, tp_orig)) ||
240 (is_superclass_of(tp_cast, tp_pred) && is_subclass_of (tp_pred, tp_orig)) ) {
241 set_Cast_op (cast, orig);
246 static void concretize_selected_entity(ir_node *sel) {
247 ir_node *cast, *ptr = get_Sel_ptr(sel);
248 type *orig_tp, *cast_tp;
249 entity *new_ent, *sel_ent;
251 sel_ent = get_Sel_entity(sel);
252 cast = get_Sel_ptr(sel);
254 while (get_irn_op(cast) == op_Cast) {
255 cast_tp = get_Cast_type(cast);
256 ptr = get_Cast_op(cast);
257 orig_tp = get_irn_typeinfo_type(ptr);
259 if (!is_Pointer_type(orig_tp)) return;
260 if (!is_Pointer_type(cast_tp)) return;
261 orig_tp = get_pointer_points_to_type(orig_tp);
262 cast_tp = get_pointer_points_to_type(cast_tp);
263 if (!is_Class_type(orig_tp)) return;
264 if (!is_Class_type(cast_tp)) return;
266 /* We only want to contretize, but not generalize. */
267 if (!is_superclass_of(cast_tp, orig_tp)) return;
269 /* Hmm, we are not properly typed. */
270 if (get_class_member_index(cast_tp, sel_ent) == -1) return;
272 new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
274 /* New ent must be member of orig_tp. */
275 if (get_class_member_index(orig_tp, new_ent) == -1) return;
277 set_Sel_entity(sel, new_ent);
278 set_Sel_ptr(sel, ptr);
279 n_sels_concretized++;
286 static void concretize_Phi_type(ir_node *phi) {
287 int i, n_preds = get_Phi_n_preds(phi);
288 ir_node **pred = alloca(n_preds * sizeof(ir_node *));
290 type *totype, *fromtype;
292 if (n_preds == 0) return;
293 pred[0] = get_Phi_pred(phi, 0);
295 if (get_irn_op(pred[0]) != op_Cast) return;
297 if (!is_Cast_upcast(pred[0])) return;
299 fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
300 totype = get_Cast_type(pred[0]);
302 pred[0] = get_Cast_op(pred[0]);
303 for (i = 1; i < n_preds; ++i) {
304 pred[i] = get_Phi_pred(phi, i);
305 if (get_irn_op(pred[i]) != op_Cast) return;
306 if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype) return;
307 pred[i] = get_Cast_op(pred[i]);
311 set_cur_block(get_nodes_block(phi));
312 new = new_Phi(n_preds, pred, get_irn_mode(phi));
313 set_irn_typeinfo_type(new, fromtype);
314 new = new_Cast(new, totype);
315 set_irn_typeinfo_type(new, totype);
319 void remove_Cmp_Null_cast(ir_node *cmp) {
320 ir_node *cast, *null, *new_null;
321 int cast_pos, null_pos;
324 cast = get_Cmp_left(cmp);
326 if (get_irn_op(cast) != op_Cast) {
329 cast = get_Cmp_right(cmp);
331 if (get_irn_op(cast) != op_Cast) return;
333 null = get_Cmp_right(cmp);
337 if (get_irn_op(null) != op_Const) return;
338 if (!mode_is_reference(get_irn_mode(null))) return;
339 if (get_Const_tarval(null) != get_mode_null(get_irn_mode(null))) return;
342 set_irn_n(cmp, cast_pos, get_Cast_op(cast));
343 fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
344 new_null = new_Const_type(get_Const_tarval(null), fromtype);
345 set_irn_typeinfo_type(new_null, fromtype);
346 set_irn_n(cmp, null_pos, new_null);
350 static void irn_optimize_class_cast(ir_node *n, void *env) {
351 if (get_irn_op(n) == op_Cast)
353 else if (get_irn_op(n) == op_Sel)
354 concretize_selected_entity(n);
355 else if (get_irn_op(n) == op_Phi)
356 concretize_Phi_type(n);
357 else if (get_irn_op(n) == op_Cmp)
358 remove_Cmp_Null_cast(n);
361 void optimize_class_casts(void) {
362 int i, n_irgs = get_irp_n_irgs();
364 if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
365 simple_analyse_types();
367 all_irg_walk(NULL, irn_optimize_class_cast, NULL);
369 set_trouts_inconsistent();
370 for (i = 0; i < n_irgs; ++i)
371 set_irg_outs_inconsistent(get_irp_irg(i));
373 if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
374 printf(" Cast optimization: %d Casts removed, %d sels concretized.\n",
375 n_casts_removed, n_sels_concretized);