Switch irg index to type size_t, making the API more consistent.
[libfirm] / ir / opt / tropt.c
1 /*
2  * Copyright (C) 1995-2011 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 "trouts.h"
36 #include "ircons.h"
37 #include "irgmod.h"
38 #include "irflag_t.h"
39 #include "xmalloc.h"
40 #include "debug.h"
41 #include "opt_init.h"
42
43 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
44
45 /* - statistics ---------------------------------------------- */
46
47 static size_t n_casts_normalized = 0;
48 static size_t n_casts_removed    = 0;
49 static size_t n_sels_concretized = 0;
50
51 /* - Cast normalization. ------------------------------------- */
52
53 static ir_type *default_gen_pointer_type_to(ir_type *tp);
54
55 #define PTR_TYPE_SUFFIX "cc_ptr_tp"  /* class cast pointer type. */
56 static ident *ptr_type_suffix = NULL;
57 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
58
59 /**
60  * Find a pointer type to a given type.
61  * Uses and updates trouts if available.
62  */
63 static ir_type *default_gen_pointer_type_to(ir_type *tp)
64 {
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, size_t depth)
85 {
86         for (; depth > 0; --depth) {
87                 tp = gen_pointer_type_to(tp);
88         }
89         return tp;
90 }
91
92
93 static ir_node *normalize_values_type(ir_type *totype, ir_node *pred)
94 {
95         ir_type *fromtype = get_irn_typeinfo_type(pred);
96         ir_node *new_cast = pred;
97         ir_node *block;
98         size_t  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) != (size_t)-1) ||
112                 (get_class_supertype_index(fromtype, totype) != (size_t)-1) ) {
113                         /* It's just what we want ... */
114                         return pred;
115         }
116
117         block = get_nodes_block(pred);
118
119         if (is_SubClass_of(totype, fromtype)) {
120                 /* downcast */
121                 while (get_class_subtype_index(fromtype, totype) == (size_t)-1) {
122                         /* Insert a cast to a subtype of fromtype. */
123                         ir_type *new_type = NULL;
124                         ir_node *new_cast;
125                         size_t   i;
126                         size_t   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))
130                                         new_type = new_sub;
131                         }
132                         assert(new_type);
133                         fromtype = new_type;
134                         new_type = pointerize_type(new_type, ref_depth);
135                         new_cast = new_r_Cast(block, pred, new_type);
136                         pred = new_cast;
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);
140                 }
141         } else {
142                 assert(is_SuperClass_of(totype, fromtype));
143                 /* upcast */
144                 while (get_class_supertype_index(fromtype, totype) == (size_t)-1) {
145                         /* Insert a cast to a supertype of fromtype. */
146                         ir_type *new_type = NULL;
147                         size_t 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;
152                         }
153                         assert(new_type);
154                         fromtype = new_type;
155                         new_type = pointerize_type(new_type, ref_depth);
156                         new_cast = new_r_Cast(block, pred, new_type);
157                         pred = new_cast;
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);
161                 }
162         }
163         return new_cast;
164 }
165
166 /**
167  * Post-Walker.
168  */
169 static void normalize_irn_class_cast(ir_node *n, void *env)
170 {
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                 size_t n_params = get_Call_n_params(n);
180                 size_t i;
181                 ir_type *tp = get_Call_type(n);
182                 for (i = 0; i < n_params; ++i) {
183                         res = normalize_values_type(get_method_param_type(tp, i), get_Call_param(n, i));
184                         set_Call_param(n, i, res);
185                 }
186         }
187 }
188
189
190 static void pure_normalize_irg_class_casts(ir_graph *irg)
191 {
192         assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
193                 "Cannot normalize irregular casts.");
194         if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
195                 verify_irg_class_cast_state(irg);
196                 return;
197         }
198
199         irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
200         set_irg_class_cast_state(irg, ir_class_casts_normalized);
201 }
202
203
204 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct)
205 {
206         assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
207
208         if (gppt_fct) gen_pointer_type_to = gppt_fct;
209
210         if (!ptr_type_suffix)
211                 ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
212
213         pure_normalize_irg_class_casts(irg);
214
215         gen_pointer_type_to = default_gen_pointer_type_to;
216 }
217
218 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct)
219 {
220         size_t i, n;
221         if (gppt_fct) gen_pointer_type_to = gppt_fct;
222
223 #if 0
224         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
225                 simple_analyse_types();
226 #endif
227
228         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
229                 ir_graph *irg = get_irp_irg(i);
230                 pure_normalize_irg_class_casts(irg);
231         }
232
233         set_irp_class_cast_state(ir_class_casts_normalized);
234         gen_pointer_type_to = default_gen_pointer_type_to;
235
236         DB((dbg, SET_LEVEL_1, " Cast normalization: %zu Casts inserted.\n", n_casts_normalized));
237 }
238
239
240
241 /* - Cast optimization. ------------------------------------------- */
242
243 /**
244  * Optimizes Casts:
245  *
246  *  (T1)(T2)x<T1> -> x<T1>
247  *
248  *  (T3)(T2)x<T1> -> (T3)x<T1>
249  *
250  * if possible.
251  *
252  * @param cast  the Cast node
253  *
254  * @return 1 if the cast was changed
255  */
256 static int cancel_out_casts(ir_node *cast)
257 {
258         ir_node *orig, *pred = get_Cast_op(cast);
259         ir_type *tp_cast, *tp_pred, *tp_orig;
260
261         if (!is_Cast(pred)) return 0;
262         orig = get_Cast_op(pred);
263
264         tp_cast = get_Cast_type(cast);
265         tp_pred = get_Cast_type(pred);
266         tp_orig = get_irn_typeinfo_type(orig);
267
268         while (is_Pointer_type(tp_cast) &&
269               is_Pointer_type(tp_pred) &&
270               is_Pointer_type(tp_orig)   ) {
271                 tp_cast = get_pointer_points_to_type(tp_cast);
272                 tp_pred = get_pointer_points_to_type(tp_pred);
273                 tp_orig = get_pointer_points_to_type(tp_orig);
274         }
275
276         if (!is_Class_type(tp_cast) || !is_Class_type(tp_pred) || !is_Class_type(tp_orig))
277                 return 0;
278
279         if (is_SubClass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
280                 return 0;
281
282         if (tp_cast == tp_orig) {
283                 exchange(cast, orig);
284                 n_casts_removed += 2;
285                 return 1;
286         }
287
288         if (!(is_SubClass_of  (tp_cast, tp_orig) || is_SubClass_of  (tp_orig, tp_cast))) {
289                 /* Avoid (B2)(A)(new B1()) --> (B2)(new B1())
290                 * if B1 =!> B2  and  B2 =!> B1
291                 */
292                 return 0;
293         }
294
295         if ((is_SubClass_of  (tp_cast, tp_pred) && is_SuperClass_of(tp_pred, tp_orig)) ||
296             (is_SuperClass_of(tp_cast, tp_pred) && is_SubClass_of  (tp_pred, tp_orig))) {
297                 /* Cast --> Pred --> Orig */
298                 set_Cast_op(cast, orig);
299                 ++n_casts_removed;
300                 return 1;
301         }
302         return 0;
303 }
304
305 /**
306  * Optimize Sel(Cast(type, ptr), ent) into Sel(ptr_type, ent_type)
307  *
308  * @param sel  the Sel node
309  *
310  * @return 1 if Cast's where removed
311  */
312 static int concretize_selected_entity(ir_node *sel)
313 {
314         ir_node   *cast, *ptr = get_Sel_ptr(sel);
315         ir_type   *orig_tp, *cast_tp;
316         ir_entity *new_ent, *sel_ent;
317         int       res = 0;
318
319         sel_ent = get_Sel_entity(sel);
320         cast    = get_Sel_ptr(sel);
321
322         while (is_Cast(cast)) {
323                 cast_tp = get_Cast_type(cast);
324                 ptr     = get_Cast_op(cast);
325                 orig_tp = get_irn_typeinfo_type(ptr);
326
327                 /* we handle only classes */
328                 if (!is_Pointer_type(orig_tp)|| !is_Pointer_type(cast_tp))
329                         return res;
330                 orig_tp = get_pointer_points_to_type(orig_tp);
331                 cast_tp = get_pointer_points_to_type(cast_tp);
332                 if (!is_Class_type(orig_tp) || !is_Class_type(cast_tp))
333                         return res;
334
335                 /* We only want to concretize, but not generalize. */
336                 if (!is_SuperClass_of(cast_tp, orig_tp))
337                         return res;
338
339                 /* The sel entity should be a member of the cast_tp, else
340                    the graph was not properly typed. */
341                 if (get_class_member_index(cast_tp, sel_ent) == (size_t)-1)
342                         return res;
343
344                 new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
345
346                 /* New ent must be member of orig_tp. */
347                 if (get_class_member_index(orig_tp, new_ent) == (size_t)-1)
348                         return res;
349
350                 /* all checks done, we can remove the Cast and update the Sel */
351                 set_Sel_entity(sel, new_ent);
352                 set_Sel_ptr(sel, ptr);
353                 ++n_sels_concretized;
354
355                 sel_ent = new_ent;
356                 cast    = ptr;
357                 res     = 1;
358         }
359         return res;
360 }
361
362 /**
363  * Move Casts of the same type through a Phi node, i.e.
364  * Phi(Cast(type, x_0), ..., Cast(type, x_n)) -> Cast(type, Phi(x_0, ..., x_n))
365  *
366  * @param phi  the Phi node
367  *
368  * @return 1 if Cast's where moved
369  */
370 static int concretize_Phi_type(ir_node *phi)
371 {
372         int       n_preds = get_Phi_n_preds(phi);
373         ir_node **pred    = ALLOCAN(ir_node*, n_preds);
374         ir_node  *nn, *blk;
375         ir_type  *totype;
376         ir_type  *fromtype;
377         int       i;
378
379         if (n_preds == 0)
380                 return 0;
381         pred[0] = get_Phi_pred(phi, 0);
382
383         if (!is_Cast(pred[0]))
384                 return 0;
385
386         if (!is_Cast_upcast(pred[0]))
387                 return 0;
388
389         fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
390         totype   = get_Cast_type(pred[0]);
391
392         pred[0] = get_Cast_op(pred[0]);
393         for (i = 1; i < n_preds; ++i) {
394                 pred[i] = get_Phi_pred(phi, i);
395                 if (!is_Cast(pred[i]))
396                         return 0;
397                 if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype)
398                         return 0;
399                 pred[i] = get_Cast_op(pred[i]);
400         }
401
402         /* Transform Phi */
403         blk = get_nodes_block(phi);
404         nn  = new_r_Phi(blk, n_preds, pred, get_irn_mode(phi));
405         set_irn_typeinfo_type(nn, fromtype);
406         nn  = new_r_Cast(blk, nn, totype);
407         set_irn_typeinfo_type(nn, totype);
408         exchange(phi, nn);
409         return 1;
410 }
411
412 /**
413  * Remove Casted null checks, i.e.
414  *
415  * Cmp(Cast(type, x_orig), NULL_type) -> Cmp(x_orig, NULL_orig)
416  *
417  * @param cmp  the Cmp node
418  *
419  * @return 1 if Cast's where removed
420  */
421 static int remove_Cmp_Null_cast(ir_node *cmp)
422 {
423         ir_graph *irg;
424         ir_node *cast, *null, *new_null;
425         int     cast_pos, null_pos;
426         ir_type *fromtype;
427         ir_mode *mode;
428
429         cast = get_Cmp_left(cmp);
430         if (!is_Cast(cast)) {
431                 null     = cast;
432                 null_pos = 0;
433                 cast     = get_Cmp_right(cmp);
434                 cast_pos = 1;
435                 if (!is_Cast(cast))
436                         return 0;
437         } else {
438                 null = get_Cmp_right(cmp);
439                 cast_pos = 0;
440                 null_pos = 1;
441         }
442
443         if (! is_Const(null))
444                 return 0;
445         mode = get_irn_mode(null);
446         if (!mode_is_reference(mode))
447                 return 0;
448         if (get_Const_tarval(null) != get_mode_null(mode))
449                 return 0;
450
451         /* Transform Cmp */
452         irg = get_irn_irg(cmp);
453         set_irn_n(cmp, cast_pos, get_Cast_op(cast));
454         fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
455         new_null = new_r_Const(irg, get_Const_tarval(null));
456         set_irn_typeinfo_type(new_null, fromtype);
457         set_irn_n(cmp, null_pos, new_null);
458         ++n_casts_removed;
459         return 1;
460 }
461
462 /**
463  * Post-Walker: Optimize class casts (mostly by trying to remove them)
464  */
465 static void irn_optimize_class_cast(ir_node *n, void *env)
466 {
467         int *changed = (int*)env;
468
469         if (is_Cast(n))
470                 *changed |= cancel_out_casts(n);
471         else if (is_Sel(n))
472                 *changed |= concretize_selected_entity(n);
473         else if (is_Phi(n))
474                 *changed |= concretize_Phi_type(n);
475         else if (is_Cmp(n))
476                 *changed |= remove_Cmp_Null_cast(n);
477 }
478
479 void optimize_class_casts(void)
480 {
481         int changed;
482
483 #if 0
484         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
485                 simple_analyse_types();
486 #endif
487
488         changed = 0;
489         all_irg_walk(NULL, irn_optimize_class_cast, &changed);
490
491         if (changed) {
492                 size_t i, n;
493
494                 set_trouts_inconsistent();
495                 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
496                         set_irg_outs_inconsistent(get_irp_irg(i));
497         }
498
499         DB((dbg, SET_LEVEL_1, " Cast optimization: %zu Casts removed, %zu Sels concretized.\n",
500                 n_casts_removed, n_sels_concretized));
501 }
502
503 void firm_init_class_casts_opt(void)
504 {
505         FIRM_DBG_REGISTER(dbg, "firm.opt.tropt");
506 }