2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
24 * File name: ir/opt/tropt.c
25 * Purpose: Optimize the type representation.
26 * Author: Goetz Lindenmaier
30 * Copyright: (c) 2005 Universität Karlsruhe
32 * Perform optimizations of the type representation.
45 #include "tr_inheritance.h"
46 #include "irtypeinfo.h"
48 #include "irsimpletype.h"
55 /* - statistics ---------------------------------------------- */
57 static int n_casts_normalized = 0;
58 static int n_casts_removed = 0;
59 static int n_sels_concretized = 0;
61 /* - Cast normalization. ------------------------------------- */
63 static ir_type *default_gen_pointer_type_to(ir_type *tp);
65 #define PTR_TYPE_SUFFIX "cc_ptr_tp" /* class cast pointer type. */
66 static ident *ptr_type_suffix = NULL;
67 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
70 * Find a pointer type to a given type.
71 * Uses and updates trouts if available.
73 static ir_type *default_gen_pointer_type_to(ir_type *tp) {
75 if (get_trouts_state() == outs_consistent) {
76 if (get_type_n_pointertypes_to(tp) > 0) {
77 res = get_type_pointertype_to(tp, 0);
79 ir_mode *mode = is_Method_type(tp) ? mode_P_code : mode_P_data;
81 res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode);
82 /* Update trout for pointer types, so we can use it in next call. */
83 add_type_pointertype_to(tp, res);
87 res = find_pointer_type_to_type(tp);
88 if (res == firm_unknown_type)
89 res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode_P_data);
95 /** Return a type that is a depth times pointer to type. */
96 static ir_type *pointerize_type(ir_type *tp, int depth) {
97 for (; depth > 0; --depth) {
98 tp = gen_pointer_type_to(tp);
104 static ir_node *normalize_values_type(ir_type *totype, ir_node *pred) {
105 ir_type *fromtype = get_irn_typeinfo_type(pred);
106 ir_node *new_cast = pred;
109 if (totype == fromtype) return pred; /* Case for optimization! */
111 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
112 totype = get_pointer_points_to_type(totype);
113 fromtype = get_pointer_points_to_type(fromtype);
117 if (!is_Class_type(totype)) return pred;
118 if (!is_Class_type(fromtype)) return pred;
120 if ((get_class_supertype_index(totype, fromtype) != -1) ||
121 (get_class_supertype_index(fromtype, totype) != -1) ) {
122 /* It's just what we want ... */
126 set_cur_block(get_nodes_block(pred));
128 if (is_SubClass_of(totype, fromtype)) {
130 while (get_class_subtype_index(fromtype, totype) == -1) {
131 /* Insert a cast to a subtype of fromtype. */
132 ir_type *new_type = NULL;
134 int i, n_subtypes = get_class_n_subtypes(fromtype);
135 for (i = 0; i < n_subtypes && !new_type; ++i) {
136 ir_type *new_sub = get_class_subtype(fromtype, i);
137 if (is_SuperClass_of(new_sub, totype))
142 new_type = pointerize_type(new_type, ref_depth);
143 new_cast = new_Cast(pred, new_type);
145 n_casts_normalized ++;
146 set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
147 if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
151 assert(is_SuperClass_of(totype, fromtype));
153 while (get_class_supertype_index(fromtype, totype) == -1) {
154 /* Insert a cast to a supertype of fromtype. */
155 ir_type *new_type = NULL;
156 int i, n_supertypes = get_class_n_supertypes(fromtype);
157 for (i = 0; i < n_supertypes && !new_type; ++i) {
158 ir_type *new_super = get_class_supertype(fromtype, i);
159 if (is_SubClass_of(new_super, totype))
160 new_type = new_super;
164 new_type = pointerize_type(new_type, ref_depth);
165 new_cast = new_Cast(pred, new_type);
167 n_casts_normalized ++;
168 set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
169 if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
176 static void normalize_irn_class_cast(ir_node *n, void *env) {
178 if (get_irn_op(n) == op_Cast) {
179 ir_node *pred = get_Cast_op(n);
180 ir_type *totype = get_Cast_type(n);
181 res = normalize_values_type(totype, pred);
183 } else if (get_irn_op(n) == op_Call) {
184 int i, n_params = get_Call_n_params(n);
185 ir_type *tp = get_Call_type(n);
186 for (i = 0; i < n_params; ++i) {
187 res = normalize_values_type(get_method_param_type(tp, i), get_Call_param(n, i));
188 set_Call_param(n, i, res);
194 static void pure_normalize_irg_class_casts(ir_graph *irg) {
195 assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
196 "Cannot normalize irregular casts.");
197 if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
198 verify_irg_class_cast_state(irg);
202 irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
203 set_irg_class_cast_state(irg, ir_class_casts_normalized);
207 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct) {
208 assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
210 if (gppt_fct) gen_pointer_type_to = gppt_fct;
212 if (!ptr_type_suffix)
213 ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
215 pure_normalize_irg_class_casts(irg);
217 gen_pointer_type_to = default_gen_pointer_type_to;
220 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct) {
221 int i, n_irgs = get_irp_n_irgs();
222 if (gppt_fct) gen_pointer_type_to = gppt_fct;
224 if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
225 simple_analyse_types();
227 for (i = 0; i < n_irgs; ++i) {
228 pure_normalize_irg_class_casts(get_irp_irg(i));
231 set_irp_class_cast_state(ir_class_casts_normalized);
232 gen_pointer_type_to = default_gen_pointer_type_to;
234 if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
235 printf(" Cast normalization: %d Casts inserted.\n", n_casts_normalized);
241 /* - Cast optimization. ------------------------------------------- */
246 * (T1)(T2)x<T1> -> x<T1>
248 * (T3)(T2)x<T1> -> (T3)x<T1>
252 static void cancel_out_casts(ir_node *cast) {
253 ir_node *orig, *pred = get_Cast_op(cast);
254 ir_type *tp_cast, *tp_pred, *tp_orig;
257 if (get_irn_op(pred) != op_Cast) return;
258 orig = get_Cast_op(pred);
260 tp_cast = get_Cast_type(cast);
261 tp_pred = get_Cast_type(pred);
262 tp_orig = get_irn_typeinfo_type(orig);
264 while (is_Pointer_type(tp_cast) &&
265 is_Pointer_type(tp_pred) &&
266 is_Pointer_type(tp_orig) ) {
267 tp_cast = get_pointer_points_to_type(tp_cast);
268 tp_pred = get_pointer_points_to_type(tp_pred);
269 tp_orig = get_pointer_points_to_type(tp_orig);
273 if (!is_Class_type(tp_cast)) return;
274 if (!is_Class_type(tp_pred)) return;
275 if (!is_Class_type(tp_orig)) return;
277 if (is_SubClass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
280 if (tp_cast == tp_orig) {
281 exchange(cast, orig);
282 n_casts_removed += 2;
286 if (!(is_SubClass_of (tp_cast, tp_orig) || is_SubClass_of (tp_orig, tp_cast))) {
287 /* Avoid (B2)(A)(new B1()) --> (B2)(new B1())
288 * if B1 =!> B2 and B2 =!> B1
293 if ((is_SubClass_of (tp_cast, tp_pred) && is_SuperClass_of(tp_pred, tp_orig)) ||
294 (is_SuperClass_of(tp_cast, tp_pred) && is_SubClass_of (tp_pred, tp_orig)) ) {
295 /* Cast --> Pred --> Orig */
296 set_Cast_op (cast, orig);
301 static void concretize_selected_entity(ir_node *sel) {
302 ir_node *cast, *ptr = get_Sel_ptr(sel);
303 ir_type *orig_tp, *cast_tp;
304 ir_entity *new_ent, *sel_ent;
306 sel_ent = get_Sel_entity(sel);
307 cast = get_Sel_ptr(sel);
309 while (get_irn_op(cast) == op_Cast) {
310 cast_tp = get_Cast_type(cast);
311 ptr = get_Cast_op(cast);
312 orig_tp = get_irn_typeinfo_type(ptr);
314 if (!is_Pointer_type(orig_tp)) return;
315 if (!is_Pointer_type(cast_tp)) return;
316 orig_tp = get_pointer_points_to_type(orig_tp);
317 cast_tp = get_pointer_points_to_type(cast_tp);
318 if (!is_Class_type(orig_tp)) return;
319 if (!is_Class_type(cast_tp)) return;
321 /* We only want to concretize, but not generalize. */
322 if (!is_SuperClass_of(cast_tp, orig_tp)) return;
324 /* Hmm, we are not properly typed. */
325 if (get_class_member_index(cast_tp, sel_ent) == -1) return;
327 new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
329 /* New ent must be member of orig_tp. */
330 if (get_class_member_index(orig_tp, new_ent) == -1) return;
332 set_Sel_entity(sel, new_ent);
333 set_Sel_ptr(sel, ptr);
334 n_sels_concretized++;
341 static void concretize_Phi_type(ir_node *phi) {
342 int i, n_preds = get_Phi_n_preds(phi);
343 ir_node **pred = alloca(n_preds * sizeof(ir_node *));
345 ir_type *totype, *fromtype;
347 if (n_preds == 0) return;
348 pred[0] = get_Phi_pred(phi, 0);
350 if (get_irn_op(pred[0]) != op_Cast) return;
352 if (!is_Cast_upcast(pred[0])) return;
354 fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
355 totype = get_Cast_type(pred[0]);
357 pred[0] = get_Cast_op(pred[0]);
358 for (i = 1; i < n_preds; ++i) {
359 pred[i] = get_Phi_pred(phi, i);
360 if (get_irn_op(pred[i]) != op_Cast) return;
361 if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype) return;
362 pred[i] = get_Cast_op(pred[i]);
366 set_cur_block(get_nodes_block(phi));
367 new = new_Phi(n_preds, pred, get_irn_mode(phi));
368 set_irn_typeinfo_type(new, fromtype);
369 new = new_Cast(new, totype);
370 set_irn_typeinfo_type(new, totype);
374 void remove_Cmp_Null_cast(ir_node *cmp) {
375 ir_node *cast, *null, *new_null;
376 int cast_pos, null_pos;
379 cast = get_Cmp_left(cmp);
381 if (get_irn_op(cast) != op_Cast) {
384 cast = get_Cmp_right(cmp);
386 if (get_irn_op(cast) != op_Cast) return;
388 null = get_Cmp_right(cmp);
392 if (get_irn_op(null) != op_Const) return;
393 if (!mode_is_reference(get_irn_mode(null))) return;
394 if (get_Const_tarval(null) != get_mode_null(get_irn_mode(null))) return;
397 set_irn_n(cmp, cast_pos, get_Cast_op(cast));
398 fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
399 new_null = new_Const_type(get_Const_tarval(null), fromtype);
400 set_irn_typeinfo_type(new_null, fromtype);
401 set_irn_n(cmp, null_pos, new_null);
408 static void irn_optimize_class_cast(ir_node *n, void *env) {
409 if (get_irn_op(n) == op_Cast)
411 else if (get_irn_op(n) == op_Sel)
412 concretize_selected_entity(n);
413 else if (get_irn_op(n) == op_Phi)
414 concretize_Phi_type(n);
415 else if (get_irn_op(n) == op_Cmp)
416 remove_Cmp_Null_cast(n);
419 void optimize_class_casts(void) {
420 int i, n_irgs = get_irp_n_irgs();
422 if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
423 simple_analyse_types();
425 all_irg_walk(NULL, irn_optimize_class_cast, NULL);
427 set_trouts_inconsistent();
428 for (i = 0; i < n_irgs; ++i)
429 set_irg_outs_inconsistent(get_irp_irg(i));
431 if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
432 printf(" Cast optimization: %d Casts removed, %d Sels concretized.\n",
433 n_casts_removed, n_sels_concretized);