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