7f0529d60e1ddabd414c0b08ea230c51cc79d8dc
[libfirm] / ir / opt / tropt.c
1 /**
2  * @file tropt.c
3  *
4  * Project:     libFIRM
5  * File name:   ir/opt/tropt.c
6  * Purpose:     Optimize the type representation.
7  * Author:      Goetz Lindenmaier
8  * Modified by:
9  * Created:     20.4.2005
10  * CVS-ID:      $Id$
11  * Copyright:   (c) 2005 Universität Karlsruhe
12  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
13  *
14  * Perform optimizations of the type representation.
15  */
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19
20 #ifdef HAVE_ALLOCA_H
21 #include <alloca.h>
22 #endif
23
24 #ifdef HAVE_MALLOC_H
25 #include <malloc.h>
26 #endif
27
28 #include "tropt.h"
29
30 #include "irprog.h"
31 #include "mangle.h"
32
33 #include "tr_inheritance.h"
34 #include "irtypeinfo.h"
35 #include "irgwalk.h"
36 #include "irsimpletype.h"
37 #include "trouts.h"
38 #include "ircons.h"
39 #include "irgmod.h"
40 #include "irflag.h"
41
42 /* - statistics ---------------------------------------------- */
43
44 static int n_casts_normalized = 0;
45 static int n_casts_removed    = 0;
46 static int n_sels_concretized = 0;
47
48 /* - Cast normalization. ------------------------------------- */
49
50 static type *default_gen_pointer_type_to(type *tp);
51
52 #define PTR_TYPE_SUFFIX "cc_ptr_tp"  /* class cast pointer type. */
53 static ident *ptr_type_suffix = NULL;
54 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
55
56 static type *default_gen_pointer_type_to(type *tp) {
57   type *res = NULL;
58   if (get_trouts_state() == outs_consistent) {
59     if (get_type_n_pointertypes_to(tp) > 0) {
60       res = get_type_pointertype_to(tp, 0);
61     } else {
62       ir_mode *mode = mode_P_data;
63
64       if (is_Method_type(tp))
65         mode = mode_P_code;
66       res = new_type_pointer_mode(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode);
67       /* Update trout for pointertypes, so we can use it in next call. */
68       add_type_pointertype_to(tp, res);
69     }
70   } else {
71     res = find_pointer_type_to_type(tp);
72     if (res == firm_unknown_type)
73       res = new_type_pointer_mode(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode_P_data);
74   }
75
76   return res;
77 }
78
79 /* Return a type that is a depth times pointer to type. */
80 static type *pointerize_type(type *tp, int depth) {
81   for (; depth > 0; --depth) {
82     tp = gen_pointer_type_to(tp);
83   }
84   return tp;
85 }
86
87
88 static ir_node *normalize_values_type(type *totype, ir_node *pred) {
89   type *fromtype = get_irn_typeinfo_type(pred);
90   ir_node *new_cast = pred;
91   int ref_depth = 0;
92
93   if (totype == fromtype) return pred;   /* Case for optimization! */
94
95   while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
96     totype   = get_pointer_points_to_type(totype);
97     fromtype = get_pointer_points_to_type(fromtype);
98     ref_depth++;
99   }
100
101   if (!is_Class_type(totype))   return pred;
102   if (!is_Class_type(fromtype)) return pred;
103
104   if ((get_class_supertype_index(totype, fromtype) != -1) ||
105       (get_class_supertype_index(fromtype, totype) != -1) ) {
106     /* It's just what we want ... */
107     return pred;
108   }
109
110   set_cur_block(get_nodes_block(pred));
111
112   if (is_subclass_of(totype, fromtype)) {
113     /* downcast */
114     while (get_class_subtype_index(fromtype, totype) == -1) {
115       /* Insert a cast to a subtype of fromtype. */
116       type *new_type = NULL;
117       ir_node *new_cast;
118       int i, n_subtypes = get_class_n_subtypes(fromtype);
119       for (i = 0; i < n_subtypes && !new_type; ++i) {
120         type *new_sub = get_class_subtype(fromtype, i);
121         if (is_superclass_of(new_sub, totype))
122           new_type = new_sub;
123       }
124       assert(new_type);
125       fromtype = new_type;
126       new_type = pointerize_type(new_type, ref_depth);
127       new_cast = new_Cast(pred, new_type);
128       pred = new_cast;
129       n_casts_normalized ++;
130       set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
131       if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
132     }
133   }
134   else {
135     assert(is_superclass_of(totype, fromtype));
136     /* upcast */
137     while (get_class_supertype_index(fromtype, totype) == -1) {
138       /* Insert a cast to a supertype of fromtype. */
139       type *new_type = NULL;
140       int i, n_supertypes = get_class_n_supertypes(fromtype);
141       for (i = 0; i < n_supertypes && !new_type; ++i) {
142         type *new_super = get_class_supertype(fromtype, i);
143         if (is_subclass_of(new_super, totype))
144           new_type = new_super;
145       }
146       assert(new_type);
147       fromtype = new_type;
148       new_type = pointerize_type(new_type, ref_depth);
149       new_cast = new_Cast(pred, new_type);
150       pred = new_cast;
151       n_casts_normalized ++;
152       set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
153       if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
154     }
155   }
156   return new_cast;
157 }
158
159
160 static void normalize_irn_class_cast(ir_node *n, void *env) {
161   ir_node *res;
162   if (get_irn_op(n) == op_Cast) {
163     ir_node *pred  = get_Cast_op(n);
164     type *totype   = get_Cast_type(n);
165     res = normalize_values_type(totype, pred);
166     set_Cast_op(n, res);
167   } else if (get_irn_op(n) == op_Call) {
168     int i, n_params = get_Call_n_params(n);
169     type *tp = get_Call_type(n);
170     for (i = 0; i < n_params; ++i) {
171       res = normalize_values_type(get_method_param_type(tp, i), get_Call_param(n, i));
172       set_Call_param(n, i, res);
173     }
174   }
175 }
176
177
178 static void pure_normalize_irg_class_casts(ir_graph *irg) {
179   assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
180          "Cannot normalize irregular casts.");
181   if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
182     verify_irg_class_cast_state(irg);
183     return;
184   }
185
186   irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
187   set_irg_class_cast_state(irg, ir_class_casts_normalized);
188 }
189
190
191 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct) {
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   int i, n_irgs = get_irp_n_irgs();
206   if (gppt_fct) gen_pointer_type_to = gppt_fct;
207
208   if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
209     simple_analyse_types();
210
211   for (i = 0; i < n_irgs; ++i) {
212     pure_normalize_irg_class_casts(get_irp_irg(i));
213   }
214
215   set_irp_class_cast_state(ir_class_casts_normalized);
216   gen_pointer_type_to = default_gen_pointer_type_to;
217
218   if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
219     printf(" Cast normalization: %d Casts inserted.\n", n_casts_normalized);
220   }
221 }
222
223
224
225 /* - Cast optimization. ------------------------------------------- */
226
227 static void cancel_out_casts(ir_node *cast) {
228   ir_node *orig, *pred = get_Cast_op(cast);
229   type *tp_cast, *tp_pred, *tp_orig;
230   int ref_depth = 0;
231
232   if (get_irn_op(pred) != op_Cast) return;
233   orig = get_Cast_op(pred);
234
235   tp_cast = get_Cast_type(cast);
236   tp_pred = get_Cast_type(pred);
237   tp_orig = get_irn_typeinfo_type(orig);
238
239   while (is_Pointer_type(tp_cast) &&
240          is_Pointer_type(tp_pred) &&
241          is_Pointer_type(tp_orig)   ) {
242     tp_cast = get_pointer_points_to_type(tp_cast);
243     tp_pred = get_pointer_points_to_type(tp_pred);
244     tp_orig = get_pointer_points_to_type(tp_orig);
245     ref_depth++;
246   }
247
248   if (!is_Class_type(tp_cast)) return;
249   if (!is_Class_type(tp_pred)) return;
250   if (!is_Class_type(tp_orig)) return;
251
252   if (is_subclass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
253     return;
254
255   if (tp_cast == tp_orig) {
256     exchange(cast, orig);
257     n_casts_removed += 2;
258     return;
259   }
260
261   if (!(is_subclass_of  (tp_cast, tp_orig) || is_subclass_of  (tp_orig, tp_cast)))
262     /* Avoid (B2)(A)(new B1()) --> (B2)(new B1()) */
263     return;
264
265   if ((is_subclass_of  (tp_cast, tp_pred) && is_superclass_of(tp_pred, tp_orig)) ||
266       (is_superclass_of(tp_cast, tp_pred) && is_subclass_of  (tp_pred, tp_orig))   ) {
267     set_Cast_op (cast, orig);
268     n_casts_removed ++;
269   }
270 }
271
272 static void concretize_selected_entity(ir_node *sel) {
273   ir_node *cast, *ptr = get_Sel_ptr(sel);
274   type *orig_tp, *cast_tp;
275   entity *new_ent, *sel_ent;
276
277   sel_ent = get_Sel_entity(sel);
278   cast = get_Sel_ptr(sel);
279
280   while (get_irn_op(cast) == op_Cast) {
281     cast_tp = get_Cast_type(cast);
282     ptr = get_Cast_op(cast);
283     orig_tp = get_irn_typeinfo_type(ptr);
284
285     if (!is_Pointer_type(orig_tp)) return;
286     if (!is_Pointer_type(cast_tp)) return;
287     orig_tp = get_pointer_points_to_type(orig_tp);
288     cast_tp = get_pointer_points_to_type(cast_tp);
289     if (!is_Class_type(orig_tp)) return;
290     if (!is_Class_type(cast_tp)) return;
291
292     /* We only want to contretize, but not generalize. */
293     if (!is_superclass_of(cast_tp, orig_tp)) return;
294
295     /* Hmm, we are not properly typed. */
296     if (get_class_member_index(cast_tp, sel_ent) == -1) return;
297
298     new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
299
300     /* New ent must be member of orig_tp. */
301     if (get_class_member_index(orig_tp, new_ent) == -1) return;
302
303     set_Sel_entity(sel, new_ent);
304     set_Sel_ptr(sel, ptr);
305     n_sels_concretized++;
306
307     sel_ent = new_ent;
308     cast = ptr;
309   }
310 }
311
312 static void concretize_Phi_type(ir_node *phi) {
313   int i, n_preds = get_Phi_n_preds(phi);
314   ir_node **pred = alloca(n_preds * sizeof(ir_node *));
315   ir_node *new;
316   type *totype, *fromtype;
317
318   if (n_preds == 0) return;
319   pred[0] = get_Phi_pred(phi, 0);
320
321   if (get_irn_op(pred[0]) != op_Cast) return;
322
323   if (!is_Cast_upcast(pred[0])) return;
324
325   fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
326   totype   = get_Cast_type(pred[0]);
327
328   pred[0] = get_Cast_op(pred[0]);
329   for (i = 1; i < n_preds; ++i) {
330     pred[i] = get_Phi_pred(phi, i);
331     if (get_irn_op(pred[i]) != op_Cast) return;
332     if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype) return;
333     pred[i] = get_Cast_op(pred[i]);
334   }
335
336   /* Transform Phi */
337   set_cur_block(get_nodes_block(phi));
338   new = new_Phi(n_preds, pred, get_irn_mode(phi));
339   set_irn_typeinfo_type(new, fromtype);
340   new = new_Cast(new, totype);
341   set_irn_typeinfo_type(new, totype);
342   exchange(phi, new);
343 }
344
345 void remove_Cmp_Null_cast(ir_node *cmp) {
346   ir_node *cast, *null, *new_null;
347   int cast_pos, null_pos;
348   type *fromtype;
349
350   cast = get_Cmp_left(cmp);
351   cast_pos = 0;
352   if (get_irn_op(cast) != op_Cast) {
353     null = cast;
354     null_pos = cast_pos;
355     cast = get_Cmp_right(cmp);
356     cast_pos = 1;
357     if (get_irn_op(cast) != op_Cast) return;
358   } else {
359     null = get_Cmp_right(cmp);
360     null_pos = 1;
361   }
362
363   if (get_irn_op(null) != op_Const) return;
364   if (!mode_is_reference(get_irn_mode(null))) return;
365   if (get_Const_tarval(null) != get_mode_null(get_irn_mode(null))) return;
366
367   /* Transform Cmp */
368   set_irn_n(cmp, cast_pos, get_Cast_op(cast));
369   fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
370   new_null = new_Const_type(get_Const_tarval(null), fromtype);
371   set_irn_typeinfo_type(new_null, fromtype);
372   set_irn_n(cmp, null_pos, new_null);
373   n_casts_removed ++;
374 }
375
376 static void irn_optimize_class_cast(ir_node *n, void *env) {
377   if (get_irn_op(n) == op_Cast)
378     cancel_out_casts(n);
379   else if (get_irn_op(n) == op_Sel)
380     concretize_selected_entity(n);
381   else if (get_irn_op(n) == op_Phi)
382     concretize_Phi_type(n);
383   else if (get_irn_op(n) == op_Cmp)
384     remove_Cmp_Null_cast(n);
385 }
386
387 void optimize_class_casts(void) {
388   int i, n_irgs = get_irp_n_irgs();
389
390   if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
391     simple_analyse_types();
392
393   all_irg_walk(NULL, irn_optimize_class_cast, NULL);
394
395   set_trouts_inconsistent();
396   for (i = 0; i < n_irgs; ++i)
397     set_irg_outs_inconsistent(get_irp_irg(i));
398
399   if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
400     printf(" Cast optimization: %d Casts removed, %d sels concretized.\n",
401            n_casts_removed, n_sels_concretized);
402   }
403 }