clarify some node comments
[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  */
26 #include "config.h"
27
28 #include <assert.h>
29
30 #include "iroptimize.h"
31 #include "irprog.h"
32 #include "irtypeinfo.h"
33 #include "irgwalk.h"
34 #include "trouts.h"
35 #include "ircons.h"
36 #include "irgmod.h"
37 #include "irflag_t.h"
38 #include "xmalloc.h"
39 #include "debug.h"
40 #include "opt_init.h"
41
42 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
43
44 /* - statistics ---------------------------------------------- */
45
46 static size_t n_casts_normalized = 0;
47 static size_t n_casts_removed    = 0;
48 static size_t n_sels_concretized = 0;
49
50 /* - Cast normalization. ------------------------------------- */
51
52 static ir_type *default_gen_pointer_type_to(ir_type *tp);
53
54 #define PTR_TYPE_SUFFIX "cc_ptr_tp"  /* class cast pointer type. */
55 static ident *ptr_type_suffix = NULL;
56 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
57
58 /**
59  * Find a pointer type to a given type.
60  * Uses and updates trouts if available.
61  */
62 static ir_type *default_gen_pointer_type_to(ir_type *tp)
63 {
64         ir_type *res = find_pointer_type_to_type(tp);
65         if (res == firm_unknown_type)
66                 res = new_type_pointer(tp);
67
68         return res;
69 }
70
71 /** Return a type that is a depth times pointer to type. */
72 static ir_type *pointerize_type(ir_type *tp, size_t depth)
73 {
74         for (; depth > 0; --depth) {
75                 tp = gen_pointer_type_to(tp);
76         }
77         return tp;
78 }
79
80
81 static ir_node *normalize_values_type(ir_type *totype, ir_node *pred)
82 {
83         ir_type *fromtype = get_irn_typeinfo_type(pred);
84         ir_node *new_cast = pred;
85         ir_node *block;
86         size_t  ref_depth = 0;
87
88         if (totype == fromtype) return pred;   /* Case for optimization! */
89
90         while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
91                 totype   = get_pointer_points_to_type(totype);
92                 fromtype = get_pointer_points_to_type(fromtype);
93                 ++ref_depth;
94         }
95
96         if (!is_Class_type(totype))   return pred;
97         if (!is_Class_type(fromtype)) return pred;
98
99         if ((get_class_supertype_index(totype, fromtype) != (size_t)-1) ||
100                 (get_class_supertype_index(fromtype, totype) != (size_t)-1) ) {
101                         /* It's just what we want ... */
102                         return pred;
103         }
104
105         block = get_nodes_block(pred);
106
107         if (is_SubClass_of(totype, fromtype)) {
108                 /* downcast */
109                 while (get_class_subtype_index(fromtype, totype) == (size_t)-1) {
110                         /* Insert a cast to a subtype of fromtype. */
111                         ir_type *new_type = NULL;
112                         ir_node *new_cast;
113                         size_t   i;
114                         size_t   n_subtypes = get_class_n_subtypes(fromtype);
115                         for (i = 0; i < n_subtypes && !new_type; ++i) {
116                                 ir_type *new_sub = get_class_subtype(fromtype, i);
117                                 if (is_SuperClass_of(new_sub, totype))
118                                         new_type = new_sub;
119                         }
120                         assert(new_type);
121                         fromtype = new_type;
122                         new_type = pointerize_type(new_type, ref_depth);
123                         new_cast = new_r_Cast(block, pred, new_type);
124                         pred = new_cast;
125                         ++n_casts_normalized;
126                         set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
127                 }
128         } else {
129                 assert(is_SuperClass_of(totype, fromtype));
130                 /* upcast */
131                 while (get_class_supertype_index(fromtype, totype) == (size_t)-1) {
132                         /* Insert a cast to a supertype of fromtype. */
133                         ir_type *new_type = NULL;
134                         size_t i, n_supertypes = get_class_n_supertypes(fromtype);
135                         for (i = 0; i < n_supertypes && !new_type; ++i) {
136                                 ir_type *new_super = get_class_supertype(fromtype, i);
137                                 if (is_SubClass_of(new_super, totype))
138                                         new_type = new_super;
139                         }
140                         assert(new_type);
141                         fromtype = new_type;
142                         new_type = pointerize_type(new_type, ref_depth);
143                         new_cast = new_r_Cast(block, pred, new_type);
144                         pred = new_cast;
145                         ++n_casts_normalized;
146                         set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
147                 }
148         }
149         return new_cast;
150 }
151
152 /**
153  * Post-Walker.
154  */
155 static void normalize_irn_class_cast(ir_node *n, void *env)
156 {
157         ir_node *res;
158         (void) env;
159         if (is_Cast(n)) {
160                 ir_node *pred   = get_Cast_op(n);
161                 ir_type *totype = get_Cast_type(n);
162                 res = normalize_values_type(totype, pred);
163                 set_Cast_op(n, res);
164         } else if (is_Call(n)) {
165                 size_t n_params = get_Call_n_params(n);
166                 size_t i;
167                 ir_type *tp = get_Call_type(n);
168                 for (i = 0; i < n_params; ++i) {
169                         res = normalize_values_type(get_method_param_type(tp, i), get_Call_param(n, i));
170                         set_Call_param(n, i, res);
171                 }
172         }
173 }
174
175
176 static void pure_normalize_irg_class_casts(ir_graph *irg)
177 {
178         assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
179                 "Cannot normalize irregular casts.");
180         if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
181                 verify_irg_class_cast_state(irg);
182                 return;
183         }
184
185         irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
186         set_irg_class_cast_state(irg, ir_class_casts_normalized);
187 }
188
189
190 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct)
191 {
192         assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
193
194         if (gppt_fct) gen_pointer_type_to = gppt_fct;
195
196         if (!ptr_type_suffix)
197                 ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
198
199         pure_normalize_irg_class_casts(irg);
200
201         gen_pointer_type_to = default_gen_pointer_type_to;
202 }
203
204 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct)
205 {
206         size_t i, n;
207         if (gppt_fct) gen_pointer_type_to = gppt_fct;
208
209 #if 0
210         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
211                 simple_analyse_types();
212 #endif
213
214         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
215                 ir_graph *irg = get_irp_irg(i);
216                 pure_normalize_irg_class_casts(irg);
217         }
218
219         set_irp_class_cast_state(ir_class_casts_normalized);
220         gen_pointer_type_to = default_gen_pointer_type_to;
221
222         DB((dbg, SET_LEVEL_1, " Cast normalization: %zu Casts inserted.\n", n_casts_normalized));
223 }
224
225
226
227 /* - Cast optimization. ------------------------------------------- */
228
229 /**
230  * Optimizes Casts:
231  *
232  *  (T1)(T2)x<T1> -> x<T1>
233  *
234  *  (T3)(T2)x<T1> -> (T3)x<T1>
235  *
236  * if possible.
237  *
238  * @param cast  the Cast node
239  *
240  * @return 1 if the cast was changed
241  */
242 static int cancel_out_casts(ir_node *cast)
243 {
244         ir_node *orig, *pred = get_Cast_op(cast);
245         ir_type *tp_cast, *tp_pred, *tp_orig;
246
247         if (!is_Cast(pred)) return 0;
248         orig = get_Cast_op(pred);
249
250         tp_cast = get_Cast_type(cast);
251         tp_pred = get_Cast_type(pred);
252         tp_orig = get_irn_typeinfo_type(orig);
253
254         while (is_Pointer_type(tp_cast) &&
255               is_Pointer_type(tp_pred) &&
256               is_Pointer_type(tp_orig)   ) {
257                 tp_cast = get_pointer_points_to_type(tp_cast);
258                 tp_pred = get_pointer_points_to_type(tp_pred);
259                 tp_orig = get_pointer_points_to_type(tp_orig);
260         }
261
262         if (!is_Class_type(tp_cast) || !is_Class_type(tp_pred) || !is_Class_type(tp_orig))
263                 return 0;
264
265         if (is_SubClass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
266                 return 0;
267
268         if (tp_cast == tp_orig) {
269                 exchange(cast, orig);
270                 n_casts_removed += 2;
271                 return 1;
272         }
273
274         if (!(is_SubClass_of  (tp_cast, tp_orig) || is_SubClass_of  (tp_orig, tp_cast))) {
275                 /* Avoid (B2)(A)(new B1()) --> (B2)(new B1())
276                 * if B1 =!> B2  and  B2 =!> B1
277                 */
278                 return 0;
279         }
280
281         if ((is_SubClass_of  (tp_cast, tp_pred) && is_SuperClass_of(tp_pred, tp_orig)) ||
282             (is_SuperClass_of(tp_cast, tp_pred) && is_SubClass_of  (tp_pred, tp_orig))) {
283                 /* Cast --> Pred --> Orig */
284                 set_Cast_op(cast, orig);
285                 ++n_casts_removed;
286                 return 1;
287         }
288         return 0;
289 }
290
291 /**
292  * Optimize Sel(Cast(type, ptr), ent) into Sel(ptr_type, ent_type)
293  *
294  * @param sel  the Sel node
295  *
296  * @return 1 if Cast's where removed
297  */
298 static int concretize_selected_entity(ir_node *sel)
299 {
300         ir_node   *cast, *ptr = get_Sel_ptr(sel);
301         ir_type   *orig_tp, *cast_tp;
302         ir_entity *new_ent, *sel_ent;
303         int       res = 0;
304
305         sel_ent = get_Sel_entity(sel);
306         cast    = get_Sel_ptr(sel);
307
308         while (is_Cast(cast)) {
309                 cast_tp = get_Cast_type(cast);
310                 ptr     = get_Cast_op(cast);
311                 orig_tp = get_irn_typeinfo_type(ptr);
312
313                 /* we handle only classes */
314                 if (!is_Pointer_type(orig_tp)|| !is_Pointer_type(cast_tp))
315                         return res;
316                 orig_tp = get_pointer_points_to_type(orig_tp);
317                 cast_tp = get_pointer_points_to_type(cast_tp);
318                 if (!is_Class_type(orig_tp) || !is_Class_type(cast_tp))
319                         return res;
320
321                 /* We only want to concretize, but not generalize. */
322                 if (!is_SuperClass_of(cast_tp, orig_tp))
323                         return res;
324
325                 /* The sel entity should be a member of the cast_tp, else
326                    the graph was not properly typed. */
327                 if (get_class_member_index(cast_tp, sel_ent) == (size_t)-1)
328                         return res;
329
330                 new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
331
332                 /* New ent must be member of orig_tp. */
333                 if (get_class_member_index(orig_tp, new_ent) == (size_t)-1)
334                         return res;
335
336                 /* all checks done, we can remove the Cast and update the Sel */
337                 set_Sel_entity(sel, new_ent);
338                 set_Sel_ptr(sel, ptr);
339                 ++n_sels_concretized;
340
341                 sel_ent = new_ent;
342                 cast    = ptr;
343                 res     = 1;
344         }
345         return res;
346 }
347
348 /**
349  * Move Casts of the same type through a Phi node, i.e.
350  * Phi(Cast(type, x_0), ..., Cast(type, x_n)) -> Cast(type, Phi(x_0, ..., x_n))
351  *
352  * @param phi  the Phi node
353  *
354  * @return 1 if Cast's where moved
355  */
356 static int concretize_Phi_type(ir_node *phi)
357 {
358         int       n_preds = get_Phi_n_preds(phi);
359         ir_node **pred    = ALLOCAN(ir_node*, n_preds);
360         ir_node  *nn, *blk;
361         ir_type  *totype;
362         ir_type  *fromtype;
363         int       i;
364
365         if (n_preds == 0)
366                 return 0;
367         pred[0] = get_Phi_pred(phi, 0);
368
369         if (!is_Cast(pred[0]))
370                 return 0;
371
372         if (!is_Cast_upcast(pred[0]))
373                 return 0;
374
375         fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
376         totype   = get_Cast_type(pred[0]);
377
378         pred[0] = get_Cast_op(pred[0]);
379         for (i = 1; i < n_preds; ++i) {
380                 pred[i] = get_Phi_pred(phi, i);
381                 if (!is_Cast(pred[i]))
382                         return 0;
383                 if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype)
384                         return 0;
385                 pred[i] = get_Cast_op(pred[i]);
386         }
387
388         /* Transform Phi */
389         blk = get_nodes_block(phi);
390         nn  = new_r_Phi(blk, n_preds, pred, get_irn_mode(phi));
391         set_irn_typeinfo_type(nn, fromtype);
392         nn  = new_r_Cast(blk, nn, totype);
393         set_irn_typeinfo_type(nn, totype);
394         exchange(phi, nn);
395         return 1;
396 }
397
398 /**
399  * Remove Casted null checks, i.e.
400  *
401  * Cmp(Cast(type, x_orig), NULL_type) -> Cmp(x_orig, NULL_orig)
402  *
403  * @param cmp  the Cmp node
404  *
405  * @return 1 if Cast's where removed
406  */
407 static int remove_Cmp_Null_cast(ir_node *cmp)
408 {
409         ir_graph *irg;
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         irg = get_irn_irg(cmp);
439         set_irn_n(cmp, cast_pos, get_Cast_op(cast));
440         fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
441         new_null = new_r_Const(irg, get_Const_tarval(null));
442         set_irn_typeinfo_type(new_null, fromtype);
443         set_irn_n(cmp, null_pos, new_null);
444         ++n_casts_removed;
445         return 1;
446 }
447
448 /**
449  * Post-Walker: Optimize class casts (mostly by trying to remove them)
450  */
451 static void irn_optimize_class_cast(ir_node *n, void *env)
452 {
453         int *changed = (int*)env;
454
455         if (is_Cast(n))
456                 *changed |= cancel_out_casts(n);
457         else if (is_Sel(n))
458                 *changed |= concretize_selected_entity(n);
459         else if (is_Phi(n))
460                 *changed |= concretize_Phi_type(n);
461         else if (is_Cmp(n))
462                 *changed |= remove_Cmp_Null_cast(n);
463 }
464
465 void optimize_class_casts(void)
466 {
467         int changed;
468
469 #if 0
470         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
471                 simple_analyse_types();
472 #endif
473
474         changed = 0;
475         all_irg_walk(NULL, irn_optimize_class_cast, &changed);
476
477         DB((dbg, SET_LEVEL_1, " Cast optimization: %zu Casts removed, %zu Sels concretized.\n",
478                 n_casts_removed, n_sels_concretized));
479 }
480
481 void firm_init_class_casts_opt(void)
482 {
483         FIRM_DBG_REGISTER(dbg, "firm.opt.tropt");
484 }