Ignore generated files.
[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 "opt_init.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                         res = new_type_pointer(tp);
71                         /* Update trout for pointer types, so we can use it in next call. */
72                         add_type_pointertype_to(tp, res);
73                 }
74         } else {
75                 res = find_pointer_type_to_type(tp);
76                 if (res == firm_unknown_type)
77                         res = new_type_pointer(tp);
78         }
79
80         return res;
81 }
82
83 /** Return a type that is a depth times pointer to type. */
84 static ir_type *pointerize_type(ir_type *tp, int depth) {
85         for (; depth > 0; --depth) {
86                 tp = gen_pointer_type_to(tp);
87         }
88         return tp;
89 }
90
91
92 static ir_node *normalize_values_type(ir_type *totype, ir_node *pred) {
93         ir_type *fromtype = get_irn_typeinfo_type(pred);
94         ir_node *new_cast = pred;
95         int ref_depth = 0;
96
97         if (totype == fromtype) return pred;   /* Case for optimization! */
98
99         while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
100                 totype   = get_pointer_points_to_type(totype);
101                 fromtype = get_pointer_points_to_type(fromtype);
102                 ref_depth++;
103         }
104
105         if (!is_Class_type(totype))   return pred;
106         if (!is_Class_type(fromtype)) return pred;
107
108         if ((get_class_supertype_index(totype, fromtype) != -1) ||
109                 (get_class_supertype_index(fromtype, totype) != -1) ) {
110                         /* It's just what we want ... */
111                         return pred;
112         }
113
114         set_cur_block(get_nodes_block(pred));
115
116         if (is_SubClass_of(totype, fromtype)) {
117                 /* downcast */
118                 while (get_class_subtype_index(fromtype, totype) == -1) {
119                         /* Insert a cast to a subtype of fromtype. */
120                         ir_type *new_type = NULL;
121                         ir_node *new_cast;
122                         int i, n_subtypes = get_class_n_subtypes(fromtype);
123                         for (i = 0; i < n_subtypes && !new_type; ++i) {
124                                 ir_type *new_sub = get_class_subtype(fromtype, i);
125                                 if (is_SuperClass_of(new_sub, totype))
126                                         new_type = new_sub;
127                         }
128                         assert(new_type);
129                         fromtype = new_type;
130                         new_type = pointerize_type(new_type, ref_depth);
131                         new_cast = new_Cast(pred, new_type);
132                         pred = new_cast;
133                         n_casts_normalized ++;
134                         set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
135                         if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
136                 }
137         } else {
138                 assert(is_SuperClass_of(totype, fromtype));
139                 /* upcast */
140                 while (get_class_supertype_index(fromtype, totype) == -1) {
141                         /* Insert a cast to a supertype of fromtype. */
142                         ir_type *new_type = NULL;
143                         int i, n_supertypes = get_class_n_supertypes(fromtype);
144                         for (i = 0; i < n_supertypes && !new_type; ++i) {
145                                 ir_type *new_super = get_class_supertype(fromtype, i);
146                                 if (is_SubClass_of(new_super, totype))
147                                         new_type = new_super;
148                         }
149                         assert(new_type);
150                         fromtype = new_type;
151                         new_type = pointerize_type(new_type, ref_depth);
152                         new_cast = new_Cast(pred, new_type);
153                         pred = new_cast;
154                         n_casts_normalized ++;
155                         set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
156                         if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
157                 }
158         }
159         return new_cast;
160 }
161
162 /**
163  * Post-Walker.
164  */
165 static void normalize_irn_class_cast(ir_node *n, void *env) {
166         ir_node *res;
167         (void) env;
168         if (is_Cast(n)) {
169                 ir_node *pred   = get_Cast_op(n);
170                 ir_type *totype = get_Cast_type(n);
171                 res = normalize_values_type(totype, pred);
172                 set_Cast_op(n, res);
173         } else if (is_Call(n)) {
174                 int i, n_params = get_Call_n_params(n);
175                 ir_type *tp = get_Call_type(n);
176                 for (i = 0; i < n_params; ++i) {
177                         res = normalize_values_type(get_method_param_type(tp, i), get_Call_param(n, i));
178                         set_Call_param(n, i, res);
179                 }
180         }
181 }
182
183
184 static void pure_normalize_irg_class_casts(ir_graph *irg) {
185         assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
186                 "Cannot normalize irregular casts.");
187         if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
188                 verify_irg_class_cast_state(irg);
189                 return;
190         }
191
192         irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
193         set_irg_class_cast_state(irg, ir_class_casts_normalized);
194 }
195
196
197 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct) {
198         assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
199
200         if (gppt_fct) gen_pointer_type_to = gppt_fct;
201
202         if (!ptr_type_suffix)
203                 ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
204
205         pure_normalize_irg_class_casts(irg);
206
207         gen_pointer_type_to = default_gen_pointer_type_to;
208 }
209
210 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct) {
211         int i;
212         if (gppt_fct) gen_pointer_type_to = gppt_fct;
213
214         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
215                 simple_analyse_types();
216
217         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
218                 pure_normalize_irg_class_casts(get_irp_irg(i));
219         }
220
221         set_irp_class_cast_state(ir_class_casts_normalized);
222         gen_pointer_type_to = default_gen_pointer_type_to;
223
224         DB((dbg, SET_LEVEL_1, " Cast normalization: %d Casts inserted.\n", n_casts_normalized));
225 }
226
227
228
229 /* - Cast optimization. ------------------------------------------- */
230
231 /**
232  * Optimizes Casts:
233  *
234  *  (T1)(T2)x<T1> -> x<T1>
235  *
236  *  (T3)(T2)x<T1> -> (T3)x<T1>
237  *
238  * if possible.
239  *
240  * @param cast  the Cast node
241  *
242  * @return 1 if the cast was changed
243  */
244 static int cancel_out_casts(ir_node *cast) {
245         ir_node *orig, *pred = get_Cast_op(cast);
246         ir_type *tp_cast, *tp_pred, *tp_orig;
247         int ref_depth = 0;
248
249         if (!is_Cast(pred)) return 0;
250         orig = get_Cast_op(pred);
251
252         tp_cast = get_Cast_type(cast);
253         tp_pred = get_Cast_type(pred);
254         tp_orig = get_irn_typeinfo_type(orig);
255
256         while (is_Pointer_type(tp_cast) &&
257               is_Pointer_type(tp_pred) &&
258               is_Pointer_type(tp_orig)   ) {
259                 tp_cast = get_pointer_points_to_type(tp_cast);
260                 tp_pred = get_pointer_points_to_type(tp_pred);
261                 tp_orig = get_pointer_points_to_type(tp_orig);
262                 ref_depth++;
263         }
264
265         if (!is_Class_type(tp_cast) || !is_Class_type(tp_pred) || !is_Class_type(tp_orig))
266                 return 0;
267
268         if (is_SubClass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
269                 return 0;
270
271         if (tp_cast == tp_orig) {
272                 exchange(cast, orig);
273                 n_casts_removed += 2;
274                 return 1;
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 0;
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                 return 1;
290         }
291         return 0;
292 }
293
294 /**
295  * Optimize Sel(Cast(type, ptr), ent) into Sel(ptr_type, ent_type)
296  *
297  * @param sel  the Sel node
298  *
299  * @return 1 if Cast's where removed
300  */
301 static int 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;
305         int       res = 0;
306
307         sel_ent = get_Sel_entity(sel);
308         cast    = get_Sel_ptr(sel);
309
310         while (is_Cast(cast)) {
311                 cast_tp = get_Cast_type(cast);
312                 ptr     = get_Cast_op(cast);
313                 orig_tp = get_irn_typeinfo_type(ptr);
314
315                 /* we handle only classes */
316                 if (!is_Pointer_type(orig_tp)|| !is_Pointer_type(cast_tp))
317                         return res;
318                 orig_tp = get_pointer_points_to_type(orig_tp);
319                 cast_tp = get_pointer_points_to_type(cast_tp);
320                 if (!is_Class_type(orig_tp) || !is_Class_type(cast_tp))
321                         return res;
322
323                 /* We only want to concretize, but not generalize. */
324                 if (!is_SuperClass_of(cast_tp, orig_tp))
325                         return res;
326
327                 /* The sel entity should be a member of the cast_tp, else
328                    the graph was not properly typed. */
329                 if (get_class_member_index(cast_tp, sel_ent) == -1)
330                         return res;
331
332                 new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
333
334                 /* New ent must be member of orig_tp. */
335                 if (get_class_member_index(orig_tp, new_ent) == -1)
336                         return res;
337
338                 /* all checks done, we can remove the Cast and update the Sel */
339                 set_Sel_entity(sel, new_ent);
340                 set_Sel_ptr(sel, ptr);
341                 ++n_sels_concretized;
342
343                 sel_ent = new_ent;
344                 cast    = ptr;
345                 res     = 1;
346         }
347         return res;
348 }
349
350 /**
351  * Move Casts of the same type through a Phi node, i.e.
352  * Phi(Cast(type, x_0), ..., Cast(type, x_n)) -> Cast(type, Phi(x_0, ..., x_n))
353  *
354  * @param phi  the Phi node
355  *
356  * @return 1 if Cast's where moved
357  */
358 static int concretize_Phi_type(ir_node *phi)
359 {
360         int       n_preds = get_Phi_n_preds(phi);
361         ir_node **pred    = ALLOCAN(ir_node*, n_preds);
362         ir_node  *nn, *blk;
363         ir_type  *totype;
364         ir_type  *fromtype;
365         int       i;
366
367         if (n_preds == 0)
368                 return 0;
369         pred[0] = get_Phi_pred(phi, 0);
370
371         if (!is_Cast(pred[0]))
372                 return 0;
373
374         if (!is_Cast_upcast(pred[0]))
375                 return 0;
376
377         fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
378         totype   = get_Cast_type(pred[0]);
379
380         pred[0] = get_Cast_op(pred[0]);
381         for (i = 1; i < n_preds; ++i) {
382                 pred[i] = get_Phi_pred(phi, i);
383                 if (!is_Cast(pred[i]))
384                         return 0;
385                 if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype)
386                         return 0;
387                 pred[i] = get_Cast_op(pred[i]);
388         }
389
390         /* Transform Phi */
391         blk = get_nodes_block(phi);
392         nn  = new_r_Phi(blk, n_preds, pred, get_irn_mode(phi));
393         set_irn_typeinfo_type(nn, fromtype);
394         nn  = new_r_Cast(blk, nn, totype);
395         set_irn_typeinfo_type(nn, totype);
396         exchange(phi, nn);
397         return 1;
398 }
399
400 /**
401  * Remove Casted null checks, i.e.
402  *
403  * Cmp(Cast(type, x_orig), NULL_type) -> Cmp(x_orig, NULL_orig)
404  *
405  * @param cmp  the Cmp node
406  *
407  * @return 1 if Cast's where removed
408  */
409 static int remove_Cmp_Null_cast(ir_node *cmp) {
410         ir_node *cast, *null, *new_null;
411         int     cast_pos, null_pos;
412         ir_type *fromtype;
413         ir_mode *mode;
414
415         cast = get_Cmp_left(cmp);
416         if (!is_Cast(cast)) {
417                 null     = cast;
418                 null_pos = 0;
419                 cast     = get_Cmp_right(cmp);
420                 cast_pos = 1;
421                 if (!is_Cast(cast))
422                         return 0;
423         } else {
424                 null = get_Cmp_right(cmp);
425                 cast_pos = 0;
426                 null_pos = 1;
427         }
428
429         if (! is_Const(null))
430                 return 0;
431         mode = get_irn_mode(null);
432         if (!mode_is_reference(mode))
433                 return 0;
434         if (get_Const_tarval(null) != get_mode_null(mode))
435                 return 0;
436
437         /* Transform Cmp */
438         set_irn_n(cmp, cast_pos, get_Cast_op(cast));
439         fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
440         new_null = new_Const_type(get_Const_tarval(null), fromtype);
441         set_irn_typeinfo_type(new_null, fromtype);
442         set_irn_n(cmp, null_pos, new_null);
443         ++n_casts_removed;
444         return 1;
445 }
446
447 /**
448  * Post-Walker: Optimize class casts (mostly by trying to remove them)
449  */
450 static void irn_optimize_class_cast(ir_node *n, void *env) {
451         int *changed = env;
452
453         if (is_Cast(n))
454                 *changed |= cancel_out_casts(n);
455         else if (is_Sel(n))
456                 *changed |= concretize_selected_entity(n);
457         else if (is_Phi(n))
458                 *changed |= concretize_Phi_type(n);
459         else if (is_Cmp(n))
460                 *changed |= remove_Cmp_Null_cast(n);
461 }
462
463 void optimize_class_casts(void) {
464         int changed;
465
466         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
467                 simple_analyse_types();
468
469         changed = 0;
470         all_irg_walk(NULL, irn_optimize_class_cast, &changed);
471
472         if (changed) {
473                 int i;
474
475                 set_trouts_inconsistent();
476                 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
477                         set_irg_outs_inconsistent(get_irp_irg(i));
478         }
479
480         DB((dbg, SET_LEVEL_1, " Cast optimization: %d Casts removed, %d Sels concretized.\n",
481                 n_casts_removed, n_sels_concretized));
482 }
483
484 void firm_init_class_casts_opt(void) {
485         FIRM_DBG_REGISTER(dbg, "firm.opt.tropt");
486 }