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