2 * Copyright (C) 1995-2008 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
22 * @brief Perform optimizations of the type representation.
24 * @author Goetz Lindenmaier
31 #include "iroptimize.h"
33 #include "irtypeinfo.h"
35 #include "irsimpletype.h"
44 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
46 /* - statistics ---------------------------------------------- */
48 static int n_casts_normalized = 0;
49 static int n_casts_removed = 0;
50 static int n_sels_concretized = 0;
52 /* - Cast normalization. ------------------------------------- */
54 static ir_type *default_gen_pointer_type_to(ir_type *tp);
56 #define PTR_TYPE_SUFFIX "cc_ptr_tp" /* class cast pointer type. */
57 static ident *ptr_type_suffix = NULL;
58 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
61 * Find a pointer type to a given type.
62 * Uses and updates trouts if available.
64 static ir_type *default_gen_pointer_type_to(ir_type *tp)
67 if (get_trouts_state() == outs_consistent) {
68 if (get_type_n_pointertypes_to(tp) > 0) {
69 res = get_type_pointertype_to(tp, 0);
71 res = new_type_pointer(tp);
72 /* Update trout for pointer types, so we can use it in next call. */
73 add_type_pointertype_to(tp, res);
76 res = find_pointer_type_to_type(tp);
77 if (res == firm_unknown_type)
78 res = new_type_pointer(tp);
84 /** Return a type that is a depth times pointer to type. */
85 static ir_type *pointerize_type(ir_type *tp, int depth)
87 for (; depth > 0; --depth) {
88 tp = gen_pointer_type_to(tp);
94 static ir_node *normalize_values_type(ir_type *totype, ir_node *pred)
96 ir_type *fromtype = get_irn_typeinfo_type(pred);
97 ir_node *new_cast = pred;
101 if (totype == fromtype) return pred; /* Case for optimization! */
103 while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
104 totype = get_pointer_points_to_type(totype);
105 fromtype = get_pointer_points_to_type(fromtype);
109 if (!is_Class_type(totype)) return pred;
110 if (!is_Class_type(fromtype)) return pred;
112 if ((get_class_supertype_index(totype, fromtype) != -1) ||
113 (get_class_supertype_index(fromtype, totype) != -1) ) {
114 /* It's just what we want ... */
118 block = get_nodes_block(pred);
120 if (is_SubClass_of(totype, fromtype)) {
122 while (get_class_subtype_index(fromtype, totype) == -1) {
123 /* Insert a cast to a subtype of fromtype. */
124 ir_type *new_type = NULL;
126 int i, n_subtypes = get_class_n_subtypes(fromtype);
127 for (i = 0; i < n_subtypes && !new_type; ++i) {
128 ir_type *new_sub = get_class_subtype(fromtype, i);
129 if (is_SuperClass_of(new_sub, totype))
134 new_type = pointerize_type(new_type, ref_depth);
135 new_cast = new_r_Cast(block, pred, new_type);
137 n_casts_normalized ++;
138 set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
139 if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
142 assert(is_SuperClass_of(totype, fromtype));
144 while (get_class_supertype_index(fromtype, totype) == -1) {
145 /* Insert a cast to a supertype of fromtype. */
146 ir_type *new_type = NULL;
147 int i, n_supertypes = get_class_n_supertypes(fromtype);
148 for (i = 0; i < n_supertypes && !new_type; ++i) {
149 ir_type *new_super = get_class_supertype(fromtype, i);
150 if (is_SubClass_of(new_super, totype))
151 new_type = new_super;
155 new_type = pointerize_type(new_type, ref_depth);
156 new_cast = new_r_Cast(block, pred, new_type);
158 n_casts_normalized ++;
159 set_irn_typeinfo_type(new_cast, new_type); /* keep type information up to date. */
160 if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
169 static void normalize_irn_class_cast(ir_node *n, void *env)
174 ir_node *pred = get_Cast_op(n);
175 ir_type *totype = get_Cast_type(n);
176 res = normalize_values_type(totype, pred);
178 } else if (is_Call(n)) {
179 int i, n_params = get_Call_n_params(n);
180 ir_type *tp = get_Call_type(n);
181 for (i = 0; i < n_params; ++i) {
182 res = normalize_values_type(get_method_param_type(tp, i), get_Call_param(n, i));
183 set_Call_param(n, i, res);
189 static void pure_normalize_irg_class_casts(ir_graph *irg)
191 assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
192 "Cannot normalize irregular casts.");
193 if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
194 verify_irg_class_cast_state(irg);
198 irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
199 set_irg_class_cast_state(irg, ir_class_casts_normalized);
203 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct)
205 assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
207 if (gppt_fct) gen_pointer_type_to = gppt_fct;
209 if (!ptr_type_suffix)
210 ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
212 pure_normalize_irg_class_casts(irg);
214 gen_pointer_type_to = default_gen_pointer_type_to;
217 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct)
220 if (gppt_fct) gen_pointer_type_to = gppt_fct;
222 if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
223 simple_analyse_types();
225 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
226 pure_normalize_irg_class_casts(get_irp_irg(i));
229 set_irp_class_cast_state(ir_class_casts_normalized);
230 gen_pointer_type_to = default_gen_pointer_type_to;
232 DB((dbg, SET_LEVEL_1, " Cast normalization: %d Casts inserted.\n", n_casts_normalized));
237 /* - Cast optimization. ------------------------------------------- */
242 * (T1)(T2)x<T1> -> x<T1>
244 * (T3)(T2)x<T1> -> (T3)x<T1>
248 * @param cast the Cast node
250 * @return 1 if the cast was changed
252 static int cancel_out_casts(ir_node *cast)
254 ir_node *orig, *pred = get_Cast_op(cast);
255 ir_type *tp_cast, *tp_pred, *tp_orig;
257 if (!is_Cast(pred)) return 0;
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);
272 if (!is_Class_type(tp_cast) || !is_Class_type(tp_pred) || !is_Class_type(tp_orig))
275 if (is_SubClass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
278 if (tp_cast == tp_orig) {
279 exchange(cast, orig);
280 n_casts_removed += 2;
284 if (!(is_SubClass_of (tp_cast, tp_orig) || is_SubClass_of (tp_orig, tp_cast))) {
285 /* Avoid (B2)(A)(new B1()) --> (B2)(new B1())
286 * if B1 =!> B2 and B2 =!> B1
291 if ((is_SubClass_of (tp_cast, tp_pred) && is_SuperClass_of(tp_pred, tp_orig)) ||
292 (is_SuperClass_of(tp_cast, tp_pred) && is_SubClass_of (tp_pred, tp_orig))) {
293 /* Cast --> Pred --> Orig */
294 set_Cast_op(cast, orig);
302 * Optimize Sel(Cast(type, ptr), ent) into Sel(ptr_type, ent_type)
304 * @param sel the Sel node
306 * @return 1 if Cast's where removed
308 static int concretize_selected_entity(ir_node *sel)
310 ir_node *cast, *ptr = get_Sel_ptr(sel);
311 ir_type *orig_tp, *cast_tp;
312 ir_entity *new_ent, *sel_ent;
315 sel_ent = get_Sel_entity(sel);
316 cast = get_Sel_ptr(sel);
318 while (is_Cast(cast)) {
319 cast_tp = get_Cast_type(cast);
320 ptr = get_Cast_op(cast);
321 orig_tp = get_irn_typeinfo_type(ptr);
323 /* we handle only classes */
324 if (!is_Pointer_type(orig_tp)|| !is_Pointer_type(cast_tp))
326 orig_tp = get_pointer_points_to_type(orig_tp);
327 cast_tp = get_pointer_points_to_type(cast_tp);
328 if (!is_Class_type(orig_tp) || !is_Class_type(cast_tp))
331 /* We only want to concretize, but not generalize. */
332 if (!is_SuperClass_of(cast_tp, orig_tp))
335 /* The sel entity should be a member of the cast_tp, else
336 the graph was not properly typed. */
337 if (get_class_member_index(cast_tp, sel_ent) == -1)
340 new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
342 /* New ent must be member of orig_tp. */
343 if (get_class_member_index(orig_tp, new_ent) == -1)
346 /* all checks done, we can remove the Cast and update the Sel */
347 set_Sel_entity(sel, new_ent);
348 set_Sel_ptr(sel, ptr);
349 ++n_sels_concretized;
359 * Move Casts of the same type through a Phi node, i.e.
360 * Phi(Cast(type, x_0), ..., Cast(type, x_n)) -> Cast(type, Phi(x_0, ..., x_n))
362 * @param phi the Phi node
364 * @return 1 if Cast's where moved
366 static int concretize_Phi_type(ir_node *phi)
368 int n_preds = get_Phi_n_preds(phi);
369 ir_node **pred = ALLOCAN(ir_node*, n_preds);
377 pred[0] = get_Phi_pred(phi, 0);
379 if (!is_Cast(pred[0]))
382 if (!is_Cast_upcast(pred[0]))
385 fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
386 totype = get_Cast_type(pred[0]);
388 pred[0] = get_Cast_op(pred[0]);
389 for (i = 1; i < n_preds; ++i) {
390 pred[i] = get_Phi_pred(phi, i);
391 if (!is_Cast(pred[i]))
393 if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype)
395 pred[i] = get_Cast_op(pred[i]);
399 blk = get_nodes_block(phi);
400 nn = new_r_Phi(blk, n_preds, pred, get_irn_mode(phi));
401 set_irn_typeinfo_type(nn, fromtype);
402 nn = new_r_Cast(blk, nn, totype);
403 set_irn_typeinfo_type(nn, totype);
409 * Remove Casted null checks, i.e.
411 * Cmp(Cast(type, x_orig), NULL_type) -> Cmp(x_orig, NULL_orig)
413 * @param cmp the Cmp node
415 * @return 1 if Cast's where removed
417 static int remove_Cmp_Null_cast(ir_node *cmp)
420 ir_node *cast, *null, *new_null;
421 int cast_pos, null_pos;
425 cast = get_Cmp_left(cmp);
426 if (!is_Cast(cast)) {
429 cast = get_Cmp_right(cmp);
434 null = get_Cmp_right(cmp);
439 if (! is_Const(null))
441 mode = get_irn_mode(null);
442 if (!mode_is_reference(mode))
444 if (get_Const_tarval(null) != get_mode_null(mode))
448 irg = get_irn_irg(cmp);
449 set_irn_n(cmp, cast_pos, get_Cast_op(cast));
450 fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
451 new_null = new_r_Const_type(irg, get_Const_tarval(null), fromtype);
452 set_irn_typeinfo_type(new_null, fromtype);
453 set_irn_n(cmp, null_pos, new_null);
459 * Post-Walker: Optimize class casts (mostly by trying to remove them)
461 static void irn_optimize_class_cast(ir_node *n, void *env)
466 *changed |= cancel_out_casts(n);
468 *changed |= concretize_selected_entity(n);
470 *changed |= concretize_Phi_type(n);
472 *changed |= remove_Cmp_Null_cast(n);
475 void optimize_class_casts(void)
479 if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
480 simple_analyse_types();
483 all_irg_walk(NULL, irn_optimize_class_cast, &changed);
488 set_trouts_inconsistent();
489 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
490 set_irg_outs_inconsistent(get_irp_irg(i));
493 DB((dbg, SET_LEVEL_1, " Cast optimization: %d Casts removed, %d Sels concretized.\n",
494 n_casts_removed, n_sels_concretized));
497 void firm_init_class_casts_opt(void)
499 FIRM_DBG_REGISTER(dbg, "firm.opt.tropt");