- BugFix: removed missing debugging artefact, causing partitions with
[libfirm] / ir / opt / tropt.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Perform optimizations of the type representation.
23  * @date    20.4.2005
24  * @author  Goetz Lindenmaier
25  * @version $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <assert.h>
32
33 #include "iroptimize.h"
34 #include "irprog.h"
35 #include "irtypeinfo.h"
36 #include "irgwalk.h"
37 #include "irsimpletype.h"
38 #include "trouts.h"
39 #include "ircons.h"
40 #include "irgmod.h"
41 #include "irflag_t.h"
42 #include "xmalloc.h"
43 #include "debug.h"
44 #include "tropt.h"
45
46 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
47
48 /* - statistics ---------------------------------------------- */
49
50 static int n_casts_normalized = 0;
51 static int n_casts_removed    = 0;
52 static int n_sels_concretized = 0;
53
54 /* - Cast normalization. ------------------------------------- */
55
56 static ir_type *default_gen_pointer_type_to(ir_type *tp);
57
58 #define PTR_TYPE_SUFFIX "cc_ptr_tp"  /* class cast pointer type. */
59 static ident *ptr_type_suffix = NULL;
60 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
61
62 /**
63  * Find a pointer type to a given type.
64  * Uses and updates trouts if available.
65  */
66 static ir_type *default_gen_pointer_type_to(ir_type *tp) {
67         ir_type *res = NULL;
68         if (get_trouts_state() == outs_consistent) {
69                 if (get_type_n_pointertypes_to(tp) > 0) {
70                         res = get_type_pointertype_to(tp, 0);
71                 } else {
72                         ir_mode *mode = is_Method_type(tp) ? mode_P_code : mode_P_data;
73
74                         res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode);
75                         /* Update trout for pointer types, so we can use it in next call. */
76                         add_type_pointertype_to(tp, res);
77                 }
78         }
79         else {
80                 res = find_pointer_type_to_type(tp);
81                 if (res == firm_unknown_type)
82                         res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode_P_data);
83         }
84
85         return res;
86 }
87
88 /** Return a type that is a depth times pointer to type. */
89 static ir_type *pointerize_type(ir_type *tp, int depth) {
90         for (; depth > 0; --depth) {
91                 tp = gen_pointer_type_to(tp);
92         }
93         return tp;
94 }
95
96
97 static ir_node *normalize_values_type(ir_type *totype, ir_node *pred) {
98         ir_type *fromtype = get_irn_typeinfo_type(pred);
99         ir_node *new_cast = pred;
100         int ref_depth = 0;
101
102         if (totype == fromtype) return pred;   /* Case for optimization! */
103
104         while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
105                 totype   = get_pointer_points_to_type(totype);
106                 fromtype = get_pointer_points_to_type(fromtype);
107                 ref_depth++;
108         }
109
110         if (!is_Class_type(totype))   return pred;
111         if (!is_Class_type(fromtype)) return pred;
112
113         if ((get_class_supertype_index(totype, fromtype) != -1) ||
114                 (get_class_supertype_index(fromtype, totype) != -1) ) {
115                         /* It's just what we want ... */
116                         return pred;
117         }
118
119         set_cur_block(get_nodes_block(pred));
120
121         if (is_SubClass_of(totype, fromtype)) {
122                 /* downcast */
123                 while (get_class_subtype_index(fromtype, totype) == -1) {
124                         /* Insert a cast to a subtype of fromtype. */
125                         ir_type *new_type = NULL;
126                         ir_node *new_cast;
127                         int i, n_subtypes = get_class_n_subtypes(fromtype);
128                         for (i = 0; i < n_subtypes && !new_type; ++i) {
129                                 ir_type *new_sub = get_class_subtype(fromtype, i);
130                                 if (is_SuperClass_of(new_sub, totype))
131                                         new_type = new_sub;
132                         }
133                         assert(new_type);
134                         fromtype = new_type;
135                         new_type = pointerize_type(new_type, ref_depth);
136                         new_cast = new_Cast(pred, new_type);
137                         pred = new_cast;
138                         n_casts_normalized ++;
139                         set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
140                         if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
141                 }
142         } else {
143                 assert(is_SuperClass_of(totype, fromtype));
144                 /* upcast */
145                 while (get_class_supertype_index(fromtype, totype) == -1) {
146                         /* Insert a cast to a supertype of fromtype. */
147                         ir_type *new_type = NULL;
148                         int i, n_supertypes = get_class_n_supertypes(fromtype);
149                         for (i = 0; i < n_supertypes && !new_type; ++i) {
150                                 ir_type *new_super = get_class_supertype(fromtype, i);
151                                 if (is_SubClass_of(new_super, totype))
152                                         new_type = new_super;
153                         }
154                         assert(new_type);
155                         fromtype = new_type;
156                         new_type = pointerize_type(new_type, ref_depth);
157                         new_cast = new_Cast(pred, new_type);
158                         pred = new_cast;
159                         n_casts_normalized ++;
160                         set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
161                         if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
162                 }
163         }
164         return new_cast;
165 }
166
167 /**
168  * Post-Walker.
169  */
170 static void normalize_irn_class_cast(ir_node *n, void *env) {
171         ir_node *res;
172         (void) env;
173         if (is_Cast(n)) {
174                 ir_node *pred   = get_Cast_op(n);
175                 ir_type *totype = get_Cast_type(n);
176                 res = normalize_values_type(totype, pred);
177                 set_Cast_op(n, res);
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);
184                 }
185         }
186 }
187
188
189 static void pure_normalize_irg_class_casts(ir_graph *irg) {
190         assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
191                 "Cannot normalize irregular casts.");
192         if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
193                 verify_irg_class_cast_state(irg);
194                 return;
195         }
196
197         irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
198         set_irg_class_cast_state(irg, ir_class_casts_normalized);
199 }
200
201
202 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct) {
203         assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
204
205         if (gppt_fct) gen_pointer_type_to = gppt_fct;
206
207         if (!ptr_type_suffix)
208                 ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
209
210         pure_normalize_irg_class_casts(irg);
211
212         gen_pointer_type_to = default_gen_pointer_type_to;
213 }
214
215 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct) {
216         int i, n_irgs = get_irp_n_irgs();
217         if (gppt_fct) gen_pointer_type_to = gppt_fct;
218
219         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
220                 simple_analyse_types();
221
222         for (i = 0; i < n_irgs; ++i) {
223                 pure_normalize_irg_class_casts(get_irp_irg(i));
224         }
225
226         set_irp_class_cast_state(ir_class_casts_normalized);
227         gen_pointer_type_to = default_gen_pointer_type_to;
228
229         DB((dbg, SET_LEVEL_1, " Cast normalization: %d Casts inserted.\n", n_casts_normalized));
230 }
231
232
233
234 /* - Cast optimization. ------------------------------------------- */
235
236 /**
237  * Optimizes Casts:
238  *
239  *  (T1)(T2)x<T1> -> x<T1>
240  *
241  *  (T3)(T2)x<T1> -> (T3)x<T1>
242  *
243  * if possible.
244  */
245 static void cancel_out_casts(ir_node *cast) {
246         ir_node *orig, *pred = get_Cast_op(cast);
247         ir_type *tp_cast, *tp_pred, *tp_orig;
248         int ref_depth = 0;
249
250         if (!is_Cast(pred)) return;
251         orig = get_Cast_op(pred);
252
253         tp_cast = get_Cast_type(cast);
254         tp_pred = get_Cast_type(pred);
255         tp_orig = get_irn_typeinfo_type(orig);
256
257         while (is_Pointer_type(tp_cast) &&
258                 is_Pointer_type(tp_pred) &&
259                 is_Pointer_type(tp_orig)   ) {
260                         tp_cast = get_pointer_points_to_type(tp_cast);
261                         tp_pred = get_pointer_points_to_type(tp_pred);
262                         tp_orig = get_pointer_points_to_type(tp_orig);
263                         ref_depth++;
264         }
265
266         if (!is_Class_type(tp_cast)) return;
267         if (!is_Class_type(tp_pred)) return;
268         if (!is_Class_type(tp_orig)) return;
269
270         if (is_SubClass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
271                 return;
272
273         if (tp_cast == tp_orig) {
274                 exchange(cast, orig);
275                 n_casts_removed += 2;
276                 return;
277         }
278
279         if (!(is_SubClass_of  (tp_cast, tp_orig) || is_SubClass_of  (tp_orig, tp_cast))) {
280                 /* Avoid (B2)(A)(new B1()) --> (B2)(new B1())
281                 * if B1 =!> B2  and  B2 =!> B1
282                 */
283                 return;
284         }
285
286         if ((is_SubClass_of  (tp_cast, tp_pred) && is_SuperClass_of(tp_pred, tp_orig)) ||
287                 (is_SuperClass_of(tp_cast, tp_pred) && is_SubClass_of  (tp_pred, tp_orig))) {
288                         /* Cast --> Pred --> Orig */
289                         set_Cast_op(cast, orig);
290                         n_casts_removed ++;
291         }
292 }
293
294 static void concretize_selected_entity(ir_node *sel) {
295         ir_node *cast, *ptr = get_Sel_ptr(sel);
296         ir_type *orig_tp, *cast_tp;
297         ir_entity *new_ent, *sel_ent;
298
299         sel_ent = get_Sel_entity(sel);
300         cast = get_Sel_ptr(sel);
301
302         while (is_Cast(cast)) {
303                 cast_tp = get_Cast_type(cast);
304                 ptr = get_Cast_op(cast);
305                 orig_tp = get_irn_typeinfo_type(ptr);
306
307                 if (!is_Pointer_type(orig_tp)) return;
308                 if (!is_Pointer_type(cast_tp)) return;
309                 orig_tp = get_pointer_points_to_type(orig_tp);
310                 cast_tp = get_pointer_points_to_type(cast_tp);
311                 if (!is_Class_type(orig_tp)) return;
312                 if (!is_Class_type(cast_tp)) return;
313
314                 /* We only want to concretize, but not generalize. */
315                 if (!is_SuperClass_of(cast_tp, orig_tp)) return;
316
317                 /* Hmm, we are not properly typed. */
318                 if (get_class_member_index(cast_tp, sel_ent) == -1) return;
319
320                 new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
321
322                 /* New ent must be member of orig_tp. */
323                 if (get_class_member_index(orig_tp, new_ent) == -1) return;
324
325                 set_Sel_entity(sel, new_ent);
326                 set_Sel_ptr(sel, ptr);
327                 n_sels_concretized++;
328
329                 sel_ent = new_ent;
330                 cast = ptr;
331         }
332 }
333
334 static void concretize_Phi_type(ir_node *phi) {
335         int i, n_preds = get_Phi_n_preds(phi);
336         ir_node **pred = alloca(n_preds * sizeof(ir_node *));
337         ir_node *nn;
338         ir_type *totype, *fromtype;
339
340         if (n_preds == 0) return;
341         pred[0] = get_Phi_pred(phi, 0);
342
343         if (!is_Cast(pred[0])) return;
344
345         if (!is_Cast_upcast(pred[0])) return;
346
347         fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
348         totype   = get_Cast_type(pred[0]);
349
350         pred[0] = get_Cast_op(pred[0]);
351         for (i = 1; i < n_preds; ++i) {
352                 pred[i] = get_Phi_pred(phi, i);
353                 if (!is_Cast(pred[i])) return;
354                 if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype) return;
355                 pred[i] = get_Cast_op(pred[i]);
356         }
357
358         /* Transform Phi */
359         set_cur_block(get_nodes_block(phi));
360         nn = new_Phi(n_preds, pred, get_irn_mode(phi));
361         set_irn_typeinfo_type(nn, fromtype);
362         nn = new_Cast(nn, totype);
363         set_irn_typeinfo_type(nn, totype);
364         exchange(phi, nn);
365 }
366
367 void remove_Cmp_Null_cast(ir_node *cmp) {
368         ir_node *cast, *null, *new_null;
369         int cast_pos, null_pos;
370         ir_type *fromtype;
371
372         cast = get_Cmp_left(cmp);
373         cast_pos = 0;
374         if (!is_Cast(cast)) {
375                 null = cast;
376                 null_pos = cast_pos;
377                 cast = get_Cmp_right(cmp);
378                 cast_pos = 1;
379                 if (!is_Cast(cast)) return;
380         } else {
381                 null = get_Cmp_right(cmp);
382                 null_pos = 1;
383         }
384
385         if (! is_Const(null)) return;
386         if (!mode_is_reference(get_irn_mode(null))) return;
387         if (get_Const_tarval(null) != get_mode_null(get_irn_mode(null))) return;
388
389         /* Transform Cmp */
390         set_irn_n(cmp, cast_pos, get_Cast_op(cast));
391         fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
392         new_null = new_Const_type(get_Const_tarval(null), fromtype);
393         set_irn_typeinfo_type(new_null, fromtype);
394         set_irn_n(cmp, null_pos, new_null);
395         n_casts_removed ++;
396 }
397
398 /**
399  * Post-Walker:
400  */
401 static void irn_optimize_class_cast(ir_node *n, void *env) {
402         (void) env;
403         if (is_Cast(n))
404                 cancel_out_casts(n);
405         else if (is_Sel(n))
406                 concretize_selected_entity(n);
407         else if (is_Phi(n))
408                 concretize_Phi_type(n);
409         else if (is_Cmp(n))
410                 remove_Cmp_Null_cast(n);
411 }
412
413 void optimize_class_casts(void) {
414         int i, n_irgs = get_irp_n_irgs();
415
416         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
417                 simple_analyse_types();
418
419         all_irg_walk(NULL, irn_optimize_class_cast, NULL);
420
421         set_trouts_inconsistent();
422         for (i = 0; i < n_irgs; ++i)
423                 set_irg_outs_inconsistent(get_irp_irg(i));
424
425         DB((dbg, SET_LEVEL_1, " Cast optimization: %d Casts removed, %d Sels concretized.\n",
426                 n_casts_removed, n_sels_concretized));
427 }
428
429 void firm_init_class_casts_opt(void) {
430         FIRM_DBG_REGISTER(dbg, "firm.opt.tropt");
431 }