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