5 * File name: ir/opt/tropt.c
6 * Purpose: Optimize the type representation.
7 * Author: Goetz Lindenmaier
11 * Copyright: (c) 2005 Universität Karlsruhe
12 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
14 * Perform optimizations of the type representation.
33 #include "tr_inheritance.h"
34 #include "irtypeinfo.h"
36 #include "irsimpletype.h"
42 /* - statistics ---------------------------------------------- */
44 static int n_casts_normalized = 0;
45 static int n_casts_removed = 0;
46 static int n_sels_concretized = 0;
48 /* - Cast normalization. ------------------------------------- */
50 static type *default_gen_pointer_type_to(type *tp);
52 #define PTR_TYPE_SUFFIX "cc_ptr_tp" /* class cast pointer type. */
53 static ident *ptr_type_suffix = NULL;
54 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
56 static type *default_gen_pointer_type_to(type *tp) {
58 if (get_trouts_state() == outs_consistent) {
59 if (get_type_n_pointertypes_to(tp) > 0) {
60 res = get_type_pointertype_to(tp, 0);
62 ir_mode *mode = is_Method_type(tp) ? mode_P_code : mode_P_data;
64 res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode);
65 /* Update trout for pointertypes, so we can use it in next call. */
66 add_type_pointertype_to(tp, res);
69 res = find_pointer_type_to_type(tp);
70 if (res == firm_unknown_type)
71 res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode_P_data);
77 /* Return a type that is a depth times pointer to type. */
78 static type *pointerize_type(type *tp, int depth) {
79 for (; depth > 0; --depth) {
80 tp = gen_pointer_type_to(tp);
86 static ir_node *normalize_values_type(type *totype, ir_node *pred) {
87 type *fromtype = get_irn_typeinfo_type(pred);
88 ir_node *new_cast = pred;
91 if (totype == fromtype) return pred; /* Case for optimization! */
93 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
94 totype = get_pointer_points_to_type(totype);
95 fromtype = get_pointer_points_to_type(fromtype);
99 if (!is_Class_type(totype)) return pred;
100 if (!is_Class_type(fromtype)) return pred;
102 if ((get_class_supertype_index(totype, fromtype) != -1) ||
103 (get_class_supertype_index(fromtype, totype) != -1) ) {
104 /* It's just what we want ... */
108 set_cur_block(get_nodes_block(pred));
110 if (is_subclass_of(totype, fromtype)) {
112 while (get_class_subtype_index(fromtype, totype) == -1) {
113 /* Insert a cast to a subtype of fromtype. */
114 type *new_type = NULL;
116 int i, n_subtypes = get_class_n_subtypes(fromtype);
117 for (i = 0; i < n_subtypes && !new_type; ++i) {
118 type *new_sub = get_class_subtype(fromtype, i);
119 if (is_superclass_of(new_sub, totype))
124 new_type = pointerize_type(new_type, ref_depth);
125 new_cast = new_Cast(pred, new_type);
127 n_casts_normalized ++;
128 set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
129 if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
133 assert(is_superclass_of(totype, fromtype));
135 while (get_class_supertype_index(fromtype, totype) == -1) {
136 /* Insert a cast to a supertype of fromtype. */
137 type *new_type = NULL;
138 int i, n_supertypes = get_class_n_supertypes(fromtype);
139 for (i = 0; i < n_supertypes && !new_type; ++i) {
140 type *new_super = get_class_supertype(fromtype, i);
141 if (is_subclass_of(new_super, totype))
142 new_type = new_super;
146 new_type = pointerize_type(new_type, ref_depth);
147 new_cast = new_Cast(pred, new_type);
149 n_casts_normalized ++;
150 set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
151 if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
158 static void normalize_irn_class_cast(ir_node *n, void *env) {
160 if (get_irn_op(n) == op_Cast) {
161 ir_node *pred = get_Cast_op(n);
162 type *totype = get_Cast_type(n);
163 res = normalize_values_type(totype, pred);
165 } else if (get_irn_op(n) == op_Call) {
166 int i, n_params = get_Call_n_params(n);
167 type *tp = get_Call_type(n);
168 for (i = 0; i < n_params; ++i) {
169 res = normalize_values_type(get_method_param_type(tp, i), get_Call_param(n, i));
170 set_Call_param(n, i, res);
176 static void pure_normalize_irg_class_casts(ir_graph *irg) {
177 assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
178 "Cannot normalize irregular casts.");
179 if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
180 verify_irg_class_cast_state(irg);
184 irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
185 set_irg_class_cast_state(irg, ir_class_casts_normalized);
189 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct) {
190 assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
192 if (gppt_fct) gen_pointer_type_to = gppt_fct;
194 if (!ptr_type_suffix)
195 ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
197 pure_normalize_irg_class_casts(irg);
199 gen_pointer_type_to = default_gen_pointer_type_to;
202 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct) {
203 int i, n_irgs = get_irp_n_irgs();
204 if (gppt_fct) gen_pointer_type_to = gppt_fct;
206 if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
207 simple_analyse_types();
209 for (i = 0; i < n_irgs; ++i) {
210 pure_normalize_irg_class_casts(get_irp_irg(i));
213 set_irp_class_cast_state(ir_class_casts_normalized);
214 gen_pointer_type_to = default_gen_pointer_type_to;
216 if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
217 printf(" Cast normalization: %d Casts inserted.\n", n_casts_normalized);
223 /* - Cast optimization. ------------------------------------------- */
225 static void cancel_out_casts(ir_node *cast) {
226 ir_node *orig, *pred = get_Cast_op(cast);
227 type *tp_cast, *tp_pred, *tp_orig;
230 if (get_irn_op(pred) != op_Cast) return;
231 orig = get_Cast_op(pred);
233 tp_cast = get_Cast_type(cast);
234 tp_pred = get_Cast_type(pred);
235 tp_orig = get_irn_typeinfo_type(orig);
237 while (is_Pointer_type(tp_cast) &&
238 is_Pointer_type(tp_pred) &&
239 is_Pointer_type(tp_orig) ) {
240 tp_cast = get_pointer_points_to_type(tp_cast);
241 tp_pred = get_pointer_points_to_type(tp_pred);
242 tp_orig = get_pointer_points_to_type(tp_orig);
246 if (!is_Class_type(tp_cast)) return;
247 if (!is_Class_type(tp_pred)) return;
248 if (!is_Class_type(tp_orig)) return;
250 if (is_subclass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
253 if (tp_cast == tp_orig) {
254 exchange(cast, orig);
255 n_casts_removed += 2;
259 if (!(is_subclass_of (tp_cast, tp_orig) || is_subclass_of (tp_orig, tp_cast)))
260 /* Avoid (B2)(A)(new B1()) --> (B2)(new B1()) */
263 if ((is_subclass_of (tp_cast, tp_pred) && is_superclass_of(tp_pred, tp_orig)) ||
264 (is_superclass_of(tp_cast, tp_pred) && is_subclass_of (tp_pred, tp_orig)) ) {
265 set_Cast_op (cast, orig);
270 static void concretize_selected_entity(ir_node *sel) {
271 ir_node *cast, *ptr = get_Sel_ptr(sel);
272 type *orig_tp, *cast_tp;
273 entity *new_ent, *sel_ent;
275 sel_ent = get_Sel_entity(sel);
276 cast = get_Sel_ptr(sel);
278 while (get_irn_op(cast) == op_Cast) {
279 cast_tp = get_Cast_type(cast);
280 ptr = get_Cast_op(cast);
281 orig_tp = get_irn_typeinfo_type(ptr);
283 if (!is_Pointer_type(orig_tp)) return;
284 if (!is_Pointer_type(cast_tp)) return;
285 orig_tp = get_pointer_points_to_type(orig_tp);
286 cast_tp = get_pointer_points_to_type(cast_tp);
287 if (!is_Class_type(orig_tp)) return;
288 if (!is_Class_type(cast_tp)) return;
290 /* We only want to contretize, but not generalize. */
291 if (!is_superclass_of(cast_tp, orig_tp)) return;
293 /* Hmm, we are not properly typed. */
294 if (get_class_member_index(cast_tp, sel_ent) == -1) return;
296 new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
298 /* New ent must be member of orig_tp. */
299 if (get_class_member_index(orig_tp, new_ent) == -1) return;
301 set_Sel_entity(sel, new_ent);
302 set_Sel_ptr(sel, ptr);
303 n_sels_concretized++;
310 static void concretize_Phi_type(ir_node *phi) {
311 int i, n_preds = get_Phi_n_preds(phi);
312 ir_node **pred = alloca(n_preds * sizeof(ir_node *));
314 type *totype, *fromtype;
316 if (n_preds == 0) return;
317 pred[0] = get_Phi_pred(phi, 0);
319 if (get_irn_op(pred[0]) != op_Cast) return;
321 if (!is_Cast_upcast(pred[0])) return;
323 fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
324 totype = get_Cast_type(pred[0]);
326 pred[0] = get_Cast_op(pred[0]);
327 for (i = 1; i < n_preds; ++i) {
328 pred[i] = get_Phi_pred(phi, i);
329 if (get_irn_op(pred[i]) != op_Cast) return;
330 if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype) return;
331 pred[i] = get_Cast_op(pred[i]);
335 set_cur_block(get_nodes_block(phi));
336 new = new_Phi(n_preds, pred, get_irn_mode(phi));
337 set_irn_typeinfo_type(new, fromtype);
338 new = new_Cast(new, totype);
339 set_irn_typeinfo_type(new, totype);
343 void remove_Cmp_Null_cast(ir_node *cmp) {
344 ir_node *cast, *null, *new_null;
345 int cast_pos, null_pos;
348 cast = get_Cmp_left(cmp);
350 if (get_irn_op(cast) != op_Cast) {
353 cast = get_Cmp_right(cmp);
355 if (get_irn_op(cast) != op_Cast) return;
357 null = get_Cmp_right(cmp);
361 if (get_irn_op(null) != op_Const) return;
362 if (!mode_is_reference(get_irn_mode(null))) return;
363 if (get_Const_tarval(null) != get_mode_null(get_irn_mode(null))) return;
366 set_irn_n(cmp, cast_pos, get_Cast_op(cast));
367 fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
368 new_null = new_Const_type(get_Const_tarval(null), fromtype);
369 set_irn_typeinfo_type(new_null, fromtype);
370 set_irn_n(cmp, null_pos, new_null);
374 static void irn_optimize_class_cast(ir_node *n, void *env) {
375 if (get_irn_op(n) == op_Cast)
377 else if (get_irn_op(n) == op_Sel)
378 concretize_selected_entity(n);
379 else if (get_irn_op(n) == op_Phi)
380 concretize_Phi_type(n);
381 else if (get_irn_op(n) == op_Cmp)
382 remove_Cmp_Null_cast(n);
385 void optimize_class_casts(void) {
386 int i, n_irgs = get_irp_n_irgs();
388 if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
389 simple_analyse_types();
391 all_irg_walk(NULL, irn_optimize_class_cast, NULL);
393 set_trouts_inconsistent();
394 for (i = 0; i < n_irgs; ++i)
395 set_irg_outs_inconsistent(get_irp_irg(i));
397 if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
398 printf(" Cast optimization: %d Casts removed, %d sels concretized.\n",
399 n_casts_removed, n_sels_concretized);